Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVF::NullptrDerefDetector Class Reference

#include <AEDetector.h>

Inheritance diagram for SVF::NullptrDerefDetector:
SVF::AEDetector

Public Member Functions

 NullptrDerefDetector ()
 ~NullptrDerefDetector ()=default
void detect (const ICFGNode *node) override
 Detects nullptr dereferences issues within a node.
void handleStubFunctions (const CallICFGNode *call) override
 Handles external API calls related to nullptr dereferences.
bool isUninit (AbstractValue v)
 Checks if an Abstract Value is uninitialized.
void addBugToReporter (const ICFGNode *node, const SVFStmt *stmt=nullptr)
 Reports a bug, generating its text only for a new location.
void reportBug () override
 Reports all detected nullptr dereference bugs.
void detectExtAPI (const CallICFGNode *call)
 Handle external API calls related to nullptr dereferences.
bool isNull (AbstractValue v)
 Check if an Abstract Value is NULL (or uninitialized).
bool canSafelyDerefPtr (const ValVar *ptr, const ICFGNode *node)
Public Member Functions inherited from SVF::AEDetector
 AEDetector ()
 Constructor initializes the detector kind to UNKNOWN.
virtual ~AEDetector ()=default
 Virtual destructor for safe polymorphic use.
DetectorKind getKind () const
 Get the kind of the detector.

Static Public Member Functions

static bool classof (const AEDetector *detector)
Static Public Member Functions inherited from SVF::AEDetector
static bool classof (const AEDetector *detector)
 Check if the detector is of the UNKNOWN kind.

Private Attributes

Set< std::string > bugLoc
 Set of locations where bugs have been reported.
SVFBugReport recoder
 Recorder for abstract execution bugs.
Map< const ICFGNode *, std::string > nodeToBugInfo
 Maps ICFG nodes to bug information.

Friends

class AbstractInterpretation

Additional Inherited Members

Public Types inherited from SVF::AEDetector
enum  DetectorKind { BUF_OVERFLOW , NULL_DEREF , UNKNOWN }
 Enumerates the types of detectors available. More...
Protected Attributes inherited from SVF::AEDetector
DetectorKind kind
 The kind of the detector.

Detailed Description

Definition at line 321 of file AEDetector.h.

Constructor & Destructor Documentation

◆ NullptrDerefDetector()

SVF::NullptrDerefDetector::NullptrDerefDetector ( )
inline

Definition at line 325 of file AEDetector.h.

326 {
328 }
@ NULL_DEREF
Detector for nullptr dereference issues.
Definition AEDetector.h:52
DetectorKind kind
The kind of the detector.
Definition AEDetector.h:104

◆ ~NullptrDerefDetector()

SVF::NullptrDerefDetector::~NullptrDerefDetector ( )
default

Member Function Documentation

◆ addBugToReporter()

void SVF::NullptrDerefDetector::addBugToReporter ( const ICFGNode * node,
const SVFStmt * stmt = nullptr )
inline

Reports a bug, generating its text only for a new location.

Parameters
nodePointer to the ICFG node where the bug was detected.
stmtOptional statement to describe; otherwise describe node.

Definition at line 367 of file AEDetector.h.

368 {
369 GenericBug::EventStack eventStack;
370 SVFBugEvent sourceInstEvent(SVFBugEvent::EventType::SourceInst, node);
371 eventStack.push_back(sourceInstEvent); // Add the source instruction event to the event stack
372
373 if (eventStack.empty())
374 {
375 return; // If the event stack is empty, return early
376 }
377 std::string loc = eventStack.back().getEventLoc(); // Get the location of the last event in the stack
378
379 // Check if the bug at this location has already been reported
380 if (bugLoc.find(loc) != bugLoc.end())
381 {
382 return; // If the bug location is already reported, return early
383 }
384 else
385 {
386 bugLoc.insert(loc); // Otherwise, mark this location as reported
387 }
388 recoder.addAbsExecBug(GenericBug::FULLNULLPTRDEREFERENCE, eventStack, 0, 0, 0, 0);
389 // Duplicate locations returned above without formatting the IR again.
390 nodeToBugInfo[node] = stmt ? stmt->toString() : node->toString();
391 }
std::vector< SVFBugEvent > EventStack
Set< std::string > bugLoc
Set of locations where bugs have been reported.
Definition AEDetector.h:431
SVFBugReport recoder
Recorder for abstract execution bugs.
Definition AEDetector.h:432
Map< const ICFGNode *, std::string > nodeToBugInfo
Maps ICFG nodes to bug information.
Definition AEDetector.h:433

