Skip to content

Commit da0b9ce

Browse files
committed
!fixup address latest comments, thanks!
1 parent 7119986 commit da0b9ce

File tree

4 files changed

+116
-102
lines changed

4 files changed

+116
-102
lines changed

llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class TypeBasedAAResult : public AAResultBase {
6060

6161
private:
6262
bool Aliases(const MDNode *A, const MDNode *B) const;
63+
64+
/// Returns true if TBAA metadata should be used, that is if TBAA is enabled
65+
/// and type sanitizer is not used.
66+
bool shouldUseTBAA() const;
6367
};
6468

6569
/// Analysis pass providing a never-invalidated alias analysis result.

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static bool isStructPathTBAA(const MDNode *MD) {
375375
AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
376376
const MemoryLocation &LocB,
377377
AAQueryInfo &AAQI, const Instruction *) {
378-
if (!EnableTBAA || UsingTypeSanitizer || UsingTypeSanitizer)
378+
if (!shouldUseTBAA())
379379
return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
380380

381381
if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
@@ -388,7 +388,7 @@ AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
388388
ModRefInfo TypeBasedAAResult::getModRefInfoMask(const MemoryLocation &Loc,
389389
AAQueryInfo &AAQI,
390390
bool IgnoreLocals) {
391-
if (!EnableTBAA)
391+
if (!shouldUseTBAA())
392392
return ModRefInfo::ModRef;
393393

394394
const MDNode *M = Loc.AATags.TBAA;
@@ -406,7 +406,7 @@ ModRefInfo TypeBasedAAResult::getModRefInfoMask(const MemoryLocation &Loc,
406406

407407
MemoryEffects TypeBasedAAResult::getMemoryEffects(const CallBase *Call,
408408
AAQueryInfo &AAQI) {
409-
if (!EnableTBAA)
409+
if (!shouldUseTBAA())
410410
return MemoryEffects::unknown();
411411

412412
// If this is an "immutable" type, the access is not observable.
@@ -426,7 +426,7 @@ MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) {
426426
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
427427
const MemoryLocation &Loc,
428428
AAQueryInfo &AAQI) {
429-
if (!EnableTBAA || UsingTypeSanitizer)
429+
if (!shouldUseTBAA())
430430
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
431431

432432
if (const MDNode *L = Loc.AATags.TBAA)
@@ -440,7 +440,7 @@ ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
440440
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
441441
const CallBase *Call2,
442442
AAQueryInfo &AAQI) {
443-
if (!EnableTBAA || UsingTypeSanitizer)
443+
if (!shouldUseTBAA())
444444
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
445445

446446
if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
@@ -705,6 +705,10 @@ bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const {
705705
return matchAccessTags(A, B);
706706
}
707707

708+
bool TypeBasedAAResult::shouldUseTBAA() const {
709+
return EnableTBAA && !UsingTypeSanitizer;
710+
}
711+
708712
AnalysisKey TypeBasedAA::Key;
709713

710714
TypeBasedAAResult TypeBasedAA::run(Function &F, FunctionAnalysisManager &AM) {
@@ -724,7 +728,7 @@ TypeBasedAAWrapperPass::TypeBasedAAWrapperPass() : ImmutablePass(ID) {
724728
}
725729

726730
bool TypeBasedAAWrapperPass::doInitialization(Module &M) {
727-
Result.reset(new TypeBasedAAResult(false));
731+
Result.reset(new TypeBasedAAResult(/*UsingTypeSanitizer=*/false));
728732
return false;
729733
}
730734

llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/IR/DataLayout.h"
2323
#include "llvm/IR/Function.h"
2424
#include "llvm/IR/IRBuilder.h"
25+
#include "llvm/IR/InstIterator.h"
2526
#include "llvm/IR/Instructions.h"
2627
#include "llvm/IR/IntrinsicInst.h"
2728
#include "llvm/IR/Intrinsics.h"
@@ -72,7 +73,7 @@ namespace {
7273
struct TypeSanitizer {
7374
TypeSanitizer(Module &M);
7475
bool run(Function &F, const TargetLibraryInfo &TLI);
75-
void instrumentGlobals();
76+
void instrumentGlobals(Module &M);
7677

7778
private:
7879
typedef SmallDenseMap<const MDNode *, GlobalVariable *, 8>
@@ -114,9 +115,11 @@ struct TypeSanitizer {
114115
uint64_t PtrShift;
115116
IntegerType *OrdTy;
116117

117-
// Callbacks to run-time library are computed in doInitialization.
118-
Function *TysanCheck;
119-
Function *TysanCtorFunction;
118+
/// Callbacks to run-time library are computed in initializeCallbacks.
119+
FunctionCallee TysanCheck;
120+
FunctionCallee TysanCtorFunction;
121+
122+
/// Callback to set types for gloabls.
120123
Function *TysanGlobalsSetTypeFunction;
121124
};
122125
} // namespace
@@ -139,29 +142,33 @@ void TypeSanitizer::initializeCallbacks(Module &M) {
139142
AttributeList Attr;
140143
Attr = Attr.addFnAttribute(M.getContext(), Attribute::NoUnwind);
141144
// Initialize the callbacks.
142-
TysanCheck = cast<Function>(
145+
TysanCheck =
143146
M.getOrInsertFunction(kTysanCheckName, Attr, IRB.getVoidTy(),
144147
IRB.getPtrTy(), // Pointer to data to be read.
145148
OrdTy, // Size of the data in bytes.
146149
IRB.getPtrTy(), // Pointer to type descriptor.
147150
OrdTy // Flags.
148-
)
149-
.getCallee());
151+
);
150152

151153
TysanCtorFunction = cast<Function>(
152154
M.getOrInsertFunction(kTysanModuleCtorName, Attr, IRB.getVoidTy())
153155
.getCallee());
154156
}
155157

156-
void TypeSanitizer::instrumentGlobals() {
157-
Module &M = *TysanCtorFunction->getParent();
158-
initializeCallbacks(M);
158+
void TypeSanitizer::instrumentGlobals(Module &M) {
159159
TysanGlobalsSetTypeFunction = nullptr;
160160

161161
NamedMDNode *Globals = M.getNamedMetadata("llvm.tysan.globals");
162162
if (!Globals)
163163
return;
164164

165+
TysanGlobalsSetTypeFunction = Function::Create(
166+
FunctionType::get(Type::getVoidTy(M.getContext()), false),
167+
GlobalValue::InternalLinkage, "__tysan_set_globals_types", &M);
168+
BasicBlock *BB =
169+
BasicBlock::Create(M.getContext(), "", TysanGlobalsSetTypeFunction);
170+
ReturnInst::Create(M.getContext(), BB);
171+
165172
const DataLayout &DL = M.getDataLayout();
166173
Value *ShadowBase = nullptr, *AppMemMask = nullptr;
167174
TypeDescriptorsMapTy TypeDescriptors;
@@ -175,15 +182,6 @@ void TypeSanitizer::instrumentGlobals() {
175182
if (!generateBaseTypeDescriptor(TBAAMD, TypeDescriptors, TypeNames, M))
176183
continue;
177184

178-
if (!TysanGlobalsSetTypeFunction) {
179-
TysanGlobalsSetTypeFunction = Function::Create(
180-
FunctionType::get(Type::getVoidTy(M.getContext()), false),
181-
GlobalValue::InternalLinkage, "__tysan_set_globals_types", &M);
182-
BasicBlock *BB =
183-
BasicBlock::Create(M.getContext(), "", TysanGlobalsSetTypeFunction);
184-
ReturnInst::Create(M.getContext(), BB);
185-
}
186-
187185
IRBuilder<> IRB(
188186
TysanGlobalsSetTypeFunction->getEntryBlock().getTerminator());
189187
Type *AccessTy = GV->getValueType();
@@ -195,23 +193,13 @@ void TypeSanitizer::instrumentGlobals() {
195193
}
196194

197195
if (TysanGlobalsSetTypeFunction) {
198-
IRBuilder<> IRB(TysanCtorFunction->getEntryBlock().getTerminator());
196+
IRBuilder<> IRB(cast<Function>(TysanCtorFunction.getCallee())
197+
->getEntryBlock()
198+
.getTerminator());
199199
IRB.CreateCall(TysanGlobalsSetTypeFunction, {});
200200
}
201201
}
202202

203-
static void insertModuleCtor(Module &M) {
204-
Function *TysanCtorFunction;
205-
std::tie(TysanCtorFunction, std::ignore) =
206-
createSanitizerCtorAndInitFunctions(M, kTysanModuleCtorName,
207-
kTysanInitName, /*InitArgTypes=*/{},
208-
/*InitArgs=*/{});
209-
210-
TypeSanitizer TySan(M);
211-
TySan.instrumentGlobals();
212-
appendToGlobalCtors(M, TysanCtorFunction, 0);
213-
}
214-
215203
static const char LUT[] = "0123456789abcdef";
216204

217205
static std::string encodeName(StringRef Name) {
@@ -220,7 +208,7 @@ static std::string encodeName(StringRef Name) {
220208
Output.reserve(Output.size() + 3 * Length);
221209
for (size_t i = 0; i < Length; ++i) {
222210
const unsigned char c = Name[i];
223-
if (isalnum((int)c)) {
211+
if (isalnum(c)) {
224212
Output.push_back(c);
225213
continue;
226214
}
@@ -337,11 +325,13 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
337325
SmallVector<Type *> TDSubTys;
338326
SmallVector<Constant *> TDSubData;
339327

340-
TDSubTys.push_back(IntptrTy);
341-
TDSubData.push_back(ConstantInt::get(IntptrTy, 2));
328+
auto PushTDSub = [&](Constant *C) {
329+
TDSubTys.push_back(C->getType());
330+
TDSubData.push_back(C);
331+
};
342332

343-
TDSubTys.push_back(IntptrTy);
344-
TDSubData.push_back(ConstantInt::get(IntptrTy, Members.size()));
333+
PushTDSub(ConstantInt::get(IntptrTy, 2));
334+
PushTDSub(ConstantInt::get(IntptrTy, Members.size()));
345335

346336
// Types that are in an anonymous namespace are local to this module.
347337
// FIXME: This should really be marked by the frontend in the metadata
@@ -351,15 +341,11 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
351341
// anonymous namespace is a template parameter, etc.).
352342
bool ShouldBeComdat = !AnonNameRegex.match(NameNode->getString());
353343
for (auto &Member : Members) {
354-
TDSubTys.push_back(Member.first->getType());
355-
TDSubData.push_back(Member.first);
356-
357-
TDSubTys.push_back(IntptrTy);
358-
TDSubData.push_back(ConstantInt::get(IntptrTy, Member.second));
344+
PushTDSub(Member.first);
345+
PushTDSub(ConstantInt::get(IntptrTy, Member.second));
359346
}
360347

361-
TDSubTys.push_back(NameData->getType());
362-
TDSubData.push_back(NameData);
348+
PushTDSub(NameData);
363349

364350
StructType *TDTy = StructType::get(C, TDSubTys);
365351
Constant *TD = ConstantStruct::get(TDTy, TDSubData);
@@ -482,72 +468,77 @@ Value *TypeSanitizer::getAppMemMask(Function &F) {
482468
return IRB.CreateLoad(IntptrTy, GlobalAppMemMask, "app.mem.mask");
483469
}
484470

485-
bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
486-
// This is required to prevent instrumenting call to __tysan_init from within
487-
// the module constructor.
488-
if (&F == TysanCtorFunction || &F == TysanGlobalsSetTypeFunction)
489-
return false;
490-
initializeCallbacks(*F.getParent());
491-
492-
SmallVector<std::pair<Instruction *, MemoryLocation>> MemoryAccesses;
493-
SmallSetVector<const MDNode *, 8> TBAAMetadata;
494-
SmallVector<Value *> MemTypeResetInsts;
495-
496-
bool Res = false;
497-
bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeType);
498-
const DataLayout &DL = F.getParent()->getDataLayout();
471+
/// Collect all loads and stores, and for what TBAA nodes we need to generate
472+
/// type descriptors.
473+
void collectMemAccessInfo(
474+
Function &F, const TargetLibraryInfo &TLI,
475+
SmallVectorImpl<std::pair<Instruction *, MemoryLocation>> &MemoryAccesses,
476+
SmallSetVector<const MDNode *, 8> &TBAAMetadata,
477+
SmallVectorImpl<Value *> &MemTypeResetInsts) {
499478
// Traverse all instructions, collect loads/stores/returns, check for calls.
500-
for (auto &BB : F) {
501-
for (auto &Inst : BB) {
502-
// Skip memory accesses inserted by another instrumentation.
503-
if (Inst.getMetadata(LLVMContext::MD_nosanitize))
504-
continue;
479+
for (Instruction &Inst : instructions(F)) {
480+
// Skip memory accesses inserted by another instrumentation.
481+
if (Inst.getMetadata(LLVMContext::MD_nosanitize))
482+
continue;
505483

506-
if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
507-
isa<AtomicCmpXchgInst>(Inst) || isa<AtomicRMWInst>(Inst)) {
508-
MemoryLocation MLoc = MemoryLocation::get(&Inst);
484+
if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
485+
isa<AtomicCmpXchgInst>(Inst) || isa<AtomicRMWInst>(Inst)) {
486+
MemoryLocation MLoc = MemoryLocation::get(&Inst);
509487

510-
// Swift errors are special (we can't introduce extra uses on them).
511-
if (MLoc.Ptr->isSwiftError())
512-
continue;
488+
// Swift errors are special (we can't introduce extra uses on them).
489+
if (MLoc.Ptr->isSwiftError())
490+
continue;
513491

514-
// Skip non-address-space-0 pointers; we don't know how to handle them.
515-
Type *PtrTy = cast<PointerType>(MLoc.Ptr->getType());
516-
if (PtrTy->getPointerAddressSpace() != 0)
517-
continue;
492+
// Skip non-address-space-0 pointers; we don't know how to handle them.
493+
Type *PtrTy = cast<PointerType>(MLoc.Ptr->getType());
494+
if (PtrTy->getPointerAddressSpace() != 0)
495+
continue;
518496

519-
if (MLoc.AATags.TBAA)
520-
TBAAMetadata.insert(MLoc.AATags.TBAA);
521-
MemoryAccesses.push_back(std::make_pair(&Inst, MLoc));
522-
} else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
523-
if (CallInst *CI = dyn_cast<CallInst>(&Inst))
524-
maybeMarkSanitizerLibraryCallNoBuiltin(CI, &TLI);
497+
if (MLoc.AATags.TBAA)
498+
TBAAMetadata.insert(MLoc.AATags.TBAA);
499+
MemoryAccesses.push_back(std::make_pair(&Inst, MLoc));
500+
} else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
501+
if (CallInst *CI = dyn_cast<CallInst>(&Inst))
502+
maybeMarkSanitizerLibraryCallNoBuiltin(CI, &TLI);
525503

526-
if (isa<MemIntrinsic>(Inst)) {
527-
MemTypeResetInsts.push_back(&Inst);
528-
} else if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
529-
if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
530-
II->getIntrinsicID() == Intrinsic::lifetime_end)
531-
MemTypeResetInsts.push_back(&Inst);
532-
}
533-
} else if (isa<AllocaInst>(Inst)) {
504+
if (isa<MemIntrinsic>(Inst)) {
534505
MemTypeResetInsts.push_back(&Inst);
506+
} else if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
507+
if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
508+
II->getIntrinsicID() == Intrinsic::lifetime_end)
509+
MemTypeResetInsts.push_back(&Inst);
535510
}
511+
} else if (isa<AllocaInst>(Inst)) {
512+
MemTypeResetInsts.push_back(&Inst);
536513
}
537514
}
515+
}
516+
517+
bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
518+
// This is required to prevent instrumenting call to __tysan_init from within
519+
// the module constructor.
520+
if (&F == TysanCtorFunction.getCallee() || &F == TysanGlobalsSetTypeFunction)
521+
return false;
522+
initializeCallbacks(*F.getParent());
523+
524+
// We need to collect all loads and stores, and know for what TBAA nodes we
525+
// need to generate type descriptors.
526+
SmallVector<std::pair<Instruction *, MemoryLocation>> MemoryAccesses;
527+
SmallSetVector<const MDNode *, 8> TBAAMetadata;
528+
SmallVector<Value *> MemTypeResetInsts;
529+
collectMemAccessInfo(F, TLI, MemoryAccesses, TBAAMetadata, MemTypeResetInsts);
538530

539531
// byval arguments also need their types reset (they're new stack memory,
540532
// just like allocas).
541533
for (auto &A : F.args())
542534
if (A.hasByValAttr())
543535
MemTypeResetInsts.push_back(&A);
544536

545-
// We have collected all loads and stores, and know for what TBAA nodes we
546-
// need to generate type descriptors.
547537

548538
Module &M = *F.getParent();
549539
TypeDescriptorsMapTy TypeDescriptors;
550540
TypeNameMapTy TypeNames;
541+
bool Res = false;
551542
for (const MDNode *MD : TBAAMetadata) {
552543
if (TypeDescriptors.count(MD))
553544
continue;
@@ -558,6 +549,8 @@ bool TypeSanitizer::run(Function &F, const TargetLibraryInfo &TLI) {
558549
Res = true;
559550
}
560551

552+
const DataLayout &DL = F.getParent()->getDataLayout();
553+
bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeType);
561554
Value *ShadowBase = nullptr, *AppMemMask = nullptr;
562555
for (auto &MA : MemoryAccesses)
563556
Res |= instrumentMemoryAccess(MA.first, MA.second, ShadowBase, AppMemMask,
@@ -863,6 +856,14 @@ PreservedAnalyses TypeSanitizerPass::run(Function &F,
863856

864857
PreservedAnalyses ModuleTypeSanitizerPass::run(Module &M,
865858
ModuleAnalysisManager &AM) {
866-
insertModuleCtor(M);
859+
Function *TysanCtorFunction;
860+
std::tie(TysanCtorFunction, std::ignore) =
861+
createSanitizerCtorAndInitFunctions(M, kTysanModuleCtorName,
862+
kTysanInitName, /*InitArgTypes=*/{},
863+
/*InitArgs=*/{});
864+
865+
TypeSanitizer TySan(M);
866+
TySan.instrumentGlobals(M);
867+
appendToGlobalCtors(M, TysanCtorFunction, 0);
867868
return PreservedAnalyses::none();
868869
}

0 commit comments

Comments
 (0)