src/share/vm/classfile/stackMapFrame.cpp

Tue, 02 Apr 2013 11:28:33 +0200

author
mgerdin
date
Tue, 02 Apr 2013 11:28:33 +0200
changeset 4850
ede380e13960
parent 3992
4ee06e614636
child 6824
2373a1f4987c
child 6960
b2daaf70fab2
permissions
-rw-r--r--

8009763: Add WB test for String.intern()
Summary: Add convenience method in StringTable, add WhiteBox method and simple sanity test
Reviewed-by: mgerdin, zgu
Contributed-by: leonid.mesnik@oracle.com

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

mercurial