Skip to content

Commit 4f5a7f1

Browse files
Matthew NagygbMattN
Matthew Nagy
authored andcommitted
[TySan] Fix struct access with different bases
1 parent 8e6e62d commit 4f5a7f1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

compiler-rt/lib/tysan/tysan.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ static bool isAliasingLegalUp(tysan_type_descriptor *TDA,
129129
break;
130130
}
131131

132+
// This offset can't be negative. Therefore we must be accessing something
133+
// partially inside the last type
134+
if (TDA->Struct.Members[Idx].Offset > OffsetA)
135+
Idx -= 1;
136+
132137
OffsetA -= TDA->Struct.Members[Idx].Offset;
133138
TDA = TDA->Struct.Members[Idx].Type;
134139
} else {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clangxx_tysan -O0 %s -o %t && %run %t >%t.out 2>&1
2+
// RUN: FileCheck %s < %t.out
3+
4+
// Modified reproducer from https://github.com/llvm/llvm-project/issues/105960
5+
6+
#include <stdio.h>
7+
8+
struct inner {
9+
char buffer;
10+
int i;
11+
};
12+
13+
void init_inner(inner *iPtr) {
14+
iPtr->i = 0;
15+
}
16+
17+
struct outer {
18+
inner foo;
19+
char buffer;
20+
};
21+
22+
int main(void) {
23+
outer *l = new outer();
24+
25+
init_inner(&l->foo);
26+
27+
int access_offsets_with_different_base = l->foo.i;
28+
printf("%d\n", access_offsets_with_different_base);
29+
30+
return 0;
31+
}
32+
33+
// CHECK-NOT: ERROR: TypeSanitizer: type-aliasing-violation

0 commit comments

Comments
 (0)