summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimur Iskhodzhanov <timurrrr@google.com>2014-05-15 16:02:56 +0000
committerTimur Iskhodzhanov <timurrrr@google.com>2014-05-15 16:02:56 +0000
commit2d53385ccf252adcc71de2d1fb28eeeb65af1f79 (patch)
tree48e7578e5260d94275ca90ceb50a5027d107425e
parente7ad96d9cd81483a74d7b622f129e12fa8e2b1e3 (diff)
[ASan/Win tests] Add memcpy/strdup/strlen interception tests
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@208899 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--test/asan/TestCases/Windows/intercept_memcpy.cc32
-rw-r--r--test/asan/TestCases/Windows/intercept_strdup.cc28
-rw-r--r--test/asan/TestCases/Windows/intercept_strlen.cc28
3 files changed, 88 insertions, 0 deletions
diff --git a/test/asan/TestCases/Windows/intercept_memcpy.cc b/test/asan/TestCases/Windows/intercept_memcpy.cc
new file mode 100644
index 000000000..4e52b1a90
--- /dev/null
+++ b/test/asan/TestCases/Windows/intercept_memcpy.cc
@@ -0,0 +1,32 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <stdio.h>
+#include <string.h>
+
+void call_memcpy(void* (*f)(void *, const void *, size_t),
+ void *a, const void *b, size_t c) {
+ f(a, b, c);
+}
+
+int main() {
+ char buff1[6] = "Hello", buff2[5];
+
+ call_memcpy(&memcpy, buff2, buff1, 5);
+ if (buff1[2] != buff2[2])
+ return 2;
+ printf("Initial test OK\n");
+ fflush(0);
+// CHECK: Initial test OK
+
+ call_memcpy(&memcpy, buff2, buff1, 6);
+// CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 6 at [[ADDR]] thread T0
+// CHECK: __asan_memcpy
+// CHECK-NEXT: call_memcpy
+// CHECK: main {{.*}}intercept_memcpy.cc:[[@LINE-5]]
+// CHECK: Address [[ADDR]] is located in stack of thread T0 at offset {{.*}} in frame
+// CHECK-NEXT: #0 {{.*}} main
+// CHECK: 'buff2' <== Memory access at offset {{.*}} overflows this variable
+}
diff --git a/test/asan/TestCases/Windows/intercept_strdup.cc b/test/asan/TestCases/Windows/intercept_strdup.cc
new file mode 100644
index 000000000..1e1a26d6a
--- /dev/null
+++ b/test/asan/TestCases/Windows/intercept_strdup.cc
@@ -0,0 +1,28 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <stdio.h>
+#include <string.h>
+#include <malloc.h>
+
+int main() {
+ char *ptr = _strdup("Hello");
+ int subscript = 1;
+ ptr[subscript] = '3';
+ printf("%s\n", ptr);
+ fflush(0);
+// CHECK: H3llo
+
+ subscript = -1;
+ ptr[subscript] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK: {{#0 .* main .*}}intercept_strdup.cc:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 1 bytes to the left of 6-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK: {{#0 .* malloc }}
+// CHECK: {{#1 .* _strdup }}
+// CHECK: {{#2 .* main .*}}intercept_strdup.cc:[[@LINE-16]]
+ free(ptr);
+}
diff --git a/test/asan/TestCases/Windows/intercept_strlen.cc b/test/asan/TestCases/Windows/intercept_strlen.cc
new file mode 100644
index 000000000..f32f40335
--- /dev/null
+++ b/test/asan/TestCases/Windows/intercept_strlen.cc
@@ -0,0 +1,28 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+ char str[] = "Hello";
+ if (5 != strlen(str))
+ return 1;
+
+ printf("Initial test OK\n");
+ fflush(0);
+// CHECK: Initial test OK
+
+ str[5] = '!'; // Losing '\0' at the end.
+ int len = strlen(str);
+// CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// FIXME: Should be READ of size 1, see issue 155.
+// CHECK: READ of size {{[0-9]+}} at [[ADDR]] thread T0
+// CHECK: strlen
+// CHECK-NEXT: main {{.*}}intercept_strlen.cc:[[@LINE-5]]
+// CHECK: Address [[ADDR]] is located in stack of thread T0 at offset {{.*}} in frame
+// CHECK-NEXT: main {{.*}}intercept_strlen.cc
+// CHECK: 'str' <== Memory access at offset {{.*}} overflows this variable
+ return len < 6;
+}