stack-trace-dlclose.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clangxx_asan -DSHARED %s -shared -o %T/stack_trace_dlclose.so -fPIC
2// RUN: %clangxx_asan -DSO_DIR=\"%T\" %s -o %t
3// RUN: ASAN_OPTIONS=exitcode=0 %run %t 2>&1 | FileCheck %s
4
5#include <assert.h>
6#include <dlfcn.h>
7#include <stdlib.h>
8#include <stdio.h>
9#include <unistd.h>
10
11#include <sanitizer/common_interface_defs.h>
12
13#ifdef SHARED
14extern "C" {
15void *foo() {
16  return malloc(1);
17}
18}
19#else
20void *handle;
21
22int main(int argc, char **argv) {
23  void *handle = dlopen(SO_DIR "/stack_trace_dlclose.so", RTLD_LAZY);
24  assert(handle);
25  void *(*foo)() = (void *(*)())dlsym(handle, "foo");
26  assert(foo);
27  void *p = foo();
28  assert(p);
29  dlclose(handle);
30
31  free(p);
32  free(p);  // double-free
33
34  return 0;
35}
36#endif
37
38// CHECK: {{    #0 0x.* in malloc}}
39// CHECK: {{    #1 0x.* \(<unknown module>\)}}
40// CHECK: {{    #2 0x.* in main}}
41