[ASan] port remaining output and feature tests to lit. TODO: we should in fact run most of the tests for 32/64 bits and for all optimization levels

llvm-svn: 161935
This commit is contained in:
Alexey Samsonov 2012-08-15 08:29:17 +00:00
parent 420052640f
commit 6016486a16
13 changed files with 284 additions and 0 deletions

View File

@ -0,0 +1,19 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | FileCheck %s
#include <string.h>
int main(int argc, char **argv) {
static char XXX[10];
static char YYY[10];
static char ZZZ[10];
memset(XXX, 0, 10);
memset(YYY, 0, 10);
memset(ZZZ, 0, 10);
int res = YYY[argc * 10]; // BOOOM
// CHECK: {{READ of size 1 at 0x.* thread T0}}
// CHECK: {{ #0 0x.* in main .*global-overflow.cc:12}}
// CHECK: {{0x.* is located 0 bytes to the right of global variable}}
// CHECK: {{.*YYY.* of size 10}}
res += XXX[argc] + ZZZ[argc];
return res;
}

View File

@ -0,0 +1,26 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer > %t.output
// RUN: FileCheck %s < %t.output
// RUN: FileCheck %s --check-prefix=CHECK-%os < %t.output
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
char *x = (char*)malloc(10 * sizeof(char));
memset(x, 0, 10);
int res = x[argc * 10]; // BOOOM
// CHECK: {{READ of size 1 at 0x.* thread T0}}
// CHECK: {{ #0 0x.* in main .*heap-overflow.cc:11}}
// CHECK: {{0x.* is located 0 bytes to the right of 10-byte region}}
// CHECK: {{allocated by thread T0 here:}}
// CHECK-Linux: {{ #0 0x.* in .*malloc}}
// CHECK-Linux: {{ #1 0x.* in main .*heap-overflow.cc:9}}
// CHECK-Darwin: {{ #0 0x.* in .*mz_malloc.*}}
// CHECK-Darwin: {{ #1 0x.* in malloc_zone_malloc.*}}
// CHECK-Darwin: {{ #2 0x.* in malloc.*}}
// CHECK-Darwin: {{ #3 0x.* in main heap-overflow.cc:9}}
free(x);
return res;
}

View File

@ -0,0 +1,53 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | c++filt > %t.output
// RUN: FileCheck %s < %t.output
// RUN: FileCheck %s --check-prefix=CHECK-%os < %t.output
#include <stdlib.h>
__attribute__((noinline))
static void LargeFunction(int *x, int zero) {
x[0]++;
x[1]++;
x[2]++;
x[3]++;
x[4]++;
x[5]++;
x[6]++;
x[7]++;
x[8]++;
x[9]++;
x[zero + 111]++; // we should report this exact line
x[10]++;
x[11]++;
x[12]++;
x[13]++;
x[14]++;
x[15]++;
x[16]++;
x[17]++;
x[18]++;
x[19]++;
}
int main(int argc, char **argv) {
int *x = new int[100];
LargeFunction(x, argc - 1);
delete x;
}
// CHECK: {{.*ERROR: AddressSanitizer heap-buffer-overflow on address}}
// CHECK: {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
// CHECK: {{READ of size 4 at 0x.* thread T0}}
// atos incorrectly extracts the symbol name for the static functions on
// Darwin.
// CHECK-Linux: {{ #0 0x.* in LargeFunction.*large_func_test.cc:20}}
// CHECK-Darwin: {{ #0 0x.* in .*LargeFunction.*large_func_test.cc:20}}
// CHECK: {{ #1 0x.* in main .*large_func_test.cc:36}}
// CHECK: {{0x.* is located 44 bytes to the right of 400-byte region}}
// CHECK: {{allocated by thread T0 here:}}
// CHECK: {{ #0 0x.* in operator new.*}}
// CHECK: {{ #1 0x.* in main .*large_func_test.cc:35}}

View File

