src/share/vm/classfile/stackMapFrame.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6872
c77d5db18942
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/stackMapFrame.hpp"
aoqi@0 27 #include "classfile/verifier.hpp"
aoqi@0 28 #include "memory/resourceArea.hpp"
aoqi@0 29 #include "oops/oop.inline.hpp"
aoqi@0 30 #include "oops/symbol.hpp"
aoqi@0 31 #include "runtime/handles.inline.hpp"
aoqi@0 32 #include "utilities/globalDefinitions.hpp"
aoqi@0 33
aoqi@0 34 StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) :
aoqi@0 35 _offset(0), _locals_size(0), _stack_size(0),
aoqi@0 36 _stack_mark(0), _flags(0), _max_locals(max_locals),
aoqi@0 37 _max_stack(max_stack), _verifier(v) {
aoqi@0 38 Thread* thr = v->thread();
aoqi@0 39 _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals);
aoqi@0 40 _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack);
aoqi@0 41 int32_t i;
aoqi@0 42 for(i = 0; i < max_locals; i++) {
aoqi@0 43 _locals[i] = VerificationType::bogus_type();
aoqi@0 44 }
aoqi@0 45 for(i = 0; i < max_stack; i++) {
aoqi@0 46 _stack[i] = VerificationType::bogus_type();
aoqi@0 47 }
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) {
aoqi@0 51 Thread* thr = _verifier->thread();
aoqi@0 52 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1);
aoqi@0 53 StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier);
aoqi@0 54 return frame;
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 void StackMapFrame::initialize_object(
aoqi@0 58 VerificationType old_object, VerificationType new_object) {
aoqi@0 59 int32_t i;
aoqi@0 60 for (i = 0; i < _max_locals; i++) {
aoqi@0 61 if (_locals[i].equals(old_object)) {
aoqi@0 62 _locals[i] = new_object;
aoqi@0 63 }
aoqi@0 64 }
aoqi@0 65 for (i = 0; i < _stack_size; i++) {
aoqi@0 66 if (_stack[i].equals(old_object)) {
aoqi@0 67 _stack[i] = new_object;
aoqi@0 68 }
aoqi@0 69 }
aoqi@0 70 if (old_object == VerificationType::uninitialized_this_type()) {
aoqi@0 71 // "this" has been initialized - reset flags
aoqi@0 72 _flags = 0;
aoqi@0 73 }
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 VerificationType StackMapFrame::set_locals_from_arg(
aoqi@0 77 const methodHandle m, VerificationType thisKlass, TRAPS) {
aoqi@0 78 SignatureStream ss(m->signature());
aoqi@0 79 int init_local_num = 0;
aoqi@0 80 if (!m->is_static()) {
aoqi@0 81 init_local_num++;
aoqi@0 82 // add one extra argument for instance method
aoqi@0 83 if (m->name() == vmSymbols::object_initializer_name() &&
aoqi@0 84 thisKlass.name() != vmSymbols::java_lang_Object()) {
aoqi@0 85 _locals[0] = VerificationType::uninitialized_this_type();
aoqi@0 86 _flags |= FLAG_THIS_UNINIT;
aoqi@0 87 } else {
aoqi@0 88 _locals[0] = thisKlass;
aoqi@0 89 }
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 // local num may be greater than size of parameters because long/double occupies two slots
aoqi@0 93 while(!ss.at_return_type()) {
aoqi@0 94 init_local_num += _verifier->change_sig_to_verificationType(
aoqi@0 95 &ss, &_locals[init_local_num],
aoqi@0 96 CHECK_VERIFY_(verifier(), VerificationType::bogus_type()));
aoqi@0 97 ss.next();
aoqi@0 98 }
aoqi@0 99 _locals_size = init_local_num;
aoqi@0 100
aoqi@0 101 switch (ss.type()) {
aoqi@0 102 case T_OBJECT:
aoqi@0 103 case T_ARRAY:
aoqi@0 104 {
aoqi@0 105 Symbol* sig = ss.as_symbol(CHECK_(VerificationType::bogus_type()));
aoqi@0 106 // Create another symbol to save as signature stream unreferences
aoqi@0 107 // this symbol.
aoqi@0 108 Symbol* sig_copy =
aoqi@0 109 verifier()->create_temporary_symbol(sig, 0, sig->utf8_length(),
aoqi@0 110 CHECK_(VerificationType::bogus_type()));
aoqi@0 111 assert(sig_copy == sig, "symbols don't match");
aoqi@0 112 return VerificationType::reference_type(sig_copy);
aoqi@0 113 }
aoqi@0 114 case T_INT: return VerificationType::integer_type();
aoqi@0 115 case T_BYTE: return VerificationType::byte_type();
aoqi@0 116 case T_CHAR: return VerificationType::char_type();
aoqi@0 117 case T_SHORT: return VerificationType::short_type();
aoqi@0 118 case T_BOOLEAN: return VerificationType::boolean_type();
aoqi@0 119 case T_FLOAT: return VerificationType::float_type();
aoqi@0 120 case T_DOUBLE: return VerificationType::double_type();
aoqi@0 121 case T_LONG: return VerificationType::long_type();
aoqi@0 122 case T_VOID: return VerificationType::bogus_type();
aoqi@0 123 default:
aoqi@0 124 ShouldNotReachHere();
aoqi@0 125 }
aoqi@0 126 return VerificationType::bogus_type();
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 void StackMapFrame::copy_locals(const StackMapFrame* src) {
aoqi@0 130 int32_t len = src->locals_size() < _locals_size ?
aoqi@0 131 src->locals_size() : _locals_size;
aoqi@0 132 for (int32_t i = 0; i < len; i++) {
aoqi@0 133 _locals[i] = src->locals()[i];
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 void StackMapFrame::copy_stack(const StackMapFrame* src) {
aoqi@0 138 int32_t len = src->stack_size() < _stack_size ?
aoqi@0 139 src->stack_size() : _stack_size;
aoqi@0 140 for (int32_t i = 0; i < len; i++) {
aoqi@0 141 _stack[i] = src->stack()[i];
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 // Returns the location of the first mismatch, or 'len' if there are no
aoqi@0 146 // mismatches
aoqi@0 147 int StackMapFrame::is_assignable_to(
aoqi@0 148 VerificationType* from, VerificationType* to, int32_t len, TRAPS) const {
aoqi@0 149 int32_t i = 0;
aoqi@0 150 for (i = 0; i < len; i++) {
aoqi@0 151 if (!to[i].is_assignable_from(from[i], verifier(), false, THREAD)) {
aoqi@0 152 break;
aoqi@0 153 }
aoqi@0 154 }
aoqi@0 155 return i;
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 bool StackMapFrame::has_flag_match_exception(
aoqi@0 159 const StackMapFrame* target) const {
aoqi@0 160 // We allow flags of {UninitThis} to assign to {} if-and-only-if the
aoqi@0 161 // target frame does not depend upon the current type.
aoqi@0 162 // This is slightly too strict, as we need only enforce that the
aoqi@0 163 // slots that were initialized by the <init> (the things that were
aoqi@0 164 // UninitializedThis before initialize_object() converted them) are unused.
aoqi@0 165 // However we didn't save that information so we'll enforce this upon
aoqi@0 166 // anything that might have been initialized. This is a rare situation
aoqi@0 167 // and javac never generates code that would end up here, but some profilers
aoqi@0 168 // (such as NetBeans) might, when adding exception handlers in <init>
aoqi@0 169 // methods to cover the invokespecial instruction. See 7020118.
aoqi@0 170
aoqi@0 171 assert(max_locals() == target->max_locals() &&
aoqi@0 172 stack_size() == target->stack_size(), "StackMap sizes must match");
aoqi@0 173
aoqi@0 174 VerificationType top = VerificationType::top_type();
aoqi@0 175 VerificationType this_type = verifier()->current_type();
aoqi@0 176
aoqi@0 177 if (!flag_this_uninit() || target->flags() != 0) {
aoqi@0 178 return false;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 for (int i = 0; i < target->locals_size(); ++i) {
aoqi@0 182 if (locals()[i] == this_type && target->locals()[i] != top) {
aoqi@0 183 return false;
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 for (int i = 0; i < target->stack_size(); ++i) {
aoqi@0 188 if (stack()[i] == this_type && target->stack()[i] != top) {
aoqi@0 189 return false;
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 return true;
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 bool StackMapFrame::is_assignable_to(
aoqi@0 197 const StackMapFrame* target, bool is_exception_handler,
aoqi@0 198 ErrorContext* ctx, TRAPS) const {
aoqi@0 199 if (_max_locals != target->max_locals()) {
aoqi@0 200 *ctx = ErrorContext::locals_size_mismatch(
aoqi@0 201 _offset, (StackMapFrame*)this, (StackMapFrame*)target);
aoqi@0 202 return false;
aoqi@0 203 }
aoqi@0 204 if (_stack_size != target->stack_size()) {
aoqi@0 205 *ctx = ErrorContext::stack_size_mismatch(
aoqi@0 206 _offset, (StackMapFrame*)this, (StackMapFrame*)target);
aoqi@0 207 return false;
aoqi@0 208 }
aoqi@0 209 // Only need to compare type elements up to target->locals() or target->stack().
aoqi@0 210 // The remaining type elements in this state can be ignored because they are
aoqi@0 211 // assignable to bogus type.
aoqi@0 212 int mismatch_loc;
aoqi@0 213 mismatch_loc = is_assignable_to(
aoqi@0 214 _locals, target->locals(), target->locals_size(), THREAD);
aoqi@0 215 if (mismatch_loc != target->locals_size()) {
aoqi@0 216 *ctx = ErrorContext::bad_type(target->offset(),
aoqi@0 217 TypeOrigin::local(mismatch_loc, (StackMapFrame*)this),
aoqi@0 218 TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target));
aoqi@0 219 return false;
aoqi@0 220 }
aoqi@0 221 mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD);
aoqi@0 222 if (mismatch_loc != _stack_size) {
aoqi@0 223 *ctx = ErrorContext::bad_type(target->offset(),
aoqi@0 224 TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this),
aoqi@0 225 TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target));
aoqi@0 226 return false;
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 bool match_flags = (_flags | target->flags()) == target->flags();
aoqi@0 230 if (match_flags || is_exception_handler && has_flag_match_exception(target)) {
aoqi@0 231 return true;
aoqi@0 232 } else {
aoqi@0 233 *ctx = ErrorContext::bad_flags(target->offset(),
aoqi@0 234 (StackMapFrame*)this, (StackMapFrame*)target);
aoqi@0 235 return false;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
aoqi@0 240 if (_stack_size <= 0) {
aoqi@0 241 verifier()->verify_error(
aoqi@0 242 ErrorContext::stack_underflow(_offset, this),
aoqi@0 243 "Operand stack underflow");
aoqi@0 244 return VerificationType::bogus_type();
aoqi@0 245 }
aoqi@0 246 VerificationType top = _stack[--_stack_size];
aoqi@0 247 bool subtype = type.is_assignable_from(
aoqi@0 248 top, verifier(), false, CHECK_(VerificationType::bogus_type()));
aoqi@0 249 if (!subtype) {
aoqi@0 250 verifier()->verify_error(
aoqi@0 251 ErrorContext::bad_type(_offset, stack_top_ctx(),
aoqi@0 252 TypeOrigin::implicit(type)),
aoqi@0 253 "Bad type on operand stack");
aoqi@0 254 return VerificationType::bogus_type();
aoqi@0 255 }
aoqi@0 256 return top;
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 VerificationType StackMapFrame::get_local(
aoqi@0 260 int32_t index, VerificationType type, TRAPS) {
aoqi@0 261 if (index >= _max_locals) {
aoqi@0 262 verifier()->verify_error(
aoqi@0 263 ErrorContext::bad_local_index(_offset, index),
aoqi@0 264 "Local variable table overflow");
aoqi@0 265 return VerificationType::bogus_type();
aoqi@0 266 }
aoqi@0 267 bool subtype = type.is_assignable_from(_locals[index],
aoqi@0 268 verifier(), false, CHECK_(VerificationType::bogus_type()));
aoqi@0 269 if (!subtype) {
aoqi@0 270 verifier()->verify_error(
aoqi@0 271 ErrorContext::bad_type(_offset,
aoqi@0 272 TypeOrigin::local(index, this),
aoqi@0 273 TypeOrigin::implicit(type)),
aoqi@0 274 "Bad local variable type");
aoqi@0 275 return VerificationType::bogus_type();
aoqi@0 276 }
aoqi@0 277 if(index >= _locals_size) { _locals_size = index + 1; }
aoqi@0 278 return _locals[index];
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 void StackMapFrame::get_local_2(
aoqi@0 282 int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
aoqi@0 283 assert(type1.is_long() || type1.is_double(), "must be long/double");
aoqi@0 284 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
aoqi@0 285 if (index >= _locals_size - 1) {
aoqi@0 286 verifier()->verify_error(
aoqi@0 287 ErrorContext::bad_local_index(_offset, index),
aoqi@0 288 "get long/double overflows locals");
aoqi@0 289 return;
aoqi@0 290 }
aoqi@0 291 bool subtype = type1.is_assignable_from(_locals[index], verifier(), false, CHECK);
aoqi@0 292 if (!subtype) {
aoqi@0 293 verifier()->verify_error(
aoqi@0 294 ErrorContext::bad_type(_offset,
aoqi@0 295 TypeOrigin::local(index, this), TypeOrigin::implicit(type1)),
aoqi@0 296 "Bad local variable type");
aoqi@0 297 } else {
aoqi@0 298 subtype = type2.is_assignable_from(_locals[index + 1], verifier(), false, CHECK);
aoqi@0 299 if (!subtype) {
aoqi@0 300 /* Unreachable? All local store routines convert a split long or double
aoqi@0 301 * into a TOP during the store. So we should never end up seeing an
aoqi@0 302 * orphaned half. */
aoqi@0 303 verifier()->verify_error(
aoqi@0 304 ErrorContext::bad_type(_offset,
aoqi@0 305 TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)),
aoqi@0 306 "Bad local variable type");
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
aoqi@0 312 assert(!type.is_check(), "Must be a real type");
aoqi@0 313 if (index >= _max_locals) {
aoqi@0 314 verifier()->verify_error(
aoqi@0 315 ErrorContext::bad_local_index(_offset, index),
aoqi@0 316 "Local variable table overflow");
aoqi@0 317 return;
aoqi@0 318 }
aoqi@0 319 // If type at index is double or long, set the next location to be unusable
aoqi@0 320 if (_locals[index].is_double() || _locals[index].is_long()) {
aoqi@0 321 assert((index + 1) < _locals_size, "Local variable table overflow");
aoqi@0 322 _locals[index + 1] = VerificationType::bogus_type();
aoqi@0 323 }
aoqi@0 324 // If type at index is double_2 or long_2, set the previous location to be unusable
aoqi@0 325 if (_locals[index].is_double2() || _locals[index].is_long2()) {
aoqi@0 326 assert(index >= 1, "Local variable table underflow");
aoqi@0 327 _locals[index - 1] = VerificationType::bogus_type();
aoqi@0 328 }
aoqi@0 329 _locals[index] = type;
aoqi@0 330 if (index >= _locals_size) {
aoqi@0 331 #ifdef ASSERT
aoqi@0 332 for (int i=_locals_size; i<index; i++) {
aoqi@0 333 assert(_locals[i] == VerificationType::bogus_type(),
aoqi@0 334 "holes must be bogus type");
aoqi@0 335 }
aoqi@0 336 #endif
aoqi@0 337 _locals_size = index + 1;
aoqi@0 338 }
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 void StackMapFrame::set_local_2(
aoqi@0 342 int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
aoqi@0 343 assert(type1.is_long() || type1.is_double(), "must be long/double");
aoqi@0 344 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
aoqi@0 345 if (index >= _max_locals - 1) {
aoqi@0 346 verifier()->verify_error(
aoqi@0 347 ErrorContext::bad_local_index(_offset, index),
aoqi@0 348 "Local variable table overflow");
aoqi@0 349 return;
aoqi@0 350 }
aoqi@0 351 // If type at index+1 is double or long, set the next location to be unusable
aoqi@0 352 if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
aoqi@0 353 assert((index + 2) < _locals_size, "Local variable table overflow");
aoqi@0 354 _locals[index + 2] = VerificationType::bogus_type();
aoqi@0 355 }
aoqi@0 356 // If type at index is double_2 or long_2, set the previous location to be unusable
aoqi@0 357 if (_locals[index].is_double2() || _locals[index].is_long2()) {
aoqi@0 358 assert(index >= 1, "Local variable table underflow");
aoqi@0 359 _locals[index - 1] = VerificationType::bogus_type();
aoqi@0 360 }
aoqi@0 361 _locals[index] = type1;
aoqi@0 362 _locals[index+1] = type2;
aoqi@0 363 if (index >= _locals_size - 1) {
aoqi@0 364 #ifdef ASSERT
aoqi@0 365 for (int i=_locals_size; i<index; i++) {
aoqi@0 366 assert(_locals[i] == VerificationType::bogus_type(),
aoqi@0 367 "holes must be bogus type");
aoqi@0 368 }
aoqi@0 369 #endif
aoqi@0 370 _locals_size = index + 2;
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 TypeOrigin StackMapFrame::stack_top_ctx() {
aoqi@0 375 return TypeOrigin::stack(_stack_size, this);
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 void StackMapFrame::print_on(outputStream* str) const {
aoqi@0 379 str->indent().print_cr("bci: @%d", _offset);
aoqi@0 380 str->indent().print_cr("flags: {%s }",
aoqi@0 381 flag_this_uninit() ? " flagThisUninit" : "");
aoqi@0 382 str->indent().print("locals: {");
aoqi@0 383 for (int32_t i = 0; i < _locals_size; ++i) {
aoqi@0 384 str->print(" ");
aoqi@0 385 _locals[i].print_on(str);
aoqi@0 386 if (i != _locals_size - 1) {
aoqi@0 387 str->print(",");
aoqi@0 388 }
aoqi@0 389 }
aoqi@0 390 str->print_cr(" }");
aoqi@0 391 str->indent().print("stack: {");
aoqi@0 392 for (int32_t j = 0; j < _stack_size; ++j) {
aoqi@0 393 str->print(" ");
aoqi@0 394 _stack[j].print_on(str);
aoqi@0 395 if (j != _stack_size - 1) {
aoqi@0 396 str->print(",");
aoqi@0 397 }
aoqi@0 398 }
aoqi@0 399 str->print_cr(" }");
aoqi@0 400 }

mercurial