◆ canSafelyDerefPtr()

bool NullptrDerefDetector::canSafelyDerefPtr ( const ValVar * ptr,
const ICFGNode * node )

Definition at line 706 of file AEDetector.cpp.

707{
709 const AbstractValue& AbsVal = ae.getAbsValue(value, node);
710 if (isUninit(AbsVal)) return false;
711 if (!AbsVal.isAddr()) return true;
712 for (const auto &addr: AbsVal.getAddrs())
713 {
714 // Unknown, null, and freed addresses cannot be safely dereferenced.
716 ae.getAbsState(node).isFreedMem(addr))
717 return false;
718 }
719 return true;
720}
static AbstractInterpretation & getAEInstance()
static bool isNullOrBlackHoleAddr(u32_t addr)
Whether addr has no concrete backing memory object.
AddressValue & getAddrs()
bool isAddr() const
bool isUninit(AbstractValue v)
Checks if an Abstract Value is uninitialized.
Definition AEDetector.h:355

◆ classof()

bool SVF::NullptrDerefDetector::classof ( const AEDetector * detector)
inlinestatic

Definition at line 332 of file AEDetector.h.

333 {
334 return detector->getKind() == AEDetector::NULL_DEREF;
335 }

◆ detect()

void NullptrDerefDetector::detect ( const ICFGNode * node)
overridevirtual

Detects nullptr dereferences issues within a node.

Parameters
asReference to the abstract state.
nodePointer to the ICFG node.

Implements SVF::AEDetector.

Definition at line 554 of file AEDetector.cpp.

555{
557 {
558 // external API like memset(*dst, elem, sz)
559 // we check if it's external api and check the corrisponding index
560 const CallICFGNode* callNode = SVFUtil::cast<CallICFGNode>(node);
561 if (SVFUtil::isExtCall(callNode->getCalledFunction()))
562 {
563 detectExtAPI(callNode);
564 }
565 }
566 else
567 {
568 for (const auto& stmt: node->getSVFStmts())
569 {
570 if (const GepStmt* gep = SVFUtil::dyn_cast<GepStmt>(stmt))
571 {
572 // like llvm bitcode `p = gep p, idx`
573 // we check rhs p's all address are valid mem
574 const ValVar* rhs = gep->getRHSVar();
575 if (!canSafelyDerefPtr(rhs, node))
576 {
577 addBugToReporter(stmt->getICFGNode(), stmt);
578 }
579 }
580 else if (const LoadStmt* load = SVFUtil::dyn_cast<LoadStmt>(stmt))
581 {
582 // like llvm bitcode `p = load q`
583 // we check lhs p's all address are valid mem
584 const ValVar* lhs = load->getLHSVar();
585 if (!canSafelyDerefPtr(lhs, node))
586 {
587 addBugToReporter(stmt->getICFGNode(), stmt);
588 }
589 }
590 }
591 }
592}
const FunObjVar * getCalledFunction() const
Definition ICFGNode.h:501
const SVFStmtList & getSVFStmts() const
Definition ICFGNode.h:116
bool canSafelyDerefPtr(const ValVar *ptr, const ICFGNode *node)
void addBugToReporter(const ICFGNode *node, const SVFStmt *stmt=nullptr)
Reports a bug, generating its text only for a new location.
Definition AEDetector.h:367
void detectExtAPI(const CallICFGNode *call)
Handle external API calls related to nullptr dereferences.
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
Definition Casting.h:360
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241
bool isExtCall(const FunObjVar *fun)
Definition SVFUtil.cpp:524
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition Casting.h:405

