InitModelTypes: fix IntToPtr

Before this commit, initModelTypes could return integers of the wrong
size for IntToPtrInst, whenever the integer being casted to pointer did
not have the exact same size of the pointer on the model.

This commit fixes the problem. Now initModelTypes, even if it might be
forced to return an integer type for IntToPtr, it makes sure that the
size of that integer matches the size of the pointer on the model.
This commit is contained in:
Pietro Fezzardi
2024-03-15 14:59:07 +01:00
committed by Alessandro Di Federico
parent 71f61d9eb5
commit 4531b9ff5d
+23 -5
View File
@@ -108,9 +108,15 @@ static RecursiveCoroutine<bool> addOperandType(const llvm::Value *Operand,
TypeMap.insert({ Operand, OperandType });
} else if (not PointersOnly) {
// Fallback to the LLVM type
auto ConstType = llvmIntToModelType(Operand->getType(), Model);
TypeMap.insert({ Operand, ConstType });
auto
ByteSize = model::Architecture::getPointerSize(Model
.Architecture());
auto BitWidth = 8 * ByteSize;
auto *LLVMIntPtrType = llvm::IntegerType::getIntNTy(Operand
->getContext(),
BitWidth);
QualifiedType IntPtrType = llvmIntToModelType(LLVMIntPtrType, Model);
TypeMap.insert({ Operand, IntPtrType });
}
rc_return true;
}
@@ -642,8 +648,20 @@ initModelTypesImpl(FunctionMetadataCache &Cache,
case Instruction::PtrToInt: {
// Forward the type if there is one
auto It = TypeMap.find(I.getOperand(0));
if (It != TypeMap.end())
Type = It->second;
if (It != TypeMap.end()) {
const QualifiedType &OperandType = It->second;
if (OperandType.isPointer()) {
Type = OperandType;
} else if (not PointersOnly) {
auto ByteSize = model::Architecture::getPointerSize(Model
.Architecture());
auto BitWidth = 8 * ByteSize;
auto *LLVMIntPtrType = llvm::IntegerType::getIntNTy(I.getContext(),
BitWidth);
Type = llvmIntToModelType(LLVMIntPtrType, Model);
revng_assert(ByteSize == Type->size());
}
}
} break;
case Instruction::PHI: {