Skip to content

Commit 1c0af8d

Browse files
authored
[TySan] Added tests for methods of ignoring instrumentation (#124125)
TySan supports some preprocessor checks and ignorelists, but they are currently untested. This PR adds some tests to make sure they all work. @fhahn @AaronBallman, this is based off the discussion in the documentation PR [#123595]
1 parent 148da06 commit 1c0af8d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

compiler-rt/test/tysan/ignorelist.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %clang_tysan %s -o %t && %run %t 10 >%t.out.0 2>&1
2+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-BOTH %s < %t.out.0
3+
// RUN: echo "fun:typeViolationignored" > %tmp
4+
// RUN: echo "src:*ignorelist.h" > %tmp
5+
// RUN: %clang_tysan -fsanitize-ignorelist=%tmp %s -o %t && %run %t 10 >%t.out 2>&1
6+
// RUN: FileCheck --check-prefixes=CHECK-IGNORELIST,CHECK-BOTH %s < %t.out
7+
8+
#include "ignorelist.h"
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
void typeViolationIgnored(float *fPtr) { printf("As int: %d\n", *(int *)fPtr); }
13+
14+
void typeViolation(int *fPtr) { printf("As float: %f\n", *(float *)fPtr); }
15+
16+
int main() {
17+
float *f = (float *)malloc(sizeof(float));
18+
*f = 413.0f;
19+
typeViolationIgnored(f);
20+
// CHECK: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
21+
// CHECK-NEXT: READ of size 4 at 0x{{.*}} with type int accesses an existing object of type float
22+
// CHECK-IGNORELIST-NOT: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
23+
24+
int *i = (int *)malloc(sizeof(int));
25+
*i = 612;
26+
typeViolation(i);
27+
// CHECK-BOTH: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
28+
// CHECK-BOTH: READ of size 4 at 0x{{.*}} with type float accesses an existing object of type int
29+
30+
typeViolationMultiFile((void *)i);
31+
// CHECK: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
32+
// CHECK-IGNORELIST-NOT: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
33+
34+
return 0;
35+
}

compiler-rt/test/tysan/ignorelist.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Used as part of the ignorelist.c test
2+
// tests if the "src:" ignorelist directive works
3+
#include <stdio.h>
4+
5+
void typeViolationMultiFile(void *value) {
6+
printf("As long: %ld\n", *(long *)value);
7+
}

compiler-rt/test/tysan/preprocessor.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_tysan -O0 %s -o %t && %run %t >%t.out 2>&1 && FileCheck --check-prefix=CHECK-SANITIZED %s < %t.out
2+
// RUN: %clang_tysan -DNOSAN -O0 %s -o %t && %run %t >%t.out 2>&1 && FileCheck --check-prefix=CHECK-NOSAN %s < %t.out
3+
// RUN: %clang -O0 %s -o %t && %run %t >%t.out 2>&1 && FileCheck --check-prefix=CHECK-SIMPLE %s < %t.out
4+
5+
#include <stdio.h>
6+
7+
#if __has_feature(type_sanitizer)
8+
9+
# ifdef NOSAN
10+
__attribute__((no_sanitize("type")))
11+
# endif
12+
int main(){
13+
14+
int value = 42;
15+
printf("As float: %f\n", *(float *)&value);
16+
// CHECK-SANITIZED: ERROR: TypeSanitizer
17+
// CHECK-NOSAN-NOT: ERROR: TypeSanitizer
18+
19+
return 0;
20+
}
21+
22+
#else
23+
24+
int main() {
25+
printf("Nothing interesting here\n");
26+
return 0;
27+
}
28+
// CHECK-SIMPLE: Nothing interesting here
29+
30+
#endif

0 commit comments

Comments
 (0)