1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5 <title>Java bytecode constraints</title>
6 <link rel=stylesheet href="java-constraints.css">
15 From the point of view of a piece of code written in the Java
16 programming language or targeted in the same way to <code>.class</code>
17 files, the Dalvik VM aims to behave in a way
18 that is fully consistent with the language's definition.
19 That is, the code running in Dalvik will behave the same as it
20 would have running in any other virtual machine. This includes
21 verification failures.
22 The Dx/Dalvik system will check roughly the same
23 constraints that any other VM would, except as noted in the file
24 <a href="verifier.html">verifier.html</a>. The following table briefly
25 lists all Dx/Dalvik verification constraints together their analogs
26 from the book <i>The Java<super>TM</super> Language Specification</i>,
27 second edition. In the numbering scheme, the first three
28 elements refer to the specification chapter, the fourth one to the
29 bullet inside that chapter. The failure mode specifies whether the
30 constraint will fail during the Dx conversion or during verification in
39 Static constraints are constraints on individual elements of the bytecode.
40 They usually can be checked without employing control or data-flow analysis
69 The <code>code</code> array must not be empty.
87 The <code>code</code> array must not be larger than 65535 bytes.
105 The first opcode in <code>code</code> array must have index
124 The <code>code</code> array must only contain valid opcodes.
142 The index of instruction <code>n+1</code> must equal the index of
143 instruction <code>n</code> plus the length of instruction
144 <code>n</code>, taking into account a possible <code>wide</code>
145 instruction. Opcodes modified by a <code>wide</code> instruction must
146 not be directly reachable.
164 The last instruction in <code>code</code> array must end at index
165 <code>code_length-1</code>.
183 All jump and branch targets must be opcodes within the same method.
184 Opcodes modified by a <code>wide</code> instruction must not be
185 directly reachable via a jump or branch instruction.
203 All targets of a <code>tableswitch</code> instruction must be opcodes
204 within the same method. Upper and lower bounds must be consistent.
205 Opcodes modified by a <code>wide</code> instruction must not be
206 directly reachable via a <code>tableswitch</code> instruction.
224 All targets of a <code>lookupswitch</code> instruction must be opcodes
225 within the same method. Its table must be consistent and sorted
226 low-to-high. Opcodes modified by a <code>wide</code> instruction must
227 not be directly reachable via a <code>lookupswitch</code> instruction.
245 The operands of <code>ldc</code> and <code>ldc_w</code> instructions
246 must be valid indices into the constant pool. The respective entries
247 must be of type <code>CONSTANT_Integer</code>,
248 <code>CONSTANT_Float</code>, or <code>CONSTANT_String</code>.
266 The operands of <code>ldc2_w</code> instructions must be valid indices
267 into the constant pool. The respective entries must be of type
268 <code>CONSTANT_Long</code> or <code>CONSTANT_Double</code>. The
269 subsequent constant pool entry must be valid and remain unused.
287 The Operands of <code>get<kind></code> and
288 <code>put<kind></code> instructions must be valid indices into
289 constant pool. The respective entries must be of type
290 <code>CONSTANT_Fieldref</code>.
308 The first two operands of <code>invokevirtual</code>,
309 <code>invokespecial</code>, and <code>invokestatic</code> must form a
310 valid 16-bit index into the constant pool. The respective entries must
311 be of type <code>CONSTANT_Methodref</code>.
329 Methods whose names start with '<' must only be invoked implicitly by
330 the VM, not by class file code. The only exception is the instance
331 initializer, which may be invoked by <code>invokespecial</code>.
349 The first two operands of <code>invokeinterface</code> must form a
350 valid 16-bit index into the constant pool. The entry must be of type
351 <code>CONSTANT_Interface_Methodref</code>. The third operand must
352 specify number of local variables and the fourth operand must always
371 The operands of <code>instanceof</code>, <code>checkcast</code>,
372 <code>new</code>, and <code>anewarray</code> instructions must
373 be a valid index into the constant pool. The first two operands of
374 <code>multianewarray</code> instruction must form a valid 16-bit index
375 into the constant pool. All respective entries must be of type
376 <code>CONSTANT_Class</code>.
394 The dimensions of an array created by <code>anewarray</code>
395 instructions must be less than <code>256</code>.
413 The <code>new</code> instruction must not reference array classes,
414 interfaces, or abstract classes.
432 The type referenced by a <code>multinewarray</code> instruction must
433 have at least as many dimensions as specified in the instruction. The
434 dimensions operand must not be <code>0</code>
452 The type referenced by a <code>newarray</code> instruction must be a
453 valid, non-reference type.
471 The index operand of instructions explicitly referencing single-width
472 local variables must be non-negative and smaller than
473 <code>max_locals</code>.
491 The index operand of instructions implicitly referencing single-width
492 local variables must be non-negative and smaller than
493 <code>max_locals</code>.
511 The index operand of instructions explicitly referencing double-width
512 local variables must be non-negative and smaller than
513 <code>max_locals-1</code>.
531 The index operand of instructions implicitly referencing double-width
532 local variables must be non-negative and smaller than
533 <code>max_locals-1</code>.
551 The index operand of <code>wide</code> instructions explicitly
552 referencing single-width local variables must be non-negative and
553 smaller than <code>max_locals</code>.
571 The index operand of <code>wide</code> instructions explicitly
572 referencing double-width local variables must be non-negative and
573 smaller than <code>max_locals-1</code>.
587 Structural constraints
591 Structural constraints are constraints on relationships between several
592 elements of the bytecode. They usually can't be checked without employing
593 control or data-flow analysis techniques.
621 The number and types of arguments (operands and local variables) must
622 always match the instruction.
640 The operand stack must have the same depth for all executions paths
641 leading to an instruction.
659 Local variable pairs must never be broken up.
677 A local variable (or pair) has to be assigned first before it can be
696 The operand stack must never grow beyond <code>max_stack</code>.
714 The operand stack must never underflow.
732 An <code>invokespecial</code> instruction must only invoke an instance
733 initializer or a method in the current class or one of its
752 An instance initializer must only be invoked on an uninitialized
753 instance residing on the operand stack.
771 Instance methods may only be invoked on and instance fields may only
772 be accessed on already initialized instances.
790 The must be no backwards branches with uninitialized instances on the
791 operand stack or in local variables. There must be no code protected
792 by an exception handler that contains local variables with
793 uninitialized instances.
811 An instance initializer must call another instance initializer (same
812 class or superclass) before any instance members can be accessed.
813 Exceptions are non-inherited instance fields, which can be assigned
814 before calling another initializer, and the <code>Object</code> class
833 All actual method arguments must be assignment-compatible with formal
852 For each instance method invocation, the actual instance must be
853 assignment-compatible with the class or interface specified in the
872 A returns instruction must match its method's return type.
890 When accessing protected members of a superclass, the actual type of
891 the instance being accessed must be either the current class or one
910 The type of a value stored into a static field must be
911 assignment-compatible with or convertible to the field's type.
929 The type of a value stored into a field must be assignment-compatible
930 with or convertible to the field's type.
948 The type of every value stored into an array must be
949 assignment-compatible with the array's component type.
967 The operand of an <code>athrow</code> instruction must be
968 assignment-compatible with <code>java.lang.Throwable</code>.
986 The last reachable instruction of a method must either be a backwards
987 jump or branch, a return, or an <code>athrow</code> instruction. It
988 must not be possible to leave the <code>code</code> array at the
1007 Local variable values must not be used as return addresses.
1025 There must be a single, uniquely determined return instruction per
1044 Subroutine calls must not be directly or indirectly self-recursive.
1062 <code>ReturnAddress</code> instances must not be reused. If a
1063 subroutine returns to a <code>ReturnAddress</code> further up the
1064 stack than where its original call instruction is located, then all
1065 <code>ReturnAddress</code> instances further down the stack must