LibJS+AK: Register GC memory as root regions for LeakSanitizer

This should fix the gigantic list of false positives dumped by
LeakSanitizer on exit .
This commit is contained in:
Andreas Kling 2024-04-03 07:53:54 +02:00
parent ccebc7a905
commit 3881717103
Notes: sideshowbarker 2024-07-17 18:23:22 +09:00
2 changed files with 8 additions and 0 deletions

View file

@ -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

View file

@ -13,6 +13,7 @@
#ifdef HAS_ADDRESS_SANITIZER
# include <sanitizer/asan_interface.h>
# include <sanitizer/lsan_interface.h>
#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);
}