stackMapFrame.cpp 14.2 KB
Newer Older
D
duke 已提交
1
/*
H
hseigel 已提交
2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29
#include "precompiled.hpp"
#include "classfile/stackMapFrame.hpp"
#include "classfile/verifier.hpp"
#include "memory/resourceArea.hpp"
#include "oops/oop.inline.hpp"
30
#include "oops/symbol.hpp"
31 32
#include "runtime/handles.inline.hpp"
#include "utilities/globalDefinitions.hpp"
D
duke 已提交
33 34

StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) :
35 36 37
                      _offset(0), _locals_size(0), _stack_size(0),
                      _stack_mark(0), _flags(0), _max_locals(max_locals),
                      _max_stack(max_stack), _verifier(v) {
D
duke 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  Thread* thr = v->thread();
  _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals);
  _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack);
  int32_t i;
  for(i = 0; i < max_locals; i++) {
    _locals[i] = VerificationType::bogus_type();
  }
  for(i = 0; i < max_stack; i++) {
    _stack[i] = VerificationType::bogus_type();
  }
}

StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) {
  Thread* thr = _verifier->thread();
  VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1);
  StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier);
  return frame;
}

void StackMapFrame::initialize_object(
    VerificationType old_object, VerificationType new_object) {
  int32_t i;
  for (i = 0; i < _max_locals; i++) {
    if (_locals[i].equals(old_object)) {
      _locals[i] = new_object;
    }
  }
  for (i = 0; i < _stack_size; i++) {
    if (_stack[i].equals(old_object)) {
      _stack[i] = new_object;
    }
  }
  if (old_object == VerificationType::uninitialized_this_type()) {
    // "this" has been initialized - reset flags
    _flags = 0;
  }
}

VerificationType StackMapFrame::set_locals_from_arg(
    const methodHandle m, VerificationType thisKlass, TRAPS) {
78
  SignatureStream ss(m->signature());
D
duke 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  int init_local_num = 0;
  if (!m->is_static()) {
    init_local_num++;
    // add one extra argument for instance method
    if (m->name() == vmSymbols::object_initializer_name() &&
       thisKlass.name() != vmSymbols::java_lang_Object()) {
      _locals[0] = VerificationType::uninitialized_this_type();
      _flags |= FLAG_THIS_UNINIT;
    } else {
      _locals[0] = thisKlass;
    }
  }

  // local num may be greater than size of parameters because long/double occupies two slots
  while(!ss.at_return_type()) {
    init_local_num += _verifier->change_sig_to_verificationType(
      &ss, &_locals[init_local_num],
      CHECK_VERIFY_(verifier(), VerificationType::bogus_type()));
    ss.next();
  }
  _locals_size = init_local_num;

  switch (ss.type()) {
    case T_OBJECT:
    case T_ARRAY:
    {
105 106 107 108 109 110 111 112
      Symbol* sig = ss.as_symbol(CHECK_(VerificationType::bogus_type()));
      // Create another symbol to save as signature stream unreferences
      // this symbol.
      Symbol* sig_copy =
        verifier()->create_temporary_symbol(sig, 0, sig->utf8_length(),
                                 CHECK_(VerificationType::bogus_type()));
      assert(sig_copy == sig, "symbols don't match");
      return VerificationType::reference_type(sig_copy);
D
duke 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    }
    case T_INT:     return VerificationType::integer_type();
    case T_BYTE:    return VerificationType::byte_type();
    case T_CHAR:    return VerificationType::char_type();
    case T_SHORT:   return VerificationType::short_type();
    case T_BOOLEAN: return VerificationType::boolean_type();
    case T_FLOAT:   return VerificationType::float_type();
    case T_DOUBLE:  return VerificationType::double_type();
    case T_LONG:    return VerificationType::long_type();
    case T_VOID:    return VerificationType::bogus_type();
    default:
      ShouldNotReachHere();
  }
  return VerificationType::bogus_type();
}

void StackMapFrame::copy_locals(const StackMapFrame* src) {
  int32_t len = src->locals_size() < _locals_size ?
    src->locals_size() : _locals_size;
  for (int32_t i = 0; i < len; i++) {
    _locals[i] = src->locals()[i];
  }
}

void StackMapFrame::copy_stack(const StackMapFrame* src) {
  int32_t len = src->stack_size() < _stack_size ?
    src->stack_size() : _stack_size;
  for (int32_t i = 0; i < len; i++) {
    _stack[i] = src->stack()[i];
  }
}

145 146 147
// Returns the location of the first mismatch, or 'len' if there are no
// mismatches
int StackMapFrame::is_assignable_to(
D
duke 已提交
148
    VerificationType* from, VerificationType* to, int32_t len, TRAPS) const {
149 150
  int32_t i = 0;
  for (i = 0; i < len; i++) {
H
hseigel 已提交
151
    if (!to[i].is_assignable_from(from[i], verifier(), false, THREAD)) {
152
      break;
D
duke 已提交
153 154
    }
  }
155
  return i;
D
duke 已提交
156 157
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
bool StackMapFrame::has_flag_match_exception(
    const StackMapFrame* target) const {
  // We allow flags of {UninitThis} to assign to {} if-and-only-if the
  // target frame does not depend upon the current type.
  // This is slightly too strict, as we need only enforce that the
  // slots that were initialized by the <init> (the things that were
  // UninitializedThis before initialize_object() converted them) are unused.
  // However we didn't save that information so we'll enforce this upon
  // anything that might have been initialized.  This is a rare situation
  // and javac never generates code that would end up here, but some profilers
  // (such as NetBeans) might, when adding exception handlers in <init>
  // methods to cover the invokespecial instruction.  See 7020118.

  assert(max_locals() == target->max_locals() &&
         stack_size() == target->stack_size(), "StackMap sizes must match");

  VerificationType top = VerificationType::top_type();
  VerificationType this_type = verifier()->current_type();

  if (!flag_this_uninit() || target->flags() != 0) {
    return false;
  }

  for (int i = 0; i < target->locals_size(); ++i) {
    if (locals()[i] == this_type && target->locals()[i] != top) {
      return false;
    }
  }

  for (int i = 0; i < target->stack_size(); ++i) {
    if (stack()[i] == this_type && target->stack()[i] != top) {
      return false;
    }
  }

  return true;
}

196
bool StackMapFrame::is_assignable_to(
197 198 199 200 201 202 203 204 205 206
    const StackMapFrame* target, bool is_exception_handler,
    ErrorContext* ctx, TRAPS) const {
  if (_max_locals != target->max_locals()) {
    *ctx = ErrorContext::locals_size_mismatch(
        _offset, (StackMapFrame*)this, (StackMapFrame*)target);
    return false;
  }
  if (_stack_size != target->stack_size()) {
    *ctx = ErrorContext::stack_size_mismatch(
        _offset, (StackMapFrame*)this, (StackMapFrame*)target);
D
duke 已提交
207 208 209 210 211
    return false;
  }
  // Only need to compare type elements up to target->locals() or target->stack().
  // The remaining type elements in this state can be ignored because they are
  // assignable to bogus type.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  int mismatch_loc;
  mismatch_loc = is_assignable_to(
    _locals, target->locals(), target->locals_size(), THREAD);
  if (mismatch_loc != target->locals_size()) {
    *ctx = ErrorContext::bad_type(target->offset(),
        TypeOrigin::local(mismatch_loc, (StackMapFrame*)this),
        TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target));
    return false;
  }
  mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD);
  if (mismatch_loc != _stack_size) {
    *ctx = ErrorContext::bad_type(target->offset(),
        TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this),
        TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target));
    return false;
  }
228

229 230 231 232 233 234 235 236
  bool match_flags = (_flags | target->flags()) == target->flags();
  if (match_flags || is_exception_handler && has_flag_match_exception(target)) {
    return true;
  } else {
    *ctx = ErrorContext::bad_flags(target->offset(),
        (StackMapFrame*)this, (StackMapFrame*)target);
    return false;
  }
D
duke 已提交
237 238 239 240
}

VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
  if (_stack_size <= 0) {
241 242 243
    verifier()->verify_error(
        ErrorContext::stack_underflow(_offset, this),
        "Operand stack underflow");
D
duke 已提交
244 245 246 247
    return VerificationType::bogus_type();
  }
  VerificationType top = _stack[--_stack_size];
  bool subtype = type.is_assignable_from(
H
hseigel 已提交
248
    top, verifier(), false, CHECK_(VerificationType::bogus_type()));
D
duke 已提交
249
  if (!subtype) {
250 251 252 253
    verifier()->verify_error(
        ErrorContext::bad_type(_offset, stack_top_ctx(),
            TypeOrigin::implicit(type)),
        "Bad type on operand stack");
D
duke 已提交
254 255 256 257 258 259 260 261
    return VerificationType::bogus_type();
  }
  return top;
}

VerificationType StackMapFrame::get_local(
    int32_t index, VerificationType type, TRAPS) {
  if (index >= _max_locals) {
262 263 264
    verifier()->verify_error(
        ErrorContext::bad_local_index(_offset, index),
        "Local variable table overflow");
D
duke 已提交
265 266 267
    return VerificationType::bogus_type();
  }
  bool subtype = type.is_assignable_from(_locals[index],
H
hseigel 已提交
268
    verifier(), false, CHECK_(VerificationType::bogus_type()));
D
duke 已提交
269
  if (!subtype) {
270 271 272 273 274
    verifier()->verify_error(
        ErrorContext::bad_type(_offset,
          TypeOrigin::local(index, this),
          TypeOrigin::implicit(type)),
        "Bad local variable type");
D
duke 已提交
275 276 277 278 279 280 281 282 283 284 285
    return VerificationType::bogus_type();
  }
  if(index >= _locals_size) { _locals_size = index + 1; }
  return _locals[index];
}

void StackMapFrame::get_local_2(
    int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
  assert(type1.is_long() || type1.is_double(), "must be long/double");
  assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
  if (index >= _locals_size - 1) {
286 287 288
    verifier()->verify_error(
        ErrorContext::bad_local_index(_offset, index),
        "get long/double overflows locals");
D
duke 已提交
289 290
    return;
  }
H
hseigel 已提交
291
  bool subtype = type1.is_assignable_from(_locals[index], verifier(), false, CHECK);
292 293 294 295 296 297
  if (!subtype) {
    verifier()->verify_error(
        ErrorContext::bad_type(_offset,
            TypeOrigin::local(index, this), TypeOrigin::implicit(type1)),
        "Bad local variable type");
  } else {
H
hseigel 已提交
298
    subtype = type2.is_assignable_from(_locals[index + 1], verifier(), false, CHECK);
299 300 301 302 303 304 305 306 307
    if (!subtype) {
      /* Unreachable? All local store routines convert a split long or double
       * into a TOP during the store.  So we should never end up seeing an
       * orphaned half.  */
      verifier()->verify_error(
          ErrorContext::bad_type(_offset,
              TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)),
          "Bad local variable type");
    }
