You are on page 1of 27

Dalvik Virtual Machine

P.N. Anantharaman
Director Engineering, Adobe Systems India
26 March 2011

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.


References and Credits

Some of the examples and content in this presentation and my other


presentations on Dalvik Virtual Machine draw heavily from the following
resources and to be credited with the authors of these literature
 Dalvik VM, Dan Bornstein Google IO 2008
 A JIT Compiler for Android’s Dalvik VM by Ben Cheng, Bill Buzbee –
Google IO, May 2010
 Understanding the Dalvik bytecode with the Dedexer tool, Gabor Paller

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 2


DVM as a part of Android stack

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 3


Application Programming Model

 Android applications are written in Java (some applications are written


partly in Java and partly native connected through JNI)
 The applications may extend, instantiate and use the different classes that
form part of Android distribution. For example, the applications that have
visible views extend the Activity class of Android.
public class myUIActivity extends Activity

…. //code that implements the activity

 The application written in Java source code gets compiled to Java class.
This in turn is converted to DVM byte code by a tool called dx. The DVM
byte code file has a .dex extension.
 The DVM loads the application classes from the dex file, executes the
application similar to the way JVM does for a Java class file (albeit several
key differences)

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 4


Topics for this chapter

 Why DVM? (particularly when JVM already exists and the Android
application programming model is Java based)
 What is DVM?
 Byte code description
 Dex file format
 DVM run time design (Interpreter and JIT compiler)
 Programming the DVM using byte code assembly (using
assemblers/disassemblers like Smali, Baksmali)

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 5


Why DVM?

Goals
 Run on a slow CPU
 Limited RAM
 OS without swap space
 Battery Powered

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 6


Memory efficiency

 RAM
 ODROID-7: 512 MB
 HTC Desire: 576 MB
 iPhone: 512 MB
 Available RAM gets reduced after the low level system start up and high
level services
 Multiple independent mutually suspicious processes
 Separate address spaces and separate memory
 Large system library

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 7


Dex File

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 8


Refer to the Google IO 2008 slides on DVM by Dan

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 9


Review Questions

 How does the dex file (produced by dx tool) differs from class file
(produced by javac)?
 In what ways DVM execution environment optimize memory as compared
to JVM based execution?
 Does the DVM enforce any security policies on the application that it
executes?
 What is a JIT compiler for DVM? What are the advantages and
disadvantages of a JIT compiler?

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 10


Register Versus Stack based VM

 Consider an example:
public int method( int i1,int i2 ) {
int i3 = i1*i2;
return i3*2;
}

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 11


Stack based VM

 Stack based machines use the stack to hold the operands, perform the
operation where the result is also stored on the stack
iload_1
iload_2
imul
istore_3
iload_3
iconst_2
imul
ireturn

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 12


Register based VM

mul-int v0,v2,v3 ; v0=v2*v3


mul-int/lit-8 v0,v0,2 ; v0=v0*2
return v0

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 13


Advantage of Register based VM over Stack based VM

 Stack based VMs have a simple architecture and provide ease of writing of
compiler back end. The JVM is stack based.
 One stack level can hold any type (char to float)
 Long and double need 2 consecutive stack levels
 Register-based architecture requires and average of 47% less executed VM
instructions than the stack based [architecture]. On the other hand the
register code is 25% larger than the corresponding stack code but this
increased cost of fetching more VM instructions due to larger code size
involves only 1.07% extra real machine loads per VM instruction which is
negligible. The overall performance of the register-based VM is that it takes
on average 32.3% less time to execute standard benchmarks
Security Engineering Research Group, Institute of Management Sciences. “Analysis of
Dalvik Virtual Machine and Class Path Library 2009”

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 14


Register based Architecture: DVM

 DVM is register based


 Uses virtual registers up to 64K
 Most instructions can use first 256
 One register can hold any type (char to float)
 Double and long values need 2 consecutive registers

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 15


