Skip to content

Commit 2235299

Browse files
committed
!fixup address first set of comments, thanks!
1 parent e7d110c commit 2235299

File tree

3 files changed

+71
-39
lines changed

3 files changed

+71
-39
lines changed

llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ static cl::opt<bool>
6464

6565
STATISTIC(NumInstrumentedAccesses, "Number of instrumented accesses");
6666

67-
static Regex AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N");
68-
6967
namespace {
7068

71-
/// TypeSanitizer: instrument the code in module to find type-based aliasing
69+
/// TypeSanitizer: instrument the code in module to find type-based aliasing
7270
/// violations.
7371
struct TypeSanitizer {
7472
TypeSanitizer(Module &M);
@@ -92,11 +90,9 @@ struct TypeSanitizer {
9290
bool SanitizeFunction,
9391
TypeDescriptorsMapTy &TypeDescriptors,
9492
const DataLayout &DL);
95-
bool instrumentMemoryAccess(Instruction *I, MemoryLocation &MLoc,
96-
Value *ShadowBase, Value *AppMemMask,
97-
bool SanitizeFunction,
98-
TypeDescriptorsMapTy &TypeDescriptors,
99-
const DataLayout &DL);
93+
94+
/// Memory-related intrinsics/instructions reset the type of the destination
95+
/// memory (including allocas and byval arguments).
10096
bool instrumentMemInst(Value *I, Value *&ShadowBase, Value *&AppMemMask,
10197
const DataLayout &DL);
10298

@@ -150,9 +146,8 @@ void TypeSanitizer::initializeCallbacks(Module &M) {
150146
OrdTy // Flags.
151147
);
152148

153-
TysanCtorFunction = cast<Function>(
154-
M.getOrInsertFunction(kTysanModuleCtorName, Attr, IRB.getVoidTy())
155-
.getCallee());
149+
TysanCtorFunction =
150+
M.getOrInsertFunction(kTysanModuleCtorName, Attr, IRB.getVoidTy());
156151
}
157152

158153
void TypeSanitizer::instrumentGlobals(Module &M) {
@@ -535,7 +530,6 @@ bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
535530
if (A.hasByValAttr())
536531
MemTypeResetInsts.push_back(&A);
537532

538-
539533
Module &M = *F.getParent();
540534
TypeDescriptorsMapTy TypeDescriptors;
541535
TypeNameMapTy TypeNames;
@@ -552,11 +546,22 @@ bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
552546

553547
const DataLayout &DL = F.getParent()->getDataLayout();
554548
bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeType);
555-
Value *ShadowBase = MemoryAccesses.empty() ? nullptr : getShadowBase(F);
556-
Value *AppMemMask = MemoryAccesses.empty() ? nullptr : getAppMemMask(F);
557-
for (auto &MA : MemoryAccesses)
558-
Res |= instrumentMemoryAccess(MA.first, MA.second, ShadowBase, AppMemMask,
559-
SanitizeFunction, TypeDescriptors, DL);
549+
bool NeedsInstrumentation =
550+
MemTypeResetInsts.empty() && MemoryAccesses.empty();
551+
Value *ShadowBase = NeedsInstrumentation ? nullptr : getShadowBase(F);
552+
Value *AppMemMask = NeedsInstrumentation ? nullptr : getAppMemMask(F);
553+
for (const auto &[I, MLoc] : MemoryAccesses) {
554+
IRBuilder<> IRB(I);
555+
assert(MLoc.Size.isPrecise());
556+
if (instrumentWithShadowUpdate(
557+
IRB, MLoc.AATags.TBAA, const_cast<Value *>(MLoc.Ptr),
558+
MLoc.Size.getValue(), I->mayReadFromMemory(), I->mayWriteToMemory(),
559+
ShadowBase, AppMemMask, false, SanitizeFunction, TypeDescriptors,
560+
DL)) {
561+
++NumInstrumentedAccesses;
562+
Res = true;
563+
}
564+
}
560565

561566
for (auto Inst : MemTypeResetInsts)
562567
Res |= instrumentMemInst(Inst, ShadowBase, AppMemMask, DL);
@@ -712,26 +717,6 @@ bool TypeSanitizer::instrumentWithShadowUpdate(
712717
return true;
713718
}
714719

715-
bool TypeSanitizer::instrumentMemoryAccess(
716-
Instruction *I, MemoryLocation &MLoc, Value *ShadowBase, Value *AppMemMask,
717-
bool SanitizeFunction, TypeDescriptorsMapTy &TypeDescriptors,
718-
const DataLayout &DL) {
719-
IRBuilder<> IRB(I);
720-
assert(MLoc.Size.isPrecise());
721-
if (instrumentWithShadowUpdate(
722-
IRB, MLoc.AATags.TBAA, const_cast<Value *>(MLoc.Ptr),
723-
MLoc.Size.getValue(), I->mayReadFromMemory(), I->mayWriteToMemory(),
724-
ShadowBase, AppMemMask, false, SanitizeFunction, TypeDescriptors,
725-
DL)) {
726-
++NumInstrumentedAccesses;
727-
return true;
728-
}
729-
730-
return false;
731-
}
732-
733-
// Memory-related intrinsics/instructions reset the type of the destination
734-
// memory (including allocas and byval arguments).
735720
bool TypeSanitizer::instrumentMemInst(Value *V, Value *&ShadowBase,
736721
Value *&AppMemMask,
737722
const DataLayout &DL) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
2+
; Test basic type sanitizer instrumentation.
3+
;
4+
; RUN: opt -passes='tysan-module,tysan' -S %s | FileCheck %s
5+
6+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7+
8+
;.
9+
; CHECK: @llvm.used = appending global [1 x ptr] [ptr @tysan.module_ctor], section "llvm.metadata"
10+
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @tysan.module_ctor, ptr null }]
11+
; CHECK: @__tysan_shadow_memory_address = external global i64
12+
; CHECK: @__tysan_app_memory_mask = external global i64
13+
;.
14+
define void @test_alloca_only(ptr %a) sanitize_type {
15+
; CHECK-LABEL: @test_alloca_only(
16+
; CHECK-NEXT: entry:
17+
; CHECK-NEXT: [[APP_MEM_MASK:%.*]] = load i64, ptr @__tysan_app_memory_mask, align 8
18+
; CHECK-NEXT: [[SHADOW_BASE:%.*]] = load i64, ptr @__tysan_shadow_memory_address, align 8
19+
; CHECK-NEXT: [[TMP1:%.*]] = alloca i32, align 4
20+
; CHECK-NEXT: [[TMP0:%.*]] = ptrtoint ptr [[TMP1]] to i64
21+
; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[TMP0]], [[APP_MEM_MASK]]
22+
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[TMP1]], 3
23+
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[TMP2]], [[SHADOW_BASE]]
24+
; CHECK-NEXT: [[TMP4:%.*]] = inttoptr i64 [[TMP3]] to ptr
25+
; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[TMP4]], i8 0, i64 32, i1 false)
26+
; CHECK-NEXT: call void @foo(ptr [[TMP1]])
27+
; CHECK-NEXT: ret void
28+
;
29+
entry:
30+
%tmp1 = alloca i32
31+
call void @foo(ptr %tmp1)
32+
ret void
33+
}
34+
35+
declare void @foo(ptr)
36+
37+
38+
!0 = !{!"Simple C++ TBAA"}
39+
!1 = !{!"omnipotent char", !0, i64 0}
40+
!2 = !{!"int", !1, i64 0}
41+
!3 = !{!2, !2, i64 0}
42+
!4 = !{!"_ZTS1x", !2, i64 0, !2, i64 4}
43+
!5 = !{!"_ZTS1v", !2, i64 8, !2, i64 12, !4, i64 16}
44+
!6 = !{!5, !2, i64 12}
45+
;.
46+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { sanitize_type }
47+
; CHECK: attributes #[[ATTR1:[0-9]+]] = { nounwind }
48+
; CHECK: attributes #[[ATTR2:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: write) }
49+
;.

llvm/test/Instrumentation/TypeSanitizer/basic.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

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

8-
9-
108
;.
119
; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @tysan.module_ctor, ptr null }]
1210
; 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

0 commit comments

Comments
 (0)