2. About myself
• In IT since 2000
• 6 years with mainframes
• Refactoring FinExpert virtual machine and finex
programming language
• Many assemblers in institute
•
•
•
•
•
Skype – mykola_bova
Twitter – mykola_bova
Facebook - FB/myk.bova
LinkedIn – ua.linkedin.com/in/mykbova
E-mail – bova.mykola@gmail.com
3. Why JVM byte code?
• A key to JVM internals and Java itself
• Practical understanding –key
for solving difficult problems
• A way to understand how it “really” works
4. What will be / will not be here?
• JVM and JVM byte code (JVM spec)
• “Touch” JVM byte code on practice (reJ,
ClassEditor)
• Libraries for byte code manipulation? – No
• Am I JVM / JVM byte code expert? - No
5. The ClassFile Structure
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
7. The Constant Pool (2)
• Table 4.3. Constant pool tags
Constant Type
CONSTANT_Class
CONSTANT_Fieldref
CONSTANT_Methodref
CONSTANT_InterfaceMethodref
CONSTANT_String
CONSTANT_Integer
CONSTANT_Float
CONSTANT_Long
CONSTANT_Double
CONSTANT_NameAndType
CONSTANT_Utf8
CONSTANT_MethodHandle
CONSTANT_MethodType
CONSTANT_InvokeDynamic
Value
7
9
10
11
8
3
4
5
6
12
1
15
16
18
8. The Constant Pool CONSTANT_Class_info
CONSTANT_Class_info {
u1 tag;
u2 name_index;
}
The CONSTANT_Class_info structure is used to
represent a class or an interface
9. The Constant Pool - CONSTANT_Fieldref_info,
CONSTANT_Methodref_info, and
CONSTANT_InterfaceMethodref_info
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
Fields, methods, and interface methods are represented by similar structures
10. The Constant Pool - The CONSTANT_String_info
Structure
CONSTANT_String_info {
u1 tag;
u2 string_index;
}
The CONSTANT_String_info structure is used to
represent constant objects of the type String
11. The Constant Pool - CONSTANT_Integer_info and
CONSTANT_Float_info
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
}
CONSTANT_Float_info {
u1 tag;
u4 bytes;
}
The CONSTANT_Integer_info and CONSTANT_Float_info
structures represent 4-byte numeric (int and float)
constants
12. The Constant Pool - The CONSTANT_Long_info and
CONSTANT_Double_info
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
The CONSTANT_Long_info and CONSTANT_Double_info
represent 8-byte numeric (long and double) constants
13. The Constant Pool - CONSTANT_NameAndType_info
CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
}
The CONSTANT_NameAndType_info structure is
used to represent a field or method, without
indicating which class or interface type it belongs
to
14. The Constant Pool - CONSTANT_Utf8_info
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
The CONSTANT_Utf8_info structure is used to
represent constant string values
15. The Constant Pool - CONSTANT_MethodHandle_info
CONSTANT_MethodHandle_info {
u1 tag;
u1 reference_kind;
u2 reference_index;
}
The CONSTANT_MethodHandle_info structure is
used to represent a method handle
16. The Constant Pool - CONSTANT_MethodType_info
CONSTANT_MethodType_info {
u1 tag;
u2 descriptor_index;
}
The CONSTANT_MethodType_info structure is
used to represent a method type
17. The Constant Pool - CONSTANT_InvokeDynamic_info
CONSTANT_InvokeDynamic_info {
u1 tag;
u2 bootstrap_method_attr_index;
u2 name_and_type_index;
}
The CONSTANT_InvokeDynamic_info structure is
used by an invokedynamic
instruction to specify a bootstrap method
18. The ClassFile Structure – Fields (1)
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
19. The ClassFile Structure – Fields (2) - Field access and
property flags
Flag Name
ACC_PUBLIC
ACC_PRIVATE
ACC_PROTECTED
ACC_STATIC
ACC_FINAL
ACC_VOLATILE
ACC_TRANSIENT
ACC_SYNTHETIC
source code.
ACC_ENUM
Value
0x0001
0x0002
0x0004
0x0008
0x0010
0x0040
0x0080
0x1000
Interpretation
Declared public.
Declared private.
Declared protected.
Declared static.
Declared final.
Declared volatile; cannot be cached.
Declared transient;
Declared synthetic; not present in the
0x4000
Declared as an element of an enum.
20. The ClassFile Structure – Methods (1)
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
21. The ClassFile Structure – Methods (2) – Methods
access and property flags
Flag Name
ACC_PUBLIC
ACC_PRIVATE
ACC_PROTECTED
ACC_STATIC
ACC_FINAL
ACC_SYNCHRONIZED
by a monitor use.
ACC_BRIDGE
ACC_VARARGS
ACC_NATIVE
than Java.
ACC_ABSTRACT
provided.
ACC_STRICT
ACC_SYNTHETIC
Value
0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
Interpretation
Declared public.
Declared private.
Declared protected.
Declared static.
Declared final.
Declared synchronized; invocation is wrapped
0x0040
0x0080
0x0100
A bridge method, generated by the compiler.
Declared with variable number of arguments.
Declared native; implemented in a language other
0x0400
Declared abstract; no implementation is
0x0800
0x1000
Declared strictfp; floating-point mode is FPstrict.
Declared synthetic; not present in the source code.
22. The ClassFile Structure – Attributes (1)
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
24. Predefined class file attributes (1) - ConstantValue
ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
}
A ConstantValue attribute represents the value
of a constant field.
25. Predefined class file attributes (2) - Code
Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack;
u2 max_locals;
u4 code_length;
u1 code[code_length];
u2 exception_table_length;
{ u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
} exception_table[exception_table_length];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
26. Predefined class file attributes (3) - StackMapTable
StackMapTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_entries;
stack_map_frame entries[number_of_entries];
}
This attribute is used during the process of
verification by type checking
27. Predefined class file attributes (4) - Exceptions
Exceptions_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_exceptions;
u2 exception_index_table[number_of_exceptions];
}
The Exceptions attribute is a variable-length attribute in
the attributes table of a method_info structure. The
Exceptions attribute indicates which checked
exceptions. a method may throw.
28. Predefined class file attributes (5) - InnerClasses
InnerClasses_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_classes;
{ u2 inner_class_info_index;
u2 outer_class_info_index;
u2 inner_name_index;
u2 inner_class_access_flags;
} classes[number_of_classes];
}
.
29. Predefined class file attributes (6) - EnclosingMethod
EnclosingMethod_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 class_index;
u2 method_index;
} throw.
A class must have an EnclosingMethod attribute if
and only if it is a local class or an anonymous
class.
30. Predefined class file attributes (7) - Synthetic
Synthetic_attribute {
u2 attribute_name_index;
u4 attribute_length;
}
A class member that does not appear in the
source code must be marked using a Synthetic
attribute
31. Predefined class file attributes (8) - Signature
Signature_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 signature_index;
}
The Signature attribute records generic signature
information for any class whose generic signature
in the Java programming language would include
references to type variables or parameterized
types.
32. Predefined class file attributes (9) - SourceFile
SourceFile_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 sourcefile_index;
}
33. Predefined class file attributes (10) SourceDebugExtension
SourceDebugExtension_attribute {
u2 attribute_name_index;
u4 attribute_length;
u1 debug_extension[attribute_length];
}
34. Predefined class file attributes (11) - LineNumberTable
LineNumberTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 line_number_table_length;
{ u2 start_pc;
u2 line_number;
} line_number_table[line_number_table_length];
}
It may be used by debuggers to determine which part of
the Java Virtual Machine code array corresponds to a
given line number in the original source file.
35. Predefined class file attributes (12) LocalVariableTable
LocalVariableTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_table_length;
{ u2 start_pc;
u2 length;
u2 name_index;
u2 descriptor_index;
u2 index;
} local_variable_table[local_variable_table_length];
}
It may be used by debuggers to determine the value of a given local
variable during the execution of a method.
36. Predefined class file attributes (13) LocalVariableTypeTable
LocalVariableTypeTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_type_table_length;
{ u2 start_pc;
u2 length;
u2 name_index;
u2 signature_index;
u2 index;
} local_variable_type_table[local_variable_type_table_length];
}
It may be used by debuggers to determine the value of a given local
variable during the execution of a method.
37. Predefined class file attributes (14) - Deprecated
Deprecated_attribute {
u2 attribute_name_index;
u4 attribute_length;
}
A class, interface, method, or field may be
marked using a Deprecated attribute to
indicate that the class, interface, method, or
field has been superseded.
38. Predefined class file attributes (15) –
RuntimeVisibleAnnotations part 1
RuntimeVisibleAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
annotation annotations[num_annotations];
}
The RuntimeVisibleAnnotations attribute records
run-time-visible Java programming language
annotations on the corresponding class, field, or
method.
39. Predefined class file attributes (15) –
RuntimeVisibleAnnotations part 2
annotation {
u2 type_index;
u2 num_element_value_pairs;
{ u2 element_name_index;
element_value value;
}
element_value_pairs[num_element_value_pairs];
}
40. Predefined class file attributes (15) –
RuntimeVisibleAnnotations part 3
element_value {
u1 tag;
union {
u2 const_value_index;
{ u2 type_name_index;
u2 const_name_index;
} enum_const_value;
u2 class_info_index;
annotation annotation_value;
{ u2 num_values;
element_value values[num_values];
} array_value;
} value;
}
41. Predefined class file attributes (16) –
RuntimeInvisibleAnnotations
RuntimeInvisibleAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_annotations;
annotation annotations[num_annotations];
}
Annotations represented by a
RuntimeInvisibleAnnotations attribute must not
be made available for return by reflective APIs
42. Predefined class file attributes (17) –
RuntimeVisibleParameterAnnotations
RuntimeVisibleParameterAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u1 num_parameters;
{ u2 num_annotations;
annotation annotations[num_annotations];
} parameter_annotations[num_parameters];
}
The RuntimeVisibleParameterAnnotations attribute records
run-time-visible Java programming language annotations
on the parameters of the corresponding method.
43. Predefined class file attributes (18) –
RuntimeInvisibleParameterAnnotations
RuntimeInvisibleParameterAnnotations_attribute {
u2 attribute_name_index;
u4 attribute_length;
u1 num_parameters;
{ u2 num_annotations;
annotation annotations[num_annotations];
} parameter_annotations[num_parameters];
}
44. Predefined class file attributes (19) –
AnnotationDefault
AnnotationDefault_attribute {
u2 attribute_name_index;
u4 attribute_length;
element_value default_value;
}
The AnnotationDefault attribute records the
default value
45. Predefined class file attributes (20) –
BootstrapMethods
BootstrapMethods_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_bootstrap_methods;
{ u2 bootstrap_method_ref;
u2 num_bootstrap_arguments;
u2 bootstrap_arguments[num_bootstrap_arguments];
} bootstrap_methods[num_bootstrap_methods];
}
The BootstrapMethods attribute records bootstrap method
specifiers referenced by invokedynamic instructions