[PATCH] D69208: [asan] Provide an interface to update an allocation stack trace.

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 31 13:55:13 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rG13e04607f75b: [asan] Provide an interface to update an allocation stack trace. (authored by eugenis).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69208/new/

https://reviews.llvm.org/D69208

Files:
  compiler-rt/include/sanitizer/asan_interface.h
  compiler-rt/lib/asan/asan_allocator.cpp
  compiler-rt/lib/asan/asan_interface.inc
  compiler-rt/lib/asan/asan_interface_internal.h
  compiler-rt/test/asan/TestCases/asan_update_allocation.cpp


Index: compiler-rt/test/asan/TestCases/asan_update_allocation.cpp
===================================================================
--- /dev/null
+++ compiler-rt/test/asan/TestCases/asan_update_allocation.cpp
@@ -0,0 +1,19 @@
+// RUN: %clangxx_asan -O0 -DSIZE=10 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
+// RUN: %clangxx_asan -O0 -DSIZE=10000000 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
+// REQUIRES: stable-runtime
+
+#include <stdlib.h>
+#include <sanitizer/asan_interface.h>
+
+void UPDATE(void *p) {
+  __asan_update_allocation_context(p);
+}
+
+int main() {
+  char *x = (char*)malloc(SIZE * sizeof(char));
+  UPDATE(x);
+  free(x);
+  return x[5];
+  // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
+  // CHECK: UPDATE
+}
Index: compiler-rt/lib/asan/asan_interface_internal.h
===================================================================
--- compiler-rt/lib/asan/asan_interface_internal.h
+++ compiler-rt/lib/asan/asan_interface_internal.h
@@ -251,6 +251,8 @@
   const char* __asan_default_suppressions();
 
   SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_vfork(void *sp);
+
+  SANITIZER_INTERFACE_ATTRIBUTE int __asan_update_allocation_context(void* addr);
 }  // extern "C"
 
 #endif  // ASAN_INTERFACE_INTERNAL_H
Index: compiler-rt/lib/asan/asan_interface.inc
===================================================================
--- compiler-rt/lib/asan/asan_interface.inc
+++ compiler-rt/lib/asan/asan_interface.inc
@@ -164,6 +164,7 @@
 INTERFACE_FUNCTION(__sanitizer_unaligned_store16)
 INTERFACE_FUNCTION(__sanitizer_unaligned_store32)
 INTERFACE_FUNCTION(__sanitizer_unaligned_store64)
+INTERFACE_FUNCTION(__asan_update_allocation_context)
 INTERFACE_WEAK_FUNCTION(__asan_default_options)
 INTERFACE_WEAK_FUNCTION(__asan_default_suppressions)
 INTERFACE_WEAK_FUNCTION(__asan_on_error)
Index: compiler-rt/lib/asan/asan_allocator.cpp
===================================================================
--- compiler-rt/lib/asan/asan_allocator.cpp
+++ compiler-rt/lib/asan/asan_allocator.cpp
@@ -399,6 +399,16 @@
     return right_chunk;
   }
 
+  bool UpdateAllocationStack(uptr addr, BufferedStackTrace *stack) {
+    AsanChunk *m = GetAsanChunkByAddr(addr);
+    if (!m) return false;
+    if (m->chunk_state != CHUNK_ALLOCATED) return false;
+    if (m->Beg() != addr) return false;
+    atomic_store((atomic_uint32_t *)&m->alloc_context_id, StackDepotPut(*stack),
+                 memory_order_relaxed);
+    return true;
+  }
+
   // -------------------- Allocation/Deallocation routines ---------------
   void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,
                  AllocType alloc_type, bool can_fill) {
@@ -1112,6 +1122,11 @@
   instance.Purge(&stack);
 }
 
+int __asan_update_allocation_context(void* addr) {
+  GET_STACK_TRACE_MALLOC;
+  return instance.UpdateAllocationStack((uptr)addr, &stack);
+}
+
 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
 // Provide default (no-op) implementation of malloc hooks.
 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_malloc_hook,
Index: compiler-rt/include/sanitizer/asan_interface.h
===================================================================
--- compiler-rt/include/sanitizer/asan_interface.h
+++ compiler-rt/include/sanitizer/asan_interface.h
@@ -315,6 +315,10 @@
 /// functions like <c>_exit()</c> and <c>execl()</c>.
 void __asan_handle_no_return(void);
 
+/// Update allocation stack trace for the given allocation to the current stack
+/// trace. Returns 1 if successfull, 0 if not.
+int __asan_update_allocation_context(void* addr);
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69208.227339.patch
Type: text/x-patch
Size: 3708 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191031/96010dca/attachment.bin>


More information about the llvm-commits mailing list