diff options
Diffstat (limited to 'lib/IR/Module.cpp')
-rw-r--r-- | lib/IR/Module.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/lib/IR/Module.cpp b/lib/IR/Module.cpp index 968b8f4..4f240c7 100644 --- a/lib/IR/Module.cpp +++ b/lib/IR/Module.cpp @@ -245,7 +245,7 @@ GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) { /// 1. If it does not exist, add a declaration of the global and return it. /// 2. Else, the global exists but has the wrong type: return the function /// with a constantexpr cast to the right type. -/// 3. Finally, if the existing global is the correct delclaration, return the +/// 3. Finally, if the existing global is the correct declaration, return the /// existing global. Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { // See if we have a definition for the specified global already. @@ -260,8 +260,10 @@ Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { // If the variable exists but has the wrong type, return a bitcast to the // right type. - if (GV->getType() != PointerType::getUnqual(Ty)) - return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); + Type *GVTy = GV->getType(); + PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace()); + if (GVTy != PTy) + return ConstantExpr::getBitCast(GV, PTy); // Otherwise, we just found the existing function or a prototype. return GV; @@ -316,11 +318,16 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) { MDNode *Flag = ModFlags->getOperand(i); - ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0)); - MDString *Key = cast<MDString>(Flag->getOperand(1)); - Value *Val = Flag->getOperand(2); - Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()), - Key, Val)); + if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) && + isa<MDString>(Flag->getOperand(1))) { + // Check the operands of the MDNode before accessing the operands. + // The verifier will actually catch these failures. + ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0)); + MDString *Key = cast<MDString>(Flag->getOperand(1)); + Value *Val = Flag->getOperand(2); + Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()), + Key, Val)); + } } } @@ -399,9 +406,15 @@ bool Module::isDematerializable(const GlobalValue *GV) const { } bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) { - if (Materializer) - return Materializer->Materialize(GV, ErrInfo); - return false; + if (!Materializer) + return false; + + error_code EC = Materializer->Materialize(GV); + if (!EC) + return false; + if (ErrInfo) + *ErrInfo = EC.message(); + return true; } void Module::Dematerialize(GlobalValue *GV) { @@ -412,7 +425,12 @@ void Module::Dematerialize(GlobalValue *GV) { bool Module::MaterializeAll(std::string *ErrInfo) { if (!Materializer) return false; - return Materializer->MaterializeModule(this, ErrInfo); + error_code EC = Materializer->MaterializeModule(this); + if (!EC) + return false; + if (ErrInfo) + *ErrInfo = EC.message(); + return true; } bool Module::MaterializeAllPermanently(std::string *ErrInfo) { |