1 /*
  2  * Copyright (c) 2003, 2020, 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/stackMapTable.hpp"
 27 #include "classfile/verifier.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "oops/constantPool.hpp"
 30 #include "oops/oop.inline.hpp"
 31 #include "runtime/handles.inline.hpp"
 32 
 33 StackMapTable::StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
 34                              u2 max_locals, u2 max_stack,
 35                              char* code_data, int code_len, TRAPS) {
 36   _code_length = code_len;
 37   _frame_count = reader->get_frame_count();
 38   if (_frame_count > 0) {
 39     _frame_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
 40                                                 StackMapFrame*, _frame_count);
 41     StackMapFrame* pre_frame = init_frame;
 42     for (int32_t i = 0; i < _frame_count; i++) {
 43       StackMapFrame* frame = reader->next(
 44         pre_frame, i == 0, max_locals, max_stack,
 45         CHECK_VERIFY(pre_frame->verifier()));
 46       _frame_array[i] = frame;
 47       int offset = frame->offset();
 48       if (offset >= code_len || code_data[offset] == 0) {
 49         frame->verifier()->verify_error(
 50             ErrorContext::bad_stackmap(i, frame),
 51             "StackMapTable error: bad offset");
 52         return;
 53       }
 54       pre_frame = frame;
 55     }
 56   }
 57   reader->check_end(CHECK);
 58 }
 59 
 60 // This method is only called by method in StackMapTable.
 61 int StackMapTable::get_index_from_offset(int32_t offset) const {
 62   int i = 0;
 63   for (; i < _frame_count; i++) {
 64     if (_frame_array[i]->offset() == offset) {
 65       return i;
 66     }
 67   }
 68   return i;  // frame with offset doesn't exist in the array
 69 }
 70 
 71 bool StackMapTable::match_stackmap(
 72     StackMapFrame* frame, int32_t target,
 73     bool match, bool update, ErrorContext* ctx, TRAPS) const {
 74   int index = get_index_from_offset(target);
 75   return match_stackmap(frame, target, index, match, update, ctx, THREAD);
 76 }
 77 
 78 // Match and/or update current_frame to the frame in stackmap table with
 79 // specified offset and frame index. Return true if the two frames match.
 80 //
 81 // The values of match and update are:                  _match__update
 82 //
 83 // checking a branch target:                             true   false
 84 // checking an exception handler:                        true   false
 85 // linear bytecode verification following an
 86 // unconditional branch:                                 false  true
 87 // linear bytecode verification not following an
 88 // unconditional branch:                                 true   true
 89 bool StackMapTable::match_stackmap(
 90     StackMapFrame* frame, int32_t target, int32_t frame_index,
 91     bool match, bool update, ErrorContext* ctx, TRAPS) const {
 92   if (frame_index < 0 || frame_index >= _frame_count) {
 93     *ctx = ErrorContext::missing_stackmap(frame->offset());
 94     frame->verifier()->verify_error(
 95         *ctx, "Expecting a stackmap frame at branch target %d", target);
 96     return false;
 97   }
 98 
 99   StackMapFrame *stackmap_frame = _frame_array[frame_index];
100   bool result = true;
101   if (match) {
102     // Has direct control flow from last instruction, need to match the two
103     // frames.
104     result = frame->is_assignable_to(stackmap_frame,
105         ctx, CHECK_VERIFY_(frame->verifier(), result));
106   }
107   if (update) {
108     // Use the frame in stackmap table as current frame
109     int lsize = stackmap_frame->locals_size();
110     int ssize = stackmap_frame->stack_size();
111     if (frame->locals_size() > lsize || frame->stack_size() > ssize) {
112       // Make sure unused type array items are all _bogus_type.
113       frame->reset();
114     }
115     frame->set_locals_size(lsize);
116     frame->copy_locals(stackmap_frame);
117     frame->set_stack_size(ssize);
118     frame->copy_stack(stackmap_frame);
119     frame->set_flags(stackmap_frame->flags());
120   }
121   return result;
122 }
123 
124 void StackMapTable::check_jump_target(
125     StackMapFrame* frame, int32_t target, TRAPS) const {
126   ErrorContext ctx;
127   bool match = match_stackmap(
128     frame, target, true, false, &ctx, CHECK_VERIFY(frame->verifier()));
129   if (!match || (target < 0 || target >= _code_length)) {
130     frame->verifier()->verify_error(ctx,
131         "Inconsistent stackmap frames at branch target %d", target);
132   }
133 }
134 
135 void StackMapTable::print_on(outputStream* str) const {
136   str->indent().print_cr("StackMapTable: frame_count = %d", _frame_count);
137   str->indent().print_cr("table = { ");
138   {
139     streamIndentor si(str);
140     for (int32_t i = 0; i < _frame_count; ++i) {
141       _frame_array[i]->print_on(str);
142     }
143   }
144   str->print_cr(" }");
145 }
146 
147 StackMapReader::StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data,
148                                int32_t code_len, TRAPS) :
149                                _verifier(v), _stream(stream),
150                                _code_data(code_data), _code_length(code_len) {
151   methodHandle m = v->method();
152   if (m->has_stackmap_table()) {
153     _cp = constantPoolHandle(THREAD, m->constants());
154     _frame_count = _stream->get_u2(CHECK);
155   } else {
156     // There's no stackmap table present. Frame count and size are 0.
157     _frame_count = 0;
158   }
159 }
160 
161 int32_t StackMapReader::chop(
162     VerificationType* locals, int32_t length, int32_t chops) {
163   if (locals == NULL) return -1;
164   int32_t pos = length - 1;
165   for (int32_t i=0; i<chops; i++) {
166     if (locals[pos].is_category2_2nd()) {
167       pos -= 2;
168     } else {
169       pos --;
170     }
171     if (pos<0 && i<(chops-1)) return -1;
172   }
173   return pos+1;
174 }
175 
176 VerificationType StackMapReader::parse_verification_type(u1* flags, TRAPS) {
177   u1 tag = _stream->get_u1(THREAD);
178   if (tag < (u1)ITEM_UninitializedThis) {
179     return VerificationType::from_tag(tag);
180   }
181   if (tag == ITEM_Object) {
182     u2 class_index = _stream->get_u2(THREAD);
183     int nconstants = _cp->length();
184     if ((class_index <= 0 || class_index >= nconstants) ||
185         (!_cp->tag_at(class_index).is_klass() &&
186          !_cp->tag_at(class_index).is_unresolved_klass())) {
187       _stream->stackmap_format_error("bad class index", THREAD);
188       return VerificationType::bogus_type();
189     }
190     Symbol* klass_name = _cp->klass_name_at(class_index);
191     if (klass_name->is_Q_signature()) {
192       Symbol* fund_name = klass_name->fundamental_name(THREAD);
193       if (fund_name == NULL) {
194         _stream->stackmap_format_error("TBD something bad happened", THREAD);
195         return VerificationType::bogus_type();
196       }
197       return VerificationType::inline_type(fund_name);
198     } else {
199       return VerificationType::reference_type(klass_name);
200     }
201   }
202   if (tag == ITEM_UninitializedThis) {
203     if (flags != NULL) {
204       *flags |= FLAG_THIS_UNINIT;
205     }
206     return VerificationType::uninitialized_this_type();
207   }
208   if (tag == ITEM_Uninitialized) {
209     u2 offset = _stream->get_u2(THREAD);
210     if (offset >= _code_length ||
211         _code_data[offset] != ClassVerifier::NEW_OFFSET) {
212       _verifier->class_format_error(
213         "StackMapTable format error: bad offset for Uninitialized");
214       return VerificationType::bogus_type();
215     }
216     return VerificationType::uninitialized_type(offset);
217   }
218   _stream->stackmap_format_error("bad verification type", THREAD);
219   return VerificationType::bogus_type();
220 }
221 
222 StackMapFrame* StackMapReader::next(
223     StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) {
224   StackMapFrame* frame;
225   int offset;
226   VerificationType* locals = NULL;
227   u1 frame_type = _stream->get_u1(THREAD);
228   if (frame_type < 64) {
229     // same_frame
230     if (first) {
231       offset = frame_type;
232       // Can't share the locals array since that is updated by the verifier.
233       if (pre_frame->locals_size() > 0) {
234         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
235           THREAD, VerificationType, pre_frame->locals_size());
236       }
237     } else {
238       offset = pre_frame->offset() + frame_type + 1;
239       locals = pre_frame->locals();
240     }
241     frame = new StackMapFrame(
242       offset, pre_frame->flags(), pre_frame->locals_size(), 0,
243       max_locals, max_stack, locals, NULL, _verifier);
244     if (first && locals != NULL) {
245       frame->copy_locals(pre_frame);
246     }
247     return frame;
248   }
249   if (frame_type < 128) {
250     // same_locals_1_stack_item_frame
251     if (first) {
252       offset = frame_type - 64;
253       // Can't share the locals array since that is updated by the verifier.
254       if (pre_frame->locals_size() > 0) {
255         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
256           THREAD, VerificationType, pre_frame->locals_size());
257       }
258     } else {
259       offset = pre_frame->offset() + frame_type - 63;
260       locals = pre_frame->locals();
261     }
262     VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
263       THREAD, VerificationType, 2);
264     u2 stack_size = 1;
265     stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
266     if (stack[0].is_category2()) {
267       stack[1] = stack[0].to_category2_2nd();
268       stack_size = 2;
269     }
270     check_verification_type_array_size(
271       stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
272     frame = new StackMapFrame(
273       offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
274       max_locals, max_stack, locals, stack, _verifier);
275     if (first && locals != NULL) {
276       frame->copy_locals(pre_frame);
277     }
278     return frame;
279   }
280 
281   u2 offset_delta = _stream->get_u2(THREAD);
282 
283   if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
284     // reserved frame types
285     _stream->stackmap_format_error(
286       "reserved frame type", CHECK_VERIFY_(_verifier, NULL));
287   }
288 
289   if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
290     // same_locals_1_stack_item_frame_extended
291     if (first) {
292       offset = offset_delta;
293       // Can't share the locals array since that is updated by the verifier.
294       if (pre_frame->locals_size() > 0) {
295         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
296           THREAD, VerificationType, pre_frame->locals_size());
297       }
298     } else {
299       offset = pre_frame->offset() + offset_delta + 1;
300       locals = pre_frame->locals();
301     }
302     VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
303       THREAD, VerificationType, 2);
304     u2 stack_size = 1;
305     stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
306     if (stack[0].is_category2()) {
307       stack[1] = stack[0].to_category2_2nd();
308       stack_size = 2;
309     }
310     check_verification_type_array_size(
311       stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
312     frame = new StackMapFrame(
313       offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
314       max_locals, max_stack, locals, stack, _verifier);
315     if (first && locals != NULL) {
316       frame->copy_locals(pre_frame);
317     }
318     return frame;
319   }
320 
321   if (frame_type <= SAME_EXTENDED) {
322     // chop_frame or same_frame_extended
323     locals = pre_frame->locals();
324     int length = pre_frame->locals_size();
325     int chops = SAME_EXTENDED - frame_type;
326     int new_length = length;
327     u1 flags = pre_frame->flags();
328     if (chops != 0) {
329       new_length = chop(locals, length, chops);
330       check_verification_type_array_size(
331         new_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
332       // Recompute flags since uninitializedThis could have been chopped.
333       flags = 0;
334       for (int i=0; i<new_length; i++) {
335         if (locals[i].is_uninitialized_this()) {
336           flags |= FLAG_THIS_UNINIT;
337           break;
338         }
339       }
340     }
341     if (first) {
342       offset = offset_delta;
343       // Can't share the locals array since that is updated by the verifier.
344       if (new_length > 0) {
345         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
346           THREAD, VerificationType, new_length);
347       } else {
348         locals = NULL;
349       }
350     } else {
351       offset = pre_frame->offset() + offset_delta + 1;
352     }
353     frame = new StackMapFrame(
354       offset, flags, new_length, 0, max_locals, max_stack,
355       locals, NULL, _verifier);
356     if (first && locals != NULL) {
357       frame->copy_locals(pre_frame);
358     }
359     return frame;
360   } else if (frame_type < SAME_EXTENDED + 4) {
361     // append_frame
362     int appends = frame_type - SAME_EXTENDED;
363     int real_length = pre_frame->locals_size();
364     int new_length = real_length + appends*2;
365     locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length);
366     VerificationType* pre_locals = pre_frame->locals();
367     int i;
368     for (i=0; i<pre_frame->locals_size(); i++) {
369       locals[i] = pre_locals[i];
370     }
371     u1 flags = pre_frame->flags();
372     for (i=0; i<appends; i++) {
373       locals[real_length] = parse_verification_type(&flags, THREAD);
374       if (locals[real_length].is_category2()) {
375         locals[real_length + 1] = locals[real_length].to_category2_2nd();
376         ++real_length;
377       }
378       ++real_length;
379     }
380     check_verification_type_array_size(
381       real_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
382     if (first) {
383       offset = offset_delta;
384     } else {
385       offset = pre_frame->offset() + offset_delta + 1;
386     }
387     frame = new StackMapFrame(
388       offset, flags, real_length, 0, max_locals,
389       max_stack, locals, NULL, _verifier);
390     return frame;
391   }
392   if (frame_type == FULL) {
393     // full_frame
394     u1 flags = 0;
395     u2 locals_size = _stream->get_u2(THREAD);
396     int real_locals_size = 0;
397     if (locals_size > 0) {
398       locals = NEW_RESOURCE_ARRAY_IN_THREAD(
399         THREAD, VerificationType, locals_size*2);
400     }
401     int i;
402     for (i=0; i<locals_size; i++) {
403       locals[real_locals_size] = parse_verification_type(&flags, THREAD);
404       if (locals[real_locals_size].is_category2()) {
405         locals[real_locals_size + 1] =
406           locals[real_locals_size].to_category2_2nd();
407         ++real_locals_size;
408       }
409       ++real_locals_size;
410     }
411     check_verification_type_array_size(
412       real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL));
413     u2 stack_size = _stream->get_u2(THREAD);
414     int real_stack_size = 0;
415     VerificationType* stack = NULL;
416     if (stack_size > 0) {
417       stack = NEW_RESOURCE_ARRAY_IN_THREAD(
418         THREAD, VerificationType, stack_size*2);
419     }
420     for (i=0; i<stack_size; i++) {
421       stack[real_stack_size] = parse_verification_type(NULL, THREAD);
422       if (stack[real_stack_size].is_category2()) {
423         stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd();
424         ++real_stack_size;
425       }
426       ++real_stack_size;
427     }
428     check_verification_type_array_size(
429       real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
430     if (first) {
431       offset = offset_delta;
432     } else {
433       offset = pre_frame->offset() + offset_delta + 1;
434     }
435     frame = new StackMapFrame(
436       offset, flags, real_locals_size, real_stack_size,
437       max_locals, max_stack, locals, stack, _verifier);
438     return frame;
439   }
440 
441   _stream->stackmap_format_error(
442     "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL));
443   return NULL;
444 }