59 const AbstractValue& lhsVal = ae.getAbsValue(gep->getLHSVar(), node);
60 const AbstractValue& rhsVal = ae.getAbsValue(gep->getRHSVar(), node);
62 ae.getGepByteOffset(gep));
65 bool hasUnknownSizeTarget =
false;
66 for (
const auto& addr : objAddrs)
70 const NodeID objId = ae.getAbsState(node).getIDFromAddr(addr);
73 hasUnknownSizeTarget =
true;
90 hasUnknownSizeTarget =
true;
97 const auto bounds = ae.getAllocaInstByteSize(addrStmt);
98 size = bounds.upperBound;
99 lower = bounds.lowerBound;
110 {IntervalValue(lower, size), accessOffset});
115 if (hasUnknownSizeTarget)
118 IntervalValue(0, IntervalValue::plus_infinity()),
119 ae.getGepByteOffset(gep)
148 if (funcName ==
"SAFE_BUFACCESS")
150 ae.getUtils()->checkpoints.erase(callNode);
157 assert(
false &&
"SAFE_BUFACCESS size is bottom");
164 <<
" — " << callNode->
toString() <<
"\n";
174 else if (funcName ==
"UNSAFE_BUFACCESS")
176 ae.getUtils()->checkpoints.erase(callNode);
177 if (callNode->
arg_size() < 2)
return;
181 assert(
false &&
"UNSAFE_BUFACCESS size is bottom");
188 <<
" — " << callNode->
toString() <<
"\n";
249 if (annotation.find(
"MEMCPY") != std::string::npos)
251 if (annotation.find(
"MEMSET") != std::string::npos)
253 if (annotation.find(
"STRCPY") != std::string::npos)
255 if (annotation.find(
"STRCAT") != std::string::npos)
268 std::vector<std::pair<u32_t, u32_t>> args =
270 for (
auto arg : args)
288 std::vector<std::pair<u32_t, u32_t>> args =
290 for (
auto arg : args)
341 return ae.getGepByteOffset(gep);
370 for (
const auto& objAddr : objAddrs)
381 for (
const auto& gepAddr : gepAddrs)
396 "GEP object is neither a GepObjVar nor an address without a backing object");
406 for (
const auto& gepAddr : gepAddrs)
417 gepObjVar, objOffsetFromBase +
offset);
422 "GEP RHS object has no offset from base");
433 "GEP object is neither a GepObjVar nor an address without a backing object");
455 IntervalValue strLen = ae.getUtils()->getStrlen(arg1Val, call);
462 const std::vector<std::string> strcatGroup = {
"__strcat_chk",
"strcat",
"__wcscat_chk",
"wcscat"};
463 const std::vector<std::string> strncatGroup = {
"__strncat_chk",
"strncat",
"__wcsncat_chk",
"wcsncat"};
473 IntervalValue totalLen = ae.getUtils()->getStrlen(arg0Val, call);
474 return canSafelyAccessMemory(arg0Val, ae.getUtils()->getStringLastByte(call, totalLen), call, &info);
478 assert(
false &&
"Unknown strcat function, please add it to strcatGroup or strncatGroup");
497 if (info) *info = {};
505 ae.updateAbsValue(value, ptrVal, node);
509 if (std::any_of(addresses.
begin(), addresses.
end(), [&](
u32_t addr)
511 return svfir->isNullOrBlkObj(ae.getAbsState(node).getIDFromAddr(addr));
515 for (
const auto& addr : addresses)
517 NodeID objId = ae.getAbsState(node).getIDFromAddr(addr);
536 const auto bounds = ae.getAllocaInstByteSize(addrStmt);
537 size = bounds.upperBound;
538 lower = bounds.lowerBound;
556 if (
offset.ub().getIntNumeral() >= size)
585 const ValVar* rhs = gep->getRHSVar();
596 const ValVar* lhs = load->getLHSVar();
612 if (funcName ==
"UNSAFE_LOAD")
615 ae.getUtils()->checkpoints.erase(callNode);
623 <<
" arg0=" << arg0Val->
getId() <<
" isSafe=" << isSafe
628 <<
" — " << callNode->
toString() <<
"\n";
638 else if (funcName ==
"SAFE_LOAD")
641 ae.getUtils()->checkpoints.erase(callNode);
642 if (callNode->
arg_size() < 1)
return;
649 <<
" — " << callNode->
toString() <<
"\n";
666 std::vector<u32_t> tmp_args;
669 if (annotation.find(
"MEMCPY") != std::string::npos)
674 tmp_args.push_back(0);
675 tmp_args.push_back(1);
680 tmp_args.push_back(1);
681 tmp_args.push_back(2);
682 tmp_args.push_back(3);
683 tmp_args.push_back(4);
686 else if (annotation.find(
"MEMSET") != std::string::npos)
689 tmp_args.push_back(0);
691 else if (annotation.find(
"STRCPY") != std::string::npos)
694 tmp_args.push_back(0);
695 tmp_args.push_back(1);
697 else if (annotation.find(
"STRCAT") != std::string::npos)
701 tmp_args.push_back(0);
702 tmp_args.push_back(1);
706 for (
const auto &arg: tmp_args)
725 if (!AbsVal.
isAddr())
return true;
726 for (
const auto &addr: AbsVal.
getAddrs())
730 ae.getAbsState(node).isFreedMem(addr))
Exception class for handling errors in Abstract Execution.
ExtAPIType
Enumeration of external API types.
static AbstractInterpretation & getAEInstance()
u32_t getIDFromAddr(u32_t addr) const
Return the internal index if addr is an address otherwise return the value of idx.
static bool isNullOrBlackHoleAddr(u32_t addr)
Whether addr has no concrete backing memory object.
AddressValue & getAddrs()
AddrSet::const_iterator end() const
AddrSet::const_iterator begin() const
const ICFGNode * getICFGNode() const
Get the ICFGNode related to the creation of this object.
bool isConstantByteSize() const
Check if byte size is a const value.
u32_t getByteSizeOfObj() const
Get the byte size of this object.
s64_t getIntNumeral() const
bool detectStrcat(const CallICFGNode *call, BufferOverflowInfo &info)
Detects buffer overflow in 'strcat' function calls.
void addToGepObjOffsetFromBase(const GepObjVar *obj, const IntervalValue &offset)
Adds an offset to a GEP object.
void detect(const ICFGNode *) override
Detect buffer overflow issues within a node.
Map< std::string, std::vector< std::pair< u32_t, u32_t > > > extAPIBufOverflowCheckRules
Rules for checking buffer overflows in external APIs.
IntervalValue getAccessOffset(NodeID objId, const GepStmt *gep)
Retrieves the access offset for a given object and GEP statement.
void updateGepObjOffsetFromBase(const ICFGNode *node, AddressValue gepAddrs, AddressValue objAddrs, IntervalValue offset)
Updates the offset of a GEP object from its base.
void detectExtAPI(const CallICFGNode *call)
Handles external API calls related to buffer overflow detection.
IntervalValue getGepObjOffsetFromBase(const GepObjVar *obj) const
Retrieves the offset of a GEP object from its base.
bool canSafelyAccessMemory(const ValVar *value, const IntervalValue &len, const ICFGNode *node, BufferOverflowInfo *info=nullptr)
Checks if memory can be safely accessed.
void handleStubFunctions(const CallICFGNode *) override
Handles external API calls related to buffer overflow detection.
bool hasGepObjOffsetFromBase(const GepObjVar *obj) const
Checks if a GEP object has an associated offset.
void initExtAPIBufOverflowCheckRules()
Initializes external API buffer overflow check rules.
void addBugToReporter(const AEException &e, const ICFGNode *node)
Adds a bug to the reporter based on an exception.
bool detectStrcpy(const CallICFGNode *call, BufferOverflowInfo &info)
Detects buffer overflow in 'strcpy' function calls.
const std::string toString() const override
const ValVar * getArgument(u32_t ArgNo) const
Parameter operations.
const FunObjVar * getCalledFunction() const
const std::string getSourceLoc() const override
static ExtAPI * getExtAPI()
const SVFStmtList & getSVFStmts() const
static bool isNullOrBlkObj(NodeID id)
Check a decoded NodeID, not an AE virtual memory address.
const BoundedInt & ub() const
Return the upper bound.
static IntervalValue top()
Create the IntervalValue [-inf, +inf].
bool canSafelyDerefPtr(const ValVar *ptr, const ICFGNode *node)
bool isUninit(AbstractValue v)
Checks if an Abstract Value is uninitialized.
void handleStubFunctions(const CallICFGNode *call) override
Handles external API calls related to nullptr dereferences.
void detect(const ICFGNode *node) override
Detects nullptr dereferences issues within a node.
void addBugToReporter(const AEException &e, const ICFGNode *node)
Adds a bug to the reporter based on an exception.
void detectExtAPI(const CallICFGNode *call)
Handle external API calls related to nullptr dereferences.
const BaseObjVar * getBaseObject(NodeID id) const
const SVFVar * getSVFVar(NodeID id) const
ObjVar/GepObjVar/BaseObjVar.
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
NodeID getId() const
Get ID.
virtual const std::string & getName() const
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
LLVM_NODISCARD bool isa(const Y &Val)
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
std::ostream & errs()
Overwrite llvm::errs().
bool isExtCall(const FunObjVar *fun)
std::ostream & outs()
Overwrite llvm::outs().
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)