Skip to content

Commit

Permalink
fix a bug of int128 (#1624)
Browse files Browse the repository at this point in the history
* fix a bug of int128

* wrap integer value function

* revise the std::make_pair

* Revert "revise the std::make_pair"

This reverts commit 75be147.

* fix addConstantIntObjNode and addConstantIntValNode

* remove getSExt ZExt in SVF-LLVM

---------

Co-authored-by: bjjwwang <[email protected]>
  • Loading branch information
bjjwwang and bjjwwang authored Jan 8, 2025
1 parent 32c2a51 commit a5cd486
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 25 deletions.
33 changes: 33 additions & 0 deletions svf-llvm/include/SVF-LLVM/LLVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,39 @@ inline bool isCallSite(const Value* val)
return SVFUtil::isa<CallBase>(val);
}

inline double getDoubleValue(const ConstantFP* fpValue) {
double dval = 0;
if (fpValue->isNormalFP())
{
const llvm::fltSemantics& semantics = fpValue->getValueAPF().getSemantics();
if (&semantics == &llvm::APFloat::IEEEhalf() ||
&semantics == &llvm::APFloat::IEEEsingle() ||
&semantics == &llvm::APFloat::IEEEdouble() ||
&semantics == &llvm::APFloat::IEEEquad() ||
&semantics == &llvm::APFloat::x87DoubleExtended())
{
dval = fpValue->getValueAPF().convertToDouble();
}
else
{
assert (false && "Unsupported floating point type");
abort();
}
}
else
{
// other cfp type, like isZero(), isInfinity(), isNegative(), etc.
// do nothing
}
return dval;
}

inline std::pair<s64_t, u64_t> getIntegerValue(const ConstantInt* intValue) {
if (intValue->getBitWidth() <= 64 && intValue->getBitWidth() >= 1)
return std::make_pair(intValue->getSExtValue(), intValue->getZExtValue());
else
return std::make_pair(0,0);
}

/// Return LLVM callsite given a value
inline const CallBase* getLLVMCallSite(const Value* value)
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/CppUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ s32_t cppUtil::getVCallIdx(const CallBase* cs)
}
else
{
idx_value = (s32_t)idx->getSExtValue();
idx_value = LLVMUtil::getIntegerValue(idx).first;
}
return idx_value;
}
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/DCHG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ std::string DCHGraph::diTypeToStr(const DIType *t)
int64_t count = -1;
if (const ConstantInt* ci = sr->getCount().dyn_cast<ConstantInt* >())
{
count = ci->getSExtValue();
count = LLVMUtil::getIntegerValue(ci).first;
}

