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 intprotected intprotected Collection<Integer>protected Collection<ProcessorVariant>protected ProcessorVariantFields 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 TypeMethodDescriptionbooleanClear 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 intGet the default processor mode.final EndiannessGet the endianness this processor operates in.intRetrieve the size of a General Purpose register, in bits.final intRetrieve the instruction alignment, in bytes.intfinal intgetMode()Get the processor mode.intRetrieve 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.booleanisRISC()The default implementation returns false (i.e.final InsnTypeparseAt(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 InsnTypeparseAtInternal(byte[] bytes, int index, int end) Decode bytes as instructions.parseWithContext(IMachineContext context) Parse a single instruction given a machine context.protected InsnTypeparseWithContextInternal(IMachineContext context) The default implementation throwsUnsupportedOperationException.protected final voidsetDefaultMode(int defaultMode) Set the default mode.final voidsetEndianness(Endianness endianness) Set the endianness of this processor.final voidsetInstructionAlignment(int align) Set the instruction alignment, in bytes.intsetMode(int newMode) Set the processor mode.voidsetVariant(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:
getRegisterNamein 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:IProcessorParse a single instruction at the specified address in memory.- Specified by:
parseAtin 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:IProcessorParse a single instruction.- Specified by:
parseAtin 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:IProcessorParse 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:
parseWithContextin 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:
getTypein interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
getRegisterBank
The default implementation attempts to find an appropriate hard-coded bank (fromgetType()) usingRegisterUtil.- Specified by:
getRegisterBankin 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:
isRISCin interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
getSupportedVariants
Description copied from interface:IProcessorRetrieve a list of supported processor variants.- Specified by:
getSupportedVariantsin interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
getVariant
Description copied from interface:IProcessorGet the processor variant used by this object.- Specified by:
getVariantin interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
setVariant
Description copied from interface:IProcessorSet the new variant used by this processor.- Specified by:
setVariantin interfaceIProcessor<InsnType extends IInstruction>- Throws:
ProcessorException- if the variant is not supported
-
getSupportedModes
Description copied from interface:IProcessorGet the list of valid modes that this processor supports.- Specified by:
getSupportedModesin interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
getDefaultMode
public final int getDefaultMode()Description copied from interface:IProcessorGet 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:
getDefaultModein 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:IProcessorRetrieve the size of the Program Counter register, in bits.- Specified by:
getPCRegisterBitsizein interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
getGPRegisterBitsize
public int getGPRegisterBitsize()Description copied from interface:IProcessorRetrieve the size of a General Purpose register, in bits.- Specified by:
getGPRegisterBitsizein interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
setMode
Description copied from interface:IProcessorSet 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:
setModein 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:IProcessorGet 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_Xxxfor common constants.- Specified by:
getModein interfaceIProcessor<InsnType extends IInstruction>- Returns:
- the processor mode, never
IProcessor.MODE_DEFAULT(0)
-
getEndianness
Description copied from interface:IProcessorGet the endianness this processor operates in.- Specified by:
getEndiannessin interfaceIProcessor<InsnType extends IInstruction>- Returns:
-
setEndianness
Description copied from interface:IProcessorSet the endianness of this processor.- Specified by:
setEndiannessin interfaceIProcessor<InsnType extends IInstruction>
-
getInstructionAlignment
public final int getInstructionAlignment()Description copied from interface:IProcessorRetrieve the instruction alignment, in bytes.- Specified by:
getInstructionAlignmentin 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:IProcessorSet 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:
setInstructionAlignmentin 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:
getResolverin 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:
createEntryPointin 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:IProcessorCreate an entry-point from a provided native address.- Specified by:
createEntryPointin 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
-