From 2d53385ccf252adcc71de2d1fb28eeeb65af1f79 Mon Sep 17 00:00:00 2001 From: Timur Iskhodzhanov Date: Thu, 15 May 2014 16:02:56 +0000 Subject: [PATCH] [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 --- test/asan/TestCases/Windows/intercept_memcpy.cc | 32 +++++++++++++++++++++++++ test/asan/TestCases/Windows/intercept_strdup.cc | 28 ++++++++++++++++++++++ test/asan/TestCases/Windows/intercept_strlen.cc | 28 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 test/asan/TestCases/Windows/intercept_memcpy.cc create mode 100644 test/asan/TestCases/Windows/intercept_strdup.cc create mode 100644 test/asan/TestCases/Windows/intercept_strlen.cc 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 +#include + +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 +#include +#include + +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 +#include + +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; +} -- 2.11.4.GIT