[compiler-rt] r266283 - [tsan] Fix size reporting for OS X zone allocator with 0-sized allocations

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 14 02:05:19 PDT 2016


Author: kuba.brecka
Date: Thu Apr 14 04:05:19 2016
New Revision: 266283

URL: http://llvm.org/viewvc/llvm-project?rev=266283&view=rev
Log:
[tsan] Fix size reporting for OS X zone allocator with 0-sized allocations

The custom zone implementation for OS X must not return 0 (even for 0-sized allocations). Returning 0 indicates that the pointer doesn't belong to the zone. This can break existing applications. The underlaying allocator allocates 1 byte for 0-sized allocations anyway, so returning 1 in this case is okay.

Differential Revision: http://reviews.llvm.org/D19100


Added:
    compiler-rt/trunk/test/tsan/Darwin/malloc_size.mm
Modified:
    compiler-rt/trunk/lib/tsan/rtl/tsan_malloc_mac.cc

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_malloc_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_malloc_mac.cc?rev=266283&r1=266282&r2=266283&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_malloc_mac.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_malloc_mac.cc Thu Apr 14 04:05:19 2016
@@ -53,7 +53,8 @@ using namespace __tsan;
   SCOPED_INTERCEPTOR_RAW(free, ptr); \
   user_free(thr, pc, ptr)
 #define COMMON_MALLOC_SIZE(ptr) \
-  uptr size = user_alloc_usable_size(ptr);
+  uptr size = user_alloc_usable_size(ptr); \
+  if (size == 0) size = 1;
 #define COMMON_MALLOC_FILL_STATS(zone, stats)
 #define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
   (void)zone_name; \

Added: compiler-rt/trunk/test/tsan/Darwin/malloc_size.mm
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/Darwin/malloc_size.mm?rev=266283&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/Darwin/malloc_size.mm (added)
+++ compiler-rt/trunk/test/tsan/Darwin/malloc_size.mm Thu Apr 14 04:05:19 2016
@@ -0,0 +1,23 @@
+// Test that malloc_zone_from_ptr returns a valid zone for a 0-sized allocation.
+
+// RUN: %clang_tsan %s -o %t -framework Foundation
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#import <Foundation/Foundation.h>
+#include <malloc/malloc.h>
+
+int main() {
+  void *p = malloc(0);
+
+  size_t s = malloc_size(p);
+  printf("size = 0x%zx\n", s);
+
+  malloc_zone_t *z = malloc_zone_from_ptr(p);
+  if (z)
+    printf("z = %p\n", z);
+  else
+    printf("no zone\n");
+}
+
+// CHECK: z = 0x{{[0-9a-f]+}}
+// CHECK-NOT: no zone




More information about the llvm-commits mailing list