xref: /aosp_15_r20/external/compiler-rt/include/sanitizer/common_interface_defs.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- sanitizer/common_interface_defs.h -----------------------*- C++ -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // Common part of the public sanitizer interface.
11*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot #ifndef SANITIZER_COMMON_INTERFACE_DEFS_H
14*7c3d14c8STreehugger Robot #define SANITIZER_COMMON_INTERFACE_DEFS_H
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot #include <stddef.h>
17*7c3d14c8STreehugger Robot #include <stdint.h>
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot // GCC does not understand __has_feature.
20*7c3d14c8STreehugger Robot #if !defined(__has_feature)
21*7c3d14c8STreehugger Robot # define __has_feature(x) 0
22*7c3d14c8STreehugger Robot #endif
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot #ifdef __cplusplus
25*7c3d14c8STreehugger Robot extern "C" {
26*7c3d14c8STreehugger Robot #endif
27*7c3d14c8STreehugger Robot   // Arguments for __sanitizer_sandbox_on_notify() below.
28*7c3d14c8STreehugger Robot   typedef struct {
29*7c3d14c8STreehugger Robot     // Enable sandbox support in sanitizer coverage.
30*7c3d14c8STreehugger Robot     int coverage_sandboxed;
31*7c3d14c8STreehugger Robot     // File descriptor to write coverage data to. If -1 is passed, a file will
32*7c3d14c8STreehugger Robot     // be pre-opened by __sanitizer_sandobx_on_notify(). This field has no
33*7c3d14c8STreehugger Robot     // effect if coverage_sandboxed == 0.
34*7c3d14c8STreehugger Robot     intptr_t coverage_fd;
35*7c3d14c8STreehugger Robot     // If non-zero, split the coverage data into well-formed blocks. This is
36*7c3d14c8STreehugger Robot     // useful when coverage_fd is a socket descriptor. Each block will contain
37*7c3d14c8STreehugger Robot     // a header, allowing data from multiple processes to be sent over the same
38*7c3d14c8STreehugger Robot     // socket.
39*7c3d14c8STreehugger Robot     unsigned int coverage_max_block_size;
40*7c3d14c8STreehugger Robot   } __sanitizer_sandbox_arguments;
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot   // Tell the tools to write their reports to "path.<pid>" instead of stderr.
43*7c3d14c8STreehugger Robot   void __sanitizer_set_report_path(const char *path);
44*7c3d14c8STreehugger Robot   // Tell the tools to write their reports to the provided file descriptor
45*7c3d14c8STreehugger Robot   // (casted to void *).
46*7c3d14c8STreehugger Robot   void __sanitizer_set_report_fd(void *fd);
47*7c3d14c8STreehugger Robot 
48*7c3d14c8STreehugger Robot   // Notify the tools that the sandbox is going to be turned on. The reserved
49*7c3d14c8STreehugger Robot   // parameter will be used in the future to hold a structure with functions
50*7c3d14c8STreehugger Robot   // that the tools may call to bypass the sandbox.
51*7c3d14c8STreehugger Robot   void __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
52*7c3d14c8STreehugger Robot 
53*7c3d14c8STreehugger Robot   // This function is called by the tool when it has just finished reporting
54*7c3d14c8STreehugger Robot   // an error. 'error_summary' is a one-line string that summarizes
55*7c3d14c8STreehugger Robot   // the error message. This function can be overridden by the client.
56*7c3d14c8STreehugger Robot   void __sanitizer_report_error_summary(const char *error_summary);
57*7c3d14c8STreehugger Robot 
58*7c3d14c8STreehugger Robot   // Some of the sanitizers (e.g. asan/tsan) may miss bugs that happen
59*7c3d14c8STreehugger Robot   // in unaligned loads/stores. In order to find such bugs reliably one needs
60*7c3d14c8STreehugger Robot   // to replace plain unaligned loads/stores with these calls.
61*7c3d14c8STreehugger Robot   uint16_t __sanitizer_unaligned_load16(const void *p);
62*7c3d14c8STreehugger Robot   uint32_t __sanitizer_unaligned_load32(const void *p);
63*7c3d14c8STreehugger Robot   uint64_t __sanitizer_unaligned_load64(const void *p);
64*7c3d14c8STreehugger Robot   void __sanitizer_unaligned_store16(void *p, uint16_t x);
65*7c3d14c8STreehugger Robot   void __sanitizer_unaligned_store32(void *p, uint32_t x);
66*7c3d14c8STreehugger Robot   void __sanitizer_unaligned_store64(void *p, uint64_t x);
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot   // Annotate the current state of a contiguous container, such as
69*7c3d14c8STreehugger Robot   // std::vector, std::string or similar.
70*7c3d14c8STreehugger Robot   // A contiguous container is a container that keeps all of its elements
71*7c3d14c8STreehugger Robot   // in a contiguous region of memory. The container owns the region of memory
72*7c3d14c8STreehugger Robot   // [beg, end); the memory [beg, mid) is used to store the current elements
73*7c3d14c8STreehugger Robot   // and the memory [mid, end) is reserved for future elements;
74*7c3d14c8STreehugger Robot   // beg <= mid <= end. For example, in "std::vector<> v"
75*7c3d14c8STreehugger Robot   //   beg = &v[0];
76*7c3d14c8STreehugger Robot   //   end = beg + v.capacity() * sizeof(v[0]);
77*7c3d14c8STreehugger Robot   //   mid = beg + v.size()     * sizeof(v[0]);
78*7c3d14c8STreehugger Robot   //
79*7c3d14c8STreehugger Robot   // This annotation tells the Sanitizer tool about the current state of the
80*7c3d14c8STreehugger Robot   // container so that the tool can report errors when memory from [mid, end)
81*7c3d14c8STreehugger Robot   // is accessed. Insert this annotation into methods like push_back/pop_back.
82*7c3d14c8STreehugger Robot   // Supply the old and the new values of mid (old_mid/new_mid).
83*7c3d14c8STreehugger Robot   // In the initial state mid == end and so should be the final
84*7c3d14c8STreehugger Robot   // state when the container is destroyed or when it reallocates the storage.
85*7c3d14c8STreehugger Robot   //
86*7c3d14c8STreehugger Robot   // Use with caution and don't use for anything other than vector-like classes.
87*7c3d14c8STreehugger Robot   //
88*7c3d14c8STreehugger Robot   // For AddressSanitizer, 'beg' should be 8-aligned and 'end' should
89*7c3d14c8STreehugger Robot   // be either 8-aligned or it should point to the end of a separate heap-,
90*7c3d14c8STreehugger Robot   // stack-, or global- allocated buffer. I.e. the following will not work:
91*7c3d14c8STreehugger Robot   //   int64_t x[2];  // 16 bytes, 8-aligned.
92*7c3d14c8STreehugger Robot   //   char *beg = (char *)&x[0];
93*7c3d14c8STreehugger Robot   //   char *end = beg + 12;  // Not 8 aligned, not the end of the buffer.
94*7c3d14c8STreehugger Robot   // This however will work fine:
95*7c3d14c8STreehugger Robot   //   int32_t x[3];  // 12 bytes, but 8-aligned under AddressSanitizer.
96*7c3d14c8STreehugger Robot   //   char *beg = (char*)&x[0];
97*7c3d14c8STreehugger Robot   //   char *end = beg + 12;  // Not 8-aligned, but is the end of the buffer.
98*7c3d14c8STreehugger Robot   void __sanitizer_annotate_contiguous_container(const void *beg,
99*7c3d14c8STreehugger Robot                                                  const void *end,
100*7c3d14c8STreehugger Robot                                                  const void *old_mid,
101*7c3d14c8STreehugger Robot                                                  const void *new_mid);
102*7c3d14c8STreehugger Robot   // Returns true if the contiguous container [beg, end) is properly poisoned
103*7c3d14c8STreehugger Robot   // (e.g. with __sanitizer_annotate_contiguous_container), i.e. if
104*7c3d14c8STreehugger Robot   //  - [beg, mid) is addressable,
105*7c3d14c8STreehugger Robot   //  - [mid, end) is unaddressable.
106*7c3d14c8STreehugger Robot   // Full verification requires O(end-beg) time; this function tries to avoid
107*7c3d14c8STreehugger Robot   // such complexity by touching only parts of the container around beg/mid/end.
108*7c3d14c8STreehugger Robot   int __sanitizer_verify_contiguous_container(const void *beg, const void *mid,
109*7c3d14c8STreehugger Robot                                               const void *end);
110*7c3d14c8STreehugger Robot 
111*7c3d14c8STreehugger Robot   // Similar to __sanitizer_verify_contiguous_container but returns the address
112*7c3d14c8STreehugger Robot   // of the first improperly poisoned byte otherwise. Returns null if the area
113*7c3d14c8STreehugger Robot   // is poisoned properly.
114*7c3d14c8STreehugger Robot   const void *__sanitizer_contiguous_container_find_bad_address(
115*7c3d14c8STreehugger Robot       const void *beg, const void *mid, const void *end);
116*7c3d14c8STreehugger Robot 
117*7c3d14c8STreehugger Robot   // Print the stack trace leading to this call. Useful for debugging user code.
118*7c3d14c8STreehugger Robot   void __sanitizer_print_stack_trace();
119*7c3d14c8STreehugger Robot 
120*7c3d14c8STreehugger Robot   // Sets the callback to be called right before death on error.
121*7c3d14c8STreehugger Robot   // Passing 0 will unset the callback.
122*7c3d14c8STreehugger Robot   void __sanitizer_set_death_callback(void (*callback)(void));
123*7c3d14c8STreehugger Robot 
124*7c3d14c8STreehugger Robot   // Interceptor hooks.
125*7c3d14c8STreehugger Robot   // Whenever a libc function interceptor is called it checks if the
126*7c3d14c8STreehugger Robot   // corresponding weak hook is defined, and it so -- calls it.
127*7c3d14c8STreehugger Robot   // The primary use case is data-flow-guided fuzzing, where the fuzzer needs
128*7c3d14c8STreehugger Robot   // to know what is being passed to libc functions, e.g. memcmp.
129*7c3d14c8STreehugger Robot   // FIXME: implement more hooks.
130*7c3d14c8STreehugger Robot   void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
131*7c3d14c8STreehugger Robot                                     const void *s2, size_t n, int result);
132*7c3d14c8STreehugger Robot   void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
133*7c3d14c8STreehugger Robot                                     const char *s2, size_t n, int result);
134*7c3d14c8STreehugger Robot   void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
135*7c3d14c8STreehugger Robot                                     const char *s2, int result);
136*7c3d14c8STreehugger Robot 
137*7c3d14c8STreehugger Robot   // Prints stack traces for all live heap allocations ordered by total
138*7c3d14c8STreehugger Robot   // allocation size until `top_percent` of total live heap is shown.
139*7c3d14c8STreehugger Robot   // `top_percent` should be between 1 and 100.
140*7c3d14c8STreehugger Robot   // Experimental feature currently available only with asan on Linux/x86_64.
141*7c3d14c8STreehugger Robot   void __sanitizer_print_memory_profile(size_t top_percent);
142*7c3d14c8STreehugger Robot 
143*7c3d14c8STreehugger Robot   // Fiber annotation interface.
144*7c3d14c8STreehugger Robot   // Before switching to a different stack, one must call
145*7c3d14c8STreehugger Robot   // __sanitizer_start_switch_fiber with a pointer to the bottom of the
146*7c3d14c8STreehugger Robot   // destination stack and its size. When code starts running on the new stack,
147*7c3d14c8STreehugger Robot   // it must call __sanitizer_finish_switch_fiber to finalize the switch.
148*7c3d14c8STreehugger Robot   // The start_switch function takes a void** to store the current fake stack if
149*7c3d14c8STreehugger Robot   // there is one (it is needed when detect_stack_use_after_return is enabled).
150*7c3d14c8STreehugger Robot   // When restoring a stack, this pointer must be given to the finish_switch
151*7c3d14c8STreehugger Robot   // function. In most cases, this void* can be stored on the stack just before
152*7c3d14c8STreehugger Robot   // switching.  When leaving a fiber definitely, null must be passed as first
153*7c3d14c8STreehugger Robot   // argument to the start_switch function so that the fake stack is destroyed.
154*7c3d14c8STreehugger Robot   // If you do not want support for stack use-after-return detection, you can
155*7c3d14c8STreehugger Robot   // always pass null to these two functions.
156*7c3d14c8STreehugger Robot   // Note that the fake stack mechanism is disabled during fiber switch, so if a
157*7c3d14c8STreehugger Robot   // signal callback runs during the switch, it will not benefit from the stack
158*7c3d14c8STreehugger Robot   // use-after-return detection.
159*7c3d14c8STreehugger Robot   void __sanitizer_start_switch_fiber(void **fake_stack_save,
160*7c3d14c8STreehugger Robot                                       const void *bottom, size_t size);
161*7c3d14c8STreehugger Robot   void __sanitizer_finish_switch_fiber(void *fake_stack_save);
162*7c3d14c8STreehugger Robot #ifdef __cplusplus
163*7c3d14c8STreehugger Robot }  // extern "C"
164*7c3d14c8STreehugger Robot #endif
165*7c3d14c8STreehugger Robot 
166*7c3d14c8STreehugger Robot #endif  // SANITIZER_COMMON_INTERFACE_DEFS_H
167