Skip to content

Commit bc12aed

Browse files
committed
Add test files and changes from review
1 parent f446595 commit bc12aed

File tree

6 files changed

+844
-15
lines changed

6 files changed

+844
-15
lines changed

compiler-rt/test/tysan/basic.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
// RUN: %clang_tysan -O0 %s -o %t && %run %t 10 >%t.out.0 2>&1
1+
// RUN: %clang_tysan -O0 -mllvm -tysan-outline-instrumentation=false %s -o %t && %run %t 10 >%t.out.0 2>&1
22
// RUN: FileCheck %s < %t.out.0
3-
// RUN: %clang_tysan -O2 %s -o %t && %run %t 10 >%t.out 2>&1
3+
// RUN: %clang_tysan -O2 -mllvm -tysan-outline-instrumentation=false %s -o %t && %run %t 10 >%t.out 2>&1
4+
// RUN: FileCheck %s < %t.out
5+
// RUN: %clang_tysan -O0 -mllvm -tysan-outline-instrumentation=true %s -o %t && %run %t 10 >%t.out.0 2>&1
6+
// RUN: FileCheck %s < %t.out.0
7+
// RUN: %clang_tysan -O2 -mllvm -tysan-outline-instrumentation=true %s -o %t && %run %t 10 >%t.out 2>&1
48
// RUN: FileCheck %s < %t.out
59

610
#include <stdio.h>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_tysan -mllvm -tysan-outline-instrumentation=true -mllvm -tysan-verify-outlined-instrumentation=true %s -o %t && %run %t >%t.out.0 2>&1
2+
// RUN: FileCheck %s < %t.out.0
3+
4+
#include <stdio.h>
5+
6+
void printInt(int* i){
7+
printf("%d\n", *i);
8+
}
9+
10+
int main(){
11+
12+
float value = 5.0f;
13+
printInt((int*)&value);
14+
15+
return 0;
16+
}
17+
18+
// CHECK: ERROR: TypeSanitizer: type-aliasing-violation
19+
// CHECK-NEXT: READ of size 4 at {{.*}} with type int accesses an existing object of type float
20+
// CHECK-NEXT: {{#0 0x.* in printInt}}
21+
// CHECK-EMPTY:
22+
// CHECK-NEXT: ERROR: TypeSanitizer: type-aliasing-violation
23+
// CHECK-NEXT: READ of size 4 at {{.*}} with type int accesses an existing object of type float
24+
// CHECK-NEXT: {{#0 0x.* in printInt}}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_tysan -mllvm -tysan-outline-instrumentation=true -O0 %s -o %t && %run %t >%t.out 2>&1
2+
// RUN: FileCheck %s < %t.out
3+
// RUN: %clang_tysan -mllvm -tysan-outline-instrumentation=true -mllvm -tysan-verify-outlined-instrumentation=true -O0 %s -o %t && %run %t >%t.out 2>&1
4+
// RUN: FileCheck %s --check-prefixes='CHECK,CHECK-VERIFY' < %t.out
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
struct X {
10+
int i;
11+
int j;
12+
};
13+
14+
int foo(struct X *p, struct X *q) {
15+
q->j = 1;
16+
p->i = 0;
17+
// CHECK: ERROR: TypeSanitizer: type-aliasing-violation
18+
// CHECK-NEXT: WRITE of size 4 at {{.*}} with type int (in X at offset 0) accesses an existing object of type int (in X at offset 4)
19+
// CHECK-NEXT: {{#0 0x.* in foo .*struct-offset.c:}}[[@LINE-3]]
20+
// CHECK-VERIFY-EMPTY:
21+
// CHECK-VERIFY-NEXT: ERROR: TypeSanitizer: type-aliasing-violation
22+
// CHECK-VERIFY-NEXT: WRITE of size 4 at {{.*}} with type int (in X at offset 0) accesses an existing object of type int (in X at offset 4)
23+
// CHECK-VERIFY-NEXT: {{#0 0x.* in foo .*struct-offset.c:}}[[@LINE-7]]
24+
return q->j;
25+
}
26+
27+
int main() {
28+
unsigned char *p = malloc(3 * sizeof(int));
29+
printf("%i\n", foo((struct X *)(p + sizeof(int)), (struct X *)p));
30+
}
31+
32+
// CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation

llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ bool TypeSanitizer::instrumentWithShadowUpdate(
633633
if (!ForceSetType && (!ClWritesAlwaysSetType || IsRead)) {
634634
// We need to check the type here. If the type is unknown, then the read
635635
// sets the type. If the type is known, then it is checked. If the type
636-
// doesn't match, then we call the runtime (which may yet determine that
637-
// the mismatch is okay).
636+
// doesn't match, then we call the runtime type check (which may yet
637+
// determine that the mismatch is okay).
638638

639639
Constant *Flags =
640640
ConstantInt::get(OrdTy, (int)IsRead | (((int)IsWrite) << 1));
@@ -904,12 +904,13 @@ bool TypeSanitizer::instrumentMemInst(Value *V, Instruction *ShadowBase,
904904
}
905905

906906
if (ClOutlineInstrumentation) {
907-
if (!Src) {
907+
if (!Src)
908908
Src = ConstantPointerNull::get(IRB.getPtrTy());
909-
}
909+
910910
IRB.CreateCall(
911911
TysanIntrumentMemInst,
912912
{Dest, Src, Size, NeedsMemMove ? IRB.getTrue() : IRB.getFalse()});
913+
return true;
913914
} else {
914915
if (!ShadowBase)
915916
ShadowBase = getShadowBase(*F);
@@ -965,6 +966,11 @@ PreservedAnalyses TypeSanitizerPass::run(Module &M,
965966
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
966967
TySan.sanitizeFunction(F, TLI);
967968
if (ClVerifyOutlinedInstrumentation && ClOutlineInstrumentation) {
969+
// Outlined instrumentation is a new option, and so this exists to
970+
// verify there is no difference in behaviour between the options.
971+
// If the outlined instrumentation triggers a verification failure
972+
// when the original inlined instrumentation does not, or vice versa,
973+
// then there is a discrepency which should be investigated.
968974
ClOutlineInstrumentation = false;
969975
TySan.sanitizeFunction(F, TLI);
970976
ClOutlineInstrumentation = true;

llvm/test/Instrumentation/TypeSanitizer/basic_outlined.ll

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@
55

66
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
77

8+
;.
9+
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @tysan.module_ctor, ptr null }]
10+
; CHECK: @__tysan_v1_Simple_20C_2b_2b_20TBAA = linkonce_odr constant { i64, i64, [16 x i8] } { i64 2, i64 0, [16 x i8] c"Simple C++ TBAA\00" }, comdat
11+
; CHECK: @__tysan_v1_omnipotent_20char = linkonce_odr constant { i64, i64, ptr, i64, [16 x i8] } { i64 2, i64 1, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, i64 0, [16 x i8] c"omnipotent char\00" }, comdat
12+
; CHECK: @__tysan_v1_int = linkonce_odr constant { i64, i64, ptr, i64, [4 x i8] } { i64 2, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [4 x i8] c"int\00" }, comdat
13+
; CHECK: @__tysan_v1_int_o_0 = linkonce_odr constant { i64, ptr, ptr, i64 } { i64 1, ptr @__tysan_v1_int, ptr @__tysan_v1_int, i64 0 }, comdat
14+
; CHECK: @__tysan_shadow_memory_address = external global i64
15+
; CHECK: @__tysan_app_memory_mask = external global i64
16+
; CHECK: @__tysan_v1___ZTS1x = linkonce_odr constant { i64, i64, ptr, i64, ptr, i64, [7 x i8] } { i64 2, i64 2, ptr @__tysan_v1_int, i64 0, ptr @__tysan_v1_int, i64 4, [7 x i8] c"_ZTS1x\00" }, comdat
17+
; CHECK: @__tysan_v1___ZTS1v = linkonce_odr constant { i64, i64, ptr, i64, ptr, i64, ptr, i64, [7 x i8] } { i64 2, i64 3, ptr @__tysan_v1_int, i64 8, ptr @__tysan_v1_int, i64 12, ptr @__tysan_v1___ZTS1x, i64 16, [7 x i8] c"_ZTS1v\00" }, comdat
18+
; CHECK: @__tysan_v1___ZTS1v_o_12 = linkonce_odr constant { i64, ptr, ptr, i64 } { i64 1, ptr @__tysan_v1___ZTS1v, ptr @__tysan_v1_int, i64 12 }, comdat
19+
; CHECK: @llvm.used = appending global [8 x ptr] [ptr @tysan.module_ctor, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, ptr @__tysan_v1_omnipotent_20char, ptr @__tysan_v1_int, ptr @__tysan_v1_int_o_0, ptr @__tysan_v1___ZTS1x, ptr @__tysan_v1___ZTS1v, ptr @__tysan_v1___ZTS1v_o_12], section "llvm.metadata"
20+
;.
821
define i32 @test_load(ptr %a) sanitize_type {
922
; CHECK-LABEL: @test_load(
1023
; CHECK-NEXT: entry:
11-
; CHECK-NEXT: %app.mem.mask = load i64, ptr @__tysan_app_memory_mask, align 8
12-
; CHECK-NEXT: %shadow.base = load i64, ptr @__tysan_shadow_memory_address, align 8
13-
; CHECK-NEXT: call void @__tysan_instrument_with_shadow_update(ptr %a, ptr @__tysan_v1_int_o_0, i1 true, i64 4, i32 1)
14-
; CHECK-NEXT: %tmp1 = load i32, ptr %a, align 4, !tbaa !0
15-
; CHECK-NEXT: ret i32 %tmp1
24+
; CHECK-NEXT: [[APP_MEM_MASK:%.*]] = load i64, ptr @__tysan_app_memory_mask, align 8
25+
; CHECK-NEXT: [[SHADOW_BASE:%.*]] = load i64, ptr @__tysan_shadow_memory_address, align 8
26+
; CHECK-NEXT: call void @__tysan_instrument_with_shadow_update(ptr [[A:%.*]], ptr @__tysan_v1_int_o_0, i1 true, i64 4, i32 1)
27+
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4, !tbaa [[TBAA0:![0-9]+]]
28+
; CHECK-NEXT: ret i32 [[TMP1]]
29+
;
1630
entry:
1731
%tmp1 = load i32, ptr %a, align 4, !tbaa !3
1832
ret i32 %tmp1
@@ -21,11 +35,12 @@ entry:
2135
define void @test_store(ptr %a) sanitize_type {
2236
; CHECK-LABEL: @test_store(
2337
; CHECK-NEXT: entry:
24-
; CHECK-NEXT: %app.mem.mask = load i64, ptr @__tysan_app_memory_mask, align 8
25-
; CHECK-NEXT: %shadow.base = load i64, ptr @__tysan_shadow_memory_address, align 8
26-
; CHECK-NEXT: call void @__tysan_instrument_with_shadow_update(ptr %a, ptr @__tysan_v1___ZTS1v_o_12, i1 true, i64 4, i32 2)
27-
; CHECK-NEXT: store i32 42, ptr %a, align 4, !tbaa !4
38+
; CHECK-NEXT: [[APP_MEM_MASK:%.*]] = load i64, ptr @__tysan_app_memory_mask, align 8
39+
; CHECK-NEXT: [[SHADOW_BASE:%.*]] = load i64, ptr @__tysan_shadow_memory_address, align 8
40+
; CHECK-NEXT: call void @__tysan_instrument_with_shadow_update(ptr [[A:%.*]], ptr @__tysan_v1___ZTS1v_o_12, i1 true, i64 4, i32 2)
41+
; CHECK-NEXT: store i32 42, ptr [[A]], align 4, !tbaa [[TBAA4:![0-9]+]]
2842
; CHECK-NEXT: ret void
43+
;
2944

3045
entry:
3146
store i32 42, ptr %a, align 4, !tbaa !6
@@ -39,3 +54,15 @@ entry:
3954
!4 = !{!"_ZTS1x", !2, i64 0, !2, i64 4}
4055
!5 = !{!"_ZTS1v", !2, i64 8, !2, i64 12, !4, i64 16}
4156
!6 = !{!5, !2, i64 12}
57+
;.
58+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { sanitize_type }
59+
; CHECK: attributes #[[ATTR1:[0-9]+]] = { nounwind }
60+
;.
61+
; CHECK: [[TBAA0]] = !{[[META1:![0-9]+]], [[META1]], i64 0}
62+
; CHECK: [[META1]] = !{!"int", [[META2:![0-9]+]], i64 0}
63+
; CHECK: [[META2]] = !{!"omnipotent char", [[META3:![0-9]+]], i64 0}
64+
; CHECK: [[META3]] = !{!"Simple C++ TBAA"}
65+
; CHECK: [[TBAA4]] = !{[[META5:![0-9]+]], [[META1]], i64 12}
66+
; CHECK: [[META5]] = !{!"_ZTS1v", [[META1]], i64 8, [[META1]], i64 12, [[META6:![0-9]+]], i64 16}
67+
; CHECK: [[META6]] = !{!"_ZTS1x", [[META1]], i64 0, [[META1]], i64 4}
68+
;.

0 commit comments

Comments
 (0)