decompiler  1.0.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
ghidra::ContextDatabase Class Referenceabstract

An interface to a database of disassembly/decompiler context information. More...

#include <globalcontext.hh>

Inheritance diagram for ghidra::ContextDatabase:
ghidra::ContextGhidra ghidra::ContextInternal

Public Member Functions

virtual ~ContextDatabase ()
 Destructor.
 
virtual int4 getContextSize (void) const =0
 Retrieve the number of words (uintm) in a context blob. More...
 
virtual void registerVariable (const string &nm, int4 sbit, int4 ebit)=0
 Register a new named context variable (as a bit range) with the database. More...
 
virtual const uintm * getContext (const Address &addr) const =0
 Get the context blob of values associated with a given address. More...
 
virtual const uintm * getContext (const Address &addr, uintb &first, uintb &last) const =0
 Get the context blob of values associated with a given address and its bounding offsets. More...
 
virtual TrackedSetgetTrackedDefault (void)=0
 Get the set of default values for all tracked registers. More...
 
virtual const TrackedSetgetTrackedSet (const Address &addr) const =0
 Get the set of tracked register values associated with the given address. More...
 
virtual TrackedSetcreateSet (const Address &addr1, const Address &addr2)=0
 Create a tracked register set that is valid over the given range. More...
 
virtual void encode (Encoder &encoder) const =0
 Encode the entire database to a stream. More...
 
virtual void decode (Decoder &decoder)=0
 Restore the state of this database object from the given stream decoder. More...
 
virtual void decodeFromSpec (Decoder &decoder)=0
 Add initial context state from elements in the compiler/processor specifications. More...
 
void setVariableDefault (const string &nm, uintm val)
 Provide a default value for a context variable. More...
 
uintm getDefaultValue (const string &nm) const
 Retrieve the default value for a context variable. More...
 
void setVariable (const string &nm, const Address &addr, uintm value)
 Set a context value at the given address. More...
 
uintm getVariable (const string &nm, const Address &addr) const
 Retrieve a context value at the given address. More...
 
void setContextChangePoint (const Address &addr, int4 num, uintm mask, uintm value)
 Set a specific context value starting at the given address. More...
 
void setContextRegion (const Address &addr1, const Address &addr2, int4 num, uintm mask, uintm value)
 Set a context variable value over a given range of addresses. More...
 
void setVariableRegion (const string &nm, const Address &begad, const Address &endad, uintm value)
 Set a context variable by name over a given range of addresses. More...
 
uintb getTrackedValue (const VarnodeData &mem, const Address &point) const
 Get the value of a tracked register at a specific address. More...
 

Protected Member Functions

virtual ContextBitRangegetVariable (const string &nm)=0
 Retrieve the context variable description object by name. More...
 
virtual const ContextBitRangegetVariable (const string &nm) const =0
 Retrieve the context variable description object by name. More...
 
virtual void getRegionForSet (vector< uintm *> &res, const Address &addr1, const Address &addr2, int4 num, uintm mask)=0
 Grab the context blob(s) for the given address range, marking bits that will be set. More...
 
virtual void getRegionToChangePoint (vector< uintm *> &res, const Address &addr, int4 num, uintm mask)=0
 Grab the context blob(s) starting at the given address up to the first point of change. More...
 
virtual uintm * getDefaultValue (void)=0
 Retrieve the memory region holding all default context values. More...
 
virtual const uintm * getDefaultValue (void) const =0
 Retrieve the memory region holding all default context values. More...
 

Static Protected Member Functions

static void encodeTracked (Encoder &encoder, const Address &addr, const TrackedSet &vec)
 Encode all tracked register values for a specific address to a stream. More...
 
static void decodeTracked (Decoder &decoder, TrackedSet &vec)
 Restore a sequence of tracked register values from the given stream decoder. More...
 

Detailed Description

An interface to a database of disassembly/decompiler context information.

