-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathextendedMessageNPE.cpp
2050 lines (1866 loc) · 69.2 KB
/
extendedMessageNPE.cpp
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 2020
*
* 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 "bcverify.h"
#include "cfreader.h"
#include "pcstack.h"
#include "rommeth.h"
#include "stackwalk.h"
#include "ut_j9vm.h"
#include "vrfytbl.h"
extern "C" {
/* Define for debug
#define DEBUG_BCV
*/
static IDATA buildBranchMap(J9NPEMessageData *npeMsgData);
static void computeNPEMsgAtPC(J9VMThread *vmThread, J9ROMMethod *romMethod, J9ROMClass *romClass, UDATA npePC,
bool npeFinalFlag, char **npeMsg, bool *isMethodFlag, UDATA *temps, J9BytecodeOffset *bytecodeOffset);
static char* convertToJavaFullyQualifiedName(J9VMThread *vmThread, J9UTF8 *fullyQualifiedNameUTF);
static char* convertMethodSignature(J9VMThread *vmThread, J9UTF8 *methodSig);
static IDATA copyToTargetStack(J9NPEMessageData *npeMsgData, UDATA target);
static char* getCompleteNPEMessage(J9VMThread *vmThread, U_8 *bcCurrentPtr, J9ROMClass *romClass, char *npeCauseMsg, bool isMethodFlag);
static char* getFullyQualifiedMethodName(J9VMThread *vmThread, J9ROMClass *romClass, U_8 *bcIndex);
static char* getLocalsName(J9VMThread *vmThread, J9ROMMethod *romMethod, U_16 localVar, UDATA bcCausePos, UDATA *temps);
static char* getMsgWithAllocation(J9VMThread *vmThread, const char *msgTemplate, ...);
static IDATA initializeNPEMessageData(J9NPEMessageData *npeMsgData);
static void initStackFromMethodSignature(J9VMThread *vmThread, J9ROMMethod *romMethod, UDATA **stackTopPtr);
static UDATA* pushViaSiganature(J9VMThread *vmThread, U_8 *signature, UDATA *stackTop, UDATA bcPos);
static void setSrcBytecodeOffset(J9BytecodeOffset *bytecodeOffset, UDATA bcPos, UDATA first, UDATA second);
static char* simulateStack(J9NPEMessageData *npeMsgData);
#define BCV_INTERNAL_DEFAULT_SIZE (32*1024)
#define BYTECODE_BRANCH_TARGET 0xFEFEFEFE
#define BYTECODE_TEMP_CHANGED 0xFDFDFDFD
#define BYTECODE_OFFSET_SHIFT 16
#define BYTECODE_OFFSET_MASK 0xFFFF
#define NPEMSG_FIRST_STACK() \
((J9BranchTargetStack *) (npeMsgData->stackMaps))
#define NPEMSG_NEXT_STACK(thisStack) \
((J9BranchTargetStack *) (((U_8 *) (thisStack)) + (npeMsgData->stackSize)))
#define NPEMSG_INDEX_STACK(stackCount) \
((J9BranchTargetStack *) (((U_8 *) NPEMSG_FIRST_STACK()) + (npeMsgData->stackSize * (stackCount))))
#define NPEMSG_CHECK_STACK_UNDERFLOW \
if( stackTop < stackBase ) { \
goto _internalError; \
}
#define NPEMSG_DROP( x ) \
stackTop -= x; \
NPEMSG_CHECK_STACK_UNDERFLOW
#define NPEMSG_POP \
*(--stackTop); \
NPEMSG_CHECK_STACK_UNDERFLOW
#define ALLOC_BUFFER(name, needed, typecast) \
if (needed > name##Size) { \
j9mem_free_memory(name); \
name = typecast j9mem_allocate_memory(needed, OMRMEM_CATEGORY_VM); \
if (NULL != name) { \
memset(name, 0, needed); \
name##Size = needed; \
} else { \
name##Size = 0; \
result = BCV_ERR_INSUFFICIENT_MEMORY; \
} \
} else { \
memset(name, 0, name##Size); \
}
/* Following two macro arguments must be variables, not expressions */
#define GETNEXT_U16(value, index) (value = *(U_16*)index, index += 2, value)
#define GETNEXT_U8(value, index) (value = *(index++))
/* Mapping the verification encoding in J9JavaBytecodeVerificationTable */
static const U_32 decodeTable[] = {
0x0, /* return index */
BCV_BASE_TYPE_INT, /* CFR_STACKMAP_TYPE_INT */
BCV_BASE_TYPE_FLOAT, /* CFR_STACKMAP_TYPE_FLOAT */
BCV_BASE_TYPE_DOUBLE, /* CFR_STACKMAP_TYPE_DOUBLE */
BCV_BASE_TYPE_LONG, /* CFR_STACKMAP_TYPE_LONG */
BCV_BASE_TYPE_NULL, /* CFR_STACKMAP_TYPE_NULL */
0x6, /* return index */
BCV_GENERIC_OBJECT, /* CFR_STACKMAP_TYPE_OBJECT */
0x8, /* return index */
0x9, /* return index */
0xA, /* return index */
0xB, /* return index */
0xC, /* return index */
0xD, /* return index */
0xE, /* return index */
0xF /* return index */
};
/**
* Replace '/' with '.' for an internal fully qualified name
*
* Note: the caller is responsible for freeing the returned string if it is not NULL.
*
* @param[in] vmThread current J9VMThread
* @param[in] fullyQualifiedNameUTF pointer to J9UTF8 containing the fully qualified name
*
* @return a char pointer to the fully qualified name,
* NULL if not successful, but keep application exception instead of throwing OOM.
*/
static char*
convertToJavaFullyQualifiedName(J9VMThread *vmThread, J9UTF8 *fullyQualifiedNameUTF)
{
PORT_ACCESS_FROM_VMC(vmThread);
UDATA length = J9UTF8_LENGTH(fullyQualifiedNameUTF);
char *result = (char *)j9mem_allocate_memory(length + 1, OMRMEM_CATEGORY_VM);
if (NULL != result) {
char *cursor = result;
char *end = result + length;
memcpy(result, J9UTF8_DATA(fullyQualifiedNameUTF), length);
while (cursor < end) {
if ('/' == *cursor) {
*cursor = '.';
}
cursor += 1;
}
*end = '\0';
}
Trc_VM_ConvertToJavaFullyQualifiedName_Get_ClassName(vmThread, result, length, fullyQualifiedNameUTF);
return result;
}
/**
* Convert a signature string
* from
* ([[[Ljava/lang/String;Ljava/lang/Void;BLjava/lang/String;ICDFJLjava/lang/Object;SZ)Ljava/lang/String;
* to
* (java.lang.String[][][], java.lang.Void, byte, java.lang.String, int, char, double, float, long, java.lang.Object, short, boolean)
*
* Note:
* The return type is ignored.
* Assuming the method signature is well formed.
* The caller is responsible for freeing the returned string if it is not NULL.
*
* @param[in] vmThread current J9VMThread
* @param[in] methodSig pointer to J9UTF8 containing the method signature
*
* @return a char pointer to the class name within fullQualifiedUTF,
* NULL if not successful, but keep application exception instead of OOM.
*/
static char*
convertMethodSignature(J9VMThread *vmThread, J9UTF8 *methodSig)
{
UDATA i = 0;
UDATA j = 0;
U_8 *string = J9UTF8_DATA(methodSig);
UDATA stringLength = J9UTF8_LENGTH(methodSig);
UDATA bufferSize = 0;
char *result = NULL;
PORT_ACCESS_FROM_VMC(vmThread);
/* first scan to calculate the buffer size required */
/* first character is '(' */
bufferSize += 1;
for (i = 1; (')' != string[i]); i++) {
UDATA arity = 0;
while ('[' == string[i]) {
arity += 1;
i += 1;
}
switch (string[i]) {
case 'B': /* FALLTHROUGH */
case 'C': /* FALLTHROUGH */
case 'J':
/* byte, char, long */
bufferSize += 4;
break;
case 'D':
/* double */
bufferSize += 6;
break;
case 'F': /* FALLTHROUGH */
case 'S':
/* float, short */
bufferSize += 5;
break;
case 'I':
/* int */
bufferSize += 3;
break;
case 'L':
{
i += 1;
UDATA objSize = 0;
while (';' != string[i]) {
objSize += 1;
i += 1;
}
bufferSize += objSize;
break;
}
case 'Z':
/* boolean */
bufferSize += 7;
break;
default:
Trc_VM_ConvertMethodSignature_Malformed_Signature(vmThread, stringLength, string, i);
break;
}
bufferSize += (2 * arity);
if (')' != string[i + 1]) {
/* ", " */
bufferSize += 2;
}
}
/* ')' and the extra byte for '\0' */
bufferSize += 2;
Trc_VM_ConvertMethodSignature_Signature_BufferSize(vmThread, stringLength, string, bufferSize);
result = (char *)j9mem_allocate_memory(bufferSize, OMRMEM_CATEGORY_VM);
if (NULL != result) {
char *cursor = result;
UDATA availableSize = bufferSize;
memset(result, 0, bufferSize);
/* first character is '(' */
j9str_printf(cursor, availableSize, "(");
cursor += 1;
availableSize -= 1;
for (i = 1; (')' != string[i]); i++) {
UDATA arity = 0;
while ('[' == string[i]) {
arity += 1;
i += 1;
}
const char *elementType = NULL;
if (IS_CLASS_SIGNATURE(string[i])) {
i += 1;
UDATA objSize = 0;
while (';' != string[i]) {
*cursor = string[i];
if ('/' == string[i]) {
*cursor = '.';
}
objSize += 1;
cursor += 1;
i += 1;
}
availableSize -= objSize;
} else {
switch (string[i]) {
case 'B':
elementType = "byte";
break;
case 'C':
elementType = "char";
break;
case 'D':
elementType = "double";
break;
case 'F':
elementType = "float";
break;
case 'I':
elementType = "int";
break;
case 'J':
elementType = "long";
break;
case 'S':
elementType = "short";
break;
case 'Z':
elementType = "boolean";
break;
default:
Trc_VM_ConvertMethodSignature_Malformed_Signature(vmThread, stringLength, string, i);
break;
}
UDATA elementLength = strlen(elementType);
j9str_printf(cursor, availableSize, elementType);
cursor += elementLength;
availableSize -= elementLength;
}
for(j = 0; j < arity; j++) {
j9str_printf(cursor, availableSize, "[]");
cursor += 2;
availableSize -= 2;
}
if (')' != string[i + 1]) {
j9str_printf(cursor, availableSize, ", ");
cursor += 2;
availableSize -= 2;
}
}
j9str_printf(cursor, availableSize, ")");
} else {
bufferSize = 0;
}
Trc_VM_ConvertMethodSignature_Signature_Result(vmThread, result, bufferSize);
return result;
}
/**
* Return an extended NPE message.
*
* Note: the caller is responsible for freeing the returned string if it is not NULL.
*
* @param[in] vmThread The current J9VMThread
* @param[in] bcCurrentPtr The pointer to the bytecode being executed and caused the NPE
* @param[in] romClass The romClass of the bytecode
* @param[in] npeCauseMsg The cause of NPE
* @param[in] isMethodFlag The flag indicates if "the return value of" should be added into the message
*
* @return an extended NPE message or NULL if such a message can't be generated
*/
static char*
getCompleteNPEMessage(J9VMThread *vmThread, U_8 *bcCurrentPtr, J9ROMClass *romClass, char *npeCauseMsg, bool isMethodFlag)
{
char *npeMsg = NULL;
const char *msgTemplate = NULL;
U_8 bcCurrent = *bcCurrentPtr;
PORT_ACCESS_FROM_VMC(vmThread);
Trc_VM_GetCompleteNPEMessage_Entry(vmThread, bcCurrentPtr, romClass, bcCurrent, npeCauseMsg, isMethodFlag);
if (((bcCurrent >= JBiaload) && (bcCurrent <= JBsaload))
|| ((bcCurrent >= JBiastore) && (bcCurrent <= JBsastore))
) {
const char *elementType = NULL;
switch (bcCurrent) {
case JBiaload: /* FALLTHROUGH */
case JBiastore:
elementType = "int";
break;
case JBlaload: /* FALLTHROUGH */
case JBlastore:
elementType = "long";
break;
case JBfaload: /* FALLTHROUGH */
case JBfastore:
elementType = "float";
break;
case JBdaload: /* FALLTHROUGH */
case JBdastore:
elementType = "double";
break;
case JBaaload: /* FALLTHROUGH */
case JBaastore:
elementType = "object";
break;
case JBbaload: /* FALLTHROUGH */
case JBbastore:
elementType = "byte/boolean";
break;
case JBcaload: /* FALLTHROUGH */
case JBcastore:
elementType = "char";
break;
case JBsaload: /* FALLTHROUGH */
case JBsastore:
elementType = "short";
break;
default:
Trc_VM_GetCompleteNPEMessage_Unreachable(vmThread, bcCurrent);
}
if (NULL == npeCauseMsg) {
if ((bcCurrent >= JBiaload) && (bcCurrent <= JBsaload)) {
msgTemplate = "Cannot load from %s array";
} else {
msgTemplate = "Cannot store to %s array";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, elementType);
} else {
if ((bcCurrent >= JBiaload) && (bcCurrent <= JBsaload)) {
if (isMethodFlag) {
msgTemplate = "Cannot load from %s array because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot load from %s array because \"%s\" is null";
}
} else {
if (isMethodFlag) {
msgTemplate = "Cannot store to %s array because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot store to %s array because \"%s\" is null";
}
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, elementType, npeCauseMsg);
}
} else {
switch (bcCurrent) {
case JBarraylength: {
if (NULL == npeCauseMsg) {
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, "Cannot read the array length");
} else {
if (isMethodFlag) {
msgTemplate = "Cannot read the array length because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot read the array length because \"%s\" is null";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, npeCauseMsg);
}
break;
}
case JBathrow: {
if (NULL == npeCauseMsg) {
npeMsg = getMsgWithAllocation(vmThread, "Cannot throw exception");
} else {
if (isMethodFlag) {
msgTemplate = "Cannot throw exception because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot throw exception because \"%s\" is null";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, npeCauseMsg);
}
break;
}
case JBmonitorenter: {
if (NULL == npeCauseMsg) {
npeMsg = getMsgWithAllocation(vmThread, "Cannot enter synchronized block");
} else {
if (isMethodFlag) {
msgTemplate = "Cannot enter synchronized block because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot enter synchronized block because \"%s\" is null";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, npeCauseMsg);
}
break;
}
case JBmonitorexit: {
if (NULL == npeCauseMsg) {
npeMsg = getMsgWithAllocation(vmThread, "Cannot exit synchronized block");
} else {
if (isMethodFlag) {
msgTemplate = "Cannot exit synchronized block because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot exit synchronized block because \"%s\" is null";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, npeCauseMsg);
}
break;
}
case JBgetfield: /* FALLTHROUGH */
case JBputfield: {
U_16 index = PARAM_16(bcCurrentPtr, 1);
UDATA cpType = J9_CP_TYPE(J9ROMCLASS_CPSHAPEDESCRIPTION(romClass), index);
Trc_VM_GetCompleteNPEMessage_Field_Index(vmThread, index);
if (J9CPTYPE_FIELD == cpType) {
J9ROMConstantPoolItem *constantPool = J9_ROM_CP_FROM_ROM_CLASS(romClass);
J9ROMConstantPoolItem *cpItem = constantPool + index;
J9ROMNameAndSignature *fieldNameAndSig = J9ROMFIELDREF_NAMEANDSIGNATURE((J9ROMFieldRef *)cpItem);
J9UTF8 *fieldName = J9ROMNAMEANDSIGNATURE_NAME(fieldNameAndSig);
if (NULL == npeCauseMsg) {
if (JBputfield == bcCurrent) {
msgTemplate = "Cannot assign field \"%.*s\"";
} else {
msgTemplate = "Cannot read field \"%.*s\"";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, J9UTF8_LENGTH(fieldName), J9UTF8_DATA(fieldName));
} else {
if (JBputfield == bcCurrent) {
if (isMethodFlag) {
msgTemplate = "Cannot assign field \"%.*s\" because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot assign field \"%.*s\" because \"%s\" is null";
}
} else {
if (isMethodFlag) {
msgTemplate = "Cannot read field \"%.*s\" because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot read field \"%.*s\" because \"%s\" is null";
}
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate, J9UTF8_LENGTH(fieldName), J9UTF8_DATA(fieldName), npeCauseMsg);
}
} else {
Trc_VM_GetCompleteNPEMessage_UnexpectedCPType(vmThread, cpType, bcCurrent);
}
break;
}
case JBinvokehandle: /* FALLTHROUGH */
case JBinvokehandlegeneric: /* FALLTHROUGH */
case JBinvokeinterface2: /* FALLTHROUGH */
case JBinvokeinterface: /* FALLTHROUGH */
case JBinvokespecial: /* FALLTHROUGH */
case JBinvokestatic: /* FALLTHROUGH */
case JBinvokevirtual: {
U_16 index = 0;
J9ROMConstantPoolItem *constantPool = J9_ROM_CP_FROM_ROM_CLASS(romClass);
if (JBinvokeinterface2 == bcCurrent) {
index = PARAM_16(bcCurrentPtr + 2, 1); /* get JBinvokeinterface instead */
} else {
index = PARAM_16(bcCurrentPtr, 1);
}
Trc_VM_GetCompleteNPEMessage_Invoke_Index(vmThread, index);
J9ROMMethodRef *romMethodRef = (J9ROMMethodRef *)&constantPool[index];
J9ROMNameAndSignature *methodNameAndSig = J9ROMMETHODREF_NAMEANDSIGNATURE(romMethodRef);
J9UTF8 *methodName = J9ROMNAMEANDSIGNATURE_NAME(methodNameAndSig);
J9UTF8 *definingClassFullQualifiedName = J9ROMSTRINGREF_UTF8DATA((J9ROMStringRef *)&constantPool[romMethodRef->classRefCPIndex]);
J9UTF8* methodSig = J9ROMNAMEANDSIGNATURE_SIGNATURE(methodNameAndSig);
bool npeMsgRequired = true;
#if JAVA_SPEC_VERSION >= 18
if (J9UTF8_LITERAL_EQUALS(J9UTF8_DATA(definingClassFullQualifiedName), J9UTF8_LENGTH(definingClassFullQualifiedName), "jdk/internal/reflect/DirectConstructorHandleAccessor")) {
if (J9UTF8_LITERAL_EQUALS(J9UTF8_DATA(methodName), J9UTF8_LENGTH(methodName), "invokeImpl")) {
/* JEP416 - jdk.internal.reflect.DirectConstructorHandleAccessor.invokeImpl() is invoked by DirectConstructorHandleAccessor.newInstance().
* No message generated for new NullPointerException().getMessage() or new NullPointerException(null).getMessage()
*/
npeMsgRequired = false;
Trc_VM_GetCompleteNPEMessage_Not_Required(vmThread);
}
} else
#endif /* JAVA_SPEC_VERSION >= 18 */
if (J9UTF8_LITERAL_EQUALS(J9UTF8_DATA(definingClassFullQualifiedName), J9UTF8_LENGTH(definingClassFullQualifiedName), "java/lang/NullPointerException")) {
if (J9UTF8_LITERAL_EQUALS(J9UTF8_DATA(methodName), J9UTF8_LENGTH(methodName), "<init>")) {
/* No message generated for new NullPointerException().getMessage() or new NullPointerException(null).getMessage() */
npeMsgRequired = false;
Trc_VM_GetCompleteNPEMessage_Not_Required(vmThread);
}
}
if (npeMsgRequired) {
char *fullyQualifiedClassName = convertToJavaFullyQualifiedName(vmThread, definingClassFullQualifiedName);
char *methodSigParameters = convertMethodSignature(vmThread, methodSig);
if (NULL == npeCauseMsg) {
npeMsg = getMsgWithAllocation(vmThread, "Cannot invoke \"%s.%.*s%s\"",
fullyQualifiedClassName, J9UTF8_LENGTH(methodName), J9UTF8_DATA(methodName), methodSigParameters);
} else {
if (isMethodFlag) {
msgTemplate = "Cannot invoke \"%s.%.*s%s\" because the return value of \"%s\" is null";
} else {
msgTemplate = "Cannot invoke \"%s.%.*s%s\" because \"%s\" is null";
}
npeMsg = getMsgWithAllocation(vmThread, msgTemplate,
fullyQualifiedClassName, J9UTF8_LENGTH(methodName), J9UTF8_DATA(methodName), methodSigParameters, npeCauseMsg);
}
j9mem_free_memory(fullyQualifiedClassName);
j9mem_free_memory(methodSigParameters);
}
break;
}
default:
Trc_VM_GetCompleteNPEMessage_MissedBytecode(vmThread, bcCurrent);
}
}
j9mem_free_memory(npeCauseMsg);
Trc_VM_GetCompleteNPEMessage_Exit(vmThread, npeMsg);
return npeMsg;
}
/*
* Initialize npeMsgData with incoming romMethod
* @param[in] npeMsgData - the J9NPEMessageData structure holding romClass/romMethod/npePC
*
* @return IDATA BCV_SUCCESS if it succeeded, BCV_ERR_INTERNAL_ERROR for any unexpected error
*/
static IDATA
initializeNPEMessageData(J9NPEMessageData *npeMsgData)
{
PORT_ACCESS_FROM_VMC(npeMsgData->vmThread);
J9ROMMethod *romMethod = npeMsgData->romMethod;
IDATA result = BCV_SUCCESS;
/* BCV_TARGET_STACK_HEADER_UDATA_SIZE for pc/stackBase/stackEnd in J9BranchTargetStack and
* BCV_STACK_OVERFLOW_BUFFER_UDATA_SIZE for late overflow detection of longs/doubles
*/
npeMsgData->stackSize = (J9_MAX_STACK_FROM_ROM_METHOD(romMethod)
+ J9_ARG_COUNT_FROM_ROM_METHOD(romMethod)
+ J9_TEMP_COUNT_FROM_ROM_METHOD(romMethod)
+ BCV_TARGET_STACK_HEADER_UDATA_SIZE
+ BCV_STACK_OVERFLOW_BUFFER_UDATA_SIZE) * sizeof(UDATA);
ALLOC_BUFFER(npeMsgData->liveStack, npeMsgData->stackSize, (J9BranchTargetStack *));
UDATA bytecodeSize = J9_BYTECODE_SIZE_FROM_ROM_METHOD(romMethod);
UDATA bytecodeBufferSize = bytecodeSize * sizeof(U_32) * 2;
ALLOC_BUFFER(npeMsgData->bytecodeOffset, bytecodeBufferSize, (J9BytecodeOffset *));
ALLOC_BUFFER(npeMsgData->bytecodeMap, bytecodeBufferSize, (U_32 *));
result = buildBranchMap(npeMsgData);
if (result >= 0) {
J9BranchTargetStack *liveStack = NULL;
if (result > 0) {
npeMsgData->stackMapsCount = result;
UDATA stackMapsSize = (npeMsgData->stackSize) * (npeMsgData->stackMapsCount);
ALLOC_BUFFER(npeMsgData->stackMaps, stackMapsSize, (UDATA *));
UDATA mapIndex = 0;
UDATA j = 0;
U_32 *bytecodeMap = npeMsgData->bytecodeMap;
liveStack = NPEMSG_FIRST_STACK ();
for (j = 0; j < bytecodeSize; j++) {
if ((bytecodeMap)[j] & BRANCH_TARGET) {
liveStack->pc = j; /* offset of the branch target */
liveStack->stackBaseIndex = -1;
liveStack->stackTopIndex = -1;
liveStack = NPEMSG_NEXT_STACK(liveStack);
bytecodeMap[j] |= (mapIndex << BRANCH_INDEX_SHIFT); /* adding the stack # */
mapIndex++;
}
}
UDATA unwalkedQueueSize = (npeMsgData->stackMapsCount + 1) * sizeof(UDATA);
ALLOC_BUFFER(npeMsgData->unwalkedQueue, unwalkedQueueSize, (UDATA *));
}
liveStack = (J9BranchTargetStack *)npeMsgData->liveStack;
UDATA *stackTop = &(liveStack->stackElements[0]);
initStackFromMethodSignature(npeMsgData->vmThread, romMethod, &stackTop);
SAVE_STACKTOP(liveStack, stackTop);
liveStack->stackBaseIndex = liveStack->stackTopIndex;
result = BCV_SUCCESS;
}
return result;
}
/*
* Initialize bytecodeMap with BRANCH_TARGET or BRANCH_EXCEPTION_START
* @param[in] npeMsgData - the J9NPEMessageData structure holding romClass/romMethod/npePC
*
* @return IDATA the number of branch targets and exception handler starts
* BCV_ERR_INTERNAL_ERROR for any unexpected error
*/
static IDATA
buildBranchMap(J9NPEMessageData *npeMsgData)
{
J9ROMMethod *romMethod = npeMsgData->romMethod;
U_32 *bytecodeMap = npeMsgData->bytecodeMap;
UDATA count = 0;
U_8 *bcStart = J9_BYTECODE_START_FROM_ROM_METHOD(romMethod);
U_8 *bcIndex = bcStart;
U_8 *bcEnd = bcStart + J9_BYTECODE_SIZE_FROM_ROM_METHOD(romMethod);
while (bcIndex < bcEnd) {
IDATA start = 0;
I_32 longBranch = 0;
UDATA bc = *bcIndex;
/* high nybble = branch action, low nybble = instruction size */
UDATA size = J9JavaInstructionSizeAndBranchActionTable[bc];
if (0 == size) {
return BCV_ERR_INTERNAL_ERROR;
}
switch (size >> 4) { /* branch action */
case 5: { /* switches */
start = bcIndex - bcStart;
IDATA pc = (start + 4) & ~3;
bcIndex = bcStart + pc;
longBranch = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
if (0 == bytecodeMap[start + longBranch]) {
bytecodeMap[start + longBranch] = BRANCH_TARGET;
count++;
}
UDATA npairs = 0;
IDATA pcs = 0;
IDATA low = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
if (JBtableswitch == bc) {
IDATA high = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
npairs = (UDATA) (high - low + 1);
pcs = 0;
} else {
npairs = (UDATA) low;
pcs = 4;
}
UDATA temp = 0;
for (temp = 0; temp < npairs; temp++) {
bcIndex += pcs;
longBranch = (I_32) PARAM_32(bcIndex, 0);
bcIndex += 4;
if (0 == bytecodeMap[start + longBranch]) {
bytecodeMap[start + longBranch] = BRANCH_TARGET;
count++;
}
}
continue;
}
case 2: /* gotos */
if (JBgotow == bc) {
start = bcIndex - bcStart;
longBranch = (I_32) PARAM_32(bcIndex, 1);
if (0 == bytecodeMap[start + longBranch]) {
bytecodeMap[start + longBranch] = BRANCH_TARGET;
count++;
}
break;
} /* fall through for JBgoto */
case 1: /* ifs */
I_16 shortBranch = (I_16) PARAM_16(bcIndex, 1);
start = bcIndex - bcStart;
if (0 == bytecodeMap[start + shortBranch]) {
bytecodeMap[start + shortBranch] = BRANCH_TARGET;
count++;
}
break;
}
bcIndex += size & 7;
}
/* need to walk exceptions as well, since they are branch targets */
if (romMethod->modifiers & J9AccMethodHasExceptionInfo) {
J9ExceptionInfo *exceptionData = J9_EXCEPTION_DATA_FROM_ROM_METHOD(romMethod);
J9ExceptionHandler *handler = NULL;
if (exceptionData->catchCount) {
UDATA temp = 0;
IDATA pc = 0;
IDATA pcs = 0;
handler = J9EXCEPTIONINFO_HANDLERS(exceptionData);
for (temp = 0; temp < (U_32) exceptionData->catchCount; temp++) {
pc = (IDATA) handler->startPC;
pcs = (IDATA) handler->handlerPC;
/* Avoid re-walking a handler that handles itself */
if (pc != pcs) {
bytecodeMap[pc] |= BRANCH_EXCEPTION_START;
}
if (0 == (bytecodeMap[pcs] & BRANCH_TARGET)) {
bytecodeMap[pcs] |= BRANCH_TARGET;
count++;
}
handler++;
}
}
}
return count;
}
/**
* Compute NPE message at npePC supplied. This can be invoked recursively.
*
* @param[in] vmThread The current J9VMThread
* @param[in] bcCurrentPtr The pointer to the bytecode being executed and caused the NPE
* @param[in] romMethod The romMethod of the bytecode
* @param[in] romClass The romClass of the bytecode
* @param[in] npePC The bytecode offset where the NPE message is to be generated
* @param[in] npeFinalFlag The flag indicates if getCompleteNPEMessage() is invoked
* @param[in,out] npeMsg An extended NPE message generated so far
* @param[in,out] isMethodFlag The flag indicates if "the return value of" should be added into the message
* @param[in] temps The location of bytecode temps supplied via liveStack->stackElements
* @param[in] bytecodeOffset The array storing the bytecode offset
* @param[in] bytecodeOffset The cause of NPE
*/
static void
computeNPEMsgAtPC(J9VMThread *vmThread, J9ROMMethod *romMethod, J9ROMClass *romClass, UDATA npePC,
bool npeFinalFlag, char **npeMsg, bool *isMethodFlag, UDATA *temps, J9BytecodeOffset *bytecodeOffset)
{
Trc_VM_ComputeNPEMsgAtPC_Entry(vmThread, romClass, romMethod, temps, bytecodeOffset, npePC, npeFinalFlag, *isMethodFlag, *npeMsg);
PORT_ACCESS_FROM_VMC(vmThread);
if (NULL != *npeMsg) {
j9mem_free_memory(*npeMsg);
*npeMsg = NULL;
}
if (BYTECODE_TEMP_CHANGED == npePC) {
/* *npeMsg is NULL */
} else {
J9ROMConstantPoolItem *constantPool = J9_ROM_CP_FROM_ROM_CLASS(romClass);
U_8 *code = J9_BYTECODE_START_FROM_ROM_METHOD(romMethod);
U_8 *bcCurrentPtr = code + npePC;
U_8 bcCurrent = *bcCurrentPtr;
Trc_VM_ComputeNPEMsgAtPC_start(vmThread, romClass, romMethod, temps, bytecodeOffset, bcCurrent, npePC, npeFinalFlag, *isMethodFlag, *npeMsg);
if ((bcCurrent >= JBiconstm1) && (bcCurrent <= JBdconst1)) {
/*
* JBiconstm1, JBiconst0, JBiconst1, JBiconst2, JBiconst3, JBiconst4, JBiconst5
* JBlconst0, JBlconst1
* JBfconst0, JBfconst1, JBfconst2
* JBdconst0, JBdconst1
*/
I_8 constNum = 0;
bool missedBCFlag = false;
if (JBiconstm1 == bcCurrent) {
constNum = -1;
} else {
if ((bcCurrent >= JBiconst0) && (bcCurrent <= JBiconst5)) {
constNum = bcCurrent - 3; /* iconst_0 = 3 (0x3) */
} else if ((JBlconst0 == bcCurrent) || (JBfconst0 == bcCurrent) || (JBdconst0 == bcCurrent)) {
constNum = 0;
} else if ((JBlconst1 == bcCurrent) || (JBfconst1 == bcCurrent) || (JBdconst1 == bcCurrent)) {
constNum = 1;
} else if (JBfconst2 == bcCurrent) {
constNum = 2;
} else {
missedBCFlag = true;
Trc_VM_ComputeNPEMsgAtPC_Constants_UnexpectedBC(vmThread, bcCurrent);
}
}
if (!missedBCFlag) {
*npeMsg = getMsgWithAllocation(vmThread, "%d", constNum);
}
} else if (((bcCurrent >= JBiadd) && (bcCurrent <= JBlxor))
|| ((bcCurrent >= JBlcmp) && (bcCurrent <= JBdcmpg))
) {
/* JBiadd, JBladd, JBfadd, JBdadd, JBisub, JBlsub, JBfsub, JBdsub, JBimul, JBlmul, JBfmul, JBdmul
* JBidiv, JBldiv, JBfdiv, JBddiv, JBirem, JBlrem, JBfrem, JBdrem, JBineg, JBlneg, JBfneg, JBdneg
* JBishl, JBlshl, JBishr, JBlshr, JBiushr, JBlushr, JBiand, JBland, JBior, JBlor, JBixor, JBlxor
*
* JBlcmp, JBfcmpl, JBfcmpg, JBdcmpl, JBdcmpg
*/
*npeMsg = getMsgWithAllocation(vmThread, "...");
} else {
switch (bcCurrent) {
case JBaconstnull:
*npeMsg = getMsgWithAllocation(vmThread, "null");
break;
case JBbipush:
*npeMsg = getMsgWithAllocation(vmThread, "%lu", *(bcCurrentPtr + 1));
break;
case JBsipush: {
UDATA sipushIndex = 0;
U_8 *tmpbcPtr = bcCurrentPtr + 1;
GETNEXT_U16(sipushIndex, tmpbcPtr);
*npeMsg = getMsgWithAllocation(vmThread, "%lu", sipushIndex);
break;
}
case JBldc: /* Fall through case !!! */
case JBldcw: /* Fall through case !!! */
case JBldc2dw: /* Fall through case !!! */
case JBldc2lw: {
UDATA ldcIndex = 0;
U_8 *tmpbcPtr = bcCurrentPtr + 1;
if (JBldc == bcCurrent) {
GETNEXT_U8(ldcIndex, tmpbcPtr);
} else if (JBldcw == bcCurrent) {
GETNEXT_U16(ldcIndex, tmpbcPtr);
} else {
/* bcCurrent is JBldc2lw or JBldc2dw */
GETNEXT_U8(ldcIndex, tmpbcPtr);
}
J9ROMConstantPoolItem *info = &constantPool[ldcIndex];
if (BCT_J9DescriptionCpTypeScalar == ((J9ROMSingleSlotConstantRef *) info)->cpType) {
/* this is a float/int constant */
ldcIndex = ((J9ROMSingleSlotConstantRef *) info)->data;
*npeMsg = getMsgWithAllocation(vmThread, "%lu", ldcIndex);
} else {
Trc_VM_ComputeNPEMsgAtPC_NotScalarType(vmThread, romClass, romMethod, constantPool, ldcIndex, info, ((J9ROMSingleSlotConstantRef *) info)->cpType);
}
break;
}
case JBanewarray: {
UDATA index = PARAM_16(bcCurrentPtr, 1);
J9ROMConstantPoolItem *info = &constantPool[index];
J9UTF8 *className = J9ROMSTRINGREF_UTF8DATA((J9ROMStringRef *) info);
char *fullyQualifiedClassName = convertToJavaFullyQualifiedName(vmThread, className);
*npeMsg = getMsgWithAllocation(vmThread, fullyQualifiedClassName);
j9mem_free_memory(fullyQualifiedClassName);
break;
}
case JBinvokeinterface2: /* Fall through case !!! */
case JBinvokevirtual: /* Fall through case !!! */
case JBinvokespecial: /* Fall through case !!! */
case JBinvokeinterface: /* Fall through case !!! */
case JBinvokestatic: {
if (npeFinalFlag) {
UDATA objectrefPos = bytecodeOffset[npePC].first;
if (BYTECODE_BRANCH_TARGET == objectrefPos) {
/* *npeMsg is NULL */
} else {
computeNPEMsgAtPC(vmThread, romMethod, romClass, objectrefPos, false, npeMsg, isMethodFlag, temps, bytecodeOffset);
}
} else {
U_8 *bcPtrTemp = (JBinvokeinterface2 == bcCurrent) ? bcCurrentPtr + 2 : bcCurrentPtr;
char *methodName = getFullyQualifiedMethodName(vmThread, romClass, bcPtrTemp);
*npeMsg = getMsgWithAllocation(vmThread, methodName);
j9mem_free_memory(methodName);
*isMethodFlag = true;
}
break;
}
case JBiload0: /* Fall through case !!! */
case JBiload1: /* Fall through case !!! */
case JBiload2: /* Fall through case !!! */
case JBiload3: /* Fall through case !!! */
case JBlload0: /* Fall through case !!! */
case JBlload1: /* Fall through case !!! */
case JBlload2: /* Fall through case !!! */
case JBlload3: /* Fall through case !!! */
case JBfload0: /* Fall through case !!! */
case JBfload1: /* Fall through case !!! */
case JBfload2: /* Fall through case !!! */
case JBfload3: /* Fall through case !!! */
case JBdload0: /* Fall through case !!! */
case JBdload1: /* Fall through case !!! */
case JBdload2: /* Fall through case !!! */
case JBdload3: /* Fall through case !!! */
case JBaload0: /* Fall through case !!! */
case JBaload1: /* Fall through case !!! */
case JBaload2: /* Fall through case !!! */
case JBaload3: /* Fall through case !!! */
case JBaload0getfield: {
U_16 localVar = 0;
if (bcCurrent >= JBiload0 && bcCurrent <= JBiload3) {
localVar = bcCurrent - JBiload0;
} else if (bcCurrent >= JBlload0 && bcCurrent <= JBlload3) {
localVar = bcCurrent - JBlload0;
} else if (bcCurrent >= JBfload0 && bcCurrent <= JBfload3) {
localVar = bcCurrent - JBfload0;
} else if (bcCurrent >= JBdload0 && bcCurrent <= JBdload3) {
localVar = bcCurrent - JBdload0;
} else if (bcCurrent >= JBaload0 && bcCurrent <= JBaload3) {
localVar = bcCurrent - JBaload0;
} else if (JBaload0getfield == bcCurrent) {
localVar = 0;
}
*npeMsg = getLocalsName(vmThread, romMethod, localVar, npePC, temps);
break;
}
case JBiload: /* Fall through case !!! */
case JBlload: /* Fall through case !!! */
case JBfload: /* Fall through case !!! */
case JBdload: /* Fall through case !!! */
case JBaload: {
/* The index is an unsigned byte that must be an index into the
* local variable array of the current frame (section 2.6).
* The local variable at index must contain a reference.
*/
*npeMsg = getLocalsName(vmThread, romMethod, *(bcCurrentPtr + 1), npePC, temps);
break;
}
case JBiloadw: /* Fall through case !!! */
case JBlloadw: /* Fall through case !!! */
case JBfloadw: /* Fall through case !!! */
case JBdloadw: /* Fall through case !!! */
case JBaloadw: {
U_16 localVar = 0;
U_8 *tmpbcPtr = bcCurrentPtr + 1;
GETNEXT_U16(localVar, tmpbcPtr);
*npeMsg = getLocalsName(vmThread, romMethod, localVar, npePC, temps);
break;
}
case JBi2l: /* Fall through case !!! */
case JBi2f: /* Fall through case !!! */
case JBi2d: /* Fall through case !!! */
case JBl2i: /* Fall through case !!! */
case JBl2f: /* Fall through case !!! */
case JBl2d: /* Fall through case !!! */
case JBf2i: /* Fall through case !!! */
case JBf2l: /* Fall through case !!! */
case JBf2d: /* Fall through case !!! */
case JBd2i: /* Fall through case !!! */
case JBd2l: /* Fall through case !!! */