-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathfixreturns.c
824 lines (681 loc) · 21.7 KB
/
fixreturns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "stackmap_internal.h"
#include "rommeth.h"
#include "bcnames.h"
#include "pcstack.h"
#include "argcount.h"
#include "cfreader.h"
#include "ut_map.h"
#define WALKED 1
#if 0 /* StackMapTable based fixreturns removed - CMVC 145180 - bogus stack maps cause shareclasses crash*/
/* Return code only used locally */
#define MISSING_MAPS 1
#endif
static IDATA fixReturnBytecodesInMethod (J9PortLibrary * portLib, J9ROMClass * romClass, J9ROMMethod * romMethod);
static void fixReturns (UDATA *scratch, U_8 * map, J9ROMClass * romClass, J9ROMMethod * romMethod);
#if 0 /* StackMapTable based fixreturns removed */
static IDATA fixReturnsWithStackMaps(J9ROMClass * romClass, J9ROMMethod * romMethod, U_32 * stackMapMethods, UDATA i);
static UDATA getNextStackIndex (U_8 * stackMapData, UDATA mapPC, UDATA stackMapCount);
static UDATA getStackDepth (U_8 ** stackMapData, UDATA * stackMapCount);
static void parseLocals (U_8** stackMapData, IDATA localDelta);
static UDATA parseStack (U_8** stackMapData, UDATA stackCount);
#endif
/*
Called by jar2jxe and the shared class loader to fix returns on unverified ROM classes.
*/
IDATA
fixReturnBytecodes(J9PortLibrary * portLib, struct J9ROMClass* romClass)
{
UDATA i;
J9ROMMethod * romMethod;
IDATA rc = 0;
BOOLEAN isJavaLangObject = (J9ROMCLASS_SUPERCLASSNAME(romClass) == NULL);
Trc_Map_fixReturnBytecodes_Class((UDATA) J9UTF8_LENGTH(J9ROMCLASS_CLASSNAME(romClass)), J9UTF8_DATA(J9ROMCLASS_CLASSNAME(romClass)));
romMethod = J9ROMCLASS_ROMMETHODS(romClass);
for (i = 0; i < romClass->romMethodCount; i++, romMethod = nextROMMethod(romMethod)) {
if ((romMethod->modifiers & (CFR_ACC_NATIVE | CFR_ACC_ABSTRACT)) == 0) {
if (isJavaLangObject) {
/* Avoid rewriting genericReturn for Object.<init>() */
U_8 * nameData = J9UTF8_DATA(J9ROMMETHOD_NAME(romMethod));
if (('<' == nameData[0]) && ('i' == nameData[1]) && (1 == romMethod->argCount)) {
continue;
}
}
rc = fixReturnBytecodesInMethod (portLib, romClass, romMethod);
if (rc != BCT_ERR_NO_ERROR) {
return rc;
}
}
}
return 0;
}
/*
Returns the bytecode for cleaning a method return - also returns the number of slots in the parameter.
*/
U_8
getReturnBytecode(J9ROMClass * romClass, J9ROMMethod * romMethod, UDATA * returnSlots)
{
U_8 sigChar, returnBytecode;
J9UTF8 * name = J9ROMMETHOD_NAME(romMethod);
J9UTF8 * signature = J9ROMMETHOD_SIGNATURE(romMethod);
U_8 * sigData = J9UTF8_DATA(signature);
UDATA sigLength = J9UTF8_LENGTH(signature);
/* Determine correct number of return slots for this method based on signature */
*returnSlots = 0;
sigChar = sigData[sigLength - 1];
/* Update the sig char in the case we are dealing with an array */
if ('[' == sigData[sigLength - 2]) {
sigChar = '[';
}
if (sigChar != 'V') {
*returnSlots = 1;
if (sigChar == 'J' || sigChar == 'D') {
*returnSlots = 2;
}
}
/* Determine the correct return bytecode to insert */
if ((J9UTF8_DATA(name)[0] == '<')
&& (J9UTF8_DATA(name)[1] == 'i')
&& !J9ROMCLASS_IS_VALUE(romClass)
) {
returnBytecode = JBreturnFromConstructor;
} else {
/* bool, byte, char, and short need special treatment since they need to be truncated before return */
if (romMethod->modifiers & J9AccSynchronized) {
switch(sigChar){
case 'Z':
case 'B':
case 'C':
case 'S':
returnBytecode = JBgenericReturn;
break;
default:
returnBytecode = JBsyncReturn0 + (U_8) *returnSlots;
break;
}
} else {
switch(sigChar){
case 'Z':
returnBytecode = JBreturnZ;
break;
case 'B':
returnBytecode = JBreturnB;
break;
case 'C':
returnBytecode = JBreturnC;
break;
case 'S':
returnBytecode = JBreturnS;
break;
default:
returnBytecode = JBreturn0 + (U_8) *returnSlots;
break;
}
}
}
return returnBytecode;
}
/*
Set up a data flow walk to identify and clean any return bytecodes.
The data consists of a "walked" map and a stack with unwalked branch pointers and matching depth counts.
*/
static IDATA
fixReturnBytecodesInMethod(J9PortLibrary * portLib, J9ROMClass * romClass, J9ROMMethod * romMethod)
{
PORT_ACCESS_FROM_PORT(portLib);
UDATA length;
UDATA *scratch;
UDATA scratchSize;
UDATA accurateGuess = FALSE;
U_8 *map;
#define LOCAL_SCRATCH 2048
UDATA localScratch[LOCAL_SCRATCH / sizeof(UDATA)];
UDATA *allocScratch = NULL;
length = (UDATA) J9_ROUNDED_BYTECODE_SIZE_FROM_ROM_METHOD(romMethod);
scratchSize = (romClass->maxBranchCount * sizeof(UDATA) * 2) + length;
/* Allocate sufficient buffer */
if(scratchSize < LOCAL_SCRATCH) {
scratch = localScratch;
} else {
allocScratch = j9mem_allocate_memory(scratchSize, OMRMEM_CATEGORY_VM);
if (allocScratch) {
scratch = allocScratch;
} else {
Trc_Map_fixReturnBytecodesInMethod_AllocationFailure(scratchSize);
return BCT_ERR_OUT_OF_MEMORY;
}
}
/* Allocate the instruction map at end of scratch space */
map = (U_8 *) (scratch + (romClass->maxBranchCount * 2));
memset(map, 0, length);
fixReturns(scratch, map, romClass, romMethod);
j9mem_free_memory(allocScratch);
return BCT_ERR_NO_ERROR;
}
/*
Data flow walk to identify the stack depth at return points and replace generic returns with the correct return type.
*/
static void
fixReturns(UDATA *scratch, U_8 * map, J9ROMClass * romClass, J9ROMMethod * romMethod)
{
IDATA pc;
I_32 low, high, offset;
U_32 index;
U_8 *bcStart, *bcIndex, *bcEnd;
U_8 sigChar, returnBytecode;
UDATA bc, action, size, temp1, temp2, target, npairs;
UDATA returnSlots;
UDATA maxStack = (UDATA) J9_MAX_STACK_FROM_ROM_METHOD(romMethod);
UDATA depth = 0;
UDATA length;
J9UTF8 *utf8Signature;
J9SRP *callSiteData = (J9SRP *) J9ROMCLASS_CALLSITEDATA(romClass);
UDATA *pendingStacks = scratch;
J9ROMConstantPoolItem *pool = (J9ROMConstantPoolItem *) & (romClass[1]);
J9ExceptionInfo *exceptionData = J9_EXCEPTION_DATA_FROM_ROM_METHOD(romMethod);
/* Determine the correct return bytecode to insert and the expected depth for a fixable return */
returnBytecode = getReturnBytecode(romClass, romMethod, &returnSlots);
/* Add exceptions to the pending walks list */
if (J9ROMMETHOD_HAS_EXCEPTION_INFO(romMethod)) {
UDATA exceptionsToWalk = (UDATA) exceptionData->catchCount;
J9ExceptionHandler *handler = J9EXCEPTIONINFO_HANDLERS(exceptionData);
while (exceptionsToWalk) {
*pendingStacks++ = handler[--exceptionsToWalk].handlerPC;
*pendingStacks++ = 1;
}
}
bcStart = J9_BYTECODE_START_FROM_ROM_METHOD(romMethod);
bcIndex = bcStart;
length = J9_BYTECODE_SIZE_FROM_ROM_METHOD(romMethod);
bcEnd = bcStart + length;
while (bcIndex < bcEnd) {
pc = bcIndex - bcStart;
bc = (UDATA) *bcIndex;
if (map[pc]) {
/* We have been here before, stop this scan */
goto _nextRoot;
}
map[pc] = WALKED;
size = (UDATA) J9JavaInstructionSizeAndBranchActionTable[bc];
action = (UDATA) JavaStackActionTable[bc];
/* action encodes pushes and pops by the bytecode - 0x80 means something special */
if (action != 0x80) {
depth -= (action & 7);
depth += ((action >> 4) & 3);
if (!(size >> 4)) {
bcIndex += size;
if (size == 0) {
/* Unknown bytecode */
Trc_Map_fixReturns_UnknownBytecode(bc, pc);
return;
}
continue;
}
switch (size >> 4) {
case 1: /* ifs */
offset = (I_16) PARAM_16(bcIndex, 1);
target = (UDATA) (pc + (I_16) offset);
if (!map[target]) {
*pendingStacks++ = target;
*pendingStacks++ = depth;
}
bcIndex += (size & 7);
continue;
case 2: /* gotos */
offset = (I_16) PARAM_16(bcIndex, 1);
target = (UDATA) (pc + (I_16) offset);
if (bc == JBgotow) {
offset = (I_32) PARAM_32(bcIndex, 1);
target = (UDATA) (pc + offset);
}
bcIndex = bcStart + target;
continue;
case 4: /* returns/athrow */
/* fix the generic returns only - already changed returns */
/* may have been changed to forward a verification error */
if (bc == JBgenericReturn) {
if (depth == returnSlots) {
*bcIndex = returnBytecode;
}
}
goto _nextRoot;
case 5: /* switches */
bcIndex = bcIndex + (4 - (pc & 3));
offset = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
temp2 = (UDATA) (pc + offset);
low = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
if (bc == JBtableswitch) {
high = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
npairs = (UDATA) (high - low + 1);
temp1 = 0;
} else {
npairs = (UDATA) low;
/* used to skip over the tableswitch key entry */
temp1 = 4;
}
for (; npairs > 0; npairs--) {
bcIndex += temp1;
offset = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
target = (UDATA) (pc + (I_32) offset);
if (!map[target]) {
*pendingStacks++ = target;
*pendingStacks++ = depth;
}
}
/* finally continue at the default switch case */
bcIndex = bcStart + temp2;
continue;
case 7: /* breakpoint */
/* Unexpected bytecode - unknown */
Trc_Map_fixReturns_UnknownBytecode(bc, pc);
return;
default:
bcIndex += (size & 7);
continue;
}
} else {
switch (bc) {
case JBdup2:
case JBdup2x1:
case JBdup2x2:
depth++; /* fall through case */
case JBldc:
case JBldcw:
case JBdup:
case JBdupx1:
case JBdupx2:
depth++; /* fall through case */
case JBswap:
break;
case JBgetfield:
depth--; /* fall through case */
case JBgetstatic:
index = PARAM_16(bcIndex, 1);
utf8Signature =
J9ROMNAMEANDSIGNATURE_SIGNATURE(J9ROMFIELDREF_NAMEANDSIGNATURE
((J9ROMFieldRef *) (&(pool[index]))));
sigChar = (U_8) J9UTF8_DATA(utf8Signature)[0];
depth += (UDATA) argCountCharConversion[sigChar - 'A'];
break;
case JBputfield:
depth--; /* fall through case !!! */
case JBputstatic:
index = PARAM_16(bcIndex, 1);
utf8Signature =
J9ROMNAMEANDSIGNATURE_SIGNATURE(J9ROMFIELDREF_NAMEANDSIGNATURE
((J9ROMFieldRef *) (&(pool[index]))));
sigChar = (U_8) J9UTF8_DATA(utf8Signature)[0];
depth -= (UDATA) argCountCharConversion[sigChar - 'A'];
break;
case JBinvokeinterface2:
bcIndex += 2;
continue;
case JBinvokehandle:
case JBinvokehandlegeneric:
case JBinvokevirtual:
case JBinvokespecial:
case JBinvokespecialsplit:
case JBinvokeinterface:
depth--;
case JBinvokedynamic:
case JBinvokestaticsplit:
case JBinvokestatic: {
index = PARAM_16(bcIndex, 1);
if (JBinvokestaticsplit == bc) {
index = *(U_16 *)(J9ROMCLASS_STATICSPLITMETHODREFINDEXES(romClass) + index);
} else if (JBinvokespecialsplit == bc) {
index = *(U_16 *)(J9ROMCLASS_SPECIALSPLITMETHODREFINDEXES(romClass) + index);
}
if (bc == JBinvokedynamic) {
/* TODO 3 byte index */
utf8Signature = (J9UTF8 *) (J9ROMNAMEANDSIGNATURE_SIGNATURE(SRP_PTR_GET(callSiteData + index, J9ROMNameAndSignature*)));
} else {
utf8Signature =
J9ROMNAMEANDSIGNATURE_SIGNATURE(J9ROMMETHODREF_NAMEANDSIGNATURE
((J9ROMMethodRef *) (&(pool[index]))));
}
depth -= (UDATA) getSendSlotsFromSignature(J9UTF8_DATA(utf8Signature));
sigChar = J9UTF8_DATA(utf8Signature)[J9UTF8_LENGTH(utf8Signature) - 1];
if (sigChar != 'V') {
depth++;
if (((sigChar == 'J') || (sigChar == 'D')) &&
(J9UTF8_DATA(utf8Signature)[J9UTF8_LENGTH(utf8Signature) - 2] != '['))
{
depth++;
}
}
if (bc == JBinvokeinterface2) {
bcIndex -= 2;
}
break;
}
case JBmultianewarray:
index = PARAM_8(bcIndex, 3);
depth -= index;
depth++;
break;
}
bcIndex += (size & 7);
continue;
}
_nextRoot:
if (pendingStacks == scratch) {
return;
}
/* pop the next pending stack for walking */
depth = *(--pendingStacks);
bcIndex = bcStart + *(--pendingStacks);
}
Trc_Map_fixReturns_WalkOffEndOfBytecodeArray();
/* Fell off end of bytecode array - should never get here */
}
#if 0 /* StackMapTable based fixreturns removed */
/*
Linear walk utilizing stackmaps to identify the stack depth at return points
and replace generic returns with the correct return type.
*/
static IDATA
fixReturnsWithStackMaps(J9ROMClass * romClass, J9ROMMethod * romMethod, U_32 * stackMapMethods, UDATA i)
{
I_32 low, high;
U_32 index;
U_8 *bcStart, *bcIndex, *bcEnd;
UDATA nextStackIndex = (UDATA) -1;
U_8 *stackMapData = NULL;
U_8 sigChar, returnBytecode;
UDATA bc, action, size, slots;
UDATA returnSlots;
UDATA depth = 0;
UDATA stackMapCount = 0;
J9UTF8 *utf8Signature;
J9SRP *callSiteData = (J9SRP *) J9ROMCLASS_CALLSITEDATA(romClass);
UDATA needStack = FALSE;
J9ROMConstantPoolItem *pool = (J9ROMConstantPoolItem *) & (romClass[1]);
/*Access the stored stack map data for this method, get pointer and map count */
if (stackMapMethods) {
if (stackMapMethods[i]) {
stackMapData = SRP_GET(stackMapMethods[i], U_8 *);
NEXT_U16(stackMapCount, stackMapData);
nextStackIndex = getNextStackIndex (stackMapData, nextStackIndex, stackMapCount);
}
}
/* Determine the correct return bytecode to insert and the expected depth for a fixable return */
returnBytecode = getReturnBytecode(romClass, romMethod, &returnSlots);
bcStart = J9_BYTECODE_START_FROM_ROM_METHOD(romMethod);
bcIndex = bcStart;
bcEnd = bcStart + J9_BYTECODE_SIZE_FROM_ROM_METHOD(romMethod);
while (bcIndex < bcEnd) {
bc = (UDATA) *bcIndex;
if (((UDATA) (bcIndex - bcStart)) == nextStackIndex) {
depth = getStackDepth (&stackMapData, &stackMapCount);
nextStackIndex = getNextStackIndex (stackMapData, (bcIndex - bcStart), stackMapCount);
needStack = FALSE;
}
/* Should always have a stack at least after any goto, athrow, return or switch type bytecode */
/* if not found, default to the flow mapper for this method */
if (needStack) {
return MISSING_MAPS;
}
size = (UDATA) J9JavaInstructionSizeAndBranchActionTable[bc];
action = (UDATA) JavaStackActionTable[bc];
/* action encodes pushes and pops by the bytecode - 0x80 means something special */
if (action != 0x80) {
depth -= (action & 7);
depth += ((action >> 4) & 3);
/* Check upper nibble of size is less than 2 */
if ((size & 0xE0) == 0) {
bcIndex += (size & 7);
if (size == 0) {
/* Must check for zero else it will infinitely loop */
/* Unknown bytecode - will fail verification later */
Trc_Map_fixReturnsWithStackMaps_UnknownBytecode(bc, (bcIndex - bcStart));
return BCT_ERR_NO_ERROR;
}
continue;
}
switch (size >> 4) {
case 2: /* goto/gotow */
case 4: /* returns/athrow */
/* fix the generic returns only - already changed returns */
/* may have been changed to forward a verification error */
if (bc == JBgenericReturn) {
if (depth == returnSlots) {
*bcIndex = returnBytecode;
} else {
/* Fail if a return cannot be fixed - use the flow algorithm */
return MISSING_MAPS;
}
}
bcIndex += (size & 7);
needStack = TRUE;
continue;
case 5: /* switches */
bcIndex = bcIndex + (4 - ((bcIndex - bcStart) & 3));
low = (I_32) PARAM_32(bcIndex, 4);
bcIndex += 8;
if (bc == CFR_BC_tableswitch) {
high = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
slots = high - low + 1;
} else {
slots = low * 2;
}
bcIndex += (slots * 4);
needStack = TRUE;
continue;
default:
bcIndex += (size & 7);
continue;
}
} else {
switch (bc) {
case JBdup2:
case JBdup2x1:
case JBdup2x2:
depth++; /* fall through case */
case JBldc:
case JBldcw:
case JBdup:
case JBdupx1:
case JBdupx2:
depth++; /* fall through case */
case JBswap:
break;
case JBgetfield:
depth--; /* fall through case */
case JBgetstatic:
index = PARAM_16(bcIndex, 1);
utf8Signature =
J9ROMNAMEANDSIGNATURE_SIGNATURE(J9ROMFIELDREF_NAMEANDSIGNATURE
((J9ROMFieldRef *) (&(pool[index]))));
sigChar = (U_8) J9UTF8_DATA(utf8Signature)[0];
depth += (UDATA) argCountCharConversion[sigChar - 'A'];
break;
case JBputfield:
depth--; /* fall through case !!! */
case JBputstatic:
index = PARAM_16(bcIndex, 1);
utf8Signature =
J9ROMNAMEANDSIGNATURE_SIGNATURE(J9ROMFIELDREF_NAMEANDSIGNATURE
((J9ROMFieldRef *) (&(pool[index]))));
sigChar = (U_8) J9UTF8_DATA(utf8Signature)[0];
depth -= (UDATA) argCountCharConversion[sigChar - 'A'];
break;
case JBinvokeinterface2:
bcIndex += 2;
/* Point to JBinterface and adjust remaining size */
size = 3;
/* Fall through */
case JBinvokehandle:
case JBinvokehandlegeneric:
case JBinvokevirtual:
case JBinvokespecial:
case JBinvokeinterface:
depth--;
case JBinvokedynamic:
case JBinvokestatic:
index = PARAM_16(bcIndex, 1);
if (bc == JBinvokedynamic) {
/* TODO 3 byte index */
utf8Signature = (J9UTF8 *) (J9ROMNAMEANDSIGNATURE_SIGNATURE(SRP_PTR_GET(callSiteData + index, J9ROMNameAndSignature*)));
} else {
utf8Signature =
J9ROMNAMEANDSIGNATURE_SIGNATURE(J9ROMMETHODREF_NAMEANDSIGNATURE
((J9ROMMethodRef *) (&(pool[index]))));
}
depth -= (UDATA) getSendSlotsFromSignature(J9UTF8_DATA(utf8Signature));
sigChar = J9UTF8_DATA(utf8Signature)[J9UTF8_LENGTH(utf8Signature) - 1];
if (sigChar != 'V') {
depth++;
if (((sigChar == 'J') || (sigChar == 'D')) &&
(J9UTF8_DATA(utf8Signature)[J9UTF8_LENGTH(utf8Signature) - 2] != '[')) {
depth++;
}
}
break;
case JBmultianewarray:
index = PARAM_8(bcIndex, 3);
depth -= index;
depth++;
break;
}
bcIndex += (size & 7);
}
}
return BCT_ERR_NO_ERROR;
}
/*
Extract and return the bytecode offset for the next stack map. -1 indicates no further maps.
*/
static UDATA
getNextStackIndex (U_8 * stackMapData, UDATA mapPC, UDATA stackMapCount)
{
U_8 mapType;
UDATA temp;
if (stackMapCount == 0) {
return (UDATA) -1;
}
mapPC++;
NEXT_U8(mapType, stackMapData);
if (mapType < CFR_STACKMAP_SAME_LOCALS_1_STACK) {
/* offset delta encoded in the mapType */
mapPC += (UDATA) mapType;
} else if (mapType < CFR_STACKMAP_SAME_LOCALS_1_STACK_END) {
/* offset delta encoded in the mapType */
mapPC += ((UDATA) mapType - CFR_STACKMAP_SAME_LOCALS_1_STACK);
} else {
/* offset delta explicit after the mapType */
mapPC += NEXT_U16(temp, stackMapData);
}
return mapPC;
}
/*
Calculate the stack depth from the current stack map.
Most occurrences will be zero.
*/
static UDATA
getStackDepth (U_8 ** stackMapData, UDATA * stackMapCount)
{
U_8 mapType;
UDATA temp;
UDATA depth = 0;
if (*stackMapCount) {
IDATA localDelta = 0;
UDATA stackCount = 0;
(*stackMapCount)--;
NEXT_U8(mapType, *stackMapData);
if (mapType < CFR_STACKMAP_SAME_LOCALS_1_STACK) {
/* done */
return depth;
} else if (mapType < CFR_STACKMAP_SAME_LOCALS_1_STACK_END) {
/* Same with one stack entry frame 64-127 */
stackCount = 1;
} else {
/* Skip the map offset_delta */
NEXT_U16(temp, *stackMapData);
if (mapType == CFR_STACKMAP_SAME_LOCALS_1_STACK_EXTENDED) {
/* Same with one stack entry extended address frame 247 */
stackCount = 1;
} else if (mapType < CFR_STACKMAP_FULL) {
/* Chop 3-1 locals frame 248-250 */
/* Same with extended address frame 251 */
/* Append 1-3 locals frame 252-254 */
localDelta = ((IDATA) mapType) - CFR_STACKMAP_SAME_EXTENDED;
} else if (mapType == CFR_STACKMAP_FULL) {
/* Full frame 255 */
NEXT_U16(localDelta, *stackMapData);
}
}
parseLocals (stackMapData, localDelta);
if (mapType == CFR_STACKMAP_FULL) {
stackCount = NEXT_U16(temp, *stackMapData);
}
depth = parseStack (stackMapData, stackCount);
}
return depth;
}
/*
Parse through the local variable descriptors in a stack map - throw away.
*/
static void
parseLocals (U_8** stackMapData, IDATA localDelta)
{
/* Eat the local descriptors */
if (localDelta > 0) {
for (;localDelta; localDelta--) {
U_8 entryType = **stackMapData;
(*stackMapData)++;
if (entryType >= CFR_STACKMAP_TYPE_OBJECT) {
(*stackMapData) += 2;
}
}
}
}
/*
Parse through the stack descriptors to calculate the depth.
*/
static UDATA
parseStack (U_8** stackMapData, UDATA stackCount)
{
/* Starting depth is one per map descriptor */
UDATA depth = stackCount;
for (;stackCount; stackCount--) {
U_8 entryType = **stackMapData;
/* Skip one descriptor byte per entry */
(*stackMapData)++;
if ((entryType == CFR_STACKMAP_TYPE_DOUBLE) || (entryType == CFR_STACKMAP_TYPE_LONG)) {
/* Increase depth for each Double/Long - they take two slots */
depth++;
} else if (entryType >= CFR_STACKMAP_TYPE_OBJECT) {
/* Skip two more descriptor bytes for other entries */
/* It is either constant pool index or an array size */
(*stackMapData) += 2;
}
}
return depth;
}
#endif