llvm.org GIT mirror llvm / ce07e02
Strip invalid TBAA when reading bitcode This ensures backward compatibility on bitcode loading. Differential Revision: https://reviews.llvm.org/D27839 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289977 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
4 changed file(s) with 49 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
4747 #include "llvm/IR/GlobalVariable.h"
4848 #include "llvm/IR/GVMaterializer.h"
4949 #include "llvm/IR/InlineAsm.h"
50 #include "llvm/IR/InstIterator.h"
5051 #include "llvm/IR/InstrTypes.h"
5152 #include "llvm/IR/Instruction.h"
5253 #include "llvm/IR/Instructions.h"
5960 #include "llvm/IR/TrackingMDRef.h"
6061 #include "llvm/IR/Type.h"
6162 #include "llvm/IR/ValueHandle.h"
63 #include "llvm/IR/Verifier.h"
6264 #include "llvm/Support/AtomicOrdering.h"
6365 #include "llvm/Support/Casting.h"
6466 #include "llvm/Support/CommandLine.h"
147149 return false;
148150 }
149151
152 // Strip all the TBAA attachment for the module.
153 void stripTBAA(Module *M) {
154 for (auto &F : *M) {
155 if (F.isMaterializable())
156 continue;
157 for (auto &I : instructions(F))
158 I.setMetadata(LLVMContext::MD_tbaa, nullptr);
159 }
160 }
161
150162 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
151163 /// "epoch" encoded in the bitcode, and return the producer name if any.
152164 Expected readIdentificationBlock(BitstreamCursor &Stream) {
459471 bool WillMaterializeAllForwardRefs = false;
460472
461473 bool StripDebugInfo = false;
474 TBAAVerifier TBAAVerifyHelper;
462475
463476 std::vector BundleTags;
464477
44484461 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))
44494462 F->setSubprogram(SP);
44504463
4464 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
4465 if (!MDLoader->isStrippingTBAA()) {
4466 for (auto &I : instructions(F)) {
4467 MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa);
4468 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA))
4469 continue;
4470 MDLoader->setStripTBAA(true);
4471 stripTBAA(F->getParent());
4472 }
4473 }
4474
44514475 // Bring in any functions that this function forward-referenced via
44524476 // blockaddresses.
44534477 return materializeForwardReferencedFunctions();
381381 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
382382 DenseMap MDKindMap;
383383
384 bool StripTBAA = false;
384385 bool HasSeenOldLoopTags = false;
385386
386387 Error parseMetadataStrings(ArrayRef Record, StringRef Blob,
418419 Function &F, const SmallVectorImpl &InstructionList);
419420
420421 Error parseMetadataKinds();
422
423 void setStripTBAA(bool Value) { StripTBAA = Value; }
424 bool isStrippingTBAA() { return StripTBAA; }
421425
422426 unsigned size() const { return MetadataList.size(); }
423427 void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
12071211 DenseMap::iterator I = MDKindMap.find(Kind);
12081212 if (I == MDKindMap.end())
12091213 return error("Invalid ID");
1214 if (I->second == LLVMContext::MD_tbaa && StripTBAA)
1215 continue;
1216
12101217 Metadata *Node = MetadataList.getMetadataFwdRef(Record[i + 1]);
12111218 if (isa(Node))
12121219 // Drop the attachment. This used to be legal, but there's no
13261333 return Pimpl->parseMetadataKinds();
13271334 }
13281335
1336 void MetadataLoader::setStripTBAA(bool StripTBAA) {
1337 return Pimpl->setStripTBAA(StripTBAA);
1338 }
1339
1340 bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
1341
13291342 unsigned MetadataLoader::size() const { return Pimpl->size(); }
13301343 void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
5151 // Parse a function metadata block
5252 Error parseFunctionMetadata() { return parseMetadata(false); }
5353
54 /// Set the mode to strip TBAA metadata on load.
55 void setStripTBAA(bool StripTBAA = true);
56
57 /// Return true if the Loader is stripping TBAA metadata.
58 bool isStrippingTBAA();
59
5460 // Return true there are remaining unresolved forward references.
5561 bool hasFwdRefs() const;
5662
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1 ; RUN: llvm-as -disable-verify < %s 2>&1 | opt -verify -S | FileCheck %s --check-prefix=STRIP
12
3 ; STRIP-NOT: tbaa
4 ; STRIP: @f_0
5 ; STRIP: Do no strip this
26 define void @f_0(i32* %ptr) {
37 ; This part checks for the easy syntactic verifier rules.
48
3337 store i32 4, i32* %ptr, !tbaa !{!3, null, !"40", i64 0}
3438 store i32 5, i32* %ptr, !tbaa !{!3, !3, !"40", i64 0}
3539 store i32 6, i32* %ptr, !tbaa !{!3, !2, i32 40, i64 0}
36 store i32 7, i32* %ptr, !tbaa !{!3, !12, i32 40, i64 0}
40 store i32 7, i32* %ptr, !tbaa !{!3, !12, i32 40, i64 0}, !metadata !42
3741 ret void
3842 }
39
43 !42 = !{!"Do no strip this!"}
4044
4145 define void @f_1(i32* %ptr) {
4246 ; This part checks for more semantic verifier rules.