15d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316
25d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// XFAIL: android
35d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines//
42d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx_asan -DSHARED %s -shared -o %T/stack_trace_dlclose.so -fPIC
52d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx_asan -DSO_DIR=\"%T\" %s -o %t
62d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: ASAN_OPTIONS=exitcode=0 %run %t 2>&1 | FileCheck %s
75d71de26cedae3dafc17449fe0182045c0bd20e8Stephen Hines// XFAIL: arm-linux-gnueabi
82d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
92d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <assert.h>
102d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <dlfcn.h>
112d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <stdlib.h>
122d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <stdio.h>
132d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <unistd.h>
142d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
152d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <sanitizer/common_interface_defs.h>
162d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
172d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#ifdef SHARED
182d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesextern "C" {
192d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesvoid *foo() {
202d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  return malloc(1);
212d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines}
222d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines}
232d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#else
242d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesvoid *handle;
252d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
262d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesint main(int argc, char **argv) {
272d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  void *handle = dlopen(SO_DIR "/stack_trace_dlclose.so", RTLD_LAZY);
282d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  assert(handle);
292d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  void *(*foo)() = (void *(*)())dlsym(handle, "foo");
302d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  assert(foo);
312d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  void *p = foo();
322d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  assert(p);
332d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  dlclose(handle);
342d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
352d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  free(p);
362d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  free(p);  // double-free
372d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
382d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines  return 0;
392d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines}
402d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#endif
412d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
422d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// CHECK: {{    #0 0x.* in malloc}}
432d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// CHECK: {{    #1 0x.* \(<unknown module>\)}}
442d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// CHECK: {{    #2 0x.* in main}}
45