[hwasan] make malloc(0) return nullptr, add basic address description for stack addresses
authorKostya Serebryany <kcc@google.com>
Fri, 31 Aug 2018 01:38:00 +0000 (01:38 +0000)
committerKostya Serebryany <kcc@google.com>
Fri, 31 Aug 2018 01:38:00 +0000 (01:38 +0000)
llvm-svn: 341156

compiler-rt/lib/hwasan/hwasan_allocator.cc
compiler-rt/lib/hwasan/hwasan_report.cc
compiler-rt/test/hwasan/TestCases/malloc-test.c [new file with mode: 0644]
compiler-rt/test/hwasan/TestCases/realloc-test.cc
compiler-rt/test/hwasan/TestCases/stack-oob.cc
compiler-rt/test/hwasan/TestCases/stack-uar.cc

index c531a42..6b62b6a 100644 (file)
@@ -121,6 +121,7 @@ void HwasanThreadLocalMallocStorage::CommitBack() {
 
 static void *HwasanAllocate(StackTrace *stack, uptr size, uptr alignment,
                           bool zeroise) {
+  if (!size) return nullptr;
   alignment = Max(alignment, kShadowAlignment);
   size = RoundUpTo(size, kShadowAlignment);
 
index 3d333ff..807d739 100644 (file)
@@ -84,6 +84,12 @@ void PrintAddressDescription(uptr tagged_addr, uptr access_size) {
 
       num_descriptions_printed++;
     }
+    if (t->AddrIsInStack(untagged_addr)) {
+      Printf("%s", d.Location());
+      Printf("Address %p is located in stack of thread %p\n", untagged_addr, t);
+      Printf("%s", d.Default());
+      num_descriptions_printed++;
+    }
   });
 
   if (!num_descriptions_printed)
diff --git a/compiler-rt/test/hwasan/TestCases/malloc-test.c b/compiler-rt/test/hwasan/TestCases/malloc-test.c
new file mode 100644 (file)
index 0000000..13d04e6
--- /dev/null
@@ -0,0 +1,13 @@
+// Test basic malloc functionality.
+// RUN: %clang_hwasan %s -o %t
+// RUN: %run %t
+
+#include <stdlib.h>
+#include <assert.h>
+#include <sanitizer/hwasan_interface.h>
+
+int main() {
+  __hwasan_enable_allocator_tagging();
+  char *a1 = (char*)malloc(0);
+  assert(a1 == NULL);  // may not be true for other malloc.
+}
index 23bc619..8387902 100644 (file)
@@ -4,8 +4,10 @@
 
 #include <stdlib.h>
 #include <assert.h>
+#include <sanitizer/hwasan_interface.h>
 
 int main() {
+  __hwasan_enable_allocator_tagging();
   char *x = (char*)realloc(nullptr, 4);
   x[0] = 10;
   x[1] = 20;
index 60b9a62..caa0de3 100644 (file)
@@ -19,7 +19,7 @@ int main() {
   // CHECK: READ of size 1 at
   // CHECK: #0 {{.*}} in f{{.*}}stack-oob.cc:14
 
-  // CHECK: HWAddressSanitizer can not describe address in more detail.
+  // CHECK: is located in stack of threa
 
   // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in f
 }
index e99dcce..b2bf1e1 100644 (file)
@@ -17,7 +17,7 @@ int main() {
   // CHECK: READ of size 1 at
   // CHECK: #0 {{.*}} in main{{.*}}stack-uar.cc:16
 
-  // CHECK: HWAddressSanitizer can not describe address in more detail.
+  // CHECK: is located in stack of thread
 
   // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in main
 }