From 80f6c99a78ac1d98ca02b1dd8ec9647c2841ea5f Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Wed, 24 Mar 2021 11:52:02 -0700 Subject: [PATCH] Verify that MDNodes belong to the same context as the Module. Differential Revision: https://reviews.llvm.org/D99289 --- llvm/lib/IR/Verifier.cpp | 3 +++ llvm/unittests/IR/VerifierTest.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 60689efce625..0a96b29407bb 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -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"); diff --git a/llvm/unittests/IR/VerifierTest.cpp b/llvm/unittests/IR/VerifierTest.cpp index 174e3eea3cce..6b1217feeac7 100644 --- a/llvm/unittests/IR/VerifierTest.cpp +++ b/llvm/unittests/IR/VerifierTest.cpp @@ -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