IR: Disallow function-local metadata attachments
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Sat, 6 Dec 2014 02:29:44 +0000 (02:29 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Sat, 6 Dec 2014 02:29:44 +0000 (02:29 +0000)
Metadata attachments to instructions cannot be function-local.

This is part of PR21532.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223574 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AsmParser/LLParser.cpp
lib/Bitcode/Reader/BitcodeReader.cpp
lib/IR/Metadata.cpp
test/Feature/metadata.ll
test/Linker/metadata-a.ll
test/Linker/metadata-b.ll

index 93eb490619f4adb4598df597579f5fe9cfe5933d..f44af673fcd0ded1986dd7047ebf323126e673d5 100644 (file)
@@ -62,6 +62,8 @@ bool LLParser::ValidateEndOfModule() {
             NumberedMetadata[SlotNo] == nullptr)
           return Error(MDList[i].Loc, "use of undefined metadata '!" +
                        Twine(SlotNo) + "'");
+        assert(!NumberedMetadata[SlotNo]->isFunctionLocal() &&
+               "Unexpected function-local metadata");
         Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
       }
     }
@@ -1529,6 +1531,8 @@ bool LLParser::ParseInstructionMetadata(Instruction *Inst,
       if (ParseMetadataListValue(ID, PFS))
         return true;
       assert(ID.Kind == ValID::t_MDNode);
+      if (ID.MDNodeVal->isFunctionLocal())
+        return TokError("unexpected function-local metadata");
       Inst->setMetadata(MDK, ID.MDNodeVal);
     } else {
       unsigned NodeID = 0;
index 9f6d02f88db46fd51883da2c93166e86f89c5084..72f823c2e3e8ab6ec13776c9044b36c608a13c46 100644 (file)
@@ -2359,8 +2359,12 @@ std::error_code BitcodeReader::ParseMetadataAttachment() {
           MDKindMap.find(Kind);
         if (I == MDKindMap.end())
           return Error(BitcodeError::InvalidID);
-        Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
-        Inst->setMetadata(I->second, cast<MDNode>(Node));
+        MDNode *Node = cast<MDNode>(MDValueList.getValueFwdRef(Record[i+1]));
+        if (Node->isFunctionLocal())
+          // Drop the attachment.  This used to be legal, but there's no
+          // upgrade path.
+          break;
+        Inst->setMetadata(I->second, Node);
         if (I->second == LLVMContext::MD_tbaa)
           InstsWithTBAATag.push_back(Inst);
       }
index 45d61a15a6b6c93d3001a2661aa6f7dc306b6dd0..e5dfaec74e592fc586a0f21d62fbbcee4203ced1 100644 (file)
@@ -639,6 +639,8 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
   
   // Handle the case when we're adding/updating metadata on an instruction.
   if (Node) {
+    assert(!Node->isFunctionLocal() &&
+           "Function-local metadata cannot be attached to instructions");
     LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
     assert(!Info.empty() == hasMetadataHashEntry() &&
            "HasMetadata bit is wonked");
index 2a857bb9c6b68e6cb47f6501d9f41a4bfe234412..f83b7fe9c329fc35835f7ec26338fb1d6a72766c 100644 (file)
@@ -5,7 +5,7 @@ define void @foo(i32 %x) {
   call void @llvm.zonk(metadata !1, i64 0, metadata !1)
   store i32 0, i32* null, !whatever !0, !whatever_else !{}, !more !{metadata !"hello"}
   store i32 0, i32* null, !whatever !{metadata !"hello", metadata !1, metadata !{}, metadata !2}
-  ret void, !whatever !{i32 %x}
+  ret void
 }
 
 declare void @llvm.zonk(metadata, i64, metadata) nounwind readnone
index 5a9d2e40b948fdc21c71b801b6e05f0606098683..76a8c8cbfc74e69ceb1ef910ffa4560086875309 100644 (file)
@@ -1,15 +1,15 @@
 ; RUN: llvm-link %s %p/metadata-b.ll -S -o - | FileCheck %s
 
 ; CHECK: define void @foo(i32 %a)
-; CHECK: ret void, !attach !0, !also !{i32 %a}
+; CHECK: ret void, !attach !0
 ; CHECK: define void @goo(i32 %b)
-; CHECK: ret void, !attach !1, !and !{i32 %b}
+; CHECK: ret void, !attach !1
 ; CHECK: !0 = metadata !{i32 524334, void (i32)* @foo}
 ; CHECK: !1 = metadata !{i32 524334, void (i32)* @goo}
 
 define void @foo(i32 %a) nounwind {
 entry:
-  ret void, !attach !0, !also !{ i32 %a }
+  ret void, !attach !0
 }
 
 !0 = metadata !{i32 524334, void (i32)* @foo}
index ef0270af07560f3eb3d5832a0b5608f2c54fe7b5..35d4f66ccbacb9cc6fc9ce72f0ca334f7ff9b8e8 100644 (file)
@@ -3,7 +3,7 @@
 
 define void @goo(i32 %b) nounwind {
 entry:
-  ret void, !attach !0, !and !{ i32 %b }
+  ret void, !attach !0
 }
 
 !0 = metadata !{i32 524334, void (i32)* @goo}