Skip to content

Commit e7d110c

Browse files
committed
!fixup pass ShadowBase/AppMemMask by value.
1 parent 0882b0e commit e7d110c

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ struct TypeSanitizer {
8787

8888
bool instrumentWithShadowUpdate(IRBuilder<> &IRB, const MDNode *TBAAMD,
8989
Value *Ptr, uint64_t AccessSize, bool IsRead,
90-
bool IsWrite, Value *&ShadowBase,
91-
Value *&AppMemMask, bool ForceSetType,
90+
bool IsWrite, Value *ShadowBase,
91+
Value *AppMemMask, bool ForceSetType,
9292
bool SanitizeFunction,
9393
TypeDescriptorsMapTy &TypeDescriptors,
9494
const DataLayout &DL);
9595
bool instrumentMemoryAccess(Instruction *I, MemoryLocation &MLoc,
96-
Value *&ShadowBase, Value *&AppMemMask,
96+
Value *ShadowBase, Value *AppMemMask,
9797
bool SanitizeFunction,
9898
TypeDescriptorsMapTy &TypeDescriptors,
9999
const DataLayout &DL);
@@ -170,7 +170,8 @@ void TypeSanitizer::instrumentGlobals(Module &M) {
170170
ReturnInst::Create(M.getContext(), BB);
171171

172172
const DataLayout &DL = M.getDataLayout();
173-
Value *ShadowBase = nullptr, *AppMemMask = nullptr;
173+
Value *ShadowBase = getShadowBase(*TysanGlobalsSetTypeFunction);
174+
Value *AppMemMask = getAppMemMask(*TysanGlobalsSetTypeFunction);
174175
TypeDescriptorsMapTy TypeDescriptors;
175176
TypeNameMapTy TypeNames;
176177

@@ -551,7 +552,8 @@ bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
551552

552553
const DataLayout &DL = F.getParent()->getDataLayout();
553554
bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeType);
554-
Value *ShadowBase = nullptr, *AppMemMask = nullptr;
555+
Value *ShadowBase = MemoryAccesses.empty() ? nullptr : getShadowBase(F);
556+
Value *AppMemMask = MemoryAccesses.empty() ? nullptr : getAppMemMask(F);
555557
for (auto &MA : MemoryAccesses)
556558
Res |= instrumentMemoryAccess(MA.first, MA.second, ShadowBase, AppMemMask,
557559
SanitizeFunction, TypeDescriptors, DL);
@@ -575,14 +577,9 @@ static Value *ConvertToShadowDataInt(IRBuilder<> &IRB, Value *Ptr,
575577

576578
bool TypeSanitizer::instrumentWithShadowUpdate(
577579
IRBuilder<> &IRB, const MDNode *TBAAMD, Value *Ptr, uint64_t AccessSize,
578-
bool IsRead, bool IsWrite, Value *&ShadowBase, Value *&AppMemMask,
580+
bool IsRead, bool IsWrite, Value *ShadowBase, Value *AppMemMask,
579581
bool ForceSetType, bool SanitizeFunction,
580582
TypeDescriptorsMapTy &TypeDescriptors, const DataLayout &DL) {
581-
if (!ShadowBase)
582-
ShadowBase = getShadowBase(*IRB.GetInsertBlock()->getParent());
583-
if (!AppMemMask)
584-
AppMemMask = getAppMemMask(*IRB.GetInsertBlock()->getParent());
585-
586583
Constant *TDGV;
587584
if (TBAAMD)
588585
TDGV = TypeDescriptors[TBAAMD];
@@ -716,9 +713,9 @@ bool TypeSanitizer::instrumentWithShadowUpdate(
716713
}
717714

718715
bool TypeSanitizer::instrumentMemoryAccess(
719-
Instruction *I, MemoryLocation &MLoc, Value *&ShadowBase,
720-
Value *&AppMemMask, bool SanitizeFunction,
721-
TypeDescriptorsMapTy &TypeDescriptors, const DataLayout &DL) {
716+
Instruction *I, MemoryLocation &MLoc, Value *ShadowBase, Value *AppMemMask,
717+
bool SanitizeFunction, TypeDescriptorsMapTy &TypeDescriptors,
718+
const DataLayout &DL) {
722719
IRBuilder<> IRB(I);
723720
assert(MLoc.Size.isPrecise());
724721
if (instrumentWithShadowUpdate(

llvm/test/Instrumentation/TypeSanitizer/globals.ll

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --include-generated-funcs
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals --include-generated-funcs
22
; RUN: opt -passes='tysan-module,tysan' -S %s | FileCheck %s
33

44
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
@@ -19,21 +19,22 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1919
;.
2020
; CHECK: @global1 = global i32 0, align 4
2121
; CHECK: @global2 = global i32 0, align 4
22+
; CHECK: @__tysan_shadow_memory_address = external global i64
23+
; CHECK: @__tysan_app_memory_mask = external global i64
2224
; 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
2325
; 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
2426
; 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
2527
; CHECK: @llvm.used = appending global [4 x ptr] [ptr @tysan.module_ctor, ptr @__tysan_v1_Simple_20C_2b_2b_20TBAA, ptr @__tysan_v1_omnipotent_20char, ptr @__tysan_v1_int], section "llvm.metadata"
26-
; CHECK: @__tysan_shadow_memory_address = external global i64
27-
; CHECK: @__tysan_app_memory_mask = external global i64
2828
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @tysan.module_ctor, ptr null }]
2929
;.
30-
; CHECK-LABEL: @tysan.module_ctor(
30+
; CHECK-LABEL: define {{[^@]+}}@tysan.module_ctor
31+
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
3132
; CHECK-NEXT: call void @__tysan_init()
3233
; CHECK-NEXT: call void @__tysan_set_globals_types()
3334
; CHECK-NEXT: ret void
3435
;
3536
;
36-
; CHECK-LABEL: @__tysan_set_globals_types(
37+
; CHECK-LABEL: define {{[^@]+}}@__tysan_set_globals_types() {
3738
; CHECK-NEXT: [[APP_MEM_MASK:%.*]] = load i64, ptr @__tysan_app_memory_mask, align 8
3839
; CHECK-NEXT: [[SHADOW_BASE:%.*]] = load i64, ptr @__tysan_shadow_memory_address, align 8
3940
; CHECK-NEXT: [[APP_PTR_MASKED:%.*]] = and i64 ptrtoint (ptr @global1 to i64), [[APP_MEM_MASK]]
@@ -67,7 +68,7 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6768
; CHECK-NEXT: ret void
6869
;
6970
;.
70-
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind }
71+
; CHECK: attributes #[[ATTR0]] = { nounwind }
7172
;.
7273
; CHECK: [[META0:![0-9]+]] = !{ptr @global1, [[META1:![0-9]+]]}
7374
; CHECK: [[META1]] = !{!"int", [[META2:![0-9]+]], i64 0}

llvm/test/Instrumentation/TypeSanitizer/invalid-metadata.ll

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --include-generated-funcs
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals --include-generated-funcs
22
; RUN: opt -passes='tysan-module,tysan' -S %s | FileCheck %s
33

44
!llvm.tysan.globals = !{!0}
@@ -9,19 +9,24 @@
99
!3 = !{!"Simple C/C++ TBAA"}
1010
;.
1111
; CHECK: @llvm.used = appending global [1 x ptr] [ptr @tysan.module_ctor], section "llvm.metadata"
12+
; CHECK: @__tysan_shadow_memory_address = external global i64
13+
; CHECK: @__tysan_app_memory_mask = external global i64
1214
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @tysan.module_ctor, ptr null }]
1315
;.
14-
; CHECK-LABEL: @tysan.module_ctor(
16+
; CHECK-LABEL: define {{[^@]+}}@tysan.module_ctor
17+
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
1518
; CHECK-NEXT: call void @__tysan_init()
1619
; CHECK-NEXT: call void @__tysan_set_globals_types()
1720
; CHECK-NEXT: ret void
1821
;
1922
;
20-
; CHECK-LABEL: @__tysan_set_globals_types(
23+
; CHECK-LABEL: define {{[^@]+}}@__tysan_set_globals_types() {
24+
; CHECK-NEXT: [[APP_MEM_MASK:%.*]] = load i64, ptr @__tysan_app_memory_mask, align 4
25+
; CHECK-NEXT: [[SHADOW_BASE:%.*]] = load i64, ptr @__tysan_shadow_memory_address, align 4
2126
; CHECK-NEXT: ret void
2227
;
2328
;.
24-
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind }
29+
; CHECK: attributes #[[ATTR0]] = { nounwind }
2530
;.
2631
; CHECK: [[META0:![0-9]+]] = distinct !{ptr undef, [[META1:![0-9]+]]}
2732
; CHECK: [[META1]] = !{!"any pointer", [[META2:![0-9]+]], i64 0}

0 commit comments

Comments
 (0)