[compiler-rt] [HWASAN] Add test to detected use after free in memcmp (PR #67204)

Kirill Stoimenov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 11:08:30 PDT 2023


https://github.com/kstoimenov updated https://github.com/llvm/llvm-project/pull/67204

>From 51758207b88144558b983bc9ccfc5917a826d038 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 22 Sep 2023 22:57:55 +0000
Subject: [PATCH] [HWASAN] Add test to detected use after free in memcmp

---
 .../test/hwasan/TestCases/memcmp_test.cpp       | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 compiler-rt/test/hwasan/TestCases/memcmp_test.cpp

diff --git a/compiler-rt/test/hwasan/TestCases/memcmp_test.cpp b/compiler-rt/test/hwasan/TestCases/memcmp_test.cpp
new file mode 100644
index 000000000000000..8f9bb4320be16af
--- /dev/null
+++ b/compiler-rt/test/hwasan/TestCases/memcmp_test.cpp
@@ -0,0 +1,17 @@
+// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// REQUIRES: compiler-rt-optimized
+
+#include <string.h>
+int main(int argc, char **argv) {
+  char a1[] = {static_cast<char>(argc), 2, 3, 4};
+  char a2[] = {1, static_cast<char>(2*argc), 3, 4};
+  int res = memcmp(a1, a2, 4 + argc);  // BOOM
+  // CHECK: AddressSanitizer: stack-buffer-overflow
+  // CHECK: {{#[0-9]+ .*memcmp}}
+  // CHECK: {{#[0-9]+ .*main}}
+  return res;
+}



More information about the llvm-commits mailing list