Home | History | Annotate | Download | only in sanitizer
      1 //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a part of AddressSanitizer.
     11 //
     12 // Public interface header.
     13 //===----------------------------------------------------------------------===//
     14 #ifndef SANITIZER_ASAN_INTERFACE_H
     15 #define SANITIZER_ASAN_INTERFACE_H
     16 
     17 #include <sanitizer/common_interface_defs.h>
     18 
     19 #ifdef __cplusplus
     20 extern "C" {
     21 #endif
     22   // Marks memory region [addr, addr+size) as unaddressable.
     23   // This memory must be previously allocated by the user program. Accessing
     24   // addresses in this region from instrumented code is forbidden until
     25   // this region is unpoisoned. This function is not guaranteed to poison
     26   // the whole region - it may poison only subregion of [addr, addr+size) due
     27   // to ASan alignment restrictions.
     28   // Method is NOT thread-safe in the sense that no two threads can
     29   // (un)poison memory in the same memory region simultaneously.
     30   void __asan_poison_memory_region(void const volatile *addr, size_t size);
     31   // Marks memory region [addr, addr+size) as addressable.
     32   // This memory must be previously allocated by the user program. Accessing
     33   // addresses in this region is allowed until this region is poisoned again.
     34   // This function may unpoison a superregion of [addr, addr+size) due to
     35   // ASan alignment restrictions.
     36   // Method is NOT thread-safe in the sense that no two threads can
     37   // (un)poison memory in the same memory region simultaneously.
     38   void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
     39 
     40 // User code should use macros instead of functions.
     41 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
     42 #define ASAN_POISON_MEMORY_REGION(addr, size) \
     43   __asan_poison_memory_region((addr), (size))
     44 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
     45   __asan_unpoison_memory_region((addr), (size))
     46 #else
     47 #define ASAN_POISON_MEMORY_REGION(addr, size) \
     48   ((void)(addr), (void)(size))
     49 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
     50   ((void)(addr), (void)(size))
     51 #endif
     52 
     53   // Returns 1 if addr is poisoned (i.e. 1-byte read/write access to this
     54   // address will result in error report from AddressSanitizer).
     55   // Otherwise returns 0.
     56   int __asan_address_is_poisoned(void const volatile *addr);
     57 
     58   // If at least on byte in [beg, beg+size) is poisoned, return the address
     59   // of the first such byte. Otherwise return 0.
     60   void *__asan_region_is_poisoned(void *beg, size_t size);
     61 
     62   // Print the description of addr (useful when debugging in gdb).
     63   void __asan_describe_address(void *addr);
     64 
     65   // This is an internal function that is called to report an error.
     66   // However it is still a part of the interface because users may want to
     67   // set a breakpoint on this function in a debugger.
     68   void __asan_report_error(void *pc, void *bp, void *sp,
     69                            void *addr, int is_write, size_t access_size);
     70 
     71   // Sets the exit code to use when reporting an error.
     72   // Returns the old value.
     73   int __asan_set_error_exit_code(int exit_code);
     74 
     75   // Sets the callback to be called right before death on error.
     76   // Passing 0 will unset the callback.
     77   void __asan_set_death_callback(void (*callback)(void));
     78 
     79   void __asan_set_error_report_callback(void (*callback)(const char*));
     80 
     81   // User may provide function that would be called right when ASan detects
     82   // an error. This can be used to notice cases when ASan detects an error, but
     83   // the program crashes before ASan report is printed.
     84   void __asan_on_error();
     85 
     86   // Returns the estimated number of bytes that will be reserved by allocator
     87   // for request of "size" bytes. If ASan allocator can't allocate that much
     88   // memory, returns the maximal possible allocation size, otherwise returns
     89   // "size".
     90   /* DEPRECATED: Use __sanitizer_get_estimated_allocated_size instead. */
     91   size_t __asan_get_estimated_allocated_size(size_t size);
     92 
     93   // Returns 1 if p was returned by the ASan allocator and is not yet freed.
     94   // Otherwise returns 0.
     95   /* DEPRECATED: Use __sanitizer_get_ownership instead. */
     96   int __asan_get_ownership(const void *p);
     97 
     98   // Returns the number of bytes reserved for the pointer p.
     99   // Requires (get_ownership(p) == true) or (p == 0).
    100   /* DEPRECATED: Use __sanitizer_get_allocated_size instead. */
    101   size_t __asan_get_allocated_size(const void *p);
    102 
    103   // Number of bytes, allocated and not yet freed by the application.
    104   /* DEPRECATED: Use __sanitizer_get_current_allocated_bytes instead. */
    105   size_t __asan_get_current_allocated_bytes();
    106 
    107   // Number of bytes, mmaped by asan allocator to fulfill allocation requests.
    108   // Generally, for request of X bytes, allocator can reserve and add to free
    109   // lists a large number of chunks of size X to use them for future requests.
    110   // All these chunks count toward the heap size. Currently, allocator never
    111   // releases memory to OS (instead, it just puts freed chunks to free lists).
    112   /* DEPRECATED: Use __sanitizer_get_heap_size instead. */
    113   size_t __asan_get_heap_size();
    114 
    115   // Number of bytes, mmaped by asan allocator, which can be used to fulfill
    116   // allocation requests. When a user program frees memory chunk, it can first
    117   // fall into quarantine and will count toward __asan_get_free_bytes() later.
    118   /* DEPRECATED: Use __sanitizer_get_free_bytes instead. */
    119   size_t __asan_get_free_bytes();
    120 
    121   // Number of bytes in unmapped pages, that are released to OS. Currently,
    122   // always returns 0.
    123   /* DEPRECATED: Use __sanitizer_get_unmapped_bytes instead. */
    124   size_t __asan_get_unmapped_bytes();
    125 
    126   // Prints accumulated stats to stderr. Used for debugging.
    127   void __asan_print_accumulated_stats();
    128 
    129   // This function may be optionally provided by user and should return
    130   // a string containing ASan runtime options. See asan_flags.h for details.
    131   const char* __asan_default_options();
    132 
    133   // Malloc hooks that may be optionally provided by user.
    134   // __asan_malloc_hook(ptr, size) is called immediately after
    135   //   allocation of "size" bytes, which returned "ptr".
    136   // __asan_free_hook(ptr) is called immediately before
    137   //   deallocation of "ptr".
    138   /* DEPRECATED: Use __sanitizer_malloc_hook / __sanitizer_free_hook instead. */
    139   void __asan_malloc_hook(void *ptr, size_t size);
    140   void __asan_free_hook(void *ptr);
    141 
    142   // The following 2 functions facilitate garbage collection in presence of
    143   // asan's fake stack.
    144 
    145   // Returns an opaque handler to be used later in __asan_addr_is_in_fake_stack.
    146   // Returns NULL if the current thread does not have a fake stack.
    147   void *__asan_get_current_fake_stack();
    148 
    149   // If fake_stack is non-NULL and addr belongs to a fake frame in
    150   // fake_stack, returns the address on real stack that corresponds to
    151   // the fake frame and sets beg/end to the boundaries of this fake frame.
    152   // Otherwise returns NULL and does not touch beg/end.
    153   // If beg/end are NULL, they are not touched.
    154   // This function may be called from a thread other than the owner of
    155   // fake_stack, but the owner thread need to be alive.
    156   void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
    157                                      void **end);
    158 
    159 #ifdef __cplusplus
    160 }  // extern "C"
    161 #endif
    162 
    163 #endif  // SANITIZER_ASAN_INTERFACE_H
    164