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