◆ detectExtAPI()

void NullptrDerefDetector::detectExtAPI ( const CallICFGNode * call)

Handle external API calls related to nullptr dereferences.

Parameters
asReference to the abstract state.
callPointer to the call ICFG node.

Definition at line 648 of file AEDetector.cpp.

649{
650 assert(call->getCalledFunction() && "FunObjVar* is nullptr");
651 // get ext type
652 // get argument index which are nullptr deref checkpoints for extapi
653 std::vector<u32_t> tmp_args;
654 for (const std::string &annotation: ExtAPI::getExtAPI()->getExtFuncAnnotations(call->getCalledFunction()))
655 {
656 if (annotation.find("MEMCPY") != std::string::npos)
657 {
658 if (call->arg_size() < 4)
659 {
660 // for memcpy(void* dest, const void* src, size_t n)
661 tmp_args.push_back(0);
662 tmp_args.push_back(1);
663 }
664 else
665 {
666 // for unsigned long iconv(void* cd, char **restrict inbuf, unsigned long *restrict inbytesleft, char **restrict outbuf, unsigned long *restrict outbytesleft)
667 tmp_args.push_back(1);
668 tmp_args.push_back(2);
669 tmp_args.push_back(3);
670 tmp_args.push_back(4);
671 }
672 }
673 else if (annotation.find("MEMSET") != std::string::npos)
674 {
675 // for memset(void* dest, elem, sz)
676 tmp_args.push_back(0);
677 }
678 else if (annotation.find("STRCPY") != std::string::npos)
679 {
680 // for strcpy(void* dest, void* src)
681 tmp_args.push_back(0);
682 tmp_args.push_back(1);
683 }
684 else if (annotation.find("STRCAT") != std::string::npos)
685 {
686 // for strcat(void* dest, const void* src)
687 // for strncat(void* dest, const void* src, size_t n)
688 tmp_args.push_back(0);
689 tmp_args.push_back(1);
690 }
691 }
692
693 for (const auto &arg: tmp_args)
694 {
695 if (call->arg_size() <= arg)
696 continue;
697 const ValVar* argVal = call->getArgument(arg);
698 if (argVal && !canSafelyDerefPtr(argVal, call))
699 {
700 addBugToReporter(call);
701 }
702 }
703}
const ValVar * getArgument(u32_t ArgNo) const
Parameter operations.
Definition ICFGNode.h:483
u32_t arg_size() const
Definition ICFGNode.h:488
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:43

◆ handleStubFunctions()

void NullptrDerefDetector::handleStubFunctions ( const CallICFGNode * call)
overridevirtual

Handles external API calls related to nullptr dereferences.

Parameters
callPointer to the call ICFG node.

Implements SVF::AEDetector.

Definition at line 595 of file AEDetector.cpp.

