[ASan/Win tests] Add more DLL tests: malloc & friends

llvm-svn: 208889
This commit is contained in:
Timur Iskhodzhanov 2014-05-15 14:42:41 +00:00
parent 436780bebb
commit e23889692b
4 changed files with 102 additions and 2 deletions

View File

@ -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 <malloc.h>
#include <stdio.h>
#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;
}

View File

@ -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 <malloc.h>
#include <stdio.h>
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;
}

View File

@ -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);

View File

@ -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 <malloc.h>
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;
}