17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "classfile/stackMapTable.hpp"
29 #include "classfile/stackMapFrame.hpp"
30 #include "classfile/stackMapTableFormat.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/verifier.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "interpreter/bytecodes.hpp"
35 #include "interpreter/bytecodeStream.hpp"
36 #include "logging/log.hpp"
37 #include "memory/oopFactory.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "oops/instanceKlass.hpp"
40 #include "oops/oop.inline.hpp"
41 #include "oops/typeArrayOop.hpp"
42 #include "prims/jvm.h"
43 #include "runtime/fieldDescriptor.hpp"
44 #include "runtime/handles.inline.hpp"
45 #include "runtime/interfaceSupport.hpp"
46 #include "runtime/javaCalls.hpp"
47 #include "runtime/orderAccess.inline.hpp"
48 #include "runtime/os.hpp"
49 #include "runtime/thread.hpp"
50 #include "services/threadService.hpp"
51 #include "utilities/bytes.hpp"
52 #include "logging/log.hpp"
53
54 #define NOFAILOVER_MAJOR_VERSION 51
55 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION 51
56 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION 52
57 #define MAX_ARRAY_DIMENSIONS 255
58
59 // Access to external entry for VerifyClassCodes - old byte code verifier
60
61 extern "C" {
62 typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
63 typedef jboolean (*verify_byte_codes_fn_new_t)(JNIEnv *, jclass, char *, jint, jint);
64 }
65
66 static void* volatile _verify_byte_codes_fn = NULL;
67
68 static volatile jint _is_new_verify_byte_codes_fn = (jint) true;
69
70 static void* verify_byte_codes_fn() {
71 if (OrderAccess::load_ptr_acquire(&_verify_byte_codes_fn) == NULL) {
72 void *lib_handle = os::native_java_library();
174 if (klass->major_version() >= STACKMAP_ATTRIBUTE_MAJOR_VERSION) {
175 ClassVerifier split_verifier(klass, THREAD);
176 split_verifier.verify_class(THREAD);
177 exception_name = split_verifier.result();
178 if (can_failover && !HAS_PENDING_EXCEPTION &&
179 (exception_name == vmSymbols::java_lang_VerifyError() ||
180 exception_name == vmSymbols::java_lang_ClassFormatError())) {
181 log_info(verification)("Fail over class verification to old verifier for: %s", klassName);
182 log_info(class, init)("Fail over class verification to old verifier for: %s", klassName);
183 exception_name = inference_verify(
184 klass, message_buffer, message_buffer_len, THREAD);
185 }
186 if (exception_name != NULL) {
187 exception_message = split_verifier.exception_message();
188 }
189 } else {
190 exception_name = inference_verify(
191 klass, message_buffer, message_buffer_len, THREAD);
192 }
193
194 if (log_is_enabled(Info, class, init)){
195 log_end_verification(Log(class, init)::info_stream(), klassName, exception_name, THREAD);
196 }
197 if (log_is_enabled(Info, verification)){
198 log_end_verification(Log(verification)::info_stream(), klassName, exception_name, THREAD);
199 }
200
201 if (HAS_PENDING_EXCEPTION) {
202 return false; // use the existing exception
203 } else if (exception_name == NULL) {
204 return true; // verifcation succeeded
205 } else { // VerifyError or ClassFormatError to be created and thrown
206 ResourceMark rm(THREAD);
207 Klass* kls =
208 SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
209 if (log_is_enabled(Debug, class, resolve)) {
210 Verifier::trace_class_resolution(kls, klass);
211 }
212
213 while (kls != NULL) {
214 if (kls == klass) {
215 // If the class being verified is the exception we're creating
216 // or one of it's superclasses, we're in trouble and are going
217 // to infinitely recurse when we try to initialize the exception.
218 // So bail out here by throwing the preallocated VM error.
645 int ex_min = code_length;
646 int ex_max = -1;
647 // Look through each item on the exception table. Each of the fields must refer
648 // to a legal instruction.
649 if (was_recursively_verified()) return;
650 verify_exception_handler_table(
651 code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
652
653 // Look through each entry on the local variable table and make sure
654 // its range of code array offsets is valid. (4169817)
655 if (m->has_localvariable_table()) {
656 verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
657 }
658
659 Array<u1>* stackmap_data = m->stackmap_data();
660 StackMapStream stream(stackmap_data);
661 StackMapReader reader(this, &stream, code_data, code_length, THREAD);
662 StackMapTable stackmap_table(&reader, ¤t_frame, max_locals, max_stack,
663 code_data, code_length, CHECK_VERIFY(this));
664
665 if (log_is_enabled(Info, verification)) {
666 ResourceMark rm(THREAD);
667 stackmap_table.print_on(Log(verification)::info_stream());
668 }
669
670 RawBytecodeStream bcs(m);
671
672 // Scan the byte code linearly from the start to the end
673 bool no_control_flow = false; // Set to true when there is no direct control
674 // flow from current instruction to the next
675 // instruction in sequence
676
677 Bytecodes::Code opcode;
678 while (!bcs.is_last_bytecode()) {
679 // Check for recursive re-verification before each bytecode.
680 if (was_recursively_verified()) return;
681
682 opcode = bcs.raw_next();
683 u2 bci = bcs.bci();
684
685 // Set current frame's offset to bci
686 current_frame.set_offset(bci);
687 current_frame.set_mark();
688
689 // Make sure every offset in stackmap table point to the beginning to
690 // an instruction. Match current_frame to stackmap_table entry with
691 // the same offset if exists.
692 stackmap_index = verify_stackmap_table(
693 stackmap_index, bci, ¤t_frame, &stackmap_table,
694 no_control_flow, CHECK_VERIFY(this));
695
696
697 bool this_uninit = false; // Set to true when invokespecial <init> initialized 'this'
698 bool verified_exc_handlers = false;
699
700 // Merge with the next instruction
701 {
702 u2 index;
703 int target;
704 VerificationType type, type2;
705 VerificationType atype;
706
707 if (log_is_enabled(Info, verification)) {
708 ResourceMark rm(THREAD);
709 current_frame.print_on(Log(verification)::info_stream());
710 log_info(verification)("offset = %d, opcode = %s", bci, Bytecodes::name(opcode));
711 }
712
713 // Make sure wide instruction is in correct format
714 if (bcs.is_wide()) {
715 if (opcode != Bytecodes::_iinc && opcode != Bytecodes::_iload &&
716 opcode != Bytecodes::_aload && opcode != Bytecodes::_lload &&
717 opcode != Bytecodes::_istore && opcode != Bytecodes::_astore &&
718 opcode != Bytecodes::_lstore && opcode != Bytecodes::_fload &&
719 opcode != Bytecodes::_dload && opcode != Bytecodes::_fstore &&
720 opcode != Bytecodes::_dstore) {
721 /* Unreachable? RawBytecodeStream's raw_next() returns 'illegal'
722 * if we encounter a wide instruction that modifies an invalid
723 * opcode (not one of the ones listed above) */
724 verify_error(ErrorContext::bad_code(bci), "Bad wide instruction");
725 return;
726 }
727 }
728
729 // Look for possible jump target in exception handlers and see if it
730 // matches current_frame. Do this check here for astore*, dstore*,
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "classfile/stackMapTable.hpp"
29 #include "classfile/stackMapFrame.hpp"
30 #include "classfile/stackMapTableFormat.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/verifier.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "interpreter/bytecodes.hpp"
35 #include "interpreter/bytecodeStream.hpp"
36 #include "logging/log.hpp"
37 #include "logging/logStream.hpp"
38 #include "memory/oopFactory.hpp"
39 #include "memory/resourceArea.hpp"
40 #include "oops/instanceKlass.hpp"
41 #include "oops/oop.inline.hpp"
42 #include "oops/typeArrayOop.hpp"
43 #include "prims/jvm.h"
44 #include "runtime/fieldDescriptor.hpp"
45 #include "runtime/handles.inline.hpp"
46 #include "runtime/interfaceSupport.hpp"
47 #include "runtime/javaCalls.hpp"
48 #include "runtime/orderAccess.inline.hpp"
49 #include "runtime/os.hpp"
50 #include "runtime/thread.hpp"
51 #include "services/threadService.hpp"
52 #include "utilities/bytes.hpp"
53
54 #define NOFAILOVER_MAJOR_VERSION 51
55 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION 51
56 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION 52
57 #define MAX_ARRAY_DIMENSIONS 255
58
59 // Access to external entry for VerifyClassCodes - old byte code verifier
60
61 extern "C" {
62 typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
63 typedef jboolean (*verify_byte_codes_fn_new_t)(JNIEnv *, jclass, char *, jint, jint);
64 }
65
66 static void* volatile _verify_byte_codes_fn = NULL;
67
68 static volatile jint _is_new_verify_byte_codes_fn = (jint) true;
69
70 static void* verify_byte_codes_fn() {
71 if (OrderAccess::load_ptr_acquire(&_verify_byte_codes_fn) == NULL) {
72 void *lib_handle = os::native_java_library();
174 if (klass->major_version() >= STACKMAP_ATTRIBUTE_MAJOR_VERSION) {
175 ClassVerifier split_verifier(klass, THREAD);
176 split_verifier.verify_class(THREAD);
177 exception_name = split_verifier.result();
178 if (can_failover && !HAS_PENDING_EXCEPTION &&
179 (exception_name == vmSymbols::java_lang_VerifyError() ||
180 exception_name == vmSymbols::java_lang_ClassFormatError())) {
181 log_info(verification)("Fail over class verification to old verifier for: %s", klassName);
182 log_info(class, init)("Fail over class verification to old verifier for: %s", klassName);
183 exception_name = inference_verify(
184 klass, message_buffer, message_buffer_len, THREAD);
185 }
186 if (exception_name != NULL) {
187 exception_message = split_verifier.exception_message();
188 }
189 } else {
190 exception_name = inference_verify(
191 klass, message_buffer, message_buffer_len, THREAD);
192 }
193
194 LogTarget(Info, class, init) lt1;
195 if (lt1.is_enabled()) {
196 LogStream ls(lt1);
197 log_end_verification(&ls, klassName, exception_name, THREAD);
198 }
199 LogTarget(Info, verification) lt2;
200 if (lt2.is_enabled()) {
201 LogStream ls(lt2);
202 log_end_verification(&ls, klassName, exception_name, THREAD);
203 }
204
205 if (HAS_PENDING_EXCEPTION) {
206 return false; // use the existing exception
207 } else if (exception_name == NULL) {
208 return true; // verifcation succeeded
209 } else { // VerifyError or ClassFormatError to be created and thrown
210 ResourceMark rm(THREAD);
211 Klass* kls =
212 SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
213 if (log_is_enabled(Debug, class, resolve)) {
214 Verifier::trace_class_resolution(kls, klass);
215 }
216
217 while (kls != NULL) {
218 if (kls == klass) {
219 // If the class being verified is the exception we're creating
220 // or one of it's superclasses, we're in trouble and are going
221 // to infinitely recurse when we try to initialize the exception.
222 // So bail out here by throwing the preallocated VM error.
649 int ex_min = code_length;
650 int ex_max = -1;
651 // Look through each item on the exception table. Each of the fields must refer
652 // to a legal instruction.
653 if (was_recursively_verified()) return;
654 verify_exception_handler_table(
655 code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
656
657 // Look through each entry on the local variable table and make sure
658 // its range of code array offsets is valid. (4169817)
659 if (m->has_localvariable_table()) {
660 verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
661 }
662
663 Array<u1>* stackmap_data = m->stackmap_data();
664 StackMapStream stream(stackmap_data);
665 StackMapReader reader(this, &stream, code_data, code_length, THREAD);
666 StackMapTable stackmap_table(&reader, ¤t_frame, max_locals, max_stack,
667 code_data, code_length, CHECK_VERIFY(this));
668
669 LogTarget(Info, verification) lt;
670 if (lt.is_enabled()) {
671 ResourceMark rm(THREAD);
672 LogStream ls(lt);
673 stackmap_table.print_on(&ls);
674 }
675
676 RawBytecodeStream bcs(m);
677
678 // Scan the byte code linearly from the start to the end
679 bool no_control_flow = false; // Set to true when there is no direct control
680 // flow from current instruction to the next
681 // instruction in sequence
682
683 Bytecodes::Code opcode;
684 while (!bcs.is_last_bytecode()) {
685 // Check for recursive re-verification before each bytecode.
686 if (was_recursively_verified()) return;
687
688 opcode = bcs.raw_next();
689 u2 bci = bcs.bci();
690
691 // Set current frame's offset to bci
692 current_frame.set_offset(bci);
693 current_frame.set_mark();
694
695 // Make sure every offset in stackmap table point to the beginning to
696 // an instruction. Match current_frame to stackmap_table entry with
697 // the same offset if exists.
698 stackmap_index = verify_stackmap_table(
699 stackmap_index, bci, ¤t_frame, &stackmap_table,
700 no_control_flow, CHECK_VERIFY(this));
701
702
703 bool this_uninit = false; // Set to true when invokespecial <init> initialized 'this'
704 bool verified_exc_handlers = false;
705
706 // Merge with the next instruction
707 {
708 u2 index;
709 int target;
710 VerificationType type, type2;
711 VerificationType atype;
712
713 LogTarget(Info, verification) lt;
714 if (lt.is_enabled()) {
715 ResourceMark rm(THREAD);
716 LogStream ls(lt);
717 current_frame.print_on(&ls);
718 lt.print("offset = %d, opcode = %s", bci, Bytecodes::name(opcode));
719 }
720
721 // Make sure wide instruction is in correct format
722 if (bcs.is_wide()) {
723 if (opcode != Bytecodes::_iinc && opcode != Bytecodes::_iload &&
724 opcode != Bytecodes::_aload && opcode != Bytecodes::_lload &&
725 opcode != Bytecodes::_istore && opcode != Bytecodes::_astore &&
726 opcode != Bytecodes::_lstore && opcode != Bytecodes::_fload &&
727 opcode != Bytecodes::_dload && opcode != Bytecodes::_fstore &&
728 opcode != Bytecodes::_dstore) {
729 /* Unreachable? RawBytecodeStream's raw_next() returns 'illegal'
730 * if we encounter a wide instruction that modifies an invalid
731 * opcode (not one of the ones listed above) */
732 verify_error(ErrorContext::bad_code(bci), "Bad wide instruction");
733 return;
734 }
735 }
736
737 // Look for possible jump target in exception handlers and see if it
738 // matches current_frame. Do this check here for astore*, dstore*,
|