[compiler-rt] r341156 - [hwasan] make malloc(0) return nullptr, add basic address description for stack addresses

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 30 18:38:00 PDT 2018


Author: kcc
Date: Thu Aug 30 18:38:00 2018
New Revision: 341156

URL: http://llvm.org/viewvc/llvm-project?rev=341156&view=rev
Log:
[hwasan] make malloc(0) return nullptr, add basic address description for stack addresses

Added:
    compiler-rt/trunk/test/hwasan/TestCases/malloc-test.c
Modified:
    compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc
    compiler-rt/trunk/lib/hwasan/hwasan_report.cc
    compiler-rt/trunk/test/hwasan/TestCases/realloc-test.cc
    compiler-rt/trunk/test/hwasan/TestCases/stack-oob.cc
    compiler-rt/trunk/test/hwasan/TestCases/stack-uar.cc

Modified: compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc?rev=341156&r1=341155&r2=341156&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_allocator.cc Thu Aug 30 18:38:00 2018
@@ -121,6 +121,7 @@ void HwasanThreadLocalMallocStorage::Com
 
 static void *HwasanAllocate(StackTrace *stack, uptr size, uptr alignment,
                           bool zeroise) {
+  if (!size) return nullptr;
   alignment = Max(alignment, kShadowAlignment);
   size = RoundUpTo(size, kShadowAlignment);
 

Modified: compiler-rt/trunk/lib/hwasan/hwasan_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_report.cc?rev=341156&r1=341155&r2=341156&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_report.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_report.cc Thu Aug 30 18:38:00 2018
@@ -84,6 +84,12 @@ void PrintAddressDescription(uptr tagged
 
       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)

Added: compiler-rt/trunk/test/hwasan/TestCases/malloc-test.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/hwasan/TestCases/malloc-test.c?rev=341156&view=auto
==============================================================================
--- compiler-rt/trunk/test/hwasan/TestCases/malloc-test.c (added)
+++ compiler-rt/trunk/test/hwasan/TestCases/malloc-test.c Thu Aug 30 18:38:00 2018
@@ -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.
+}

Modified: compiler-rt/trunk/test/hwasan/TestCases/realloc-test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/hwasan/TestCases/realloc-test.cc?rev=341156&r1=341155&r2=341156&view=diff
==============================================================================
--- compiler-rt/trunk/test/hwasan/TestCases/realloc-test.cc (original)
+++ compiler-rt/trunk/test/hwasan/TestCases/realloc-test.cc Thu Aug 30 18:38:00 2018
@@ -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;

Modified: compiler-rt/trunk/test/hwasan/TestCases/stack-oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/hwasan/TestCases/stack-oob.cc?rev=341156&r1=341155&r2=341156&view=diff
==============================================================================
--- compiler-rt/trunk/test/hwasan/TestCases/stack-oob.cc (original)
+++ compiler-rt/trunk/test/hwasan/TestCases/stack-oob.cc Thu Aug 30 18:38:00 2018
@@ -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
 }

Modified: compiler-rt/trunk/test/hwasan/TestCases/stack-uar.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/hwasan/TestCases/stack-uar.cc?rev=341156&r1=341155&r2=341156&view=diff
==============================================================================
--- compiler-rt/trunk/test/hwasan/TestCases/stack-uar.cc (original)
+++ compiler-rt/trunk/test/hwasan/TestCases/stack-uar.cc Thu Aug 30 18:38:00 2018
@@ -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
 }




More information about the llvm-commits mailing list