Verify that MDNodes belong to the same context as the Module.

Differential Revision: https://reviews.llvm.org/D99289
This commit is contained in:
Nick Lewycky 2021-03-24 11:52:02 -07:00
parent a6aae5f7fc
commit 80f6c99a78
2 changed files with 18 additions and 0 deletions

View File

@ -813,6 +813,9 @@ void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
if (!MDNodes.insert(&MD).second)
return;
Assert(&MD.getContext() == &Context,
"MDNode context does not match Module context!", &MD);
switch (MD.getMetadataID()) {
default:
llvm_unreachable("Invalid MDNode subclass");

View File

@ -238,5 +238,20 @@ TEST(VerifierTest, DetectInvalidDebugInfo) {
}
}
TEST(VerifierTest, MDNodeWrongContext) {
LLVMContext C1, C2;
auto *Node = MDNode::get(C1, None);
Module M("M", C2);
auto *NamedNode = M.getOrInsertNamedMetadata("test");
NamedNode->addOperand(Node);
std::string Error;
raw_string_ostream ErrorOS(Error);
EXPECT_TRUE(verifyModule(M, &ErrorOS));
EXPECT_TRUE(StringRef(ErrorOS.str())
.startswith("MDNode context does not match Module context!"));
}
} // end anonymous namespace
} // end namespace llvm