From d644ab022a7be985255db29fd466798e9b138bee Mon Sep 17 00:00:00 2001 From: Thurston Dang Date: Tue, 4 Apr 2023 00:42:37 +0000 Subject: [PATCH] Update __sanitizer_get_allocated_begin to return const void* D147005 introduced __sanitizer_get_allocated_begin, with a return value of void*. This involved a few naughty casts that dropped the const. This patch adds back the const qualifier. Differential Revision: https://reviews.llvm.org/D147489 --- compiler-rt/include/sanitizer/allocator_interface.h | 2 +- compiler-rt/lib/asan/asan_allocator.cpp | 6 +++--- compiler-rt/lib/dfsan/dfsan_allocator.cpp | 6 +++--- compiler-rt/lib/hwasan/hwasan_allocator.cpp | 6 +++--- compiler-rt/lib/lsan/lsan_allocator.cpp | 6 +++--- compiler-rt/lib/memprof/memprof_allocator.cpp | 6 +++--- compiler-rt/lib/msan/msan_allocator.cpp | 6 +++--- .../lib/sanitizer_common/sanitizer_allocator_interface.h | 2 +- compiler-rt/lib/tsan/rtl/tsan_mman.cpp | 6 +++--- .../test/sanitizer_common/TestCases/get_allocated_begin.cpp | 2 +- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/compiler-rt/include/sanitizer/allocator_interface.h b/compiler-rt/include/sanitizer/allocator_interface.h index d846f3f330741..d0cfce79c1aef 100644 --- a/compiler-rt/include/sanitizer/allocator_interface.h +++ b/compiler-rt/include/sanitizer/allocator_interface.h @@ -28,7 +28,7 @@ extern "C" { /* If a pointer lies within an allocation, it will return the start address of the allocation. Otherwise, it returns nullptr. */ - void *__sanitizer_get_allocated_begin(const void *p); + const void *__sanitizer_get_allocated_begin(const void *p); /* Returns the number of bytes reserved for the pointer p. Requires (get_ownership(p) == true) or (p == 0). */ diff --git a/compiler-rt/lib/asan/asan_allocator.cpp b/compiler-rt/lib/asan/asan_allocator.cpp index 4b65b44a88f91..708d975a93dcf 100644 --- a/compiler-rt/lib/asan/asan_allocator.cpp +++ b/compiler-rt/lib/asan/asan_allocator.cpp @@ -1164,7 +1164,7 @@ IgnoreObjectResult IgnoreObjectLocked(const void *p) { // ---------------------- Interface ---------------- {{{1 using namespace __asan; -void *AllocationBegin(const void *p) { +const void *AllocationBegin(const void *p) { AsanChunk *m = __asan::instance.GetAsanChunkByAddr((uptr)p); if (!m) return nullptr; @@ -1172,7 +1172,7 @@ void *AllocationBegin(const void *p) { return nullptr; if (m->UsedSize() == 0) return nullptr; - return (void *)(m->Beg()); + return (const void *)(m->Beg()); } // ASan allocator doesn't reserve extra bytes, so normally we would @@ -1198,7 +1198,7 @@ uptr __sanitizer_get_allocated_size(const void *p) { return allocated_size; } -void *__sanitizer_get_allocated_begin(const void *p) { +const void *__sanitizer_get_allocated_begin(const void *p) { return AllocationBegin(p); } diff --git a/compiler-rt/lib/dfsan/dfsan_allocator.cpp b/compiler-rt/lib/dfsan/dfsan_allocator.cpp index 7ae6024fb2c9d..36346d163d982 100644 --- a/compiler-rt/lib/dfsan/dfsan_allocator.cpp +++ b/compiler-rt/lib/dfsan/dfsan_allocator.cpp @@ -174,7 +174,7 @@ void *DFsanCalloc(uptr nmemb, uptr size) { return DFsanAllocate(nmemb * size, sizeof(u64), true /*zeroise*/); } -void *AllocationBegin(const void *p) { +const void *AllocationBegin(const void *p) { if (!p) return nullptr; void *beg = allocator.GetBlockBegin(p); @@ -185,7 +185,7 @@ void *AllocationBegin(const void *p) { return nullptr; if (b->requested_size == 0) return nullptr; - return (void *)beg; + return (const void *)beg; } static uptr AllocationSize(const void *p) { @@ -308,7 +308,7 @@ uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; } int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; } -void *__sanitizer_get_allocated_begin(const void *p) { +const void *__sanitizer_get_allocated_begin(const void *p) { return AllocationBegin(p); } diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp index 8ccdeb23fa995..994a580dc95e0 100644 --- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp +++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp @@ -397,7 +397,7 @@ HwasanChunkView FindHeapChunkByAddress(uptr address) { return HwasanChunkView(reinterpret_cast(block), metadata); } -void *AllocationBegin(const void *p) { +const void *AllocationBegin(const void *p) { const void *untagged_ptr = UntagPtr(p); if (!untagged_ptr) return nullptr; @@ -411,7 +411,7 @@ void *AllocationBegin(const void *p) { return nullptr; tag_t tag = GetTagFromPointer((uptr)p); - return (void *)AddTagToPointer((uptr)beg, tag); + return (const void *)AddTagToPointer((uptr)beg, tag); } static uptr AllocationSize(const void *tagged_ptr) { @@ -658,7 +658,7 @@ uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; } int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; } -void *__sanitizer_get_allocated_begin(const void *p) { +const void *__sanitizer_get_allocated_begin(const void *p) { return AllocationBegin(p); } diff --git a/compiler-rt/lib/lsan/lsan_allocator.cpp b/compiler-rt/lib/lsan/lsan_allocator.cpp index b0a54d7cd9bc5..471b134a26471 100644 --- a/compiler-rt/lib/lsan/lsan_allocator.cpp +++ b/compiler-rt/lib/lsan/lsan_allocator.cpp @@ -145,7 +145,7 @@ void GetAllocatorCacheRange(uptr *begin, uptr *end) { *end = *begin + sizeof(AllocatorCache); } -void *GetMallocBegin(const void *p) { +const void *GetMallocBegin(const void *p) { if (!p) return nullptr; void *beg = allocator.GetBlockBegin(p); @@ -158,7 +158,7 @@ void *GetMallocBegin(const void *p) { return nullptr; if (m->requested_size == 0) return nullptr; - return (void *)beg; + return (const void *)beg; } uptr GetMallocUsableSize(const void *p) { @@ -380,7 +380,7 @@ SANITIZER_INTERFACE_ATTRIBUTE int __sanitizer_get_ownership(const void *p) { return Metadata(p) != nullptr; } SANITIZER_INTERFACE_ATTRIBUTE -void * __sanitizer_get_allocated_begin(const void *p) { +const void * __sanitizer_get_allocated_begin(const void *p) { return GetMallocBegin(p); } diff --git a/compiler-rt/lib/memprof/memprof_allocator.cpp b/compiler-rt/lib/memprof/memprof_allocator.cpp index 80a87d49dfc6e..49c0aad39cfbd 100644 --- a/compiler-rt/lib/memprof/memprof_allocator.cpp +++ b/compiler-rt/lib/memprof/memprof_allocator.cpp @@ -681,7 +681,7 @@ int memprof_posix_memalign(void **memptr, uptr alignment, uptr size, return 0; } -void *memprof_malloc_begin(const void *p) { +const void *memprof_malloc_begin(const void *p) { u64 user_requested_size; MemprofChunk *m = instance.GetMemprofChunkByAddr((uptr)p, user_requested_size); @@ -690,7 +690,7 @@ void *memprof_malloc_begin(const void *p) { if (user_requested_size == 0) return nullptr; - return (void *)m->Beg(); + return (const void *)m->Beg(); } uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp) { @@ -711,7 +711,7 @@ int __sanitizer_get_ownership(const void *p) { return memprof_malloc_usable_size(p, 0, 0) != 0; } -void *__sanitizer_get_allocated_begin(const void *p) { +const void *__sanitizer_get_allocated_begin(const void *p) { return memprof_malloc_begin(p); } diff --git a/compiler-rt/lib/msan/msan_allocator.cpp b/compiler-rt/lib/msan/msan_allocator.cpp index 08ec3314b26e6..1013303af6795 100644 --- a/compiler-rt/lib/msan/msan_allocator.cpp +++ b/compiler-rt/lib/msan/msan_allocator.cpp @@ -260,7 +260,7 @@ static void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) { return MsanAllocate(stack, nmemb * size, sizeof(u64), true); } -void *AllocationBegin(const void *p) { +const void *AllocationBegin(const void *p) { if (!p) return nullptr; void *beg = allocator.GetBlockBegin(p); @@ -272,7 +272,7 @@ void *AllocationBegin(const void *p) { if (b->requested_size == 0) return nullptr; - return (void *)beg; + return (const void *)beg; } static uptr AllocationSize(const void *p) { @@ -388,7 +388,7 @@ uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; } int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; } -void *__sanitizer_get_allocated_begin(const void *p) { +const void *__sanitizer_get_allocated_begin(const void *p) { return AllocationBegin(p); } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h index 35c7c97df3299..504109e9d3f6f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_interface.h @@ -21,7 +21,7 @@ extern "C" { SANITIZER_INTERFACE_ATTRIBUTE uptr __sanitizer_get_estimated_allocated_size(uptr size); SANITIZER_INTERFACE_ATTRIBUTE int __sanitizer_get_ownership(const void *p); -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void * +SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const void * __sanitizer_get_allocated_begin(const void *p); SANITIZER_INTERFACE_ATTRIBUTE uptr __sanitizer_get_allocated_size(const void *p); diff --git a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp index 3cc4d16955ede..b548265fe6833 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_mman.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_mman.cpp @@ -352,7 +352,7 @@ void *user_pvalloc(ThreadState *thr, uptr pc, uptr sz) { return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, PageSize)); } -void *user_alloc_begin(const void *p) { +const void *user_alloc_begin(const void *p) { if (p == nullptr || !IsAppMem((uptr)p)) return nullptr; void *beg = allocator()->GetBlockBegin(p); @@ -363,7 +363,7 @@ void *user_alloc_begin(const void *p) { if (!b) return nullptr; // Not a valid pointer. - return (void *)beg; + return (const void *)beg; } uptr user_alloc_usable_size(const void *p) { @@ -444,7 +444,7 @@ int __sanitizer_get_ownership(const void *p) { return allocator()->GetBlockBegin(p) != 0; } -void *__sanitizer_get_allocated_begin(const void *p) { +const void *__sanitizer_get_allocated_begin(const void *p) { return user_alloc_begin(p); } diff --git a/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp b/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp index 6892a4a7fb282..1683063baea26 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/get_allocated_begin.cpp @@ -23,7 +23,7 @@ int main(void) { // Bogus value to unpoison start. Calling __sanitizer_get_allocated_begin // does not unpoison it. - void *start = NULL; + const void *start = NULL; for (int j = 0; j < sizes[i]; j++) { printf("j: %d\n", j);