Instructions¶
Decoded Dalvik bytecode. Method.instructions returns a list of these objects. See bytecode in the code guide.
opcode_name
¶
The name of a Dalvik opcode byte, such as invoke-virtual for 0x6e.
An opcode the format does not use gives unused-XX, with the byte in hex.
to_string
¶
to_string(instruction: Instruction) -> str
One instruction as text: the opcode name, then its operands.
Operands that point into a table of the DEX file are shown as raw indices, such
as string@12 or method@4. An instruction alone cannot turn them into names.
For names, use Method.calls(), Method.field_accesses(), Method.string_loads()
and Method.type_uses().
Instruction
module-attribute
¶
Instruction = Union[
NopInstruction,
GotoInstruction,
IfInstruction,
IfZeroInstruction,
SwitchInstruction,
ReturnInstruction,
ThrowInstruction,
MoveInstruction,
ConstInstruction,
ConstStringInstruction,
ConstClassInstruction,
ConstMethodHandleInstruction,
ConstMethodTypeInstruction,
TypeInstruction,
FilledNewArrayInstruction,
MonitorInstruction,
FillArrayInstruction,
CompareInstruction,
ArrayOpInstruction,
ArrayLengthInstruction,
FieldInstruction,
InvokeInstruction,
InvokePolymorphicInstruction,
InvokeCustomInstruction,
UnaryOpInstruction,
BinaryOpInstruction,
BinaryLitInstruction,
InvalidInstruction,
]
Any decoded instruction: one of the 28 instruction classes.
Use isinstance to find out which one. Every class has a base with the offset, the
opcode and the size.
InstructionView
¶
Bases: Sequence[Instruction]
The decoded instructions of one method, as a read-only sequence.
It copies nothing up front: reading view[i] builds that one instruction.
It supports len(), indexing, slicing (which returns a list) and
iteration. The view keeps the analysis alive.
InstructionBase
¶
The fields every instruction has.
OperandWidth
¶
NopInstruction
¶
nop: does nothing.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
MoveInstruction
¶
move, move-wide, move-object and their /from16 and /16 forms, the
move-result instructions, and move-exception.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
src
instance-attribute
¶
The register read. None for the move-result instructions and
move-exception, which take their value from the last call or the caught
exception and name no source register.
ConstInstruction
¶
const/4, const/16, const, const/high16, const-wide/16, const-wide/32,
const-wide, const-wide/high16: load a number into a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
value
instance-attribute
¶
The number, as a 64-bit integer for every form. The bytecode does not say whether
a constant is an integer or a floating-point number, so a float or double
constant appears here as its bit pattern.
width
instance-attribute
¶
width: OperandWidth
PAIR for the const-wide forms, SINGLE for the others.
ConstStringInstruction
¶
const-string, const-string/jumbo: load a string into a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
string_index
instance-attribute
¶
Index of the string in the DEX file's string table. Method.string_loads()
gives the string itself.
ConstClassInstruction
¶
const-class: load a class object into a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
type_index
instance-attribute
¶
Index of the type in the DEX file's type table. Method.type_uses() gives
the descriptor.
ConstMethodHandleInstruction
¶
const-method-handle: load a java.lang.invoke.MethodHandle into a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
method_handle_index
instance-attribute
¶
Index of the handle in the DEX file's method handle table.
ConstMethodTypeInstruction
¶
const-method-type: load a java.lang.invoke.MethodType into a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
proto_index
instance-attribute
¶
Index of the prototype in the DEX file's prototype table.
ReturnInstruction
¶
return-void, return, return-wide, return-object.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
reg
instance-attribute
¶
The register that holds the returned value, or None for return-void.
ThrowInstruction
¶
throw: throws the exception held in a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
GotoInstruction
¶
goto, goto/16, goto/32: an unconditional jump.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
branch_offset
instance-attribute
¶
Where the branch goes, in code units, counted from this instruction's own offset. It is negative for a jump backwards.
IfInstruction
¶
if-eq, if-ne, if-lt, if-ge, if-gt, if-le: compare two registers and
jump when the condition holds.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
branch_offset
instance-attribute
¶
Where the branch goes, in code units, counted from this instruction's own offset. It is negative for a jump backwards.
IfZeroInstruction
¶
if-eqz, if-nez, if-ltz, if-gez, if-gtz, if-lez: compare one register
with zero and jump when the condition holds.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
branch_offset
instance-attribute
¶
Where the branch goes, in code units, counted from this instruction's own offset. It is negative for a jump backwards.
SwitchInstruction
¶
packed-switch, sparse-switch: jump to the case that matches a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
payload_offset
instance-attribute
¶
Where the case table is stored, in code units, counted from this instruction's own offset.
cases
instance-attribute
¶
cases: list[SwitchCase]
The decoded cases. The list is empty when the case table is damaged.
SwitchCase
¶
CompareInstruction
¶
cmpl-float, cmpg-float, cmpl-double, cmpg-double, cmp-long: compare two
values and store -1, 0 or 1.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
src_width
instance-attribute
¶
src_width: OperandWidth
PAIR when the operands are long or double, SINGLE for float.
TypeInstruction
¶
check-cast, instance-of, new-instance, new-array.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
type_index
instance-attribute
¶
Index of the type in the DEX file's type table. Method.type_uses() gives
the descriptor.
dest
instance-attribute
¶
The register the instruction works on: the register that is cast for
check-cast, the result for instance-of and new-array, the new object for
new-instance.
src
instance-attribute
¶
The object register for instance-of, the size register for new-array. None
for check-cast and new-instance, which use one register.
FilledNewArrayInstruction
¶
filled-new-array, filled-new-array/range: create an array and fill it from
registers.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
type_index
instance-attribute
¶
Index of the array type in the DEX file's type table.
FillArrayInstruction
¶
fill-array-data: fill an array from a table of constants stored in the code.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
payload_offset
instance-attribute
¶
Where the table of constants is stored, in code units, counted from this instruction's own offset.
MonitorInstruction
¶
monitor-enter, monitor-exit: take and release the lock of an object. They are
the start and end of a synchronized block.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
ArrayOpInstruction
¶
The aget and aput instructions: read or write one element of an array.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
value_reg
instance-attribute
¶
The register the element is read into, or written from.
ArrayLengthInstruction
¶
array-length: store the length of an array in a register.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
FieldInstruction
¶
The iget, iput, sget and sput instructions: read or write a field.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
field_index
instance-attribute
¶
Index of the field in the DEX file's field table. Method.field_accesses()
gives the FieldId.
object_reg
instance-attribute
¶
The register that holds the object. None for a static field.
InvokeInstruction
¶
invoke-virtual, invoke-super, invoke-direct, invoke-static,
invoke-interface and their /range forms: call a method.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
method_index
instance-attribute
¶
Index of the method in the DEX file's method table. Method.calls() gives the
MethodId and the kind of invoke.
arg_regs
instance-attribute
¶
The argument registers, in order. For a call on an object the first one holds the
object. A long or double argument takes two entries.
InvokePolymorphicInstruction
¶
invoke-polymorphic, invoke-polymorphic/range: call a signature-polymorphic
method, such as MethodHandle.invokeExact, with the prototype of this call.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
method_index
instance-attribute
¶
Index of the method in the DEX file's method table, such as
MethodHandle.invokeExact. Method.calls() gives the MethodId.
proto_index
instance-attribute
¶
Index in the DEX file's prototype table of the prototype of this call: the types the call site passes and expects back.
arg_regs
instance-attribute
¶
The argument registers, in order. The first one holds the method handle.
InvokeCustomInstruction
¶
invoke-custom, invoke-custom/range: call through a call site bound at
runtime.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
call_site_index
instance-attribute
¶
Index of the call site in the DEX file's call site table.
UnaryOpInstruction
¶
One-operand arithmetic and conversions: neg-int, not-long, int-to-long,
double-to-float and the like.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
BinaryOpInstruction
¶
Two-operand arithmetic on registers: add-int, sub-long, mul-double,
shl-int and the like, and their /2addr forms.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
src_a
instance-attribute
¶
The first operand. For a /2addr form it is the same register as dest.
BinaryLitInstruction
¶
Arithmetic on a register and a constant: add-int/lit16, rsub-int,
mul-int/lit8, ushr-int/lit8 and the like.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.
InvalidInstruction
¶
An opcode the Dalvik format does not use, as found in a damaged or deliberately
broken method. base.opcode is the raw byte and base.size is 1. Android
rejects such a method, so it never runs; the control-flow graph treats the
instruction like a nop.
base
instance-attribute
¶
base: InstructionBase
The fields every instruction has: offset, opcode and size.