[PATCH] D60613: Make setInitializer() assert that the entire initializer is usable.

Arnt Gulbrandsen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 12 06:42:35 PDT 2019


arnt created this revision.
arnt added reviewers: efriedma, arichardson.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Until now, it was possible to set an initilizer for e.g. a struct that
contained a global constant from some other module. The verifier would
complain, but the verifier's complaint doesn't point very accurately at
the problem.


Repository:
  rL LLVM

https://reviews.llvm.org/D60613

Files:
  llvm/lib/IR/Globals.cpp


Index: llvm/lib/IR/Globals.cpp
===================================================================
--- llvm/lib/IR/Globals.cpp
+++ llvm/lib/IR/Globals.cpp
@@ -359,6 +359,21 @@
   getParent()->getGlobalList().erase(getIterator());
 }
 
+static void assertThatAllGlobalsAreIn(Constant *Value, Module *Correct) {
+  if(ConstantStruct *V = dyn_cast<ConstantStruct>(Value)) {
+    int n = V->getNumOperands();
+    while(n)
+      assertThatAllGlobalsAreIn(V->getOperand(--n), Correct);
+  } else if(ConstantArray *V = dyn_cast<ConstantArray>(Value)) {
+    int n = V->getType()->getNumElements();
+    while(n)
+      assertThatAllGlobalsAreIn(V->getAggregateElement(--n), Correct);
+  } else if(GlobalValue *V = dyn_cast<GlobalValue>(Value)) {
+    assert((!V->getParent() || V->getParent() == Correct) &&
+           "A Constant and its initializer cannot be in different modules");
+  }
+}
+
 void GlobalVariable::setInitializer(Constant *InitVal) {
   if (!InitVal) {
     if (hasInitializer()) {
@@ -377,6 +392,7 @@
     if (!hasInitializer())
       setGlobalVariableNumOperands(1);
     Op<0>().set(InitVal);
+    assertThatAllGlobalsAreIn(InitVal, getParent());
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60613.194868.patch
Type: text/x-patch
Size: 1176 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190412/86ea7c2d/attachment.bin>


More information about the llvm-commits mailing list