Skip to content

14 — Dalvik and Smali · Cheat Sheet

Printable. Tables and commands only. Full explanations in README.md.


1. Register model

Fact Value
Register width 32 bits
long / double Two adjacent registers, named by the lower
Parameter location The last ins_size registers of the frame
pN to vN pN == v(registers_size − ins_size + N)
p0, instance method this
p0, static method First declared parameter
.registers N N = total registers, parameters included
.locals N N = locals only; assembler adds the parameters
4-bit register field v0–v15
8-bit register field v0–v255
16-bit register field v0–v65535
Max register entries in 35c invoke 5 words (a long counts as 2)

2. Type descriptors

Desc Java Desc Java
V void (return only) I int
Z boolean J long
B byte F float
S short D double
C char L<pkg>/<Name>; object
[<desc> array
Form Example
Class Lcom/example/Foo;
Nested class Lcom/example/Outer$Inner;
Array of int [I
Array of String [Ljava/lang/String;
2-D int array [[I
Method reference Lcom/ex/Foo;->bar(ILjava/lang/String;)Z
Field reference Lcom/ex/Foo;->baz:[B
Shorty for (Ljava/lang/String;[IJ)Z ZLLJ

3. Directives

Directive Scope
.class <flags> L…; file, first line
.super L…; file
.source "F.java" file (debug)
.implements L…; file, repeated
.field <flags> name:Desc [ = value ] file
.method <flags> name(Args)Ret … .end method file
.registers N / .locals N method, first
.param pN, "name" method
.prologue, .line N method (debug)
.local vN, "n":Desc / .end local / .restart local method (debug)
:label method
.catch LExc; {:s .. :e} :h method
.catchall {:s .. :e} :h method
.packed-switch <first> … .end packed-switch payload
.sparse-switch … .end sparse-switch payload
.array-data <width> … .end array-data payload
.annotation <vis> L…; … .end annotation class/field/method/param
Access flag Hex Access flag Hex
public 0x1 interface 0x200
private 0x2 abstract 0x400
protected 0x4 strictfp 0x800
static 0x8 synthetic 0x1000
final 0x10 annotation 0x2000
synchronized 0x20 enum 0x4000
volatile / bridge 0x40 constructor 0x10000
transient / varargs 0x80 declared-synchronized 0x20000
native 0x100
Annotation visibility Meaning
build In the DEX, not reflective
runtime Readable by reflection — survives obfuscation
system Signature, EnclosingClass, InnerClass, MemberClasses, Throws

4. Label prefixes generated by baksmali

Prefix Marks Suffix is
:goto_N goto target hex code-unit offset
:cond_N conditional-branch target hex code-unit offset
:try_start_N / :try_end_N protected range bounds counter
:catch_N / :catchall_N handler entry counter
:pswitch_N / :pswitch_data_N packed-switch case / payload counter
:sswitch_N / :sswitch_data_N sparse-switch case / payload counter
:array_N fill-array-data payload counter

5. Complete opcode map — 0x00–0xFF

Op Fmt Mnemonic Op Fmt Mnemonic
00 10x nop 01 12x move vA, vB
02 22x move/from16 vAA, vBBBB 03 32x move/16 vAAAA, vBBBB
04 12x move-wide vA, vB 05 22x move-wide/from16
06 32x move-wide/16 07 12x move-object vA, vB
08 22x move-object/from16 09 32x move-object/16
0a 11x move-result vAA 0b 11x move-result-wide vAA
0c 11x move-result-object vAA 0d 11x move-exception vAA
0e 10x return-void 0f 11x return vAA
10 11x return-wide vAA 11 11x return-object vAA
12 11n const/4 vA, #+B 13 21s const/16 vAA, #+BBBB
14 31i const vAA, #+BBBBBBBB 15 21h const/high16 vAA, #+BBBB0000
16 21s const-wide/16 17 31i const-wide/32
18 51l const-wide vAA, #64bit 19 21h const-wide/high16
1a 21c const-string vAA, "…" 1b 31c const-string/jumbo
1c 21c const-class vAA, L…; 1d 11x monitor-enter vAA
1e 11x monitor-exit vAA 1f 21c check-cast vAA, L…;
20 22c instance-of vA, vB, L…; 21 12x array-length vA, vB
22 21c new-instance vAA, L…; 23 22c new-array vA, vB, L…;
24 35c filled-new-array 25 3rc filled-new-array/range
26 31t fill-array-data vAA, :a 27 11x throw vAA
28 10t goto :l 29 20t goto/16 :l
2a 30t goto/32 :l 2b 31t packed-switch vAA, :d
2c 31t sparse-switch vAA, :d 2d 23x cmpl-float (NaN → −1)
2e 23x cmpg-float (NaN → +1) 2f 23x cmpl-double
30 23x cmpg-double 31 23x cmp-long
32 22t if-eq vA, vB, :l 33 22t if-ne vA, vB, :l
34 22t if-lt vA, vB, :l 35 22t if-ge vA, vB, :l
36 22t if-gt vA, vB, :l 37 22t if-le vA, vB, :l
38 21t if-eqz vAA, :l 39 21t if-nez vAA, :l
3a 21t if-ltz vAA, :l 3b 21t if-gez vAA, :l
3c 21t if-gtz vAA, :l 3d 21t if-lez vAA, :l
3e–43 — unused 44 23x aget vAA, vBB, vCC
45 23x aget-wide 46 23x aget-object
47 23x aget-boolean 48 23x aget-byte (sign-ext)
49 23x aget-char (zero-ext) 4a 23x aget-short
4b 23x aput vAA, vBB, vCC 4c 23x aput-wide
4d 23x aput-object 4e 23x aput-boolean
4f 23x aput-byte 50 23x aput-char
51 23x aput-short 52 22c iget vA, vB, f@
53 22c iget-wide 54 22c iget-object
55 22c iget-boolean 56 22c iget-byte
57 22c iget-char 58 22c iget-short
59 22c iput vA, vB, f@ 5a 22c iput-wide
5b 22c iput-object 5c 22c iput-boolean
5d 22c iput-byte 5e 22c iput-char
5f 22c iput-short 60 21c sget vAA, f@
61 21c sget-wide 62 21c sget-object
63 21c sget-boolean 64 21c sget-byte
65 21c sget-char 66 21c sget-short
67 21c sput vAA, f@ 68 21c sput-wide
69 21c sput-object 6a 21c sput-boolean
6b 21c sput-byte 6c 21c sput-char
6d 21c sput-short 6e 35c invoke-virtual
6f 35c invoke-super 70 35c invoke-direct
71 35c invoke-static 72 35c invoke-interface
73 — unused 74 3rc invoke-virtual/range
75 3rc invoke-super/range 76 3rc invoke-direct/range
77 3rc invoke-static/range 78 3rc invoke-interface/range
79–7a — unused 7b 12x neg-int
7c 12x not-int 7d 12x neg-long
7e 12x not-long 7f 12x neg-float
80 12x neg-double 81 12x int-to-long
82 12x int-to-float 83 12x int-to-double
84 12x long-to-int 85 12x long-to-float
86 12x long-to-double 87 12x float-to-int
88 12x float-to-long 89 12x float-to-double
8a 12x double-to-int 8b 12x double-to-long
8c 12x double-to-float 8d 12x int-to-byte
8e 12x int-to-char 8f 12x int-to-short
90–9a 23x add/sub/mul/div/rem/and/or/xor/shl/shr/ushr -int 9b–a5 23x same eleven, -long
a6–aa 23x add/sub/mul/div/rem -float ab–af 23x same five, -double
b0–ba 12x the eleven -int/2addr bb–c5 12x the eleven -long/2addr
c6–ca 12x the five -float/2addr cb–cf 12x the five -double/2addr
d0 22s add-int/lit16 d1 22s rsub-int (no suffix)
d2 22s mul-int/lit16 d3 22s div-int/lit16
d4 22s rem-int/lit16 d5 22s and-int/lit16
d6 22s or-int/lit16 d7 22s xor-int/lit16
d8 22b add-int/lit8 d9 22b rsub-int/lit8
da 22b mul-int/lit8 db 22b div-int/lit8
dc 22b rem-int/lit8 dd 22b and-int/lit8
de 22b or-int/lit8 df 22b xor-int/lit8
e0 22b shl-int/lit8 e1 22b shr-int/lit8
e2 22b ushr-int/lit8 e3–f9 — unused (quickened on API 26–29)
fa 45cc invoke-polymorphic fb 4rcc invoke-polymorphic/range
fc 35c invoke-custom fd 3rc invoke-custom/range
fe 21c const-method-handle ff 21c const-method-type

Quickened opcodes (ART, Android 8.0–10 only)

Op Mnemonic Op Mnemonic
e3 iget-quick eb iput-boolean-quick
e4 iget-wide-quick ec iput-byte-quick
e5 iget-object-quick ed iput-char-quick
e6 iput-quick ee iput-short-quick
e7 iput-wide-quick ef iget-boolean-quick
e8 iput-object-quick f0 iget-byte-quick
e9 invoke-virtual-quick f1 iget-char-quick
ea invoke-virtual/range-quick f2 iget-short-quick

6. Mnemonic suffix decoder

Suffix Meaning
/2addr Destination is also the first source: vA = vA op vB
/lit8 Last operand is an 8-bit signed literal
/lit16 Last operand is a 16-bit signed literal
/from16 Source register field widened to 16 bits
/16 Both register fields widened to 16 bits
/32 Operand widened to 32 bits
/high16 Literal supplies the high bits; low bits zero
/jumbo Constant-pool index widened to 32 bits
/range Register list replaced by first-register + count; requires contiguous registers
-wide Operates on a 64-bit register pair
-object Operates on a reference
cmpl-* NaN comparison yields −1
cmpg-* NaN comparison yields +1

7. Format identifiers

Fmt Units Shape Fmt Units Shape
10x 1 opcode only 22x 2 vAA, vBBBB
12x 1 vA, vB 23x 2 vAA, vBB, vCC
11n 1 vA, #+B 30t 3 +AAAAAAAA
11x 1 vAA 31c 3 vAA, index32
10t 1 +AA 31i 3 vAA, #+BBBBBBBB
20t 2 +AAAA 31t 3 vAA, +BBBBBBBB
21c 2 vAA, index 32x 3 vAAAA, vBBBB
21h 2 vAA, #+BBBB0000 35c 3 {vC..vG}, index
21s 2 vAA, #+BBBB 3rc 3 {vCCCC..}, index
21t 2 vAA, +BBBB 45cc 4 {vC..vG}, meth, proto
22b 2 vAA, vBB, #+CC 4rcc 4 {vCCCC..}, meth, proto
22c 2 vA, vB, index 51l 5 vAA, #+64-bit
22s 2 vA, vB, #+CCCC
22t 2 vA, vB, +CCCC

Read NAx: N = size in 16-bit code units, A = registers addressed (r = range), x = extra-operand kind. One code unit = 2 bytes. Byte-level layouts: see 12-DEX-Internals §3.25.


8. Verifier rules that break patches

Rule Failure
move-result* immediately follows its invoke-* VerifyError at class load
move-exception is the first instruction of a handler VerifyError
Register numbers < registers_size Assembler rejects
Wide value must not straddle the frame end Assembler / verifier rejects
Instruction family must match the type (return-object for L…;) VerifyError
No fall-through into a payload VerifyError
Uninitialised reference (between new-instance and <init>) must not escape VerifyError
Branch targets on instruction boundaries Assembler rejects
try range must cover ≥ 1 instruction Assembler rejects
monitor-enter / monitor-exit balanced on every path VerifyError or deadlock

9. Patch recipes

Goal Smali
boolean → true const/4 v0, 0x1 return v0
boolean → false const/4 v0, 0x0 return v0
int → 0 const/4 v0, 0x0 return v0
String → constant const-string v0, "x" return-object v0
Object → null const/4 v0, 0x0 return-object v0
long → 0 const-wide/16 v0, 0x0 return-wide v0
void → no-op return-void
Invert a branch if-eqz ↔ if-nez
Skip a block goto :after before it
Delete an instruction, keep offsets replace with nop
Overwrite a call result after move-result vX: const/4 vX, 0x1
Log a value after move-result*: const-string v1, "TAG" + invoke-static {v1, v0}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I
Non-String → String for logging invoke-static {v0}, Ljava/lang/String;->valueOf(I)Ljava/lang/String;
byte[] → String invoke-static {v0}, Ljava/util/Arrays;->toString([B)Ljava/lang/String;

Remember: raise .locals/.registers for any new register, and never write raw vN for a parameter.


10. Commands

Decode / rebuild

apktool d app.apk -o work/                 # full decode (Smali + resources)
apktool d -r -b app.apk -o work/           # Smali only, no debug directives -- much faster
apktool b work/ -o patched.apk
zipalign -p -f -v 4 patched.apk aligned.apk
apksigner sign --ks ~/.android/debug.keystore --ks-pass pass:android --out signed.apk aligned.apk
apksigner verify -v signed.apk
adb uninstall com.example.target ; adb install signed.apk

Bare DEX

unzip -o app.apk 'classes*.dex' -d dex/
baksmali d dex/classes.dex -o smali/
baksmali d dex/classes.dex -o smali/ --api 34 --allow-odex-opcodes --no-parameter-registers
baksmali list classes dex/classes.dex
smali a smali/ -o classes.dex --api 34
zip -j app.apk classes.dex

Inspect

dexdump -d classes.dex                     # disassembly with hex code units
dexdump -f classes.dex                     # header
jadx -d out/ --show-bad-code app.apk       # Java + Smali
adb logcat -s art:* AndroidRuntime:*       # verifier rejections
adb shell pm path com.example.target       # find the installed APK

Chapter tooling

python3 scripts/smali_stats.py work/ --top 25
python3 scripts/smali_stats.py work/ --heuristics
python3 scripts/smali_frame.py work/smali/com/example/Foo.smali
python3 scripts/smali_frame.py work/ --check          # scan a whole tree for register hazards
./scripts/smali-hunt.sh work/
frida -U -f com.example.target -l scripts/trace-method.js --no-pause

11. Grep catalogue

Target Command
All strings, ranked grep -rhoE 'const-string(/jumbo)? [vp][0-9]+, ".*"' smali*/ \| sed 's/.*, "//;s/"$//' \| sort \| uniq -c \| sort -rn
URLs grep -rhoE '"https?://[^"]+"' smali*/ \| sort -u
Crypto setup grep -rn 'Ljavax/crypto/Cipher;->getInstance' smali*/
Key material grep -rn 'fill-array-data' smali*/
TLS trust grep -rn 'HostnameVerifier\|X509TrustManager\|SSLContext;->init' smali*/
Root checks grep -rn '"su"\|/system/xbin\|magisk\|test-keys' smali*/
Signature checks grep -rn 'GET_SIGNATURES\|signingInfo\|getPackageInfo' smali*/
Reflection grep -rn 'Class;->forName\|getDeclaredMethod\|reflect/Method;->invoke' smali*/
Dynamic loading grep -rn 'Ldalvik/system/.*ClassLoader' smali*/
Native surface grep -rn '\.method.*native' smali*/ \| wc -l
Retrofit endpoints grep -rn -A3 '\.annotation runtime Lretrofit2/http/' smali*/
Gson field names grep -rn -A2 'SerializedName' smali*/
Boolean gates grep -rn '^\.method.*\(is\|has\|check\|verify\|valid\|allow\)[A-Za-z]*(.*)Z$' smali*/
Release logging grep -rn 'Landroid/util/Log;->' smali*/ \| wc -l
Kotlin ratio grep -rc 'Intrinsics;->checkNotNullParameter' smali*/
Callers of a method grep -rn 'Lcom/ex/Foo;->bar' smali*/
Construction sites grep -rn -A4 'new-instance .*Lcom/ex/Foo;' smali*/

Always use smali*/ with the glob — a multidex app has smali/, smali_classes2/, …


12. Recognition table

Shape in Smali Was in source
StringBuilder + append chain + toString "a" + b
hashCode() → sparse-switch → equals() → packed-switch switch on a String
.catchall + a body appearing twice try / finally
iterator() / hasNext() / next() / check-cast for (T t : collection)
array-length + index register + aget-object for (T t : array)
monitor-enter / body / monitor-exit + catchall synchronized (o) { }
synthetic static one-liner named access$NNN Inner class reading an outer private field
static final field named INSTANCE Kotlin object
Nested Companion class + static final Companion Kotlin companion object
Continuation parameter + label field + switch Kotlin suspend function
Intrinsics;->checkNotNullParameter at method top Kotlin non-null parameter
static method whose first parameter is the receiver Kotlin extension function
$default suffix + int bitmask parameter Kotlin default parameter values
$VALUES array + Ljava/lang/Enum; superclass An enum
synthetic bridge with an erased signature Generic method override
sget-boolean on $assertionsDisabled assert
while(true) + huge switch + state assignment per case Control-flow flattening

13. Historical Dalvik VM

Item Value
Introduced Android 1.0 (2008), by Dan Bornstein
Named after Dalvík, a village in Iceland
JIT introduced Android 2.2 Froyo (2010) — a trace JIT
ART optional Android 4.4 KitKat (developer option)
Dalvik removed Android 5.0 Lollipop
Interpreter mterp, hand-written assembly, 64-byte handler stride
Dispatch table_start + (opcode * handler_size) — a shift and an add
Install-time optimiser dexopt — verify, quicken, byte-swap, record dependencies
ODEX magic dey\n + 036\0 (plain DEX is dex\n + 036\0)
ODEX location /data/dalvik-cache/data@app@<pkg>-N.apk@classes.dex
JIT unit A trace — one linear path, ending at a branch/call/return/throw
JIT thread Separate; the app thread never blocked
Trace chaining Exit branches patched to jump directly into the next trace
GC Mark-and-sweep, non-compacting; concurrent marking from 2.3
Fatal flaw Nothing persisted — every process re-JITted at every launch
Surviving dalvik.vm.* property Still read by ART
dalvik.vm.heapstartsize Yes
dalvik.vm.heapgrowthlimit Yes
dalvik.vm.heapsize Yes
dalvik.vm.checkjni Yes
dalvik.vm.stack-trace-file Yes
dalvik.vm.execution-mode No — Dalvik only
dalvik.vm.dexopt-flags No — replaced by compiler filters

14. AOSP paths

Path Repository Holds
libdexfile/dex/dex_instruction_list.h platform/art Opcode ground truth
libdexfile/dex/dex_instruction.h, .cc platform/art Decoding
opcode-gen/bytecode.txt platform/dalvik Tooling-side opcode source
libdex/DexOpcodes.h, InstrUtils.cpp platform/dalvik Generated tables
libdex/DexFile.h platform/dalvik DEX_MAGIC, DEX_OPT_MAGIC, DexOptHeader
vm/mterp/README.txt platform/dalvik @ kitkat-release Interpreter design
vm/interp/Jit.cpp platform/dalvik @ kitkat-release Trace selection
vm/compiler/Frontend.cpp platform/dalvik @ kitkat-release Trace compilation
vm/analysis/ platform/dalvik @ kitkat-release Verifier + dexopt optimiser
docs/dexopt.html, docs/verifier.html platform/dalvik Original design documents

vm/ does not exist on branches after kitkat-release.