diff options
author | Alexey Samsonov <vonosmas@gmail.com> | 2014-07-07 17:39:31 +0000 |
---|---|---|
committer | Alexey Samsonov <vonosmas@gmail.com> | 2014-07-07 17:39:31 +0000 |
commit | 2ef0c30d2101ca6521b7ecbdd17e4fbcca09a951 (patch) | |
tree | 362337877acfa4824664514a5a3b54216bc670c1 /lib/msan | |
parent | 58dd99ec88cc51a5a1adcd4eddff212576a9067c (diff) |
Generalize sanitizer allocator public interface.
Introduce new public header <sanitizer/allocator_interface.h> and a set
of functions __sanitizer_get_ownership(), __sanitizer_malloc_hook() etc.
that will eventually replace their tool-specific equivalents
(__asan_get_ownership(), __msan_get_ownership() etc.). Tool-specific
functions are now deprecated and implemented as stubs redirecting
to __sanitizer_ versions (which are implemented differently in each tool).
Replace all uses of __xsan_ versions with __sanitizer_ versions in unit
and lit tests.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@212469 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/msan')
-rw-r--r-- | lib/msan/msan.h | 6 | ||||
-rw-r--r-- | lib/msan/msan_allocator.cc | 36 | ||||
-rw-r--r-- | lib/msan/msan_interceptors.cc | 3 | ||||
-rw-r--r-- | lib/msan/msan_interface_internal.h | 11 | ||||
-rw-r--r-- | lib/msan/tests/msan_test.cc | 35 |
5 files changed, 56 insertions, 35 deletions
diff --git a/lib/msan/msan.h b/lib/msan/msan.h index f8b79a7d1..05a8c476b 100644 --- a/lib/msan/msan.h +++ b/lib/msan/msan.h @@ -141,8 +141,10 @@ void MsanTSDDtor(void *tsd); } // namespace __msan #define MSAN_MALLOC_HOOK(ptr, size) \ - if (&__msan_malloc_hook) __msan_malloc_hook(ptr, size) + if (&__msan_malloc_hook) __msan_malloc_hook(ptr, size); \ + if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size) #define MSAN_FREE_HOOK(ptr) \ - if (&__msan_free_hook) __msan_free_hook(ptr) + if (&__msan_free_hook) __msan_free_hook(ptr); \ + if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr) #endif // MSAN_H diff --git a/lib/msan/msan_allocator.cc b/lib/msan/msan_allocator.cc index cb8af279d..fb1788f2a 100644 --- a/lib/msan/msan_allocator.cc +++ b/lib/msan/msan_allocator.cc @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "sanitizer_common/sanitizer_allocator.h" +#include "sanitizer_common/sanitizer_allocator_interface.h" #include "sanitizer_common/sanitizer_stackdepot.h" #include "msan.h" #include "msan_allocator.h" @@ -182,24 +183,45 @@ static uptr AllocationSize(const void *p) { using namespace __msan; -uptr __msan_get_current_allocated_bytes() { +uptr __sanitizer_get_current_allocated_bytes() { uptr stats[AllocatorStatCount]; allocator.GetStats(stats); return stats[AllocatorStatAllocated]; } +uptr __msan_get_current_allocated_bytes() { + return __sanitizer_get_current_allocated_bytes(); +} -uptr __msan_get_heap_size() { +uptr __sanitizer_get_heap_size() { uptr stats[AllocatorStatCount]; allocator.GetStats(stats); return stats[AllocatorStatMapped]; } +uptr __msan_get_heap_size() { + return __sanitizer_get_heap_size(); +} -uptr __msan_get_free_bytes() { return 1; } +uptr __sanitizer_get_free_bytes() { return 1; } +uptr __msan_get_free_bytes() { + return __sanitizer_get_free_bytes(); +} -uptr __msan_get_unmapped_bytes() { return 1; } +uptr __sanitizer_get_unmapped_bytes() { return 1; } +uptr __msan_get_unmapped_bytes() { + return __sanitizer_get_unmapped_bytes(); +} -uptr __msan_get_estimated_allocated_size(uptr size) { return size; } +uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; } +uptr __msan_get_estimated_allocated_size(uptr size) { + return __sanitizer_get_estimated_allocated_size(size); +} -int __msan_get_ownership(const void *p) { return AllocationSize(p) != 0; } +int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; } +int __msan_get_ownership(const void *p) { + return __sanitizer_get_ownership(p); +} -uptr __msan_get_allocated_size(const void *p) { return AllocationSize(p); } +uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); } +uptr __msan_get_allocated_size(const void *p) { + return __sanitizer_get_allocated_size(p); +} diff --git a/lib/msan/msan_interceptors.cc b/lib/msan/msan_interceptors.cc index 74a19b851..ba0999a5e 100644 --- a/lib/msan/msan_interceptors.cc +++ b/lib/msan/msan_interceptors.cc @@ -21,6 +21,7 @@ #include "msan_thread.h" #include "sanitizer_common/sanitizer_platform_limits_posix.h" #include "sanitizer_common/sanitizer_allocator.h" +#include "sanitizer_common/sanitizer_allocator_interface.h" #include "sanitizer_common/sanitizer_allocator_internal.h" #include "sanitizer_common/sanitizer_atomic.h" #include "sanitizer_common/sanitizer_common.h" @@ -208,7 +209,7 @@ INTERCEPTOR(void, cfree, void *ptr) { } INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { - return __msan_get_allocated_size(ptr); + return __sanitizer_get_allocated_size(ptr); } // This function actually returns a struct by value, but we can't unpoison a diff --git a/lib/msan/msan_interface_internal.h b/lib/msan/msan_interface_internal.h index c005c2a36..47b47dc4b 100644 --- a/lib/msan/msan_interface_internal.h +++ b/lib/msan/msan_interface_internal.h @@ -161,32 +161,27 @@ void __sanitizer_unaligned_store32(uu32 *p, u32 x); SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_unaligned_store64(uu64 *p, u64 x); +// --------------------------- +// FIXME: Replace these functions with __sanitizer equivalent. SANITIZER_INTERFACE_ATTRIBUTE uptr __msan_get_estimated_allocated_size(uptr size); - SANITIZER_INTERFACE_ATTRIBUTE int __msan_get_ownership(const void *p); - SANITIZER_INTERFACE_ATTRIBUTE uptr __msan_get_allocated_size(const void *p); - SANITIZER_INTERFACE_ATTRIBUTE uptr __msan_get_current_allocated_bytes(); - SANITIZER_INTERFACE_ATTRIBUTE uptr __msan_get_heap_size(); - SANITIZER_INTERFACE_ATTRIBUTE uptr __msan_get_free_bytes(); - SANITIZER_INTERFACE_ATTRIBUTE uptr __msan_get_unmapped_bytes(); - SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE /* OPTIONAL */ void __msan_malloc_hook(void *ptr, uptr size); - SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE /* OPTIONAL */ void __msan_free_hook(void *ptr); +// --------------------------- SANITIZER_INTERFACE_ATTRIBUTE void __msan_dr_is_initialized(); diff --git a/lib/msan/tests/msan_test.cc b/lib/msan/tests/msan_test.cc index 0596a6f4b..c60099637 100644 --- a/lib/msan/tests/msan_test.cc +++ b/lib/msan/tests/msan_test.cc @@ -18,6 +18,7 @@ #include "sanitizer_common/tests/sanitizer_test_utils.h" +#include "sanitizer/allocator_interface.h" #include "sanitizer/msan_interface.h" #include "msandr_test_so.h" @@ -3022,12 +3023,12 @@ TEST(MemorySanitizer, valloc) { TEST(MemorySanitizer, pvalloc) { void *p = pvalloc(kPageSize + 100); EXPECT_EQ(0U, (uintptr_t)p % kPageSize); - EXPECT_EQ(2 * kPageSize, __msan_get_allocated_size(p)); + EXPECT_EQ(2 * kPageSize, __sanitizer_get_allocated_size(p)); free(p); p = pvalloc(0); // pvalloc(0) should allocate at least one page. EXPECT_EQ(0U, (uintptr_t)p % kPageSize); - EXPECT_EQ(kPageSize, __msan_get_allocated_size(p)); + EXPECT_EQ(kPageSize, __sanitizer_get_allocated_size(p)); free(p); } @@ -4076,7 +4077,7 @@ TEST(MemorySanitizerStress, DISABLED_MallocStackTrace) { TEST(MemorySanitizerAllocator, get_estimated_allocated_size) { size_t sizes[] = {0, 20, 5000, 1<<20}; for (size_t i = 0; i < sizeof(sizes) / sizeof(*sizes); ++i) { - size_t alloc_size = __msan_get_estimated_allocated_size(sizes[i]); + size_t alloc_size = __sanitizer_get_estimated_allocated_size(sizes[i]); EXPECT_EQ(alloc_size, sizes[i]); } } @@ -4085,26 +4086,26 @@ TEST(MemorySanitizerAllocator, get_allocated_size_and_ownership) { char *array = reinterpret_cast<char*>(malloc(100)); int *int_ptr = new int; - EXPECT_TRUE(__msan_get_ownership(array)); - EXPECT_EQ(100U, __msan_get_allocated_size(array)); + EXPECT_TRUE(__sanitizer_get_ownership(array)); + EXPECT_EQ(100U, __sanitizer_get_allocated_size(array)); - EXPECT_TRUE(__msan_get_ownership(int_ptr)); - EXPECT_EQ(sizeof(*int_ptr), __msan_get_allocated_size(int_ptr)); + EXPECT_TRUE(__sanitizer_get_ownership(int_ptr)); + EXPECT_EQ(sizeof(*int_ptr), __sanitizer_get_allocated_size(int_ptr)); void *wild_addr = reinterpret_cast<void*>(0x1); - EXPECT_FALSE(__msan_get_ownership(wild_addr)); - EXPECT_EQ(0U, __msan_get_allocated_size(wild_addr)); + EXPECT_FALSE(__sanitizer_get_ownership(wild_addr)); + EXPECT_EQ(0U, __sanitizer_get_allocated_size(wild_addr)); - EXPECT_FALSE(__msan_get_ownership(array + 50)); - EXPECT_EQ(0U, __msan_get_allocated_size(array + 50)); + EXPECT_FALSE(__sanitizer_get_ownership(array + 50)); + EXPECT_EQ(0U, __sanitizer_get_allocated_size(array + 50)); + + // NULL is a valid argument for GetAllocatedSize but is not owned. + EXPECT_FALSE(__sanitizer_get_ownership(NULL)); + EXPECT_EQ(0U, __sanitizer_get_allocated_size(NULL)); - // NULL is a valid argument for GetAllocatedSize but is not owned. - EXPECT_FALSE(__msan_get_ownership(NULL)); - EXPECT_EQ(0U, __msan_get_allocated_size(NULL)); - free(array); - EXPECT_FALSE(__msan_get_ownership(array)); - EXPECT_EQ(0U, __msan_get_allocated_size(array)); + EXPECT_FALSE(__sanitizer_get_ownership(array)); + EXPECT_EQ(0U, __sanitizer_get_allocated_size(array)); delete int_ptr; } |