From 4531b9ff5d99df36c07dfc6374e8ccbc931aa33d Mon Sep 17 00:00:00 2001 From: Pietro Fezzardi Date: Fri, 15 Mar 2024 14:59:07 +0100 Subject: [PATCH] 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. --- lib/InitModelTypes/InitModelTypes.cpp | 28 ++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/InitModelTypes/InitModelTypes.cpp b/lib/InitModelTypes/InitModelTypes.cpp index bc718b7f0..ee36cd174 100644 --- a/lib/InitModelTypes/InitModelTypes.cpp +++ b/lib/InitModelTypes/InitModelTypes.cpp @@ -108,9 +108,15 @@ static RecursiveCoroutine 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: {