[llvm] r223754 - AsmParser: Reject invalid mismatch between forward ref and def

David Majnemer david.majnemer at gmail.com
Mon Dec 8 21:43:56 PST 2014


Author: majnemer
Date: Mon Dec  8 23:43:56 2014
New Revision: 223754

URL: http://llvm.org/viewvc/llvm-project?rev=223754&view=rev
Log:
AsmParser: Reject invalid mismatch between forward ref and def

Don't assume that the forward referenced entity was of the same
global-kind as the new entity.

This fixes PR21779.

Added:
    llvm/trunk/test/Assembler/invalid-fwdref2.ll
Modified:
    llvm/trunk/lib/AsmParser/LLParser.cpp

Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=223754&r1=223753&r2=223754&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Mon Dec  8 23:43:56 2014
@@ -787,33 +787,36 @@ bool LLParser::ParseGlobal(const std::st
   if (Ty->isFunctionTy() || Ty->isLabelTy())
     return Error(TyLoc, "invalid type for global variable");
 
-  GlobalVariable *GV = nullptr;
+  GlobalValue *GVal = nullptr;
 
   // See if the global was forward referenced, if so, use the global.
   if (!Name.empty()) {
-    if (GlobalValue *GVal = M->getNamedValue(Name)) {
+    GVal = M->getNamedValue(Name);
+    if (GVal) {
       if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
         return Error(NameLoc, "redefinition of global '@" + Name + "'");
-      GV = cast<GlobalVariable>(GVal);
     }
   } else {
     std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
       I = ForwardRefValIDs.find(NumberedVals.size());
     if (I != ForwardRefValIDs.end()) {
-      GV = cast<GlobalVariable>(I->second.first);
+      GVal = I->second.first;
       ForwardRefValIDs.erase(I);
     }
   }
 
+  GlobalVariable *GV;
   if (!GV) {
     GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
                             Name, nullptr, GlobalVariable::NotThreadLocal,
                             AddrSpace);
   } else {
-    if (GV->getType()->getElementType() != Ty)
+    if (GVal->getType()->getElementType() != Ty)
       return Error(TyLoc,
             "forward reference and definition of global have different types");
 
+    GV = cast<GlobalVariable>(GVal);
+
     // Move the forward-reference to the correct spot in the module.
     M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
   }

Added: llvm/trunk/test/Assembler/invalid-fwdref2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/invalid-fwdref2.ll?rev=223754&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/invalid-fwdref2.ll (added)
+++ llvm/trunk/test/Assembler/invalid-fwdref2.ll Mon Dec  8 23:43:56 2014
@@ -0,0 +1,4 @@
+; RUN: not llvm-as %s -disable-output 2>&1 | grep "forward reference and definition of global have different types"
+
+ at a2 = alias void ()* @g2
+ at g2 = internal global i8 42





More information about the llvm-commits mailing list