Context information is a set of named variables that hold concrete values at specific addresses in the target executable being analyzed. A variable can hold different values at different addresses, but a specific value at a specific address never changes. Analysis recovers these values over time, populating this database, and querying this database lets analysis provides concrete values for memory locations in context.

Context variables come in two flavors:

Low-level context variables can be queried and set by name – getVariable(), setVariable(), setVariableRegion() – but the disassembler accesses all the variables at an address as a group via getContext(), setContextChangePoint(), setContextRegion(). In this setting, all the values are packed together in an array of words, a context blob (See ContextBitRange).

Tracked variables are also queried as a group via getTrackedSet() and createSet(). These return a list of TrackedContext objects.

Member Function Documentation

◆ createSet()

virtual TrackedSet& ghidra::ContextDatabase::createSet ( const Address addr1,
const Address addr2 
)
pure virtual

Create a tracked register set that is valid over the given range.

This really should be an internal routine. The created set is empty, old values are blown away. If old/default values are to be preserved, they must be copied back in.

Parameters
addr1is the starting address of the given range
addr2is (1 past) the ending address of the given range
Returns
the empty set of tracked register values

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ decode()

virtual void ghidra::ContextDatabase::decode ( Decoder decoder)
pure virtual

Restore the state of this database object from the given stream decoder.

Parameters
decoderis the given stream decoder

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ decodeFromSpec()

virtual void ghidra::ContextDatabase::decodeFromSpec ( Decoder decoder)
pure virtual

Add initial context state from elements in the compiler/processor specifications.

Parse a <context_data> element from the given stream decoder from either the compiler or processor specification file for the architecture, initializing this database.

Parameters
decoderis the given stream decoder

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ decodeTracked()

void ghidra::ContextDatabase::decodeTracked ( Decoder decoder,
TrackedSet vec 
)
staticprotected

Restore a sequence of tracked register values from the given stream decoder.

Parse a <tracked_pointset> element, decoding each child in turn to populate a list of TrackedContext objects.

Parameters
decoderis the given stream decoder
vecis the container that will hold the new TrackedContext objects

References ghidra::Decoder::peekElement().

Referenced by ghidra::ContextGhidra::getTrackedSet().

◆ encode()

virtual void ghidra::ContextDatabase::encode ( Encoder encoder) const
pure virtual

Encode the entire database to a stream.

Parameters
encoderis the stream encoder

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ encodeTracked()

void ghidra::ContextDatabase::encodeTracked ( Encoder encoder,
const Address addr,
const TrackedSet vec 
)
staticprotected

Encode all tracked register values for a specific address to a stream.

Encode all the tracked register values associated with a specific target address as a <tracked_pointset> tag.

Parameters
encoderis the stream encoder
addris the specific address we have tracked values for
vecis the list of tracked values

References ghidra::Encoder::closeElement(), ghidra::AddrSpace::encodeAttributes(), ghidra::Address::getOffset(), ghidra::Address::getSpace(), and ghidra::Encoder::openElement().

◆ getContext() [1/2]

virtual const uintm* ghidra::ContextDatabase::getContext ( const Address addr) const
pure virtual

Get the context blob of values associated with a given address.

Parameters
addris the given address
Returns
the memory region holding the context values for the address

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getContext() [2/2]

virtual const uintm* ghidra::ContextDatabase::getContext ( const Address addr,
uintb &  first,
uintb &  last 
) const
pure virtual

Get the context blob of values associated with a given address and its bounding offsets.

In addition to the memory region, the range of addresses for which the region is valid is passed back as offsets into the address space.

Parameters
addris the given address
firstwill hold the starting offset of the valid range
lastwill hold the ending offset of the valid range
Returns
the memory region holding the context values for the address

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getContextSize()

virtual int4 ghidra::ContextDatabase::getContextSize ( void  ) const
pure virtual

Retrieve the number of words (uintm) in a context blob.

Returns
the number of words

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getDefaultValue() [1/3]

