From owner-freebsd-hackers@FreeBSD.ORG Mon Dec 31 19:26:28 2007 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0B1F416A41A for ; Mon, 31 Dec 2007 19:26:28 +0000 (UTC) (envelope-from kabaev@gmail.com) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.246]) by mx1.freebsd.org (Postfix) with ESMTP id B625713C4CE for ; Mon, 31 Dec 2007 19:26:27 +0000 (UTC) (envelope-from kabaev@gmail.com) Received: by an-out-0708.google.com with SMTP id c14so990372anc.13 for ; Mon, 31 Dec 2007 11:26:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:date:from:to:cc:subject:message-id:in-reply-to:references:x-mailer:mime-version:content-type; bh=qO3B/461Q3YkUOUgOkBmjDpZ6muAyY7na1DggY3H/Xg=; b=X0Ha9HGxroJnR+I+R0mcvvLVlFSosceklou/81LWIgyDMgP5ZJqZLl5ZH0OE4QMreNvjdi9lmfu5ZgJCMaPcx3DEesJRjqAUeg3Lc8I8tSh1GYeNhPK/Is3X7mKMKFFvBJI8zVwZHsyReFfje2yGxUsQ+V1mZGWX7QOmEg6h06o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:to:cc:subject:message-id:in-reply-to:references:x-mailer:mime-version:content-type; b=p2na/sawkUZXk4puoOCHeDL3MZdTl1mwFRyCw9rFa/xriJlpV4nsOIoChmxEXYc4fjRr87MvJDZAg5KTgI3+Xywnbh1VQrv87UoVeI8OIuuboSxh1nhOZsu2MGn6uEw4wafqLULPyw8uoYrlS8qD08vQQttWUkjpNSDgGk31Cdk= Received: by 10.101.1.7 with SMTP id d7mr26456494ani.91.1199129186992; Mon, 31 Dec 2007 11:26:26 -0800 (PST) Received: from kan.dnsalias.net ( [24.218.183.247]) by mx.google.com with ESMTPS id o29sm20383089elf.14.2007.12.31.11.26.25 (version=SSLv3 cipher=OTHER); Mon, 31 Dec 2007 11:26:26 -0800 (PST) Date: Mon, 31 Dec 2007 14:26:20 -0500 From: Alexander Kabaev To: "Markus Hoenicka" Message-ID: <20071231142620.39f2fbd2@kan.dnsalias.net> In-Reply-To: <18297.6718.750894.937199@yeti.mininet> References: <18297.6718.750894.937199@yeti.mininet> X-Mailer: Claws Mail 3.0.2 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: multipart/signed; boundary="Sig_/GwOo5uNn_S_7+PXNyx5=9Pm"; protocol="application/pgp-signature"; micalg=PGP-SHA1 Cc: freebsd-hackers@freebsd.org Subject: Re: dlopen(), atexit() crash on FreeBSD (testcase included) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Dec 2007 19:26:28 -0000 --Sig_/GwOo5uNn_S_7+PXNyx5=9Pm Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable On Mon, 31 Dec 2007 17:35:10 +0100 "Markus Hoenicka" wrote: > Hi, >=20 > I've been redirected by Giorgos Keramidas to this list after reporting > a problem on the freebsd-questions list. I'd greatly appreciate if you > could have a look at the following problem. Apparently programs are > doomed to segfault on FreeBSD if dlopen()ed modules install exit > handlers via atexit(). Similar problem reports have cropped up before, > see e.g. >=20 > http://www.imagemagick.org/pipermail/magick-developers/2006-March/002523.= html >=20 > My system runs: >=20 > FreeBSD yeti.mininet 6.1-RELEASE FreeBSD 6.1-RELEASE #1: Mon Aug 28 > 22:24:48 CEST 2006 > markus@yeti.mininet:/usr/src/sys/i386/compile/YETI i386 >=20 > I'm one of the developers of libdbi, a database abstraction layer for > C, see http://libdbi.sourceforge.net. >=20 > libdbi is a library for programs which are supposed to be able to > access different database engines with a unified API. libdbi > essentially maps generic API calls to the specific database client > library calls of a particular database engine. To do this, libdbi > loads available database drivers at runtime via dlopen() calls. Each > of these drivers is linked against one database client > library. E.g. the Firebird driver is linked against > libfbclient.so. When libdbi is properly shut down, it unloads all > loaded drivers by calling dlclose() on each of them. >=20 > This design works well on all supported platforms and with all > supported database engines, with one exception: the Firebird driver on > FreeBSD invariably causes a segfault when the application linked > against libdbi exits: >=20 > #0 0x28514fe4 in ?? () > #1 0x281507c3 in __cxa_finalize () from /lib/libc.so.6 > #2 0x281503fe in exit () from /lib/libc.so.6 > #3 0x0804a40f in main (argc=3D1, argv=3D0xbfbfe754) at test_dbi.c:419 >=20 > The reason appears to be that the Firebird client libraries install > exit handlers via atexit(). Remember that due to libdbi's design to > load all available drivers whether or not they are used later, libdbi > will cause a crash even if no Firebird database is accessed - it is > sufficient that the driver has been loaded. As per Giorgos' suggestion > it is simple to circumvent this segfault by avoiding the call to > dlclose() before exiting, but I wonder whether there is a more robust > solution for this problem. >=20 > The attached minimal testcase is sufficient to illustrate the > problem. atexitmod.c defines a module which is loaded by datest.c Make > sure to fix the hardcoded path in datest.c before building the app. To > build the test program and watch it crash, do the following: >=20 > gcc -shared -o atexitmod.so atexitmod.c > gcc -o datest datest.c > ./datest >=20 > Commenting out either the atexit() call in atexitmod.c or the > dlclose() call in datest.c prevent the segfault. >=20 > If you find some solution, please cc me as I'm not subscribed to > freebsd-hackers. >=20 > regards, > Markus As designed. atexit should not be used by shared objects that do not expect themselves to live until actual exit() happens. ELF provides proper _init/_fini sections to support shared object initialization/destruction. --=20 Alexander Kabaev --Sig_/GwOo5uNn_S_7+PXNyx5=9Pm Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (FreeBSD) iD8DBQFHeUJcQ6z1jMm+XZYRAiCzAJ94kVQ5yrRdhSdtjxzrhHZKPK3awACcDrSU q6TUk0RsoiMf0oN/S73q0nE= =9at7 -----END PGP SIGNATURE----- --Sig_/GwOo5uNn_S_7+PXNyx5=9Pm--