1*3cab2bb3Spatrick //===-- sanitizer/lsan_interface.h ------------------------------*- C++ -*-===// 2*3cab2bb3Spatrick // 3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*3cab2bb3Spatrick // 7*3cab2bb3Spatrick //===----------------------------------------------------------------------===// 8*3cab2bb3Spatrick // 9*3cab2bb3Spatrick // This file is a part of LeakSanitizer. 10*3cab2bb3Spatrick // 11*3cab2bb3Spatrick // Public interface header. 12*3cab2bb3Spatrick //===----------------------------------------------------------------------===// 13*3cab2bb3Spatrick #ifndef SANITIZER_LSAN_INTERFACE_H 14*3cab2bb3Spatrick #define SANITIZER_LSAN_INTERFACE_H 15*3cab2bb3Spatrick 16*3cab2bb3Spatrick #include <sanitizer/common_interface_defs.h> 17*3cab2bb3Spatrick 18*3cab2bb3Spatrick #ifdef __cplusplus 19*3cab2bb3Spatrick extern "C" { 20*3cab2bb3Spatrick #endif 21*3cab2bb3Spatrick // Allocations made between calls to __lsan_disable() and __lsan_enable() will 22*3cab2bb3Spatrick // be treated as non-leaks. Disable/enable pairs may be nested. 23*3cab2bb3Spatrick void __lsan_disable(void); 24*3cab2bb3Spatrick void __lsan_enable(void); 25*3cab2bb3Spatrick 26*3cab2bb3Spatrick // The heap object into which p points will be treated as a non-leak. 27*3cab2bb3Spatrick void __lsan_ignore_object(const void *p); 28*3cab2bb3Spatrick 29*3cab2bb3Spatrick // Memory regions registered through this interface will be treated as sources 30*3cab2bb3Spatrick // of live pointers during leak checking. Useful if you store pointers in 31*3cab2bb3Spatrick // mapped memory. 32*3cab2bb3Spatrick // Points of note: 33*3cab2bb3Spatrick // - __lsan_unregister_root_region() must be called with the same pointer and 34*3cab2bb3Spatrick // size that have earlier been passed to __lsan_register_root_region() 35*3cab2bb3Spatrick // - LSan will skip any inaccessible memory when scanning a root region. E.g., 36*3cab2bb3Spatrick // if you map memory within a larger region that you have mprotect'ed, you can 37*3cab2bb3Spatrick // register the entire large region. 38*3cab2bb3Spatrick // - the implementation is not optimized for performance. This interface is 39*3cab2bb3Spatrick // intended to be used for a small number of relatively static regions. 40*3cab2bb3Spatrick void __lsan_register_root_region(const void *p, size_t size); 41*3cab2bb3Spatrick void __lsan_unregister_root_region(const void *p, size_t size); 42*3cab2bb3Spatrick 43*3cab2bb3Spatrick // Check for leaks now. This function behaves identically to the default 44*3cab2bb3Spatrick // end-of-process leak check. In particular, it will terminate the process if 45*3cab2bb3Spatrick // leaks are found and the exitcode runtime flag is non-zero. 46*3cab2bb3Spatrick // Subsequent calls to this function will have no effect and end-of-process 47*3cab2bb3Spatrick // leak check will not run. Effectively, end-of-process leak check is moved to 48*3cab2bb3Spatrick // the time of first invocation of this function. 49*3cab2bb3Spatrick // By calling this function early during process shutdown, you can instruct 50*3cab2bb3Spatrick // LSan to ignore shutdown-only leaks which happen later on. 51*3cab2bb3Spatrick void __lsan_do_leak_check(void); 52*3cab2bb3Spatrick 53*3cab2bb3Spatrick // Check for leaks now. Returns zero if no leaks have been found or if leak 54*3cab2bb3Spatrick // detection is disabled, non-zero otherwise. 55*3cab2bb3Spatrick // This function may be called repeatedly, e.g. to periodically check a 56*3cab2bb3Spatrick // long-running process. It prints a leak report if appropriate, but does not 57*3cab2bb3Spatrick // terminate the process. It does not affect the behavior of 58*3cab2bb3Spatrick // __lsan_do_leak_check() or the end-of-process leak check, and is not 59*3cab2bb3Spatrick // affected by them. 60*3cab2bb3Spatrick int __lsan_do_recoverable_leak_check(void); 61*3cab2bb3Spatrick 62*3cab2bb3Spatrick // The user may optionally provide this function to disallow leak checking 63*3cab2bb3Spatrick // for the program it is linked into (if the return value is non-zero). This 64*3cab2bb3Spatrick // function must be defined as returning a constant value; any behavior beyond 65*3cab2bb3Spatrick // that is unsupported. 66*3cab2bb3Spatrick // To avoid dead stripping, you may need to define this function with 67*3cab2bb3Spatrick // __attribute__((used)) 68*3cab2bb3Spatrick int __lsan_is_turned_off(void); 69*3cab2bb3Spatrick 70*3cab2bb3Spatrick // This function may be optionally provided by user and should return 71*3cab2bb3Spatrick // a string containing LSan runtime options. See lsan_flags.inc for details. 72*3cab2bb3Spatrick const char *__lsan_default_options(void); 73*3cab2bb3Spatrick 74*3cab2bb3Spatrick // This function may be optionally provided by the user and should return 75*3cab2bb3Spatrick // a string containing LSan suppressions. 76*3cab2bb3Spatrick const char *__lsan_default_suppressions(void); 77*3cab2bb3Spatrick #ifdef __cplusplus 78*3cab2bb3Spatrick } // extern "C" 79*3cab2bb3Spatrick 80*3cab2bb3Spatrick namespace __lsan { 81*3cab2bb3Spatrick class ScopedDisabler { 82*3cab2bb3Spatrick public: ScopedDisabler()83*3cab2bb3Spatrick ScopedDisabler() { __lsan_disable(); } ~ScopedDisabler()84*3cab2bb3Spatrick ~ScopedDisabler() { __lsan_enable(); } 85*3cab2bb3Spatrick }; 86*3cab2bb3Spatrick } // namespace __lsan 87*3cab2bb3Spatrick #endif 88*3cab2bb3Spatrick 89*3cab2bb3Spatrick #endif // SANITIZER_LSAN_INTERFACE_H 90