ASan: add testcase for backtrace interceptor
authorThurston Dang <thurston@google.com>
Fri, 12 May 2023 22:47:54 +0000 (22:47 +0000)
committerThurston Dang <thurston@google.com>
Fri, 12 May 2023 23:02:19 +0000 (23:02 +0000)
It is a known, longstanding issue that some ASan interceptors
may write to freed memory, causing corruption
(https://github.com/google/sanitizers/issues/321). This patch
adds a testcase for the backtrace interceptor (one of the
known cases).

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D150491

compiler-rt/test/asan/TestCases/backtrace_interceptor.cpp [new file with mode: 0644]

diff --git a/compiler-rt/test/asan/TestCases/backtrace_interceptor.cpp b/compiler-rt/test/asan/TestCases/backtrace_interceptor.cpp
new file mode 100644 (file)
index 0000000..8ffcc08
--- /dev/null
@@ -0,0 +1,28 @@
+// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// Interceptor can cause use-after-free
+// (https://github.com/google/sanitizers/issues/321)
+// XFAIL: *
+
+// Test the backtrace() interceptor.
+
+#include <assert.h>
+#include <execinfo.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_BT 100
+
+int main() {
+  void **buffer = (void **)malloc(sizeof(void *) * MAX_BT);
+  assert(buffer != NULL);
+  free(buffer);
+
+  int numEntries = backtrace(buffer, MAX_BT);
+  printf("backtrace returned %d entries\n", numEntries);
+
+  // CHECK: use-after-free
+  // CHECK: SUMMARY
+  return 0;
+}