From f9fef3d275dd5a4a729e16601a7e3e8207f1a171 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Fri, 3 Feb 2012 08:50:16 +0000 Subject: [PATCH] AddressSanitizer: simplify RoundUpToPowerOfTwo/clz; add a couple of CHECKs for real_X calls. Patch by timurrrr@google.com llvm-svn: 149687 --- compiler-rt/lib/asan/asan_allocator.cc | 32 +++++++++++--------------- compiler-rt/lib/asan/asan_poisoning.cc | 1 + 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/compiler-rt/lib/asan/asan_allocator.cc b/compiler-rt/lib/asan/asan_allocator.cc index 3a8f9a7e910b..4f4bed4b51f0 100644 --- a/compiler-rt/lib/asan/asan_allocator.cc +++ b/compiler-rt/lib/asan/asan_allocator.cc @@ -78,28 +78,21 @@ static inline size_t Log2(size_t x) { #endif } -static inline size_t clz(size_t x) { -#if defined(_WIN64) - unsigned long ret; // NOLINT - _BitScanReverse64(&ret, x); - return ret; -#elif defined(_WIN32) - unsigned long ret; // NOLINT - _BitScanReverse(&ret, x); - return ret; -#else - return __builtin_clzl(x); -#endif -} - static inline size_t RoundUpToPowerOfTwo(size_t size) { CHECK(size); if (IsPowerOfTwo(size)) return size; - size_t up = __WORDSIZE - clz(size); - CHECK(size < (1ULL << up)); - CHECK(size > (1ULL << (up - 1))); - return 1UL << up; + unsigned long up; // NOLINT +#if defined(_WIN64) + _BitScanReverse64(&up, size); +#elif defined(_WIN32) + _BitScanReverse(&up, size); +#else + up = __WORDSIZE - 1 - __builtin_clzl(size); +#endif + CHECK(size < (1ULL << (up + 1))); + CHECK(size > (1ULL << up)); + return 1UL << (up + 1); } static inline size_t SizeClassToSize(uint8_t size_class) { @@ -747,6 +740,7 @@ static uint8_t *Reallocate(uint8_t *old_ptr, size_t new_size, size_t memcpy_size = Min(new_size, old_size); uint8_t *new_ptr = Allocate(0, new_size, stack); if (new_ptr) { + CHECK(real_memcpy != NULL); real_memcpy(new_ptr, old_ptr, memcpy_size); Deallocate(old_ptr, stack); } @@ -873,7 +867,7 @@ void asan_mz_force_unlock() { // ---------------------- Fake stack-------------------- {{{1 FakeStack::FakeStack() { - CHECK(real_memset); + CHECK(real_memset != NULL); real_memset(this, 0, sizeof(*this)); } diff --git a/compiler-rt/lib/asan/asan_poisoning.cc b/compiler-rt/lib/asan/asan_poisoning.cc index daa1ad68e46c..61f6fe45cc9a 100644 --- a/compiler-rt/lib/asan/asan_poisoning.cc +++ b/compiler-rt/lib/asan/asan_poisoning.cc @@ -24,6 +24,7 @@ void PoisonShadow(uintptr_t addr, size_t size, uint8_t value) { CHECK(AddrIsAlignedByGranularity(addr + size)); uintptr_t shadow_beg = MemToShadow(addr); uintptr_t shadow_end = MemToShadow(addr + size); + CHECK(real_memset != NULL); real_memset((void*)shadow_beg, value, shadow_end - shadow_beg); }