decompiler  1.0.0
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
ghidra::AliasChecker Class Reference

A light-weight class for analyzing pointers and aliasing on the stack. More...

#include <varmap.hh>

Classes

struct  AddBase
 A helper class holding a Varnode pointer reference and a possible index added to it. More...
 

Public Member Functions

 AliasChecker ()
 Constructor.
 
void gather (const Funcdata *f, AddrSpace *spc, bool defer)
 Gather Varnodes that point on the stack. More...
 
bool hasLocalAlias (Varnode *vn) const
 Return true if it looks like the given Varnode is aliased by a pointer. More...
 
void sortAlias (void) const
 Sort the alias starting offsets.
 
const vector< AddBase > & getAddBase (void) const
 Get the collection of pointer Varnodes.
 
const vector< uintb > & getAlias (void) const
 Get the list of alias starting offsets.
 

Static Public Member Functions

static void gatherAdditiveBase (Varnode *startvn, vector< AddBase > &addbase)
 Gather result Varnodes for all sums that the given starting Varnode is involved in. More...
 
static uintb gatherOffset (Varnode *vn)
 If the given Varnode is a sum result, return the constant portion of this sum. More...
 

Private Member Functions

void deriveBoundaries (const FuncProto &proto)
 Set up basic boundaries for the stack layout. More...
 
void gatherInternal (void) const
 Run through Varnodes looking for pointers into the stack. More...
 

Private Attributes

const Funcdatafd
 Function being searched for aliases.
 
AddrSpacespace
 AddressSpace in which to search.
 
vector< AddBaseaddBase
 Collection of pointers into the AddressSpace.
 
vector< uintb > alias
 List of aliased addresses (as offsets)
 
bool calculated
 Have aliases been calculated.
 
uintb localExtreme
 Largest possible offset for a local variable.
 
uintb localBoundary
 Boundary offset separating locals and parameters.
 
uintb aliasBoundary
 Shallowest alias.
 
int4 direction
 1=stack grows negative, -1=positive
 

Detailed Description

A light-weight class for analyzing pointers and aliasing on the stack.

The gather() method looks for pointer references into a specific AddressSpace (usually the stack). Then hasLocalAlias() checks if a specific Varnode within the AddressSpace is (possibly) aliased by one of the gathered pointer references.

Member Function Documentation

◆ deriveBoundaries()

void ghidra::AliasChecker::deriveBoundaries ( const FuncProto proto)
private

Set up basic boundaries for the stack layout.

Set up basic offset boundaries for what constitutes a local variable or a parameter on the stack. This can be informed by the ProtoModel if available.

Parameters
protois the function prototype to use as a prototype model

References ghidra::Range::getLast(), ghidra::FuncProto::getLocalRange(), ghidra::FuncProto::getParamRange(), and ghidra::FuncProto::hasModel().

◆ gather()

void ghidra::AliasChecker::gather ( const Funcdata f,
AddrSpace spc,
bool  defer 
)

Gather Varnodes that point on the stack.

For the given function and address space, gather all Varnodes that are pointers into the address space. The actual calculation can be deferred until the first time hasLocalAlias() is called.

Parameters
fis the given function
spcis the given address space
deferis true is gathering is deferred

References ghidra::Scope::fd, ghidra::Funcdata::getFuncProto(), ghidra::ScopeLocal::space, and ghidra::AddrSpace::stackGrowsNegative().

Referenced by ghidra::ActionActiveParam::apply(), and ghidra::MapState::gatherOpen().

◆ gatherAdditiveBase()

void ghidra::AliasChecker::gatherAdditiveBase ( Varnode startvn,
vector< AddBase > &  addbase 
)
static

Gather result Varnodes for all sums that the given starting Varnode is involved in.

For every sum that involves startvn, collect the final result Varnode of the sum. A sum is any expression involving only the additive operators INT_ADD, INT_SUB, PTRADD, PTRSUB, and SEGMENTOP. The routine traverses forward recursively through all descendants of vn that are additive operations and collects all the roots of the traversed trees.

Parameters
startvnis the Varnode to trace
addbasewill contain all the collected roots

References ghidra::Varnode::beginDescend(), ghidra::PcodeOp::code(), ghidra::CPUI_COPY, ghidra::CPUI_INT_ADD, ghidra::CPUI_INT_SUB, ghidra::CPUI_PTRADD, ghidra::CPUI_PTRSUB, ghidra::CPUI_SEGMENTOP, ghidra::Varnode::endDescend(), ghidra::PcodeOp::getIn(), ghidra::PcodeOp::getOut(), ghidra::Varnode::isConstant(), ghidra::Varnode::isMark(), and ghidra::Varnode::setMark().

◆ gatherInternal()

void ghidra::AliasChecker::gatherInternal ( void  ) const
private

Run through Varnodes looking for pointers into the stack.

If there is an AddrSpace (stack) pointer, find its input Varnode, and look for additive uses of it. Once all these Varnodes are accumulated, calculate specific offsets that start a region being aliased.

References ghidra::AddrSpace::addressToByte(), ghidra::Scope::fd, ghidra::Funcdata::findSpacebaseInput(), ghidra::AddrSpace::getWordSize(), and ghidra::ScopeLocal::space.

◆ gatherOffset()

uintb ghidra::AliasChecker::gatherOffset ( Varnode vn)
static

If the given Varnode is a sum result, return the constant portion of this sum.

Treat vn as the result of a series of ADD operations. Examine all the constant terms of this sum and add them together by traversing the syntax tree rooted at vn, backwards, only through additive operations.

Parameters
vnis the given Varnode to gather off of
Returns
the resulting sub-sum

References ghidra::calc_mask(), ghidra::PcodeOp::code(), ghidra::CPUI_COPY, ghidra::CPUI_INT_ADD, ghidra::CPUI_INT_SUB, ghidra::CPUI_PTRADD, ghidra::CPUI_PTRSUB, ghidra::CPUI_SEGMENTOP, ghidra::Varnode::getDef(), ghidra::PcodeOp::getIn(), ghidra::Varnode::getOffset(), ghidra::Varnode::getSize(), and ghidra::Varnode::isConstant().

◆ hasLocalAlias()

bool ghidra::AliasChecker::hasLocalAlias ( Varnode vn) const

Return true if it looks like the given Varnode is aliased by a pointer.

This is gives a rough analysis of whether the given Varnode might be aliased by another pointer in the function. If false is returned, the Varnode is not likely to have an alias. If true is returned, the Varnode might have an alias.

Parameters
vnis the given Varnode
Returns
true if the Varnode might have a pointer alias

References ghidra::Varnode::getOffset(), ghidra::Varnode::getSpace(), and ghidra::ScopeLocal::space.

Referenced by ghidra::FuncCallSpecs::checkInputTrialUse().


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