@ -42,5 +42,12 @@ if not os.path.exists(symbolizer):
lit.fatal("Can't find symbolizer script on path %r" % symbolizer)
config.substitutions.append( ('%symbolizer', (" " + symbolizer + " " )))
# Define CHECK-%os to check for OS-dependent output.
config.substitutions.append( ('CHECK-%os', ("CHECK-" + config.host_os)))
# Default test suffixes.
config.suffixes = ['.c', '.cc', '.cpp']
# AddressSanitizer tests are currently supported on Linux and Darwin only.
if config.host_os not in ['Linux', 'Darwin']:
config.unsupported = True

View File

@ -0,0 +1,13 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | c++filt | FileCheck %s
#include <string.h>
int main(int argc, char **argv) {
char a1[] = {argc, 2, 3, 4};
char a2[] = {1, 2*argc, 3, 4};
int res = memcmp(a1, a2, 4 + argc); // BOOM
// CHECK: AddressSanitizer stack-buffer-overflow
// CHECK: {{#0.*memcmp}}
// CHECK: {{#1.*main}}
return res;
}

View File

@ -0,0 +1,22 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | c++filt > %t.output
// RUN: FileCheck %s < %t.output
// RUN: FileCheck %s --check-prefix=CHECK-%os < %t.output
__attribute__((noinline))
static void NullDeref(int *ptr) {
ptr[10]++;
}
int main() {
NullDeref((int*)0);
}
// CHECK: {{.*ERROR: AddressSanitizer crashed on unknown address}}
// CHECK: {{0x0*00028 .*pc 0x.*}}
// CHECK: {{AddressSanitizer can not provide additional info.}}
// atos on Mac cannot extract the symbol name correctly.
// CHECK-Linux: {{ #0 0x.* in NullDeref.*null_deref.cc:8}}
// CHECK-Darwin: {{ #0 0x.* in .*NullDeref.*null_deref.cc:8}}
// CHECK: {{ #1 0x.* in main.*null_deref.cc:11}}

View File

@ -0,0 +1,15 @@
// Sanity checking a test in pure C.
// RUN: %clang -g -faddress-sanitizer -O2 %s -o %t
// RUN: %t 2>&1 | FileCheck %s
// Sanity checking a test in pure C with -pie.
// RUN: %clang -g -faddress-sanitizer -O2 %s -pie -o %t
// RUN: %t 2>&1 | FileCheck %s
#include <stdlib.h>
int main() {
char *x = (char*)malloc(10 * sizeof(char));
free(x);
return x[5];
// CHECK: heap-use-after-free
}

View File

@ -0,0 +1,10 @@
// RUN: %clang -g -faddress-sanitizer -O2 %s -o %t
// RUN: ASAN_OPTIONS="sleep_before_dying=1" %t 2>&1 | FileCheck %s
#include <stdlib.h>
int main() {
char *x = (char*)malloc(10 * sizeof(char));
free(x);
return x[5];
// CHECK: Sleeping for 1 second
}

View File

@ -0,0 +1,13 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | FileCheck %s
#include <string.h>
int main(int argc, char **argv) {
char x[10];
memset(x, 0, 10);
int res = x[argc * 10]; // BOOOM
// CHECK: {{READ of size 1 at 0x.* thread T0}}
// CHECK: {{ #0 0x.* in main .*stack-overflow.cc:8}}
// CHECK: {{Address 0x.* is .* frame <main>}}
return res;
}

View File

@ -0,0 +1,31 @@
// XFAIL: *
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | c++filt | FileCheck %s
#include <stdio.h>
__attribute__((noinline))
char *Ident(char *x) {
fprintf(stderr, "1: %p\n", x);
return x;
}
__attribute__((noinline))
char *Func1() {
char local;
return Ident(&local);
}
__attribute__((noinline))
void Func2(char *x) {
fprintf(stderr, "2: %p\n", x);
*x = 1;
// CHECK: {{WRITE of size 1 .* thread T0}}
// CHECK: {{ #0.*Func2.*stack-use-after-return.cc:18}}
// CHECK: {{is located in frame <.*Func1.*> of T0's stack}}
}
int main(int argc, char **argv) {
Func2(Func1());
return 0;
}

View File

@ -0,0 +1,12 @@
// RUN: %clang -g -faddress-sanitizer -O2 %s -o %t
// RUN: ASAN_OPTIONS="strip_path_prefix='/'" %t 2>&1 | FileCheck %s
#include <stdlib.h>
int main() {
char *x = (char*)malloc(10 * sizeof(char));
free(x);
return x[5];
// Check that paths in error report don't start with slash.
// CHECK: heap-use-after-free
// CHECK-NOT: #0 0x{{.*}} ({{[/].*}})
}

View File

@ -0,0 +1,28 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | c++filt > %t.output
// RUN: FileCheck %s < %t.output
// RUN: FileCheck %s --check-prefix=CHECK-%os < %t.output
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv) {
char *hello = (char*)malloc(6);
strcpy(hello, "hello");
char *short_buffer = (char*)malloc(9);
strncpy(short_buffer, hello, 10); // BOOM
// CHECK: {{WRITE of size 1 at 0x.* thread T0}}
// CHECK-Linux: {{ #0 0x.* in .*strncpy}}
// CHECK-Darwin: {{ #0 0x.* in wrap_strncpy}}
// CHECK: {{ #1 0x.* in main .*strncpy-overflow.cc:12}}
// CHECK: {{0x.* is located 0 bytes to the right of 9-byte region}}
// CHECK: {{allocated by thread T0 here:}}
// CHECK-Linux: {{ #0 0x.* in .*malloc}}
// CHECK-Linux: {{ #1 0x.* in main .*strncpy-overflow.cc:11}}
// CHECK-Darwin: {{ #0 0x.* in .*mz_malloc.*}}
// CHECK-Darwin: {{ #1 0x.* in malloc_zone_malloc.*}}
// CHECK-Darwin: {{ #2 0x.* in malloc.*}}
// CHECK-Darwin: {{ #3 0x.* in main .*strncpy-overflow.cc:11}}
return short_buffer[8];
}

View File

@ -0,0 +1,35 @@
// RUN: %clang_asan -m64 -O2 %s -o %t
// RUN: %t 2>&1 | %symbolizer | c++filt > %t.output
// RUN: FileCheck %s < %t.output
// RUN: FileCheck %s --check-prefix=CHECK-%os < %t.output
#include <stdlib.h>
int main() {
char *x = (char*)malloc(10 * sizeof(char));
free(x);
return x[5];
// CHECK: {{.*ERROR: AddressSanitizer heap-use-after-free on address}}
// CHECK: {{0x.* at pc 0x.* bp 0x.* sp 0x.*}}
// CHECK: {{READ of size 1 at 0x.* thread T0}}
// CHECK: {{ #0 0x.* in main .*use-after-free.cc:10}}
// CHECK: {{0x.* is located 5 bytes inside of 10-byte region .0x.*,0x.*}}
// CHECK: {{freed by thread T0 here:}}
// CHECK-Linux: {{ #0 0x.* in .*free}}
// CHECK-Linux: {{ #1 0x.* in main .*use-after-free.cc:9}}
// CHECK-Darwin: {{ #0 0x.* in .*mz_free.*}}
// We override free() on Darwin, thus no malloc_zone_free
// CHECK-Darwin: {{ #1 0x.* in wrap_free}}
// CHECK-Darwin: {{ #2 0x.* in main .*use-after-free.cc:9}}
// CHECK: {{previously allocated by thread T0 here:}}
// CHECK-Linux: {{ #0 0x.* in .*malloc}}
// CHECK-Linux: {{ #1 0x.* in main .*use-after-free.cc:8}}
// CHECK-Darwin: {{ #0 0x.* in .*mz_malloc.*}}
// CHECK-Darwin: {{ #1 0x.* in malloc_zone_malloc.*}}
// CHECK-Darwin: {{ #2 0x.* in malloc.*}}
// CHECK-Darwin: {{ #3 0x.* in main .*use-after-free.cc:8}}
}