From eafa65ae70968b7e3c655da79c47321346bb7c42 Mon Sep 17 00:00:00 2001 From: Timur Iskhodzhanov Date: Thu, 15 May 2014 14:42:41 +0000 Subject: [PATCH] [ASan/Win tests] Add more DLL tests: malloc & friends git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@208889 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/asan/TestCases/Windows/dll_aligned_mallocs.cc | 34 +++++++++++++++++++ .../TestCases/Windows/dll_allocators_sanity.cc | 39 ++++++++++++++++++++++ ...oc_left_oob_crash.cc => dll_malloc_left_oob.cc} | 4 +-- test/asan/TestCases/Windows/dll_malloc_uaf.cc | 27 +++++++++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 test/asan/TestCases/Windows/dll_aligned_mallocs.cc create mode 100644 test/asan/TestCases/Windows/dll_allocators_sanity.cc rename test/asan/TestCases/Windows/{dll_malloc_left_oob_crash.cc => dll_malloc_left_oob.cc} (82%) create mode 100644 test/asan/TestCases/Windows/dll_malloc_uaf.cc diff --git a/test/asan/TestCases/Windows/dll_aligned_mallocs.cc b/test/asan/TestCases/Windows/dll_aligned_mallocs.cc new file mode 100644 index 000000000..db2b9d19b --- /dev/null +++ b/test/asan/TestCases/Windows/dll_aligned_mallocs.cc @@ -0,0 +1,34 @@ +// RUN: %clangxx_asan -O0 %p/dll_host.cc -Fe%t +// RUN: %clangxx_asan -LD -O0 %s -Fe%t.dll +// RUN: %run %t %t.dll | FileCheck %s + +#include +#include + +#define CHECK_ALIGNED(ptr,alignment) \ + do { \ + if (((uintptr_t)(ptr) % (alignment)) != 0) \ + return __LINE__; \ + } \ + while(0) + +extern "C" __declspec(dllexport) +int test_function() { + int *p = (int*)_aligned_malloc(1024 * sizeof(int), 32); + CHECK_ALIGNED(p, 32); + p[512] = 0; + _aligned_free(p); + + p = (int*)_aligned_malloc(128, 128); + CHECK_ALIGNED(p, 128); + p = (int*)_aligned_realloc(p, 2048 * sizeof(int), 128); + CHECK_ALIGNED(p, 128); + p[1024] = 0; + if (_aligned_msize(p, 128, 0) != 2048 * sizeof(int)) + return __LINE__; + _aligned_free(p); + + printf("All ok\n"); +// CHECK: All ok + return 0; +} diff --git a/test/asan/TestCases/Windows/dll_allocators_sanity.cc b/test/asan/TestCases/Windows/dll_allocators_sanity.cc new file mode 100644 index 000000000..2f3f78fe8 --- /dev/null +++ b/test/asan/TestCases/Windows/dll_allocators_sanity.cc @@ -0,0 +1,39 @@ +// RUN: %clangxx_asan -O0 %p/dll_host.cc -Fe%t +// RUN: %clangxx_asan -LD -O0 %s -Fe%t.dll +// RUN: %run %t %t.dll | FileCheck %s + +#include +#include + +extern "C" __declspec(dllexport) +int test_function() { + int *p = (int*)malloc(1024 * sizeof(int)); + p[512] = 0; + free(p); + + p = (int*)malloc(128); + p = (int*)realloc(p, 2048 * sizeof(int)); + p[1024] = 0; + free(p); + + p = (int*)calloc(16, sizeof(int)); + if (p[8] != 0) + return 1; + p[15]++; + if (16 * sizeof(int) != _msize(p)) + return 2; + free(p); + + p = new int; + *p = 42; + delete p; + + p = new int[42]; + p[15]++; + delete [] p; + + printf("All ok\n"); +// CHECK: All ok + + return 0; +} diff --git a/test/asan/TestCases/Windows/dll_malloc_left_oob_crash.cc b/test/asan/TestCases/Windows/dll_malloc_left_oob.cc similarity index 82% rename from test/asan/TestCases/Windows/dll_malloc_left_oob_crash.cc rename to test/asan/TestCases/Windows/dll_malloc_left_oob.cc index 8084337b0..75c90c100 100644 --- a/test/asan/TestCases/Windows/dll_malloc_left_oob_crash.cc +++ b/test/asan/TestCases/Windows/dll_malloc_left_oob.cc @@ -10,12 +10,12 @@ int test_function() { buffer[-1] = 42; // CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]] // CHECK: WRITE of size 1 at [[ADDR]] thread T0 -// CHECK: test_function {{.*}}dll_malloc_left_oob_crash.cc:[[@LINE-3]] +// CHECK: test_function {{.*}}dll_malloc_left_oob.cc:[[@LINE-3]] // CHECK: main {{.*}}dll_host.cc // CHECK: [[ADDR]] is located 1 bytes to the left of 42-byte region // CHECK-LABEL: allocated by thread T0 here: // CHECK: malloc -// CHECK: test_function {{.*}}dll_malloc_left_oob_crash.cc:[[@LINE-9]] +// CHECK: test_function {{.*}}dll_malloc_left_oob.cc:[[@LINE-9]] // CHECK: main {{.*}}dll_host.cc // CHECK-LABEL: SUMMARY free(buffer); diff --git a/test/asan/TestCases/Windows/dll_malloc_uaf.cc b/test/asan/TestCases/Windows/dll_malloc_uaf.cc new file mode 100644 index 000000000..e65cf81af --- /dev/null +++ b/test/asan/TestCases/Windows/dll_malloc_uaf.cc @@ -0,0 +1,27 @@ +// RUN: %clangxx_asan -O0 %p/dll_host.cc -Fe%t +// RUN: %clangxx_asan -LD -O0 %s -Fe%t.dll +// FIXME: 'cat' is needed due to PR19744. +// RUN: not %run %t %t.dll 2>&1 | cat | FileCheck %s + +#include + +extern "C" __declspec(dllexport) +int test_function() { + char *buffer = (char*)malloc(42); + free(buffer); + buffer[0] = 42; +// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]] +// CHECK: WRITE of size 1 at [[ADDR]] thread T0 +// CHECK: test_function {{.*}}dll_malloc_uaf.cc:[[@LINE-3]] +// CHECK: main {{.*}}dll_host +// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region +// CHECK-LABEL: freed by thread T0 here: +// CHECK: free +// CHECK: test_function {{.*}}dll_malloc_uaf.cc:[[@LINE-9]] +// CHECK: main {{.*}}dll_host +// CHECK-LABEL: previously allocated by thread T0 here: +// CHECK: malloc +// CHECK: test_function {{.*}}dll_malloc_uaf.cc:[[@LINE-14]] +// CHECK: main {{.*}}dll_host + return 0; +} -- 2.11.4.GIT