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