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     case T_INLINE_TYPE:
105     {
106       Symbol* sig = ss.as_symbol();
107       if (!sig->is_permanent()) {
108         // Create another symbol to save as signature stream unreferences
109         // this symbol.
110         Symbol *sig_copy =
111           verifier()->create_temporary_symbol(sig);
112         assert(sig_copy == sig, "symbols don't match");
113         sig = sig_copy;
114       }
115       if (ss.type() == T_INLINE_TYPE) {
116         return VerificationType::inline_type(sig);
117       }
118       return VerificationType::reference_type(sig);
119     }
120     case T_INT:     return VerificationType::integer_type();
121     case T_BYTE:    return VerificationType::byte_type();
122     case T_CHAR:    return VerificationType::char_type();
123     case T_SHORT:   return VerificationType::short_type();
124     case T_BOOLEAN: return VerificationType::boolean_type();
125     case T_FLOAT:   return VerificationType::float_type();
126     case T_DOUBLE:  return VerificationType::double_type();
127     case T_LONG:    return VerificationType::long_type();
128     case T_VOID:    return VerificationType::bogus_type();
129     default:
130       ShouldNotReachHere();
131   }
132   return VerificationType::bogus_type();
133 }
134 
135 void StackMapFrame::copy_locals(const StackMapFrame* src) {
136   int32_t len = src->locals_size() < _locals_size ?
137     src->locals_size() : _locals_size;
138   for (int32_t i = 0; i < len; i++) {
139     _locals[i] = src->locals()[i];
140   }
141 }
142 
143 void StackMapFrame::copy_stack(const StackMapFrame* src) {
144   int32_t len = src->stack_size() < _stack_size ?
145     src->stack_size() : _stack_size;
146   for (int32_t i = 0; i < len; i++) {
147     _stack[i] = src->stack()[i];
148   }
149 }
150 
151 // Returns the location of the first mismatch, or 'len' if there are no
152 // mismatches
153 int StackMapFrame::is_assignable_to(
154     VerificationType* from, VerificationType* to, int32_t len, TRAPS) const {
155   int32_t i = 0;
156   for (i = 0; i < len; i++) {
157     if (!to[i].is_assignable_from(from[i], verifier(), false, THREAD)) {
158       break;
159     }
160   }
161   return i;
162 }
163 
164 bool StackMapFrame::is_assignable_to(
165     const StackMapFrame* target, ErrorContext* ctx, TRAPS) const {
166   if (_max_locals != target->max_locals()) {
167     *ctx = ErrorContext::locals_size_mismatch(
168         _offset, (StackMapFrame*)this, (StackMapFrame*)target);
169     return false;
170   }
171   if (_stack_size != target->stack_size()) {
172     *ctx = ErrorContext::stack_size_mismatch(
173         _offset, (StackMapFrame*)this, (StackMapFrame*)target);
174     return false;
175   }
176   // Only need to compare type elements up to target->locals() or target->stack().
177   // The remaining type elements in this state can be ignored because they are
178   // assignable to bogus type.
179   int mismatch_loc;
180   mismatch_loc = is_assignable_to(
181     _locals, target->locals(), target->locals_size(), THREAD);
182   if (mismatch_loc != target->locals_size()) {
183     *ctx = ErrorContext::bad_type(target->offset(),
184         TypeOrigin::local(mismatch_loc, (StackMapFrame*)this),
185         TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target));
186     return false;
187   }
188   mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD);
189   if (mismatch_loc != _stack_size) {
190     *ctx = ErrorContext::bad_type(target->offset(),
191         TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this),
192         TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target));
193     return false;
194   }
195 
196   if ((_flags | target->flags()) == target->flags()) {
197     return true;
198   } else {
199     *ctx = ErrorContext::bad_flags(target->offset(),
200         (StackMapFrame*)this, (StackMapFrame*)target);
201     return false;
202   }
203 }
204 
205 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
206   if (_stack_size <= 0) {
207     verifier()->verify_error(
208         ErrorContext::stack_underflow(_offset, this),
209         "Operand stack underflow");
210     return VerificationType::bogus_type();
211   }
212   VerificationType top = _stack[--_stack_size];
213   bool subtype = type.is_assignable_from(
214     top, verifier(), false, CHECK_(VerificationType::bogus_type()));
215   if (!subtype) {
216     verifier()->verify_error(
217         ErrorContext::bad_type(_offset, stack_top_ctx(),
218             TypeOrigin::implicit(type)),
219         "Bad type on operand stack");
220     return VerificationType::bogus_type();
221   }
222   return top;
223 }
224 
225 VerificationType StackMapFrame::get_local(
226     int32_t index, VerificationType type, TRAPS) {
227   if (index >= _max_locals) {
228     verifier()->verify_error(
229         ErrorContext::bad_local_index(_offset, index),
230         "Local variable table overflow");
231     return VerificationType::bogus_type();
232   }
233   bool subtype = type.is_assignable_from(_locals[index],
234     verifier(), false, CHECK_(VerificationType::bogus_type()));
235   if (!subtype) {
236     verifier()->verify_error(
237         ErrorContext::bad_type(_offset,
238           TypeOrigin::local(index, this),
239           TypeOrigin::implicit(type)),
240         "Bad local variable type");
241     return VerificationType::bogus_type();
242   }
243   if(index >= _locals_size) { _locals_size = index + 1; }
244   return _locals[index];
245 }
246 
247 void StackMapFrame::get_local_2(
248     int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
249   assert(type1.is_long() || type1.is_double(), "must be long/double");
250   assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
251   if (index >= _locals_size - 1) {
252     verifier()->verify_error(
253         ErrorContext::bad_local_index(_offset, index),
254         "get long/double overflows locals");
255     return;
256   }
257   bool subtype = type1.is_assignable_from(_locals[index], verifier(), false, CHECK);
258   if (!subtype) {
259     verifier()->verify_error(
260         ErrorContext::bad_type(_offset,
261             TypeOrigin::local(index, this), TypeOrigin::implicit(type1)),
262         "Bad local variable type");
263   } else {
264     subtype = type2.is_assignable_from(_locals[index + 1], verifier(), false, CHECK);
265     if (!subtype) {
266       /* Unreachable? All local store routines convert a split long or double
267        * into a TOP during the store.  So we should never end up seeing an
268        * orphaned half.  */
269       verifier()->verify_error(
270           ErrorContext::bad_type(_offset,
271               TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)),
272           "Bad local variable type");
273     }
274   }
275 }
276 
277 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
278   assert(!type.is_check(), "Must be a real type");
279   if (index >= _max_locals) {
280     verifier()->verify_error(
281         ErrorContext::bad_local_index(_offset, index),
282         "Local variable table overflow");
283     return;
284   }
285   // If type at index is double or long, set the next location to be unusable
286   if (_locals[index].is_double() || _locals[index].is_long()) {
287     assert((index + 1) < _locals_size, "Local variable table overflow");
288     _locals[index + 1] = VerificationType::bogus_type();
289   }
290   // If type at index is double_2 or long_2, set the previous location to be unusable
291   if (_locals[index].is_double2() || _locals[index].is_long2()) {
292     assert(index >= 1, "Local variable table underflow");
293     _locals[index - 1] = VerificationType::bogus_type();
294   }
295   _locals[index] = type;
296   if (index >= _locals_size) {
297 #ifdef ASSERT
298     for (int i=_locals_size; i<index; i++) {
299       assert(_locals[i] == VerificationType::bogus_type(),
300              "holes must be bogus type");
301     }
302 #endif
303     _locals_size = index + 1;
304   }
305 }
306 
307 void StackMapFrame::set_local_2(
308     int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
309   assert(type1.is_long() || type1.is_double(), "must be long/double");
310   assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
311   if (index >= _max_locals - 1) {
312     verifier()->verify_error(
313         ErrorContext::bad_local_index(_offset, index),
314         "Local variable table overflow");
315     return;
316   }
317   // If type at index+1 is double or long, set the next location to be unusable
318   if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
319     assert((index + 2) < _locals_size, "Local variable table overflow");
320     _locals[index + 2] = VerificationType::bogus_type();
321   }
322   // If type at index is double_2 or long_2, set the previous location to be unusable
323   if (_locals[index].is_double2() || _locals[index].is_long2()) {
324     assert(index >= 1, "Local variable table underflow");
325     _locals[index - 1] = VerificationType::bogus_type();
326   }
327   _locals[index] = type1;
328   _locals[index+1] = type2;
329   if (index >= _locals_size - 1) {
330 #ifdef ASSERT
331     for (int i=_locals_size; i<index; i++) {
332       assert(_locals[i] == VerificationType::bogus_type(),
333              "holes must be bogus type");
334     }
335 #endif
336     _locals_size = index + 2;
337   }
338 }
339 
340 TypeOrigin StackMapFrame::stack_top_ctx() {
341   return TypeOrigin::stack(_stack_size, this);
342 }
343 
344 void StackMapFrame::print_on(outputStream* str) const {
345   str->indent().print_cr("bci: @%d", _offset);
346   str->indent().print_cr("flags: {%s }",
347       flag_this_uninit() ? " flagThisUninit" : "");
348   str->indent().print("locals: {");
349   for (int32_t i = 0; i < _locals_size; ++i) {
350     str->print(" ");
351     _locals[i].print_on(str);
352     if (i != _locals_size - 1) {
353       str->print(",");
354     }
355   }
356   str->print_cr(" }");
357   str->indent().print("stack: {");
358   for (int32_t j = 0; j < _stack_size; ++j) {
359     str->print(" ");
360     _stack[j].print_on(str);
361     if (j != _stack_size - 1) {
362       str->print(",");
363     }
364   }
365   str->print_cr(" }");
366 }