596{
597 std::string funcName = callNode->getCalledFunction()->getName();
599 if (funcName == "UNSAFE_LOAD")
600 {
601 // void UNSAFE_LOAD(void* ptr);
602 ae.getUtils()->checkpoints.erase(callNode);
603 if (callNode->arg_size() < 1)
604 return;
605
606 const ValVar* arg0Val = callNode->getArgument(0);
607 // opt may directly dereference a null pointer and call UNSAFE_LOAD(null)
608 bool isSafe = canSafelyDerefPtr(arg0Val, callNode) && arg0Val->getId() != 0;
609 SVFUtil::outs() << "[UNSAFE_LOAD] node=" << callNode->getId()
610 << " arg0=" << arg0Val->getId() << " isSafe=" << isSafe
611 << "\n";
612 if (!isSafe)
613 {
614 SVFUtil::outs() << SVFUtil::sucMsg("success: expected null dereference at UNSAFE_LOAD")
615 << " — " << callNode->toString() << "\n";
616 return;
617 }
618 else
619 {
620 SVFUtil::outs() << SVFUtil::errMsg("failure: null dereference expected at UNSAFE_LOAD, but none detected")
621 << " — Position: " << callNode->getSourceLoc() << "\n";
622 assert(false);
623 }
624 }
625 else if (funcName == "SAFE_LOAD")
626 {
627 // void SAFE_LOAD(void* ptr);
628 ae.getUtils()->checkpoints.erase(callNode);
629 if (callNode->arg_size() < 1) return;
630 const ValVar* arg0Val = callNode->getArgument(0);
631 // opt may directly dereference a null pointer and call UNSAFE_LOAD(null)ols
632 bool isSafe = canSafelyDerefPtr(arg0Val, callNode) && arg0Val->getId() != 0;
633 if (isSafe)
634 {
635 SVFUtil::outs() << SVFUtil::sucMsg("success: expected safe dereference at SAFE_LOAD")
636 << " — " << callNode->toString() << "\n";
637 return;
638 }
639 else
640 {
641 SVFUtil::outs() << SVFUtil::errMsg("failure: unexpected null dereference at SAFE_LOAD")
642 << " — Position: " << callNode->getSourceLoc() << "\n";
643 assert(false);
644 }
645 }
646}
NodeID getId() const
Get ID.
Definition SVFValue.h:158
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
Definition SVFUtil.cpp:75
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition SVFUtil.cpp:98
std::ostream & outs()
Overwrite llvm::outs().
Definition SVFUtil.h:58

◆ isNull()

bool SVF::NullptrDerefDetector::isNull ( AbstractValue v)
inline

Check if an Abstract Value is NULL (or uninitialized).

Parameters
vAn Abstract Value of loaded from an address in an Abstract State.

Definition at line 423 of file AEDetector.h.

424 {
425 return !v.isAddr() && !v.isInterval();
426 }

◆ isUninit()

bool SVF::NullptrDerefDetector::isUninit ( AbstractValue v)
inline

Checks if an Abstract Value is uninitialized.

Parameters
vThe Abstract Value to check.
Returns
True if the value is uninitialized, false otherwise.

Definition at line 355 of file AEDetector.h.

356 {
357 // uninitialized value has neither interval value nor address value
358 bool is = v.getAddrs().isBottom() && v.getInterval().isBottom();
359 return is;
360 }

◆ reportBug()

void SVF::NullptrDerefDetector::reportBug ( )
inlineoverridevirtual

Reports all detected nullptr dereference bugs.

Implements SVF::AEDetector.

Definition at line 396 of file AEDetector.h.

397 {
398 if (!nodeToBugInfo.empty())
399 {
400 std::cerr << "###################### Nullptr Dereference (" + std::to_string(nodeToBugInfo.size())
401 + " found)######################\n";
402 std::cerr << "---------------------------------------------\n";
403 for (const auto& it : nodeToBugInfo)
404 {
405 std::cerr << it.second << "\n---------------------------------------------\n";
406 }
407 }
408 }

◆ AbstractInterpretation

friend class AbstractInterpretation
friend

Definition at line 323 of file AEDetector.h.

Member Data Documentation

◆ bugLoc

Set<std::string> SVF::NullptrDerefDetector::bugLoc
private

Set of locations where bugs have been reported.

Definition at line 431 of file AEDetector.h.

◆ nodeToBugInfo

Map<const ICFGNode*, std::string> SVF::NullptrDerefDetector::nodeToBugInfo
private

Maps ICFG nodes to bug information.

Definition at line 433 of file AEDetector.h.

◆ recoder

SVFBugReport SVF::NullptrDerefDetector::recoder
private

Recorder for abstract execution bugs.

Definition at line 432 of file AEDetector.h.


The documentation for this class was generated from the following files: