[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
Fri Oct 18 17:29:00 PDT 2019


eugenis created this revision.
eugenis added reviewers: pcc, kcc.
Herald added projects: Sanitizers, LLVM.
Herald added a subscriber: Sanitizers.

Sometimes an allocation stack trace is not very informative. Provide a
way to replace it with a stack trace of the user's choice.


Repository:
  rG LLVM Github Monorepo

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_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(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(void* addr);
 }  // extern "C"
 
 #endif  // ASAN_INTERFACE_INTERNAL_H
Index: compiler-rt/lib/asan/asan_allocator.cpp
===================================================================
--- compiler-rt/lib/asan/asan_allocator.cpp
+++ compiler-rt/lib/asan/asan_allocator.cpp
@@ -394,6 +394,15 @@
     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;
+    m->alloc_context_id = StackDepotPut(*stack);
+    return true;
+  }
+
   // -------------------- Allocation/Deallocation routines ---------------
   void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,
                  AllocType alloc_type, bool can_fill) {
@@ -1105,6 +1114,11 @@
   instance.Purge(&stack);
 }
 
+int __asan_update_allocation(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(void* addr);
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69208.225722.patch
Type: text/x-patch
Size: 3028 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191019/52256df7/attachment.bin>


More information about the llvm-commits mailing list