NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/53805: dlclose(3) does not interact correctly with atexit(3) callbacks



>Number:         53805
>Category:       lib
>Synopsis:       dlclose(3) does not interact correctly with atexit(3) callbacks
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Dec 20 17:10:00 +0000 2018
>Originator:     Kamil Rytarowski
>Release:        NetBSD 8.99.27 amd64
>Organization:
TNF
>Environment:
NetBSD chieftec 8.99.27 NetBSD 8.99.27 (GENERIC) #6: Mon Dec 17 10:44:49 CET 2018  root@chieftec:/public/netbsd-root/sys/arch/amd64/compile/GENERIC amd64
>Description:
atexit(3) callback registered in DSO misbehaves if DSO is dlclose(3)d.

 - atexit(3) callback registered in DSO is not called upon dlclose(3),
 - atexit(3) callback registered in DSO is called on program termination, even if dlclose(3)d was called and this causes crash.

On Linux and likely others (Darwin, FreeBSD, OpenBSD, Solaris, etc) this works as expected as this is triggered by check-profile from LLVM compiler-rt test: instrprof-dlopen-dlclose-gcov.test
>How-To-Repeat:
$  cat dso.c                                                                                       
#include <stdlib.h>

static void A(void)
{
}

void func(int K) {}

__attribute__ ((constructor (0)))
void __llvm_gcov_init(void)
{
        atexit(A);
}
$ clang -m64 -g -O0 -o func.shared -fPIC -shared dso.c

$ cat main.c                                                                                       
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  dlerror();
  void *f1_handle = dlopen("./func.shared", RTLD_LAZY | RTLD_GLOBAL);
  if (f1_handle == NULL) {
    fprintf(stderr, "unable to open 'func.shared': %s\n", dlerror());
    return EXIT_FAILURE;
  }

  if (dlclose(f1_handle) != 0) {
    fprintf(stderr, "unable to close 'func.shared': %s\n", dlerror());
    return EXIT_FAILURE;
  }

  return 0;
}
$ clang -m64 -g -O0 -o main main.c                      
$ ./main                                                                                           
Memory fault (core dumped) 
>Fix:
Expected fix: call atexit(3) callbacks upon dlclose(3).



Home | Main Index | Thread Index | Old Index