1 /*
2 * Copyright (c) 2010, 2026, 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 #ifndef SHARE_CLASSFILE_STACKMAPTABLEFORMAT_HPP
26 #define SHARE_CLASSFILE_STACKMAPTABLEFORMAT_HPP
27
28 #include "classfile/verificationType.hpp"
29 #include "utilities/checkedCast.hpp"
30
31 // These classes represent the stack-map substructures described in the JVMS
32 // (hence the non-conforming naming scheme).
33
34 // These classes work with the types in their compressed form in-place (as they
35 // would appear in the classfile). No virtual methods or fields allowed.
36
37 class verification_type_info {
38 private:
39 // u1 tag
40 // u2 cpool_index || u2 bci (for ITEM_Object & ITEM_Uninitailized only)
41
42 address tag_addr() const { return (address)this; }
43 address cpool_index_addr() const { return tag_addr() + sizeof(u1); }
44 address bci_addr() const { return cpool_index_addr(); }
45 NONCOPYABLE(verification_type_info);
46
47 protected:
48 // No constructors - should be 'private', but GCC issues a warning if it is
49 verification_type_info() {}
50
51 public:
52
53 static verification_type_info* at(address addr) {
54 return (verification_type_info*)addr;
55 }
56
57 static verification_type_info* create_at(address addr, u1 tag) {
58 verification_type_info* vti = (verification_type_info*)addr;
59 vti->set_tag(tag);
60 return vti;
61 }
62
63 static verification_type_info* create_object_at(address addr, u2 cp_idx) {
64 verification_type_info* vti = (verification_type_info*)addr;
65 vti->set_tag(ITEM_Object);
66 vti->set_cpool_index(cp_idx);
67 return vti;
68 }
69
70 static verification_type_info* create_uninit_at(address addr, u2 bci) {
71 verification_type_info* vti = (verification_type_info*)addr;
72 vti->set_tag(ITEM_Uninitialized);
73 vti->set_bci(bci);
74 return vti;
75 }
76
77 static size_t calculate_size(u1 tag) {
78 if (tag == ITEM_Object || tag == ITEM_Uninitialized) {
79 return sizeof(u1) + sizeof(u2);
80 } else {
81 return sizeof(u1);
82 }
83 }
84
85 static size_t max_size() { return sizeof(u1) + sizeof(u2); }
86
87 u1 tag() const { return *(u1*)tag_addr(); }
88 void set_tag(u1 tag) { *((u1*)tag_addr()) = tag; }
89
90 bool is_object() const { return tag() == ITEM_Object; }
91 bool is_uninitialized() const { return tag() == ITEM_Uninitialized; }
92
93 u2 cpool_index() const {
94 assert(is_object(), "This type has no cp_index");
95 return Bytes::get_Java_u2(cpool_index_addr());
96 }
97 void set_cpool_index(u2 idx) {
98 assert(is_object(), "This type has no cp_index");
99 Bytes::put_Java_u2(cpool_index_addr(), idx);
100 }
101
102 u2 bci() const {
103 assert(is_uninitialized(), "This type has no bci");
104 return Bytes::get_Java_u2(bci_addr());
105 }
106
107 void set_bci(u2 bci) {
108 assert(is_uninitialized(), "This type has no bci");
109 Bytes::put_Java_u2(bci_addr(), bci);
110 }
111
112 void copy_from(verification_type_info* from) {
113 set_tag(from->tag());
114 if (from->is_object()) {
115 set_cpool_index(from->cpool_index());
116 } else if (from->is_uninitialized()) {
117 set_bci(from->bci());
118 }
119 }
120
121 size_t size() const {
122 return calculate_size(tag());
123 }
124
125 verification_type_info* next() {
126 return (verification_type_info*)((address)this + size());
127 }
128
129 // This method is used when reading unverified data in order to ensure
130 // that we don't read past a particular memory limit. It returns false
131 // if any part of the data structure is outside the specified memory bounds.
132 bool verify(address start, address end) {
133 return ((address)this >= start &&
134 (address)this < end &&
135 (bci_addr() + sizeof(u2) <= end ||
136 (!is_object() && !is_uninitialized())));
137 }
138
139 void print_on(outputStream* st) {
140 switch (tag()) {
141 case ITEM_Top: st->print("Top"); break;
142 case ITEM_Integer: st->print("Integer"); break;
143 case ITEM_Float: st->print("Float"); break;
144 case ITEM_Double: st->print("Double"); break;
145 case ITEM_Long: st->print("Long"); break;
146 case ITEM_Null: st->print("Null"); break;
147 case ITEM_UninitializedThis:
148 st->print("UninitializedThis"); break;
149 case ITEM_Uninitialized:
150 st->print("Uninitialized[#%d]", bci()); break;
151 case ITEM_Object:
152 st->print("Object[#%d]", cpool_index()); break;
153 default:
154 st->print("BAD:%d", tag()); break;
155 }
156 }
157 };
158
159 #define FOR_EACH_STACKMAP_FRAME_TYPE(macro, arg1, arg2) \
160 macro(early_larval, arg1, arg2) \
161 macro(same_frame, arg1, arg2) \
162 macro(same_frame_extended, arg1, arg2) \
163 macro(same_locals_1_stack_item_frame, arg1, arg2) \
164 macro(same_locals_1_stack_item_extended, arg1, arg2) \
165 macro(chop_frame, arg1, arg2) \
166 macro(append_frame, arg1, arg2) \
167 macro(full_frame, arg1, arg2)
168
169 #define SM_FORWARD_DECL(type, arg1, arg2) class type;
170 FOR_EACH_STACKMAP_FRAME_TYPE(SM_FORWARD_DECL, x, x)
171 #undef SM_FORWARD_DECL
172
173 class stack_map_frame {
174 NONCOPYABLE(stack_map_frame);
175
176 protected:
177 address frame_type_addr() const { return (address)this; }
178
179 // No constructors - should be 'private', but GCC issues a warning if it is
180 stack_map_frame() {}
181
182 public:
183
184 static stack_map_frame* at(address addr) {
185 return (stack_map_frame*)addr;
186 }
187
188 stack_map_frame* next() const {
189 return at((address)this + size());
190 }
191
192 u1 frame_type() const { return *(u1*)frame_type_addr(); }
193 void set_frame_type(u1 type) { *((u1*)frame_type_addr()) = type; }
194
195 // pseudo-virtual methods
196 inline size_t size() const;
197 inline int offset_delta() const;
198 inline void set_offset_delta(int offset_delta);
199 inline int number_of_types() const; // number of types contained in the frame
200 inline verification_type_info* types() const; // pointer to first type
201 inline bool is_valid_offset(int offset_delta) const;
202
203 // This method must be used when reading unverified data in order to ensure
204 // that we don't read past a particular memory limit. It returns false
205 // if any part of the data structure is outside the specified memory bounds.
206 inline bool verify(address start, address end) const;
207
208 inline void print_on(outputStream* st, int current_offset) const;
209 inline void print_truncated(outputStream* st, int current_offset) const;
210
211 // Create as_xxx and is_xxx methods for the subtypes
212 #define FRAME_TYPE_DECL(stackmap_frame_type, arg1, arg2) \
213 inline stackmap_frame_type* as_##stackmap_frame_type() const; \
214 bool is_##stackmap_frame_type() { \
215 return as_##stackmap_frame_type() != nullptr; \
216 }
217
218 FOR_EACH_STACKMAP_FRAME_TYPE(FRAME_TYPE_DECL, x, x)
219 #undef FRAME_TYPE_DECL
220 };
221
222 class same_frame : public stack_map_frame {
223 private:
224 static int frame_type_to_offset_delta(u1 frame_type) {
225 return frame_type + 1;
226 }
227 static u1 offset_delta_to_frame_type(int offset_delta) {
228 return checked_cast<u1>(offset_delta - 1);
229 }
230
231 public:
232
233 static bool is_frame_type(u1 tag) {
234 return tag < 64;
235 }
236
237 static same_frame* at(address addr) {
238 assert(is_frame_type(*addr), "Wrong frame id");
239 return (same_frame*)addr;
240 }
241
242 static same_frame* create_at(address addr, int offset_delta) {
243 same_frame* sm = (same_frame*)addr;
244 sm->set_offset_delta(offset_delta);
245 return sm;
246 }
247
248 static size_t calculate_size() { return sizeof(u1); }
249
250 size_t size() const { return calculate_size(); }
251 int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }
252
253 void set_offset_delta(int offset_delta) {
254 assert(offset_delta <= 64, "Offset too large for same_frame");
255 set_frame_type(offset_delta_to_frame_type(offset_delta));
256 }
257
258 int number_of_types() const { return 0; }
259 verification_type_info* types() const { return nullptr; }
260
261 bool is_valid_offset(int offset_delta) const {
262 return is_frame_type(offset_delta_to_frame_type(offset_delta));
263 }
264
265 bool verify_subtype(address start, address end) const {
266 return true;
267 }
268
269 void print_on(outputStream* st, int current_offset = -1) const {
270 st->print("same_frame(@%d)", offset_delta() + current_offset);
271 }
272
273 void print_truncated(outputStream* st, int current_offset = -1) const {
274 print_on(st, current_offset);
275 }
276 };
277
278 class same_frame_extended : public stack_map_frame {
279 private:
280 enum { _frame_id = 251 };
281 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
282
283 public:
284 static bool is_frame_type(u1 tag) {
285 return tag == _frame_id;
286 }
287
288 static same_frame_extended* at(address addr) {
289 assert(is_frame_type(*addr), "Wrong frame type");
290 return (same_frame_extended*)addr;
291 }
292
293 static same_frame_extended* create_at(address addr, u2 offset_delta) {
294 same_frame_extended* sm = (same_frame_extended*)addr;
295 sm->set_frame_type(_frame_id);
296 sm->set_offset_delta(offset_delta);
297 return sm;
298 }
299
300 static size_t calculate_size() { return sizeof(u1) + sizeof(u2); }
301
302 size_t size() const { return calculate_size(); }
303 int offset_delta() const {
304 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
305 }
306
307 void set_offset_delta(int offset_delta) {
308 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
309 }
310
311 int number_of_types() const { return 0; }
312 verification_type_info* types() const { return nullptr; }
313 bool is_valid_offset(int offset) const { return true; }
314
315 bool verify_subtype(address start, address end) const {
316 return frame_type_addr() + size() <= end;
317 }
318
319 void print_on(outputStream* st, int current_offset = -1) const {
320 st->print("same_frame_extended(@%d)", offset_delta() + current_offset);
321 }
322
323 void print_truncated(outputStream* st, int current_offset = -1) const {
324 print_on(st, current_offset);
325 }
326 };
327
328 class same_locals_1_stack_item_frame : public stack_map_frame {
329 private:
330 address type_addr() const { return frame_type_addr() + sizeof(u1); }
331
332 static int frame_type_to_offset_delta(u1 frame_type) {
333 return frame_type - 63;
334 }
335 static u1 offset_delta_to_frame_type(int offset_delta) {
336 return (u1)(offset_delta + 63);
337 }
338
339 public:
340 static bool is_frame_type(u1 tag) {
341 return tag >= 64 && tag < 128;
342 }
343
344 static same_locals_1_stack_item_frame* at(address addr) {
345 assert(is_frame_type(*addr), "Wrong frame id");
346 return (same_locals_1_stack_item_frame*)addr;
347 }
348
349 static same_locals_1_stack_item_frame* create_at(
350 address addr, int offset_delta, verification_type_info* vti) {
351 same_locals_1_stack_item_frame* sm = (same_locals_1_stack_item_frame*)addr;
352 sm->set_offset_delta(offset_delta);
353 if (vti != nullptr) {
354 sm->set_type(vti);
355 }
356 return sm;
357 }
358
359 static size_t calculate_size(verification_type_info* vti) {
360 return sizeof(u1) + vti->size();
361 }
362
363 static size_t max_size() {
364 return sizeof(u1) + verification_type_info::max_size();
365 }
366
367 size_t size() const { return calculate_size(types()); }
368 int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }
369
370 void set_offset_delta(int offset_delta) {
371 assert(offset_delta > 0 && offset_delta <= 64,
372 "Offset too large for this frame type");
373 set_frame_type(offset_delta_to_frame_type(offset_delta));
374 }
375
376 void set_type(verification_type_info* vti) {
377 verification_type_info* cur = types();
378 cur->copy_from(vti);
379 }
380
381 int number_of_types() const { return 1; }
382 verification_type_info* types() const {
383 return verification_type_info::at(type_addr());
384 }
385
386 bool is_valid_offset(int offset_delta) const {
387 return is_frame_type(offset_delta_to_frame_type(offset_delta));
388 }
389
390 bool verify_subtype(address start, address end) const {
391 return types()->verify(start, end);
392 }
393
394 void print_on(outputStream* st, int current_offset = -1) const {
395 st->print("same_locals_1_stack_item_frame(@%d,",
396 offset_delta() + current_offset);
397 types()->print_on(st);
398 st->print(")");
399 }
400
401 void print_truncated(outputStream* st, int current_offset = -1) const {
402 st->print("same_locals_1_stack_item_frame(@%d), output truncated, Stackmap exceeds table size.",
403 offset_delta() + current_offset);
404 }
405 };
406
407 class early_larval : public stack_map_frame {
408 private:
409 static int frame_type_to_offset_delta(u1 frame_type) {
410 return 0;
411 }
412 static u1 offset_delta_to_frame_type(int offset_delta) {
413 return checked_cast<u1>(246);
414 }
415
416 address num_unset_fields_addr() const { return frame_type_addr() + sizeof(u1); }
417 int number_of_unset_fields() const { return Bytes::get_Java_u2(num_unset_fields_addr()); }
418
419 // EARLY_LARVAL frames wrap regular stack map frames
420 stack_map_frame* nested_frame() const { return (stack_map_frame*)(frame_type_addr() + calculate_size(number_of_unset_fields())); }
421
422 public:
423 static bool is_frame_type(u1 tag) {
424 return tag == 246;
425 }
426
427 static early_larval* at(address addr) {
428 assert(is_frame_type(*addr), "Wrong frame id");
429 return (early_larval*)addr;
430 }
431
432 static early_larval* create_at(address addr, int offset_delta) {
433 early_larval* sm = (early_larval*)addr;
434 return sm;
435 }
436
437 static size_t calculate_size(u2 num_unset_fields) { return sizeof(u1) + sizeof(u2) + (sizeof(u2) * num_unset_fields); }
438
439 size_t size() const { return calculate_size(number_of_unset_fields()) + nested_frame()->size(); }
440 int offset_delta() const { return nested_frame()->offset_delta(); }
441
442 void set_offset_delta(int offset_delta) {
443 assert(offset_delta == 0, "early_larval should not have an offset");
444 set_frame_type(offset_delta_to_frame_type(offset_delta));
445 }
446
447 int number_of_types() const { return 0; }
448 verification_type_info* types() const { return nullptr; }
449
450 bool is_valid_offset(int offset_delta) const {
451 return is_frame_type(offset_delta_to_frame_type(offset_delta));
452 }
453
454 bool verify_subtype(address start, address end) const {
455 return nested_frame()->verify(start, end);
456 }
457
458 void print_on(outputStream* st, int current_offset = -1) const {
459 st->print("early_larval(%d unset fields: ", number_of_unset_fields());
460 st->print("[ ");
461 address addr = num_unset_fields_addr() + sizeof(u2);
462 for (int i = 0; i < number_of_unset_fields(); i++) {
463 st->print("%u ", Bytes::get_Java_u2(addr + (i * sizeof(u2))));
464 }
465 st->print_cr("])");
466 st->print("\t");
467 nested_frame()->print_on(st, current_offset);
468 }
469
470 void print_truncated(outputStream* st, int current_offset = -1) const {
471 // If we fail to verify, we may have a nested early_larval
472 st->print("early_larval(%d unset fields), output truncated, Stackmap exceeds table size.", number_of_unset_fields());
473 }
474 };
475
476 class same_locals_1_stack_item_extended : public stack_map_frame {
477 private:
478 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
479 address type_addr() const { return offset_delta_addr() + sizeof(u2); }
480
481 enum { _frame_id = 247 };
482
483 public:
484 static bool is_frame_type(u1 tag) {
485 return tag == _frame_id;
486 }
487
488 static same_locals_1_stack_item_extended* at(address addr) {
489 assert(is_frame_type(*addr), "Wrong frame id");
490 return (same_locals_1_stack_item_extended*)addr;
491 }
492
493 static same_locals_1_stack_item_extended* create_at(
494 address addr, int offset_delta, verification_type_info* vti) {
495 same_locals_1_stack_item_extended* sm =
496 (same_locals_1_stack_item_extended*)addr;
497 sm->set_frame_type(_frame_id);
498 sm->set_offset_delta(offset_delta);
499 if (vti != nullptr) {
500 sm->set_type(vti);
501 }
502 return sm;
503 }
504
505 static size_t calculate_size(verification_type_info* vti) {
506 return sizeof(u1) + sizeof(u2) + vti->size();
507 }
508
509 size_t size() const { return calculate_size(types()); }
510 int offset_delta() const {
511 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
512 }
513
514 void set_offset_delta(int offset_delta) {
515 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
516 }
517
518 void set_type(verification_type_info* vti) {
519 verification_type_info* cur = types();
520 cur->copy_from(vti);
521 }
522
523 int number_of_types() const { return 1; }
524 verification_type_info* types() const {
525 return verification_type_info::at(type_addr());
526 }
527 bool is_valid_offset(int offset) { return true; }
528
529 bool verify_subtype(address start, address end) const {
530 return type_addr() < end && types()->verify(start, end);
531 }
532
533 void print_on(outputStream* st, int current_offset = -1) const {
534 st->print("same_locals_1_stack_item_extended(@%d,",
535 offset_delta() + current_offset);
536 types()->print_on(st);
537 st->print(")");
538 }
539
540 void print_truncated(outputStream* st, int current_offset = -1) const {
541 st->print("same_locals_1_stack_item_extended(@%d), output truncated, Stackmap exceeds table size.",
542 offset_delta() + current_offset);
543 }
544 };
545
546 class chop_frame : public stack_map_frame {
547 private:
548 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
549
550 static int frame_type_to_chops(u1 frame_type) {
551 int chop = 251 - frame_type;
552 return chop;
553 }
554
555 static u1 chops_to_frame_type(int chop) {
556 return checked_cast<u1>(251 - chop);
557 }
558
559 public:
560 static bool is_frame_type(u1 tag) {
561 return frame_type_to_chops(tag) > 0 && frame_type_to_chops(tag) < 4;
562 }
563
564 static chop_frame* at(address addr) {
565 assert(is_frame_type(*addr), "Wrong frame id");
566 return (chop_frame*)addr;
567 }
568
569 static chop_frame* create_at(address addr, int offset_delta, int chops) {
570 chop_frame* sm = (chop_frame*)addr;
571 sm->set_chops(chops);
572 sm->set_offset_delta(offset_delta);
573 return sm;
574 }
575
576 static size_t calculate_size() {
577 return sizeof(u1) + sizeof(u2);
578 }
579
580 size_t size() const { return calculate_size(); }
581 int offset_delta() const {
582 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
583 }
584 void set_offset_delta(int offset_delta) {
585 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
586 }
587
588 int chops() const {
589 int chops = frame_type_to_chops(frame_type());
590 assert(chops > 0 && chops < 4, "Invalid number of chops in frame");
591 return chops;
592 }
593 void set_chops(int chops) {
594 assert(chops > 0 && chops <= 3, "Bad number of chops");
595 set_frame_type(chops_to_frame_type(chops));
596 }
597
598 int number_of_types() const { return 0; }
599 verification_type_info* types() const { return nullptr; }
600 bool is_valid_offset(int offset) { return true; }
601
602 bool verify_subtype(address start, address end) const {
603 return frame_type_addr() + size() <= end;
604 }
605
606 void print_on(outputStream* st, int current_offset = -1) const {
607 st->print("chop_frame(@%d,%d)", offset_delta() + current_offset, chops());
608 }
609
610 void print_truncated(outputStream* st, int current_offset = -1) const {
611 print_on(st, current_offset);
612 }
613 };
614
615 class append_frame : public stack_map_frame {
616 private:
617 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
618 address types_addr() const { return offset_delta_addr() + sizeof(u2); }
619
620 static int frame_type_to_appends(u1 frame_type) {
621 int append = frame_type - 251;
622 return append;
623 }
624
625 static u1 appends_to_frame_type(int appends) {
626 assert(appends > 0 && appends < 4, "Invalid append amount");
627 return checked_cast<u1>(251 + appends);
628 }
629
630 public:
631 static bool is_frame_type(u1 tag) {
632 return frame_type_to_appends(tag) > 0 && frame_type_to_appends(tag) < 4;
633 }
634
635 static append_frame* at(address addr) {
636 assert(is_frame_type(*addr), "Wrong frame id");
637 return (append_frame*)addr;
638 }
639
640 static append_frame* create_at(
641 address addr, int offset_delta, int appends,
642 verification_type_info* types) {
643 append_frame* sm = (append_frame*)addr;
644 sm->set_appends(appends);
645 sm->set_offset_delta(offset_delta);
646 if (types != nullptr) {
647 verification_type_info* cur = sm->types();
648 for (int i = 0; i < appends; ++i) {
649 cur->copy_from(types);
650 cur = cur->next();
651 types = types->next();
652 }
653 }
654 return sm;
655 }
656
657 static size_t calculate_size(int appends, verification_type_info* types) {
658 size_t sz = sizeof(u1) + sizeof(u2);
659 for (int i = 0; i < appends; ++i) {
660 sz += types->size();
661 types = types->next();
662 }
663 return sz;
664 }
665
666 static size_t max_size() {
667 return sizeof(u1) + sizeof(u2) + 3 * verification_type_info::max_size();
668 }
669
670 size_t size() const { return calculate_size(number_of_types(), types()); }
671 int offset_delta() const {
672 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
673 }
674
675 void set_offset_delta(int offset_delta) {
676 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
677 }
678
679 void set_appends(int appends) {
680 assert(appends > 0 && appends < 4, "Bad number of appends");
681 set_frame_type(appends_to_frame_type(appends));
682 }
683
684 int number_of_types() const {
685 int appends = frame_type_to_appends(frame_type());
686 assert(appends > 0 && appends < 4, "Invalid number of appends in frame");
687 return appends;
688 }
689 verification_type_info* types() const {
690 return verification_type_info::at(types_addr());
691 }
692 bool is_valid_offset(int offset) const { return true; }
693
694 bool verify_subtype(address start, address end) const {
695 verification_type_info* vti = types();
696 if ((address)vti < end && vti->verify(start, end)) {
697 int nof = number_of_types();
698 vti = vti->next();
699 if (nof < 2 || vti->verify(start, end)) {
700 vti = vti->next();
701 if (nof < 3 || vti->verify(start, end)) {
702 return true;
703 }
704 }
705 }
706 return false;
707 }
708
709 void print_on(outputStream* st, int current_offset = -1) const {
710 st->print("append_frame(@%d,", offset_delta() + current_offset);
711 verification_type_info* vti = types();
712 for (int i = 0; i < number_of_types(); ++i) {
713 vti->print_on(st);
714 if (i != number_of_types() - 1) {
715 st->print(",");
716 }
717 vti = vti->next();
718 }
719 st->print(")");
720 }
721
722 void print_truncated(outputStream* st, int current_offset = -1) const {
723 st->print("append_frame(@%d), output truncated, Stackmap exceeds table size.",
724 offset_delta() + current_offset);
725 }
726 };
727
728 class full_frame : public stack_map_frame {
729 private:
730 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
731 address num_locals_addr() const { return offset_delta_addr() + sizeof(u2); }
732 address locals_addr() const { return num_locals_addr() + sizeof(u2); }
733 address stack_slots_addr(address end_of_locals) const {
734 return end_of_locals;
735 }
736 address stack_addr(address end_of_locals) const {
737 return stack_slots_addr(end_of_locals) + sizeof(u2);
738 }
739
740 enum { _frame_id = 255 };
741
742 public:
743 static bool is_frame_type(u1 tag) {
744 return tag == _frame_id;
745 }
746
747 static full_frame* at(address addr) {
748 assert(is_frame_type(*addr), "Wrong frame id");
749 return (full_frame*)addr;
750 }
751
752 static full_frame* create_at(
753 address addr, int offset_delta, int num_locals,
754 verification_type_info* locals,
755 int stack_slots, verification_type_info* stack) {
756 full_frame* sm = (full_frame*)addr;
757 sm->set_frame_type(_frame_id);
758 sm->set_offset_delta(offset_delta);
759 sm->set_num_locals(num_locals);
760 if (locals != nullptr) {
761 verification_type_info* cur = sm->locals();
762 for (int i = 0; i < num_locals; ++i) {
763 cur->copy_from(locals);
764 cur = cur->next();
765 locals = locals->next();
766 }
767 address end_of_locals = (address)cur;
768 sm->set_stack_slots(end_of_locals, stack_slots);
769 cur = sm->stack(end_of_locals);
770 for (int i = 0; i < stack_slots; ++i) {
771 cur->copy_from(stack);
772 cur = cur->next();
773 stack = stack->next();
774 }
775 }
776 return sm;
777 }
778
779 static size_t calculate_size(
780 int num_locals, verification_type_info* locals,
781 int stack_slots, verification_type_info* stack) {
782 size_t sz = sizeof(u1) + sizeof(u2) + sizeof(u2) + sizeof(u2);
783 verification_type_info* vti = locals;
784 for (int i = 0; i < num_locals; ++i) {
785 sz += vti->size();
786 vti = vti->next();
787 }
788 vti = stack;
789 for (int i = 0; i < stack_slots; ++i) {
790 sz += vti->size();
791 vti = vti->next();
792 }
793 return sz;
794 }
795
796 static size_t max_size(int locals, int stack) {
797 return sizeof(u1) + 3 * sizeof(u2) +
798 (locals + stack) * verification_type_info::max_size();
799 }
800
801 size_t size() const {
802 address eol = end_of_locals();
803 return calculate_size(num_locals(), locals(), stack_slots(eol), stack(eol));
804 }
805
806 int offset_delta() const {
807 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
808 }
809 int num_locals() const { return Bytes::get_Java_u2(num_locals_addr()); }
810 verification_type_info* locals() const {
811 return verification_type_info::at(locals_addr());
812 }
813 address end_of_locals() const {
814 verification_type_info* vti = locals();
815 for (int i = 0; i < num_locals(); ++i) {
816 vti = vti->next();
817 }
818 return (address)vti;
819 }
820 int stack_slots(address end_of_locals) const {
821 return Bytes::get_Java_u2(stack_slots_addr(end_of_locals));
822 }
823 verification_type_info* stack(address end_of_locals) const {
824 return verification_type_info::at(stack_addr(end_of_locals));
825 }
826
827 void set_offset_delta(int offset_delta) {
828 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
829 }
830 void set_num_locals(int num_locals) {
831 Bytes::put_Java_u2(num_locals_addr(), checked_cast<u2>(num_locals));
832 }
833 void set_stack_slots(address end_of_locals, int stack_slots) {
834 Bytes::put_Java_u2(stack_slots_addr(end_of_locals), checked_cast<u2>(stack_slots));
835 }
836
837 // These return only the locals. Extra processing is required for stack
838 // types of full frames.
839 int number_of_types() const { return num_locals(); }
840 verification_type_info* types() const { return locals(); }
841 bool is_valid_offset(int offset) { return true; }
842
843 bool verify_subtype(address start, address end) const {
844 verification_type_info* vti = types();
845 if ((address)vti >= end) {
846 return false;
847 }
848 int count = number_of_types();
849 for (int i = 0; i < count; ++i) {
850 if (!vti->verify(start, end)) {
851 return false;
852 }
853 vti = vti->next();
854 }
855 address eol = (address)vti;
856 if (eol + sizeof(u2) > end) {
857 return false;
858 }
859 count = stack_slots(eol);
860 vti = stack(eol);
861 for (int i = 0; i < stack_slots(eol); ++i) {
862 if (!vti->verify(start, end)) {
863 return false;
864 }
865 vti = vti->next();
866 }
867 return true;
868 }
869
870 void print_on(outputStream* st, int current_offset = -1) const {
871 st->print("full_frame(@%d,{", offset_delta() + current_offset);
872 verification_type_info* vti = locals();
873 for (int i = 0; i < num_locals(); ++i) {
874 vti->print_on(st);
875 if (i != num_locals() - 1) {
876 st->print(",");
877 }
878 vti = vti->next();
879 }
880 st->print("},{");
881 address end_of_locals = (address)vti;
882 vti = stack(end_of_locals);
883 int ss = stack_slots(end_of_locals);
884 for (int i = 0; i < ss; ++i) {
885 vti->print_on(st);
886 if (i != ss - 1) {
887 st->print(",");
888 }
889 vti = vti->next();
890 }
891 st->print("})");
892 }
893
894 void print_truncated(outputStream* st, int current_offset = -1) const {
895 st->print("full_frame(@%d), output truncated, Stackmap exceeds table size.",
896 offset_delta() + current_offset);
897 }
898 };
899
900 #define VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
901 stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
902 if (item_##stack_frame_type != nullptr) { \
903 return item_##stack_frame_type->func_name args; \
904 }
905
906 #define VOID_VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
907 stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
908 if (item_##stack_frame_type != nullptr) { \
909 item_##stack_frame_type->func_name args; \
910 return; \
911 }
912
913 size_t stack_map_frame::size() const {
914 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, size, ());
915 return 0;
916 }
917
918 int stack_map_frame::offset_delta() const {
919 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, offset_delta, ());
920 return 0;
921 }
922
923 void stack_map_frame::set_offset_delta(int offset_delta) {
924 FOR_EACH_STACKMAP_FRAME_TYPE(
925 VOID_VIRTUAL_DISPATCH, set_offset_delta, (offset_delta));
926 }
927
928 int stack_map_frame::number_of_types() const {
929 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, number_of_types, ());
930 return 0;
931 }
932
933 verification_type_info* stack_map_frame::types() const {
934 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, types, ());
935 return nullptr;
936 }
937
938 bool stack_map_frame::is_valid_offset(int offset) const {
939 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, is_valid_offset, (offset));
940 return true;
941 }
942
943 bool stack_map_frame::verify(address start, address end) const {
944 if (frame_type_addr() >= start && frame_type_addr() < end) {
945 FOR_EACH_STACKMAP_FRAME_TYPE(
946 VIRTUAL_DISPATCH, verify_subtype, (start, end));
947 }
948 return false;
949 }
950
951 void stack_map_frame::print_on(outputStream* st, int offs = -1) const {
952 FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_on, (st, offs));
953 }
954
955 void stack_map_frame::print_truncated(outputStream* st, int offs = -1) const {
956 FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_truncated, (st, offs));
957 }
958
959 #undef VIRTUAL_DISPATCH
960 #undef VOID_VIRTUAL_DISPATCH
961
962 #define AS_SUBTYPE_DEF(stack_frame_type, arg1, arg2) \
963 stack_frame_type* stack_map_frame::as_##stack_frame_type() const { \
964 if (stack_frame_type::is_frame_type(frame_type())) { \
965 return (stack_frame_type*)this; \
966 } else { \
967 return nullptr; \
968 } \
969 }
970
971 FOR_EACH_STACKMAP_FRAME_TYPE(AS_SUBTYPE_DEF, x, x)
972 #undef AS_SUBTYPE_DEF
973
974 class stack_map_table {
975 private:
976 address number_of_entries_addr() const {
977 return (address)this;
978 }
979 address entries_addr() const {
980 return number_of_entries_addr() + sizeof(u2);
981 }
982 NONCOPYABLE(stack_map_table);
983
984 protected:
985 // No constructors - should be 'private', but GCC issues a warning if it is
986 stack_map_table() {}
987
988 public:
989
990 static stack_map_table* at(address addr) {
991 return (stack_map_table*)addr;
992 }
993
994 u2 number_of_entries() const {
995 return Bytes::get_Java_u2(number_of_entries_addr());
996 }
997 stack_map_frame* entries() const {
998 return stack_map_frame::at(entries_addr());
999 }
1000
1001 void set_number_of_entries(u2 num) {
1002 Bytes::put_Java_u2(number_of_entries_addr(), num);
1003 }
1004 };
1005
1006 class stack_map_table_attribute {
1007 private:
1008 address name_index_addr() const {
1009 return (address)this;
1010 }
1011 address attribute_length_addr() const {
1012 return name_index_addr() + sizeof(u2);
1013 }
1014 address stack_map_table_addr() const {
1015 return attribute_length_addr() + sizeof(u4);
1016 }
1017 NONCOPYABLE(stack_map_table_attribute);
1018
1019 protected:
1020 // No constructors - should be 'private', but GCC issues a warning if it is
1021 stack_map_table_attribute() {}
1022
1023 public:
1024
1025 static stack_map_table_attribute* at(address addr) {
1026 return (stack_map_table_attribute*)addr;
1027 }
1028
1029 u2 name_index() const {
1030 return Bytes::get_Java_u2(name_index_addr());
1031 }
1032 u4 attribute_length() const {
1033 return Bytes::get_Java_u4(attribute_length_addr());
1034 }
1035 stack_map_table* table() const {
1036 return stack_map_table::at(stack_map_table_addr());
1037 }
1038
1039 void set_name_index(u2 idx) {
1040 Bytes::put_Java_u2(name_index_addr(), idx);
1041 }
1042 void set_attribute_length(u4 len) {
1043 Bytes::put_Java_u4(attribute_length_addr(), len);
1044 }
1045 };
1046
1047 #undef FOR_EACH_STACKMAP_FRAME_TYPE
1048
1049 #endif // SHARE_CLASSFILE_STACKMAPTABLEFORMAT_HPP