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