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