Class AbstractVarnodeEvaluator<T>
- Type Parameters:
T- the type of values resulting from evaluation
- All Implemented Interfaces:
VarnodeEvaluator<T>
- Direct Known Subclasses:
ArithmeticVarnodeEvaluator
VarnodeEvaluator
Unlike PcodeExecutor this abstract class is not explicitly bound to a p-code state nor
arithmetic. Instead it defines abstract methods for accessing "leaf" varnodes and evaluating ops.
To evaluate a varnode, it first checks if the varnode is a leaf, which is defined by an extension
class. If it is, it converts the static address to a dynamic one and invokes the appropriate
value getter. An extension class would likely implement those getters using a
PcodeExecutorState. If the varnode is not a leaf, the evaluator will ascend by examining
its defining p-code op, evaluate its input varnodes recursively and then compute the output using
the provided p-code arithmetic. This implementation maintains a map of evaluated varnodes and
their values so that any intermediate varnode is evaluated just once. Note that the evaluation
algorithm assumes their are no cycles in the AST, which should be the case by definition.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract AddressapplyBase(long offset) Resolve a (static) stack offset to its physical (dynamic) address in the frameprotected TThe method invoked when an unrecognized or unsupported operator is encounteredprotected abstract TConcatenate the given valuesprotected TEvaluate a varnode, which could be either a leaf or a branchprotected abstract TevaluateAbstract(Program program, AddressSpace space, T offset, int size, Map<Varnode, T> already) Evaluate a variable whose offset is of typeAbstractVarnodeEvaluatorprotected abstract TevaluateBinaryOp(Program program, PcodeOp op, BinaryOpBehavior binOp, Map<Varnode, T> already) Evaluate a binary opprotected TEvaluate the given varnode's defining p-code opprotected abstract TevaluateConstant(long value, int size) Evaluate a constantprotected TevaluateLeaf(Program program, Varnode vn) Evaluate a leaf varnodeprotected abstract TEvaluate aPcodeOp.LOADopprotected abstract TevaluateMemory(Address address, int size) Evaluate a variable in memoryevaluateOp(Program program, PcodeOp op) Evaluate a high p-code opprotected TLikeevaluateOp(Program, PcodeOp), but uses a cacheprotected abstract TEvaluate aPcodeOp.PTRADDopprotected abstract TEvaluate aPcodeOp.PTRSUBopprotected TevaluateRegister(Address address, int size) Evaluate the given register variableprotected TevaluateStack(long offset, int size) Evaluate the given stack variableprotected TevaluateStorage(Program program, VariableStorage storage, T identity) Evaluate variable storage, providing an "identity" valueprotected abstract TevaluateUnaryOp(Program program, PcodeOp op, UnaryOpBehavior unOp, Map<Varnode, T> already) Evaluate a unary opprotected TevaluateUnique(long offset, int size) Evaluate a unique variableevaluateVarnode(Program program, Varnode vn) Evaluate a varnodeprotected TEvaluate a varnode, which could be either a leaf or a branch, taking its cached value if availableprotected intgetIntConst(Varnode vn) Assert that a varnode is constant and get its value as an integer.protected abstract booleanCheck if the given varnode is a leaf in the evaluationprotected AddresstranslateMemory(Program program, Address address) Map the given static address to dynamicMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ghidra.pcode.eval.VarnodeEvaluator
evaluateStorage
-
Constructor Details
-
AbstractVarnodeEvaluator
public AbstractVarnodeEvaluator()
-
-
Method Details
-
catenate
Concatenate the given values- Parameters:
sizeTotal- the expected output size in bytesupper- the value of the left (more significant) piecelower- the value of the right (less significant) piecesizeLower- the size of the lower piece- Returns:
- the result of concatenation
-
isLeaf
Check if the given varnode is a leaf in the evaluationThis allows the extension class to determine the base case when recursively ascending the AST.
- Parameters:
vn- the varnode- Returns:
- true to treat the varnode as a base case, or false to ascend to its defining p-code op
-
applyBase
Resolve a (static) stack offset to its physical (dynamic) address in the frameWhen a leaf varnode is a stack address, this is used to map it to a physical address before invoking
evaluateMemory(Address, int).- Parameters:
offset- the offset- Returns:
- the address in target memory
-
translateMemory
Map the given static address to dynamicWhen a leaf varnode is a memory address, this is used to map it to a dynamic address before invoking
evaluateMemory(Address, int). This is needed in case the module has been relocated in the dynamic context. Note this is not used to translate register or stack addresses, since those are abstract concepts. Stack addresses are translated usingapplyBase(long), the result of which should already be a dynamic address.- Parameters:
program- the program specifying the static contextaddress- the address in the static context- Returns:
- the address in the dynamic context
-
evaluateLeaf
Evaluate a leaf varnodeThis method translates the varnode accordingly and delegates the evaluation, indirectly, to
evaluateMemory(Address, int). Notable exceptions are constants, which are just evaluated to their immediate value, and unique variables, which cannot ordinarily be leaves.- Parameters:
program- the program defining the static contextvn- the varnode- Returns:
- the value obtained from the dynamic context
-
doEvaluateVarnode
Evaluate a varnode, which could be either a leaf or a branchThis method is invoked by
evaluateVarnode(Program, Varnode, Map)when the value has not already been computed. Only that method should invoke this one directly.- Parameters:
program- the program defining the static contextvn- the varnodealready- a cache of already-evaluated varnodes and their values- Returns:
- the value
-
evaluateVarnode
Evaluate a varnode, which could be either a leaf or a branch, taking its cached value if available- Parameters:
program- the program defining the static contextvn- the varnodealready- a cache of already-evaluated varnodes and their values- Returns:
- the value
-
evaluateVarnode
Evaluate a varnode- Specified by:
evaluateVarnodein interfaceVarnodeEvaluator<T>- Parameters:
program- the program containing the varnodevn- the varnode to evaluate- Returns:
- the value of the varnode
-
evaluateStorage
Evaluate variable storage, providing an "identity" value- Parameters:
program- the program containing the variable storagestorage- the storage to evaluateidentity- the value if storage had no varnodes- Returns:
- the value of the storage
-
evaluateBranch
Evaluate the given varnode's defining p-code op- Parameters:
program- the program defining the static contextvn- the varnodealready- a cache of already-evaluated varnodes and their values- Returns:
- the value
-
evaluateConstant
Evaluate a constant- Parameters:
value- the constant valuesize- the size of the value in bytes- Returns:
- the value as a
AbstractVarnodeEvaluator
-
evaluateRegister
Evaluate the given register variable- Parameters:
address- the address of the registersize- the size of the variable in bytes- Returns:
- the value
-
evaluateStack
Evaluate the given stack variable- Parameters:
offset- the stack offset of the variablesize- the size of the variable in bytes- Returns:
- the value
-
evaluateMemory
Evaluate a variable in memoryBy default all register, stack, and memory addresses are directed here. Register addresses are passed through without modification. Stack addresses will have the frame base applied via
applyBase(long), and memory addresses will be mapped throughtranslateMemory(Program, Address).- Parameters:
address- the address of the variablesize- the size of the variable in bytes- Returns:
- the value
-
evaluateUnique
Evaluate a unique variableThis is only invoked when trying to evaluate a leaf, which should never occur for a unique variable. Thus, by default, this throws a
PcodeExecutionException.- Parameters:
offset- the offset of the variablesize- the size of the variable in bytes- Returns:
- the value
-
evaluateAbstract
protected abstract T evaluateAbstract(Program program, AddressSpace space, T offset, int size, Map<Varnode, T> already) Evaluate a variable whose offset is of typeAbstractVarnodeEvaluatorThe three parameters
space,offset, andsizeimitate the varnode triple, except that the offset is abstract. This is typically invoked for aPcodeOp.LOAD, i.e., a dereference.- Parameters:
program- the program defining the static contextspace- the address space of the variableoffset- the offset of the variablesize- the size of the variable in bytesalready- a cache of already-evaluated varnodes and their values- Returns:
- the value
-
evaluateUnaryOp
protected abstract T evaluateUnaryOp(Program program, PcodeOp op, UnaryOpBehavior unOp, Map<Varnode, T> already) Evaluate a unary opThis evaluates the input varnode then computes the output value.
- Parameters:
program- the program defining the static contextop- the op whose output to evaluateunOp- the concrete behavior of the opalready- a cache of already-evaluated varnodes and their values- Returns:
- the output value
-
evaluateBinaryOp
protected abstract T evaluateBinaryOp(Program program, PcodeOp op, BinaryOpBehavior binOp, Map<Varnode, T> already) Evaluate a binary opThis evaluates the input varnodes then computes the output value.
- Parameters:
program- the program defining the static contextop- the op whose output to evaluatebinOp- the concrete behavior of the opalready- a cache of already-evaluated varnodes and their values- Returns:
- the output value
-
evaluatePtrAdd
Evaluate aPcodeOp.PTRADDop- Parameters:
program- the program defining the static contextop- the op whose output to evaluatealready- a cache of already-evaluated varnodes and their values- Returns:
- the output value
-
evaluatePtrSub
Evaluate aPcodeOp.PTRSUBop- Parameters:
program- the program defining the static contextop- the op whose output to evaluatealready- a cache of already-evaluated varnodes and their values- Returns:
- the output value
-
getIntConst
Assert that a varnode is constant and get its value as an integer.Here "constant" means a literal or immediate value. It does not read from the state.
- Parameters:
vn- the varnode- Returns:
- the value
-
evaluateLoad
Evaluate aPcodeOp.LOADop- Parameters:
program- the program defining the static contextop- the op whose output to evaluatealready- a cache of already-evaluated varnodes and their values- Returns:
- the output value
-
evaluateOp
Description copied from interface:VarnodeEvaluatorEvaluate a high p-code op- Specified by:
evaluateOpin interfaceVarnodeEvaluator<T>- Parameters:
program- the program containing the opop- the p-code op- Returns:
- the value of the op's output
-
evaluateOp
LikeevaluateOp(Program, PcodeOp), but uses a cache- Parameters:
program- the program defining the static contextop- the op whose output to evaluatealready- a cache of already-evaluated varnodes and their values- Returns:
- the output value
-
badOp
The method invoked when an unrecognized or unsupported operator is encountered- Parameters:
op- the op- Returns:
- the value, but this usually throws an exception
-