<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Crash in LLParser with undefined tbaa metadata"
   href="https://llvm.org/bugs/show_bug.cgi?id=27196">27196</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Crash in LLParser with undefined tbaa metadata
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM assembly language parser
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>philip.pfaffe@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16165" name="attach_16165" title="Crashing Testcase">attachment 16165</a> <a href="attachment.cgi?id=16165&action=edit" title="Crashing Testcase">[details]</a></span>
Crashing Testcase

If IR is annotated with TBAA metadata but the referenced MD isn't declared, an
assertion in MDNode::getOperand is triggered. In LLParser::ValidateEndOfModule
in the call to UpgradeInstWithTBAATag, there is an unconditional check
"isa<MDNode>(MD->getOperand(0))", which ends badly if MD->getNumOperands() is
0.

Crashing testcase attached.

A similar unchecked call chain occurs in BitcodeReader, but i haven't been able
to reproduce the error there, and i'm not sure whether the bitcode format
actually allows for this problem to arise.

A possible fix for the LLParser could be this:

--- a/lib/AsmParser/LLParser.cpp
+++ b/lib/AsmParser/LLParser.cpp
@@ -100,8 +100,9 @@ void LLParser::restoreParsingState(const SlotMapping
*Slots) {
 /// ValidateEndOfModule - Do final validity and sanity checks at the end of
the
 /// module.
 bool LLParser::ValidateEndOfModule() {
-  for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
-    UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
+  for (auto *I : InstsWithTBAATag)
+    if (I->getMetadata(LLVMContext::MD_tbaa)->getNumOperands() > 0)
+      UpgradeInstWithTBAATag(I);

   // Handle any function attribute group forward references.
   for (std::map<Value*, std::vector<unsigned> >::iterator</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>