Home | History | Annotate | Download | only in Posix
      1 // Test for ASAN_OPTIONS=start_deactivated=1 mode.
      2 // Main executable is uninstrumented, but linked to ASan runtime. The shared
      3 // library is instrumented. Memory errors before dlopen are not detected.
      4 
      5 // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
      6 // RUN: %clangxx -O0 %s -c -o %t.o
      7 // RUN: %clangxx_asan -O0 %t.o -ldl -o %t
      8 // RUN: ASAN_OPTIONS=start_deactivated=1 not %run %t 2>&1 | FileCheck %s
      9 // XFAIL: arm-linux-gnueabi
     10 
     11 #if !defined(SHARED_LIB)
     12 #include <dlfcn.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 #include <unistd.h>
     17 
     18 #include <string>
     19 
     20 #include "sanitizer/asan_interface.h"
     21 
     22 void test_malloc_shadow() {
     23   char *p = (char *)malloc(100);
     24   char *q = (char *)__asan_region_is_poisoned(p + 95, 8);
     25   fprintf(stderr, "=%zd=\n", q ? q - (p + 95) : -1);
     26   free(p);
     27 }
     28 
     29 typedef void (*Fn)();
     30 
     31 int main(int argc, char *argv[]) {
     32   test_malloc_shadow();
     33   // CHECK: =-1=
     34 
     35   std::string path = std::string(argv[0]) + "-so.so";
     36   void *dso = dlopen(path.c_str(), RTLD_NOW);
     37   if (!dso) {
     38     fprintf(stderr, "dlopen failed: %s\n", dlerror());
     39     return 1;
     40   }
     41 
     42   test_malloc_shadow();
     43   // CHECK: =5=
     44 
     45   void *fn = dlsym(dso, "do_another_bad_thing");
     46   if (!fn) {
     47     fprintf(stderr, "dlsym failed: %s\n", dlerror());
     48     return 1;
     49   }
     50 
     51   ((Fn)fn)();
     52   // CHECK: AddressSanitizer: heap-buffer-overflow
     53   // CHECK: READ of size 1
     54   // CHECK: {{#0 .* in do_another_bad_thing}}
     55   // CHECK: is located 5 bytes to the right of 100-byte region
     56   // CHECK: in do_another_bad_thing
     57 
     58   return 0;
     59 }
     60 #else  // SHARED_LIB
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 
     64 extern "C" void do_another_bad_thing() {
     65   char *volatile p = (char *)malloc(100);
     66   printf("%hhx\n", p[105]);
     67 }
     68 #endif  // SHARED_LIB
     69