1 /*
2 * Copyright (c) 2010, 2023, 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(same_frame, arg1, arg2) \
161 macro(same_frame_extended, arg1, arg2) \
162 macro(same_locals_1_stack_item_frame, arg1, arg2) \
163 macro(same_locals_1_stack_item_extended, arg1, arg2) \
164 macro(chop_frame, arg1, arg2) \
165 macro(append_frame, arg1, arg2) \
166 macro(full_frame, arg1, arg2)
167
168 #define SM_FORWARD_DECL(type, arg1, arg2) class type;
169 FOR_EACH_STACKMAP_FRAME_TYPE(SM_FORWARD_DECL, x, x)
170 #undef SM_FORWARD_DECL
171
172 class stack_map_frame {
173 NONCOPYABLE(stack_map_frame);
174
175 protected:
176 address frame_type_addr() const { return (address)this; }
177
178 // No constructors - should be 'private', but GCC issues a warning if it is
179 stack_map_frame() {}
180
181 public:
182
183 static stack_map_frame* at(address addr) {
184 return (stack_map_frame*)addr;
185 }
186
187 stack_map_frame* next() const {
188 return at((address)this + size());
189 }
190
191 u1 frame_type() const { return *(u1*)frame_type_addr(); }
192 void set_frame_type(u1 type) { *((u1*)frame_type_addr()) = type; }
193
194 // pseudo-virtual methods
195 inline size_t size() const;
196 inline int offset_delta() const;
197 inline void set_offset_delta(int offset_delta);
198 inline int number_of_types() const; // number of types contained in the frame
199 inline verification_type_info* types() const; // pointer to first type
200 inline bool is_valid_offset(int offset_delta) const;
201
202 // This method must be used when reading unverified data in order to ensure
203 // that we don't read past a particular memory limit. It returns false
204 // if any part of the data structure is outside the specified memory bounds.
205 inline bool verify(address start, address end) const;
206
207 inline void print_on(outputStream* st, int current_offset) const;
208 inline void print_truncated(outputStream* st, int current_offset) const;
209
210 // Create as_xxx and is_xxx methods for the subtypes
211 #define FRAME_TYPE_DECL(stackmap_frame_type, arg1, arg2) \
212 inline stackmap_frame_type* as_##stackmap_frame_type() const; \
213 bool is_##stackmap_frame_type() { \
214 return as_##stackmap_frame_type() != nullptr; \
215 }
216
217 FOR_EACH_STACKMAP_FRAME_TYPE(FRAME_TYPE_DECL, x, x)
218 #undef FRAME_TYPE_DECL
219 };
220
221 class same_frame : public stack_map_frame {
222 private:
223 static int frame_type_to_offset_delta(u1 frame_type) {
224 return frame_type + 1; }
225 static u1 offset_delta_to_frame_type(int offset_delta) {
226 return checked_cast<u1>(offset_delta - 1); }
227
228 public:
229
230 static bool is_frame_type(u1 tag) {
231 return tag < 64;
232 }
233
234 static same_frame* at(address addr) {
235 assert(is_frame_type(*addr), "Wrong frame id");
236 return (same_frame*)addr;
237 }
238
239 static same_frame* create_at(address addr, int offset_delta) {
240 same_frame* sm = (same_frame*)addr;
241 sm->set_offset_delta(offset_delta);
242 return sm;
243 }
244
245 static size_t calculate_size() { return sizeof(u1); }
246
247 size_t size() const { return calculate_size(); }
248 int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }
249
250 void set_offset_delta(int offset_delta) {
251 assert(offset_delta <= 64, "Offset too large for same_frame");
252 set_frame_type(offset_delta_to_frame_type(offset_delta));
253 }
254
255 int number_of_types() const { return 0; }
256 verification_type_info* types() const { return nullptr; }
257
258 bool is_valid_offset(int offset_delta) const {
259 return is_frame_type(offset_delta_to_frame_type(offset_delta));
260 }
261
262 bool verify_subtype(address start, address end) const {
263 return true;
264 }
265
266 void print_on(outputStream* st, int current_offset = -1) const {
267 st->print("same_frame(@%d)", offset_delta() + current_offset);
268 }
269
270 void print_truncated(outputStream* st, int current_offset = -1) const {
271 print_on(st, current_offset);
272 }
273 };
274
275 class same_frame_extended : public stack_map_frame {
276 private:
277 enum { _frame_id = 251 };
278 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
279
280 public:
281 static bool is_frame_type(u1 tag) {
282 return tag == _frame_id;
283 }
284
285 static same_frame_extended* at(address addr) {
286 assert(is_frame_type(*addr), "Wrong frame type");
287 return (same_frame_extended*)addr;
288 }
289
290 static same_frame_extended* create_at(address addr, u2 offset_delta) {
291 same_frame_extended* sm = (same_frame_extended*)addr;
292 sm->set_frame_type(_frame_id);
293 sm->set_offset_delta(offset_delta);
294 return sm;
295 }
296
297 static size_t calculate_size() { return sizeof(u1) + sizeof(u2); }
298
299 size_t size() const { return calculate_size(); }
300 int offset_delta() const {
301 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
302 }
303
304 void set_offset_delta(int offset_delta) {
305 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
306 }
307
308 int number_of_types() const { return 0; }
309 verification_type_info* types() const { return nullptr; }
310 bool is_valid_offset(int offset) const { return true; }
311
312 bool verify_subtype(address start, address end) const {
313 return frame_type_addr() + size() <= end;
314 }
315
316 void print_on(outputStream* st, int current_offset = -1) const {
317 st->print("same_frame_extended(@%d)", offset_delta() + current_offset);
318 }
319
320 void print_truncated(outputStream* st, int current_offset = -1) const {
321 print_on(st, current_offset);
322 }
323 };
324
325 class same_locals_1_stack_item_frame : public stack_map_frame {
326 private:
327 address type_addr() const { return frame_type_addr() + sizeof(u1); }
328
329 static int frame_type_to_offset_delta(u1 frame_type) {
330 return frame_type - 63; }
331 static u1 offset_delta_to_frame_type(int offset_delta) {
332 return (u1)(offset_delta + 63); }
333
334 public:
335 static bool is_frame_type(u1 tag) {
336 return tag >= 64 && tag < 128;
337 }
338
339 static same_locals_1_stack_item_frame* at(address addr) {
340 assert(is_frame_type(*addr), "Wrong frame id");
341 return (same_locals_1_stack_item_frame*)addr;
342 }
343
344 static same_locals_1_stack_item_frame* create_at(
345 address addr, int offset_delta, verification_type_info* vti) {
346 same_locals_1_stack_item_frame* sm = (same_locals_1_stack_item_frame*)addr;
347 sm->set_offset_delta(offset_delta);
348 if (vti != nullptr) {
349 sm->set_type(vti);
350 }
351 return sm;
352 }
353
354 static size_t calculate_size(verification_type_info* vti) {
355 return sizeof(u1) + vti->size();
356 }
357
358 static size_t max_size() {
359 return sizeof(u1) + verification_type_info::max_size();
360 }
361
362 size_t size() const { return calculate_size(types()); }
363 int offset_delta() const { return frame_type_to_offset_delta(frame_type()); }
364
365 void set_offset_delta(int offset_delta) {
366 assert(offset_delta > 0 && offset_delta <= 64,
367 "Offset too large for this frame type");
368 set_frame_type(offset_delta_to_frame_type(offset_delta));
369 }
370
371 void set_type(verification_type_info* vti) {
372 verification_type_info* cur = types();
373 cur->copy_from(vti);
374 }
375
376 int number_of_types() const { return 1; }
377 verification_type_info* types() const {
378 return verification_type_info::at(type_addr());
379 }
380
381 bool is_valid_offset(int offset_delta) const {
382 return is_frame_type(offset_delta_to_frame_type(offset_delta));
383 }
384
385 bool verify_subtype(address start, address end) const {
386 return types()->verify(start, end);
387 }
388
389 void print_on(outputStream* st, int current_offset = -1) const {
390 st->print("same_locals_1_stack_item_frame(@%d,",
391 offset_delta() + current_offset);
392 types()->print_on(st);
393 st->print(")");
394 }
395
396 void print_truncated(outputStream* st, int current_offset = -1) const {
397 st->print("same_locals_1_stack_item_frame(@%d), output truncated, Stackmap exceeds table size.",
398 offset_delta() + current_offset);
399 }
400 };
401
402 class same_locals_1_stack_item_extended : public stack_map_frame {
403 private:
404 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
405 address type_addr() const { return offset_delta_addr() + sizeof(u2); }
406
407 enum { _frame_id = 247 };
408
409 public:
410 static bool is_frame_type(u1 tag) {
411 return tag == _frame_id;
412 }
413
414 static same_locals_1_stack_item_extended* at(address addr) {
415 assert(is_frame_type(*addr), "Wrong frame id");
416 return (same_locals_1_stack_item_extended*)addr;
417 }
418
419 static same_locals_1_stack_item_extended* create_at(
420 address addr, int offset_delta, verification_type_info* vti) {
421 same_locals_1_stack_item_extended* sm =
422 (same_locals_1_stack_item_extended*)addr;
423 sm->set_frame_type(_frame_id);
424 sm->set_offset_delta(offset_delta);
425 if (vti != nullptr) {
426 sm->set_type(vti);
427 }
428 return sm;
429 }
430
431 static size_t calculate_size(verification_type_info* vti) {
432 return sizeof(u1) + sizeof(u2) + vti->size();
433 }
434
435 size_t size() const { return calculate_size(types()); }
436 int offset_delta() const {
437 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
438 }
439
440 void set_offset_delta(int offset_delta) {
441 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
442 }
443
444 void set_type(verification_type_info* vti) {
445 verification_type_info* cur = types();
446 cur->copy_from(vti);
447 }
448
449 int number_of_types() const { return 1; }
450 verification_type_info* types() const {
451 return verification_type_info::at(type_addr());
452 }
453 bool is_valid_offset(int offset) { return true; }
454
455 bool verify_subtype(address start, address end) const {
456 return type_addr() < end && types()->verify(start, end);
457 }
458
459 void print_on(outputStream* st, int current_offset = -1) const {
460 st->print("same_locals_1_stack_item_extended(@%d,",
461 offset_delta() + current_offset);
462 types()->print_on(st);
463 st->print(")");
464 }
465
466 void print_truncated(outputStream* st, int current_offset = -1) const {
467 st->print("same_locals_1_stack_item_extended(@%d), output truncated, Stackmap exceeds table size.",
468 offset_delta() + current_offset);
469 }
470 };
471
472 class chop_frame : public stack_map_frame {
473 private:
474 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
475
476 static int frame_type_to_chops(u1 frame_type) {
477 int chop = 251 - frame_type;
478 return chop;
479 }
480
481 static u1 chops_to_frame_type(int chop) {
482 return checked_cast<u1>(251 - chop);
483 }
484
485 public:
486 static bool is_frame_type(u1 tag) {
487 return frame_type_to_chops(tag) > 0 && frame_type_to_chops(tag) < 4;
488 }
489
490 static chop_frame* at(address addr) {
491 assert(is_frame_type(*addr), "Wrong frame id");
492 return (chop_frame*)addr;
493 }
494
495 static chop_frame* create_at(address addr, int offset_delta, int chops) {
496 chop_frame* sm = (chop_frame*)addr;
497 sm->set_chops(chops);
498 sm->set_offset_delta(offset_delta);
499 return sm;
500 }
501
502 static size_t calculate_size() {
503 return sizeof(u1) + sizeof(u2);
504 }
505
506 size_t size() const { return calculate_size(); }
507 int offset_delta() const {
508 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
509 }
510 void set_offset_delta(int offset_delta) {
511 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
512 }
513
514 int chops() const {
515 int chops = frame_type_to_chops(frame_type());
516 assert(chops > 0 && chops < 4, "Invalid number of chops in frame");
517 return chops;
518 }
519 void set_chops(int chops) {
520 assert(chops > 0 && chops <= 3, "Bad number of chops");
521 set_frame_type(chops_to_frame_type(chops));
522 }
523
524 int number_of_types() const { return 0; }
525 verification_type_info* types() const { return nullptr; }
526 bool is_valid_offset(int offset) { return true; }
527
528 bool verify_subtype(address start, address end) const {
529 return frame_type_addr() + size() <= end;
530 }
531
532 void print_on(outputStream* st, int current_offset = -1) const {
533 st->print("chop_frame(@%d,%d)", offset_delta() + current_offset, chops());
534 }
535
536 void print_truncated(outputStream* st, int current_offset = -1) const {
537 print_on(st, current_offset);
538 }
539 };
540
541 class append_frame : public stack_map_frame {
542 private:
543 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
544 address types_addr() const { return offset_delta_addr() + sizeof(u2); }
545
546 static int frame_type_to_appends(u1 frame_type) {
547 int append = frame_type - 251;
548 return append;
549 }
550
551 static u1 appends_to_frame_type(int appends) {
552 assert(appends > 0 && appends < 4, "Invalid append amount");
553 return checked_cast<u1>(251 + appends);
554 }
555
556 public:
557 static bool is_frame_type(u1 tag) {
558 return frame_type_to_appends(tag) > 0 && frame_type_to_appends(tag) < 4;
559 }
560
561 static append_frame* at(address addr) {
562 assert(is_frame_type(*addr), "Wrong frame id");
563 return (append_frame*)addr;
564 }
565
566 static append_frame* create_at(
567 address addr, int offset_delta, int appends,
568 verification_type_info* types) {
569 append_frame* sm = (append_frame*)addr;
570 sm->set_appends(appends);
571 sm->set_offset_delta(offset_delta);
572 if (types != nullptr) {
573 verification_type_info* cur = sm->types();
574 for (int i = 0; i < appends; ++i) {
575 cur->copy_from(types);
576 cur = cur->next();
577 types = types->next();
578 }
579 }
580 return sm;
581 }
582
583 static size_t calculate_size(int appends, verification_type_info* types) {
584 size_t sz = sizeof(u1) + sizeof(u2);
585 for (int i = 0; i < appends; ++i) {
586 sz += types->size();
587 types = types->next();
588 }
589 return sz;
590 }
591
592 static size_t max_size() {
593 return sizeof(u1) + sizeof(u2) + 3 * verification_type_info::max_size();
594 }
595
596 size_t size() const { return calculate_size(number_of_types(), types()); }
597 int offset_delta() const {
598 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
599 }
600
601 void set_offset_delta(int offset_delta) {
602 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
603 }
604
605 void set_appends(int appends) {
606 assert(appends > 0 && appends < 4, "Bad number of appends");
607 set_frame_type(appends_to_frame_type(appends));
608 }
609
610 int number_of_types() const {
611 int appends = frame_type_to_appends(frame_type());
612 assert(appends > 0 && appends < 4, "Invalid number of appends in frame");
613 return appends;
614 }
615 verification_type_info* types() const {
616 return verification_type_info::at(types_addr());
617 }
618 bool is_valid_offset(int offset) const { return true; }
619
620 bool verify_subtype(address start, address end) const {
621 verification_type_info* vti = types();
622 if ((address)vti < end && vti->verify(start, end)) {
623 int nof = number_of_types();
624 vti = vti->next();
625 if (nof < 2 || vti->verify(start, end)) {
626 vti = vti->next();
627 if (nof < 3 || vti->verify(start, end)) {
628 return true;
629 }
630 }
631 }
632 return false;
633 }
634
635 void print_on(outputStream* st, int current_offset = -1) const {
636 st->print("append_frame(@%d,", offset_delta() + current_offset);
637 verification_type_info* vti = types();
638 for (int i = 0; i < number_of_types(); ++i) {
639 vti->print_on(st);
640 if (i != number_of_types() - 1) {
641 st->print(",");
642 }
643 vti = vti->next();
644 }
645 st->print(")");
646 }
647
648 void print_truncated(outputStream* st, int current_offset = -1) const {
649 st->print("append_frame(@%d), output truncated, Stackmap exceeds table size.",
650 offset_delta() + current_offset);
651 }
652 };
653
654 class full_frame : public stack_map_frame {
655 private:
656 address offset_delta_addr() const { return frame_type_addr() + sizeof(u1); }
657 address num_locals_addr() const { return offset_delta_addr() + sizeof(u2); }
658 address locals_addr() const { return num_locals_addr() + sizeof(u2); }
659 address stack_slots_addr(address end_of_locals) const {
660 return end_of_locals; }
661 address stack_addr(address end_of_locals) const {
662 return stack_slots_addr(end_of_locals) + sizeof(u2); }
663
664 enum { _frame_id = 255 };
665
666 public:
667 static bool is_frame_type(u1 tag) {
668 return tag == _frame_id;
669 }
670
671 static full_frame* at(address addr) {
672 assert(is_frame_type(*addr), "Wrong frame id");
673 return (full_frame*)addr;
674 }
675
676 static full_frame* create_at(
677 address addr, int offset_delta, int num_locals,
678 verification_type_info* locals,
679 int stack_slots, verification_type_info* stack) {
680 full_frame* sm = (full_frame*)addr;
681 sm->set_frame_type(_frame_id);
682 sm->set_offset_delta(offset_delta);
683 sm->set_num_locals(num_locals);
684 if (locals != nullptr) {
685 verification_type_info* cur = sm->locals();
686 for (int i = 0; i < num_locals; ++i) {
687 cur->copy_from(locals);
688 cur = cur->next();
689 locals = locals->next();
690 }
691 address end_of_locals = (address)cur;
692 sm->set_stack_slots(end_of_locals, stack_slots);
693 cur = sm->stack(end_of_locals);
694 for (int i = 0; i < stack_slots; ++i) {
695 cur->copy_from(stack);
696 cur = cur->next();
697 stack = stack->next();
698 }
699 }
700 return sm;
701 }
702
703 static size_t calculate_size(
704 int num_locals, verification_type_info* locals,
705 int stack_slots, verification_type_info* stack) {
706 size_t sz = sizeof(u1) + sizeof(u2) + sizeof(u2) + sizeof(u2);
707 verification_type_info* vti = locals;
708 for (int i = 0; i < num_locals; ++i) {
709 sz += vti->size();
710 vti = vti->next();
711 }
712 vti = stack;
713 for (int i = 0; i < stack_slots; ++i) {
714 sz += vti->size();
715 vti = vti->next();
716 }
717 return sz;
718 }
719
720 static size_t max_size(int locals, int stack) {
721 return sizeof(u1) + 3 * sizeof(u2) +
722 (locals + stack) * verification_type_info::max_size();
723 }
724
725 size_t size() const {
726 address eol = end_of_locals();
727 return calculate_size(num_locals(), locals(), stack_slots(eol), stack(eol));
728 }
729
730 int offset_delta() const {
731 return Bytes::get_Java_u2(offset_delta_addr()) + 1;
732 }
733 int num_locals() const { return Bytes::get_Java_u2(num_locals_addr()); }
734 verification_type_info* locals() const {
735 return verification_type_info::at(locals_addr());
736 }
737 address end_of_locals() const {
738 verification_type_info* vti = locals();
739 for (int i = 0; i < num_locals(); ++i) {
740 vti = vti->next();
741 }
742 return (address)vti;
743 }
744 int stack_slots(address end_of_locals) const {
745 return Bytes::get_Java_u2(stack_slots_addr(end_of_locals));
746 }
747 verification_type_info* stack(address end_of_locals) const {
748 return verification_type_info::at(stack_addr(end_of_locals));
749 }
750
751 void set_offset_delta(int offset_delta) {
752 Bytes::put_Java_u2(offset_delta_addr(), checked_cast<u2>(offset_delta - 1));
753 }
754 void set_num_locals(int num_locals) {
755 Bytes::put_Java_u2(num_locals_addr(), checked_cast<u2>(num_locals));
756 }
757 void set_stack_slots(address end_of_locals, int stack_slots) {
758 Bytes::put_Java_u2(stack_slots_addr(end_of_locals), checked_cast<u2>(stack_slots));
759 }
760
761 // These return only the locals. Extra processing is required for stack
762 // types of full frames.
763 int number_of_types() const { return num_locals(); }
764 verification_type_info* types() const { return locals(); }
765 bool is_valid_offset(int offset) { return true; }
766
767 bool verify_subtype(address start, address end) const {
768 verification_type_info* vti = types();
769 if ((address)vti >= end) {
770 return false;
771 }
772 int count = number_of_types();
773 for (int i = 0; i < count; ++i) {
774 if (!vti->verify(start, end)) {
775 return false;
776 }
777 vti = vti->next();
778 }
779 address eol = (address)vti;
780 if (eol + sizeof(u2) > end) {
781 return false;
782 }
783 count = stack_slots(eol);
784 vti = stack(eol);
785 for (int i = 0; i < stack_slots(eol); ++i) {
786 if (!vti->verify(start, end)) {
787 return false;
788 }
789 vti = vti->next();
790 }
791 return true;
792 }
793
794 void print_on(outputStream* st, int current_offset = -1) const {
795 st->print("full_frame(@%d,{", offset_delta() + current_offset);
796 verification_type_info* vti = locals();
797 for (int i = 0; i < num_locals(); ++i) {
798 vti->print_on(st);
799 if (i != num_locals() - 1) {
800 st->print(",");
801 }
802 vti = vti->next();
803 }
804 st->print("},{");
805 address end_of_locals = (address)vti;
806 vti = stack(end_of_locals);
807 int ss = stack_slots(end_of_locals);
808 for (int i = 0; i < ss; ++i) {
809 vti->print_on(st);
810 if (i != ss - 1) {
811 st->print(",");
812 }
813 vti = vti->next();
814 }
815 st->print("})");
816 }
817
818 void print_truncated(outputStream* st, int current_offset = -1) const {
819 st->print("full_frame(@%d), output truncated, Stackmap exceeds table size.",
820 offset_delta() + current_offset);
821 }
822 };
823
824 #define VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
825 stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
826 if (item_##stack_frame_type != nullptr) { \
827 return item_##stack_frame_type->func_name args; \
828 }
829
830 #define VOID_VIRTUAL_DISPATCH(stack_frame_type, func_name, args) \
831 stack_frame_type* item_##stack_frame_type = as_##stack_frame_type(); \
832 if (item_##stack_frame_type != nullptr) { \
833 item_##stack_frame_type->func_name args; \
834 return; \
835 }
836
837 size_t stack_map_frame::size() const {
838 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, size, ());
839 return 0;
840 }
841
842 int stack_map_frame::offset_delta() const {
843 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, offset_delta, ());
844 return 0;
845 }
846
847 void stack_map_frame::set_offset_delta(int offset_delta) {
848 FOR_EACH_STACKMAP_FRAME_TYPE(
849 VOID_VIRTUAL_DISPATCH, set_offset_delta, (offset_delta));
850 }
851
852 int stack_map_frame::number_of_types() const {
853 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, number_of_types, ());
854 return 0;
855 }
856
857 verification_type_info* stack_map_frame::types() const {
858 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, types, ());
859 return nullptr;
860 }
861
862 bool stack_map_frame::is_valid_offset(int offset) const {
863 FOR_EACH_STACKMAP_FRAME_TYPE(VIRTUAL_DISPATCH, is_valid_offset, (offset));
864 return true;
865 }
866
867 bool stack_map_frame::verify(address start, address end) const {
868 if (frame_type_addr() >= start && frame_type_addr() < end) {
869 FOR_EACH_STACKMAP_FRAME_TYPE(
870 VIRTUAL_DISPATCH, verify_subtype, (start, end));
871 }
872 return false;
873 }
874
875 void stack_map_frame::print_on(outputStream* st, int offs = -1) const {
876 FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_on, (st, offs));
877 }
878
879 void stack_map_frame::print_truncated(outputStream* st, int offs = -1) const {
880 FOR_EACH_STACKMAP_FRAME_TYPE(VOID_VIRTUAL_DISPATCH, print_truncated, (st, offs));
881 }
882
883 #undef VIRTUAL_DISPATCH
884 #undef VOID_VIRTUAL_DISPATCH
885
886 #define AS_SUBTYPE_DEF(stack_frame_type, arg1, arg2) \
887 stack_frame_type* stack_map_frame::as_##stack_frame_type() const { \
888 if (stack_frame_type::is_frame_type(frame_type())) { \
889 return (stack_frame_type*)this; \
890 } else { \
891 return nullptr; \
892 } \
893 }
894
895 FOR_EACH_STACKMAP_FRAME_TYPE(AS_SUBTYPE_DEF, x, x)
896 #undef AS_SUBTYPE_DEF
897
898 class stack_map_table {
899 private:
900 address number_of_entries_addr() const {
901 return (address)this;
902 }
903 address entries_addr() const {
904 return number_of_entries_addr() + sizeof(u2);
905 }
906 NONCOPYABLE(stack_map_table);
907
908 protected:
909 // No constructors - should be 'private', but GCC issues a warning if it is
910 stack_map_table() {}
911
912 public:
913
914 static stack_map_table* at(address addr) {
915 return (stack_map_table*)addr;
916 }
917
918 u2 number_of_entries() const {
919 return Bytes::get_Java_u2(number_of_entries_addr());
920 }
921 stack_map_frame* entries() const {
922 return stack_map_frame::at(entries_addr());
923 }
924
925 void set_number_of_entries(u2 num) {
926 Bytes::put_Java_u2(number_of_entries_addr(), num);
927 }
928 };
929
930 class stack_map_table_attribute {
931 private:
932 address name_index_addr() const {
933 return (address)this; }
934 address attribute_length_addr() const {
935 return name_index_addr() + sizeof(u2); }
936 address stack_map_table_addr() const {
937 return attribute_length_addr() + sizeof(u4); }
938 NONCOPYABLE(stack_map_table_attribute);
939
940 protected:
941 // No constructors - should be 'private', but GCC issues a warning if it is
942 stack_map_table_attribute() {}
943
944 public:
945
946 static stack_map_table_attribute* at(address addr) {
947 return (stack_map_table_attribute*)addr;
948 }
949
950 u2 name_index() const {
951 return Bytes::get_Java_u2(name_index_addr()); }
952 u4 attribute_length() const {
953 return Bytes::get_Java_u4(attribute_length_addr()); }
954 stack_map_table* table() const {
955 return stack_map_table::at(stack_map_table_addr());
956 }
957
958 void set_name_index(u2 idx) {
959 Bytes::put_Java_u2(name_index_addr(), idx);
960 }
961 void set_attribute_length(u4 len) {
962 Bytes::put_Java_u4(attribute_length_addr(), len);
963 }
964 };
965
966 #undef FOR_EACH_STACKMAP_FRAME_TYPE
967
968 #endif // SHARE_CLASSFILE_STACKMAPTABLEFORMAT_HPP