Bug 18136 - Different semantics between atexit and destructor functions
Summary: Different semantics between atexit and destructor functions
Status: NEW
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.19
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-17 16:22 UTC by philipp.waehnert
Modified: 2024-10-18 16:54 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:
fweimer: security-


Attachments
Described example (656 bytes, application/x-bzip)
2015-03-17 16:22 UTC, philipp.waehnert
Details

Note You need to log in before you can comment on or make changes to this bug.
Description philipp.waehnert 2015-03-17 16:22:25 UTC
Created attachment 8191 [details]
Described example

I have a DSO, let us call it libframework.so, which dynamically loads a second DSO, let's call it libplugin.so. If libframework.so unloads the loaded libplugin.so at its end of lifetime, it matters whether this unloading function is registered with atexit() or is decorated with __attribute__((destructor)):

$ cat framework.c
#include <stddef.h>

#include <dlfcn.h>

static void *handle = NULL;

__attribute__((destructor))
static void close_plugin() {
    dlclose(handle);
}

void func() {
    handle = dlopen("./libplugin.so", RTLD_LOCAL | RTLD_NOW);
    /* atexit(close_plugin); */
    void (*gunc)() = dlsym(handle, "gunc");
    gunc();
}

void hunc() {
   puts("Hello World from the framework");
}

$ cat plugin.c
extern void hunc();

void gunc() {
    puts("Hello World from the plugin");
    hunc();
}

$ cat client.c
#include <dlfcn.h>

int main() {
    void *handle = dlopen("./libframework.so", RTLD_GLOBAL | RTLD_NOW);
    void (*func)() = dlsym(handle, "func");
    func();
    dlclose(handle);
    return 0;
}

$cat Makefile
all: libplugin.so libframework.so client

libplugin.so: plugin.c
	gcc -g -shared -fPIC -o libplugin.so plugin.c

libframework.so: framework.c
	gcc -g -shared -fPIC -o libframework.so framework.c -ldl

client: client.c
	gcc -g -o client client.c -ldl

clean:
	rm -f client libplugin.so libframework.so

The implementation above works quite well. Although the client can't unload libframework.so since the still loaded libplugin.so uses a symbol from there, at least at the end of the process the runtime environment unloads all DSOs correctly.

If we now remove the __attribute__((destructor)) and use atexit() instead, the client still can't unload libframework.so but in addition at the end of the process the entire thing gets crashed with a segfault.

As far as I've analysed the difference, the reason lies in the fact that _dl_fini() in libc/elf/dl-fini.c does a "bump l_direct_opencount of all objects so that they are not dlclose()ed from underneath us." (as a comment in line 201 says). This prevents the premature unloading of libframework.so. Functions decorated with __attribute__((destructor)) will be called by _dl_fini().

Functions registered with atexit() are called directly in __run_exit_handlers() in libc/stdlib/exit.c and do not have a similar "bumping".
Comment 1 jsm-csl@polyomino.org.uk 2015-03-17 18:12:04 UTC
If you want a function to be called on unload rather than exit if unload 
happens first, use __cxa_atexit (note the extra arguments) rather than 
atexit.
Comment 2 jsm-csl@polyomino.org.uk 2015-03-17 18:17:50 UTC
It's not clear how well-defined the interaction of atexit with unloading 
dynamic objects is.  Do you get the problem if you use __cxa_atexit 
instead?
Comment 3 philipp.waehnert 2015-03-18 09:11:36 UTC
Concerning the backtraces these three approaches are very similar:

__attribute__((destructor)):
#0  close_plugin (p=0x7ffff7632040) at framework.c:11
#1  0x00007ffff7deb0ea in _dl_fini () at dl-fini.c:252
#2  0x00007ffff786cca9 in __run_exit_handlers (status=0, 
    listp=0x7ffff7bd25a8 <__exit_funcs>, 
    run_list_atexit=run_list_atexit@entry=true) at exit.c:82
#3  0x00007ffff786ccf5 in __GI_exit (status=<optimized out>) at exit.c:104
#4  0x00007ffff7856a6c in __libc_start_main (main=0x40069d <main>, argc=1, 
    argv=0x7fffffffdf78, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffdf68) at libc-start.c:319
#5  0x00000000004005d9 in _start ()

atexit():
#0  close_plugin () at framework.c:11
#1  0x00007ffff786cca9 in __run_exit_handlers (status=0, 
    listp=0x7ffff7bd25a8 <__exit_funcs>, 
    run_list_atexit=run_list_atexit@entry=true) at exit.c:82
#2  0x00007ffff786ccf5 in __GI_exit (status=<optimized out>) at exit.c:104
#3  0x00007ffff7856a6c in __libc_start_main (main=0x40069d <main>, argc=1, 
    argv=0x7fffffffdf78, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffdf68) at libc-start.c:319
#4  0x00000000004005d9 in _start ()

__cxa_atexit():
#0  close_plugin (p=0x602030) at framework.c:11
#1  0x00007ffff786cca9 in __run_exit_handlers (status=0, 
    listp=0x7ffff7bd25a8 <__exit_funcs>, 
    run_list_atexit=run_list_atexit@entry=true) at exit.c:82
#2  0x00007ffff786ccf5 in __GI_exit (status=<optimized out>) at exit.c:104
#3  0x00007ffff7856a6c in __libc_start_main (main=0x40069d <main>, argc=1, 
    argv=0x7fffffffdf78, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffdf68) at libc-start.c:319
#4  0x00000000004005d9 in _start ()

The first one has this extra call to _dl_fini() and this makes exactly the difference.

I agree, it seems to be a bad idea to unload dynamically loaded libraries inside such functions. It would be better, if libframework.so exposed close_plugin() and the client is in charge to remind libframework.so to clean up. But my problem stems from a historically grown code base and under C++ these flawed approaches seem way more natural: The same situation arises if the class loading the plugins is a singleton and unloads them in its destructor.
Comment 4 account disabled by myself since useless 2018-03-28 12:17:51 UTC
Related to bugs 21031, 21032.