The bytecode verifier in the Dalvik VM attempts to provide the same sorts of checks and guarantees that other popular virtual machines do. We perform generally the same set of checks as are described in _The Java Virtual Machine Specification, Second Edition_, including the updates planned for the Third Edition.
Verification can be enabled for all classes, disabled for all, or enabled only for "remote" (non-bootstrap) classes. It should be performed for any class that will be processed with the DEX optimizer, and in fact the default VM behavior is to only optimize verified classes.
The verification process adds additional time to the build and to the installation of new applications. It's fairly quick for app-sized DEX files, but rather slow for the big "core" and "framework" files. Why do it all, when our system relies on UNIX processes for security?
It's also a convenient framework to deal with certain situations, notably replacement of instructions that access volatile 64-bit fields with more rigorous versions that guarantee atomicity.
There are a few checks that the Dalvik bytecode verifier does not perform, because they're not relevant. For example:
jsr
and ret
do not apply,
because Dalvik doesn't support subroutines.
new-instance
instruction.
This solves the same problem -- trickery potentially allowing
uninitialized objects to slip past the verifier -- without unduly
limiting branches.
move-exception
instruction can only appear as
the first instruction in an exception handler.
move-result*
instructions can only appear
immediately after an appropriate invoke-*
or filled-new-array
instruction.
The VM is permitted but not required to enforce "structured locking" constraints, which are designed to ensure that, when a method returns, all monitors locked by the method have been unlocked an equal number of times. This is not currently implemented.
The Dalvik verifier is more restrictive than other VMs in one area:
type safety on sub-32-bit integer widths. These additional restrictions
should make it impossible to, say, pass a value outside the range
[-128, 127] to a function that takes a byte
as an argument.
The verifier may reject a class immediately, or it may defer throwing an exception until the code is actually used. For example, if a class attempts to perform an illegal access on a field, the VM should throw an IllegalAccessError the first time the instruction is encountered. On the other hand, if a class contains an invalid bytecode, it should be rejected immediately with a VerifyError.
Immediate VerifyErrors are accompanied by detailed, if somewhat cryptic, information in the log file. From this it's possible to determine the exact instruction that failed, and the reason for the failure.
It's a bit tricky to implement deferred verification errors in Dalvik. A few approaches were considered:
In early versions of Dalvik (as found in Android 1.6 and earlier), the verifier simply regarded all problems as immediately fatal. This generally worked, but in some cases the VM was rejecting classes because of bits of code that were never used. The VerifyError itself was sometimes difficult to decipher, because it was thrown during verification rather than at the point where the problem was first noticed during execution.
The current version uses a variation of approach #1. The dexopt command works the way it did before, leaving the code untouched and flagging fully-correct classes as "pre-verified". When the VM loads a class that didn't pass pre-verification, the verifier is invoked. If a "deferrable" problem is detected, a modifiable copy of the instructions in the problematic method is made. In that copy, the troubled instruction is replaced with an "always throw" opcode, and verification continues.
In the example used earlier, an attempt to read from an inaccessible field would result in the "field get" instruction being replaced by "always throw IllegalAccessError on field X". Creating copies of method bodies requires additional heap space, but since this affects very few methods overall the memory impact should be minor.
Copyright © 2008 The Android Open Source Project