Add __sanitizer_get_allocated_begin API and implementations

This function will return the start of the allocation, if given a pointer that lies within an allocation. Otherwise, it returns NULL.

It will be useful for detecting dynamic TLS allocations in glibc >=2.25, which
uses malloc (see https://github.com/google/sanitizers/issues/1409#issuecomment-1214244142).

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D147005
This commit is contained in:
Thurston Dang
2023-04-03 21:14:40 +00:00
parent de92a20131
commit 415b1cfd57
12 changed files with 195 additions and 1 deletions

View File

@@ -681,6 +681,18 @@ int memprof_posix_memalign(void **memptr, uptr alignment, uptr size,
return 0;
}
void *memprof_malloc_begin(const void *p) {
u64 user_requested_size;
MemprofChunk *m =
instance.GetMemprofChunkByAddr((uptr)p, user_requested_size);
if (!m)
return nullptr;
if (user_requested_size == 0)
return nullptr;
return (void *)m->Beg();
}
uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp) {
if (!ptr)
return 0;
@@ -699,6 +711,10 @@ int __sanitizer_get_ownership(const void *p) {
return memprof_malloc_usable_size(p, 0, 0) != 0;
}
void *__sanitizer_get_allocated_begin(const void *p) {
return memprof_malloc_begin(p);
}
uptr __sanitizer_get_allocated_size(const void *p) {
return memprof_malloc_usable_size(p, 0, 0);
}