virtual uintm* ghidra::ContextDatabase::getDefaultValue ( void  )
protectedpure virtual

Retrieve the memory region holding all default context values.

This fetches the active memory holding the default context values on top of which all other context values are overlaid.

Returns
the memory region holding all the default context values

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getDefaultValue() [2/3]

virtual const uintm* ghidra::ContextDatabase::getDefaultValue ( void  ) const
protectedpure virtual

Retrieve the memory region holding all default context values.

This fetches the active memory holding the default context values on top of which all other context values are overlaid.

Returns
the memory region holding all the default context values

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getDefaultValue() [3/3]

uintm ghidra::ContextDatabase::getDefaultValue ( const string &  nm) const

Retrieve the default value for a context variable.

This will return the default value used for addresses that have not been overlaid with other values.

Parameters
nmis the name of the context variable
Returns
the variable's default value

References ghidra::ContextBitRange::getValue().

◆ getRegionForSet()

virtual void ghidra::ContextDatabase::getRegionForSet ( vector< uintm *> &  res,
const Address addr1,
const Address addr2,
int4  num,
uintm  mask 
)
protectedpure virtual

Grab the context blob(s) for the given address range, marking bits that will be set.

This is an internal routine for obtaining the actual memory regions holding context values for the address range. This also informs the system which bits are getting set. A split is forced at the first address, and at least one memory region is passed back. The second address can be invalid in which case the memory region passed back is valid from the first address to whatever the next split point is.

Parameters
reswill hold pointers to memory regions for the given range
addr1is the starting address of the range
addr2is (1 past) the last address of the range or is invalid
numis the word index for the context value that will be set
maskis a mask of the value being set (within its word)

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getRegionToChangePoint()

virtual void ghidra::ContextDatabase::getRegionToChangePoint ( vector< uintm *> &  res,
const Address addr,
int4  num,
uintm  mask 
)
protectedpure virtual

Grab the context blob(s) starting at the given address up to the first point of change.

This is an internal routine for obtaining the actual memory regions holding context values starting at the given address. A specific context value is specified, and all memory regions are returned up to the first address where that particular context value changes.

Parameters
reswill hold pointers to memory regions being passed back
addris the starting address of the regions to fetch
numis the word index for the specific context value being set
maskis a mask of the context value being set (within its word)

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getTrackedDefault()

virtual TrackedSet& ghidra::ContextDatabase::getTrackedDefault ( void  )
pure virtual

Get the set of default values for all tracked registers.

Returns
the list of TrackedContext objects

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getTrackedSet()

virtual const TrackedSet& ghidra::ContextDatabase::getTrackedSet ( const Address addr) const
pure virtual

Get the set of tracked register values associated with the given address.

Parameters
addris the given address
Returns
the list of TrackedContext objects

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

Referenced by ghidra::ActionConstbase::apply().

◆ getTrackedValue()

uintb ghidra::ContextDatabase::getTrackedValue ( const VarnodeData mem,
const Address point 
) const

Get the value of a tracked register at a specific address.

A specific storage region and code address is given. If the region is tracked the value at the address is retrieved. If the specified storage region is contained in the tracked region, the retrieved value is trimmed to match the containment before returning it. If the region is not tracked, a value of 0 is returned.

Parameters
memis the specified storage region
pointis the code address
Returns
the tracked value or zero

References ghidra::calc_mask(), ghidra::AddrSpace::isBigEndian(), ghidra::TrackedContext::loc, ghidra::VarnodeData::offset, ghidra::VarnodeData::size, ghidra::VarnodeData::space, and ghidra::TrackedContext::val.

◆ getVariable() [1/3]

virtual ContextBitRange& ghidra::ContextDatabase::getVariable ( const string &  nm)
protectedpure virtual

Retrieve the context variable description object by name.

If the variable doesn't exist an exception is thrown.

Parameters
nmis the name of the context value
Returns
the ContextBitRange object matching the name

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getVariable() [2/3]

