stackMapFrame.cpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 2003, 2006, 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 "incls/_precompiled.incl"
26# include "incls/_stackMapFrame.cpp.incl"
27
28StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) :
29                      _offset(0), _locals_size(0), _stack_size(0), _flags(0),
30                      _max_locals(max_locals), _max_stack(max_stack),
31                      _verifier(v) {
32  Thread* thr = v->thread();
33  _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals);
34  _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack);
35  int32_t i;
36  for(i = 0; i < max_locals; i++) {
37    _locals[i] = VerificationType::bogus_type();
38  }
39  for(i = 0; i < max_stack; i++) {
40    _stack[i] = VerificationType::bogus_type();
41  }
42}
43
44StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) {
45  Thread* thr = _verifier->thread();
46  VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1);
47  StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier);
48  return frame;
49}
50
51bool StackMapFrame::has_new_object() const {
52  int32_t i;
53  for (i = 0; i < _max_locals; i++) {
54    if (_locals[i].is_uninitialized()) {
55      return true;
56    }
57  }
58  for (i = 0; i < _stack_size; i++) {
59    if (_stack[i].is_uninitialized()) {
60      return true;
61    }
62  }
63  return false;
64}
65
66void StackMapFrame::initialize_object(
67    VerificationType old_object, VerificationType new_object) {
68  int32_t i;
69  for (i = 0; i < _max_locals; i++) {
70    if (_locals[i].equals(old_object)) {
71      _locals[i] = new_object;
72    }
73  }
74  for (i = 0; i < _stack_size; i++) {
75    if (_stack[i].equals(old_object)) {
76      _stack[i] = new_object;
77    }
78  }
79  if (old_object == VerificationType::uninitialized_this_type()) {
80    // "this" has been initialized - reset flags
81    _flags = 0;
82  }
83}
84
85VerificationType StackMapFrame::set_locals_from_arg(
86    const methodHandle m, VerificationType thisKlass, TRAPS) {
87  symbolHandle signature(THREAD, m->signature());
88  SignatureStream ss(signature);
89  int init_local_num = 0;
90  if (!m->is_static()) {
91    init_local_num++;
92    // add one extra argument for instance method
93    if (m->name() == vmSymbols::object_initializer_name() &&
94       thisKlass.name() != vmSymbols::java_lang_Object()) {
95      _locals[0] = VerificationType::uninitialized_this_type();
96      _flags |= FLAG_THIS_UNINIT;
97    } else {
98      _locals[0] = thisKlass;
99    }
100  }
101
102  // local num may be greater than size of parameters because long/double occupies two slots
103  while(!ss.at_return_type()) {
104    init_local_num += _verifier->change_sig_to_verificationType(
105      &ss, &_locals[init_local_num],
106      CHECK_VERIFY_(verifier(), VerificationType::bogus_type()));
107    ss.next();
108  }
109  _locals_size = init_local_num;
110
111  switch (ss.type()) {
112    case T_OBJECT:
113    case T_ARRAY:
114    {
115      symbolOop sig = ss.as_symbol(CHECK_(VerificationType::bogus_type()));
116      return VerificationType::reference_type(symbolHandle(THREAD, sig));
117    }
118    case T_INT:     return VerificationType::integer_type();
119    case T_BYTE:    return VerificationType::byte_type();
120    case T_CHAR:    return VerificationType::char_type();
121    case T_SHORT:   return VerificationType::short_type();
122    case T_BOOLEAN: return VerificationType::boolean_type();
123    case T_FLOAT:   return VerificationType::float_type();
124    case T_DOUBLE:  return VerificationType::double_type();
125    case T_LONG:    return VerificationType::long_type();
126    case T_VOID:    return VerificationType::bogus_type();
127    default:
128      ShouldNotReachHere();
129  }
130  return VerificationType::bogus_type();
131}
132
133void StackMapFrame::copy_locals(const StackMapFrame* src) {
134  int32_t len = src->locals_size() < _locals_size ?
135    src->locals_size() : _locals_size;
136  for (int32_t i = 0; i < len; i++) {
137    _locals[i] = src->locals()[i];
138  }
139}
140
141void StackMapFrame::copy_stack(const StackMapFrame* src) {
142  int32_t len = src->stack_size() < _stack_size ?
143    src->stack_size() : _stack_size;
144  for (int32_t i = 0; i < len; i++) {
145    _stack[i] = src->stack()[i];
146  }
147}
148
149
150bool StackMapFrame::is_assignable_to(
151    VerificationType* from, VerificationType* to, int32_t len, TRAPS) const {
152  for (int32_t i = 0; i < len; i++) {
153    bool subtype = to[i].is_assignable_from(
154      from[i], verifier()->current_class(), THREAD);
155    if (!subtype) {
156      return false;
157    }
158  }
159  return true;
160}
161
162bool StackMapFrame::is_assignable_to(const StackMapFrame* target, TRAPS) const {
163  if (_max_locals != target->max_locals() || _stack_size != target->stack_size()) {
164    return false;
165  }
166  // Only need to compare type elements up to target->locals() or target->stack().
167  // The remaining type elements in this state can be ignored because they are
168  // assignable to bogus type.
169  bool match_locals = is_assignable_to(
170    _locals, target->locals(), target->locals_size(), CHECK_false);
171  bool match_stack = is_assignable_to(
172    _stack, target->stack(), _stack_size, CHECK_false);
173  bool match_flags = (_flags | target->flags()) == target->flags();
174  return (match_locals && match_stack && match_flags);
175}
176
177VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
178  if (_stack_size <= 0) {
179    verifier()->verify_error(_offset, "Operand stack underflow");
180    return VerificationType::bogus_type();
181  }
182  VerificationType top = _stack[--_stack_size];
183  bool subtype = type.is_assignable_from(
184    top, verifier()->current_class(), CHECK_(VerificationType::bogus_type()));
185  if (!subtype) {
186    verifier()->verify_error(_offset, "Bad type on operand stack");
187    return VerificationType::bogus_type();
188  }
189  NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); )
190  return top;
191}
192
193VerificationType StackMapFrame::get_local(
194    int32_t index, VerificationType type, TRAPS) {
195  if (index >= _max_locals) {
196    verifier()->verify_error(_offset, "Local variable table overflow");
197    return VerificationType::bogus_type();
198  }
199  bool subtype = type.is_assignable_from(_locals[index],
200    verifier()->current_class(), CHECK_(VerificationType::bogus_type()));
201  if (!subtype) {
202    verifier()->verify_error(_offset, "Bad local variable type");
203    return VerificationType::bogus_type();
204  }
205  if(index >= _locals_size) { _locals_size = index + 1; }
206  return _locals[index];
207}
208
209void StackMapFrame::get_local_2(
210    int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
211  assert(type1.is_long() || type1.is_double(), "must be long/double");
212  assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
213  if (index >= _locals_size - 1) {
214    verifier()->verify_error(_offset, "get long/double overflows locals");
215    return;
216  }
217  bool subtype1 = type1.is_assignable_from(
218    _locals[index], verifier()->current_class(), CHECK);
219  bool subtype2 = type2.is_assignable_from(
220    _locals[index+1], verifier()->current_class(), CHECK);
221  if (!subtype1 || !subtype2) {
222    verifier()->verify_error(_offset, "Bad local variable type");
223    return;
224  }
225}
226
227void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
228  assert(!type.is_check(), "Must be a real type");
229  if (index >= _max_locals) {
230    verifier()->verify_error("Local variable table overflow", _offset);
231    return;
232  }
233  // If type at index is double or long, set the next location to be unusable
234  if (_locals[index].is_double() || _locals[index].is_long()) {
235    assert((index + 1) < _locals_size, "Local variable table overflow");
236    _locals[index + 1] = VerificationType::bogus_type();
237  }
238  // If type at index is double_2 or long_2, set the previous location to be unusable
239  if (_locals[index].is_double2() || _locals[index].is_long2()) {
240    assert(index >= 1, "Local variable table underflow");
241    _locals[index - 1] = VerificationType::bogus_type();
242  }
243  _locals[index] = type;
244  if (index >= _locals_size) {
245#ifdef ASSERT
246    for (int i=_locals_size; i<index; i++) {
247      assert(_locals[i] == VerificationType::bogus_type(),
248             "holes must be bogus type");
249    }
250#endif
251    _locals_size = index + 1;
252  }
253}
254
255void StackMapFrame::set_local_2(
256    int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
257  assert(type1.is_long() || type1.is_double(), "must be long/double");
258  assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
259  if (index >= _max_locals - 1) {
260    verifier()->verify_error("Local variable table overflow", _offset);
261    return;
262  }
263  // If type at index+1 is double or long, set the next location to be unusable
264  if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
265    assert((index + 2) < _locals_size, "Local variable table overflow");
266    _locals[index + 2] = VerificationType::bogus_type();
267  }
268  // If type at index is double_2 or long_2, set the previous location to be unusable
269  if (_locals[index].is_double2() || _locals[index].is_long2()) {
270    assert(index >= 1, "Local variable table underflow");
271    _locals[index - 1] = VerificationType::bogus_type();
272  }
273  _locals[index] = type1;
274  _locals[index+1] = type2;
275  if (index >= _locals_size - 1) {
276#ifdef ASSERT
277    for (int i=_locals_size; i<index; i++) {
278      assert(_locals[i] == VerificationType::bogus_type(),
279             "holes must be bogus type");
280    }
281#endif
282    _locals_size = index + 2;
283  }
284}
285
286#ifndef PRODUCT
287
288void StackMapFrame::print() const {
289  tty->print_cr("stackmap_frame[%d]:", _offset);
290  tty->print_cr("flags = 0x%x", _flags);
291  tty->print("locals[%d] = { ", _locals_size);
292  for (int32_t i = 0; i < _locals_size; i++) {
293    _locals[i].print_on(tty);
294  }
295  tty->print_cr(" }");
296  tty->print("stack[%d] = { ", _stack_size);
297  for (int32_t j = 0; j < _stack_size; j++) {
298    _stack[j].print_on(tty);
299  }
300  tty->print_cr(" }");
301}
302
303#endif
304