diff options
| author | Alexey Samsonov <samsonov@google.com> | 2012-01-17 13:31:54 +0000 |
|---|---|---|
| committer | Alexey Samsonov <samsonov@google.com> | 2012-01-17 13:31:54 +0000 |
| commit | 1e310295d02bbf5f95ad7e8a2e5dcbe92e231bf6 (patch) | |
| tree | f1b24f27703aee51bc8331eeaf3c861128794be4 | |
| parent | 2b2e0721d722a4639f3d3761c8add419090ae465 (diff) | |
| download | bcm5719-llvm-1e310295d02bbf5f95ad7e8a2e5dcbe92e231bf6.tar.gz bcm5719-llvm-1e310295d02bbf5f95ad7e8a2e5dcbe92e231bf6.zip | |
AddressSanitizer: from this patch, ASan allocator returns false for __asan_get_ownership(NULL) and produce error reports for malloc_usable_size(NULL) and __asan_get_allocated_size(NULL)
llvm-svn: 148304
| -rw-r--r-- | compiler-rt/lib/asan/asan_allocator.cc | 32 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/asan_interface.h | 4 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/tests/asan_interface_test.cc | 6 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/tests/asan_test.cc | 2 |
4 files changed, 12 insertions, 32 deletions
diff --git a/compiler-rt/lib/asan/asan_allocator.cc b/compiler-rt/lib/asan/asan_allocator.cc index b5de1f85dc0..e0525a7f447 100644 --- a/compiler-rt/lib/asan/asan_allocator.cc +++ b/compiler-rt/lib/asan/asan_allocator.cc @@ -811,22 +811,10 @@ int asan_posix_memalign(void **memptr, size_t alignment, size_t size, return 0; } -static void GetAllocationSizeAndOwnership(const void *ptr, size_t *size, - bool *owned) { - size_t allocation_size = malloc_info.AllocationSize((uintptr_t)ptr); - if (size != NULL) { - *size = allocation_size; - } - if (owned != NULL) { - *owned = (ptr == NULL) || (allocation_size > 0); - } -} - size_t asan_malloc_usable_size(void *ptr, AsanStackTrace *stack) { - size_t usable_size; - bool owned; - GetAllocationSizeAndOwnership(ptr, &usable_size, &owned); - if (!owned) { + CHECK(stack); + size_t usable_size = malloc_info.AllocationSize((uintptr_t)ptr); + if (usable_size == 0) { Report("ERROR: AddressSanitizer attempting to call malloc_usable_size() " "for pointer which is not owned: %p\n", ptr); stack->PrintStack(); @@ -837,9 +825,7 @@ size_t asan_malloc_usable_size(void *ptr, AsanStackTrace *stack) { } size_t asan_mz_size(const void *ptr) { - size_t mz_size; - GetAllocationSizeAndOwnership(ptr, &mz_size, NULL); - return mz_size; + return malloc_info.AllocationSize((uintptr_t)ptr); } void DescribeHeapAddress(uintptr_t addr, uintptr_t access_size) { @@ -1026,17 +1012,13 @@ size_t __asan_get_estimated_allocated_size(size_t size) { } bool __asan_get_ownership(const void *p) { - bool owned; - GetAllocationSizeAndOwnership(p, NULL, &owned); - return owned; + return malloc_info.AllocationSize((uintptr_t)p) > 0; } size_t __asan_get_allocated_size(const void *p) { - size_t allocated_size; - bool owned; - GetAllocationSizeAndOwnership(p, &allocated_size, &owned); + size_t allocated_size = malloc_info.AllocationSize((uintptr_t)p); // Die if p is not malloced or if it is already freed. - if (!owned) { + if (allocated_size == 0) { Report("ERROR: AddressSanitizer attempting to call " "__asan_get_allocated_size() for pointer which is " "not owned: %p\n", p); diff --git a/compiler-rt/lib/asan/asan_interface.h b/compiler-rt/lib/asan/asan_interface.h index 7506586fefe..90598b40ff8 100644 --- a/compiler-rt/lib/asan/asan_interface.h +++ b/compiler-rt/lib/asan/asan_interface.h @@ -107,11 +107,11 @@ extern "C" { // memory, returns the maximal possible allocation size, otherwise returns // "size". size_t __asan_get_estimated_allocated_size(size_t size); - // Returns true if p is NULL or if p was returned by the ASan allocator and + // Returns true if p was returned by the ASan allocator and // is not yet freed. bool __asan_get_ownership(const void *p); // Returns the number of bytes reserved for the pointer p. - // Requires (get_ownership(p) == true). + // Requires (__asan_get_ownership(p) == true). size_t __asan_get_allocated_size(const void *p); // Number of bytes, allocated and not yet freed by the application. size_t __asan_get_current_allocated_bytes(); diff --git a/compiler-rt/lib/asan/tests/asan_interface_test.cc b/compiler-rt/lib/asan/tests/asan_interface_test.cc index 521853ec756..44a93f9923d 100644 --- a/compiler-rt/lib/asan/tests/asan_interface_test.cc +++ b/compiler-rt/lib/asan/tests/asan_interface_test.cc @@ -44,16 +44,14 @@ TEST(AddressSanitizerInterface, GetAllocatedSizeAndOwnershipTest) { // We cannot call GetAllocatedSize from the memory we didn't map, // and from the interior pointers (not returned by previous malloc). void *wild_addr = (void*)0x1; + EXPECT_EQ(false, __asan_get_ownership(NULL)); + EXPECT_DEATH(__asan_get_allocated_size(NULL), kGetAllocatedSizeErrorMsg); EXPECT_EQ(false, __asan_get_ownership(wild_addr)); EXPECT_DEATH(__asan_get_allocated_size(wild_addr), kGetAllocatedSizeErrorMsg); EXPECT_EQ(false, __asan_get_ownership(array + kArraySize / 2)); EXPECT_DEATH(__asan_get_allocated_size(array + kArraySize / 2), kGetAllocatedSizeErrorMsg); - // NULL is a valid argument and is owned. - EXPECT_EQ(true, __asan_get_ownership(NULL)); - EXPECT_EQ(0, __asan_get_allocated_size(NULL)); - // When memory is freed, it's not owned, and call to GetAllocatedSize // is forbidden. free(array); diff --git a/compiler-rt/lib/asan/tests/asan_test.cc b/compiler-rt/lib/asan/tests/asan_test.cc index 363bb893e98..ca4aab6fd71 100644 --- a/compiler-rt/lib/asan/tests/asan_test.cc +++ b/compiler-rt/lib/asan/tests/asan_test.cc @@ -528,9 +528,9 @@ TEST(AddressSanitizer, MallocUsableSizeTest) { const size_t kArraySize = 100; char *array = Ident((char*)malloc(kArraySize)); int *int_ptr = Ident(new int); - EXPECT_EQ(0, malloc_usable_size(NULL)); EXPECT_EQ(kArraySize, malloc_usable_size(array)); EXPECT_EQ(sizeof(int), malloc_usable_size(int_ptr)); + EXPECT_DEATH(malloc_usable_size(NULL), kMallocUsableSizeErrorMsg); EXPECT_DEATH(malloc_usable_size((void*)0x123), kMallocUsableSizeErrorMsg); EXPECT_DEATH(malloc_usable_size(array + kArraySize / 2), kMallocUsableSizeErrorMsg); |

