-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Expand file tree
/
Copy pathjniCheck.cpp
More file actions
2374 lines (2131 loc) · 80.4 KB
/
Copy pathjniCheck.cpp
File metadata and controls
2374 lines (2131 loc) · 80.4 KB
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 (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "classfile/javaClasses.inline.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "jni.h"
#include "jvm.h"
#include "logging/log.hpp"
#include "logging/logTag.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/guardedMemory.hpp"
#include "memory/resourceArea.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/oop.inline.hpp"
#include "oops/symbol.hpp"
#include "prims/jniCheck.hpp"
#include "prims/jvm_misc.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/javaThread.hpp"
#include "runtime/jfieldIDWorkaround.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "utilities/formatBuffer.hpp"
#include "utilities/utf8.hpp"
// Heap objects are allowed to be directly referenced only in VM code,
// not in native code.
#define ASSERT_OOPS_ALLOWED \
assert(JavaThread::current()->thread_state() == _thread_in_vm, \
"jniCheck examining oops in bad state.")
// Execute the given block of source code with the thread in VM state.
// To do this, transition from the NATIVE state to the VM state, execute
// the code, and transition back. The ThreadInVMfromNative constructor
// performs the transition to VM state, its destructor restores the
// NATIVE state.
#define IN_VM(source_code) { \
{ \
ThreadInVMfromNative __tiv(thr); \
source_code \
} \
}
/*
* DECLARATIONS
*/
static struct JNINativeInterface_ * unchecked_jni_NativeInterface;
/*
* MACRO DEFINITIONS
*/
// All JNI checked functions here use JNI_ENTRY_CHECKED() instead of the
// QUICK_ENTRY or LEAF variants found in jni.cpp. This allows handles
// to be created if a fatal error should occur.
// Check for thread not attached to VM; need to catch this before
// assertions in the wrapper routines might fire
// Check for env being the one value appropriate for this thread.
#define JNI_ENTRY_CHECKED(result_type, header) \
extern "C" { \
result_type JNICALL header { \
Thread* cur = Thread::current_or_null(); \
if (cur == nullptr || !cur->is_Java_thread()) { \
tty->print_cr("%s", fatal_using_jnienv_in_nonjava); \
os::abort(true); \
} \
JavaThread* thr = JavaThread::cast(cur); \
JNIEnv* xenv = thr->jni_environment(); \
if (env != xenv) { \
NativeReportJNIFatalError(thr, warn_wrong_jnienv); \
} \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thr)); \
VM_ENTRY_BASE(result_type, header, thr)
#define UNCHECKED() (unchecked_jni_NativeInterface)
static const char * warn_wrong_jnienv = "Using JNIEnv in the wrong thread";
static const char * warn_bad_class_descriptor1 = "JNI FindClass received a bad class descriptor \"";
static const char * warn_bad_class_descriptor2 = "\". A correct class descriptor " \
"has no leading \"L\" or trailing \";\". Incorrect descriptors will not be accepted in future releases.";
static const char * fatal_using_jnienv_in_nonjava = "FATAL ERROR in native method: Using JNIEnv in non-Java thread";
static const char * warn_other_function_in_critical = "Warning: Calling other JNI functions in the scope of " \
"Get/ReleasePrimitiveArrayCritical or Get/ReleaseStringCritical";
static const char * fatal_bad_ref_to_jni = "Bad global or local ref passed to JNI";
static const char * fatal_received_null_class = "JNI received a null class";
static const char * fatal_class_not_a_class = "JNI received a class argument that is not a class";
static const char * fatal_class_not_a_throwable_class = "JNI Throw or ThrowNew received a class argument that is not a Throwable or Throwable subclass";
static const char * fatal_wrong_class_or_method = "Wrong object class or methodID passed to JNI call";
static const char * fatal_non_weak_method = "non-weak methodID passed to JNI call";
static const char * fatal_unknown_array_object = "Unknown array object passed to JNI array operations";
static const char * fatal_object_array_expected = "Object array expected but not received for JNI array operation";
static const char * fatal_prim_type_array_expected = "Primitive type array expected but not received for JNI array operation";
static const char * fatal_non_array = "Non-array passed to JNI array operations";
static const char * fatal_element_type_mismatch = "Array element type mismatch in JNI";
static const char * fatal_should_be_static = "Non-static field ID passed to JNI";
static const char * fatal_wrong_static_field = "Wrong static field ID passed to JNI";
static const char * fatal_static_field_not_found = "Static field not found in JNI get/set field operations";
static const char * fatal_static_field_mismatch = "Field type (static) mismatch in JNI get/set field operations";
static const char * fatal_should_be_nonstatic = "Static field ID passed to JNI";
static const char * fatal_null_object = "Null object passed to JNI";
static const char * fatal_wrong_field = "Wrong field ID passed to JNI";
static const char * fatal_instance_field_not_found = "Instance field not found in JNI get/set field operations";
static const char * fatal_instance_field_mismatch = "Field type (instance) mismatch in JNI get/set field operations";
static const char * fatal_non_string = "JNI string operation received a non-string";
static const char * fatal_non_utf8_class_name1 = "JNI class name is not a valid UTF8 string \"";
static const char * fatal_non_utf8_class_name2 = "\"";
// When in VM state:
static void ReportJNIWarning(JavaThread* thr, const char *msg) {
tty->print_cr("WARNING in native method: %s", msg);
thr->print_jni_stack();
}
// When in NATIVE state:
static void NativeReportJNIFatalError(JavaThread* thr, const char *msg) {
IN_VM(
ReportJNIFatalError(thr, msg);
)
}
static void NativeReportJNIWarning(JavaThread* thr, const char *msg) {
IN_VM(
ReportJNIWarning(thr, msg);
)
}
/*
* SUPPORT FUNCTIONS
*/
/**
* Check whether or not a programmer has actually checked for exceptions. According
* to the JNI Specification ("jni/spec/design.html#java_exceptions"):
*
* There are two cases where the programmer needs to check for exceptions without
* being able to first check an error code:
*
* - The JNI functions that invoke a Java method return the result of the Java method.
* The programmer must call ExceptionOccurred() to check for possible exceptions
* that occurred during the execution of the Java method.
*
* - Some of the JNI array access functions do not return an error code, but may
* throw an ArrayIndexOutOfBoundsException or ArrayStoreException.
*
* In all other cases, a non-error return value guarantees that no exceptions have been thrown.
*
* Programmers often defend against ArrayIndexOutOfBoundsException, so warning
* for these functions would be pedantic.
*/
static inline void
check_pending_exception(JavaThread* thr) {
if (thr->has_pending_exception()) {
NativeReportJNIWarning(thr, "JNI call made with exception pending");
}
if (thr->is_pending_jni_exception_check()) {
IN_VM(
tty->print_cr("WARNING in native method: JNI call made without checking exceptions when required to from %s",
thr->get_pending_jni_exception_check());
thr->print_jni_stack();
)
thr->clear_pending_jni_exception_check(); // Just complain once
}
}
static inline void
functionEnterCritical(JavaThread* thr)
{
check_pending_exception(thr);
}
static inline void
functionEnterCriticalExceptionAllowed(JavaThread* thr)
{
}
static inline void
functionEnter(JavaThread* thr)
{
if (thr->in_critical()) {
tty->print_cr("%s", warn_other_function_in_critical);
}
check_pending_exception(thr);
}
static inline void
functionEnterExceptionAllowed(JavaThread* thr)
{
if (thr->in_critical()) {
tty->print_cr("%s", warn_other_function_in_critical);
}
}
static inline void
functionExit(JavaThread* thr)
{
// No checks at this time
}
static inline void
checkStaticFieldID(JavaThread* thr, jfieldID fid, jclass cls, int ftype, bool setter)
{
fieldDescriptor fd;
/* make sure it is a static field */
if (!jfieldIDWorkaround::is_static_jfieldID(fid))
ReportJNIFatalError(thr, fatal_should_be_static);
/* validate the class being passed */
ASSERT_OOPS_ALLOWED;
Klass* k_oop = jniCheck::validate_class(thr, cls, false);
/* check for proper subclass hierarchy */
JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fid);
Klass* f_oop = id->holder();
if (!k_oop->is_subtype_of(f_oop))
ReportJNIFatalError(thr, fatal_wrong_static_field);
/* check for proper field type */
if (!id->find_local_field(&fd))
ReportJNIFatalError(thr, fatal_static_field_not_found);
if ((fd.field_type() != ftype) &&
!(fd.field_type() == T_ARRAY && ftype == T_OBJECT)) {
ReportJNIFatalError(thr, fatal_static_field_mismatch);
}
/* check if setting a final field */
if (setter && fd.is_final() && !fd.is_mutable_static_final()) {
ResourceMark rm(thr);
stringStream ss;
ss.print("SetStatic<Type>Field called to mutate final static field %s.%s", k_oop->external_name(), fd.name()->as_C_string());
ReportJNIWarning(thr, ss.as_string());
}
}
static inline void
checkInstanceFieldID(JavaThread* thr, jfieldID fid, jobject obj, int ftype, bool setter)
{
fieldDescriptor fd;
/* make sure it is an instance field */
if (jfieldIDWorkaround::is_static_jfieldID(fid))
ReportJNIFatalError(thr, fatal_should_be_nonstatic);
/* validate the object being passed and then get its class */
ASSERT_OOPS_ALLOWED;
oop oopObj = jniCheck::validate_object(thr, obj);
if (oopObj == nullptr) {
ReportJNIFatalError(thr, fatal_null_object);
}
Klass* k_oop = oopObj->klass();
if (!jfieldIDWorkaround::is_valid_jfieldID(k_oop, fid)) {
ReportJNIFatalError(thr, fatal_wrong_field);
}
/* make sure the field exists */
int offset = jfieldIDWorkaround::from_instance_jfieldID(k_oop, fid);
if (!InstanceKlass::cast(k_oop)->contains_field_offset(offset))
ReportJNIFatalError(thr, fatal_wrong_field);
/* check for proper field type */
if (!InstanceKlass::cast(k_oop)->find_field_from_offset(offset, false, &fd))
ReportJNIFatalError(thr, fatal_instance_field_not_found);
if ((fd.field_type() != ftype) &&
!(fd.field_type() == T_ARRAY && ftype == T_OBJECT)) {
ReportJNIFatalError(thr, fatal_instance_field_mismatch);
}
/* check if setting a final field */
if (setter && fd.is_final()) {
ResourceMark rm(thr);
stringStream ss;
ss.print("Set<Type>Field called to mutate final instance field %s.%s", k_oop->external_name(), fd.name()->as_C_string());
ReportJNIWarning(thr, ss.as_string());
}
}
static inline void
checkString(JavaThread* thr, jstring js)
{
ASSERT_OOPS_ALLOWED;
oop s = jniCheck::validate_object(thr, js);
if ((s == nullptr) || !java_lang_String::is_instance(s))
ReportJNIFatalError(thr, fatal_non_string);
}
static inline arrayOop
check_is_array(JavaThread* thr, jarray jArray)
{
ASSERT_OOPS_ALLOWED;
arrayOop aOop;
aOop = (arrayOop)jniCheck::validate_object(thr, jArray);
if (aOop == nullptr || !aOop->is_array()) {
ReportJNIFatalError(thr, fatal_non_array);
}
return aOop;
}
static inline arrayOop
check_is_primitive_array(JavaThread* thr, jarray jArray) {
arrayOop aOop = check_is_array(thr, jArray);
if (!aOop->is_typeArray()) {
ReportJNIFatalError(thr, fatal_prim_type_array_expected);
}
return aOop;
}
static inline void
check_primitive_array_type(JavaThread* thr, jarray jArray, BasicType elementType)
{
BasicType array_type;
arrayOop aOop;
aOop = check_is_primitive_array(thr, jArray);
array_type = TypeArrayKlass::cast(aOop->klass())->element_type();
if (array_type != elementType) {
ReportJNIFatalError(thr, fatal_element_type_mismatch);
}
}
static inline void
check_is_obj_array(JavaThread* thr, jarray jArray) {
arrayOop aOop = check_is_array(thr, jArray);
if (!aOop->is_objArray()) {
ReportJNIFatalError(thr, fatal_object_array_expected);
}
}
// Arbitrary (but well-known) tag for GetStringChars
const void* STRING_TAG = (void*)0x47114711;
// Arbitrary (but well-known) tag for GetStringUTFChars
const void* STRING_UTF_TAG = (void*) 0x48124812;
// Arbitrary (but well-known) tag for GetPrimitiveArrayCritical
const void* CRITICAL_TAG = (void*)0x49134913;
/*
* Copy and wrap array elements for bounds checking.
* Remember the original elements (GuardedMemory::get_tag())
*/
static void* check_jni_wrap_copy_array(JavaThread* thr, jarray array,
void* orig_elements, jboolean is_critical = JNI_FALSE) {
void* result;
IN_VM(
oop a = JNIHandles::resolve_non_null(array);
size_t len = arrayOop(a)->length() <<
TypeArrayKlass::cast(a->klass())->log2_element_size();
result = GuardedMemory::wrap_copy(orig_elements, len, orig_elements, is_critical ? CRITICAL_TAG : nullptr);
)
return result;
}
static void* check_wrapped_array(JavaThread* thr, const char* fn_name,
void* obj, void* carray, size_t* rsz, jboolean is_critical) {
if (carray == nullptr) {
tty->print_cr("%s: elements vector null" PTR_FORMAT, fn_name, p2i(obj));
NativeReportJNIFatalError(thr, "Elements vector null");
}
GuardedMemory guarded(carray);
void* orig_result = guarded.get_tag();
if (!guarded.verify_guards()) {
tty->print_cr("%s: release array failed bounds check, incorrect pointer returned ? array: "
PTR_FORMAT " carray: " PTR_FORMAT, fn_name, p2i(obj), p2i(carray));
DEBUG_ONLY(guarded.print_on(tty);) // This may crash.
NativeReportJNIFatalError(thr, err_msg("%s: failed bounds check", fn_name));
}
if (orig_result == nullptr) {
tty->print_cr("%s: unrecognized elements. array: " PTR_FORMAT " carray: " PTR_FORMAT,
fn_name, p2i(obj), p2i(carray));
DEBUG_ONLY(guarded.print_on(tty);) // This may crash.
NativeReportJNIFatalError(thr, err_msg("%s: unrecognized elements", fn_name));
}
if (orig_result == STRING_TAG || orig_result == STRING_UTF_TAG) {
bool was_utf = orig_result == STRING_UTF_TAG;
tty->print_cr("%s: called on something allocated by %s",
fn_name, was_utf ? "GetStringUTFChars" : "GetStringChars");
DEBUG_ONLY(guarded.print_on(tty);) // This may crash.
NativeReportJNIFatalError(thr, err_msg("%s called on something allocated by %s",
fn_name, was_utf ? "GetStringUTFChars" : "GetStringChars"));
}
if (is_critical && (guarded.get_tag2() != CRITICAL_TAG)) {
tty->print_cr("%s: called on something not allocated by GetPrimitiveArrayCritical", fn_name);
DEBUG_ONLY(guarded.print_on(tty);) // This may crash.
NativeReportJNIFatalError(thr, err_msg("%s called on something not allocated by GetPrimitiveArrayCritical",
fn_name));
}
if (!is_critical && (guarded.get_tag2() == CRITICAL_TAG)) {
tty->print_cr("%s: called on something allocated by GetPrimitiveArrayCritical", fn_name);
DEBUG_ONLY(guarded.print_on(tty);) // This may crash.
NativeReportJNIFatalError(thr, err_msg("%s called on something allocated by GetPrimitiveArrayCritical",
fn_name));
}
if (rsz != nullptr) {
*rsz = guarded.get_user_size();
}
return orig_result;
}
static void* check_wrapped_array_release(JavaThread* thr, const char* fn_name,
void* obj, void* carray, jint mode, jboolean is_critical) {
size_t sz;
void* orig_result = check_wrapped_array(thr, fn_name, obj, carray, &sz, is_critical);
switch (mode) {
case 0:
memcpy(orig_result, carray, sz);
GuardedMemory::free_copy(carray);
break;
case JNI_COMMIT:
memcpy(orig_result, carray, sz);
if (is_critical) {
// For ReleasePrimitiveArrayCritical we must free the internal buffer
// allocated through GuardedMemory.
GuardedMemory::free_copy(carray);
}
break;
case JNI_ABORT:
GuardedMemory::free_copy(carray);
break;
default:
tty->print_cr("%s: Unrecognized mode %i releasing array "
PTR_FORMAT " elements " PTR_FORMAT, fn_name, mode, p2i(obj), p2i(carray));
NativeReportJNIFatalError(thr, "Unrecognized array release mode");
}
return orig_result;
}
oop jniCheck::validate_handle(JavaThread* thr, jobject obj) {
if ((obj != nullptr) && (JNIHandles::handle_type(thr, obj) != JNIInvalidRefType)) {
ASSERT_OOPS_ALLOWED;
return JNIHandles::resolve_external_guard(obj);
}
ReportJNIFatalError(thr, fatal_bad_ref_to_jni);
return nullptr;
}
Method* jniCheck::validate_jmethod_id(JavaThread* thr, jmethodID method_id) {
ASSERT_OOPS_ALLOWED;
// do the fast jmethodID check first
Method* m = Method::checked_resolve_jmethod_id(method_id);
if (m == nullptr) {
ReportJNIFatalError(thr, fatal_wrong_class_or_method);
}
// jmethodIDs are handles in the class loader data,
// but that can be expensive so check it last
else if (!Method::validate_jmethod_id(method_id)) {
ReportJNIFatalError(thr, fatal_non_weak_method);
}
return m;
}
oop jniCheck::validate_object(JavaThread* thr, jobject obj) {
if (obj == nullptr) return nullptr;
ASSERT_OOPS_ALLOWED;
oop oopObj = jniCheck::validate_handle(thr, obj);
if (oopObj == nullptr) {
ReportJNIFatalError(thr, fatal_bad_ref_to_jni);
}
return oopObj;
}
// Warn if a class descriptor is in decorated form; class descriptors
// passed to JNI findClass should not be decorated unless they are
// array descriptors.
void jniCheck::validate_class_descriptor(JavaThread* thr, const char* name) {
if (name == nullptr) return; // implementation accepts null so just return
size_t len = strlen(name);
if (len >= 2 &&
name[0] == JVM_SIGNATURE_CLASS && // 'L'
name[len-1] == JVM_SIGNATURE_ENDCLASS ) { // ';'
char msg[JVM_MAXPATHLEN];
jio_snprintf(msg, JVM_MAXPATHLEN, "%s%s%s",
warn_bad_class_descriptor1, name, warn_bad_class_descriptor2);
ReportJNIWarning(thr, msg);
}
// Verify that the class name given is a valid utf8 string
if (!UTF8::is_legal_utf8((const unsigned char*)name, strlen(name), false)) {
char msg[JVM_MAXPATHLEN];
jio_snprintf(msg, JVM_MAXPATHLEN, "%s%s%s", fatal_non_utf8_class_name1, name, fatal_non_utf8_class_name2);
ReportJNIFatalError(thr, msg);
}
}
Klass* jniCheck::validate_class(JavaThread* thr, jclass clazz, bool allow_primitive) {
ASSERT_OOPS_ALLOWED;
oop mirror = jniCheck::validate_handle(thr, clazz);
if (mirror == nullptr) {
ReportJNIFatalError(thr, fatal_received_null_class);
}
if (mirror->klass() != vmClasses::Class_klass()) {
ReportJNIFatalError(thr, fatal_class_not_a_class);
}
Klass* k = java_lang_Class::as_Klass(mirror);
// Make allowances for primitive classes ...
if (!(k != nullptr || (allow_primitive && java_lang_Class::is_primitive(mirror)))) {
ReportJNIFatalError(thr, fatal_class_not_a_class);
}
return k;
}
void jniCheck::validate_throwable_klass(JavaThread* thr, Klass* klass) {
ASSERT_OOPS_ALLOWED;
assert(klass != nullptr, "klass argument must have a value");
if (!klass->is_instance_klass() ||
!klass->is_subclass_of(vmClasses::Throwable_klass())) {
ReportJNIFatalError(thr, fatal_class_not_a_throwable_class);
}
}
void jniCheck::validate_call(JavaThread* thr, jclass clazz, jmethodID method_id, jobject obj) {
ASSERT_OOPS_ALLOWED;
Method* m = jniCheck::validate_jmethod_id(thr, method_id);
InstanceKlass* holder = m->method_holder();
if (clazz != nullptr) {
Klass* k = jniCheck::validate_class(thr, clazz, false);
// Check that method is in the class, must be InstanceKlass
if (!InstanceKlass::cast(k)->is_subtype_of(holder)) {
ReportJNIFatalError(thr, fatal_wrong_class_or_method);
}
}
if (obj != nullptr) {
oop recv = jniCheck::validate_object(thr, obj);
assert(recv != nullptr, "validate_object checks that");
Klass* rk = recv->klass();
// Check that the object is a subtype of method holder too.
if (!rk->is_subtype_of(holder)) {
ReportJNIFatalError(thr, fatal_wrong_class_or_method);
}
}
}
/*
* IMPLEMENTATION OF FUNCTIONS IN CHECKED TABLE
*/
JNI_ENTRY_CHECKED(jclass,
checked_jni_DefineClass(JNIEnv *env,
const char *name,
jobject loader,
const jbyte *buf,
jsize len))
functionEnter(thr);
IN_VM(
jniCheck::validate_object(thr, loader);
)
jclass result = UNCHECKED()->DefineClass(env, name, loader, buf, len);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jclass,
checked_jni_FindClass(JNIEnv *env,
const char *name))
functionEnter(thr);
IN_VM(
jniCheck::validate_class_descriptor(thr, name);
)
jclass result = UNCHECKED()->FindClass(env, name);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jmethodID,
checked_jni_FromReflectedMethod(JNIEnv *env,
jobject method))
functionEnter(thr);
IN_VM(
jniCheck::validate_object(thr, method);
)
jmethodID result = UNCHECKED()->FromReflectedMethod(env, method);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jfieldID,
checked_jni_FromReflectedField(JNIEnv *env,
jobject field))
functionEnter(thr);
IN_VM(
jniCheck::validate_object(thr, field);
)
jfieldID result = UNCHECKED()->FromReflectedField(env, field);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_ToReflectedMethod(JNIEnv *env,
jclass cls,
jmethodID methodID,
jboolean isStatic))
functionEnter(thr);
IN_VM(
jniCheck::validate_call(thr, cls, methodID);
)
jobject result = UNCHECKED()->ToReflectedMethod(env, cls, methodID,
isStatic);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jclass,
checked_jni_GetSuperclass(JNIEnv *env,
jclass sub))
functionEnter(thr);
IN_VM(
jniCheck::validate_class(thr, sub, true);
)
jclass result = UNCHECKED()->GetSuperclass(env, sub);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jboolean,
checked_jni_IsAssignableFrom(JNIEnv *env,
jclass sub,
jclass sup))
functionEnter(thr);
IN_VM(
jniCheck::validate_class(thr, sub, true);
jniCheck::validate_class(thr, sup, true);
)
jboolean result = UNCHECKED()->IsAssignableFrom(env, sub, sup);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_ToReflectedField(JNIEnv *env,
jclass cls,
jfieldID fieldID,
jboolean isStatic))
functionEnter(thr);
IN_VM(
jniCheck::validate_class(thr, cls, false);
)
jobject result = UNCHECKED()->ToReflectedField(env, cls, fieldID,
isStatic);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jint,
checked_jni_Throw(JNIEnv *env,
jthrowable obj))
functionEnter(thr);
IN_VM(
oop oopObj = jniCheck::validate_object(thr, obj);
if (oopObj == nullptr) {
// Unchecked Throw tolerates a null obj, so just warn
ReportJNIWarning(thr, "JNI Throw called with null throwable");
} else {
jniCheck::validate_throwable_klass(thr, oopObj->klass());
}
)
jint result = UNCHECKED()->Throw(env, obj);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jint,
checked_jni_ThrowNew(JNIEnv *env,
jclass clazz,
const char *msg))
functionEnter(thr);
IN_VM(
Klass* k = jniCheck::validate_class(thr, clazz, false);
assert(k != nullptr, "validate_class shouldn't return null Klass*");
jniCheck::validate_throwable_klass(thr, k);
)
jint result = UNCHECKED()->ThrowNew(env, clazz, msg);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jthrowable,
checked_jni_ExceptionOccurred(JNIEnv *env))
thr->clear_pending_jni_exception_check();
functionEnterExceptionAllowed(thr);
jthrowable result = UNCHECKED()->ExceptionOccurred(env);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(void,
checked_jni_ExceptionDescribe(JNIEnv *env))
functionEnterExceptionAllowed(thr);
UNCHECKED()->ExceptionDescribe(env);
functionExit(thr);
JNI_END
JNI_ENTRY_CHECKED(void,
checked_jni_ExceptionClear(JNIEnv *env))
thr->clear_pending_jni_exception_check();
functionEnterExceptionAllowed(thr);
UNCHECKED()->ExceptionClear(env);
functionExit(thr);
JNI_END
JNI_ENTRY_CHECKED(void,
checked_jni_FatalError(JNIEnv *env,
const char *msg))
thr->clear_pending_jni_exception_check();
functionEnter(thr);
UNCHECKED()->FatalError(env, msg);
functionExit(thr);
JNI_END
JNI_ENTRY_CHECKED(jint,
checked_jni_PushLocalFrame(JNIEnv *env,
jint capacity))
functionEnterExceptionAllowed(thr);
if (capacity < 0)
NativeReportJNIFatalError(thr, "negative capacity");
jint result = UNCHECKED()->PushLocalFrame(env, capacity);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_PopLocalFrame(JNIEnv *env,
jobject result))
functionEnterExceptionAllowed(thr);
jobject res = UNCHECKED()->PopLocalFrame(env, result);
functionExit(thr);
return res;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_NewGlobalRef(JNIEnv *env,
jobject lobj))
functionEnter(thr);
IN_VM(
if (lobj != nullptr) {
jniCheck::validate_handle(thr, lobj);
}
)
jobject result = UNCHECKED()->NewGlobalRef(env,lobj);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(void,
checked_jni_DeleteGlobalRef(JNIEnv *env,
jobject gref))
functionEnterExceptionAllowed(thr);
IN_VM(
jniCheck::validate_object(thr, gref);
if (gref && !JNIHandles::is_global_handle(gref)) {
ReportJNIFatalError(thr,
"Invalid global JNI handle passed to DeleteGlobalRef");
}
)
UNCHECKED()->DeleteGlobalRef(env,gref);
functionExit(thr);
JNI_END
JNI_ENTRY_CHECKED(void,
checked_jni_DeleteLocalRef(JNIEnv *env,
jobject obj))
functionEnterExceptionAllowed(thr);
IN_VM(
jniCheck::validate_object(thr, obj);
if (obj && !(JNIHandles::is_local_handle(thr, obj) ||
JNIHandles::is_frame_handle(thr, obj)))
ReportJNIFatalError(thr,
"Invalid local JNI handle passed to DeleteLocalRef");
)
UNCHECKED()->DeleteLocalRef(env, obj);
functionExit(thr);
JNI_END
JNI_ENTRY_CHECKED(jboolean,
checked_jni_IsSameObject(JNIEnv *env,
jobject obj1,
jobject obj2))
functionEnterExceptionAllowed(thr);
IN_VM(
/* This JNI function can be used to compare weak global references
* to nullptr objects. If the handles are valid, but contain nullptr,
* then don't attempt to validate the object.
*/
if (obj1 != nullptr && jniCheck::validate_handle(thr, obj1) != nullptr) {
jniCheck::validate_object(thr, obj1);
}
if (obj2 != nullptr && jniCheck::validate_handle(thr, obj2) != nullptr) {
jniCheck::validate_object(thr, obj2);
}
)
jboolean result = UNCHECKED()->IsSameObject(env,obj1,obj2);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_NewLocalRef(JNIEnv *env,
jobject ref))
functionEnter(thr);
IN_VM(
if (ref != nullptr) {
jniCheck::validate_handle(thr, ref);
}
)
jobject result = UNCHECKED()->NewLocalRef(env, ref);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jint,
checked_jni_EnsureLocalCapacity(JNIEnv *env,
jint capacity))
functionEnter(thr);
if (capacity < 0) {
NativeReportJNIFatalError(thr, "negative capacity");
}
jint result = UNCHECKED()->EnsureLocalCapacity(env, capacity);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_AllocObject(JNIEnv *env,
jclass clazz))
functionEnter(thr);
IN_VM(
jniCheck::validate_class(thr, clazz, false);
)
jobject result = UNCHECKED()->AllocObject(env,clazz);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_NewObject(JNIEnv *env,
jclass clazz,
jmethodID methodID,
...))
functionEnter(thr);
va_list args;
IN_VM(
jniCheck::validate_call(thr, clazz, methodID);
)
va_start(args, methodID);
jobject result = UNCHECKED()->NewObjectV(env,clazz,methodID,args);
va_end(args);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_NewObjectV(JNIEnv *env,
jclass clazz,
jmethodID methodID,
va_list args))
functionEnter(thr);
IN_VM(
jniCheck::validate_call(thr, clazz, methodID);
)
jobject result = UNCHECKED()->NewObjectV(env,clazz,methodID,args);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jobject,
checked_jni_NewObjectA(JNIEnv *env,
jclass clazz,
jmethodID methodID,
const jvalue *args))
functionEnter(thr);
IN_VM(
jniCheck::validate_call(thr, clazz, methodID);
)
jobject result = UNCHECKED()->NewObjectA(env,clazz,methodID,args);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jclass,
checked_jni_GetObjectClass(JNIEnv *env,
jobject obj))
functionEnter(thr);
IN_VM(
jniCheck::validate_object(thr, obj);
)
jclass result = UNCHECKED()->GetObjectClass(env,obj);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jboolean,
checked_jni_IsInstanceOf(JNIEnv *env,
jobject obj,
jclass clazz))
functionEnter(thr);
IN_VM(
jniCheck::validate_object(thr, obj);
jniCheck::validate_class(thr, clazz, true);
)
jboolean result = UNCHECKED()->IsInstanceOf(env,obj,clazz);
functionExit(thr);
return result;
JNI_END
JNI_ENTRY_CHECKED(jmethodID,
checked_jni_GetMethodID(JNIEnv *env,
jclass clazz,
const char *name,
const char *sig))
functionEnter(thr);
IN_VM(
jniCheck::validate_class(thr, clazz, false);
)
jmethodID result = UNCHECKED()->GetMethodID(env,clazz,name,sig);
functionExit(thr);
return result;
JNI_END
#define WRAPPER_CallMethod(ResultType, Result) \
JNI_ENTRY_CHECKED(ResultType, \
checked_jni_Call##Result##Method(JNIEnv *env, \
jobject obj, \
jmethodID methodID, \
...)) \
functionEnter(thr); \
va_list args; \
IN_VM( \
jniCheck::validate_call(thr, nullptr, methodID, obj); \
) \
va_start(args,methodID); \
ResultType result =UNCHECKED()->Call##Result##MethodV(env, obj, methodID, \
args); \
va_end(args); \
thr->set_pending_jni_exception_check("Call"#Result"Method"); \
functionExit(thr); \
return result; \
JNI_END \
\
JNI_ENTRY_CHECKED(ResultType, \
checked_jni_Call##Result##MethodV(JNIEnv *env, \
jobject obj, \
jmethodID methodID, \
va_list args)) \
functionEnter(thr); \
IN_VM(\
jniCheck::validate_call(thr, nullptr, methodID, obj); \