1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/classfile/stackMapFrame.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,303 @@ 1.4 +/* 1.5 + * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_stackMapFrame.cpp.incl" 1.30 + 1.31 +StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) : 1.32 + _offset(0), _locals_size(0), _stack_size(0), _flags(0), 1.33 + _max_locals(max_locals), _max_stack(max_stack), 1.34 + _verifier(v) { 1.35 + Thread* thr = v->thread(); 1.36 + _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals); 1.37 + _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack); 1.38 + int32_t i; 1.39 + for(i = 0; i < max_locals; i++) { 1.40 + _locals[i] = VerificationType::bogus_type(); 1.41 + } 1.42 + for(i = 0; i < max_stack; i++) { 1.43 + _stack[i] = VerificationType::bogus_type(); 1.44 + } 1.45 +} 1.46 + 1.47 +StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) { 1.48 + Thread* thr = _verifier->thread(); 1.49 + VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1); 1.50 + StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier); 1.51 + return frame; 1.52 +} 1.53 + 1.54 +bool StackMapFrame::has_new_object() const { 1.55 + int32_t i; 1.56 + for (i = 0; i < _max_locals; i++) { 1.57 + if (_locals[i].is_uninitialized()) { 1.58 + return true; 1.59 + } 1.60 + } 1.61 + for (i = 0; i < _stack_size; i++) { 1.62 + if (_stack[i].is_uninitialized()) { 1.63 + return true; 1.64 + } 1.65 + } 1.66 + return false; 1.67 +} 1.68 + 1.69 +void StackMapFrame::initialize_object( 1.70 + VerificationType old_object, VerificationType new_object) { 1.71 + int32_t i; 1.72 + for (i = 0; i < _max_locals; i++) { 1.73 + if (_locals[i].equals(old_object)) { 1.74 + _locals[i] = new_object; 1.75 + } 1.76 + } 1.77 + for (i = 0; i < _stack_size; i++) { 1.78 + if (_stack[i].equals(old_object)) { 1.79 + _stack[i] = new_object; 1.80 + } 1.81 + } 1.82 + if (old_object == VerificationType::uninitialized_this_type()) { 1.83 + // "this" has been initialized - reset flags 1.84 + _flags = 0; 1.85 + } 1.86 +} 1.87 + 1.88 +VerificationType StackMapFrame::set_locals_from_arg( 1.89 + const methodHandle m, VerificationType thisKlass, TRAPS) { 1.90 + symbolHandle signature(THREAD, m->signature()); 1.91 + SignatureStream ss(signature); 1.92 + int init_local_num = 0; 1.93 + if (!m->is_static()) { 1.94 + init_local_num++; 1.95 + // add one extra argument for instance method 1.96 + if (m->name() == vmSymbols::object_initializer_name() && 1.97 + thisKlass.name() != vmSymbols::java_lang_Object()) { 1.98 + _locals[0] = VerificationType::uninitialized_this_type(); 1.99 + _flags |= FLAG_THIS_UNINIT; 1.100 + } else { 1.101 + _locals[0] = thisKlass; 1.102 + } 1.103 + } 1.104 + 1.105 + // local num may be greater than size of parameters because long/double occupies two slots 1.106 + while(!ss.at_return_type()) { 1.107 + init_local_num += _verifier->change_sig_to_verificationType( 1.108 + &ss, &_locals[init_local_num], 1.109 + CHECK_VERIFY_(verifier(), VerificationType::bogus_type())); 1.110 + ss.next(); 1.111 + } 1.112 + _locals_size = init_local_num; 1.113 + 1.114 + switch (ss.type()) { 1.115 + case T_OBJECT: 1.116 + case T_ARRAY: 1.117 + { 1.118 + symbolOop sig = ss.as_symbol(CHECK_(VerificationType::bogus_type())); 1.119 + return VerificationType::reference_type(symbolHandle(THREAD, sig)); 1.120 + } 1.121 + case T_INT: return VerificationType::integer_type(); 1.122 + case T_BYTE: return VerificationType::byte_type(); 1.123 + case T_CHAR: return VerificationType::char_type(); 1.124 + case T_SHORT: return VerificationType::short_type(); 1.125 + case T_BOOLEAN: return VerificationType::boolean_type(); 1.126 + case T_FLOAT: return VerificationType::float_type(); 1.127 + case T_DOUBLE: return VerificationType::double_type(); 1.128 + case T_LONG: return VerificationType::long_type(); 1.129 + case T_VOID: return VerificationType::bogus_type(); 1.130 + default: 1.131 + ShouldNotReachHere(); 1.132 + } 1.133 + return VerificationType::bogus_type(); 1.134 +} 1.135 + 1.136 +void StackMapFrame::copy_locals(const StackMapFrame* src) { 1.137 + int32_t len = src->locals_size() < _locals_size ? 1.138 + src->locals_size() : _locals_size; 1.139 + for (int32_t i = 0; i < len; i++) { 1.140 + _locals[i] = src->locals()[i]; 1.141 + } 1.142 +} 1.143 + 1.144 +void StackMapFrame::copy_stack(const StackMapFrame* src) { 1.145 + int32_t len = src->stack_size() < _stack_size ? 1.146 + src->stack_size() : _stack_size; 1.147 + for (int32_t i = 0; i < len; i++) { 1.148 + _stack[i] = src->stack()[i]; 1.149 + } 1.150 +} 1.151 + 1.152 + 1.153 +bool StackMapFrame::is_assignable_to( 1.154 + VerificationType* from, VerificationType* to, int32_t len, TRAPS) const { 1.155 + for (int32_t i = 0; i < len; i++) { 1.156 + bool subtype = to[i].is_assignable_from( 1.157 + from[i], verifier()->current_class(), THREAD); 1.158 + if (!subtype) { 1.159 + return false; 1.160 + } 1.161 + } 1.162 + return true; 1.163 +} 1.164 + 1.165 +bool StackMapFrame::is_assignable_to(const StackMapFrame* target, TRAPS) const { 1.166 + if (_max_locals != target->max_locals() || _stack_size != target->stack_size()) { 1.167 + return false; 1.168 + } 1.169 + // Only need to compare type elements up to target->locals() or target->stack(). 1.170 + // The remaining type elements in this state can be ignored because they are 1.171 + // assignable to bogus type. 1.172 + bool match_locals = is_assignable_to( 1.173 + _locals, target->locals(), target->locals_size(), CHECK_false); 1.174 + bool match_stack = is_assignable_to( 1.175 + _stack, target->stack(), _stack_size, CHECK_false); 1.176 + bool match_flags = (_flags | target->flags()) == target->flags(); 1.177 + return (match_locals && match_stack && match_flags); 1.178 +} 1.179 + 1.180 +VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) { 1.181 + if (_stack_size <= 0) { 1.182 + verifier()->verify_error(_offset, "Operand stack underflow"); 1.183 + return VerificationType::bogus_type(); 1.184 + } 1.185 + VerificationType top = _stack[--_stack_size]; 1.186 + bool subtype = type.is_assignable_from( 1.187 + top, verifier()->current_class(), CHECK_(VerificationType::bogus_type())); 1.188 + if (!subtype) { 1.189 + verifier()->verify_error(_offset, "Bad type on operand stack"); 1.190 + return VerificationType::bogus_type(); 1.191 + } 1.192 + NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); ) 1.193 + return top; 1.194 +} 1.195 + 1.196 +VerificationType StackMapFrame::get_local( 1.197 + int32_t index, VerificationType type, TRAPS) { 1.198 + if (index >= _max_locals) { 1.199 + verifier()->verify_error(_offset, "Local variable table overflow"); 1.200 + return VerificationType::bogus_type(); 1.201 + } 1.202 + bool subtype = type.is_assignable_from(_locals[index], 1.203 + verifier()->current_class(), CHECK_(VerificationType::bogus_type())); 1.204 + if (!subtype) { 1.205 + verifier()->verify_error(_offset, "Bad local variable type"); 1.206 + return VerificationType::bogus_type(); 1.207 + } 1.208 + if(index >= _locals_size) { _locals_size = index + 1; } 1.209 + return _locals[index]; 1.210 +} 1.211 + 1.212 +void StackMapFrame::get_local_2( 1.213 + int32_t index, VerificationType type1, VerificationType type2, TRAPS) { 1.214 + assert(type1.is_long() || type1.is_double(), "must be long/double"); 1.215 + assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); 1.216 + if (index >= _locals_size - 1) { 1.217 + verifier()->verify_error(_offset, "get long/double overflows locals"); 1.218 + return; 1.219 + } 1.220 + bool subtype1 = type1.is_assignable_from( 1.221 + _locals[index], verifier()->current_class(), CHECK); 1.222 + bool subtype2 = type2.is_assignable_from( 1.223 + _locals[index+1], verifier()->current_class(), CHECK); 1.224 + if (!subtype1 || !subtype2) { 1.225 + verifier()->verify_error(_offset, "Bad local variable type"); 1.226 + return; 1.227 + } 1.228 +} 1.229 + 1.230 +void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) { 1.231 + assert(!type.is_check(), "Must be a real type"); 1.232 + if (index >= _max_locals) { 1.233 + verifier()->verify_error("Local variable table overflow", _offset); 1.234 + return; 1.235 + } 1.236 + // If type at index is double or long, set the next location to be unusable 1.237 + if (_locals[index].is_double() || _locals[index].is_long()) { 1.238 + assert((index + 1) < _locals_size, "Local variable table overflow"); 1.239 + _locals[index + 1] = VerificationType::bogus_type(); 1.240 + } 1.241 + // If type at index is double_2 or long_2, set the previous location to be unusable 1.242 + if (_locals[index].is_double2() || _locals[index].is_long2()) { 1.243 + assert(index >= 1, "Local variable table underflow"); 1.244 + _locals[index - 1] = VerificationType::bogus_type(); 1.245 + } 1.246 + _locals[index] = type; 1.247 + if (index >= _locals_size) { 1.248 +#ifdef ASSERT 1.249 + for (int i=_locals_size; i<index; i++) { 1.250 + assert(_locals[i] == VerificationType::bogus_type(), 1.251 + "holes must be bogus type"); 1.252 + } 1.253 +#endif 1.254 + _locals_size = index + 1; 1.255 + } 1.256 +} 1.257 + 1.258 +void StackMapFrame::set_local_2( 1.259 + int32_t index, VerificationType type1, VerificationType type2, TRAPS) { 1.260 + assert(type1.is_long() || type1.is_double(), "must be long/double"); 1.261 + assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); 1.262 + if (index >= _max_locals - 1) { 1.263 + verifier()->verify_error("Local variable table overflow", _offset); 1.264 + return; 1.265 + } 1.266 + // If type at index+1 is double or long, set the next location to be unusable 1.267 + if (_locals[index+1].is_double() || _locals[index+1].is_long()) { 1.268 + assert((index + 2) < _locals_size, "Local variable table overflow"); 1.269 + _locals[index + 2] = VerificationType::bogus_type(); 1.270 + } 1.271 + // If type at index is double_2 or long_2, set the previous location to be unusable 1.272 + if (_locals[index].is_double2() || _locals[index].is_long2()) { 1.273 + assert(index >= 1, "Local variable table underflow"); 1.274 + _locals[index - 1] = VerificationType::bogus_type(); 1.275 + } 1.276 + _locals[index] = type1; 1.277 + _locals[index+1] = type2; 1.278 + if (index >= _locals_size - 1) { 1.279 +#ifdef ASSERT 1.280 + for (int i=_locals_size; i<index; i++) { 1.281 + assert(_locals[i] == VerificationType::bogus_type(), 1.282 + "holes must be bogus type"); 1.283 + } 1.284 +#endif 1.285 + _locals_size = index + 2; 1.286 + } 1.287 +} 1.288 + 1.289 +#ifndef PRODUCT 1.290 + 1.291 +void StackMapFrame::print() const { 1.292 + tty->print_cr("stackmap_frame[%d]:", _offset); 1.293 + tty->print_cr("flags = 0x%x", _flags); 1.294 + tty->print("locals[%d] = { ", _locals_size); 1.295 + for (int32_t i = 0; i < _locals_size; i++) { 1.296 + _locals[i].print_on(tty); 1.297 + } 1.298 + tty->print_cr(" }"); 1.299 + tty->print("stack[%d] = { ", _stack_size); 1.300 + for (int32_t j = 0; j < _stack_size; j++) { 1.301 + _stack[j].print_on(tty); 1.302 + } 1.303 + tty->print_cr(" }"); 1.304 +} 1.305 + 1.306 +#endif