[compiler-rt] 0fd7c49 - [ubsan] Add runtime test for -fsanitize=local-bounds (#120038)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 18 15:23:38 PST 2024
Author: Vitaly Buka
Date: 2024-12-18T15:23:34-08:00
New Revision: 0fd7c49a770da936b704ea9b2162a85377b1e374
URL: https://github.com/llvm/llvm-project/commit/0fd7c49a770da936b704ea9b2162a85377b1e374
DIFF: https://github.com/llvm/llvm-project/commit/0fd7c49a770da936b704ea9b2162a85377b1e374.diff
LOG: [ubsan] Add runtime test for -fsanitize=local-bounds (#120038)
Added:
compiler-rt/test/ubsan/TestCases/Misc/local_bounds.cpp
compiler-rt/test/ubsan_minimal/TestCases/local_bounds.cpp
Modified:
Removed:
################################################################################
diff --git a/compiler-rt/test/ubsan/TestCases/Misc/local_bounds.cpp b/compiler-rt/test/ubsan/TestCases/Misc/local_bounds.cpp
new file mode 100644
index 00000000000000..edfe439c92790d
--- /dev/null
+++ b/compiler-rt/test/ubsan/TestCases/Misc/local_bounds.cpp
@@ -0,0 +1,28 @@
+// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && %run %t 1
+// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && not --crash %run %t 3
+
+// FIXME: it's always trap for now.
+
+#include <cstdlib>
+
+struct S {
+ int k;
+ int l;
+};
+
+__attribute__((noinline)) void init(S *s) {
+ __asm__ __volatile__("" : : "r"(s) : "memory");
+}
+
+__attribute__((noinline, no_sanitize("memory"))) int test(char i) {
+ S a;
+ init(&a);
+ S b;
+ init(&b);
+ return ((int *)(&a))[i];
+}
+
+int main(int argc, char **argv) {
+ test(argv[1][0] - '0');
+ return 0;
+}
diff --git a/compiler-rt/test/ubsan_minimal/TestCases/local_bounds.cpp b/compiler-rt/test/ubsan_minimal/TestCases/local_bounds.cpp
new file mode 100644
index 00000000000000..edfe439c92790d
--- /dev/null
+++ b/compiler-rt/test/ubsan_minimal/TestCases/local_bounds.cpp
@@ -0,0 +1,28 @@
+// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && %run %t 1
+// RUN: %clangxx -fsanitize=local-bounds %s -O3 -o %t && not --crash %run %t 3
+
+// FIXME: it's always trap for now.
+
+#include <cstdlib>
+
+struct S {
+ int k;
+ int l;
+};
+
+__attribute__((noinline)) void init(S *s) {
+ __asm__ __volatile__("" : : "r"(s) : "memory");
+}
+
+__attribute__((noinline, no_sanitize("memory"))) int test(char i) {
+ S a;
+ init(&a);
+ S b;
+ init(&b);
+ return ((int *)(&a))[i];
+}
+
+int main(int argc, char **argv) {
+ test(argv[1][0] - '0');
+ return 0;
+}
More information about the llvm-commits
mailing list