mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2026-04-20 00:20:11 +00:00
Be more careful in parsing Module::ModFlagBehavior value
to make sure we don't do invalid load of an enum. Share the conversion code between llvm::Module implementation and the verifier. This bug was reported by UBSan. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217395 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
+15
-4
@@ -259,6 +259,17 @@ void Module::eraseNamedMetadata(NamedMDNode *NMD) {
|
||||
NamedMDList.erase(NMD);
|
||||
}
|
||||
|
||||
bool Module::isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB) {
|
||||
if (ConstantInt *Behavior = dyn_cast<ConstantInt>(V)) {
|
||||
uint64_t Val = Behavior->getLimitedValue();
|
||||
if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
|
||||
MFB = static_cast<ModFlagBehavior>(Val);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
|
||||
void Module::
|
||||
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
|
||||
@@ -266,15 +277,15 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
|
||||
if (!ModFlags) return;
|
||||
|
||||
for (const MDNode *Flag : ModFlags->operands()) {
|
||||
if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) &&
|
||||
ModFlagBehavior MFB;
|
||||
if (Flag->getNumOperands() >= 3 &&
|
||||
isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
|
||||
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));
|
||||
Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user