Class JitCodeGenerator<THIS extends JitCompiledPassage>
- Type Parameters:
THIS- the type of the generated passage
This implements the Code Generation phase of the JitCompiler. With all the prior
analysis, code generation is just a careful process of visiting all of the ops, variables, and
analytic results to ensure everything is incorporated and accounted for.
The Target Classfile
The target is a classfile that implements JitCompiledPassage. As such, it must implement
all of the specified methods in that interface as well as a constructor having a specific
signature. That signature takes a
JitPcodeThread and, being a constructor, returns void. We will also need to
generate a static initializer to populate some metadata and pre-fetch any static things, e.g.,
the SleighLanguage for the emulation target. The fields are:
staticStringLANGUAGE_ID- The language ID (as inLanguageIDof the emulation targetstaticLanguageLANGUAGE- The language (ISA) of the emulation targetstaticAddressFactoryADDRESS_FACTORY- The address factory of the languagestaticList<JitPassage.AddrCtx> ENTRIES- The list of entry pointsJitPcodeThreadthread- The bound thread for this instance of the compiled passageJitBytesPcodeExecutorStatestate- The run-time machine state for this thread of emulation
Static Initializer
In the Java language, statements in a class's static block, as well as the initial values
of static fields are implemented by the classfile's <clinit> method. We use it to
pre-construct contextreg values and varnode refs for use in birthing and
retirement. They are kept in static fields. We also initialize the static ENTRIES field,
which is public (via reflection) and describes each entry point generated. It has the type
List<JitPassage.AddrCtx>. A call to JitCompiledPassage.run(int) should pass
in the position of the desired entry point in the ENTRIES list.
Constructor
In the Java language, statements in a class's constructor, as well as the initial values of
instance fields are implemented by the classfile's <init> methods. We provide a single
constructor that accepts a JitPcodeThread. Upon construction, the generated
JitCompiledPassage is "bound" to the given thread. The constructor pre-fetches parts of
the thread's state and userop definitions, and it allocates JitCompiledPassage.ExitSlots. Each of these are kept in instance
fields.
thread() Method
This method implements JitCompiledPassage.thread(), a simple getter for the
thread field.
run() Method
This method implements JitCompiledPassage.run(int), the actual semantics of the
translated machine instructions selected for the passage. It accepts a single parameter, which is
the position in the ENTRIES list of the desired entry point blockId. The
structure is as follows:
- Entry point dispatch - a large
switchstatement on the entryblockId - P-code translation - the block-by-block op-by-op translation of the p-code to bytecode
- Exception handlers - exception handlers as requested by various elements of the p-code translation
- Parameter declarations -
thisandblockId - Allocated local declarations - declares all locals allocated by
JitAllocationModel
Entry Point Dispatch
This part of the run method dispatches execution to the correct entry point within the translated passage. It consists of these sub-parts:
- Switch table - a
tableswitchto jump to the code for the scope transition into the entry block given byblockId - Scope transitions - for each block, birth its live varnodes then jump to the block's translation
- Default case - throws an
IllegalArgumentExceptionfor an invalidblockId
This first ensure that a valid entry point was given in blockId. If not, we jump to the
default case which throws an exception. Otherwise, we jump to the appropriate entry transition.
Every block flow edge is subject to a scope transition wherein varnodes that leave scope must be
retired and varnodes that enter scope must be birthed. We generate an entry transition for each
possible entry block. That transition births all the varnodes that are in scope for that entry
block then jumps to the entry block's p-code translation.
P-code Translation
Here, most of the generation is performed via delegation to an object model, based on the use-def
graph. We first iterate over the blocks, in the same order as they appear in the decoded passage.
This will ensure that fall-through control transitions in the p-code map to fall-through
transitions in the emitted bytecode. If the block is the target of a bytecode jump, i.e., it's an
entry block or the target of a p-code branch, then we emit a label at the start of the block. We
then iterate over each p-code op in the block delegating each to the appropriate generator. We
emit "line number" information for each op to help debug crashes. A generator may register an
exception handler to be emitted later in the "exception handlers" part of the run method.
If the block has fall through, we emit the appropriate scope transition before proceeding to the
next block. Note that scope transitions for branch ops are emitted by the generators for those
ops.
For details about individual p-code op translations, see OpGen. For details about
individual SSA value (constant and variable) translations, see VarGen. For details about
emitting scope transitions, see VarGen.BlockTransition.
- Implementation Notes:
- Throughout most of the code that emits bytecode, there are (human-generated) comments to track the contents of the JVM stack. Items pushed onto the stack appear at the right. If type is important, then those are denoted using :TYPE after the relevant variable. TODO: It'd be nice to have a bytecode API that enforces stack structure using the compiler (somehow), but that's probably overkill. Also, I have yet to see what the official classfile API will bring.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classFor testing and debugging: A means to inject granular line number informationstatic interfaceA mechanism to emit bytecode that loads a program counterstatic enumThe manners in which the program counter and decode context can be "retired." -
Constructor Summary
ConstructorsConstructorDescriptionJitCodeGenerator(MethodHandles.Lookup lookup, JitAnalysisContext context, JitControlFlowModel cfm, JitDataFlowModel dfm, JitVarScopeModel vsm, JitTypeModel tm, JitAllocationModel am, JitOpUseModel oum) Construct a code generator for the given passage's target classfile -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]dumpBytecode(byte[] bytes) For diagnostics: Dump the generated classfile to an actual file on diskprotected <N extends Emitter.Next>
Emitter<Emitter.Ent<N, Types.TRef<Address>>> genAddress(Emitter<N> em, Address address) Emit code to load anAddressonto the JVM stackprotected ghidra.pcode.emu.jit.gen.JitCodeGenerator.GenBlockResultgenBlock(OpGen.OpResult prev, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localCtxmod, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq, JitControlFlowModel.JitBlock block, int opIdx) Emit the bytecode translation for the given p-code blockprotected ghidra.pcode.emu.jit.gen.JitCodeGenerator.GenBlockResultgenBlockOps(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localCtxmod, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq, JitControlFlowModel.JitBlock block, int opIdx) Emit the bytecode translation for the ops in the given p-code blockprotected Emitter<Emitter.Bot> Emit bytecode for the static initializerprotected byte[]generate()Generate the classfile and get the raw bytes<N extends Emitter.Next>
Emitter<N> genExit(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitControlFlowModel.JitBlock block, JitCodeGenerator.PcGen pcGen, RegisterValue ctx) Emit code to exit the passageprotected Emitter<Emitter.Bot> genInitMethod(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis) Emit all the bytecode for the constructorprotected <N extends Emitter.Next>
Emitter<Emitter.Ent<N, Types.TRef<JitBytesPcodeExecutorStatePiece.JitBytesPcodeExecutorStateSpace>>> genLoadJitStateSpace(Emitter<N> em, Local<Types.TRef<THIS>> localThis, AddressSpace space) Emit bytecode to load the givenJitBytesPcodeExecutorStatePiece.JitBytesPcodeExecutorStateSpaceonto the JVM stackprotected OpGen.OpResultgenOp(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localCtxmod, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq, PcodeOp op, JitControlFlowModel.JitBlock block, int opIdx) Emit the bytecode translation for a given p-code op<N extends Emitter.Next>
Emitter<Emitter.Ent<N, Types.TInt>> genReadLegToStack(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JitType.MpIntJitType type, int leg, Opnd.Ext ext) Emit bytecode to load one leg of a multi-precision value from the varnode onto the JVM stack.<N extends Emitter.Next>
Emitter<Emitter.Ent<N, Types.TRef<int[]>>> genReadToArray(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JitType.MpIntJitType type, Opnd.Ext ext, Scope scope, int slack) Emit bytecode to load the varnode's value into an integer array in little-endian order, pushing its ref onto the JVM stack.<N extends Emitter.Next>
Emitter<Emitter.Ent<N, Types.TInt>> genReadToBool(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v) Emit bytecode to load the varnode's value, interpreted as a boolean, as an integer onto the JVM stack.<N extends Emitter.Next>
Opnd.OpndEm<JitType.MpIntJitType, N> genReadToOpnd(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JitType.MpIntJitType type, Opnd.Ext ext, Scope scope) Emit bytecode to read the given value into a series of locals<T extends Types.BPrim<?>,JT extends JitType.SimpleJitType<T, JT>, N extends Emitter.Next>
Emitter<Emitter.Ent<N, T>> genReadToStack(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JT type, Opnd.Ext ext) Emit bytecode to read the given value onto the JVM stack.<N extends Emitter.Next>
Emitter<N> genRetirePcCtx(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitCodeGenerator.PcGen pcGen, RegisterValue ctx, JitCodeGenerator.RetireMode mode) Emit bytecode to set the emulator's counter and contextreg.protected Emitter<Emitter.Dead> genRunMethod(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localBlockId, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq) Emit all the bytecode for therunmethod.protected Emitter<Emitter.Bot> Emit code into the static initializer to initialize theENTRIESfield.protected Emitter<Emitter.Bot> genStaticEntry(Emitter<Emitter.Bot> em, JitPassage.AddrCtx entry) Emit bytecode into the class initializer that adds the given entry point intoENTRIES.protected <N extends Emitter.Next>
Emitter<N> genValInit(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v) Emit into the constructor any bytecode necessary to support the given value.<N1 extends Emitter.Next,N0 extends Emitter.Ent<N1, Types.TRef<int[]>>>
Emitter<N1> genWriteFromArray(Emitter<N0> em, Local<Types.TRef<THIS>> localThis, JitVar v, JitType.MpIntJitType type, Opnd.Ext ext, Scope scope) Emit bytecode to store a varnode's value from an array of integer legs, in little endian order<N extends Emitter.Next>
Emitter<N> genWriteFromOpnd(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVar v, Opnd<JitType.MpIntJitType> opnd, Opnd.Ext ext, Scope scope) Emit bytecode to store a varnode's value from several locals.<T extends Types.BPrim<?>,JT extends JitType.SimpleJitType<T, JT>, N1 extends Emitter.Next, N0 extends Emitter.Ent<N1, T>>
Emitter<N1> genWriteFromStack(Emitter<N0> em, Local<Types.TRef<THIS>> localThis, JitVar v, JT type, Opnd.Ext ext, Scope scope) Emit bytecode to write the value on the JVM stack into the given variable.Get the address that generated the given p-code op.Get the allocation modelGet the analysis contextGet the error message for a given p-code op.Get the context of the instruction that generated the given p-code op.getOpEntry(PcodeOp op) Check if the given p-code op is the first of an instruction.Get the type modelGet the variable scope modelGet the label at the start of a block's translationload()Generate the classfile for this passage and load it into this JVM.Request an exception handler that can retire state for a given oprequestFieldForArrDirect(Address address) Request a field for the bytes backing the page at the given addressRequest a field for theJitCompiledPassage.ExitSlotfor the given targetRequest a field for aJitBytesPcodeExecutorStatePiece.JitBytesPcodeExecutorStateSpacefor the given address spacerequestFieldForUserop(PcodeUseropLibrary.PcodeUseropDefinition<byte[]> userop) Request a field for the given useropprotected ghidra.pcode.emu.jit.gen.FieldForContextRequest a field for the given contextreg valueRequest a field for the given p-code opRequest a field for the given varnoderesolveType(JitVal val, JitTypeBehavior type) Resolve the type of the given value to the given behaviorprotected Emitter<Emitter.Bot> Emit the first bytecodes for the static initializerprotected Emitter<Emitter.Bot> startInitMethod(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TRef<JitPcodeThread>> localThread) Emit the first bytecodes for the class constructor
-
Constructor Details
-
JitCodeGenerator
public JitCodeGenerator(MethodHandles.Lookup lookup, JitAnalysisContext context, JitControlFlowModel cfm, JitDataFlowModel dfm, JitVarScopeModel vsm, JitTypeModel tm, JitAllocationModel am, JitOpUseModel oum) Construct a code generator for the given passage's target classfileThis constructor chooses the name for the target classfile based on the passage's entry seed. It has the form:
Passage$at_address_context. The address is as rendered byAddress.toString()but with characters replaced to make it a valid JVM classfile name. The decode context is rendered in hexadecimal. This constructor also declares the fields and methods, and emits the definition forJitCompiledPassage.thread().- Parameters:
lookup- a means of accessing user-defined components, namely useropscontext- the analysis context for the passagecfm- the control flow modeldfm- the data flow modelvsm- the variable scope modeltm- the type modelam- the allocation modeloum- the op use model
-
-
Method Details
-
getAnalysisContext
Get the analysis context- Returns:
- the context
-
getVariableScopeModel
Get the variable scope model- Returns:
- the model
-
getTypeModel
Get the type model- Returns:
- the model
-
getAllocationModel
Get the allocation model- Returns:
- the model
-
startClInitMethod
Emit the first bytecodes for the static initializerThis generates code equivalent to:
static { LANGUAGE =getLanguage(LANGUAGE_ID); ADDRESS_FACTORY = LANGUAGE.getAddressFactory(); }Note that
LANGUAGE_IDis initialized to a constantStringin its declaration. Additionalstatic fieldsmay be requested as the p-code translation is emitted. -
startInitMethod
protected Emitter<Emitter.Bot> startInitMethod(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TRef<JitPcodeThread>> localThread) Emit the first bytecodes for the class constructorThis generates code equivalent to:
public Passage$at_00400000_0(JitPcodeThread thread) { super(); // Implicit in Java, but we must emit i this.thread = thread; this.state = thread.GetState(); }Additional
instance fieldsmay be requested as the p-code translation is emitted. -
genLoadJitStateSpace
protected <N extends Emitter.Next> Emitter<Emitter.Ent<N,Types.TRef<JitBytesPcodeExecutorStatePiece.JitBytesPcodeExecutorStateSpace>>> genLoadJitStateSpace(Emitter<N> em, Local<Types.TRef<THIS>> localThis, AddressSpace space) Emit bytecode to load the givenJitBytesPcodeExecutorStatePiece.JitBytesPcodeExecutorStateSpaceonto the JVM stackThis is equivalent to the Java expression
state.getForSpace(AddressFactory.getAddressSpace(spaceId)). The id of the givenspaceis encoded as an immediate or in the constant pool and is represented asspaceId.- Type Parameters:
N- the tail of the stack (...)- Parameters:
em- the emitterlocalThis- a handle tothisspace- the space to load at run time- Returns:
- the emitter with ..., stateSpace
-
requestFieldForSpaceIndirect
Request a field for aJitBytesPcodeExecutorStatePiece.JitBytesPcodeExecutorStateSpacefor the given address space- Parameters:
space- the address space- Returns:
- the field request
-
requestFieldForArrDirect
Request a field for the bytes backing the page at the given address- Parameters:
address- the address contained by the desired page- Returns:
- the field request
-
requestStaticFieldForContext
Request a field for the given contextreg value- Parameters:
ctx- the contextreg value- Returns:
- the field request
-
requestStaticFieldForVarnode
Request a field for the given varnode- Parameters:
vn- the varnode- Returns:
- the field request
-
requestStaticFieldForOp
Request a field for the given p-code opThis will request fields for each varnode for the op's operands
- Parameters:
op- the p-code op- Returns:
- the field request
-
requestFieldForUserop
public FieldForUserop requestFieldForUserop(PcodeUseropLibrary.PcodeUseropDefinition<byte[]> userop) Request a field for the given userop- Parameters:
userop- the userop- Returns:
- the field request
-
requestFieldForExitSlot
Request a field for theJitCompiledPassage.ExitSlotfor the given target- Parameters:
target- the target address and decode context- Returns:
- the field request
-
labelForBlock
Get the label at the start of a block's translation- Parameters:
block- the block- Returns:
- the label
-
requestExceptionHandler
public ExceptionHandler requestExceptionHandler(JitPassage.DecodedPcodeOp op, JitControlFlowModel.JitBlock block) Request an exception handler that can retire state for a given op- Parameters:
op- the op that might throw an exceptionblock- the block containing the op- Returns:
- the exception handler request
-
genValInit
protected <N extends Emitter.Next> Emitter<N> genValInit(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v) Emit into the constructor any bytecode necessary to support the given value.- Parameters:
v- the value from the use-def graph
-
genReadToStack
public <T extends Types.BPrim<?>,JT extends JitType.SimpleJitType<T, Emitter<Emitter.Ent<N,JT>, N extends Emitter.Next> T>> genReadToStack(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JT type, Opnd.Ext ext) Emit bytecode to read the given value onto the JVM stack.Although the value may be assigned a type by the
JitTypeModel, the type needed by a given op might be different.- Type Parameters:
T- the required JVM type of the valueJT- the required p-code type of the valueN- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (source) value to readtype- the required p-code type of the valueext- the kind of extension to apply- Returns:
- the code visitor typed with the resulting stack, i.e., having pushed the value
-
genReadToOpnd
public <N extends Emitter.Next> Opnd.OpndEm<JitType.MpIntJitType,N> genReadToOpnd(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JitType.MpIntJitType type, Opnd.Ext ext, Scope scope) Emit bytecode to read the given value into a series of locals- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (source) value to readtype- the required p-code type of the valueext- the kind of extension to applyscope- a scope for generating temporary local storage- Returns:
- the operand containing the locals, and the emitter typed with the incoming stack
-
genReadLegToStack
public <N extends Emitter.Next> Emitter<Emitter.Ent<N,Types.TInt>> genReadLegToStack(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JitType.MpIntJitType type, int leg, Opnd.Ext ext) Emit bytecode to load one leg of a multi-precision value from the varnode onto the JVM stack.- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (source) value to readtype- the p-code type of the complete multi-precision valueleg- the index of the leg to load, 0 being least significantext- the kind of extension to apply- Returns:
- the emitter typed with the resulting stack, i.e., having the int leg pushed onto it
-
genReadToArray
public <N extends Emitter.Next> Emitter<Emitter.Ent<N,Types.TRef<int[]>>> genReadToArray(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v, JitType.MpIntJitType type, Opnd.Ext ext, Scope scope, int slack) Emit bytecode to load the varnode's value into an integer array in little-endian order, pushing its ref onto the JVM stack.Ideally, multi-precision integers should be loaded into a series of locals, i.e., using
genReadToOpnd(Emitter, Local, JitVal, MpIntJitType, Ext, Scope), but this may not always be the best course of action. The first case is for userops, where it'd be onerous and counter-intuitive for a user to receive a single varnode in several parameters. The annotation system to sort that all out would also be atrocious and not easily made compatible with non-JIT emulation. Instead, mp-int arguments are received viaint[]parameters. The second case is for more complicated p-code ops. One notable example isint_mult. Theoretically, yes, we could emit all of the operations to compute the product using long multiplication inline; however, for large operands, that would produce an enormous number of bytecodes. Given the 64KB-per-method limit, we could quickly squeeze ourselves out of efficient translation of lengthy passages. Theslackparameter is provided since some of these algorithms (e.g., division) need an extra leg as scratch space. If we don't allocate it here, we force complexity into the implementation, as it would need to provide its own locals or re-allocate and copy the array.- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (source) value to readtype- the p-code type of the complete multi-precision valueext- the kind of extension to applyscope- a scope for generating temporary local storageslack- the number of additional, more significant, elements to allocate in the array- Returns:
- the emitter typed with the resulting stack, i.e., having the ref pushed onto it
-
genReadToBool
public <N extends Emitter.Next> Emitter<Emitter.Ent<N,Types.TInt>> genReadToBool(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVal v) Emit bytecode to load the varnode's value, interpreted as a boolean, as an integer onto the JVM stack.Any non-zero value is considered true, though ideally, slaspec authors should ensure all booleans are 1) 1-byte ints, and 2) only ever take the value 0 (false) or 1 (true). Nevertheless, we can't assume this guidance is followed. When we know a large (esp. multi-precision) variable is being used as a boolean, we have some opportunity for short-circuiting.
- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (source) value to read- Returns:
- the emitter typed with the resulting stack, i.e., having the int boolean pushed onto it
-
genWriteFromStack
public <T extends Types.BPrim<?>,JT extends JitType.SimpleJitType<T, Emitter<N1> genWriteFromStackJT>, N1 extends Emitter.Next, N0 extends Emitter.Ent<N1, T>> (Emitter<N0> em, Local<Types.TRef<THIS>> localThis, JitVar v, JT type, Opnd.Ext ext, Scope scope) Emit bytecode to write the value on the JVM stack into the given variable.Although the destination variable may be assigned a type by the
JitTypeModel, the type of the value on the stack may not match. This method needs to know that type so that, if necessary, it can convert it to the appropriate JVM type for the local variable that holds it.- Type Parameters:
T- the JVM type of the value on the stackJT- the p-code type of the value on the stackN1- the tail of the incoming stackN0- the incoming stack having the value on top- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (destination) variable to writetype- the p-code type of the value on the stackext- the kind of extension to applyscope- a scope for generating temporary local storage- Returns:
- the emitter typed with the resulting stack, i.e., having popped the value
-
genWriteFromOpnd
public <N extends Emitter.Next> Emitter<N> genWriteFromOpnd(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitVar v, Opnd<JitType.MpIntJitType> opnd, Opnd.Ext ext, Scope scope) Emit bytecode to store a varnode's value from several locals.- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (destination) variable to writeopnd- the operand whose locals contain the value to be storedext- the kind of extension to applyscope- a scope for generating temporary local storage- Returns:
- the emitter typed with the incoming stack
-
genWriteFromArray
public <N1 extends Emitter.Next,N0 extends Emitter.Ent<N1, Emitter<N1> genWriteFromArrayTypes.TRef<int[]>>> (Emitter<N0> em, Local<Types.TRef<THIS>> localThis, JitVar v, JitType.MpIntJitType type, Opnd.Ext ext, Scope scope) Emit bytecode to store a varnode's value from an array of integer legs, in little endian order- Type Parameters:
N1- the tail of the incoming stackN0- the incoming stack having the array ref on top- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencev- the (destination) variable to writetype- the p-code type of the value on the stackext- the kind of extension to applyscope- a scope for generating temporary local storage- Returns:
- the emitter typed with the resulting stack, i.e., having popped the array
-
genOp
protected OpGen.OpResult genOp(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localCtxmod, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq, PcodeOp op, JitControlFlowModel.JitBlock block, int opIdx) Emit the bytecode translation for a given p-code opThis first finds the use-def node for the op and then verifies that it has not been eliminated. If not, then it find the appropriate generator, emits line number information, and then emits the actual translation.
Line number information in the JVM is a map of strictly-positive line numbers to bytecode offsets. The ASM library allows this to be populated by placing labels and then emitting a line-number-to-label entry (via
MethodVisitor.visitLineNumber(int, Label). It seems the JVM presumes the entire class is defined in a single source file, so we are unable to (ab)use a filename field to encode debug information. We can encode the op index into the (integer) line number, although we have to add 1 to make it strictly positive.- Parameters:
em- the emitter typed with the empty stacklocalThis- a handle to the local holding thethisreferencelocalCtxmod- a handle to the local holdingctxmodretReq- an indication of what must be returned by thisJitCompiledPassage.run(int)method.op- the opblock- the block containing the opopIdx- the index of the op within the whole passage
-
genBlockOps
protected ghidra.pcode.emu.jit.gen.JitCodeGenerator.GenBlockResult genBlockOps(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localCtxmod, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq, JitControlFlowModel.JitBlock block, int opIdx) Emit the bytecode translation for the ops in the given p-code blockThis simply invokes
genOp(Emitter, Local, Local, RetReq, PcodeOp, JitBlock, int)on each op in the block and counts up the indices. Other per-block instrumentation is not included.- Parameters:
em- the emitterlocalThis- a handle tothislocalCtxmod- a handle toctxmodretReq- the required return type, in case an op needs to exit the passageblock- the blockopIdx- the index, within the whole passage, of the first op in the block- Returns:
- the result of block generation
- See Also:
-
genBlock
protected ghidra.pcode.emu.jit.gen.JitCodeGenerator.GenBlockResult genBlock(OpGen.OpResult prev, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localCtxmod, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq, JitControlFlowModel.JitBlock block, int opIdx) Emit the bytecode translation for the given p-code blockThis checks if the block needs a label, i.e., it is an entry or the target of a branch, and then optionally emits an invocation of
JitCompiledPassage.count(int, int). Finally, it emits the actual ops' translations viagenBlockOps(Emitter, Local, Local, RetReq, JitBlock, int).- Parameters:
block- the blockopIdx- the index, within the whole passage, of the first op in the block- Returns:
- the index, within the whole passage, of the op immediately after the block
-
genAddress
protected <N extends Emitter.Next> Emitter<Emitter.Ent<N,Types.TRef<Address>>> genAddress(Emitter<N> em, Address address) Emit code to load anAddressonto the JVM stackNote this does not load the identical address, but reconstructs it at run time.
- Type Parameters:
N- the tail of the stack (...)- Parameters:
em- the emitteraddress- the address to load- Returns:
- the emitter with ..., address
-
genStaticEntry
Emit bytecode into the class initializer that adds the given entry point intoENTRIES.Consider the entry
(ram:00400000,ctx=80000000). The code would be equivalent to:static { ENTRIES.add(new AddrCtx( ADDRESS_FACTORY.getAddressSpace(ramId).getAddress(0x400000), CTX_80000000)); }Note this method will request the appropriate
CTX_...field.- Parameters:
entry- the entry point to add
-
genStaticEntries
Emit code into the static initializer to initialize theENTRIESfield.This first constructs a new
ArrayListand assigns it to the field. Then, for each block representing a possible entry, it adds an element giving the address and contextreg value for the first op of that block. -
genClInitMethod
Emit bytecode for the static initializerNote that this method must be called after the bytecode for the run method is generated, because that generator may request various static fields to be created and initialized. Those requests are not known until the run-method generator has finished.
- Parameters:
em- the emitter- Returns:
- the emitter
-
genInitMethod
protected Emitter<Emitter.Bot> genInitMethod(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis) Emit all the bytecode for the constructorNote that this must be called after the bytecode for the run method is generated, because that generator may request various instance fields to be created and initialized. Those requests are not known until the run-method generator has finished.
To ensure a reasonable order, for debugging's sake, we request fields (and their initializations) for all the variables and values before iterating over the ops. This ensures, e.g., locals are declared in order of address for the varnodes they hold. Similarly, the pre-fetched byte arrays, whether for uniques, registers, or memory are initialized in order of address. Were these requests not made, they'd still get requested by the op generators, but the order would be less helpful.
-
genRunMethod
protected Emitter<Emitter.Dead> genRunMethod(Emitter<Emitter.Bot> em, Local<Types.TRef<THIS>> localThis, Local<Types.TInt> localBlockId, Methods.RetReq<Types.TRef<JitCompiledPassage.EntryPoint>> retReq) Emit all the bytecode for therunmethod.The structure of this method is described by this class's documentation. It first declares all the locals allocated by the
JitAllocationModel. It then collects the list of entries points and assigns a label to each. These are used when emitting the entry dispatch code. Several of those labels may also be re-used when translating branch ops. We must iterate over the blocks in the same order asgenStaticEntries(Emitter), so that our indices and its match. Thus, we emit atableswitchwhere each value maps to the blocks label identified in the same position of theENTRIESfield. We also provide a default case that just throws anIllegalArgumentException. We do not jump directly to the block's translation. Instead we emit a prologue for each block, wherein we birth the variables that block expects to be live, and then jump to the translation. Then, we emit the translation for each block usinggenBlock(OpResult, Local, Local, RetReq, JitBlock, int), placing transitions between those connected by fall through usingVarGen.computeBlockTransition(Local, JitCodeGenerator, JitBlock, JitBlock). Finally, we emit each requested exception handler usingExceptionHandler.genRun(Emitter, Local, JitCodeGenerator). -
load
Generate the classfile for this passage and load it into this JVM.- Returns:
- the translation, wrapped in utilities that knows how to process and instantiate it
-
dumpBytecode
protected byte[] dumpBytecode(byte[] bytes) For diagnostics: Dump the generated classfile to an actual file on disk- Parameters:
bytes- the classfile bytes- Returns:
- the same classfile bytes
- See Also:
-
generate
protected byte[] generate()Generate the classfile and get the raw bytesThis emits all the bytecode for all the required methods, static initializer, and constructor. Once complete, this closes out the methods by letting the ASM library compute the JVM stack frames as well as the maximum stack size and local variable count. Finally, it closes out the class a retrieves the resulting bytes.
- Returns:
- the classfile bytes
- Implementation Notes:
- The frame and maximums computation does not always succeed, and unfortunately, the
ASM library is not terribly keen to explain why. If
JitCompiler.Diag.DUMP_CLASSis enabled, we will catch whatever hairy exception gets thrown and close out the method anyway. The resulting class will not likely load into any JVM, but at least you might be able to examine it.
-
getOpEntry
Check if the given p-code op is the first of an instruction.- Parameters:
op- the op to check- Returns:
- the address-context pair
- See Also:
-
getExitContext
Get the context of the instruction that generated the given p-code op.This is necessary when exiting the passage, whether due to an exception or "normal" exit. The emulator's context must be updated so that it can resume execution appropriately.
- Parameters:
op- the p-code op causing the exit- Returns:
- the contextreg value
-
genRetirePcCtx
public <N extends Emitter.Next> Emitter<N> genRetirePcCtx(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitCodeGenerator.PcGen pcGen, RegisterValue ctx, JitCodeGenerator.RetireMode mode) Emit bytecode to set the emulator's counter and contextreg.Within a translated passage, there's no need to keep constant track of the program counter (nor decode context), since all the decoding has already been done. However, whenever we exit the passage and return control back to the emulator (whether by
returnorthrow) we must "retire" the program counter and decode context, as if the emulator had interpreted all the instructions just executed. This ensures that the emulator has the correct seed when seeking its next entry point, which may require decoding a new passage.- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferencepcGen- a means to emit bytecode to load the counter (as a long) onto the JVM stack. For errors, this is the address of the op causing the error. For branches, this is the branch target, which may be loaded from a varnode for an indirect branch.ctx- the contextreg value. For errors, this is the decode context of the op causing the error. For branches, this is the decode context at the target.mode- whether to set the machine state, too- Returns:
- the emitter typed with the incoming stack
-
genExit
public <N extends Emitter.Next> Emitter<N> genExit(Emitter<N> em, Local<Types.TRef<THIS>> localThis, JitControlFlowModel.JitBlock block, JitCodeGenerator.PcGen pcGen, RegisterValue ctx) Emit code to exit the passageThis retires all the variables of the current block as well as the program counter and decode context. It does not generate the actual
areturnorathrow, but everything required up to that point.- Type Parameters:
N- the incoming stack- Parameters:
em- the emitter typed with the incoming stacklocalThis- a handle to the local holding thethisreferenceblock- the block containing the op at which we are exitingpcGen- as ingenRetirePcCtx(Emitter, Local, PcGen, RegisterValue, RetireMode)ctx- as ingenRetirePcCtx(Emitter, Local, PcGen, RegisterValue, RetireMode)- Returns:
- the emitter with the incoming stack
-
getErrorMessage
Get the error message for a given p-code op.- Parameters:
op- the p-code op generating the error- Returns:
- the message
- See Also:
-
getAddressForOp
Get the address that generated the given p-code op.NOTE: The decoder rewrites ops to ensure they have the decode address, even if they were injected or from an inlined userop.
- Parameters:
op- the op- Returns:
- the address, i.e., the program counter at the time the op is executed
-
resolveType
Resolve the type of the given value to the given behavior- Parameters:
val- the valuetype- the behavior- Returns:
- the type
-