Register frames

 Each invoked method has its own set of registers. Hence the registers
can be treated like local variables
 Invoked methods don’t affect the registers of invoking methods

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 16


Which is better?

 Most CPU architectures are register based. RISC architectures support a


large number of registers.
 The above implies that it is easier to map a virtual register based bytecode
instruction to a physical machine. For example, consider mul-int v0, v2, v3 that
performs: v2 * v3 and places the output on v0. this can be easily mapped to a
physical register set with suitable machine instruction
 Implementing stack based operations on a real machine requires memory
access and hence stack operations are slower than register operations
 It is possible for a JIT compiler to emulate stack based operations on
machine registers. The efficiency is then dependent upon the quality of
JIT compiler. This also may suggest that the JIT compiler itself may be
more sophisticated and hence may take more memory and also runtime
cycles for optimizations

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 17


Descriptors

 FieldDescriptor : FieldType
 ComponentType : FieldType
 FieldType : BaseType | ObjectType | ArrayType
 BaseType
 ObjectType : L<classname>;
 ArrayType : [ ComponentType
 ParameterDescriptor : FieldType
 MethodDescriptor : (ParameterDescriptor*) ReturnDescriptor
 ReturnDescriptor : FieldType | V

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 18


Base Type Terminals

 Base types
 I – int
 J – long
 Z – boolean
 D – double
 F – float
 S – short
 C – char
 B – Byte

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 19


Examples of field/method descriptors

Descriptor Field Declaration


I int i
[[J long [][] myLong;
[Ljava/lang/object; java.lang.object [] myObj;
Ljava/util/Hashtable; java.util.Hashtable myHash;
[[[Z boolean [][][] myBool;

Descriptor Method Declaration


()I int length();
()Ljava/lang/String; String toString();
([Ljava/lang/String;)V void main(String [] args);
()V void wait();
(JI)V void wait(long timeout, int nanos);
([BII)I int read(byte[] b, int off, int length);

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 20


DVM – General design

 The VM is register based and frames are fixed in size upon creation.
Each frame consists of a fixed number of registers as well as any adjunct
data required to execute the method
 The N arguments to a method land in the last N registers of methods
invocation frame
 Registers are 32 bit wide – adjacent registers can be combined for
double precision
 (Object) null == (int) 0
 The storage unit in the instruction stream is a 16 bit unsigned quantity.
Some bits in some instructions are ignored or must be zero
 Destination – then – source ordering for arguments
 Suffix “wide” refers to 64 bit quantities

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 21


Methods

 Rich meta information is associated with every Dalvik method that


include:
 Signature
 Try-catch information
 Annotations
 Number of registers used
 Debug information
 Line numbers
 Local variables life time

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 22


Method head example

.method private callEnumValues()[Ljava/lang/Object;


.annotation systemVisibility
Ldalvik/annotation/Signature;
value [Ljava/lang/String; = { "()[TT;" }
.end annotation
.limit registers 6
; this: v5 (Ljava/lang/ClassCache;)
.catch java/lang/IllegalAccessException from lbc5b4 to lbc5ce using lbc5e0
.catch java/lang/reflect/InvocationTargetException from lbc5b4 to lbc5ce
using lbc5f0
.catch java/lang/NoSuchMethodException from lbc58c to lbc5b0 using
lbc5d0
.var 5 is this Ljava/lang/ClassCache; from lbc58c to lbc59e

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 23


Method invocations

 Static methods: in the DVM byte code implementing the method, “this” is
not passed on as the first argument
 Direct: these are method invocation where the derived classes can’t
override the parent’s method. Hence these methods are invoked directly
without involving the class’s vtable
 Virtual: Derived classes can override the methods of their parent classes
from where they were derived. These are invoked using a vtable
associated with the class

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 24


Refer to DVM Instruction set

 Reference: http://www.netmite.com/android/mydroid/dalvik/docs/dalvik-
bytecode.html

© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 25


© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 26
© 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 27

You might also like