ss << "[" << count << "]";
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/ICFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
/// default case is set to -1;
s64_t val = -1;
if (condVal && condVal->getBitWidth() <= 64)
val = condVal->getSExtValue();
val = LLVMUtil::getIntegerValue(condVal).first;
icfg->addConditionalIntraEdge(srcNode, dstNode,val);
}
else
Expand Down
3 changes: 1 addition & 2 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ std::vector<const Function* > LLVMModuleSet::getLLVMGlobalFunctions(const Global

if (priority && func)
{
queue.push(LLVMGlobalFunction(priority
->getZExtValue(),
queue.push(LLVMGlobalFunction(LLVMUtil::getIntegerValue(priority).second,
func));
}
}
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/ObjTypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ void ObjTypeInference::validateTypeCheck(const CallBase *cs)
SVFUtil::dyn_cast<llvm::ConstantInt>(cs->getOperand(1));
assert(pInt && "the second argument is a integer");
u32_t iTyNum = objTyToNumFields(objType);
if (iTyNum >= pInt->getZExtValue())
if (iTyNum >= LLVMUtil::getIntegerValue(pInt).second)
SVFUtil::outs() << SVFUtil::sucMsg("\t SUCCESS :") << dumpValueAndDbgInfo(cs)
<< SVFUtil::pasMsg(" TYPE: ")
<< dumpType(objType) << "\n";
Expand Down
18 changes: 9 additions & 9 deletions svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ void SVFIRBuilder::initialiseNodes()
}
else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
{
pag->addConstantFPValNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second, icfgNode);
pag->addConstantFPValNode(iter->first, LLVMUtil::getDoubleValue(fpValue), iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
fpValue, pag->getGNode(iter->second));
}
else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
{
pag->addConstantIntValNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second, icfgNode);
pag->addConstantIntValNode(iter->first, LLVMUtil::getIntegerValue(intValue), iter->second, icfgNode);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
intValue, pag->getGNode(iter->second));
}
Expand Down Expand Up @@ -322,13 +322,13 @@ void SVFIRBuilder::initialiseNodes()
}
else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
{
pag->addConstantFPObjNode(iter->first, fpValue->getValueAPF().convertToDouble(), iter->second);
pag->addConstantFPObjNode(iter->first, LLVMUtil::getDoubleValue(fpValue), iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
fpValue, pag->getGNode(iter->second));
}
else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
{
pag->addConstantIntObjNode(iter->first, intValue->getSExtValue(), intValue->getZExtValue(), iter->second);
pag->addConstantIntObjNode(iter->first, LLVMUtil::getIntegerValue(intValue), iter->second);
llvmModuleSet()->addToLLVMVal2SVFVarMap(
intValue, pag->getGNode(iter->second));
}
Expand Down Expand Up @@ -457,17 +457,17 @@ bool SVFIRBuilder::computeGepOffset(const User *V, AccessPath& ap)
// but we can distinguish different field of an array of struct, e.g. s[1].f1 is different from s[0].f2
if(const ArrayType* arrTy = SVFUtil::dyn_cast<ArrayType>(gepTy))
{
if(!op || (arrTy->getArrayNumElements() <= (u32_t)op->getSExtValue()))
if(!op || (arrTy->getArrayNumElements() <= (u32_t)LLVMUtil::getIntegerValue(op).first))
continue;
APOffset idx = op->getSExtValue();
APOffset idx = (u32_t)LLVMUtil::getIntegerValue(op).first;
u32_t offset = pag->getSymbolInfo()->getFlattenedElemIdx(llvmModuleSet()->getSVFType(arrTy), idx);
ap.setFldIdx(ap.getConstantStructFldIdx() + offset);
}
else if (const StructType *ST = SVFUtil::dyn_cast<StructType>(gepTy))
{
assert(op && "non-const offset accessing a struct");
//The actual index
APOffset idx = op->getSExtValue();
APOffset idx = (u32_t)LLVMUtil::getIntegerValue(op).first;
u32_t offset = pag->getSymbolInfo()->getFlattenedElemIdx(llvmModuleSet()->getSVFType(ST), idx);
ap.setFldIdx(ap.getConstantStructFldIdx() + offset);
}
Expand Down Expand Up @@ -1176,7 +1176,7 @@ void SVFIRBuilder::visitSwitchInst(SwitchInst &inst)
/// default case is set to -1;
s64_t val = -1;
if (condVal && condVal->getBitWidth() <= 64)
val = condVal->getSExtValue();
val = (u32_t)LLVMUtil::getIntegerValue(condVal).first;
const ICFGNode* icfgNode = llvmModuleSet()->getICFGNode(succInst);
successors.push_back(std::make_pair(icfgNode, val));
}
Expand Down Expand Up @@ -1297,7 +1297,7 @@ const Value* SVFIRBuilder::getBaseValueForExtArg(const Value* V)
for (bridge_gep_iterator gi = bridge_gep_begin(gep), ge = bridge_gep_end(gep); gi != ge; ++gi)
{
if(const ConstantInt* op = SVFUtil::dyn_cast<ConstantInt>(gi.getOperand()))
totalidx += op->getSExtValue();
totalidx += LLVMUtil::getIntegerValue(op).first;
}
if(totalidx == 0 && !SVFUtil::isa<StructType>(value->getType()))
value = gep->getPointerOperand();
Expand Down
5 changes: 3 additions & 2 deletions svf-llvm/lib/SVFIRExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const Type* SVFIRBuilder::getBaseTypeAndFlattenedFields(const Value* V, std::vec
/// use user-specified size for this copy operation if the size is a constaint int
if(szValue && SVFUtil::isa<ConstantInt>(szValue))
{
numOfElems = (numOfElems > SVFUtil::cast<ConstantInt>(szValue)->getSExtValue()) ? SVFUtil::cast<ConstantInt>(szValue)->getSExtValue() : numOfElems;
auto szIntVal = LLVMUtil::getIntegerValue(SVFUtil::cast<ConstantInt>(szValue));
numOfElems = (numOfElems > szIntVal.first) ? szIntVal.first : numOfElems;
}

LLVMContext& context = LLVMModuleSet::getLLVMModuleSet()->getContext();
Expand All @@ -64,7 +65,7 @@ const Type* SVFIRBuilder::getBaseTypeAndFlattenedFields(const Value* V, std::vec
{
SymbolTableBuilder builder(pag->getSymbolInfo());
builder.collectSym(offset);
pag->addConstantIntValNode(svfOffset, offset->getSExtValue(), offset->getZExtValue(), pag->getSymbolInfo()->getValSym(svfOffset), nullptr);
pag->addConstantIntValNode(svfOffset, LLVMUtil::getIntegerValue(offset), pag->getSymbolInfo()->getValSym(svfOffset), nullptr);
}
ls.addOffsetVarAndGepTypePair(getPAG()->getGNode(getPAG()->getValueNode(svfOffset)), nullptr);
fields.push_back(ls);
Expand Down
6 changes: 3 additions & 3 deletions svf-llvm/lib/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ u32_t SymbolTableBuilder::analyzeHeapAllocByteSize(const Value* val)
llvm::dyn_cast<llvm::ConstantInt>(arg))
{
// Multiply the constant Value if all Args are const
product *= constIntArg->getZExtValue();
product *= LLVMUtil::getIntegerValue(constIntArg).second;
}
else
{
Expand Down Expand Up @@ -871,8 +871,8 @@ void SymbolTableBuilder::initTypeInfo(ObjTypeInfo* typeinfo, const Value* val,
/// In most cases, `NumElements` is not specified in the instruction, which means there is only one element (objSize=1).
if(const ConstantInt* sz = SVFUtil::dyn_cast<ConstantInt>(allocaInst->getArraySize()))
{
elemNum = sz->getZExtValue() * getNumOfElements(objTy);
byteSize = sz->getZExtValue() * typeinfo->getType()->getByteSize();
elemNum = LLVMUtil::getIntegerValue(sz).second * getNumOfElements(objTy);
byteSize = LLVMUtil::getIntegerValue(sz).second * typeinfo->getType()->getByteSize();
}
/// if ArraySize is not constant, byteSize is not static determined.
else
Expand Down
9 changes: 4 additions & 5 deletions svf/include/SVFIR/SVFIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ class SVFIR : public IRGraph
return addNode(node, i);
}

inline NodeID addConstantIntValNode(const SVFValue* curInst, s64_t sval, u64_t zval, const NodeID i,
inline NodeID addConstantIntValNode(const SVFValue* curInst, const std::pair<s64_t, u64_t>& intValue, const NodeID i,
const ICFGNode* icfgNode)
{
SVFVar* node = new ConstantIntValVar(curInst, sval, zval, i, icfgNode);
SVFVar* node = new ConstantIntValVar(curInst, intValue.first, intValue.second, i, icfgNode);
return addNode(node, i);
}

Expand Down Expand Up @@ -656,13 +656,12 @@ class SVFIR : public IRGraph
}


inline NodeID addConstantIntObjNode(const SVFValue* curInst, s64_t sval, u64_t zval, const NodeID i)
{
inline NodeID addConstantIntObjNode(const SVFValue* curInst, const std::pair<s64_t, u64_t>& intValue, const NodeID i) {
const MemObj* mem = getMemObj(curInst);
NodeID base = mem->getId();
memToFieldsMap[base].set(mem->getId());
ConstantIntObjVar* node =
new ConstantIntObjVar(curInst, sval, zval, mem->getId(), mem);
new ConstantIntObjVar(curInst, intValue.first, intValue.second, mem->getId(), mem);
return addObjNode(curInst, node, mem->getId());
}

Expand Down

0 comments on commit a5cd486

Please sign in to comment.