[llvm] 80f6c99 - Verify that MDNodes belong to the same context as the Module.

Nick Lewycky via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 24 12:38:14 PDT 2021


Author: Nick Lewycky
Date: 2021-03-24T12:38:05-07:00
New Revision: 80f6c99a78ac1d98ca02b1dd8ec9647c2841ea5f

URL: https://github.com/llvm/llvm-project/commit/80f6c99a78ac1d98ca02b1dd8ec9647c2841ea5f
DIFF: https://github.com/llvm/llvm-project/commit/80f6c99a78ac1d98ca02b1dd8ec9647c2841ea5f.diff

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

Differential Revision: https://reviews.llvm.org/D99289

Added: 
    

Modified: 
    llvm/lib/IR/Verifier.cpp
    llvm/unittests/IR/VerifierTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 60689efce6251..0a96b29407bb5 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 174e3eea3cce7..6b1217feeac7a 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


        


More information about the llvm-commits mailing list