src/share/vm/classfile/stackMapFrame.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8703
02a3d0dcbedd
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
kevinw@8703 2 * Copyright (c) 2003, 2016, 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::is_assignable_to(
kevinw@8703 159 const StackMapFrame* target, ErrorContext* ctx, TRAPS) const {
aoqi@0 160 if (_max_locals != target->max_locals()) {
aoqi@0 161 *ctx = ErrorContext::locals_size_mismatch(
aoqi@0 162 _offset, (StackMapFrame*)this, (StackMapFrame*)target);
aoqi@0 163 return false;
aoqi@0 164 }
aoqi@0 165 if (_stack_size != target->stack_size()) {
aoqi@0 166 *ctx = ErrorContext::stack_size_mismatch(
aoqi@0 167 _offset, (StackMapFrame*)this, (StackMapFrame*)target);
aoqi@0 168 return false;
aoqi@0 169 }
aoqi@0 170 // Only need to compare type elements up to target->locals() or target->stack().
aoqi@0 171 // The remaining type elements in this state can be ignored because they are
aoqi@0 172 // assignable to bogus type.
aoqi@0 173 int mismatch_loc;
aoqi@0 174 mismatch_loc = is_assignable_to(
aoqi@0 175 _locals, target->locals(), target->locals_size(), THREAD);
aoqi@0 176 if (mismatch_loc != target->locals_size()) {
aoqi@0 177 *ctx = ErrorContext::bad_type(target->offset(),
aoqi@0 178 TypeOrigin::local(mismatch_loc, (StackMapFrame*)this),
aoqi@0 179 TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target));
aoqi@0 180 return false;
aoqi@0 181 }
aoqi@0 182 mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD);
aoqi@0 183 if (mismatch_loc != _stack_size) {
aoqi@0 184 *ctx = ErrorContext::bad_type(target->offset(),
aoqi@0 185 TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this),
aoqi@0 186 TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target));
aoqi@0 187 return false;
aoqi@0 188 }
aoqi@0 189
kevinw@8703 190 if ((_flags | target->flags()) == target->flags()) {
aoqi@0 191 return true;
aoqi@0 192 } else {
aoqi@0 193 *ctx = ErrorContext::bad_flags(target->offset(),
aoqi@0 194 (StackMapFrame*)this, (StackMapFrame*)target);
aoqi@0 195 return false;
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
aoqi@0 200 if (_stack_size <= 0) {
aoqi@0 201 verifier()->verify_error(
aoqi@0 202 ErrorContext::stack_underflow(_offset, this),
aoqi@0 203 "Operand stack underflow");
aoqi@0 204 return VerificationType::bogus_type();
aoqi@0 205 }
aoqi@0 206 VerificationType top = _stack[--_stack_size];
aoqi@0 207 bool subtype = type.is_assignable_from(
aoqi@0 208 top, verifier(), false, CHECK_(VerificationType::bogus_type()));
aoqi@0 209 if (!subtype) {
aoqi@0 210 verifier()->verify_error(
aoqi@0 211 ErrorContext::bad_type(_offset, stack_top_ctx(),
aoqi@0 212 TypeOrigin::implicit(type)),
aoqi@0 213 "Bad type on operand stack");
aoqi@0 214 return VerificationType::bogus_type();
aoqi@0 215 }
aoqi@0 216 return top;
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 VerificationType StackMapFrame::get_local(
aoqi@0 220 int32_t index, VerificationType type, TRAPS) {
aoqi@0 221 if (index >= _max_locals) {
aoqi@0 222 verifier()->verify_error(
aoqi@0 223 ErrorContext::bad_local_index(_offset, index),
aoqi@0 224 "Local variable table overflow");
aoqi@0 225 return VerificationType::bogus_type();
aoqi@0 226 }
aoqi@0 227 bool subtype = type.is_assignable_from(_locals[index],
aoqi@0 228 verifier(), false, CHECK_(VerificationType::bogus_type()));
aoqi@0 229 if (!subtype) {
aoqi@0 230 verifier()->verify_error(
aoqi@0 231 ErrorContext::bad_type(_offset,
aoqi@0 232 TypeOrigin::local(index, this),
aoqi@0 233 TypeOrigin::implicit(type)),
aoqi@0 234 "Bad local variable type");
aoqi@0 235 return VerificationType::bogus_type();
aoqi@0 236 }
aoqi@0 237 if(index >= _locals_size) { _locals_size = index + 1; }
aoqi@0 238 return _locals[index];
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 void StackMapFrame::get_local_2(
aoqi@0 242 int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
aoqi@0 243 assert(type1.is_long() || type1.is_double(), "must be long/double");
aoqi@0 244 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
aoqi@0 245 if (index >= _locals_size - 1) {
aoqi@0 246 verifier()->verify_error(
aoqi@0 247 ErrorContext::bad_local_index(_offset, index),
aoqi@0 248 "get long/double overflows locals");
aoqi@0 249 return;
aoqi@0 250 }
aoqi@0 251 bool subtype = type1.is_assignable_from(_locals[index], verifier(), false, CHECK);
aoqi@0 252 if (!subtype) {
aoqi@0 253 verifier()->verify_error(
aoqi@0 254 ErrorContext::bad_type(_offset,
aoqi@0 255 TypeOrigin::local(index, this), TypeOrigin::implicit(type1)),
aoqi@0 256 "Bad local variable type");
aoqi@0 257 } else {
aoqi@0 258 subtype = type2.is_assignable_from(_locals[index + 1], verifier(), false, CHECK);
aoqi@0 259 if (!subtype) {
aoqi@0 260 /* Unreachable? All local store routines convert a split long or double
aoqi@0 261 * into a TOP during the store. So we should never end up seeing an
aoqi@0 262 * orphaned half. */
aoqi@0 263 verifier()->verify_error(
aoqi@0 264 ErrorContext::bad_type(_offset,
aoqi@0 265 TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)),
aoqi@0 266 "Bad local variable type");
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
aoqi@0 272 assert(!type.is_check(), "Must be a real type");
aoqi@0 273 if (index >= _max_locals) {
aoqi@0 274 verifier()->verify_error(
aoqi@0 275 ErrorContext::bad_local_index(_offset, index),
aoqi@0 276 "Local variable table overflow");
aoqi@0 277 return;
aoqi@0 278 }
aoqi@0 279 // If type at index is double or long, set the next location to be unusable
aoqi@0 280 if (_locals[index].is_double() || _locals[index].is_long()) {
aoqi@0 281 assert((index + 1) < _locals_size, "Local variable table overflow");
aoqi@0 282 _locals[index + 1] = VerificationType::bogus_type();
aoqi@0 283 }
aoqi@0 284 // If type at index is double_2 or long_2, set the previous location to be unusable
aoqi@0 285 if (_locals[index].is_double2() || _locals[index].is_long2()) {
aoqi@0 286 assert(index >= 1, "Local variable table underflow");
aoqi@0 287 _locals[index - 1] = VerificationType::bogus_type();
aoqi@0 288 }
aoqi@0 289 _locals[index] = type;
aoqi@0 290 if (index >= _locals_size) {
aoqi@0 291 #ifdef ASSERT
aoqi@0 292 for (int i=_locals_size; i<index; i++) {
aoqi@0 293 assert(_locals[i] == VerificationType::bogus_type(),
aoqi@0 294 "holes must be bogus type");
aoqi@0 295 }
aoqi@0 296 #endif
aoqi@0 297 _locals_size = index + 1;
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 void StackMapFrame::set_local_2(
aoqi@0 302 int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
aoqi@0 303 assert(type1.is_long() || type1.is_double(), "must be long/double");
aoqi@0 304 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
aoqi@0 305 if (index >= _max_locals - 1) {
aoqi@0 306 verifier()->verify_error(
aoqi@0 307 ErrorContext::bad_local_index(_offset, index),
aoqi@0 308 "Local variable table overflow");
aoqi@0 309 return;
aoqi@0 310 }
aoqi@0 311 // If type at index+1 is double or long, set the next location to be unusable
aoqi@0 312 if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
aoqi@0 313 assert((index + 2) < _locals_size, "Local variable table overflow");
aoqi@0 314 _locals[index + 2] = VerificationType::bogus_type();
aoqi@0 315 }
aoqi@0 316 // If type at index is double_2 or long_2, set the previous location to be unusable
aoqi@0 317 if (_locals[index].is_double2() || _locals[index].is_long2()) {
aoqi@0 318 assert(index >= 1, "Local variable table underflow");
aoqi@0 319 _locals[index - 1] = VerificationType::bogus_type();
aoqi@0 320 }
aoqi@0 321 _locals[index] = type1;
aoqi@0 322 _locals[index+1] = type2;
aoqi@0 323 if (index >= _locals_size - 1) {
aoqi@0 324 #ifdef ASSERT
aoqi@0 325 for (int i=_locals_size; i<index; i++) {
aoqi@0 326 assert(_locals[i] == VerificationType::bogus_type(),
aoqi@0 327 "holes must be bogus type");
aoqi@0 328 }
aoqi@0 329 #endif
aoqi@0 330 _locals_size = index + 2;
aoqi@0 331 }
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 TypeOrigin StackMapFrame::stack_top_ctx() {
aoqi@0 335 return TypeOrigin::stack(_stack_size, this);
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 void StackMapFrame::print_on(outputStream* str) const {
aoqi@0 339 str->indent().print_cr("bci: @%d", _offset);
aoqi@0 340 str->indent().print_cr("flags: {%s }",
aoqi@0 341 flag_this_uninit() ? " flagThisUninit" : "");
aoqi@0 342 str->indent().print("locals: {");
aoqi@0 343 for (int32_t i = 0; i < _locals_size; ++i) {
aoqi@0 344 str->print(" ");
aoqi@0 345 _locals[i].print_on(str);
aoqi@0 346 if (i != _locals_size - 1) {
aoqi@0 347 str->print(",");
aoqi@0 348 }
aoqi@0 349 }
aoqi@0 350 str->print_cr(" }");
aoqi@0 351 str->indent().print("stack: {");
aoqi@0 352 for (int32_t j = 0; j < _stack_size; ++j) {
aoqi@0 353 str->print(" ");
aoqi@0 354 _stack[j].print_on(str);
aoqi@0 355 if (j != _stack_size - 1) {
aoqi@0 356 str->print(",");
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359 str->print_cr(" }");
aoqi@0 360 }

mercurial