Home | History | Annotate | Download | only in pyautolib
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // Fake Address Sanitizer run-time support.
      6 // Enough for programs to link and run, but will not find any errors.
      7 // Also, linking this to shared libraries voids the warranty.
      8 //
      9 // We need this fake thunks if we build Chrome with ASAN support because
     10 // pyautolib DSO is instrumented with ASAN and is loaded to regular python
     11 // process that has no ASAN runtime.
     12 //
     13 // We have three options here:
     14 // 1) use our custom build of Python that have ASAN runtime linked in,
     15 // 2) do not instrument pyautolib with ASAN support,
     16 // 3) use this fake asan thunks linked to pyautolib so that instrumented code
     17 //    does not complain.
     18 //
     19 // Note that we should not use real ASAN runtime linked with DSO because it will
     20 // not have information about memory allocations made prior to DSO load.
     21 //
     22 // Option (2) is not easy because pyautolib uses code from Chrome
     23 // (see chrome_tests.gypi, dependencies for target_name: pyautolib) that
     24 // has been instrumented with ASAN. So even if we disable -sanitize=address
     25 // for pyautolib own sources, ASAN instrumentation will creep in from there.
     26 // To avoid ASAN instrumentation, we might force Chrome build to compile all our
     27 // dependencies one more time without -fsanitize=address.
     28 //
     29 // Note also that using these empty stubs prevents ASAN from catching bugs in
     30 // Python-pyautolib process. But we do not need it, we are interested in Chrome
     31 // bugs.
     32 
     33 
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <sys/mman.h>
     37 
     38 void __asan_init() {
     39   static int inited = 0;
     40   if (inited) return;
     41   inited = 1;
     42 #if __WORDSIZE == 64
     43   unsigned long start = 0x100000000000;
     44   unsigned long size  = 0x100000000000;
     45 #else
     46   unsigned long start = 0x20000000;
     47   unsigned long size = 0x20000000;
     48 #endif
     49   void *res = mmap((void*)start, size,
     50                    PROT_READ | PROT_WRITE,
     51                    MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
     52                    0, 0);
     53   if (res == (void*)start) {
     54     fprintf(stderr, "Fake AddressSanitizer run-time initialized ok at %p\n",
     55             res);
     56   } else {
     57     fprintf(stderr, "Fake AddressSanitizer run-time failed to initialize.\n"
     58             "You have been warned. Aborting.");
     59     abort();
     60   }
     61 }
     62 
     63 // Update the name when asan api updates.
     64 void __asan_init_v1() {
     65   __asan_init();
     66 }
     67 
     68 void __asan_handle_no_return() { }
     69 void __asan_register_globals() { }
     70 void __asan_report_load1() { }
     71 void __asan_report_load16() { }
     72 void __asan_report_load2() { }
     73 void __asan_report_load4() { }
     74 void __asan_report_load8() { }
     75 void __asan_report_load_n() { }
     76 void __asan_report_store1() { }
     77 void __asan_report_store16() { }
     78 void __asan_report_store2() { }
     79 void __asan_report_store4() { }
     80 void __asan_report_store8() { }
     81 void __asan_report_store_n() { }
     82 void __asan_set_error_report_callback() { }
     83 void __asan_unregister_globals() { }
     84 void __sanitizer_sandbox_on_notify() { }
     85