1 /*
  2  * Copyright (c) 2003, 2021, 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 "classfile/vmSymbols.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "oops/oop.inline.hpp"
 31 #include "oops/symbol.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "utilities/globalDefinitions.hpp"
 34 
 35 StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) :
 36                       _offset(0), _locals_size(0), _stack_size(0),
 37                       _stack_mark(0), _max_locals(max_locals),
 38                       _max_stack(max_stack), _flags(0), _verifier(v) {
 39   Thread* thr = v->thread();
 40   _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals);
 41   _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack);
 42   int32_t i;
 43   for(i = 0; i < max_locals; i++) {
 44     _locals[i] = VerificationType::bogus_type();
 45   }
 46   for(i = 0; i < max_stack; i++) {
 47     _stack[i] = VerificationType::bogus_type();
 48   }
 49 }
 50 
 51 StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) {
 52   Thread* thr = _verifier->thread();
 53   VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1);
 54   StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier);
 55   return frame;
 56 }
 57 
 58 void StackMapFrame::initialize_object(
 59     VerificationType old_object, VerificationType new_object) {
 60   int32_t i;
 61   for (i = 0; i < _max_locals; i++) {
 62     if (_locals[i].equals(old_object)) {
 63       _locals[i] = new_object;
 64     }
 65   }
 66   for (i = 0; i < _stack_size; i++) {
 67     if (_stack[i].equals(old_object)) {
 68       _stack[i] = new_object;
 69     }
 70   }
 71   if (old_object == VerificationType::uninitialized_this_type()) {
 72     // "this" has been initialized - reset flags
 73     _flags = 0;
 74   }
 75 }
 76 
 77 VerificationType StackMapFrame::set_locals_from_arg(
 78     const methodHandle& m, VerificationType thisKlass) {
 79   SignatureStream ss(m->signature());
 80   int init_local_num = 0;
 81   if (!m->is_static()) {
 82     init_local_num++;
 83     // add one extra argument for instance method
 84     if (m->is_object_constructor() &&
 85        thisKlass.name() != vmSymbols::java_lang_Object()) {
 86       _locals[0] = VerificationType::uninitialized_this_type();
 87       _flags |= FLAG_THIS_UNINIT;
 88     } else {
 89       _locals[0] = thisKlass;
 90     }
 91   }
 92 
 93   // local num may be greater than size of parameters because long/double occupies two slots
 94   while(!ss.at_return_type()) {
 95     init_local_num += _verifier->change_sig_to_verificationType(
 96       &ss, &_locals[init_local_num]);
 97     ss.next();
 98   }
 99   _locals_size = init_local_num;
100 
101   switch (ss.type()) {
102     case T_OBJECT:
103     case T_ARRAY:
104     {
105       Symbol* sig = ss.as_symbol();
106       if (!sig->is_permanent()) {
107         // Create another symbol to save as signature stream unreferences
108         // this symbol.
109         Symbol *sig_copy =
110           verifier()->create_temporary_symbol(sig);
111         assert(sig_copy == sig, "symbols don't match");
112         sig = sig_copy;
113       }
114       return VerificationType::reference_type(sig);
115     }
116     case T_INT:     return VerificationType::integer_type();
117     case T_BYTE:    return VerificationType::byte_type();
118     case T_CHAR:    return VerificationType::char_type();
119     case T_SHORT:   return VerificationType::short_type();
120     case T_BOOLEAN: return VerificationType::boolean_type();
121     case T_FLOAT:   return VerificationType::float_type();
122     case T_DOUBLE:  return VerificationType::double_type();
123     case T_LONG:    return VerificationType::long_type();
124     case T_VOID:    return VerificationType::bogus_type();
125     default:
126       ShouldNotReachHere();
127   }
128   return VerificationType::bogus_type();
129 }
130 
131 void StackMapFrame::copy_locals(const StackMapFrame* src) {
132   int32_t len = src->locals_size() < _locals_size ?
133     src->locals_size() : _locals_size;
134   for (int32_t i = 0; i < len; i++) {
135     _locals[i] = src->locals()[i];
136   }
137 }
138 
139 void StackMapFrame::copy_stack(const StackMapFrame* src) {
140   int32_t len = src->stack_size() < _stack_size ?
141     src->stack_size() : _stack_size;
142   for (int32_t i = 0; i < len; i++) {
143     _stack[i] = src->stack()[i];
144   }
145 }
146 
147 // Returns the location of the first mismatch, or 'len' if there are no
148 // mismatches
149 int StackMapFrame::is_assignable_to(
150     VerificationType* from, VerificationType* to, int32_t len, TRAPS) const {
151   int32_t i = 0;
152   for (i = 0; i < len; i++) {
153     if (!to[i].is_assignable_from(from[i], verifier(), false, THREAD)) {
154       break;
155     }
156   }
157   return i;
158 }
159 
160 bool StackMapFrame::is_assignable_to(
161     const StackMapFrame* target, ErrorContext* ctx, TRAPS) const {
162   if (_max_locals != target->max_locals()) {
163     *ctx = ErrorContext::locals_size_mismatch(
164         _offset, (StackMapFrame*)this, (StackMapFrame*)target);
165     return false;
166   }
167   if (_stack_size != target->stack_size()) {
168     *ctx = ErrorContext::stack_size_mismatch(
169         _offset, (StackMapFrame*)this, (StackMapFrame*)target);
170     return false;
171   }
172   // Only need to compare type elements up to target->locals() or target->stack().
173   // The remaining type elements in this state can be ignored because they are
174   // assignable to bogus type.
175   int mismatch_loc;
176   mismatch_loc = is_assignable_to(
177     _locals, target->locals(), target->locals_size(), THREAD);
178   if (mismatch_loc != target->locals_size()) {
179     *ctx = ErrorContext::bad_type(target->offset(),
180         TypeOrigin::local(mismatch_loc, (StackMapFrame*)this),
181         TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target));
182     return false;
183   }
184   mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD);
185   if (mismatch_loc != _stack_size) {
186     *ctx = ErrorContext::bad_type(target->offset(),
187         TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this),
188         TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target));
189     return false;
190   }
191 
192   if ((_flags | target->flags()) == target->flags()) {
193     return true;
194   } else {
195     *ctx = ErrorContext::bad_flags(target->offset(),
196         (StackMapFrame*)this, (StackMapFrame*)target);
197     return false;
198   }
199 }
200 
201 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
202   if (_stack_size <= 0) {
203     verifier()->verify_error(
204         ErrorContext::stack_underflow(_offset, this),
205         "Operand stack underflow");
206     return VerificationType::bogus_type();
207   }
208   VerificationType top = _stack[--_stack_size];
209   bool subtype = type.is_assignable_from(
210     top, verifier(), false, CHECK_(VerificationType::bogus_type()));
211   if (!subtype) {
212     verifier()->verify_error(
213         ErrorContext::bad_type(_offset, stack_top_ctx(),
214             TypeOrigin::implicit(type)),
215         "Bad type on operand stack");
216     return VerificationType::bogus_type();
217   }
218   return top;
219 }
220 
221 VerificationType StackMapFrame::get_local(
222     int32_t index, VerificationType type, TRAPS) {
223   if (index >= _max_locals) {
224     verifier()->verify_error(
225         ErrorContext::bad_local_index(_offset, index),
226         "Local variable table overflow");
227     return VerificationType::bogus_type();
228   }
229   bool subtype = type.is_assignable_from(_locals[index],
230     verifier(), false, CHECK_(VerificationType::bogus_type()));
231   if (!subtype) {
232     verifier()->verify_error(
233         ErrorContext::bad_type(_offset,
234           TypeOrigin::local(index, this),
235           TypeOrigin::implicit(type)),
236         "Bad local variable type");
237     return VerificationType::bogus_type();
238   }
239   if(index >= _locals_size) { _locals_size = index + 1; }
240   return _locals[index];
241 }
242 
243 void StackMapFrame::get_local_2(
244     int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
245   assert(type1.is_long() || type1.is_double(), "must be long/double");
246   assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
247   if (index >= _locals_size - 1) {
248     verifier()->verify_error(
249         ErrorContext::bad_local_index(_offset, index),
250         "get long/double overflows locals");
251     return;
252   }
253   bool subtype = type1.is_assignable_from(_locals[index], verifier(), false, CHECK);
254   if (!subtype) {
255     verifier()->verify_error(
256         ErrorContext::bad_type(_offset,
257             TypeOrigin::local(index, this), TypeOrigin::implicit(type1)),
258         "Bad local variable type");
259   } else {
260     subtype = type2.is_assignable_from(_locals[index + 1], verifier(), false, CHECK);
261     if (!subtype) {
262       /* Unreachable? All local store routines convert a split long or double
263        * into a TOP during the store.  So we should never end up seeing an
264        * orphaned half.  */
265       verifier()->verify_error(
266           ErrorContext::bad_type(_offset,
267               TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)),
268           "Bad local variable type");
269     }
270   }
271 }
272 
273 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
274   assert(!type.is_check(), "Must be a real type");
275   if (index >= _max_locals) {
276     verifier()->verify_error(
277         ErrorContext::bad_local_index(_offset, index),
278         "Local variable table overflow");
279     return;
280   }
281   // If type at index is double or long, set the next location to be unusable
282   if (_locals[index].is_double() || _locals[index].is_long()) {
283     assert((index + 1) < _locals_size, "Local variable table overflow");
284     _locals[index + 1] = VerificationType::bogus_type();
285   }
286   // If type at index is double_2 or long_2, set the previous location to be unusable
287   if (_locals[index].is_double2() || _locals[index].is_long2()) {
288     assert(index >= 1, "Local variable table underflow");
289     _locals[index - 1] = VerificationType::bogus_type();
290   }
291   _locals[index] = type;
292   if (index >= _locals_size) {
293 #ifdef ASSERT
294     for (int i=_locals_size; i<index; i++) {
295       assert(_locals[i] == VerificationType::bogus_type(),
296              "holes must be bogus type");
297     }
298 #endif
299     _locals_size = index + 1;
300   }
301 }
302 
303 void StackMapFrame::set_local_2(
304     int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
305   assert(type1.is_long() || type1.is_double(), "must be long/double");
306   assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
307   if (index >= _max_locals - 1) {
308     verifier()->verify_error(
309         ErrorContext::bad_local_index(_offset, index),
310         "Local variable table overflow");
311     return;
312   }
313   // If type at index+1 is double or long, set the next location to be unusable
314   if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
315     assert((index + 2) < _locals_size, "Local variable table overflow");
316     _locals[index + 2] = VerificationType::bogus_type();
317   }
318   // If type at index is double_2 or long_2, set the previous location to be unusable
319   if (_locals[index].is_double2() || _locals[index].is_long2()) {
320     assert(index >= 1, "Local variable table underflow");
321     _locals[index - 1] = VerificationType::bogus_type();
322   }
323   _locals[index] = type1;
324   _locals[index+1] = type2;
325   if (index >= _locals_size - 1) {
326 #ifdef ASSERT
327     for (int i=_locals_size; i<index; i++) {
328       assert(_locals[i] == VerificationType::bogus_type(),
329              "holes must be bogus type");
330     }
331 #endif
332     _locals_size = index + 2;
333   }
334 }
335 
336 TypeOrigin StackMapFrame::stack_top_ctx() {
337   return TypeOrigin::stack(_stack_size, this);
338 }
339 
340 void StackMapFrame::print_on(outputStream* str) const {
341   str->indent().print_cr("bci: @%d", _offset);
342   str->indent().print_cr("flags: {%s }",
343       flag_this_uninit() ? " flagThisUninit" : "");
344   str->indent().print("locals: {");
345   for (int32_t i = 0; i < _locals_size; ++i) {
346     str->print(" ");
347     _locals[i].print_on(str);
348     if (i != _locals_size - 1) {
349       str->print(",");
350     }
351   }
352   str->print_cr(" }");
353   str->indent().print("stack: {");
354   for (int32_t j = 0; j < _stack_size; ++j) {
355     str->print(" ");
356     _stack[j].print_on(str);
357     if (j != _stack_size - 1) {
358       str->print(",");
359     }
360   }
361   str->print_cr(" }");
362 }