D
duke 已提交
308 309 310 311 312 313
  }
}

void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
  assert(!type.is_check(), "Must be a real type");
  if (index >= _max_locals) {
314 315 316
    verifier()->verify_error(
        ErrorContext::bad_local_index(_offset, index),
        "Local variable table overflow");
D
duke 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    return;
  }
  // If type at index is double or long, set the next location to be unusable
  if (_locals[index].is_double() || _locals[index].is_long()) {
    assert((index + 1) < _locals_size, "Local variable table overflow");
    _locals[index + 1] = VerificationType::bogus_type();
  }
  // If type at index is double_2 or long_2, set the previous location to be unusable
  if (_locals[index].is_double2() || _locals[index].is_long2()) {
    assert(index >= 1, "Local variable table underflow");
    _locals[index - 1] = VerificationType::bogus_type();
  }
  _locals[index] = type;
  if (index >= _locals_size) {
#ifdef ASSERT
    for (int i=_locals_size; i<index; i++) {
      assert(_locals[i] == VerificationType::bogus_type(),
             "holes must be bogus type");
    }
#endif
    _locals_size = index + 1;
  }
}

void StackMapFrame::set_local_2(
    int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
  assert(type1.is_long() || type1.is_double(), "must be long/double");
  assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
  if (index >= _max_locals - 1) {
346 347 348
    verifier()->verify_error(
        ErrorContext::bad_local_index(_offset, index),
        "Local variable table overflow");
D
duke 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    return;
  }
  // If type at index+1 is double or long, set the next location to be unusable
  if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
    assert((index + 2) < _locals_size, "Local variable table overflow");
    _locals[index + 2] = VerificationType::bogus_type();
  }
  // If type at index is double_2 or long_2, set the previous location to be unusable
  if (_locals[index].is_double2() || _locals[index].is_long2()) {
    assert(index >= 1, "Local variable table underflow");
    _locals[index - 1] = VerificationType::bogus_type();
  }
  _locals[index] = type1;
  _locals[index+1] = type2;
  if (index >= _locals_size - 1) {
#ifdef ASSERT
    for (int i=_locals_size; i<index; i++) {
      assert(_locals[i] == VerificationType::bogus_type(),
             "holes must be bogus type");
    }
#endif
    _locals_size = index + 2;
  }
}

374 375
TypeOrigin StackMapFrame::stack_top_ctx() {
  return TypeOrigin::stack(_stack_size, this);
D
duke 已提交
376 377
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
void StackMapFrame::print_on(outputStream* str) const {
  str->indent().print_cr("bci: @%d", _offset);
  str->indent().print_cr("flags: {%s }",
      flag_this_uninit() ? " flagThisUninit" : "");
  str->indent().print("locals: {");
  for (int32_t i = 0; i < _locals_size; ++i) {
    str->print(" ");
    _locals[i].print_on(str);
    if (i != _locals_size - 1) {
      str->print(",");
    }
  }
  str->print_cr(" }");
  str->indent().print("stack: {");
  for (int32_t j = 0; j < _stack_size; ++j) {
    str->print(" ");
    _stack[j].print_on(str);
    if (j != _stack_size - 1) {
      str->print(",");
    }
  }
  str->print_cr(" }");
}