virtual const ContextBitRange& ghidra::ContextDatabase::getVariable ( const string &  nm) const
protectedpure virtual

Retrieve the context variable description object by name.

If the variable doesn't exist an exception is thrown.

Parameters
nmis the name of the context value
Returns
the ContextBitRange object matching the name

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

◆ getVariable() [3/3]

uintm ghidra::ContextDatabase::getVariable ( const string &  nm,
const Address addr 
) const

Retrieve a context value at the given address.

If a value has not been explicit set for an address range containing the given address, the default value for the variable is returned

Parameters
nmis the name of the context variable
addris the address for which the specific value is needed
Returns
the context variable value for the address

References ghidra::ContextBitRange::getValue().

◆ registerVariable()

virtual void ghidra::ContextDatabase::registerVariable ( const string &  nm,
int4  sbit,
int4  ebit 
)
pure virtual

Register a new named context variable (as a bit range) with the database.

A new variable is registered by providing a name and the range of bits the value will occupy within the context blob. The full blob size is automatically increased if necessary. The variable must be contained within a single word, and all variables must be registered before any values can be set.

Parameters
nmis the name of the new variable
sbitis the position of the variable's most significant bit within the blob
ebitis the position of the variable's least significant bit within the blob

Implemented in ghidra::ContextInternal, and ghidra::ContextGhidra.

Referenced by ghidra::Sleigh::registerContext().

◆ setContextChangePoint()

void ghidra::ContextDatabase::setContextChangePoint ( const Address addr,
int4  num,
uintm  mask,
uintm  value 
)

Set a specific context value starting at the given address.

The new value is painted across an address range starting, starting with the given address up to the point where another change for the variable was specified. No other context variable is changed, inside (or outside) the range.

Parameters
addris the given starting address
numis the index of the word (within the context blob) of the context variable
maskis the mask delimiting the context variable (within its word)
valueis the (already shifted) value being set

◆ setContextRegion()

void ghidra::ContextDatabase::setContextRegion ( const Address addr1,
const Address addr2,
int4  num,
uintm  mask,
uintm  value 
)

Set a context variable value over a given range of addresses.

The new value is painted over an explicit range of addresses. No other context variable is changed inside (or outside) the range.

Parameters
addr1is the starting address of the given range
addr2is the ending address of the given range
numis the index of the word (within the context blob) of the context variable
maskis the mask delimiting the context variable (within its word)
valueis the (already shifted) value being set

◆ setVariable()

void ghidra::ContextDatabase::setVariable ( const string &  nm,
const Address addr,
uintm  value 
)

Set a context value at the given address.

The variable will be changed to the new value, starting at the given address up to the next point of change.

Parameters
nmis the name of the context variable
addris the given address
valueis the new value to set

References ghidra::ContextBitRange::getMask(), ghidra::ContextBitRange::getShift(), ghidra::ContextBitRange::getWord(), ghidra::ContextBitRange::mask, and ghidra::ContextBitRange::setValue().

◆ setVariableDefault()

void ghidra::ContextDatabase::setVariableDefault ( const string &  nm,
uintm  val 
)

Provide a default value for a context variable.

The default value is returned for addresses that have not been overlaid with other values.

Parameters
nmis the name of the context variable
valis the default value to establish

References ghidra::ContextBitRange::setValue().

Referenced by ghidra::Sleigh::setContextDefault().

◆ setVariableRegion()

void ghidra::ContextDatabase::setVariableRegion ( const string &  nm,
const Address begad,
const Address endad,
uintm  value 
)

Set a context variable by name over a given range of addresses.

The new value is painted over an explicit range of addresses. No other context variable is changed inside (or outside) the range.

Parameters
nmis the name of the context variable to set
begadis the starting address of the given range
endadis the ending address of the given range
valueis the new value to set

References ghidra::ContextBitRange::getMask(), ghidra::ContextBitRange::getShift(), ghidra::ContextBitRange::getWord(), and ghidra::ContextBitRange::setValue().


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