summaryrefslogtreecommitdiff
path: root/compiler-rt/test
diff options
context:
space:
mode:
authorThurston Dang <thurston@google.com>2023-05-13 00:00:31 +0000
committerThurston Dang <thurston@google.com>2023-05-13 05:26:45 +0000
commit04fd535409ddc601a4654e38ff28db3f13c10713 (patch)
tree95da8bea961be4f6b220f3d7b908f41e08cab2f7 /compiler-rt/test
parent0bbf3ddf5fea86e0eb0726142827e175aadaf53b (diff)
downloadllvm-04fd535409ddc601a4654e38ff28db3f13c10713.tar.gz
ASan: add backtrace_symbols test and clarify code is correct
This is another patch for https://github.com/google/sanitizers/issues/321 (sanitizer interceptors can write to freed memory, causing corruption), in this case for backtrace_symbols. backtrace_symbols is already correct, hence this patch removes the TODO note. Additionally, this patch adds a test case for it. Differential Revision: https://reviews.llvm.org/D150498
Diffstat (limited to 'compiler-rt/test')
-rw-r--r--compiler-rt/test/asan/TestCases/backtrace_symbols_interceptor.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/compiler-rt/test/asan/TestCases/backtrace_symbols_interceptor.cpp b/compiler-rt/test/asan/TestCases/backtrace_symbols_interceptor.cpp
new file mode 100644
index 000000000000..a78969408a7f
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/backtrace_symbols_interceptor.cpp
@@ -0,0 +1,36 @@
+// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// Test the backtrace_symbols() 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);
+
+ int numEntries = backtrace(buffer, MAX_BT);
+ printf("backtrace returned %d entries\n", numEntries);
+
+ free(buffer);
+
+ // Deliberate use-after-free of 'buffer'. We expect ASan to
+ // catch this, without triggering internal sanitizer errors.
+ char **strings = backtrace_symbols(buffer, numEntries);
+ assert(strings != NULL);
+
+ for (int i = 0; i < numEntries; i++) {
+ printf("%s\n", strings[i]);
+ }
+
+ free(strings);
+
+ // CHECK: use-after-free
+ // CHECK: SUMMARY
+ return 0;
+}