Class AbstractProcessor<InsnType extends IInstruction>
java.lang.Object
com.pnfsoftware.jeb.core.units.code.asm.processor.AbstractProcessor<InsnType>
- Type Parameters:
InsnType
-
- All Implemented Interfaces:
IProcessor<InsnType>
@Ser
public abstract class AbstractProcessor<InsnType extends IInstruction>
extends Object
implements IProcessor<InsnType>
An abstract implementation of a
processor
. It is recommended to inherit from
this class. The implementor simply needs to implement a
parseAt()
method. Instruction alignment is
enforced.
Implementations of parseAtInternal(byte[], int, int)
cannot be guaranteed to be
thread-safe. Because of that, parsing is synchronized to allow concurrent threads to parse
safely. Use lock
instead of `synchronized(this)` for locking. (This will allow us to
offer implementors a way to disable locking/synchronizing when the implementation is truly
concurrent.)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
protected int
protected Collection<Integer>
protected Collection<ProcessorVariant>
protected ProcessorVariant
Fields inherited from interface com.pnfsoftware.jeb.core.units.code.asm.processor.IProcessor
MODE_128, MODE_16, MODE_256, MODE_32, MODE_64, MODE_8, MODE_DEFAULT, MODE_INVALID
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractProcessor
(int parseCacheLength, int defaultMode, IUnitCreator parent, int instructionAlign) AbstractProcessor
(int parseCacheLength, int defaultMode, IUnitCreator parent, int instructionAlign, int parseBufferBefore) Create a processor, retrieving Endianness from parentIUnitCreator
.AbstractProcessor
(int parseCacheLength, int defaultMode, Endianness endianness, int instructionAlign) Create a processor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Clear the instruction cache, if this processed uses one.createEntryPoint
(long address) The default implementation creates an entry-point using the exact provided address, with a default processor mode.createEntryPoint
(long address, int defaultMode) Create an entry-point from a provided native address.final int
Get the default processor mode.final Endianness
Get the endianness this processor operates in.int
Retrieve the size of a General Purpose register, in bits.final int
Retrieve the instruction alignment, in bytes.int
final int
getMode()
Get the processor mode.int
Retrieve the size of the Program Counter register, in bits.The default implementation attempts to find an appropriate hard-coded bank (fromgetType()
) usingRegisterUtil
.getRegisterName
(long registerCode) The default implementation attempts to find a name usinggetRegisterBank()
.The default implementation returns null.Get the list of valid modes that this processor supports.Retrieve a list of supported processor variants.getType()
The default implementation returnsProcessorType.UNKNOWN
.Get the processor variant used by this object.boolean
isRISC()
The default implementation returns false (i.e.final InsnType
parseAt
(byte[] bytes, int index, int end) Parse a single instruction.parseAt
(IVirtualMemory vm, long address) Parse a single instruction at the specified address in memory.protected abstract InsnType
parseAtInternal
(byte[] bytes, int index, int end) Decode bytes as instructions.parseWithContext
(IMachineContext context) Parse a single instruction given a machine context.protected InsnType
parseWithContextInternal
(IMachineContext context) The default implementation throwsUnsupportedOperationException
.protected final void
setDefaultMode
(int defaultMode) Set the default mode.final void
setEndianness
(Endianness endianness) Set the endianness of this processor.final void
setInstructionAlignment
(int align) Set the instruction alignment, in bytes.int
setMode
(int newMode) Set the processor mode.void
setVariant
(ProcessorVariant variant) Set the new variant used by this processor.
-
Field Details
-
supportedModes
-
defaultMode
protected int defaultMode -
mode
protected int mode -
supportedVariants
-
variant
-
-
Constructor Details
-
AbstractProcessor
public AbstractProcessor(int parseCacheLength, int defaultMode, Endianness endianness, int instructionAlign) Create a processor.- Parameters:
parseCacheLength
-defaultMode
-endianness
-instructionAlign
-
-
AbstractProcessor
public AbstractProcessor(int parseCacheLength, int defaultMode, IUnitCreator parent, int instructionAlign) -
AbstractProcessor
public AbstractProcessor(int parseCacheLength, int defaultMode, IUnitCreator parent, int instructionAlign, int parseBufferBefore) Create a processor, retrieving Endianness from parentIUnitCreator
. If the endianness cannot be retrieved, default little-endian will be used.- Parameters:
parseCacheLength
- Total byte length to retrieve whenparseAt(IVirtualMemory, long)
method is called.defaultMode
- Default Processor Modeparent
- Creator of the processorinstructionAlign
- Minimum instruction alignment allowed.parseBufferBefore
- number of bytes to parse before current address whenparseAt(IVirtualMemory, long)
method is called. Note than the byte range will be [address-parseBufferBefore, address+parseCacheLength]
-
-
Method Details
-
getRegisterName
The default implementation attempts to find a name usinggetRegisterBank()
.- Specified by:
getRegisterName
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
registerCode
- register code used by this processor object as well as generated instruction and operand objects. Not to be confused with standardized register indices defined inregister bank objects
- Returns:
- a name or null (on error or if the implementation is not provided)
-
parseAt
Description copied from interface:IProcessor
Parse a single instruction at the specified address in memory.- Specified by:
parseAt
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
vm
- the input virtual memoryaddress
- the address to read at, with permission ofread+write
- Returns:
- the instruction, never null. On parsing error, an exception is raised
- Throws:
ProcessorException
- on parsing error
-
parseAt
Description copied from interface:IProcessor
Parse a single instruction.- Specified by:
parseAt
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
bytes
- input code bufferindex
- where to parse in the input bufferend
- exclusive end offset in the buffer: if parsing at the given index offset requires bytes past the end offset, an exception is raised- Returns:
- the instruction, never null - on parsing error, an exception is raised
- Throws:
ProcessorException
- on parsing error
-
parseAtInternal
protected abstract InsnType parseAtInternal(byte[] bytes, int index, int end) throws ProcessorException Decode bytes as instructions. The one and only method all processors must implement. This method is not synchronized; implementations should keep its protection level to `protected`, unless they want client code to be able to do fast(er), albeit unsafe, parsing. Most implementations should keep this method protected, and client code should callparseAt(byte[], int, int)
or other similar, safe methods.- Parameters:
bytes
-index
-end
-- Returns:
- Throws:
ProcessorException
-
parseWithContext
Description copied from interface:IProcessor
Parse a single instruction given a machine context. The context provides access to at least:- memory data
- a register bank, including the program counter
- Specified by:
parseWithContext
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
- Throws:
ProcessorException
-
parseWithContextInternal
The default implementation throwsUnsupportedOperationException
.- Parameters:
context
-- Returns:
- Throws:
ProcessorException
-
getType
The default implementation returnsProcessorType.UNKNOWN
. Most implementation will want to override this default and return a proper processor type.- Specified by:
getType
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
getRegisterBank
The default implementation attempts to find an appropriate hard-coded bank (fromgetType()
) usingRegisterUtil
.- Specified by:
getRegisterBank
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
isRISC
public boolean isRISC()The default implementation returns false (i.e. assume the processor is not RISC unless explicitly specified).- Specified by:
isRISC
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
getSupportedVariants
Description copied from interface:IProcessor
Retrieve a list of supported processor variants.- Specified by:
getSupportedVariants
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
getVariant
Description copied from interface:IProcessor
Get the processor variant used by this object.- Specified by:
getVariant
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
setVariant
Description copied from interface:IProcessor
Set the new variant used by this processor.- Specified by:
setVariant
in interfaceIProcessor<InsnType extends IInstruction>
- Throws:
ProcessorException
- if the variant is not supported
-
getSupportedModes
Description copied from interface:IProcessor
Get the list of valid modes that this processor supports.- Specified by:
getSupportedModes
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
getDefaultMode
public final int getDefaultMode()Description copied from interface:IProcessor
Get the default processor mode. The mode indicates the current operating size of the processor. Typically, this represents the size in bits of general purpose (GP) registers, as well as the size of the program counter (PC).- Specified by:
getDefaultMode
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
- the default processor mode, never
IProcessor.MODE_DEFAULT
(0)
-
setDefaultMode
protected final void setDefaultMode(int defaultMode) Set the default mode. Only for implementors. This method is and should be called just once (at construction time).- Parameters:
defaultMode
- the default processor mode, cannot be 0
-
getPCRegisterBitsize
public int getPCRegisterBitsize()Description copied from interface:IProcessor
Retrieve the size of the Program Counter register, in bits.- Specified by:
getPCRegisterBitsize
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
getGPRegisterBitsize
public int getGPRegisterBitsize()Description copied from interface:IProcessor
Retrieve the size of a General Purpose register, in bits.- Specified by:
getGPRegisterBitsize
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
setMode
Description copied from interface:IProcessor
Set the processor mode. Implementors may override this class, if they have other/better/proper ways to determine the processor operating mode. In that case, the mode provided by this method may be regarded as a hint - or disregarded entirely.- Specified by:
setMode
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
newMode
- the mode, the valueIProcessor.MODE_DEFAULT
(0) can be provided to revert to the default processor mode- Returns:
- the previous processor mode
- Throws:
ProcessorException
- if the mode is not supported
-
getMode
public final int getMode()Description copied from interface:IProcessor
Get the processor mode. Typically, the processor mode is a size in bits that indicates how instructions are parsed and/or the size of general-purpose registers used for arithmetic operations and/or how memory slots are addressed are accessed. Typical processor modes are 8-, 16-, 32- or 64- bits). Note: A 'processor mode' could have a different semantic, but anything non-standard is unlikely to be understood by client code. This function will never return 0 SeeMODE_Xxx
for common constants.- Specified by:
getMode
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
- the processor mode, never
IProcessor.MODE_DEFAULT
(0)
-
getEndianness
Description copied from interface:IProcessor
Get the endianness this processor operates in.- Specified by:
getEndianness
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
-
setEndianness
Description copied from interface:IProcessor
Set the endianness of this processor.- Specified by:
setEndianness
in interfaceIProcessor<InsnType extends IInstruction>
-
getInstructionAlignment
public final int getInstructionAlignment()Description copied from interface:IProcessor
Retrieve the instruction alignment, in bytes.- Specified by:
getInstructionAlignment
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
- the instruction alignment, eg, 1, 2, 4, 8, etc.
-
getInstructionAlignmentMask
public int getInstructionAlignmentMask() -
setInstructionAlignment
public final void setInstructionAlignment(int align) Description copied from interface:IProcessor
Set the instruction alignment, in bytes. The implementation may decide to enforce alignment, or ignore it. If alignment is enforced, attempting to parse an unaligned instruction should raise aProcessorException
.- Specified by:
setInstructionAlignment
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
align
- the alignment, must be a strictly positive power of 2
-
getResolver
The default implementation returns null. Careful, access to this object is subject to locking.- Specified by:
getResolver
in interfaceIProcessor<InsnType extends IInstruction>
- Returns:
- a resolver
-
createEntryPoint
The default implementation creates an entry-point using the exact provided address, with a default processor mode.- Specified by:
createEntryPoint
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
address
- address of the instruction- Returns:
- the entry point (careful! may contain an address different -adjusted- than the input parameter)
-
createEntryPoint
Description copied from interface:IProcessor
Create an entry-point from a provided native address.- Specified by:
createEntryPoint
in interfaceIProcessor<InsnType extends IInstruction>
- Parameters:
address
- address of the instructiondefaultMode
- default processor mode if it cannot be determined by address- Returns:
- the entry point (careful! may contain an address different -adjusted- than the input parameter)
-
clearInstructionCache
public boolean clearInstructionCache()Clear the instruction cache, if this processed uses one.The default implementation does nothing.
- Returns:
- true if a cache was in place, was used, and was cleared; false otherwise
-