10b57cec5SDimitry Andric //=-- lsan_common.h -------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file is a part of LeakSanitizer. 100b57cec5SDimitry Andric // Private LSan header. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LSAN_COMMON_H 150b57cec5SDimitry Andric #define LSAN_COMMON_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_allocator.h" 180b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_common.h" 190b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h" 200b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_platform.h" 21349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h" 220b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_stoptheworld.h" 230b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_symbolizer.h" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric // LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) on Linux. 260b57cec5SDimitry Andric // Also, LSan doesn't like 32 bit architectures 270b57cec5SDimitry Andric // because of "small" (4 bytes) pointer size that leads to high false negative 280b57cec5SDimitry Andric // ratio on large leaks. But we still want to have it for some 32 bit arches 290b57cec5SDimitry Andric // (e.g. x86), see https://github.com/google/sanitizers/issues/403. 300b57cec5SDimitry Andric // To enable LeakSanitizer on a new architecture, one needs to implement the 310b57cec5SDimitry Andric // internal_clone function as well as (probably) adjust the TLS machinery for 320b57cec5SDimitry Andric // the new architecture inside the sanitizer library. 33e8d8bef9SDimitry Andric // Exclude leak-detection on arm32 for Android because `__aeabi_read_tp` 34e8d8bef9SDimitry Andric // is missing. This caused a link error. 35e8d8bef9SDimitry Andric #if SANITIZER_ANDROID && (__ANDROID_API__ < 28 || defined(__arm__)) 36e8d8bef9SDimitry Andric # define CAN_SANITIZE_LEAKS 0 37*81ad6265SDimitry Andric #elif (SANITIZER_LINUX || SANITIZER_APPLE) && (SANITIZER_WORDSIZE == 64) && \ 380b57cec5SDimitry Andric (defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \ 395ffd83dbSDimitry Andric defined(__powerpc64__) || defined(__s390x__)) 400b57cec5SDimitry Andric # define CAN_SANITIZE_LEAKS 1 41*81ad6265SDimitry Andric #elif defined(__i386__) && (SANITIZER_LINUX || SANITIZER_APPLE) 420b57cec5SDimitry Andric # define CAN_SANITIZE_LEAKS 1 43e8d8bef9SDimitry Andric #elif defined(__arm__) && SANITIZER_LINUX 440b57cec5SDimitry Andric # define CAN_SANITIZE_LEAKS 1 45fe6060f1SDimitry Andric #elif SANITIZER_RISCV64 && SANITIZER_LINUX 46fe6060f1SDimitry Andric # define CAN_SANITIZE_LEAKS 1 475ffd83dbSDimitry Andric #elif SANITIZER_NETBSD || SANITIZER_FUCHSIA 480b57cec5SDimitry Andric # define CAN_SANITIZE_LEAKS 1 490b57cec5SDimitry Andric #else 500b57cec5SDimitry Andric # define CAN_SANITIZE_LEAKS 0 510b57cec5SDimitry Andric #endif 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric namespace __sanitizer { 540b57cec5SDimitry Andric class FlagParser; 550b57cec5SDimitry Andric class ThreadRegistry; 56e8d8bef9SDimitry Andric class ThreadContextBase; 570b57cec5SDimitry Andric struct DTLS; 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric namespace __lsan { 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric // Chunk tags. 630b57cec5SDimitry Andric enum ChunkTag { 640b57cec5SDimitry Andric kDirectlyLeaked = 0, // default 650b57cec5SDimitry Andric kIndirectlyLeaked = 1, 660b57cec5SDimitry Andric kReachable = 2, 670b57cec5SDimitry Andric kIgnored = 3 680b57cec5SDimitry Andric }; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric struct Flags { 710b57cec5SDimitry Andric #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name; 720b57cec5SDimitry Andric #include "lsan_flags.inc" 730b57cec5SDimitry Andric #undef LSAN_FLAG 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric void SetDefaults(); 760b57cec5SDimitry Andric uptr pointer_alignment() const { 770b57cec5SDimitry Andric return use_unaligned ? 1 : sizeof(uptr); 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric }; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric extern Flags lsan_flags; 820b57cec5SDimitry Andric inline Flags *flags() { return &lsan_flags; } 830b57cec5SDimitry Andric void RegisterLsanFlags(FlagParser *parser, Flags *f); 840b57cec5SDimitry Andric 850eae32dcSDimitry Andric struct LeakedChunk { 860eae32dcSDimitry Andric uptr chunk; 870eae32dcSDimitry Andric u32 stack_trace_id; 880eae32dcSDimitry Andric uptr leaked_size; 890eae32dcSDimitry Andric ChunkTag tag; 900eae32dcSDimitry Andric }; 910eae32dcSDimitry Andric 920eae32dcSDimitry Andric using LeakedChunks = InternalMmapVector<LeakedChunk>; 930eae32dcSDimitry Andric 940b57cec5SDimitry Andric struct Leak { 950b57cec5SDimitry Andric u32 id; 960b57cec5SDimitry Andric uptr hit_count; 970b57cec5SDimitry Andric uptr total_size; 980b57cec5SDimitry Andric u32 stack_trace_id; 990b57cec5SDimitry Andric bool is_directly_leaked; 1000b57cec5SDimitry Andric bool is_suppressed; 1010b57cec5SDimitry Andric }; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric struct LeakedObject { 1040b57cec5SDimitry Andric u32 leak_id; 1050b57cec5SDimitry Andric uptr addr; 1060b57cec5SDimitry Andric uptr size; 1070b57cec5SDimitry Andric }; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // Aggregates leaks by stack trace prefix. 1100b57cec5SDimitry Andric class LeakReport { 1110b57cec5SDimitry Andric public: 1120b57cec5SDimitry Andric LeakReport() {} 1130eae32dcSDimitry Andric void AddLeakedChunks(const LeakedChunks &chunks); 1140b57cec5SDimitry Andric void ReportTopLeaks(uptr max_leaks); 1150b57cec5SDimitry Andric void PrintSummary(); 116e8d8bef9SDimitry Andric uptr ApplySuppressions(); 1170b57cec5SDimitry Andric uptr UnsuppressedLeakCount(); 118e8d8bef9SDimitry Andric uptr IndirectUnsuppressedLeakCount(); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric private: 1210b57cec5SDimitry Andric void PrintReportForLeak(uptr index); 1220b57cec5SDimitry Andric void PrintLeakedObjectsForLeak(uptr index); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric u32 next_id_ = 0; 1250b57cec5SDimitry Andric InternalMmapVector<Leak> leaks_; 1260b57cec5SDimitry Andric InternalMmapVector<LeakedObject> leaked_objects_; 1270b57cec5SDimitry Andric }; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric typedef InternalMmapVector<uptr> Frontier; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Platform-specific functions. 1320b57cec5SDimitry Andric void InitializePlatformSpecificModules(); 1330b57cec5SDimitry Andric void ProcessGlobalRegions(Frontier *frontier); 1340b57cec5SDimitry Andric void ProcessPlatformSpecificAllocations(Frontier *frontier); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric struct RootRegion { 1370b57cec5SDimitry Andric uptr begin; 1380b57cec5SDimitry Andric uptr size; 1390b57cec5SDimitry Andric }; 1400b57cec5SDimitry Andric 1415ffd83dbSDimitry Andric // LockStuffAndStopTheWorld can start to use Scan* calls to collect into 1425ffd83dbSDimitry Andric // this Frontier vector before the StopTheWorldCallback actually runs. 1435ffd83dbSDimitry Andric // This is used when the OS has a unified callback API for suspending 1445ffd83dbSDimitry Andric // threads and enumerating roots. 1455ffd83dbSDimitry Andric struct CheckForLeaksParam { 1465ffd83dbSDimitry Andric Frontier frontier; 1470eae32dcSDimitry Andric LeakedChunks leaks; 1485ffd83dbSDimitry Andric bool success = false; 1495ffd83dbSDimitry Andric }; 1505ffd83dbSDimitry Andric 151349cc55cSDimitry Andric InternalMmapVectorNoCtor<RootRegion> const *GetRootRegions(); 1520b57cec5SDimitry Andric void ScanRootRegion(Frontier *frontier, RootRegion const ®ion, 1530b57cec5SDimitry Andric uptr region_begin, uptr region_end, bool is_readable); 1545ffd83dbSDimitry Andric void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg); 155e8d8bef9SDimitry Andric void GetAdditionalThreadContextPtrs(ThreadContextBase *tctx, void *ptrs); 15668d75effSDimitry Andric // Run stoptheworld while holding any platform-specific locks, as well as the 15768d75effSDimitry Andric // allocator and thread registry locks. 1585ffd83dbSDimitry Andric void LockStuffAndStopTheWorld(StopTheWorldCallback callback, 1595ffd83dbSDimitry Andric CheckForLeaksParam* argument); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric void ScanRangeForPointers(uptr begin, uptr end, 1620b57cec5SDimitry Andric Frontier *frontier, 1630b57cec5SDimitry Andric const char *region_type, ChunkTag tag); 1640b57cec5SDimitry Andric void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric enum IgnoreObjectResult { 1670b57cec5SDimitry Andric kIgnoreObjectSuccess, 1680b57cec5SDimitry Andric kIgnoreObjectAlreadyIgnored, 1690b57cec5SDimitry Andric kIgnoreObjectInvalid 1700b57cec5SDimitry Andric }; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Functions called from the parent tool. 1730b57cec5SDimitry Andric const char *MaybeCallLsanDefaultOptions(); 1740b57cec5SDimitry Andric void InitCommonLsan(); 1750b57cec5SDimitry Andric void DoLeakCheck(); 1760b57cec5SDimitry Andric void DoRecoverableLeakCheckVoid(); 1770b57cec5SDimitry Andric void DisableCounterUnderflow(); 1780b57cec5SDimitry Andric bool DisabledInThisThread(); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // Used to implement __lsan::ScopedDisabler. 1810b57cec5SDimitry Andric void DisableInThisThread(); 1820b57cec5SDimitry Andric void EnableInThisThread(); 1830b57cec5SDimitry Andric // Can be used to ignore memory allocated by an intercepted 1840b57cec5SDimitry Andric // function. 1850b57cec5SDimitry Andric struct ScopedInterceptorDisabler { 1860b57cec5SDimitry Andric ScopedInterceptorDisabler() { DisableInThisThread(); } 1870b57cec5SDimitry Andric ~ScopedInterceptorDisabler() { EnableInThisThread(); } 1880b57cec5SDimitry Andric }; 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric // According to Itanium C++ ABI array cookie is a one word containing 1910b57cec5SDimitry Andric // size of allocated array. 1920b57cec5SDimitry Andric static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size, 1930b57cec5SDimitry Andric uptr addr) { 1940b57cec5SDimitry Andric return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr && 1950b57cec5SDimitry Andric *reinterpret_cast<uptr *>(chunk_beg) == 0; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // According to ARM C++ ABI array cookie consists of two words: 1990b57cec5SDimitry Andric // struct array_cookie { 2000b57cec5SDimitry Andric // std::size_t element_size; // element_size != 0 2010b57cec5SDimitry Andric // std::size_t element_count; 2020b57cec5SDimitry Andric // }; 2030b57cec5SDimitry Andric static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size, 2040b57cec5SDimitry Andric uptr addr) { 2050b57cec5SDimitry Andric return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr && 2060b57cec5SDimitry Andric *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // Special case for "new T[0]" where T is a type with DTOR. 2100b57cec5SDimitry Andric // new T[0] will allocate a cookie (one or two words) for the array size (0) 2110b57cec5SDimitry Andric // and store a pointer to the end of allocated chunk. The actual cookie layout 2120b57cec5SDimitry Andric // varies between platforms according to their C++ ABI implementation. 2130b57cec5SDimitry Andric inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size, 2140b57cec5SDimitry Andric uptr addr) { 2150b57cec5SDimitry Andric #if defined(__arm__) 2160b57cec5SDimitry Andric return IsARMABIArrayCookie(chunk_beg, chunk_size, addr); 2170b57cec5SDimitry Andric #else 2180b57cec5SDimitry Andric return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr); 2190b57cec5SDimitry Andric #endif 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric // The following must be implemented in the parent tool. 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric void ForEachChunk(ForEachChunkCallback callback, void *arg); 2250b57cec5SDimitry Andric // Returns the address range occupied by the global allocator object. 2260b57cec5SDimitry Andric void GetAllocatorGlobalRange(uptr *begin, uptr *end); 2270b57cec5SDimitry Andric // Wrappers for allocator's ForceLock()/ForceUnlock(). 2280b57cec5SDimitry Andric void LockAllocator(); 2290b57cec5SDimitry Andric void UnlockAllocator(); 2300b57cec5SDimitry Andric // Returns true if [addr, addr + sizeof(void *)) is poisoned. 2310b57cec5SDimitry Andric bool WordIsPoisoned(uptr addr); 2320b57cec5SDimitry Andric // Wrappers for ThreadRegistry access. 23304eeddc0SDimitry Andric void LockThreadRegistry() SANITIZER_NO_THREAD_SAFETY_ANALYSIS; 23404eeddc0SDimitry Andric void UnlockThreadRegistry() SANITIZER_NO_THREAD_SAFETY_ANALYSIS; 2350eae32dcSDimitry Andric 2360eae32dcSDimitry Andric struct ScopedStopTheWorldLock { 2370eae32dcSDimitry Andric ScopedStopTheWorldLock() { 2380eae32dcSDimitry Andric LockThreadRegistry(); 2390eae32dcSDimitry Andric LockAllocator(); 2400eae32dcSDimitry Andric } 2410eae32dcSDimitry Andric 2420eae32dcSDimitry Andric ~ScopedStopTheWorldLock() { 2430eae32dcSDimitry Andric UnlockAllocator(); 2440eae32dcSDimitry Andric UnlockThreadRegistry(); 2450eae32dcSDimitry Andric } 2460eae32dcSDimitry Andric 2470eae32dcSDimitry Andric ScopedStopTheWorldLock &operator=(const ScopedStopTheWorldLock &) = delete; 2480eae32dcSDimitry Andric ScopedStopTheWorldLock(const ScopedStopTheWorldLock &) = delete; 2490eae32dcSDimitry Andric }; 2500eae32dcSDimitry Andric 2510b57cec5SDimitry Andric ThreadRegistry *GetThreadRegistryLocked(); 2520b57cec5SDimitry Andric bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end, 2530b57cec5SDimitry Andric uptr *tls_begin, uptr *tls_end, uptr *cache_begin, 2540b57cec5SDimitry Andric uptr *cache_end, DTLS **dtls); 2555ffd83dbSDimitry Andric void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches); 2560b57cec5SDimitry Andric void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback, 2570b57cec5SDimitry Andric void *arg); 2580b57cec5SDimitry Andric // If called from the main thread, updates the main thread's TID in the thread 2590b57cec5SDimitry Andric // registry. We need this to handle processes that fork() without a subsequent 2600b57cec5SDimitry Andric // exec(), which invalidates the recorded TID. To update it, we must call 2610b57cec5SDimitry Andric // gettid() from the main thread. Our solution is to call this function before 2620b57cec5SDimitry Andric // leak checking and also before every call to pthread_create() (to handle cases 2630b57cec5SDimitry Andric // where leak checking is initiated from a non-main thread). 2640b57cec5SDimitry Andric void EnsureMainThreadIDIsCorrect(); 2650b57cec5SDimitry Andric // If p points into a chunk that has been allocated to the user, returns its 2660b57cec5SDimitry Andric // user-visible address. Otherwise, returns 0. 2670b57cec5SDimitry Andric uptr PointsIntoChunk(void *p); 2680b57cec5SDimitry Andric // Returns address of user-visible chunk contained in this allocator chunk. 2690b57cec5SDimitry Andric uptr GetUserBegin(uptr chunk); 2700b57cec5SDimitry Andric // Helper for __lsan_ignore_object(). 2710b57cec5SDimitry Andric IgnoreObjectResult IgnoreObjectLocked(const void *p); 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric // Return the linker module, if valid for the platform. 2740b57cec5SDimitry Andric LoadedModule *GetLinker(); 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric // Return true if LSan has finished leak checking and reported leaks. 2770b57cec5SDimitry Andric bool HasReportedLeaks(); 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric // Run platform-specific leak handlers. 2800b57cec5SDimitry Andric void HandleLeaks(); 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric // Wrapper for chunk metadata operations. 2830b57cec5SDimitry Andric class LsanMetadata { 2840b57cec5SDimitry Andric public: 2850b57cec5SDimitry Andric // Constructor accepts address of user-visible chunk. 2860b57cec5SDimitry Andric explicit LsanMetadata(uptr chunk); 2870b57cec5SDimitry Andric bool allocated() const; 2880b57cec5SDimitry Andric ChunkTag tag() const; 2890b57cec5SDimitry Andric void set_tag(ChunkTag value); 2900b57cec5SDimitry Andric uptr requested_size() const; 2910b57cec5SDimitry Andric u32 stack_trace_id() const; 2920b57cec5SDimitry Andric private: 2930b57cec5SDimitry Andric void *metadata_; 2940b57cec5SDimitry Andric }; 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric } // namespace __lsan 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric extern "C" { 2990b57cec5SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 3000b57cec5SDimitry Andric const char *__lsan_default_options(); 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 3030b57cec5SDimitry Andric int __lsan_is_turned_off(); 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 3060b57cec5SDimitry Andric const char *__lsan_default_suppressions(); 307349cc55cSDimitry Andric 308349cc55cSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 309349cc55cSDimitry Andric void __lsan_register_root_region(const void *p, __lsan::uptr size); 310349cc55cSDimitry Andric 311349cc55cSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 312349cc55cSDimitry Andric void __lsan_unregister_root_region(const void *p, __lsan::uptr size); 313349cc55cSDimitry Andric 3140b57cec5SDimitry Andric } // extern "C" 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric #endif // LSAN_COMMON_H 317