From 38817171039127aa7b6a83b1ae3ccd399a5c96a8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 3 Apr 2024 07:53:54 +0200 Subject: [PATCH] LibJS+AK: Register GC memory as root regions for LeakSanitizer This should fix the gigantic list of false positives dumped by LeakSanitizer on exit . --- AK/Platform.h | 4 ++++ Userland/Libraries/LibJS/Heap/BlockAllocator.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/AK/Platform.h b/AK/Platform.h index 7125ab9689d..435695227bf 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -218,9 +218,13 @@ # define HAS_ADDRESS_SANITIZER # define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region(addr, size) # define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region(addr, size) +# define LSAN_REGISTER_ROOT_REGION(base, size) __lsan_register_root_region(base, size) +# define LSAN_UNREGISTER_ROOT_REGION(base, size) __lsan_unregister_root_region(base, size) #else # define ASAN_POISON_MEMORY_REGION(addr, size) # define ASAN_UNPOISON_MEMORY_REGION(addr, size) +# define LSAN_REGISTER_ROOT_REGION(base, size) +# define LSAN_UNREGISTER_ROOT_REGION(base, size) #endif #ifndef AK_OS_SERENITY diff --git a/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp b/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp index 22c59004ccf..2501a14442c 100644 --- a/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp +++ b/Userland/Libraries/LibJS/Heap/BlockAllocator.cpp @@ -13,6 +13,7 @@ #ifdef HAS_ADDRESS_SANITIZER # include +# include #endif // FIXME: Implement MADV_FREE and/or MADV_DONTNEED on SerenityOS. @@ -43,6 +44,7 @@ void* BlockAllocator::allocate_block([[maybe_unused]] char const* name) size_t random_index = get_random_uniform(m_blocks.size()); auto* block = m_blocks.unstable_take(random_index); ASAN_UNPOISON_MEMORY_REGION(block, HeapBlock::block_size); + LSAN_REGISTER_ROOT_REGION(block, HeapBlock::block_size); #ifdef AK_OS_SERENITY if (set_mmap_name(block, HeapBlock::block_size, name) < 0) { perror("set_mmap_name"); @@ -58,6 +60,7 @@ void* BlockAllocator::allocate_block([[maybe_unused]] char const* name) auto* block = (HeapBlock*)mmap(nullptr, HeapBlock::block_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); #endif VERIFY(block != MAP_FAILED); + LSAN_REGISTER_ROOT_REGION(block, HeapBlock::block_size); return block; } @@ -88,6 +91,7 @@ void BlockAllocator::deallocate_block(void* block) #endif ASAN_POISON_MEMORY_REGION(block, HeapBlock::block_size); + LSAN_UNREGISTER_ROOT_REGION(block, HeapBlock::block_size); m_blocks.append(block); }