[llvm-commits] PATCH: AddressSanitizer: return False for __asan_get_ownership(NULL)

Alexey Samsonov samsonov at google.com
Tue Jan 17 06:20:50 PST 2012


Please review this at http://codereview.appspot.com/5543070/

Affected files:
 M     asan_allocator.cc
 M     asan_interface.h
 M     tests/asan_interface_test.cc

-- 
Alexey Samsonov
Software Engineer, Moscow
samsonov at google.com
-------------- next part --------------
Index: tests/asan_interface_test.cc
===================================================================
--- tests/asan_interface_test.cc	(revision 148305)
+++ tests/asan_interface_test.cc	(working copy)
@@ -50,8 +50,8 @@
   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));
+  // NULL is not owned, but is a valid argument for __asan_get_allocated_size().
+  EXPECT_EQ(false, __asan_get_ownership(NULL));
   EXPECT_EQ(0, __asan_get_allocated_size(NULL));
 
   // When memory is freed, it's not owned, and call to GetAllocatedSize
Index: asan_interface.h
===================================================================
--- asan_interface.h	(revision 148305)
+++ asan_interface.h	(working copy)
@@ -107,11 +107,11 @@
   // 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 (get_ownership(p) == true) or (p == NULL).
   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();
Index: asan_allocator.cc
===================================================================
--- asan_allocator.cc	(revision 148305)
+++ asan_allocator.cc	(working copy)
@@ -811,22 +811,11 @@
   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);
+  if (ptr == NULL) return 0;
+  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 +826,7 @@
 }
 
 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 +1013,14 @@
 }
 
 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);
+  if (p == NULL) return 0;
+  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);


More information about the llvm-commits mailing list