jvm1
TRANSCRIPT
![Page 1: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/1.jpg)
JVM byte code class file format
Nick BovaSep 1 2013
![Page 2: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/2.jpg)
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 – [email protected]
![Page 3: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/3.jpg)
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
![Page 4: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/4.jpg)
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
![Page 5: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/5.jpg)
The ClassFile StructureClassFile { 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];}
![Page 6: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/6.jpg)
The Constant Pool (1)
cp_info { u1 tag; u1 info[];}
![Page 7: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/7.jpg)
The Constant Pool (2)• Table 4.3. Constant pool tagsConstant Type ValueCONSTANT_Class 7CONSTANT_Fieldref 9CONSTANT_Methodref 10CONSTANT_InterfaceMethodref 11CONSTANT_String 8CONSTANT_Integer 3CONSTANT_Float 4CONSTANT_Long 5CONSTANT_Double 6CONSTANT_NameAndType 12CONSTANT_Utf8 1CONSTANT_MethodHandle 15CONSTANT_MethodType 16CONSTANT_InvokeDynamic 18
![Page 8: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/8.jpg)
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
![Page 9: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/9.jpg)
The Constant Pool - CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and
CONSTANT_InterfaceMethodref_infoCONSTANT_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
![Page 10: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/10.jpg)
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
![Page 11: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/11.jpg)
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
![Page 12: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/12.jpg)
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
![Page 13: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/13.jpg)
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
![Page 14: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/14.jpg)
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
![Page 15: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/15.jpg)
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
![Page 16: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/16.jpg)
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
![Page 17: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/17.jpg)
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 invokedynamicinstruction to specify a bootstrap method
![Page 18: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/18.jpg)
The ClassFile Structure – Fields (1)
field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count];}
![Page 19: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/19.jpg)
The ClassFile Structure – Fields (2) - Field access and property flags
Flag Name Value InterpretationACC_PUBLIC 0x0001 Declared public.ACC_PRIVATE 0x0002 Declared private.ACC_PROTECTED 0x0004 Declared protected.ACC_STATIC 0x0008 Declared static.ACC_FINAL 0x0010 Declared final.ACC_VOLATILE 0x0040 Declared volatile; cannot be cached.ACC_TRANSIENT 0x0080 Declared transient;ACC_SYNTHETIC 0x1000 Declared synthetic; not present in
the source code.ACC_ENUM 0x4000 Declared as an element of an enum.
![Page 20: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/20.jpg)
The ClassFile Structure – Methods (1)
method_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count];}
![Page 21: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/21.jpg)
The ClassFile Structure – Methods (2) – Methods access and property flags
Flag Name Value InterpretationACC_PUBLIC 0x0001 Declared public.ACC_PRIVATE 0x0002 Declared private.ACC_PROTECTED 0x0004 Declared protected.ACC_STATIC 0x0008 Declared static.ACC_FINAL 0x0010 Declared final.ACC_SYNCHRONIZED 0x0020 Declared synchronized; invocation is wrappedby a monitor use.ACC_BRIDGE 0x0040 A bridge method, generated by the compiler.ACC_VARARGS 0x0080 Declared with variable number of arguments.ACC_NATIVE 0x0100 Declared native; implemented in a language otherthan Java.ACC_ABSTRACT 0x0400 Declared abstract; no implementation isprovided.ACC_STRICT 0x0800 Declared strictfp; floating-point mode is FPstrict.ACC_SYNTHETIC 0x1000 Declared synthetic; not present in the source code.
![Page 22: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/22.jpg)
The ClassFile Structure – Attributes (1)
attribute_info { u2 attribute_name_index; u4 attribute_length; u1 info[attribute_length];}
![Page 23: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/23.jpg)
The ClassFile Structure – Attributes (2) - Predefined class file attributes
Attribute Java SE class fileConstantValue 1.0.2 45.3Code 1.0.2 45.3StackMapTable 6 50.0Exceptions 1.0.2 45.3InnerClasses 1.1 45.3EnclosingMethod 5.0 49.0Synthetic 1.1 45.3Signature 5.0 49.0SourceFile 1.0.2 45.3SourceDebugExtension 5.0 49.0LineNumberTable 1.0.2 45.3LocalVariableTable 1.0.2 45.3LocalVariableTypeTable 5.0 49.0Deprecated 1.1 45.3RuntimeVisibleAnnotations 5.0 49.0RuntimeInvisibleAnnotations 5.0 49.0RuntimeVisibleParameterAnnotations 5.0 49.0RuntimeInvisibleParameterAnnotations 5.0 49.0AnnotationDefault 5.0 49.0BootstrapMethods 7 51.0
![Page 24: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/24.jpg)
Predefined class file attributes (1) - ConstantValue
ConstantValue_attribute { u2 attribute_name_index; u4 attribute_length; u2 constantvalue_index;}A ConstantValue attribute represents the valueof a constant field.
![Page 25: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/25.jpg)
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];}
![Page 26: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/26.jpg)
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
![Page 27: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/27.jpg)
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.
![Page 28: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/28.jpg)
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];} .
![Page 29: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/29.jpg)
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.
![Page 30: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/30.jpg)
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
![Page 31: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/31.jpg)
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.
![Page 32: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/32.jpg)
Predefined class file attributes (9) - SourceFile
SourceFile_attribute { u2 attribute_name_index; u4 attribute_length; u2 sourcefile_index;}
![Page 33: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/33.jpg)
Predefined class file attributes (10) - SourceDebugExtension
SourceDebugExtension_attribute { u2 attribute_name_index; u4 attribute_length; u1 debug_extension[attribute_length];}
![Page 34: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/34.jpg)
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.
![Page 35: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/35.jpg)
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.
![Page 36: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/36.jpg)
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.
![Page 37: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/37.jpg)
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.
![Page 38: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/38.jpg)
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.
![Page 39: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/39.jpg)
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];
}
![Page 40: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/40.jpg)
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;}
![Page 41: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/41.jpg)
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
![Page 42: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/42.jpg)
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.
![Page 43: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/43.jpg)
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];}
![Page 44: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/44.jpg)
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
![Page 45: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/45.jpg)
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
![Page 46: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/46.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
Flags This class Super class
Interfaces
Fields
Methods
Attributes
![Page 47: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/47.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
This class Super class
Interfaces
Fields
Methods
Attributes
Flags
![Page 48: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/48.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
This class Super class
Interfaces
Fields
Methods
Attributes
Flags
![Page 49: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/49.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
This class Super class
Interfaces
Fields
Methods
Attributes
Flags
![Page 50: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/50.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
This class Super class
Interfaces
Fields
Methods
Attributes
Flags
![Page 51: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/51.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
This class Super class
Interfaces
Fields
Methods
Attributes
Flags
![Page 52: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/52.jpg)
Class format
CA FE BA BE Minor version Major version
Constant pool
This class Super class
Interfaces
Fields
Methods
Attributes
Flags
![Page 53: Jvm1](https://reader034.vdocuments.mx/reader034/viewer/2022051314/5551808bb4c90596028b480e/html5/thumbnails/53.jpg)
Easy Hack with Constant Pool