Mon, 06 Aug 2012 15:54:45 -0400
7116786: RFE: Detailed information on VerifyErrors
Summary: Provide additional detail in VerifyError messages
Reviewed-by: sspitsyn, acorn
1 /*
2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
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 */
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 "memory/oopFactory.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/instanceKlass.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "oops/typeArrayOop.hpp"
41 #include "prims/jvm.h"
42 #include "runtime/fieldDescriptor.hpp"
43 #include "runtime/handles.inline.hpp"
44 #include "runtime/interfaceSupport.hpp"
45 #include "runtime/javaCalls.hpp"
46 #include "runtime/orderAccess.hpp"
47 #include "runtime/os.hpp"
48 #ifdef TARGET_ARCH_x86
49 # include "bytes_x86.hpp"
50 #endif
51 #ifdef TARGET_ARCH_sparc
52 # include "bytes_sparc.hpp"
53 #endif
54 #ifdef TARGET_ARCH_zero
55 # include "bytes_zero.hpp"
56 #endif
57 #ifdef TARGET_ARCH_arm
58 # include "bytes_arm.hpp"
59 #endif
60 #ifdef TARGET_ARCH_ppc
61 # include "bytes_ppc.hpp"
62 #endif
64 #define NOFAILOVER_MAJOR_VERSION 51
66 // Access to external entry for VerifyClassCodes - old byte code verifier
68 extern "C" {
69 typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint);
70 typedef jboolean (*verify_byte_codes_fn_new_t)(JNIEnv *, jclass, char *, jint, jint);
71 }
73 static void* volatile _verify_byte_codes_fn = NULL;
75 static volatile jint _is_new_verify_byte_codes_fn = (jint) true;
77 static void* verify_byte_codes_fn() {
78 if (_verify_byte_codes_fn == NULL) {
79 void *lib_handle = os::native_java_library();
80 void *func = os::dll_lookup(lib_handle, "VerifyClassCodesForMajorVersion");
81 OrderAccess::release_store_ptr(&_verify_byte_codes_fn, func);
82 if (func == NULL) {
83 OrderAccess::release_store(&_is_new_verify_byte_codes_fn, false);
84 func = os::dll_lookup(lib_handle, "VerifyClassCodes");
85 OrderAccess::release_store_ptr(&_verify_byte_codes_fn, func);
86 }
87 }
88 return (void*)_verify_byte_codes_fn;
89 }
92 // Methods in Verifier
94 bool Verifier::should_verify_for(oop class_loader, bool should_verify_class) {
95 return (class_loader == NULL || !should_verify_class) ?
96 BytecodeVerificationLocal : BytecodeVerificationRemote;
97 }
99 bool Verifier::relax_verify_for(oop loader) {
100 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
101 bool need_verify =
102 // verifyAll
103 (BytecodeVerificationLocal && BytecodeVerificationRemote) ||
104 // verifyRemote
105 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted);
106 return !need_verify;
107 }
109 bool Verifier::verify(instanceKlassHandle klass, Verifier::Mode mode, bool should_verify_class, TRAPS) {
110 HandleMark hm;
111 ResourceMark rm(THREAD);
113 Symbol* exception_name = NULL;
114 const size_t message_buffer_len = klass->name()->utf8_length() + 1024;
115 char* message_buffer = NEW_RESOURCE_ARRAY(char, message_buffer_len);
116 char* exception_message = message_buffer;
118 const char* klassName = klass->external_name();
119 bool can_failover = FailOverToOldVerifier &&
120 klass->major_version() < NOFAILOVER_MAJOR_VERSION;
122 // If the class should be verified, first see if we can use the split
123 // verifier. If not, or if verification fails and FailOverToOldVerifier
124 // is set, then call the inference verifier.
125 if (is_eligible_for_verification(klass, should_verify_class)) {
126 if (TraceClassInitialization) {
127 tty->print_cr("Start class verification for: %s", klassName);
128 }
129 if (UseSplitVerifier &&
130 klass->major_version() >= STACKMAP_ATTRIBUTE_MAJOR_VERSION) {
131 ClassVerifier split_verifier(klass, THREAD);
132 split_verifier.verify_class(THREAD);
133 exception_name = split_verifier.result();
134 if (can_failover && !HAS_PENDING_EXCEPTION &&
135 (exception_name == vmSymbols::java_lang_VerifyError() ||
136 exception_name == vmSymbols::java_lang_ClassFormatError())) {
137 if (TraceClassInitialization || VerboseVerification) {
138 tty->print_cr(
139 "Fail over class verification to old verifier for: %s", klassName);
140 }
141 exception_name = inference_verify(
142 klass, message_buffer, message_buffer_len, THREAD);
143 }
144 if (exception_name != NULL) {
145 exception_message = split_verifier.exception_message();
146 }
147 } else {
148 exception_name = inference_verify(
149 klass, message_buffer, message_buffer_len, THREAD);
150 }
152 if (TraceClassInitialization || VerboseVerification) {
153 if (HAS_PENDING_EXCEPTION) {
154 tty->print("Verification for %s has", klassName);
155 tty->print_cr(" exception pending %s ",
156 instanceKlass::cast(PENDING_EXCEPTION->klass())->external_name());
157 } else if (exception_name != NULL) {
158 tty->print_cr("Verification for %s failed", klassName);
159 }
160 tty->print_cr("End class verification for: %s", klassName);
161 }
162 }
164 if (HAS_PENDING_EXCEPTION) {
165 return false; // use the existing exception
166 } else if (exception_name == NULL) {
167 return true; // verifcation succeeded
168 } else { // VerifyError or ClassFormatError to be created and thrown
169 ResourceMark rm(THREAD);
170 instanceKlassHandle kls =
171 SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false);
172 while (!kls.is_null()) {
173 if (kls == klass) {
174 // If the class being verified is the exception we're creating
175 // or one of it's superclasses, we're in trouble and are going
176 // to infinitely recurse when we try to initialize the exception.
177 // So bail out here by throwing the preallocated VM error.
178 THROW_OOP_(Universe::virtual_machine_error_instance(), false);
179 }
180 kls = kls->super();
181 }
182 message_buffer[message_buffer_len - 1] = '\0'; // just to be sure
183 THROW_MSG_(exception_name, exception_message, false);
184 }
185 }
187 bool Verifier::is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class) {
188 Symbol* name = klass->name();
189 klassOop refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass();
191 return (should_verify_for(klass->class_loader(), should_verify_class) &&
192 // return if the class is a bootstrapping class
193 // or defineClass specified not to verify by default (flags override passed arg)
194 // We need to skip the following four for bootstraping
195 name != vmSymbols::java_lang_Object() &&
196 name != vmSymbols::java_lang_Class() &&
197 name != vmSymbols::java_lang_String() &&
198 name != vmSymbols::java_lang_Throwable() &&
200 // Can not verify the bytecodes for shared classes because they have
201 // already been rewritten to contain constant pool cache indices,
202 // which the verifier can't understand.
203 // Shared classes shouldn't have stackmaps either.
204 !klass()->is_shared() &&
206 // As of the fix for 4486457 we disable verification for all of the
207 // dynamically-generated bytecodes associated with the 1.4
208 // reflection implementation, not just those associated with
209 // sun/reflect/SerializationConstructorAccessor.
210 // NOTE: this is called too early in the bootstrapping process to be
211 // guarded by Universe::is_gte_jdk14x_version()/UseNewReflection.
212 (refl_magic_klass == NULL ||
213 !klass->is_subtype_of(refl_magic_klass) ||
214 VerifyReflectionBytecodes)
215 );
216 }
218 Symbol* Verifier::inference_verify(
219 instanceKlassHandle klass, char* message, size_t message_len, TRAPS) {
220 JavaThread* thread = (JavaThread*)THREAD;
221 JNIEnv *env = thread->jni_environment();
223 void* verify_func = verify_byte_codes_fn();
225 if (verify_func == NULL) {
226 jio_snprintf(message, message_len, "Could not link verifier");
227 return vmSymbols::java_lang_VerifyError();
228 }
230 ResourceMark rm(THREAD);
231 if (VerboseVerification) {
232 tty->print_cr("Verifying class %s with old format", klass->external_name());
233 }
235 jclass cls = (jclass) JNIHandles::make_local(env, klass->java_mirror());
236 jint result;
238 {
239 HandleMark hm(thread);
240 ThreadToNativeFromVM ttn(thread);
241 // ThreadToNativeFromVM takes care of changing thread_state, so safepoint
242 // code knows that we have left the VM
244 if (_is_new_verify_byte_codes_fn) {
245 verify_byte_codes_fn_new_t func =
246 CAST_TO_FN_PTR(verify_byte_codes_fn_new_t, verify_func);
247 result = (*func)(env, cls, message, (int)message_len,
248 klass->major_version());
249 } else {
250 verify_byte_codes_fn_t func =
251 CAST_TO_FN_PTR(verify_byte_codes_fn_t, verify_func);
252 result = (*func)(env, cls, message, (int)message_len);
253 }
254 }
256 JNIHandles::destroy_local(cls);
258 // These numbers are chosen so that VerifyClassCodes interface doesn't need
259 // to be changed (still return jboolean (unsigned char)), and result is
260 // 1 when verification is passed.
261 if (result == 0) {
262 return vmSymbols::java_lang_VerifyError();
263 } else if (result == 1) {
264 return NULL; // verified.
265 } else if (result == 2) {
266 THROW_MSG_(vmSymbols::java_lang_OutOfMemoryError(), message, NULL);
267 } else if (result == 3) {
268 return vmSymbols::java_lang_ClassFormatError();
269 } else {
270 ShouldNotReachHere();
271 return NULL;
272 }
273 }
275 TypeOrigin TypeOrigin::null() {
276 return TypeOrigin();
277 }
278 TypeOrigin TypeOrigin::local(u2 index, StackMapFrame* frame) {
279 assert(frame != NULL, "Must have a frame");
280 return TypeOrigin(CF_LOCALS, index, StackMapFrame::copy(frame),
281 frame->local_at(index));
282 }
283 TypeOrigin TypeOrigin::stack(u2 index, StackMapFrame* frame) {
284 assert(frame != NULL, "Must have a frame");
285 return TypeOrigin(CF_STACK, index, StackMapFrame::copy(frame),
286 frame->stack_at(index));
287 }
288 TypeOrigin TypeOrigin::sm_local(u2 index, StackMapFrame* frame) {
289 assert(frame != NULL, "Must have a frame");
290 return TypeOrigin(SM_LOCALS, index, StackMapFrame::copy(frame),
291 frame->local_at(index));
292 }
293 TypeOrigin TypeOrigin::sm_stack(u2 index, StackMapFrame* frame) {
294 assert(frame != NULL, "Must have a frame");
295 return TypeOrigin(SM_STACK, index, StackMapFrame::copy(frame),
296 frame->stack_at(index));
297 }
298 TypeOrigin TypeOrigin::bad_index(u2 index) {
299 return TypeOrigin(BAD_INDEX, index, NULL, VerificationType::bogus_type());
300 }
301 TypeOrigin TypeOrigin::cp(u2 index, VerificationType vt) {
302 return TypeOrigin(CONST_POOL, index, NULL, vt);
303 }
304 TypeOrigin TypeOrigin::signature(VerificationType vt) {
305 return TypeOrigin(SIG, 0, NULL, vt);
306 }
307 TypeOrigin TypeOrigin::implicit(VerificationType t) {
308 return TypeOrigin(IMPLICIT, 0, NULL, t);
309 }
310 TypeOrigin TypeOrigin::frame(StackMapFrame* frame) {
311 return TypeOrigin(FRAME_ONLY, 0, StackMapFrame::copy(frame),
312 VerificationType::bogus_type());
313 }
315 void TypeOrigin::reset_frame() {
316 if (_frame != NULL) {
317 _frame->restore();
318 }
319 }
321 void TypeOrigin::details(outputStream* ss) const {
322 _type.print_on(ss);
323 switch (_origin) {
324 case CF_LOCALS:
325 ss->print(" (current frame, locals[%d])", _index);
326 break;
327 case CF_STACK:
328 ss->print(" (current frame, stack[%d])", _index);
329 break;
330 case SM_LOCALS:
331 ss->print(" (stack map, locals[%d])", _index);
332 break;
333 case SM_STACK:
334 ss->print(" (stack map, stack[%d])", _index);
335 break;
336 case CONST_POOL:
337 ss->print(" (constant pool %d)", _index);
338 break;
339 case SIG:
340 ss->print(" (from method signature)");
341 break;
342 case IMPLICIT:
343 case FRAME_ONLY:
344 case NONE:
345 default:
346 ;
347 }
348 }
350 #ifdef ASSERT
351 void TypeOrigin::print_on(outputStream* str) const {
352 str->print("{%d,%d,%p:", _origin, _index, _frame);
353 if (_frame != NULL) {
354 _frame->print_on(str);
355 } else {
356 str->print("null");
357 }
358 str->print(",");
359 _type.print_on(str);
360 str->print("}");
361 }
362 #endif
364 void ErrorContext::details(outputStream* ss, methodOop method) const {
365 if (is_valid()) {
366 ss->print_cr("");
367 ss->print_cr("Exception Details:");
368 location_details(ss, method);
369 reason_details(ss);
370 frame_details(ss);
371 bytecode_details(ss, method);
372 handler_details(ss, method);
373 stackmap_details(ss, method);
374 }
375 }
377 void ErrorContext::reason_details(outputStream* ss) const {
378 streamIndentor si(ss);
379 ss->indent().print_cr("Reason:");
380 streamIndentor si2(ss);
381 ss->indent().print("");
382 switch (_fault) {
383 case INVALID_BYTECODE:
384 ss->print("Error exists in the bytecode");
385 break;
386 case WRONG_TYPE:
387 if (_expected.is_valid()) {
388 ss->print("Type ");
389 _type.details(ss);
390 ss->print(" is not assignable to ");
391 _expected.details(ss);
392 } else {
393 ss->print("Invalid type: ");
394 _type.details(ss);
395 }
396 break;
397 case FLAGS_MISMATCH:
398 if (_expected.is_valid()) {
399 ss->print("Current frame's flags are not assignable "
400 "to stack map frame's.");
401 } else {
402 ss->print("Current frame's flags are invalid in this context.");
403 }
404 break;
405 case BAD_CP_INDEX:
406 ss->print("Constant pool index %d is invalid", _type.index());
407 break;
408 case BAD_LOCAL_INDEX:
409 ss->print("Local index %d is invalid", _type.index());
410 break;
411 case LOCALS_SIZE_MISMATCH:
412 ss->print("Current frame's local size doesn't match stackmap.");
413 break;
414 case STACK_SIZE_MISMATCH:
415 ss->print("Current frame's stack size doesn't match stackmap.");
416 break;
417 case STACK_OVERFLOW:
418 ss->print("Exceeded max stack size.");
419 break;
420 case STACK_UNDERFLOW:
421 ss->print("Attempt to pop empty stack.");
422 break;
423 case MISSING_STACKMAP:
424 ss->print("Expected stackmap frame at this location.");
425 break;
426 case BAD_STACKMAP:
427 ss->print("Invalid stackmap specification.");
428 break;
429 case UNKNOWN:
430 default:
431 ShouldNotReachHere();
432 ss->print_cr("Unknown");
433 }
434 ss->print_cr("");
435 }
437 void ErrorContext::location_details(outputStream* ss, methodOop method) const {
438 if (_bci != -1 && method != NULL) {
439 streamIndentor si(ss);
440 const char* bytecode_name = "<invalid>";
441 if (method->validate_bci_from_bcx(_bci) != -1) {
442 Bytecodes::Code code = Bytecodes::code_or_bp_at(method->bcp_from(_bci));
443 if (Bytecodes::is_defined(code)) {
444 bytecode_name = Bytecodes::name(code);
445 } else {
446 bytecode_name = "<illegal>";
447 }
448 }
449 instanceKlass* ik = instanceKlass::cast(method->method_holder());
450 ss->indent().print_cr("Location:");
451 streamIndentor si2(ss);
452 ss->indent().print_cr("%s.%s%s @%d: %s",
453 ik->name()->as_C_string(), method->name()->as_C_string(),
454 method->signature()->as_C_string(), _bci, bytecode_name);
455 }
456 }
458 void ErrorContext::frame_details(outputStream* ss) const {
459 streamIndentor si(ss);
460 if (_type.is_valid() && _type.frame() != NULL) {
461 ss->indent().print_cr("Current Frame:");
462 streamIndentor si2(ss);
463 _type.frame()->print_on(ss);
464 }
465 if (_expected.is_valid() && _expected.frame() != NULL) {
466 ss->indent().print_cr("Stackmap Frame:");
467 streamIndentor si2(ss);
468 _expected.frame()->print_on(ss);
469 }
470 }
472 void ErrorContext::bytecode_details(outputStream* ss, methodOop method) const {
473 if (method != NULL) {
474 streamIndentor si(ss);
475 ss->indent().print_cr("Bytecode:");
476 streamIndentor si2(ss);
477 ss->print_data(method->code_base(), method->code_size(), false);
478 }
479 }
481 void ErrorContext::handler_details(outputStream* ss, methodOop method) const {
482 if (method != NULL) {
483 streamIndentor si(ss);
484 ExceptionTable table(method);
485 if (table.length() > 0) {
486 ss->indent().print_cr("Exception Handler Table:");
487 streamIndentor si2(ss);
488 for (int i = 0; i < table.length(); ++i) {
489 ss->indent().print_cr("bci [%d, %d] => handler: %d", table.start_pc(i),
490 table.end_pc(i), table.handler_pc(i));
491 }
492 }
493 }
494 }
496 void ErrorContext::stackmap_details(outputStream* ss, methodOop method) const {
497 if (method != NULL && method->has_stackmap_table()) {
498 streamIndentor si(ss);
499 ss->indent().print_cr("Stackmap Table:");
500 typeArrayOop data = method->stackmap_data();
501 stack_map_table* sm_table =
502 stack_map_table::at((address)data->byte_at_addr(0));
503 stack_map_frame* sm_frame = sm_table->entries();
504 streamIndentor si2(ss);
505 int current_offset = -1;
506 for (u2 i = 0; i < sm_table->number_of_entries(); ++i) {
507 ss->indent();
508 sm_frame->print_on(ss, current_offset);
509 ss->print_cr("");
510 current_offset += sm_frame->offset_delta();
511 sm_frame = sm_frame->next();
512 }
513 }
514 }
516 // Methods in ClassVerifier
518 ClassVerifier::ClassVerifier(
519 instanceKlassHandle klass, TRAPS)
520 : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass) {
521 _this_type = VerificationType::reference_type(klass->name());
522 // Create list to hold symbols in reference area.
523 _symbols = new GrowableArray<Symbol*>(100, 0, NULL);
524 }
526 ClassVerifier::~ClassVerifier() {
527 // Decrement the reference count for any symbols created.
528 for (int i = 0; i < _symbols->length(); i++) {
529 Symbol* s = _symbols->at(i);
530 s->decrement_refcount();
531 }
532 }
534 VerificationType ClassVerifier::object_type() const {
535 return VerificationType::reference_type(vmSymbols::java_lang_Object());
536 }
538 TypeOrigin ClassVerifier::ref_ctx(const char* sig, TRAPS) {
539 VerificationType vt = VerificationType::reference_type(
540 create_temporary_symbol(sig, (int)strlen(sig), THREAD));
541 return TypeOrigin::implicit(vt);
542 }
544 void ClassVerifier::verify_class(TRAPS) {
545 if (VerboseVerification) {
546 tty->print_cr("Verifying class %s with new format",
547 _klass->external_name());
548 }
550 objArrayHandle methods(THREAD, _klass->methods());
551 int num_methods = methods->length();
553 for (int index = 0; index < num_methods; index++) {
554 // Check for recursive re-verification before each method.
555 if (was_recursively_verified()) return;
557 methodOop m = (methodOop)methods->obj_at(index);
558 if (m->is_native() || m->is_abstract()) {
559 // If m is native or abstract, skip it. It is checked in class file
560 // parser that methods do not override a final method.
561 continue;
562 }
563 verify_method(methodHandle(THREAD, m), CHECK_VERIFY(this));
564 }
566 if (VerboseVerification || TraceClassInitialization) {
567 if (was_recursively_verified())
568 tty->print_cr("Recursive verification detected for: %s",
569 _klass->external_name());
570 }
571 }
573 void ClassVerifier::verify_method(methodHandle m, TRAPS) {
574 _method = m; // initialize _method
575 if (VerboseVerification) {
576 tty->print_cr("Verifying method %s", m->name_and_sig_as_C_string());
577 }
579 const char* bad_type_msg = "Bad type on operand stack in %s";
581 int32_t max_stack = m->max_stack();
582 int32_t max_locals = m->max_locals();
583 constantPoolHandle cp(THREAD, m->constants());
585 if (!SignatureVerifier::is_valid_method_signature(m->signature())) {
586 class_format_error("Invalid method signature");
587 return;
588 }
590 // Initial stack map frame: offset is 0, stack is initially empty.
591 StackMapFrame current_frame(max_locals, max_stack, this);
592 // Set initial locals
593 VerificationType return_type = current_frame.set_locals_from_arg(
594 m, current_type(), CHECK_VERIFY(this));
596 int32_t stackmap_index = 0; // index to the stackmap array
598 u4 code_length = m->code_size();
600 // Scan the bytecode and map each instruction's start offset to a number.
601 char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this));
603 int ex_min = code_length;
604 int ex_max = -1;
605 // Look through each item on the exception table. Each of the fields must refer
606 // to a legal instruction.
607 verify_exception_handler_table(
608 code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this));
610 // Look through each entry on the local variable table and make sure
611 // its range of code array offsets is valid. (4169817)
612 if (m->has_localvariable_table()) {
613 verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this));
614 }
616 typeArrayHandle stackmap_data(THREAD, m->stackmap_data());
617 StackMapStream stream(stackmap_data);
618 StackMapReader reader(this, &stream, code_data, code_length, THREAD);
619 StackMapTable stackmap_table(&reader, ¤t_frame, max_locals, max_stack,
620 code_data, code_length, CHECK_VERIFY(this));
622 if (VerboseVerification) {
623 stackmap_table.print_on(tty);
624 }
626 RawBytecodeStream bcs(m);
628 // Scan the byte code linearly from the start to the end
629 bool no_control_flow = false; // Set to true when there is no direct control
630 // flow from current instruction to the next
631 // instruction in sequence
632 Bytecodes::Code opcode;
633 while (!bcs.is_last_bytecode()) {
634 // Check for recursive re-verification before each bytecode.
635 if (was_recursively_verified()) return;
637 opcode = bcs.raw_next();
638 u2 bci = bcs.bci();
640 // Set current frame's offset to bci
641 current_frame.set_offset(bci);
642 current_frame.set_mark();
644 // Make sure every offset in stackmap table point to the beginning to
645 // an instruction. Match current_frame to stackmap_table entry with
646 // the same offset if exists.
647 stackmap_index = verify_stackmap_table(
648 stackmap_index, bci, ¤t_frame, &stackmap_table,
649 no_control_flow, CHECK_VERIFY(this));
652 bool this_uninit = false; // Set to true when invokespecial <init> initialized 'this'
654 // Merge with the next instruction
655 {
656 u2 index;
657 int target;
658 VerificationType type, type2;
659 VerificationType atype;
661 #ifndef PRODUCT
662 if (VerboseVerification) {
663 current_frame.print_on(tty);
664 tty->print_cr("offset = %d, opcode = %s", bci, Bytecodes::name(opcode));
665 }
666 #endif
668 // Make sure wide instruction is in correct format
669 if (bcs.is_wide()) {
670 if (opcode != Bytecodes::_iinc && opcode != Bytecodes::_iload &&
671 opcode != Bytecodes::_aload && opcode != Bytecodes::_lload &&
672 opcode != Bytecodes::_istore && opcode != Bytecodes::_astore &&
673 opcode != Bytecodes::_lstore && opcode != Bytecodes::_fload &&
674 opcode != Bytecodes::_dload && opcode != Bytecodes::_fstore &&
675 opcode != Bytecodes::_dstore) {
676 /* Unreachable? RawBytecodeStream's raw_next() returns 'illegal'
677 * if we encounter a wide instruction that modifies an invalid
678 * opcode (not one of the ones listed above) */
679 verify_error(ErrorContext::bad_code(bci), "Bad wide instruction");
680 return;
681 }
682 }
684 switch (opcode) {
685 case Bytecodes::_nop :
686 no_control_flow = false; break;
687 case Bytecodes::_aconst_null :
688 current_frame.push_stack(
689 VerificationType::null_type(), CHECK_VERIFY(this));
690 no_control_flow = false; break;
691 case Bytecodes::_iconst_m1 :
692 case Bytecodes::_iconst_0 :
693 case Bytecodes::_iconst_1 :
694 case Bytecodes::_iconst_2 :
695 case Bytecodes::_iconst_3 :
696 case Bytecodes::_iconst_4 :
697 case Bytecodes::_iconst_5 :
698 current_frame.push_stack(
699 VerificationType::integer_type(), CHECK_VERIFY(this));
700 no_control_flow = false; break;
701 case Bytecodes::_lconst_0 :
702 case Bytecodes::_lconst_1 :
703 current_frame.push_stack_2(
704 VerificationType::long_type(),
705 VerificationType::long2_type(), CHECK_VERIFY(this));
706 no_control_flow = false; break;
707 case Bytecodes::_fconst_0 :
708 case Bytecodes::_fconst_1 :
709 case Bytecodes::_fconst_2 :
710 current_frame.push_stack(
711 VerificationType::float_type(), CHECK_VERIFY(this));
712 no_control_flow = false; break;
713 case Bytecodes::_dconst_0 :
714 case Bytecodes::_dconst_1 :
715 current_frame.push_stack_2(
716 VerificationType::double_type(),
717 VerificationType::double2_type(), CHECK_VERIFY(this));
718 no_control_flow = false; break;
719 case Bytecodes::_sipush :
720 case Bytecodes::_bipush :
721 current_frame.push_stack(
722 VerificationType::integer_type(), CHECK_VERIFY(this));
723 no_control_flow = false; break;
724 case Bytecodes::_ldc :
725 verify_ldc(
726 opcode, bcs.get_index_u1(), ¤t_frame,
727 cp, bci, CHECK_VERIFY(this));
728 no_control_flow = false; break;
729 case Bytecodes::_ldc_w :
730 case Bytecodes::_ldc2_w :
731 verify_ldc(
732 opcode, bcs.get_index_u2(), ¤t_frame,
733 cp, bci, CHECK_VERIFY(this));
734 no_control_flow = false; break;
735 case Bytecodes::_iload :
736 verify_iload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
737 no_control_flow = false; break;
738 case Bytecodes::_iload_0 :
739 case Bytecodes::_iload_1 :
740 case Bytecodes::_iload_2 :
741 case Bytecodes::_iload_3 :
742 index = opcode - Bytecodes::_iload_0;
743 verify_iload(index, ¤t_frame, CHECK_VERIFY(this));
744 no_control_flow = false; break;
745 case Bytecodes::_lload :
746 verify_lload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
747 no_control_flow = false; break;
748 case Bytecodes::_lload_0 :
749 case Bytecodes::_lload_1 :
750 case Bytecodes::_lload_2 :
751 case Bytecodes::_lload_3 :
752 index = opcode - Bytecodes::_lload_0;
753 verify_lload(index, ¤t_frame, CHECK_VERIFY(this));
754 no_control_flow = false; break;
755 case Bytecodes::_fload :
756 verify_fload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
757 no_control_flow = false; break;
758 case Bytecodes::_fload_0 :
759 case Bytecodes::_fload_1 :
760 case Bytecodes::_fload_2 :
761 case Bytecodes::_fload_3 :
762 index = opcode - Bytecodes::_fload_0;
763 verify_fload(index, ¤t_frame, CHECK_VERIFY(this));
764 no_control_flow = false; break;
765 case Bytecodes::_dload :
766 verify_dload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
767 no_control_flow = false; break;
768 case Bytecodes::_dload_0 :
769 case Bytecodes::_dload_1 :
770 case Bytecodes::_dload_2 :
771 case Bytecodes::_dload_3 :
772 index = opcode - Bytecodes::_dload_0;
773 verify_dload(index, ¤t_frame, CHECK_VERIFY(this));
774 no_control_flow = false; break;
775 case Bytecodes::_aload :
776 verify_aload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
777 no_control_flow = false; break;
778 case Bytecodes::_aload_0 :
779 case Bytecodes::_aload_1 :
780 case Bytecodes::_aload_2 :
781 case Bytecodes::_aload_3 :
782 index = opcode - Bytecodes::_aload_0;
783 verify_aload(index, ¤t_frame, CHECK_VERIFY(this));
784 no_control_flow = false; break;
785 case Bytecodes::_iaload :
786 type = current_frame.pop_stack(
787 VerificationType::integer_type(), CHECK_VERIFY(this));
788 atype = current_frame.pop_stack(
789 VerificationType::reference_check(), CHECK_VERIFY(this));
790 if (!atype.is_int_array()) {
791 verify_error(ErrorContext::bad_type(bci,
792 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)),
793 bad_type_msg, "iaload");
794 return;
795 }
796 current_frame.push_stack(
797 VerificationType::integer_type(), CHECK_VERIFY(this));
798 no_control_flow = false; break;
799 case Bytecodes::_baload :
800 type = current_frame.pop_stack(
801 VerificationType::integer_type(), CHECK_VERIFY(this));
802 atype = current_frame.pop_stack(
803 VerificationType::reference_check(), CHECK_VERIFY(this));
804 if (!atype.is_bool_array() && !atype.is_byte_array()) {
805 verify_error(
806 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
807 bad_type_msg, "baload");
808 return;
809 }
810 current_frame.push_stack(
811 VerificationType::integer_type(), CHECK_VERIFY(this));
812 no_control_flow = false; break;
813 case Bytecodes::_caload :
814 type = current_frame.pop_stack(
815 VerificationType::integer_type(), CHECK_VERIFY(this));
816 atype = current_frame.pop_stack(
817 VerificationType::reference_check(), CHECK_VERIFY(this));
818 if (!atype.is_char_array()) {
819 verify_error(ErrorContext::bad_type(bci,
820 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)),
821 bad_type_msg, "caload");
822 return;
823 }
824 current_frame.push_stack(
825 VerificationType::integer_type(), CHECK_VERIFY(this));
826 no_control_flow = false; break;
827 case Bytecodes::_saload :
828 type = current_frame.pop_stack(
829 VerificationType::integer_type(), CHECK_VERIFY(this));
830 atype = current_frame.pop_stack(
831 VerificationType::reference_check(), CHECK_VERIFY(this));
832 if (!atype.is_short_array()) {
833 verify_error(ErrorContext::bad_type(bci,
834 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)),
835 bad_type_msg, "saload");
836 return;
837 }
838 current_frame.push_stack(
839 VerificationType::integer_type(), CHECK_VERIFY(this));
840 no_control_flow = false; break;
841 case Bytecodes::_laload :
842 type = current_frame.pop_stack(
843 VerificationType::integer_type(), CHECK_VERIFY(this));
844 atype = current_frame.pop_stack(
845 VerificationType::reference_check(), CHECK_VERIFY(this));
846 if (!atype.is_long_array()) {
847 verify_error(ErrorContext::bad_type(bci,
848 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)),
849 bad_type_msg, "laload");
850 return;
851 }
852 current_frame.push_stack_2(
853 VerificationType::long_type(),
854 VerificationType::long2_type(), CHECK_VERIFY(this));
855 no_control_flow = false; break;
856 case Bytecodes::_faload :
857 type = current_frame.pop_stack(
858 VerificationType::integer_type(), CHECK_VERIFY(this));
859 atype = current_frame.pop_stack(
860 VerificationType::reference_check(), CHECK_VERIFY(this));
861 if (!atype.is_float_array()) {
862 verify_error(ErrorContext::bad_type(bci,
863 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)),
864 bad_type_msg, "faload");
865 return;
866 }
867 current_frame.push_stack(
868 VerificationType::float_type(), CHECK_VERIFY(this));
869 no_control_flow = false; break;
870 case Bytecodes::_daload :
871 type = current_frame.pop_stack(
872 VerificationType::integer_type(), CHECK_VERIFY(this));
873 atype = current_frame.pop_stack(
874 VerificationType::reference_check(), CHECK_VERIFY(this));
875 if (!atype.is_double_array()) {
876 verify_error(ErrorContext::bad_type(bci,
877 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)),
878 bad_type_msg, "daload");
879 return;
880 }
881 current_frame.push_stack_2(
882 VerificationType::double_type(),
883 VerificationType::double2_type(), CHECK_VERIFY(this));
884 no_control_flow = false; break;
885 case Bytecodes::_aaload : {
886 type = current_frame.pop_stack(
887 VerificationType::integer_type(), CHECK_VERIFY(this));
888 atype = current_frame.pop_stack(
889 VerificationType::reference_check(), CHECK_VERIFY(this));
890 if (!atype.is_reference_array()) {
891 verify_error(ErrorContext::bad_type(bci,
892 current_frame.stack_top_ctx(),
893 TypeOrigin::implicit(VerificationType::reference_check())),
894 bad_type_msg, "aaload");
895 return;
896 }
897 if (atype.is_null()) {
898 current_frame.push_stack(
899 VerificationType::null_type(), CHECK_VERIFY(this));
900 } else {
901 VerificationType component =
902 atype.get_component(this, CHECK_VERIFY(this));
903 current_frame.push_stack(component, CHECK_VERIFY(this));
904 }
905 no_control_flow = false; break;
906 }
907 case Bytecodes::_istore :
908 verify_istore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
909 no_control_flow = false; break;
910 case Bytecodes::_istore_0 :
911 case Bytecodes::_istore_1 :
912 case Bytecodes::_istore_2 :
913 case Bytecodes::_istore_3 :
914 index = opcode - Bytecodes::_istore_0;
915 verify_istore(index, ¤t_frame, CHECK_VERIFY(this));
916 no_control_flow = false; break;
917 case Bytecodes::_lstore :
918 verify_lstore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
919 no_control_flow = false; break;
920 case Bytecodes::_lstore_0 :
921 case Bytecodes::_lstore_1 :
922 case Bytecodes::_lstore_2 :
923 case Bytecodes::_lstore_3 :
924 index = opcode - Bytecodes::_lstore_0;
925 verify_lstore(index, ¤t_frame, CHECK_VERIFY(this));
926 no_control_flow = false; break;
927 case Bytecodes::_fstore :
928 verify_fstore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
929 no_control_flow = false; break;
930 case Bytecodes::_fstore_0 :
931 case Bytecodes::_fstore_1 :
932 case Bytecodes::_fstore_2 :
933 case Bytecodes::_fstore_3 :
934 index = opcode - Bytecodes::_fstore_0;
935 verify_fstore(index, ¤t_frame, CHECK_VERIFY(this));
936 no_control_flow = false; break;
937 case Bytecodes::_dstore :
938 verify_dstore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
939 no_control_flow = false; break;
940 case Bytecodes::_dstore_0 :
941 case Bytecodes::_dstore_1 :
942 case Bytecodes::_dstore_2 :
943 case Bytecodes::_dstore_3 :
944 index = opcode - Bytecodes::_dstore_0;
945 verify_dstore(index, ¤t_frame, CHECK_VERIFY(this));
946 no_control_flow = false; break;
947 case Bytecodes::_astore :
948 verify_astore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
949 no_control_flow = false; break;
950 case Bytecodes::_astore_0 :
951 case Bytecodes::_astore_1 :
952 case Bytecodes::_astore_2 :
953 case Bytecodes::_astore_3 :
954 index = opcode - Bytecodes::_astore_0;
955 verify_astore(index, ¤t_frame, CHECK_VERIFY(this));
956 no_control_flow = false; break;
957 case Bytecodes::_iastore :
958 type = current_frame.pop_stack(
959 VerificationType::integer_type(), CHECK_VERIFY(this));
960 type2 = current_frame.pop_stack(
961 VerificationType::integer_type(), CHECK_VERIFY(this));
962 atype = current_frame.pop_stack(
963 VerificationType::reference_check(), CHECK_VERIFY(this));
964 if (!atype.is_int_array()) {
965 verify_error(ErrorContext::bad_type(bci,
966 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)),
967 bad_type_msg, "iastore");
968 return;
969 }
970 no_control_flow = false; break;
971 case Bytecodes::_bastore :
972 type = current_frame.pop_stack(
973 VerificationType::integer_type(), CHECK_VERIFY(this));
974 type2 = current_frame.pop_stack(
975 VerificationType::integer_type(), CHECK_VERIFY(this));
976 atype = current_frame.pop_stack(
977 VerificationType::reference_check(), CHECK_VERIFY(this));
978 if (!atype.is_bool_array() && !atype.is_byte_array()) {
979 verify_error(
980 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
981 bad_type_msg, "bastore");
982 return;
983 }
984 no_control_flow = false; break;
985 case Bytecodes::_castore :
986 current_frame.pop_stack(
987 VerificationType::integer_type(), CHECK_VERIFY(this));
988 current_frame.pop_stack(
989 VerificationType::integer_type(), CHECK_VERIFY(this));
990 atype = current_frame.pop_stack(
991 VerificationType::reference_check(), CHECK_VERIFY(this));
992 if (!atype.is_char_array()) {
993 verify_error(ErrorContext::bad_type(bci,
994 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)),
995 bad_type_msg, "castore");
996 return;
997 }
998 no_control_flow = false; break;
999 case Bytecodes::_sastore :
1000 current_frame.pop_stack(
1001 VerificationType::integer_type(), CHECK_VERIFY(this));
1002 current_frame.pop_stack(
1003 VerificationType::integer_type(), CHECK_VERIFY(this));
1004 atype = current_frame.pop_stack(
1005 VerificationType::reference_check(), CHECK_VERIFY(this));
1006 if (!atype.is_short_array()) {
1007 verify_error(ErrorContext::bad_type(bci,
1008 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)),
1009 bad_type_msg, "sastore");
1010 return;
1011 }
1012 no_control_flow = false; break;
1013 case Bytecodes::_lastore :
1014 current_frame.pop_stack_2(
1015 VerificationType::long2_type(),
1016 VerificationType::long_type(), CHECK_VERIFY(this));
1017 current_frame.pop_stack(
1018 VerificationType::integer_type(), CHECK_VERIFY(this));
1019 atype = current_frame.pop_stack(
1020 VerificationType::reference_check(), CHECK_VERIFY(this));
1021 if (!atype.is_long_array()) {
1022 verify_error(ErrorContext::bad_type(bci,
1023 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)),
1024 bad_type_msg, "lastore");
1025 return;
1026 }
1027 no_control_flow = false; break;
1028 case Bytecodes::_fastore :
1029 current_frame.pop_stack(
1030 VerificationType::float_type(), CHECK_VERIFY(this));
1031 current_frame.pop_stack
1032 (VerificationType::integer_type(), CHECK_VERIFY(this));
1033 atype = current_frame.pop_stack(
1034 VerificationType::reference_check(), CHECK_VERIFY(this));
1035 if (!atype.is_float_array()) {
1036 verify_error(ErrorContext::bad_type(bci,
1037 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)),
1038 bad_type_msg, "fastore");
1039 return;
1040 }
1041 no_control_flow = false; break;
1042 case Bytecodes::_dastore :
1043 current_frame.pop_stack_2(
1044 VerificationType::double2_type(),
1045 VerificationType::double_type(), CHECK_VERIFY(this));
1046 current_frame.pop_stack(
1047 VerificationType::integer_type(), CHECK_VERIFY(this));
1048 atype = current_frame.pop_stack(
1049 VerificationType::reference_check(), CHECK_VERIFY(this));
1050 if (!atype.is_double_array()) {
1051 verify_error(ErrorContext::bad_type(bci,
1052 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)),
1053 bad_type_msg, "dastore");
1054 return;
1055 }
1056 no_control_flow = false; break;
1057 case Bytecodes::_aastore :
1058 type = current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1059 type2 = current_frame.pop_stack(
1060 VerificationType::integer_type(), CHECK_VERIFY(this));
1061 atype = current_frame.pop_stack(
1062 VerificationType::reference_check(), CHECK_VERIFY(this));
1063 // more type-checking is done at runtime
1064 if (!atype.is_reference_array()) {
1065 verify_error(ErrorContext::bad_type(bci,
1066 current_frame.stack_top_ctx(),
1067 TypeOrigin::implicit(VerificationType::reference_check())),
1068 bad_type_msg, "aastore");
1069 return;
1070 }
1071 // 4938384: relaxed constraint in JVMS 3nd edition.
1072 no_control_flow = false; break;
1073 case Bytecodes::_pop :
1074 current_frame.pop_stack(
1075 VerificationType::category1_check(), CHECK_VERIFY(this));
1076 no_control_flow = false; break;
1077 case Bytecodes::_pop2 :
1078 type = current_frame.pop_stack(CHECK_VERIFY(this));
1079 if (type.is_category1()) {
1080 current_frame.pop_stack(
1081 VerificationType::category1_check(), CHECK_VERIFY(this));
1082 } else if (type.is_category2_2nd()) {
1083 current_frame.pop_stack(
1084 VerificationType::category2_check(), CHECK_VERIFY(this));
1085 } else {
1086 /* Unreachable? Would need a category2_1st on TOS
1087 * which does not appear possible. */
1088 verify_error(
1089 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1090 bad_type_msg, "pop2");
1091 return;
1092 }
1093 no_control_flow = false; break;
1094 case Bytecodes::_dup :
1095 type = current_frame.pop_stack(
1096 VerificationType::category1_check(), CHECK_VERIFY(this));
1097 current_frame.push_stack(type, CHECK_VERIFY(this));
1098 current_frame.push_stack(type, CHECK_VERIFY(this));
1099 no_control_flow = false; break;
1100 case Bytecodes::_dup_x1 :
1101 type = current_frame.pop_stack(
1102 VerificationType::category1_check(), CHECK_VERIFY(this));
1103 type2 = current_frame.pop_stack(
1104 VerificationType::category1_check(), CHECK_VERIFY(this));
1105 current_frame.push_stack(type, CHECK_VERIFY(this));
1106 current_frame.push_stack(type2, CHECK_VERIFY(this));
1107 current_frame.push_stack(type, CHECK_VERIFY(this));
1108 no_control_flow = false; break;
1109 case Bytecodes::_dup_x2 :
1110 {
1111 VerificationType type3;
1112 type = current_frame.pop_stack(
1113 VerificationType::category1_check(), CHECK_VERIFY(this));
1114 type2 = current_frame.pop_stack(CHECK_VERIFY(this));
1115 if (type2.is_category1()) {
1116 type3 = current_frame.pop_stack(
1117 VerificationType::category1_check(), CHECK_VERIFY(this));
1118 } else if (type2.is_category2_2nd()) {
1119 type3 = current_frame.pop_stack(
1120 VerificationType::category2_check(), CHECK_VERIFY(this));
1121 } else {
1122 /* Unreachable? Would need a category2_1st at stack depth 2 with
1123 * a category1 on TOS which does not appear possible. */
1124 verify_error(ErrorContext::bad_type(
1125 bci, current_frame.stack_top_ctx()), bad_type_msg, "dup_x2");
1126 return;
1127 }
1128 current_frame.push_stack(type, CHECK_VERIFY(this));
1129 current_frame.push_stack(type3, CHECK_VERIFY(this));
1130 current_frame.push_stack(type2, CHECK_VERIFY(this));
1131 current_frame.push_stack(type, CHECK_VERIFY(this));
1132 no_control_flow = false; break;
1133 }
1134 case Bytecodes::_dup2 :
1135 type = current_frame.pop_stack(CHECK_VERIFY(this));
1136 if (type.is_category1()) {
1137 type2 = current_frame.pop_stack(
1138 VerificationType::category1_check(), CHECK_VERIFY(this));
1139 } else if (type.is_category2_2nd()) {
1140 type2 = current_frame.pop_stack(
1141 VerificationType::category2_check(), CHECK_VERIFY(this));
1142 } else {
1143 /* Unreachable? Would need a category2_1st on TOS which does not
1144 * appear possible. */
1145 verify_error(
1146 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1147 bad_type_msg, "dup2");
1148 return;
1149 }
1150 current_frame.push_stack(type2, CHECK_VERIFY(this));
1151 current_frame.push_stack(type, CHECK_VERIFY(this));
1152 current_frame.push_stack(type2, CHECK_VERIFY(this));
1153 current_frame.push_stack(type, CHECK_VERIFY(this));
1154 no_control_flow = false; break;
1155 case Bytecodes::_dup2_x1 :
1156 {
1157 VerificationType type3;
1158 type = current_frame.pop_stack(CHECK_VERIFY(this));
1159 if (type.is_category1()) {
1160 type2 = current_frame.pop_stack(
1161 VerificationType::category1_check(), CHECK_VERIFY(this));
1162 } else if (type.is_category2_2nd()) {
1163 type2 = current_frame.pop_stack(
1164 VerificationType::category2_check(), CHECK_VERIFY(this));
1165 } else {
1166 /* Unreachable? Would need a category2_1st on TOS which does
1167 * not appear possible. */
1168 verify_error(
1169 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1170 bad_type_msg, "dup2_x1");
1171 return;
1172 }
1173 type3 = current_frame.pop_stack(
1174 VerificationType::category1_check(), CHECK_VERIFY(this));
1175 current_frame.push_stack(type2, CHECK_VERIFY(this));
1176 current_frame.push_stack(type, CHECK_VERIFY(this));
1177 current_frame.push_stack(type3, CHECK_VERIFY(this));
1178 current_frame.push_stack(type2, CHECK_VERIFY(this));
1179 current_frame.push_stack(type, CHECK_VERIFY(this));
1180 no_control_flow = false; break;
1181 }
1182 case Bytecodes::_dup2_x2 :
1183 {
1184 VerificationType type3, type4;
1185 type = current_frame.pop_stack(CHECK_VERIFY(this));
1186 if (type.is_category1()) {
1187 type2 = current_frame.pop_stack(
1188 VerificationType::category1_check(), CHECK_VERIFY(this));
1189 } else if (type.is_category2_2nd()) {
1190 type2 = current_frame.pop_stack(
1191 VerificationType::category2_check(), CHECK_VERIFY(this));
1192 } else {
1193 /* Unreachable? Would need a category2_1st on TOS which does
1194 * not appear possible. */
1195 verify_error(
1196 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1197 bad_type_msg, "dup2_x2");
1198 return;
1199 }
1200 type3 = current_frame.pop_stack(CHECK_VERIFY(this));
1201 if (type3.is_category1()) {
1202 type4 = current_frame.pop_stack(
1203 VerificationType::category1_check(), CHECK_VERIFY(this));
1204 } else if (type3.is_category2_2nd()) {
1205 type4 = current_frame.pop_stack(
1206 VerificationType::category2_check(), CHECK_VERIFY(this));
1207 } else {
1208 /* Unreachable? Would need a category2_1st on TOS after popping
1209 * a long/double or two category 1's, which does not
1210 * appear possible. */
1211 verify_error(
1212 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()),
1213 bad_type_msg, "dup2_x2");
1214 return;
1215 }
1216 current_frame.push_stack(type2, CHECK_VERIFY(this));
1217 current_frame.push_stack(type, CHECK_VERIFY(this));
1218 current_frame.push_stack(type4, CHECK_VERIFY(this));
1219 current_frame.push_stack(type3, CHECK_VERIFY(this));
1220 current_frame.push_stack(type2, CHECK_VERIFY(this));
1221 current_frame.push_stack(type, CHECK_VERIFY(this));
1222 no_control_flow = false; break;
1223 }
1224 case Bytecodes::_swap :
1225 type = current_frame.pop_stack(
1226 VerificationType::category1_check(), CHECK_VERIFY(this));
1227 type2 = current_frame.pop_stack(
1228 VerificationType::category1_check(), CHECK_VERIFY(this));
1229 current_frame.push_stack(type, CHECK_VERIFY(this));
1230 current_frame.push_stack(type2, CHECK_VERIFY(this));
1231 no_control_flow = false; break;
1232 case Bytecodes::_iadd :
1233 case Bytecodes::_isub :
1234 case Bytecodes::_imul :
1235 case Bytecodes::_idiv :
1236 case Bytecodes::_irem :
1237 case Bytecodes::_ishl :
1238 case Bytecodes::_ishr :
1239 case Bytecodes::_iushr :
1240 case Bytecodes::_ior :
1241 case Bytecodes::_ixor :
1242 case Bytecodes::_iand :
1243 current_frame.pop_stack(
1244 VerificationType::integer_type(), CHECK_VERIFY(this));
1245 // fall through
1246 case Bytecodes::_ineg :
1247 current_frame.pop_stack(
1248 VerificationType::integer_type(), CHECK_VERIFY(this));
1249 current_frame.push_stack(
1250 VerificationType::integer_type(), CHECK_VERIFY(this));
1251 no_control_flow = false; break;
1252 case Bytecodes::_ladd :
1253 case Bytecodes::_lsub :
1254 case Bytecodes::_lmul :
1255 case Bytecodes::_ldiv :
1256 case Bytecodes::_lrem :
1257 case Bytecodes::_land :
1258 case Bytecodes::_lor :
1259 case Bytecodes::_lxor :
1260 current_frame.pop_stack_2(
1261 VerificationType::long2_type(),
1262 VerificationType::long_type(), CHECK_VERIFY(this));
1263 // fall through
1264 case Bytecodes::_lneg :
1265 current_frame.pop_stack_2(
1266 VerificationType::long2_type(),
1267 VerificationType::long_type(), CHECK_VERIFY(this));
1268 current_frame.push_stack_2(
1269 VerificationType::long_type(),
1270 VerificationType::long2_type(), CHECK_VERIFY(this));
1271 no_control_flow = false; break;
1272 case Bytecodes::_lshl :
1273 case Bytecodes::_lshr :
1274 case Bytecodes::_lushr :
1275 current_frame.pop_stack(
1276 VerificationType::integer_type(), CHECK_VERIFY(this));
1277 current_frame.pop_stack_2(
1278 VerificationType::long2_type(),
1279 VerificationType::long_type(), CHECK_VERIFY(this));
1280 current_frame.push_stack_2(
1281 VerificationType::long_type(),
1282 VerificationType::long2_type(), CHECK_VERIFY(this));
1283 no_control_flow = false; break;
1284 case Bytecodes::_fadd :
1285 case Bytecodes::_fsub :
1286 case Bytecodes::_fmul :
1287 case Bytecodes::_fdiv :
1288 case Bytecodes::_frem :
1289 current_frame.pop_stack(
1290 VerificationType::float_type(), CHECK_VERIFY(this));
1291 // fall through
1292 case Bytecodes::_fneg :
1293 current_frame.pop_stack(
1294 VerificationType::float_type(), CHECK_VERIFY(this));
1295 current_frame.push_stack(
1296 VerificationType::float_type(), CHECK_VERIFY(this));
1297 no_control_flow = false; break;
1298 case Bytecodes::_dadd :
1299 case Bytecodes::_dsub :
1300 case Bytecodes::_dmul :
1301 case Bytecodes::_ddiv :
1302 case Bytecodes::_drem :
1303 current_frame.pop_stack_2(
1304 VerificationType::double2_type(),
1305 VerificationType::double_type(), CHECK_VERIFY(this));
1306 // fall through
1307 case Bytecodes::_dneg :
1308 current_frame.pop_stack_2(
1309 VerificationType::double2_type(),
1310 VerificationType::double_type(), CHECK_VERIFY(this));
1311 current_frame.push_stack_2(
1312 VerificationType::double_type(),
1313 VerificationType::double2_type(), CHECK_VERIFY(this));
1314 no_control_flow = false; break;
1315 case Bytecodes::_iinc :
1316 verify_iinc(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this));
1317 no_control_flow = false; break;
1318 case Bytecodes::_i2l :
1319 type = current_frame.pop_stack(
1320 VerificationType::integer_type(), CHECK_VERIFY(this));
1321 current_frame.push_stack_2(
1322 VerificationType::long_type(),
1323 VerificationType::long2_type(), CHECK_VERIFY(this));
1324 no_control_flow = false; break;
1325 case Bytecodes::_l2i :
1326 current_frame.pop_stack_2(
1327 VerificationType::long2_type(),
1328 VerificationType::long_type(), CHECK_VERIFY(this));
1329 current_frame.push_stack(
1330 VerificationType::integer_type(), CHECK_VERIFY(this));
1331 no_control_flow = false; break;
1332 case Bytecodes::_i2f :
1333 current_frame.pop_stack(
1334 VerificationType::integer_type(), CHECK_VERIFY(this));
1335 current_frame.push_stack(
1336 VerificationType::float_type(), CHECK_VERIFY(this));
1337 no_control_flow = false; break;
1338 case Bytecodes::_i2d :
1339 current_frame.pop_stack(
1340 VerificationType::integer_type(), CHECK_VERIFY(this));
1341 current_frame.push_stack_2(
1342 VerificationType::double_type(),
1343 VerificationType::double2_type(), CHECK_VERIFY(this));
1344 no_control_flow = false; break;
1345 case Bytecodes::_l2f :
1346 current_frame.pop_stack_2(
1347 VerificationType::long2_type(),
1348 VerificationType::long_type(), CHECK_VERIFY(this));
1349 current_frame.push_stack(
1350 VerificationType::float_type(), CHECK_VERIFY(this));
1351 no_control_flow = false; break;
1352 case Bytecodes::_l2d :
1353 current_frame.pop_stack_2(
1354 VerificationType::long2_type(),
1355 VerificationType::long_type(), CHECK_VERIFY(this));
1356 current_frame.push_stack_2(
1357 VerificationType::double_type(),
1358 VerificationType::double2_type(), CHECK_VERIFY(this));
1359 no_control_flow = false; break;
1360 case Bytecodes::_f2i :
1361 current_frame.pop_stack(
1362 VerificationType::float_type(), CHECK_VERIFY(this));
1363 current_frame.push_stack(
1364 VerificationType::integer_type(), CHECK_VERIFY(this));
1365 no_control_flow = false; break;
1366 case Bytecodes::_f2l :
1367 current_frame.pop_stack(
1368 VerificationType::float_type(), CHECK_VERIFY(this));
1369 current_frame.push_stack_2(
1370 VerificationType::long_type(),
1371 VerificationType::long2_type(), CHECK_VERIFY(this));
1372 no_control_flow = false; break;
1373 case Bytecodes::_f2d :
1374 current_frame.pop_stack(
1375 VerificationType::float_type(), CHECK_VERIFY(this));
1376 current_frame.push_stack_2(
1377 VerificationType::double_type(),
1378 VerificationType::double2_type(), CHECK_VERIFY(this));
1379 no_control_flow = false; break;
1380 case Bytecodes::_d2i :
1381 current_frame.pop_stack_2(
1382 VerificationType::double2_type(),
1383 VerificationType::double_type(), CHECK_VERIFY(this));
1384 current_frame.push_stack(
1385 VerificationType::integer_type(), CHECK_VERIFY(this));
1386 no_control_flow = false; break;
1387 case Bytecodes::_d2l :
1388 current_frame.pop_stack_2(
1389 VerificationType::double2_type(),
1390 VerificationType::double_type(), CHECK_VERIFY(this));
1391 current_frame.push_stack_2(
1392 VerificationType::long_type(),
1393 VerificationType::long2_type(), CHECK_VERIFY(this));
1394 no_control_flow = false; break;
1395 case Bytecodes::_d2f :
1396 current_frame.pop_stack_2(
1397 VerificationType::double2_type(),
1398 VerificationType::double_type(), CHECK_VERIFY(this));
1399 current_frame.push_stack(
1400 VerificationType::float_type(), CHECK_VERIFY(this));
1401 no_control_flow = false; break;
1402 case Bytecodes::_i2b :
1403 case Bytecodes::_i2c :
1404 case Bytecodes::_i2s :
1405 current_frame.pop_stack(
1406 VerificationType::integer_type(), CHECK_VERIFY(this));
1407 current_frame.push_stack(
1408 VerificationType::integer_type(), CHECK_VERIFY(this));
1409 no_control_flow = false; break;
1410 case Bytecodes::_lcmp :
1411 current_frame.pop_stack_2(
1412 VerificationType::long2_type(),
1413 VerificationType::long_type(), CHECK_VERIFY(this));
1414 current_frame.pop_stack_2(
1415 VerificationType::long2_type(),
1416 VerificationType::long_type(), CHECK_VERIFY(this));
1417 current_frame.push_stack(
1418 VerificationType::integer_type(), CHECK_VERIFY(this));
1419 no_control_flow = false; break;
1420 case Bytecodes::_fcmpl :
1421 case Bytecodes::_fcmpg :
1422 current_frame.pop_stack(
1423 VerificationType::float_type(), CHECK_VERIFY(this));
1424 current_frame.pop_stack(
1425 VerificationType::float_type(), CHECK_VERIFY(this));
1426 current_frame.push_stack(
1427 VerificationType::integer_type(), CHECK_VERIFY(this));
1428 no_control_flow = false; break;
1429 case Bytecodes::_dcmpl :
1430 case Bytecodes::_dcmpg :
1431 current_frame.pop_stack_2(
1432 VerificationType::double2_type(),
1433 VerificationType::double_type(), CHECK_VERIFY(this));
1434 current_frame.pop_stack_2(
1435 VerificationType::double2_type(),
1436 VerificationType::double_type(), CHECK_VERIFY(this));
1437 current_frame.push_stack(
1438 VerificationType::integer_type(), CHECK_VERIFY(this));
1439 no_control_flow = false; break;
1440 case Bytecodes::_if_icmpeq:
1441 case Bytecodes::_if_icmpne:
1442 case Bytecodes::_if_icmplt:
1443 case Bytecodes::_if_icmpge:
1444 case Bytecodes::_if_icmpgt:
1445 case Bytecodes::_if_icmple:
1446 current_frame.pop_stack(
1447 VerificationType::integer_type(), CHECK_VERIFY(this));
1448 // fall through
1449 case Bytecodes::_ifeq:
1450 case Bytecodes::_ifne:
1451 case Bytecodes::_iflt:
1452 case Bytecodes::_ifge:
1453 case Bytecodes::_ifgt:
1454 case Bytecodes::_ifle:
1455 current_frame.pop_stack(
1456 VerificationType::integer_type(), CHECK_VERIFY(this));
1457 target = bcs.dest();
1458 stackmap_table.check_jump_target(
1459 ¤t_frame, target, CHECK_VERIFY(this));
1460 no_control_flow = false; break;
1461 case Bytecodes::_if_acmpeq :
1462 case Bytecodes::_if_acmpne :
1463 current_frame.pop_stack(
1464 VerificationType::reference_check(), CHECK_VERIFY(this));
1465 // fall through
1466 case Bytecodes::_ifnull :
1467 case Bytecodes::_ifnonnull :
1468 current_frame.pop_stack(
1469 VerificationType::reference_check(), CHECK_VERIFY(this));
1470 target = bcs.dest();
1471 stackmap_table.check_jump_target
1472 (¤t_frame, target, CHECK_VERIFY(this));
1473 no_control_flow = false; break;
1474 case Bytecodes::_goto :
1475 target = bcs.dest();
1476 stackmap_table.check_jump_target(
1477 ¤t_frame, target, CHECK_VERIFY(this));
1478 no_control_flow = true; break;
1479 case Bytecodes::_goto_w :
1480 target = bcs.dest_w();
1481 stackmap_table.check_jump_target(
1482 ¤t_frame, target, CHECK_VERIFY(this));
1483 no_control_flow = true; break;
1484 case Bytecodes::_tableswitch :
1485 case Bytecodes::_lookupswitch :
1486 verify_switch(
1487 &bcs, code_length, code_data, ¤t_frame,
1488 &stackmap_table, CHECK_VERIFY(this));
1489 no_control_flow = true; break;
1490 case Bytecodes::_ireturn :
1491 type = current_frame.pop_stack(
1492 VerificationType::integer_type(), CHECK_VERIFY(this));
1493 verify_return_value(return_type, type, bci,
1494 ¤t_frame, CHECK_VERIFY(this));
1495 no_control_flow = true; break;
1496 case Bytecodes::_lreturn :
1497 type2 = current_frame.pop_stack(
1498 VerificationType::long2_type(), CHECK_VERIFY(this));
1499 type = current_frame.pop_stack(
1500 VerificationType::long_type(), CHECK_VERIFY(this));
1501 verify_return_value(return_type, type, bci,
1502 ¤t_frame, CHECK_VERIFY(this));
1503 no_control_flow = true; break;
1504 case Bytecodes::_freturn :
1505 type = current_frame.pop_stack(
1506 VerificationType::float_type(), CHECK_VERIFY(this));
1507 verify_return_value(return_type, type, bci,
1508 ¤t_frame, CHECK_VERIFY(this));
1509 no_control_flow = true; break;
1510 case Bytecodes::_dreturn :
1511 type2 = current_frame.pop_stack(
1512 VerificationType::double2_type(), CHECK_VERIFY(this));
1513 type = current_frame.pop_stack(
1514 VerificationType::double_type(), CHECK_VERIFY(this));
1515 verify_return_value(return_type, type, bci,
1516 ¤t_frame, CHECK_VERIFY(this));
1517 no_control_flow = true; break;
1518 case Bytecodes::_areturn :
1519 type = current_frame.pop_stack(
1520 VerificationType::reference_check(), CHECK_VERIFY(this));
1521 verify_return_value(return_type, type, bci,
1522 ¤t_frame, CHECK_VERIFY(this));
1523 no_control_flow = true; break;
1524 case Bytecodes::_return :
1525 if (return_type != VerificationType::bogus_type()) {
1526 verify_error(ErrorContext::bad_code(bci),
1527 "Method expects a return value");
1528 return;
1529 }
1530 // Make sure "this" has been initialized if current method is an
1531 // <init>
1532 if (_method->name() == vmSymbols::object_initializer_name() &&
1533 current_frame.flag_this_uninit()) {
1534 verify_error(ErrorContext::bad_code(bci),
1535 "Constructor must call super() or this() "
1536 "before return");
1537 return;
1538 }
1539 no_control_flow = true; break;
1540 case Bytecodes::_getstatic :
1541 case Bytecodes::_putstatic :
1542 case Bytecodes::_getfield :
1543 case Bytecodes::_putfield :
1544 verify_field_instructions(
1545 &bcs, ¤t_frame, cp, CHECK_VERIFY(this));
1546 no_control_flow = false; break;
1547 case Bytecodes::_invokevirtual :
1548 case Bytecodes::_invokespecial :
1549 case Bytecodes::_invokestatic :
1550 verify_invoke_instructions(
1551 &bcs, code_length, ¤t_frame,
1552 &this_uninit, return_type, cp, CHECK_VERIFY(this));
1553 no_control_flow = false; break;
1554 case Bytecodes::_invokeinterface :
1555 case Bytecodes::_invokedynamic :
1556 verify_invoke_instructions(
1557 &bcs, code_length, ¤t_frame,
1558 &this_uninit, return_type, cp, CHECK_VERIFY(this));
1559 no_control_flow = false; break;
1560 case Bytecodes::_new :
1561 {
1562 index = bcs.get_index_u2();
1563 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1564 VerificationType new_class_type =
1565 cp_index_to_type(index, cp, CHECK_VERIFY(this));
1566 if (!new_class_type.is_object()) {
1567 verify_error(ErrorContext::bad_type(bci,
1568 TypeOrigin::cp(index, new_class_type)),
1569 "Illegal new instruction");
1570 return;
1571 }
1572 type = VerificationType::uninitialized_type(bci);
1573 current_frame.push_stack(type, CHECK_VERIFY(this));
1574 no_control_flow = false; break;
1575 }
1576 case Bytecodes::_newarray :
1577 type = get_newarray_type(bcs.get_index(), bci, CHECK_VERIFY(this));
1578 current_frame.pop_stack(
1579 VerificationType::integer_type(), CHECK_VERIFY(this));
1580 current_frame.push_stack(type, CHECK_VERIFY(this));
1581 no_control_flow = false; break;
1582 case Bytecodes::_anewarray :
1583 verify_anewarray(
1584 bci, bcs.get_index_u2(), cp, ¤t_frame, CHECK_VERIFY(this));
1585 no_control_flow = false; break;
1586 case Bytecodes::_arraylength :
1587 type = current_frame.pop_stack(
1588 VerificationType::reference_check(), CHECK_VERIFY(this));
1589 if (!(type.is_null() || type.is_array())) {
1590 verify_error(ErrorContext::bad_type(
1591 bci, current_frame.stack_top_ctx()),
1592 bad_type_msg, "arraylength");
1593 }
1594 current_frame.push_stack(
1595 VerificationType::integer_type(), CHECK_VERIFY(this));
1596 no_control_flow = false; break;
1597 case Bytecodes::_checkcast :
1598 {
1599 index = bcs.get_index_u2();
1600 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1601 current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1602 VerificationType klass_type = cp_index_to_type(
1603 index, cp, CHECK_VERIFY(this));
1604 current_frame.push_stack(klass_type, CHECK_VERIFY(this));
1605 no_control_flow = false; break;
1606 }
1607 case Bytecodes::_instanceof : {
1608 index = bcs.get_index_u2();
1609 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1610 current_frame.pop_stack(object_type(), CHECK_VERIFY(this));
1611 current_frame.push_stack(
1612 VerificationType::integer_type(), CHECK_VERIFY(this));
1613 no_control_flow = false; break;
1614 }
1615 case Bytecodes::_monitorenter :
1616 case Bytecodes::_monitorexit :
1617 current_frame.pop_stack(
1618 VerificationType::reference_check(), CHECK_VERIFY(this));
1619 no_control_flow = false; break;
1620 case Bytecodes::_multianewarray :
1621 {
1622 index = bcs.get_index_u2();
1623 u2 dim = *(bcs.bcp()+3);
1624 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
1625 VerificationType new_array_type =
1626 cp_index_to_type(index, cp, CHECK_VERIFY(this));
1627 if (!new_array_type.is_array()) {
1628 verify_error(ErrorContext::bad_type(bci,
1629 TypeOrigin::cp(index, new_array_type)),
1630 "Illegal constant pool index in multianewarray instruction");
1631 return;
1632 }
1633 if (dim < 1 || new_array_type.dimensions() < dim) {
1634 verify_error(ErrorContext::bad_code(bci),
1635 "Illegal dimension in multianewarray instruction: %d", dim);
1636 return;
1637 }
1638 for (int i = 0; i < dim; i++) {
1639 current_frame.pop_stack(
1640 VerificationType::integer_type(), CHECK_VERIFY(this));
1641 }
1642 current_frame.push_stack(new_array_type, CHECK_VERIFY(this));
1643 no_control_flow = false; break;
1644 }
1645 case Bytecodes::_athrow :
1646 type = VerificationType::reference_type(
1647 vmSymbols::java_lang_Throwable());
1648 current_frame.pop_stack(type, CHECK_VERIFY(this));
1649 no_control_flow = true; break;
1650 default:
1651 // We only need to check the valid bytecodes in class file.
1652 // And jsr and ret are not in the new class file format in JDK1.5.
1653 verify_error(ErrorContext::bad_code(bci),
1654 "Bad instruction: %02x", opcode);
1655 no_control_flow = false;
1656 return;
1657 } // end switch
1658 } // end Merge with the next instruction
1660 // Look for possible jump target in exception handlers and see if it
1661 // matches current_frame
1662 if (bci >= ex_min && bci < ex_max) {
1663 verify_exception_handler_targets(
1664 bci, this_uninit, ¤t_frame, &stackmap_table, CHECK_VERIFY(this));
1665 }
1666 } // end while
1668 // Make sure that control flow does not fall through end of the method
1669 if (!no_control_flow) {
1670 verify_error(ErrorContext::bad_code(code_length),
1671 "Control flow falls through code end");
1672 return;
1673 }
1674 }
1676 char* ClassVerifier::generate_code_data(methodHandle m, u4 code_length, TRAPS) {
1677 char* code_data = NEW_RESOURCE_ARRAY(char, code_length);
1678 memset(code_data, 0, sizeof(char) * code_length);
1679 RawBytecodeStream bcs(m);
1681 while (!bcs.is_last_bytecode()) {
1682 if (bcs.raw_next() != Bytecodes::_illegal) {
1683 int bci = bcs.bci();
1684 if (bcs.raw_code() == Bytecodes::_new) {
1685 code_data[bci] = NEW_OFFSET;
1686 } else {
1687 code_data[bci] = BYTECODE_OFFSET;
1688 }
1689 } else {
1690 verify_error(ErrorContext::bad_code(bcs.bci()), "Bad instruction");
1691 return NULL;
1692 }
1693 }
1695 return code_data;
1696 }
1698 void ClassVerifier::verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS) {
1699 ExceptionTable exhandlers(_method());
1700 int exlength = exhandlers.length();
1701 constantPoolHandle cp (THREAD, _method->constants());
1703 for(int i = 0; i < exlength; i++) {
1704 //reacquire the table in case a GC happened
1705 ExceptionTable exhandlers(_method());
1706 u2 start_pc = exhandlers.start_pc(i);
1707 u2 end_pc = exhandlers.end_pc(i);
1708 u2 handler_pc = exhandlers.handler_pc(i);
1709 if (start_pc >= code_length || code_data[start_pc] == 0) {
1710 class_format_error("Illegal exception table start_pc %d", start_pc);
1711 return;
1712 }
1713 if (end_pc != code_length) { // special case: end_pc == code_length
1714 if (end_pc > code_length || code_data[end_pc] == 0) {
1715 class_format_error("Illegal exception table end_pc %d", end_pc);
1716 return;
1717 }
1718 }
1719 if (handler_pc >= code_length || code_data[handler_pc] == 0) {
1720 class_format_error("Illegal exception table handler_pc %d", handler_pc);
1721 return;
1722 }
1723 int catch_type_index = exhandlers.catch_type_index(i);
1724 if (catch_type_index != 0) {
1725 VerificationType catch_type = cp_index_to_type(
1726 catch_type_index, cp, CHECK_VERIFY(this));
1727 VerificationType throwable =
1728 VerificationType::reference_type(vmSymbols::java_lang_Throwable());
1729 bool is_subclass = throwable.is_assignable_from(
1730 catch_type, this, CHECK_VERIFY(this));
1731 if (!is_subclass) {
1732 // 4286534: should throw VerifyError according to recent spec change
1733 verify_error(ErrorContext::bad_type(handler_pc,
1734 TypeOrigin::cp(catch_type_index, catch_type),
1735 TypeOrigin::implicit(throwable)),
1736 "Catch type is not a subclass "
1737 "of Throwable in exception handler %d", handler_pc);
1738 return;
1739 }
1740 }
1741 if (start_pc < min) min = start_pc;
1742 if (end_pc > max) max = end_pc;
1743 }
1744 }
1746 void ClassVerifier::verify_local_variable_table(u4 code_length, char* code_data, TRAPS) {
1747 int localvariable_table_length = _method()->localvariable_table_length();
1748 if (localvariable_table_length > 0) {
1749 LocalVariableTableElement* table = _method()->localvariable_table_start();
1750 for (int i = 0; i < localvariable_table_length; i++) {
1751 u2 start_bci = table[i].start_bci;
1752 u2 length = table[i].length;
1754 if (start_bci >= code_length || code_data[start_bci] == 0) {
1755 class_format_error(
1756 "Illegal local variable table start_pc %d", start_bci);
1757 return;
1758 }
1759 u4 end_bci = (u4)(start_bci + length);
1760 if (end_bci != code_length) {
1761 if (end_bci >= code_length || code_data[end_bci] == 0) {
1762 class_format_error( "Illegal local variable table length %d", length);
1763 return;
1764 }
1765 }
1766 }
1767 }
1768 }
1770 u2 ClassVerifier::verify_stackmap_table(u2 stackmap_index, u2 bci,
1771 StackMapFrame* current_frame,
1772 StackMapTable* stackmap_table,
1773 bool no_control_flow, TRAPS) {
1774 if (stackmap_index < stackmap_table->get_frame_count()) {
1775 u2 this_offset = stackmap_table->get_offset(stackmap_index);
1776 if (no_control_flow && this_offset > bci) {
1777 verify_error(ErrorContext::missing_stackmap(bci),
1778 "Expecting a stack map frame");
1779 return 0;
1780 }
1781 if (this_offset == bci) {
1782 ErrorContext ctx;
1783 // See if current stack map can be assigned to the frame in table.
1784 // current_frame is the stackmap frame got from the last instruction.
1785 // If matched, current_frame will be updated by this method.
1786 bool matches = stackmap_table->match_stackmap(
1787 current_frame, this_offset, stackmap_index,
1788 !no_control_flow, true, &ctx, CHECK_VERIFY_(this, 0));
1789 if (!matches) {
1790 // report type error
1791 verify_error(ctx, "Instruction type does not match stack map");
1792 return 0;
1793 }
1794 stackmap_index++;
1795 } else if (this_offset < bci) {
1796 // current_offset should have met this_offset.
1797 class_format_error("Bad stack map offset %d", this_offset);
1798 return 0;
1799 }
1800 } else if (no_control_flow) {
1801 verify_error(ErrorContext::bad_code(bci), "Expecting a stack map frame");
1802 return 0;
1803 }
1804 return stackmap_index;
1805 }
1807 void ClassVerifier::verify_exception_handler_targets(u2 bci, bool this_uninit, StackMapFrame* current_frame,
1808 StackMapTable* stackmap_table, TRAPS) {
1809 constantPoolHandle cp (THREAD, _method->constants());
1810 ExceptionTable exhandlers(_method());
1811 int exlength = exhandlers.length();
1812 for(int i = 0; i < exlength; i++) {
1813 //reacquire the table in case a GC happened
1814 ExceptionTable exhandlers(_method());
1815 u2 start_pc = exhandlers.start_pc(i);
1816 u2 end_pc = exhandlers.end_pc(i);
1817 u2 handler_pc = exhandlers.handler_pc(i);
1818 int catch_type_index = exhandlers.catch_type_index(i);
1819 if(bci >= start_pc && bci < end_pc) {
1820 u1 flags = current_frame->flags();
1821 if (this_uninit) { flags |= FLAG_THIS_UNINIT; }
1822 StackMapFrame* new_frame = current_frame->frame_in_exception_handler(flags);
1823 if (catch_type_index != 0) {
1824 // We know that this index refers to a subclass of Throwable
1825 VerificationType catch_type = cp_index_to_type(
1826 catch_type_index, cp, CHECK_VERIFY(this));
1827 new_frame->push_stack(catch_type, CHECK_VERIFY(this));
1828 } else {
1829 VerificationType throwable =
1830 VerificationType::reference_type(vmSymbols::java_lang_Throwable());
1831 new_frame->push_stack(throwable, CHECK_VERIFY(this));
1832 }
1833 ErrorContext ctx;
1834 bool matches = stackmap_table->match_stackmap(
1835 new_frame, handler_pc, true, false, &ctx, CHECK_VERIFY(this));
1836 if (!matches) {
1837 verify_error(ctx, "Stack map does not match the one at "
1838 "exception handler %d", handler_pc);
1839 return;
1840 }
1841 }
1842 }
1843 }
1845 void ClassVerifier::verify_cp_index(
1846 u2 bci, constantPoolHandle cp, int index, TRAPS) {
1847 int nconstants = cp->length();
1848 if ((index <= 0) || (index >= nconstants)) {
1849 verify_error(ErrorContext::bad_cp_index(bci, index),
1850 "Illegal constant pool index %d in class %s",
1851 index, instanceKlass::cast(cp->pool_holder())->external_name());
1852 return;
1853 }
1854 }
1856 void ClassVerifier::verify_cp_type(
1857 u2 bci, int index, constantPoolHandle cp, unsigned int types, TRAPS) {
1859 // In some situations, bytecode rewriting may occur while we're verifying.
1860 // In this case, a constant pool cache exists and some indices refer to that
1861 // instead. Be sure we don't pick up such indices by accident.
1862 // We must check was_recursively_verified() before we get here.
1863 guarantee(cp->cache() == NULL, "not rewritten yet");
1865 verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
1866 unsigned int tag = cp->tag_at(index).value();
1867 if ((types & (1 << tag)) == 0) {
1868 verify_error(ErrorContext::bad_cp_index(bci, index),
1869 "Illegal type at constant pool entry %d in class %s",
1870 index, instanceKlass::cast(cp->pool_holder())->external_name());
1871 return;
1872 }
1873 }
1875 void ClassVerifier::verify_cp_class_type(
1876 u2 bci, int index, constantPoolHandle cp, TRAPS) {
1877 verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
1878 constantTag tag = cp->tag_at(index);
1879 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
1880 verify_error(ErrorContext::bad_cp_index(bci, index),
1881 "Illegal type at constant pool entry %d in class %s",
1882 index, instanceKlass::cast(cp->pool_holder())->external_name());
1883 return;
1884 }
1885 }
1887 void ClassVerifier::verify_error(ErrorContext ctx, const char* msg, ...) {
1888 stringStream ss;
1890 ctx.reset_frames();
1891 _exception_type = vmSymbols::java_lang_VerifyError();
1892 _error_context = ctx;
1893 va_list va;
1894 va_start(va, msg);
1895 ss.vprint(msg, va);
1896 va_end(va);
1897 _message = ss.as_string();
1898 #ifdef ASSERT
1899 ResourceMark rm;
1900 const char* exception_name = _exception_type->as_C_string();
1901 Exceptions::debug_check_abort(exception_name, NULL);
1902 #endif // ndef ASSERT
1903 }
1905 void ClassVerifier::class_format_error(const char* msg, ...) {
1906 stringStream ss;
1907 _exception_type = vmSymbols::java_lang_ClassFormatError();
1908 va_list va;
1909 va_start(va, msg);
1910 ss.vprint(msg, va);
1911 va_end(va);
1912 if (!_method.is_null()) {
1913 ss.print(" in method %s", _method->name_and_sig_as_C_string());
1914 }
1915 _message = ss.as_string();
1916 }
1918 klassOop ClassVerifier::load_class(Symbol* name, TRAPS) {
1919 // Get current loader and protection domain first.
1920 oop loader = current_class()->class_loader();
1921 oop protection_domain = current_class()->protection_domain();
1923 return SystemDictionary::resolve_or_fail(
1924 name, Handle(THREAD, loader), Handle(THREAD, protection_domain),
1925 true, CHECK_NULL);
1926 }
1928 bool ClassVerifier::is_protected_access(instanceKlassHandle this_class,
1929 klassOop target_class,
1930 Symbol* field_name,
1931 Symbol* field_sig,
1932 bool is_method) {
1933 No_Safepoint_Verifier nosafepoint;
1935 // If target class isn't a super class of this class, we don't worry about this case
1936 if (!this_class->is_subclass_of(target_class)) {
1937 return false;
1938 }
1939 // Check if the specified method or field is protected
1940 instanceKlass* target_instance = instanceKlass::cast(target_class);
1941 fieldDescriptor fd;
1942 if (is_method) {
1943 methodOop m = target_instance->uncached_lookup_method(field_name, field_sig);
1944 if (m != NULL && m->is_protected()) {
1945 if (!this_class->is_same_class_package(m->method_holder())) {
1946 return true;
1947 }
1948 }
1949 } else {
1950 klassOop member_klass = target_instance->find_field(field_name, field_sig, &fd);
1951 if (member_klass != NULL && fd.is_protected()) {
1952 if (!this_class->is_same_class_package(member_klass)) {
1953 return true;
1954 }
1955 }
1956 }
1957 return false;
1958 }
1960 void ClassVerifier::verify_ldc(
1961 int opcode, u2 index, StackMapFrame* current_frame,
1962 constantPoolHandle cp, u2 bci, TRAPS) {
1963 verify_cp_index(bci, cp, index, CHECK_VERIFY(this));
1964 constantTag tag = cp->tag_at(index);
1965 unsigned int types;
1966 if (opcode == Bytecodes::_ldc || opcode == Bytecodes::_ldc_w) {
1967 if (!tag.is_unresolved_string() && !tag.is_unresolved_klass()) {
1968 types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float)
1969 | (1 << JVM_CONSTANT_String) | (1 << JVM_CONSTANT_Class)
1970 | (1 << JVM_CONSTANT_MethodHandle) | (1 << JVM_CONSTANT_MethodType);
1971 // Note: The class file parser already verified the legality of
1972 // MethodHandle and MethodType constants.
1973 verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
1974 }
1975 } else {
1976 assert(opcode == Bytecodes::_ldc2_w, "must be ldc2_w");
1977 types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long);
1978 verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this));
1979 }
1980 if (tag.is_string() && cp->is_pseudo_string_at(index)) {
1981 current_frame->push_stack(object_type(), CHECK_VERIFY(this));
1982 } else if (tag.is_string() || tag.is_unresolved_string()) {
1983 current_frame->push_stack(
1984 VerificationType::reference_type(
1985 vmSymbols::java_lang_String()), CHECK_VERIFY(this));
1986 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1987 current_frame->push_stack(
1988 VerificationType::reference_type(
1989 vmSymbols::java_lang_Class()), CHECK_VERIFY(this));
1990 } else if (tag.is_int()) {
1991 current_frame->push_stack(
1992 VerificationType::integer_type(), CHECK_VERIFY(this));
1993 } else if (tag.is_float()) {
1994 current_frame->push_stack(
1995 VerificationType::float_type(), CHECK_VERIFY(this));
1996 } else if (tag.is_double()) {
1997 current_frame->push_stack_2(
1998 VerificationType::double_type(),
1999 VerificationType::double2_type(), CHECK_VERIFY(this));
2000 } else if (tag.is_long()) {
2001 current_frame->push_stack_2(
2002 VerificationType::long_type(),
2003 VerificationType::long2_type(), CHECK_VERIFY(this));
2004 } else if (tag.is_method_handle()) {
2005 current_frame->push_stack(
2006 VerificationType::reference_type(
2007 vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this));
2008 } else if (tag.is_method_type()) {
2009 current_frame->push_stack(
2010 VerificationType::reference_type(
2011 vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this));
2012 } else {
2013 /* Unreachable? verify_cp_type has already validated the cp type. */
2014 verify_error(
2015 ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc");
2016 return;
2017 }
2018 }
2020 void ClassVerifier::verify_switch(
2021 RawBytecodeStream* bcs, u4 code_length, char* code_data,
2022 StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS) {
2023 int bci = bcs->bci();
2024 address bcp = bcs->bcp();
2025 address aligned_bcp = (address) round_to((intptr_t)(bcp + 1), jintSize);
2027 // 4639449 & 4647081: padding bytes must be 0
2028 u2 padding_offset = 1;
2029 while ((bcp + padding_offset) < aligned_bcp) {
2030 if(*(bcp + padding_offset) != 0) {
2031 verify_error(ErrorContext::bad_code(bci),
2032 "Nonzero padding byte in lookswitch or tableswitch");
2033 return;
2034 }
2035 padding_offset++;
2036 }
2037 int default_offset = (int) Bytes::get_Java_u4(aligned_bcp);
2038 int keys, delta;
2039 current_frame->pop_stack(
2040 VerificationType::integer_type(), CHECK_VERIFY(this));
2041 if (bcs->raw_code() == Bytecodes::_tableswitch) {
2042 jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize);
2043 jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize);
2044 if (low > high) {
2045 verify_error(ErrorContext::bad_code(bci),
2046 "low must be less than or equal to high in tableswitch");
2047 return;
2048 }
2049 keys = high - low + 1;
2050 if (keys < 0) {
2051 verify_error(ErrorContext::bad_code(bci), "too many keys in tableswitch");
2052 return;
2053 }
2054 delta = 1;
2055 } else {
2056 keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
2057 if (keys < 0) {
2058 verify_error(ErrorContext::bad_code(bci),
2059 "number of keys in lookupswitch less than 0");
2060 return;
2061 }
2062 delta = 2;
2063 // Make sure that the lookupswitch items are sorted
2064 for (int i = 0; i < (keys - 1); i++) {
2065 jint this_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i)*jintSize);
2066 jint next_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i+2)*jintSize);
2067 if (this_key >= next_key) {
2068 verify_error(ErrorContext::bad_code(bci),
2069 "Bad lookupswitch instruction");
2070 return;
2071 }
2072 }
2073 }
2074 int target = bci + default_offset;
2075 stackmap_table->check_jump_target(current_frame, target, CHECK_VERIFY(this));
2076 for (int i = 0; i < keys; i++) {
2077 // Because check_jump_target() may safepoint, the bytecode could have
2078 // moved, which means 'aligned_bcp' is no good and needs to be recalculated.
2079 aligned_bcp = (address)round_to((intptr_t)(bcs->bcp() + 1), jintSize);
2080 target = bci + (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize);
2081 stackmap_table->check_jump_target(
2082 current_frame, target, CHECK_VERIFY(this));
2083 }
2084 NOT_PRODUCT(aligned_bcp = NULL); // no longer valid at this point
2085 }
2087 bool ClassVerifier::name_in_supers(
2088 Symbol* ref_name, instanceKlassHandle current) {
2089 klassOop super = current->super();
2090 while (super != NULL) {
2091 if (super->klass_part()->name() == ref_name) {
2092 return true;
2093 }
2094 super = super->klass_part()->super();
2095 }
2096 return false;
2097 }
2099 void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs,
2100 StackMapFrame* current_frame,
2101 constantPoolHandle cp,
2102 TRAPS) {
2103 u2 index = bcs->get_index_u2();
2104 verify_cp_type(bcs->bci(), index, cp,
2105 1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this));
2107 // Get field name and signature
2108 Symbol* field_name = cp->name_ref_at(index);
2109 Symbol* field_sig = cp->signature_ref_at(index);
2111 if (!SignatureVerifier::is_valid_type_signature(field_sig)) {
2112 class_format_error(
2113 "Invalid signature for field in class %s referenced "
2114 "from constant pool index %d", _klass->external_name(), index);
2115 return;
2116 }
2118 // Get referenced class type
2119 VerificationType ref_class_type = cp_ref_index_to_type(
2120 index, cp, CHECK_VERIFY(this));
2121 if (!ref_class_type.is_object()) {
2122 /* Unreachable? Class file parser verifies Fieldref contents */
2123 verify_error(ErrorContext::bad_type(bcs->bci(),
2124 TypeOrigin::cp(index, ref_class_type)),
2125 "Expecting reference to class in class %s at constant pool index %d",
2126 _klass->external_name(), index);
2127 return;
2128 }
2129 VerificationType target_class_type = ref_class_type;
2131 assert(sizeof(VerificationType) == sizeof(uintptr_t),
2132 "buffer type must match VerificationType size");
2133 uintptr_t field_type_buffer[2];
2134 VerificationType* field_type = (VerificationType*)field_type_buffer;
2135 // If we make a VerificationType[2] array directly, the compiler calls
2136 // to the c-runtime library to do the allocation instead of just
2137 // stack allocating it. Plus it would run constructors. This shows up
2138 // in performance profiles.
2140 SignatureStream sig_stream(field_sig, false);
2141 VerificationType stack_object_type;
2142 int n = change_sig_to_verificationType(
2143 &sig_stream, field_type, CHECK_VERIFY(this));
2144 u2 bci = bcs->bci();
2145 bool is_assignable;
2146 switch (bcs->raw_code()) {
2147 case Bytecodes::_getstatic: {
2148 for (int i = 0; i < n; i++) {
2149 current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2150 }
2151 break;
2152 }
2153 case Bytecodes::_putstatic: {
2154 for (int i = n - 1; i >= 0; i--) {
2155 current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2156 }
2157 break;
2158 }
2159 case Bytecodes::_getfield: {
2160 stack_object_type = current_frame->pop_stack(
2161 target_class_type, CHECK_VERIFY(this));
2162 for (int i = 0; i < n; i++) {
2163 current_frame->push_stack(field_type[i], CHECK_VERIFY(this));
2164 }
2165 goto check_protected;
2166 }
2167 case Bytecodes::_putfield: {
2168 for (int i = n - 1; i >= 0; i--) {
2169 current_frame->pop_stack(field_type[i], CHECK_VERIFY(this));
2170 }
2171 stack_object_type = current_frame->pop_stack(CHECK_VERIFY(this));
2173 // The JVMS 2nd edition allows field initialization before the superclass
2174 // initializer, if the field is defined within the current class.
2175 fieldDescriptor fd;
2176 if (stack_object_type == VerificationType::uninitialized_this_type() &&
2177 target_class_type.equals(current_type()) &&
2178 _klass->find_local_field(field_name, field_sig, &fd)) {
2179 stack_object_type = current_type();
2180 }
2181 is_assignable = target_class_type.is_assignable_from(
2182 stack_object_type, this, CHECK_VERIFY(this));
2183 if (!is_assignable) {
2184 verify_error(ErrorContext::bad_type(bci,
2185 current_frame->stack_top_ctx(),
2186 TypeOrigin::cp(index, target_class_type)),
2187 "Bad type on operand stack in putfield");
2188 return;
2189 }
2190 }
2191 check_protected: {
2192 if (_this_type == stack_object_type)
2193 break; // stack_object_type must be assignable to _current_class_type
2194 Symbol* ref_class_name =
2195 cp->klass_name_at(cp->klass_ref_index_at(index));
2196 if (!name_in_supers(ref_class_name, current_class()))
2197 // stack_object_type must be assignable to _current_class_type since:
2198 // 1. stack_object_type must be assignable to ref_class.
2199 // 2. ref_class must be _current_class or a subclass of it. It can't
2200 // be a superclass of it. See revised JVMS 5.4.4.
2201 break;
2203 klassOop ref_class_oop = load_class(ref_class_name, CHECK);
2204 if (is_protected_access(current_class(), ref_class_oop, field_name,
2205 field_sig, false)) {
2206 // It's protected access, check if stack object is assignable to
2207 // current class.
2208 is_assignable = current_type().is_assignable_from(
2209 stack_object_type, this, CHECK_VERIFY(this));
2210 if (!is_assignable) {
2211 verify_error(ErrorContext::bad_type(bci,
2212 current_frame->stack_top_ctx(),
2213 TypeOrigin::implicit(current_type())),
2214 "Bad access to protected data in getfield");
2215 return;
2216 }
2217 }
2218 break;
2219 }
2220 default: ShouldNotReachHere();
2221 }
2222 }
2224 void ClassVerifier::verify_invoke_init(
2225 RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type,
2226 StackMapFrame* current_frame, u4 code_length, bool *this_uninit,
2227 constantPoolHandle cp, TRAPS) {
2228 u2 bci = bcs->bci();
2229 VerificationType type = current_frame->pop_stack(
2230 VerificationType::reference_check(), CHECK_VERIFY(this));
2231 if (type == VerificationType::uninitialized_this_type()) {
2232 // The method must be an <init> method of this class or its superclass
2233 klassOop superk = current_class()->super();
2234 if (ref_class_type.name() != current_class()->name() &&
2235 ref_class_type.name() != superk->klass_part()->name()) {
2236 verify_error(ErrorContext::bad_type(bci,
2237 TypeOrigin::implicit(ref_class_type),
2238 TypeOrigin::implicit(current_type())),
2239 "Bad <init> method call");
2240 return;
2241 }
2242 current_frame->initialize_object(type, current_type());
2243 *this_uninit = true;
2244 } else if (type.is_uninitialized()) {
2245 u2 new_offset = type.bci();
2246 address new_bcp = bcs->bcp() - bci + new_offset;
2247 if (new_offset > (code_length - 3) || (*new_bcp) != Bytecodes::_new) {
2248 /* Unreachable? Stack map parsing ensures valid type and new
2249 * instructions have a valid BCI. */
2250 verify_error(ErrorContext::bad_code(new_offset),
2251 "Expecting new instruction");
2252 return;
2253 }
2254 u2 new_class_index = Bytes::get_Java_u2(new_bcp + 1);
2255 verify_cp_class_type(bci, new_class_index, cp, CHECK_VERIFY(this));
2257 // The method must be an <init> method of the indicated class
2258 VerificationType new_class_type = cp_index_to_type(
2259 new_class_index, cp, CHECK_VERIFY(this));
2260 if (!new_class_type.equals(ref_class_type)) {
2261 verify_error(ErrorContext::bad_type(bci,
2262 TypeOrigin::cp(new_class_index, new_class_type),
2263 TypeOrigin::cp(ref_class_index, ref_class_type)),
2264 "Call to wrong <init> method");
2265 return;
2266 }
2267 // According to the VM spec, if the referent class is a superclass of the
2268 // current class, and is in a different runtime package, and the method is
2269 // protected, then the objectref must be the current class or a subclass
2270 // of the current class.
2271 VerificationType objectref_type = new_class_type;
2272 if (name_in_supers(ref_class_type.name(), current_class())) {
2273 klassOop ref_klass = load_class(
2274 ref_class_type.name(), CHECK_VERIFY(this));
2275 methodOop m = instanceKlass::cast(ref_klass)->uncached_lookup_method(
2276 vmSymbols::object_initializer_name(),
2277 cp->signature_ref_at(bcs->get_index_u2()));
2278 instanceKlassHandle mh(THREAD, m->method_holder());
2279 if (m->is_protected() && !mh->is_same_class_package(_klass())) {
2280 bool assignable = current_type().is_assignable_from(
2281 objectref_type, this, CHECK_VERIFY(this));
2282 if (!assignable) {
2283 verify_error(ErrorContext::bad_type(bci,
2284 TypeOrigin::cp(new_class_index, objectref_type),
2285 TypeOrigin::implicit(current_type())),
2286 "Bad access to protected <init> method");
2287 return;
2288 }
2289 }
2290 }
2291 current_frame->initialize_object(type, new_class_type);
2292 } else {
2293 verify_error(ErrorContext::bad_type(bci, current_frame->stack_top_ctx()),
2294 "Bad operand type when invoking <init>");
2295 return;
2296 }
2297 }
2299 void ClassVerifier::verify_invoke_instructions(
2300 RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
2301 bool *this_uninit, VerificationType return_type,
2302 constantPoolHandle cp, TRAPS) {
2303 // Make sure the constant pool item is the right type
2304 u2 index = bcs->get_index_u2();
2305 Bytecodes::Code opcode = bcs->raw_code();
2306 unsigned int types = (opcode == Bytecodes::_invokeinterface
2307 ? 1 << JVM_CONSTANT_InterfaceMethodref
2308 : opcode == Bytecodes::_invokedynamic
2309 ? 1 << JVM_CONSTANT_InvokeDynamic
2310 : 1 << JVM_CONSTANT_Methodref);
2311 verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this));
2313 // Get method name and signature
2314 Symbol* method_name = cp->name_ref_at(index);
2315 Symbol* method_sig = cp->signature_ref_at(index);
2317 if (!SignatureVerifier::is_valid_method_signature(method_sig)) {
2318 class_format_error(
2319 "Invalid method signature in class %s referenced "
2320 "from constant pool index %d", _klass->external_name(), index);
2321 return;
2322 }
2324 // Get referenced class type
2325 VerificationType ref_class_type;
2326 if (opcode == Bytecodes::_invokedynamic) {
2327 if (!EnableInvokeDynamic ||
2328 _klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2329 class_format_error(
2330 (!EnableInvokeDynamic ?
2331 "invokedynamic instructions not enabled in this JVM" :
2332 "invokedynamic instructions not supported by this class file version"),
2333 _klass->external_name());
2334 return;
2335 }
2336 } else {
2337 ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this));
2338 }
2340 // For a small signature length, we just allocate 128 bytes instead
2341 // of parsing the signature once to find its size.
2342 // -3 is for '(', ')' and return descriptor; multiply by 2 is for
2343 // longs/doubles to be consertive.
2344 assert(sizeof(VerificationType) == sizeof(uintptr_t),
2345 "buffer type must match VerificationType size");
2346 uintptr_t on_stack_sig_types_buffer[128];
2347 // If we make a VerificationType[128] array directly, the compiler calls
2348 // to the c-runtime library to do the allocation instead of just
2349 // stack allocating it. Plus it would run constructors. This shows up
2350 // in performance profiles.
2352 VerificationType* sig_types;
2353 int size = (method_sig->utf8_length() - 3) * 2;
2354 if (size > 128) {
2355 // Long and double occupies two slots here.
2356 ArgumentSizeComputer size_it(method_sig);
2357 size = size_it.size();
2358 sig_types = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, size);
2359 } else{
2360 sig_types = (VerificationType*)on_stack_sig_types_buffer;
2361 }
2362 SignatureStream sig_stream(method_sig);
2363 int sig_i = 0;
2364 while (!sig_stream.at_return_type()) {
2365 sig_i += change_sig_to_verificationType(
2366 &sig_stream, &sig_types[sig_i], CHECK_VERIFY(this));
2367 sig_stream.next();
2368 }
2369 int nargs = sig_i;
2371 #ifdef ASSERT
2372 {
2373 ArgumentSizeComputer size_it(method_sig);
2374 assert(nargs == size_it.size(), "Argument sizes do not match");
2375 assert(nargs <= (method_sig->utf8_length() - 3) * 2, "estimate of max size isn't conservative enough");
2376 }
2377 #endif
2379 // Check instruction operands
2380 u2 bci = bcs->bci();
2381 if (opcode == Bytecodes::_invokeinterface) {
2382 address bcp = bcs->bcp();
2383 // 4905268: count operand in invokeinterface should be nargs+1, not nargs.
2384 // JSR202 spec: The count operand of an invokeinterface instruction is valid if it is
2385 // the difference between the size of the operand stack before and after the instruction
2386 // executes.
2387 if (*(bcp+3) != (nargs+1)) {
2388 verify_error(ErrorContext::bad_code(bci),
2389 "Inconsistent args count operand in invokeinterface");
2390 return;
2391 }
2392 if (*(bcp+4) != 0) {
2393 verify_error(ErrorContext::bad_code(bci),
2394 "Fourth operand byte of invokeinterface must be zero");
2395 return;
2396 }
2397 }
2399 if (opcode == Bytecodes::_invokedynamic) {
2400 address bcp = bcs->bcp();
2401 if (*(bcp+3) != 0 || *(bcp+4) != 0) {
2402 verify_error(ErrorContext::bad_code(bci),
2403 "Third and fourth operand bytes of invokedynamic must be zero");
2404 return;
2405 }
2406 }
2408 if (method_name->byte_at(0) == '<') {
2409 // Make sure <init> can only be invoked by invokespecial
2410 if (opcode != Bytecodes::_invokespecial ||
2411 method_name != vmSymbols::object_initializer_name()) {
2412 verify_error(ErrorContext::bad_code(bci),
2413 "Illegal call to internal method");
2414 return;
2415 }
2416 } else if (opcode == Bytecodes::_invokespecial
2417 && !ref_class_type.equals(current_type())
2418 && !ref_class_type.equals(VerificationType::reference_type(
2419 current_class()->super()->klass_part()->name()))) {
2420 bool subtype = ref_class_type.is_assignable_from(
2421 current_type(), this, CHECK_VERIFY(this));
2422 if (!subtype) {
2423 verify_error(ErrorContext::bad_code(bci),
2424 "Bad invokespecial instruction: "
2425 "current class isn't assignable to reference class.");
2426 return;
2427 }
2428 }
2429 // Match method descriptor with operand stack
2430 for (int i = nargs - 1; i >= 0; i--) { // Run backwards
2431 current_frame->pop_stack(sig_types[i], CHECK_VERIFY(this));
2432 }
2433 // Check objectref on operand stack
2434 if (opcode != Bytecodes::_invokestatic &&
2435 opcode != Bytecodes::_invokedynamic) {
2436 if (method_name == vmSymbols::object_initializer_name()) { // <init> method
2437 verify_invoke_init(bcs, index, ref_class_type, current_frame,
2438 code_length, this_uninit, cp, CHECK_VERIFY(this));
2439 } else { // other methods
2440 // Ensures that target class is assignable to method class.
2441 if (opcode == Bytecodes::_invokespecial) {
2442 current_frame->pop_stack(current_type(), CHECK_VERIFY(this));
2443 } else if (opcode == Bytecodes::_invokevirtual) {
2444 VerificationType stack_object_type =
2445 current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
2446 if (current_type() != stack_object_type) {
2447 assert(cp->cache() == NULL, "not rewritten yet");
2448 Symbol* ref_class_name =
2449 cp->klass_name_at(cp->klass_ref_index_at(index));
2450 // See the comments in verify_field_instructions() for
2451 // the rationale behind this.
2452 if (name_in_supers(ref_class_name, current_class())) {
2453 klassOop ref_class = load_class(ref_class_name, CHECK);
2454 if (is_protected_access(
2455 _klass, ref_class, method_name, method_sig, true)) {
2456 // It's protected access, check if stack object is
2457 // assignable to current class.
2458 bool is_assignable = current_type().is_assignable_from(
2459 stack_object_type, this, CHECK_VERIFY(this));
2460 if (!is_assignable) {
2461 if (ref_class_type.name() == vmSymbols::java_lang_Object()
2462 && stack_object_type.is_array()
2463 && method_name == vmSymbols::clone_name()) {
2464 // Special case: arrays pretend to implement public Object
2465 // clone().
2466 } else {
2467 verify_error(ErrorContext::bad_type(bci,
2468 current_frame->stack_top_ctx(),
2469 TypeOrigin::implicit(current_type())),
2470 "Bad access to protected data in invokevirtual");
2471 return;
2472 }
2473 }
2474 }
2475 }
2476 }
2477 } else {
2478 assert(opcode == Bytecodes::_invokeinterface, "Unexpected opcode encountered");
2479 current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this));
2480 }
2481 }
2482 }
2483 // Push the result type.
2484 if (sig_stream.type() != T_VOID) {
2485 if (method_name == vmSymbols::object_initializer_name()) {
2486 // <init> method must have a void return type
2487 /* Unreachable? Class file parser verifies that methods with '<' have
2488 * void return */
2489 verify_error(ErrorContext::bad_code(bci),
2490 "Return type must be void in <init> method");
2491 return;
2492 }
2493 VerificationType return_type[2];
2494 int n = change_sig_to_verificationType(
2495 &sig_stream, return_type, CHECK_VERIFY(this));
2496 for (int i = 0; i < n; i++) {
2497 current_frame->push_stack(return_type[i], CHECK_VERIFY(this)); // push types backwards
2498 }
2499 }
2500 }
2502 VerificationType ClassVerifier::get_newarray_type(
2503 u2 index, u2 bci, TRAPS) {
2504 const char* from_bt[] = {
2505 NULL, NULL, NULL, NULL, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J",
2506 };
2507 if (index < T_BOOLEAN || index > T_LONG) {
2508 verify_error(ErrorContext::bad_code(bci), "Illegal newarray instruction");
2509 return VerificationType::bogus_type();
2510 }
2512 // from_bt[index] contains the array signature which has a length of 2
2513 Symbol* sig = create_temporary_symbol(
2514 from_bt[index], 2, CHECK_(VerificationType::bogus_type()));
2515 return VerificationType::reference_type(sig);
2516 }
2518 void ClassVerifier::verify_anewarray(
2519 u2 bci, u2 index, constantPoolHandle cp,
2520 StackMapFrame* current_frame, TRAPS) {
2521 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this));
2522 current_frame->pop_stack(
2523 VerificationType::integer_type(), CHECK_VERIFY(this));
2525 VerificationType component_type =
2526 cp_index_to_type(index, cp, CHECK_VERIFY(this));
2527 int length;
2528 char* arr_sig_str;
2529 if (component_type.is_array()) { // it's an array
2530 const char* component_name = component_type.name()->as_utf8();
2531 // add one dimension to component
2532 length = (int)strlen(component_name) + 1;
2533 arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length);
2534 arr_sig_str[0] = '[';
2535 strncpy(&arr_sig_str[1], component_name, length - 1);
2536 } else { // it's an object or interface
2537 const char* component_name = component_type.name()->as_utf8();
2538 // add one dimension to component with 'L' prepended and ';' postpended.
2539 length = (int)strlen(component_name) + 3;
2540 arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length);
2541 arr_sig_str[0] = '[';
2542 arr_sig_str[1] = 'L';
2543 strncpy(&arr_sig_str[2], component_name, length - 2);
2544 arr_sig_str[length - 1] = ';';
2545 }
2546 Symbol* arr_sig = create_temporary_symbol(
2547 arr_sig_str, length, CHECK_VERIFY(this));
2548 VerificationType new_array_type = VerificationType::reference_type(arr_sig);
2549 current_frame->push_stack(new_array_type, CHECK_VERIFY(this));
2550 }
2552 void ClassVerifier::verify_iload(u2 index, StackMapFrame* current_frame, TRAPS) {
2553 current_frame->get_local(
2554 index, VerificationType::integer_type(), CHECK_VERIFY(this));
2555 current_frame->push_stack(
2556 VerificationType::integer_type(), CHECK_VERIFY(this));
2557 }
2559 void ClassVerifier::verify_lload(u2 index, StackMapFrame* current_frame, TRAPS) {
2560 current_frame->get_local_2(
2561 index, VerificationType::long_type(),
2562 VerificationType::long2_type(), CHECK_VERIFY(this));
2563 current_frame->push_stack_2(
2564 VerificationType::long_type(),
2565 VerificationType::long2_type(), CHECK_VERIFY(this));
2566 }
2568 void ClassVerifier::verify_fload(u2 index, StackMapFrame* current_frame, TRAPS) {
2569 current_frame->get_local(
2570 index, VerificationType::float_type(), CHECK_VERIFY(this));
2571 current_frame->push_stack(
2572 VerificationType::float_type(), CHECK_VERIFY(this));
2573 }
2575 void ClassVerifier::verify_dload(u2 index, StackMapFrame* current_frame, TRAPS) {
2576 current_frame->get_local_2(
2577 index, VerificationType::double_type(),
2578 VerificationType::double2_type(), CHECK_VERIFY(this));
2579 current_frame->push_stack_2(
2580 VerificationType::double_type(),
2581 VerificationType::double2_type(), CHECK_VERIFY(this));
2582 }
2584 void ClassVerifier::verify_aload(u2 index, StackMapFrame* current_frame, TRAPS) {
2585 VerificationType type = current_frame->get_local(
2586 index, VerificationType::reference_check(), CHECK_VERIFY(this));
2587 current_frame->push_stack(type, CHECK_VERIFY(this));
2588 }
2590 void ClassVerifier::verify_istore(u2 index, StackMapFrame* current_frame, TRAPS) {
2591 current_frame->pop_stack(
2592 VerificationType::integer_type(), CHECK_VERIFY(this));
2593 current_frame->set_local(
2594 index, VerificationType::integer_type(), CHECK_VERIFY(this));
2595 }
2597 void ClassVerifier::verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS) {
2598 current_frame->pop_stack_2(
2599 VerificationType::long2_type(),
2600 VerificationType::long_type(), CHECK_VERIFY(this));
2601 current_frame->set_local_2(
2602 index, VerificationType::long_type(),
2603 VerificationType::long2_type(), CHECK_VERIFY(this));
2604 }
2606 void ClassVerifier::verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS) {
2607 current_frame->pop_stack(VerificationType::float_type(), CHECK_VERIFY(this));
2608 current_frame->set_local(
2609 index, VerificationType::float_type(), CHECK_VERIFY(this));
2610 }
2612 void ClassVerifier::verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS) {
2613 current_frame->pop_stack_2(
2614 VerificationType::double2_type(),
2615 VerificationType::double_type(), CHECK_VERIFY(this));
2616 current_frame->set_local_2(
2617 index, VerificationType::double_type(),
2618 VerificationType::double2_type(), CHECK_VERIFY(this));
2619 }
2621 void ClassVerifier::verify_astore(u2 index, StackMapFrame* current_frame, TRAPS) {
2622 VerificationType type = current_frame->pop_stack(
2623 VerificationType::reference_check(), CHECK_VERIFY(this));
2624 current_frame->set_local(index, type, CHECK_VERIFY(this));
2625 }
2627 void ClassVerifier::verify_iinc(u2 index, StackMapFrame* current_frame, TRAPS) {
2628 VerificationType type = current_frame->get_local(
2629 index, VerificationType::integer_type(), CHECK_VERIFY(this));
2630 current_frame->set_local(index, type, CHECK_VERIFY(this));
2631 }
2633 void ClassVerifier::verify_return_value(
2634 VerificationType return_type, VerificationType type, u2 bci,
2635 StackMapFrame* current_frame, TRAPS) {
2636 if (return_type == VerificationType::bogus_type()) {
2637 verify_error(ErrorContext::bad_type(bci,
2638 current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)),
2639 "Method expects a return value");
2640 return;
2641 }
2642 bool match = return_type.is_assignable_from(type, this, CHECK_VERIFY(this));
2643 if (!match) {
2644 verify_error(ErrorContext::bad_type(bci,
2645 current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)),
2646 "Bad return type");
2647 return;
2648 }
2649 }
2651 // The verifier creates symbols which are substrings of Symbols.
2652 // These are stored in the verifier until the end of verification so that
2653 // they can be reference counted.
2654 Symbol* ClassVerifier::create_temporary_symbol(const Symbol *s, int begin,
2655 int end, TRAPS) {
2656 Symbol* sym = SymbolTable::new_symbol(s, begin, end, CHECK_NULL);
2657 _symbols->push(sym);
2658 return sym;
2659 }
2661 Symbol* ClassVerifier::create_temporary_symbol(const char *s, int length, TRAPS) {
2662 Symbol* sym = SymbolTable::new_symbol(s, length, CHECK_NULL);
2663 _symbols->push(sym);
2664 return sym;
2665 }