1 /* 2 * Copyright (c) 2003, 2014, 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 34 StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) : 35 _offset(0), _locals_size(0), _stack_size(0), 36 _stack_mark(0), _flags(0), _max_locals(max_locals), 37 _max_stack(max_stack), _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 50 StackMapFrame* 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 57 // Return true if frame has an uninitialized (new) object that differs 58 // from the target frame's object. 59 bool StackMapFrame::has_unique_new_object(const StackMapFrame *target_frame) const { 60 int32_t i; 61 for (i = 0; i < _max_locals; i++) { 62 if (_locals[i].is_uninitialized() && 63 !_locals[i].equals(target_frame->_locals[i])) { 64 return true; 65 } 66 } 67 for (i = 0; i < _stack_size; i++) { 68 if (_stack[i].is_uninitialized() && 69 !_stack[i].equals(target_frame->_stack[i])) { 70 return true; 71 } 72 } 73 return false; 74 } 75 76 void StackMapFrame::initialize_object( 77 VerificationType old_object, VerificationType new_object) { 78 int32_t i; 79 for (i = 0; i < _max_locals; i++) { 80 if (_locals[i].equals(old_object)) { 81 _locals[i] = new_object; 82 } 83 } 84 for (i = 0; i < _stack_size; i++) { 85 if (_stack[i].equals(old_object)) { 86 _stack[i] = new_object; 87 } 88 } 89 if (old_object == VerificationType::uninitialized_this_type()) { 90 // "this" has been initialized - reset flags 91 _flags = 0; 92 } 93 } 94 95 VerificationType StackMapFrame::set_locals_from_arg( 96 const methodHandle m, VerificationType thisKlass, TRAPS) { 97 SignatureStream ss(m->signature()); 98 int init_local_num = 0; 99 if (!m->is_static()) { 100 init_local_num++; 101 // add one extra argument for instance method 102 if (m->name() == vmSymbols::object_initializer_name() && 103 thisKlass.name() != vmSymbols::java_lang_Object()) { 104 _locals[0] = VerificationType::uninitialized_this_type(); 105 _flags |= FLAG_THIS_UNINIT; 106 } else { 107 _locals[0] = thisKlass; 108 } 109 } 110 111 // local num may be greater than size of parameters because long/double occupies two slots 112 while(!ss.at_return_type()) { 113 init_local_num += _verifier->change_sig_to_verificationType( 114 &ss, &_locals[init_local_num], 115 CHECK_VERIFY_(verifier(), VerificationType::bogus_type())); 116 ss.next(); 117 } 118 _locals_size = init_local_num; 119 120 switch (ss.type()) { 121 case T_OBJECT: 122 case T_ARRAY: 123 { 124 Symbol* sig = ss.as_symbol(CHECK_(VerificationType::bogus_type())); 125 // Create another symbol to save as signature stream unreferences 126 // this symbol. 127 Symbol* sig_copy = 128 verifier()->create_temporary_symbol(sig, 0, sig->utf8_length(), 129 CHECK_(VerificationType::bogus_type())); 130 assert(sig_copy == sig, "symbols don't match"); 131 return VerificationType::reference_type(sig_copy); 132 } 133 case T_INT: return VerificationType::integer_type(); 134 case T_BYTE: return VerificationType::byte_type(); 135 case T_CHAR: return VerificationType::char_type(); 136 case T_SHORT: return VerificationType::short_type(); 137 case T_BOOLEAN: return VerificationType::boolean_type(); 138 case T_FLOAT: return VerificationType::float_type(); 139 case T_DOUBLE: return VerificationType::double_type(); 140 case T_LONG: return VerificationType::long_type(); 141 case T_VOID: return VerificationType::bogus_type(); 142 default: 143 ShouldNotReachHere(); 144 } 145 return VerificationType::bogus_type(); 146 } 147 148 void StackMapFrame::copy_locals(const StackMapFrame* src) { 149 int32_t len = src->locals_size() < _locals_size ? 150 src->locals_size() : _locals_size; 151 for (int32_t i = 0; i < len; i++) { 152 _locals[i] = src->locals()[i]; 153 } 154 } 155 156 void StackMapFrame::copy_stack(const StackMapFrame* src) { 157 int32_t len = src->stack_size() < _stack_size ? 158 src->stack_size() : _stack_size; 159 for (int32_t i = 0; i < len; i++) { 160 _stack[i] = src->stack()[i]; 161 } 162 } 163 164 // Returns the location of the first mismatch, or 'len' if there are no 165 // mismatches 166 int StackMapFrame::is_assignable_to( 167 VerificationType* from, VerificationType* to, int32_t len, TRAPS) const { 168 int32_t i = 0; 169 for (i = 0; i < len; i++) { 170 if (!to[i].is_assignable_from(from[i], verifier(), THREAD)) { 171 break; 172 } 173 } 174 return i; 175 } 176 177 bool StackMapFrame::has_flag_match_exception( 178 const StackMapFrame* target) const { 179 // We allow flags of {UninitThis} to assign to {} if-and-only-if the 180 // target frame does not depend upon the current type. 181 // This is slightly too strict, as we need only enforce that the 182 // slots that were initialized by the <init> (the things that were 183 // UninitializedThis before initialize_object() converted them) are unused. 184 // However we didn't save that information so we'll enforce this upon 185 // anything that might have been initialized. This is a rare situation 186 // and javac never generates code that would end up here, but some profilers 187 // (such as NetBeans) might, when adding exception handlers in <init> 188 // methods to cover the invokespecial instruction. See 7020118. 189 190 assert(max_locals() == target->max_locals() && 191 stack_size() == target->stack_size(), "StackMap sizes must match"); 192 193 VerificationType top = VerificationType::top_type(); 194 VerificationType this_type = verifier()->current_type(); 195 196 if (!flag_this_uninit() || target->flags() != 0) { 197 return false; 198 } 199 200 for (int i = 0; i < target->locals_size(); ++i) { 201 if (locals()[i] == this_type && target->locals()[i] != top) { 202 return false; 203 } 204 } 205 206 for (int i = 0; i < target->stack_size(); ++i) { 207 if (stack()[i] == this_type && target->stack()[i] != top) { 208 return false; 209 } 210 } 211 212 return true; 213 } 214 215 bool StackMapFrame::is_assignable_to( 216 const StackMapFrame* target, bool is_exception_handler, 217 ErrorContext* ctx, TRAPS) const { 218 if (_max_locals != target->max_locals()) { 219 *ctx = ErrorContext::locals_size_mismatch( 220 _offset, (StackMapFrame*)this, (StackMapFrame*)target); 221 return false; 222 } 223 if (_stack_size != target->stack_size()) { 224 *ctx = ErrorContext::stack_size_mismatch( 225 _offset, (StackMapFrame*)this, (StackMapFrame*)target); 226 return false; 227 } 228 // Only need to compare type elements up to target->locals() or target->stack(). 229 // The remaining type elements in this state can be ignored because they are 230 // assignable to bogus type. 231 int mismatch_loc; 232 mismatch_loc = is_assignable_to( 233 _locals, target->locals(), target->locals_size(), THREAD); 234 if (mismatch_loc != target->locals_size()) { 235 *ctx = ErrorContext::bad_type(target->offset(), 236 TypeOrigin::local(mismatch_loc, (StackMapFrame*)this), 237 TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target)); 238 return false; 239 } 240 mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD); 241 if (mismatch_loc != _stack_size) { 242 *ctx = ErrorContext::bad_type(target->offset(), 243 TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this), 244 TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target)); 245 return false; 246 } 247 248 bool match_flags = (_flags | target->flags()) == target->flags(); 249 if (match_flags || is_exception_handler && has_flag_match_exception(target)) { 250 return true; 251 } else { 252 *ctx = ErrorContext::bad_flags(target->offset(), 253 (StackMapFrame*)this, (StackMapFrame*)target); 254 return false; 255 } 256 } 257 258 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) { 259 if (_stack_size <= 0) { 260 verifier()->verify_error( 261 ErrorContext::stack_underflow(_offset, this), 262 "Operand stack underflow"); 263 return VerificationType::bogus_type(); 264 } 265 VerificationType top = _stack[--_stack_size]; 266 bool subtype = type.is_assignable_from( 267 top, verifier(), CHECK_(VerificationType::bogus_type())); 268 if (!subtype) { 269 verifier()->verify_error( 270 ErrorContext::bad_type(_offset, stack_top_ctx(), 271 TypeOrigin::implicit(type)), 272 "Bad type on operand stack"); 273 return VerificationType::bogus_type(); 274 } 275 return top; 276 } 277 278 VerificationType StackMapFrame::get_local( 279 int32_t index, VerificationType type, TRAPS) { 280 if (index >= _max_locals) { 281 verifier()->verify_error( 282 ErrorContext::bad_local_index(_offset, index), 283 "Local variable table overflow"); 284 return VerificationType::bogus_type(); 285 } 286 bool subtype = type.is_assignable_from(_locals[index], 287 verifier(), CHECK_(VerificationType::bogus_type())); 288 if (!subtype) { 289 verifier()->verify_error( 290 ErrorContext::bad_type(_offset, 291 TypeOrigin::local(index, this), 292 TypeOrigin::implicit(type)), 293 "Bad local variable type"); 294 return VerificationType::bogus_type(); 295 } 296 if(index >= _locals_size) { _locals_size = index + 1; } 297 return _locals[index]; 298 } 299 300 void StackMapFrame::get_local_2( 301 int32_t index, VerificationType type1, VerificationType type2, TRAPS) { 302 assert(type1.is_long() || type1.is_double(), "must be long/double"); 303 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); 304 if (index >= _locals_size - 1) { 305 verifier()->verify_error( 306 ErrorContext::bad_local_index(_offset, index), 307 "get long/double overflows locals"); 308 return; 309 } 310 bool subtype = type1.is_assignable_from(_locals[index], verifier(), CHECK); 311 if (!subtype) { 312 verifier()->verify_error( 313 ErrorContext::bad_type(_offset, 314 TypeOrigin::local(index, this), TypeOrigin::implicit(type1)), 315 "Bad local variable type"); 316 } else { 317 subtype = type2.is_assignable_from(_locals[index + 1], verifier(), CHECK); 318 if (!subtype) { 319 /* Unreachable? All local store routines convert a split long or double 320 * into a TOP during the store. So we should never end up seeing an 321 * orphaned half. */ 322 verifier()->verify_error( 323 ErrorContext::bad_type(_offset, 324 TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)), 325 "Bad local variable type"); 326 } 327 } 328 } 329 330 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) { 331 assert(!type.is_check(), "Must be a real type"); 332 if (index >= _max_locals) { 333 verifier()->verify_error( 334 ErrorContext::bad_local_index(_offset, index), 335 "Local variable table overflow"); 336 return; 337 } 338 // If type at index is double or long, set the next location to be unusable 339 if (_locals[index].is_double() || _locals[index].is_long()) { 340 assert((index + 1) < _locals_size, "Local variable table overflow"); 341 _locals[index + 1] = VerificationType::bogus_type(); 342 } 343 // If type at index is double_2 or long_2, set the previous location to be unusable 344 if (_locals[index].is_double2() || _locals[index].is_long2()) { 345 assert(index >= 1, "Local variable table underflow"); 346 _locals[index - 1] = VerificationType::bogus_type(); 347 } 348 _locals[index] = type; 349 if (index >= _locals_size) { 350 #ifdef ASSERT 351 for (int i=_locals_size; i<index; i++) { 352 assert(_locals[i] == VerificationType::bogus_type(), 353 "holes must be bogus type"); 354 } 355 #endif 356 _locals_size = index + 1; 357 } 358 } 359 360 void StackMapFrame::set_local_2( 361 int32_t index, VerificationType type1, VerificationType type2, TRAPS) { 362 assert(type1.is_long() || type1.is_double(), "must be long/double"); 363 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); 364 if (index >= _max_locals - 1) { 365 verifier()->verify_error( 366 ErrorContext::bad_local_index(_offset, index), 367 "Local variable table overflow"); 368 return; 369 } 370 // If type at index+1 is double or long, set the next location to be unusable 371 if (_locals[index+1].is_double() || _locals[index+1].is_long()) { 372 assert((index + 2) < _locals_size, "Local variable table overflow"); 373 _locals[index + 2] = VerificationType::bogus_type(); 374 } 375 // If type at index is double_2 or long_2, set the previous location to be unusable 376 if (_locals[index].is_double2() || _locals[index].is_long2()) { 377 assert(index >= 1, "Local variable table underflow"); 378 _locals[index - 1] = VerificationType::bogus_type(); 379 } 380 _locals[index] = type1; 381 _locals[index+1] = type2; 382 if (index >= _locals_size - 1) { 383 #ifdef ASSERT 384 for (int i=_locals_size; i<index; i++) { 385 assert(_locals[i] == VerificationType::bogus_type(), 386 "holes must be bogus type"); 387 } 388 #endif 389 _locals_size = index + 2; 390 } 391 } 392 393 TypeOrigin StackMapFrame::stack_top_ctx() { 394 return TypeOrigin::stack(_stack_size, this); 395 } 396 397 void StackMapFrame::print_on(outputStream* str) const { 398 str->indent().print_cr("bci: @%d", _offset); 399 str->indent().print_cr("flags: {%s }", 400 flag_this_uninit() ? " flagThisUninit" : ""); 401 str->indent().print("locals: {"); 402 for (int32_t i = 0; i < _locals_size; ++i) { 403 str->print(" "); 404 _locals[i].print_on(str); 405 if (i != _locals_size - 1) { 406 str->print(","); 407 } 408 } 409 str->print_cr(" }"); 410 str->indent().print("stack: {"); 411 for (int32_t j = 0; j < _stack_size; ++j) { 412 str->print(" "); 413 _stack[j].print_on(str); 414 if (j != _stack_size - 1) { 415 str->print(","); 416 } 417 } 418 str->print_cr(" }"); 419 }