- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathjvmciEnv.cpp
2018 lines (1856 loc) · 80.7 KB
/
jvmciEnv.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 (c) 1999, 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/stringTable.hpp"
#include"classfile/symbolTable.hpp"
#include"classfile/systemDictionary.hpp"
#include"code/codeCache.hpp"
#include"compiler/compilerOracle.hpp"
#include"compiler/compileTask.hpp"
#include"gc/shared/barrierSet.hpp"
#include"gc/shared/barrierSetNMethod.hpp"
#include"jvm_io.h"
#include"jvmci/jniAccessMark.inline.hpp"
#include"jvmci/jvmciCompiler.hpp"
#include"jvmci/jvmciRuntime.hpp"
#include"memory/oopFactory.hpp"
#include"memory/resourceArea.hpp"
#include"memory/universe.hpp"
#include"oops/objArrayKlass.hpp"
#include"oops/typeArrayOop.inline.hpp"
#include"prims/jvmtiExport.hpp"
#include"runtime/arguments.hpp"
#include"runtime/deoptimization.hpp"
#include"runtime/fieldDescriptor.inline.hpp"
#include"runtime/javaCalls.hpp"
#include"runtime/jniHandles.inline.hpp"
#include"runtime/os.hpp"
#include"utilities/permitForbiddenFunctions.hpp"
JVMCICompileState::JVMCICompileState(CompileTask* task, JVMCICompiler* compiler):
_task(task),
_compiler(compiler),
_retryable(true),
_failure_reason(nullptr),
_failure_reason_on_C_heap(false) {
// Get Jvmti capabilities under lock to get consistent values.
MutexLocker mu(JvmtiThreadState_lock);
_jvmti_redefinition_count = JvmtiExport::redefinition_count();
_jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint() ? 1 : 0;
_jvmti_can_access_local_variables = JvmtiExport::can_access_local_variables() ? 1 : 0;
_jvmti_can_post_on_exceptions = JvmtiExport::can_post_on_exceptions() ? 1 : 0;
_jvmti_can_pop_frame = JvmtiExport::can_pop_frame() ? 1 : 0;
_target_method_is_old = _task != nullptr && _task->method()->is_old();
if (task->is_blocking()) {
task->set_blocking_jvmci_compile_state(this);
}
}
voidJVMCICompileState::set_failure(bool retryable, constchar* reason, bool reason_on_C_heap) {
if (_failure_reason != nullptr && _failure_reason_on_C_heap) {
os::free((void*) _failure_reason);
}
_failure_reason = reason;
_failure_reason_on_C_heap = reason_on_C_heap;
_retryable = retryable;
}
voidJVMCICompileState::notify_libjvmci_oome() {
constchar* msg = "Out of memory initializing libjvmci or attaching it to the current thread";
set_failure(true, msg);
_compiler->on_upcall(msg);
}
// Update global JVMCI compilation ticks after 512 thread-local JVMCI compilation ticks.
// This mitigates the overhead of the atomic operation used for the global update.
#defineTHREAD_TICKS_PER_GLOBAL_TICKS (2 << 9)
#defineTHREAD_TICKS_PER_GLOBAL_TICKS_MASK (THREAD_TICKS_PER_GLOBAL_TICKS - 1)
voidJVMCICompileState::inc_compilation_ticks() {
if ((++_compilation_ticks & THREAD_TICKS_PER_GLOBAL_TICKS_MASK) == 0) {
_compiler->inc_global_compilation_ticks();
}
}
boolJVMCICompileState::jvmti_state_changed() const {
// Some classes were redefined
if (jvmti_redefinition_count() != JvmtiExport::redefinition_count()) {
returntrue;
}
if (!jvmti_can_access_local_variables() &&
JvmtiExport::can_access_local_variables()) {
returntrue;
}
if (!jvmti_can_hotswap_or_post_breakpoint() &&
JvmtiExport::can_hotswap_or_post_breakpoint()) {
returntrue;
}
if (!jvmti_can_post_on_exceptions() &&
JvmtiExport::can_post_on_exceptions()) {
returntrue;
}
if (!jvmti_can_pop_frame() &&
JvmtiExport::can_pop_frame()) {
returntrue;
}
returnfalse;
}
voidJVMCIEnv::init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env) {
assert(thread != nullptr, "npe");
_env = nullptr;
_pop_frame_on_close = false;
_detach_on_close = false;
if (!UseJVMCINativeLibrary) {
// In HotSpot mode, JNI isn't used at all.
_runtime = JVMCI::java_runtime();
_is_hotspot = true;
return;
}
if (parent_env != nullptr) {
// If the parent JNI environment is non-null then figure out whether it
// is a HotSpot or shared library JNIEnv and set the state appropriately.
_is_hotspot = thread->jni_environment() == parent_env;
if (_is_hotspot) {
// Select the Java runtime
_runtime = JVMCI::java_runtime();
return;
}
_runtime = thread->libjvmci_runtime();
assert(_runtime != nullptr, "npe");
_env = parent_env;
return;
}
// Running in JVMCI shared library mode so ensure the shared library
// is loaded and initialized and get a shared library JNIEnv
_is_hotspot = false;
_runtime = JVMCI::compiler_runtime(thread);
_env = _runtime->init_shared_library_javavm(&_init_error, &_init_error_msg);
if (_env != nullptr) {
// Creating the JVMCI shared library VM also attaches the current thread
_detach_on_close = true;
} elseif (_init_error != JNI_OK) {
// Caller creating this JVMCIEnv must handle the error.
JVMCI_event_1("[%s:%d] Error creating libjvmci (err: %d, %s)", _file, _line,
_init_error, _init_error_msg == nullptr ? "unknown" : _init_error_msg);
return;
} else {
_runtime->GetEnv(thread, (void**)&parent_env, JNI_VERSION_1_2);
if (parent_env != nullptr) {
// Even though there's a parent JNI env, there's no guarantee
// it was opened by a JVMCIEnv scope and thus may not have
// pushed a local JNI frame. As such, we use a new JNI local
// frame in this scope to ensure local JNI refs are collected
// in a timely manner after leaving this scope.
_env = parent_env;
} else {
ResourceMark rm; // Thread name is resource allocated
JavaVMAttachArgs attach_args;
attach_args.version = JNI_VERSION_1_2;
attach_args.name = const_cast<char*>(thread->name());
attach_args.group = nullptr;
_init_error = _runtime->AttachCurrentThread(thread, (void**) &_env, &attach_args);
if (_init_error == JNI_OK) {
_detach_on_close = true;
} else {
// Caller creating this JVMCIEnv must handle the error.
_env = nullptr;
JVMCI_event_1("[%s:%d] Error attaching to libjvmci (err: %d)", _file, _line, _init_error);
return;
}
}
}
assert(_env != nullptr, "missing env");
assert(_throw_to_caller == false, "must be");
JNIAccessMark jni(this, thread);
jint result = _env->PushLocalFrame(32);
if (result != JNI_OK) {
JVMCI_event_1("[%s:%d] Error pushing local JNI frame (err: %d)", _file, _line, result);
return;
}
_pop_frame_on_close = true;
}
JVMCIEnv::JVMCIEnv(JavaThread* thread, JVMCICompileState* compile_state, constchar* file, int line):
_throw_to_caller(false), _file(file), _line(line), _init_error(JNI_OK), _init_error_msg(nullptr), _compile_state(compile_state) {
init_env_mode_runtime(thread, nullptr);
}
JVMCIEnv::JVMCIEnv(JavaThread* thread, constchar* file, int line):
_throw_to_caller(false), _file(file), _line(line), _init_error(JNI_OK), _init_error_msg(nullptr), _compile_state(nullptr) {
init_env_mode_runtime(thread, nullptr);
}
JVMCIEnv::JVMCIEnv(JavaThread* thread, JNIEnv* parent_env, constchar* file, int line):
_throw_to_caller(true), _file(file), _line(line), _init_error(JNI_OK), _init_error_msg(nullptr), _compile_state(nullptr) {
assert(parent_env != nullptr, "npe");
init_env_mode_runtime(thread, parent_env);
assert(_env == nullptr || parent_env == _env, "mismatched JNIEnvironment");
assert(_init_error == JNI_OK, "err: %d", _init_error);
}
voidJVMCIEnv::init(JavaThread* thread, bool is_hotspot, constchar* file, int line) {
_compile_state = nullptr;
_throw_to_caller = false;
_file = file;
_line = line;
_init_error = JNI_OK;
_init_error_msg = nullptr;
if (is_hotspot) {
_env = nullptr;
_pop_frame_on_close = false;
_detach_on_close = false;
_is_hotspot = true;
_runtime = JVMCI::java_runtime();
} else {
init_env_mode_runtime(thread, nullptr);
}
}
voidJVMCIEnv::check_init(JVMCI_TRAPS) {
guarantee(JVMCIENV != this, "must be");
if (_init_error == JNI_OK) {
return;
}
if (_init_error == JNI_ENOMEM) {
JVMCI_THROW_MSG(OutOfMemoryError, "JNI_ENOMEM creating or attaching to libjvmci");
}
stringStream st;
st.print("Error creating or attaching to libjvmci (err: %d, description: %s)",
_init_error, _init_error_msg == nullptr ? "unknown" : _init_error_msg);
JVMCI_THROW_MSG(InternalError, st.freeze());
}
voidJVMCIEnv::check_init(TRAPS) {
if (_init_error == JNI_OK) {
return;
}
if (_init_error == JNI_ENOMEM) {
THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "JNI_ENOMEM creating or attaching to libjvmci");
}
stringStream st;
st.print("Error creating or attaching to libjvmci (err: %d, description: %s)",
_init_error, _init_error_msg == nullptr ? "unknown" : _init_error_msg);
THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), st.freeze());
}
// Prints a pending exception (if any) and its stack trace to st.
// Also partially logs the stack trace to the JVMCI event log.
voidJVMCIEnv::describe_pending_exception(outputStream* st) {
ResourceMark rm;
char* stack_trace = nullptr;
if (pending_exception_as_string(nullptr, (constchar**) &stack_trace)) {
st->print_raw_cr(stack_trace);
// Use up to half the lines of the JVMCI event log to
// show the stack trace.
char* cursor = stack_trace;
int line = 0;
constint max_lines = LogEventsBufferEntries / 2;
char* last_line = nullptr;
while (*cursor != '\0') {
char* eol = strchr(cursor, '\n');
if (eol == nullptr) {
if (line == max_lines - 1) {
last_line = cursor;
} elseif (line < max_lines) {
JVMCI_event_1("%s", cursor);
}
cursor = cursor + strlen(cursor);
} else {
*eol = '\0';
if (line == max_lines - 1) {
last_line = cursor;
} elseif (line < max_lines) {
JVMCI_event_1("%s", cursor);
}
cursor = eol + 1;
}
line++;
}
if (last_line != nullptr) {
if (line > max_lines) {
JVMCI_event_1("%s [elided %d more stack trace lines]", last_line, line - max_lines);
} else {
JVMCI_event_1("%s", last_line);
}
}
}
}
boolJVMCIEnv::pending_exception_as_string(constchar** to_string, constchar** stack_trace) {
JavaThread* THREAD = JavaThread::current(); // For exception macros.
JVMCIObject to_string_obj;
JVMCIObject stack_trace_obj;
bool had_nested_exception = false;
if (!is_hotspot()) {
JNIAccessMark jni(this, THREAD);
jthrowable ex = jni()->ExceptionOccurred();
if (ex != nullptr) {
jni()->ExceptionClear();
jobjectArray pair = (jobjectArray) jni()->CallStaticObjectMethod(
JNIJVMCI::HotSpotJVMCIRuntime::clazz(),
JNIJVMCI::HotSpotJVMCIRuntime::exceptionToString_method(),
ex, to_string != nullptr, stack_trace != nullptr);
if (jni()->ExceptionCheck()) {
// As last resort, dump nested exception
jni()->ExceptionDescribe();
had_nested_exception = true;
} else {
guarantee(pair != nullptr, "pair is null");
int len = jni()->GetArrayLength(pair);
guarantee(len == 2, "bad len is %d", len);
if (to_string != nullptr) {
to_string_obj = JVMCIObject::create(jni()->GetObjectArrayElement(pair, 0), false);
}
if (stack_trace != nullptr) {
stack_trace_obj = JVMCIObject::create(jni()->GetObjectArrayElement(pair, 1), false);
}
}
} else {
returnfalse;
}
} else {
if (HAS_PENDING_EXCEPTION) {
Handleexception(THREAD, PENDING_EXCEPTION);
CLEAR_PENDING_EXCEPTION;
JavaCallArguments jargs;
jargs.push_oop(exception);
jargs.push_int(to_string != nullptr);
jargs.push_int(stack_trace != nullptr);
JavaValue result(T_OBJECT);
JavaCalls::call_static(&result,
HotSpotJVMCI::HotSpotJVMCIRuntime::klass(),
vmSymbols::exceptionToString_name(),
vmSymbols::exceptionToString_signature(), &jargs, THREAD);
if (HAS_PENDING_EXCEPTION) {
Handlenested_exception(THREAD, PENDING_EXCEPTION);
CLEAR_PENDING_EXCEPTION;
java_lang_Throwable::print_stack_trace(nested_exception, tty);
// Clear and ignore any exceptions raised during printing
CLEAR_PENDING_EXCEPTION;
had_nested_exception = true;
} else {
oop pair = result.get_oop();
guarantee(pair->is_objArray(), "must be");
objArrayOop pair_arr = objArrayOop(pair);
int len = pair_arr->length();
guarantee(len == 2, "bad len is %d", len);
if (to_string != nullptr) {
to_string_obj = HotSpotJVMCI::wrap(pair_arr->obj_at(0));
}
if (stack_trace != nullptr) {
stack_trace_obj = HotSpotJVMCI::wrap(pair_arr->obj_at(1));
}
}
} else {
returnfalse;
}
}
if (had_nested_exception) {
if (to_string != nullptr) {
*to_string = "nested exception occurred converting exception to string";
}
if (stack_trace != nullptr) {
*stack_trace = "nested exception occurred converting exception stack to string";
}
} else {
if (to_string_obj.is_non_null()) {
*to_string = as_utf8_string(to_string_obj);
}
if (stack_trace_obj.is_non_null()) {
*stack_trace = as_utf8_string(stack_trace_obj);
}
}
returntrue;
}
// Shared code for translating an exception from HotSpot to libjvmci or vice versa.
classExceptionTranslation: publicStackObj {
protected:
enum DecodeFormat {
_encoded_ok = 0, // exception was successfully encoded into buffer
_buffer_alloc_fail = 1, // native memory for buffer could not be allocated
_encode_oome_fail = 2, // OutOfMemoryError thrown during encoding
_encode_fail = 3, // some other problem occured during encoding. If buffer != 0,
// buffer contains a `struct { u4 len; char[len] desc}`
// describing the problem
_encode_oome_in_vm = 4// an OutOfMemoryError thrown from within VM code on a
// thread that cannot call Java (OOME has no stack trace)
};
JVMCIEnv* _from_env; // Source of translation. Can be null.
JVMCIEnv* _to_env; // Destination of translation. Never null.
ExceptionTranslation(JVMCIEnv* from_env, JVMCIEnv* to_env) : _from_env(from_env), _to_env(to_env) {}
// Encodes the exception in `_from_env` into `buffer`.
// Where N is the number of bytes needed for the encoding, returns N if N <= `buffer_size`
// and the encoding was written to `buffer` otherwise returns -N.
virtualintencode(JavaThread* THREAD, jlong buffer, int buffer_size) = 0;
// Decodes the exception in `buffer` in `_to_env` and throws it.
virtualvoiddecode(JavaThread* THREAD, DecodeFormat format, jlong buffer) = 0;
staticbooldebug_translated_exception() {
constchar* prop_value = Arguments::get_property("jdk.internal.vm.TranslatedException.debug");
return prop_value != nullptr && strcmp("true", prop_value) == 0;
}
public:
voiddoit(JavaThread* THREAD) {
int buffer_size = 2048;
while (true) {
ResourceMark rm;
jlong buffer = (jlong) NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, jbyte, buffer_size);
if (buffer == 0L) {
JVMCI_event_1("error translating exception: translation buffer allocation failed");
decode(THREAD, _buffer_alloc_fail, 0L);
return;
}
int res = encode(THREAD, buffer, buffer_size);
if (_to_env->has_pending_exception()) {
// Propagate pending exception
return;
}
if (res < 0) {
int required_buffer_size = -res;
if (required_buffer_size > buffer_size) {
buffer_size = required_buffer_size;
}
} else {
decode(THREAD, _encoded_ok, buffer);
if (!_to_env->has_pending_exception()) {
_to_env->throw_InternalError("decodeAndThrowThrowable should have thrown an exception");
}
return;
}
}
}
};
// Translates an exception on the HotSpot heap to an exception on the shared library heap.
classHotSpotToSharedLibraryExceptionTranslation : publicExceptionTranslation {
private:
constHandle& _throwable;
char* print_throwable_to_buffer(Handle throwable, jlong buffer, int buffer_size) {
char* char_buffer = (char*) buffer + 4;
stringStream st(char_buffer, (size_t) buffer_size - 4);
java_lang_Throwable::print_stack_trace(throwable, &st);
u4 len = (u4) st.size();
*((u4*) buffer) = len;
return char_buffer;
}
boolhandle_pending_exception(JavaThread* THREAD, jlong buffer, int buffer_size) {
if (HAS_PENDING_EXCEPTION) {
Handle throwable = Handle(THREAD, PENDING_EXCEPTION);
Symbol *ex_name = throwable->klass()->name();
CLEAR_PENDING_EXCEPTION;
if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
JVMCI_event_1("error translating exception: OutOfMemoryError");
decode(THREAD, _encode_oome_fail, 0L);
} else {
char* char_buffer = print_throwable_to_buffer(throwable, buffer, buffer_size);
JVMCI_event_1("error translating exception: %s", char_buffer);
decode(THREAD, _encode_fail, buffer);
}
returntrue;
}
returnfalse;
}
intencode(JavaThread* THREAD, jlong buffer, int buffer_size) {
if (!THREAD->can_call_java()) {
Symbol *ex_name = _throwable->klass()->name();
if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
JVMCI_event_1("translating exception: OutOfMemoryError within VM code");
decode(THREAD, _encode_oome_in_vm, 0L);
return0;
}
char* char_buffer = print_throwable_to_buffer(_throwable, buffer, buffer_size);
constchar* detail = log_is_enabled(Info, exceptions) ? "" : " (-Xlog:exceptions may give more detail)";
JVMCI_event_1("cannot call Java to translate exception%s: %s", detail, char_buffer);
decode(THREAD, _encode_fail, buffer);
return0;
}
Klass* vmSupport = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_vm_VMSupport(), true, THREAD);
if (handle_pending_exception(THREAD, buffer, buffer_size)) {
return0;
}
JavaCallArguments jargs;
jargs.push_oop(_throwable);
jargs.push_long(buffer);
jargs.push_int(buffer_size);
JavaValue result(T_INT);
JavaCalls::call_static(&result,
vmSupport,
vmSymbols::encodeThrowable_name(),
vmSymbols::encodeThrowable_signature(), &jargs, THREAD);
if (handle_pending_exception(THREAD, buffer, buffer_size)) {
return0;
}
return result.get_jint();
}
voiddecode(JavaThread* THREAD, DecodeFormat format, jlong buffer) {
JVMCI_event_1("decoding exception from JVM heap (format: %d, buffer[%d]) ", format, buffer == 0L ? -1 : *((u4*) buffer));
JNIAccessMark jni(_to_env, THREAD);
jni()->CallStaticVoidMethod(JNIJVMCI::VMSupport::clazz(),
JNIJVMCI::VMSupport::decodeAndThrowThrowable_method(),
format, buffer, false, debug_translated_exception());
}
public:
HotSpotToSharedLibraryExceptionTranslation(JVMCIEnv* hotspot_env, JVMCIEnv* jni_env, constHandle& throwable) :
ExceptionTranslation(hotspot_env, jni_env), _throwable(throwable) {}
};
// Translates an exception on the shared library heap to an exception on the HotSpot heap.
classSharedLibraryToHotSpotExceptionTranslation : publicExceptionTranslation {
private:
jthrowable _throwable;
intencode(JavaThread* THREAD, jlong buffer, int buffer_size) {
JNIAccessMark jni(_from_env, THREAD);
int res = jni()->CallStaticIntMethod(JNIJVMCI::VMSupport::clazz(),
JNIJVMCI::VMSupport::encodeThrowable_method(),
_throwable, buffer, buffer_size);
if (jni()->ExceptionCheck()) {
// Cannot get name of exception thrown as that can raise another exception.
jni()->ExceptionClear();
JVMCI_event_1("error translating exception: unknown error");
decode(THREAD, _encode_fail, 0L);
return0;
}
return res;
}
voiddecode(JavaThread* THREAD, DecodeFormat format, jlong buffer) {
JVMCI_event_1("decoding exception to JVM heap (format: %d, buffer[%d]) ", format, buffer == 0L ? -1 : *((u4*) buffer));
Klass* vmSupport = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_vm_VMSupport(), true, CHECK);
JavaCallArguments jargs;
jargs.push_int(format);
jargs.push_long(buffer);
jargs.push_int(true);
jargs.push_int(debug_translated_exception());
JavaValue result(T_VOID);
JavaCalls::call_static(&result,
vmSupport,
vmSymbols::decodeAndThrowThrowable_name(),
vmSymbols::decodeAndThrowThrowable_signature(), &jargs, THREAD);
}
public:
SharedLibraryToHotSpotExceptionTranslation(JVMCIEnv* hotspot_env, JVMCIEnv* jni_env, jthrowable throwable) :
ExceptionTranslation(jni_env, hotspot_env), _throwable(throwable) {}
};
voidJVMCIEnv::translate_to_jni_exception(JavaThread* THREAD, constHandle& throwable, JVMCIEnv* hotspot_env, JVMCIEnv* jni_env) {
HotSpotToSharedLibraryExceptionTranslation(hotspot_env, jni_env, throwable).doit(THREAD);
}
voidJVMCIEnv::translate_from_jni_exception(JavaThread* THREAD, jthrowable throwable, JVMCIEnv* hotspot_env, JVMCIEnv* jni_env) {
SharedLibraryToHotSpotExceptionTranslation(hotspot_env, jni_env, throwable).doit(THREAD);
}
jboolean JVMCIEnv::transfer_pending_exception_to_jni(JavaThread* THREAD, JVMCIEnv* hotspot_env, JVMCIEnv* jni_env) {
if (HAS_PENDING_EXCEPTION) {
Handle throwable = Handle(THREAD, PENDING_EXCEPTION);
CLEAR_PENDING_EXCEPTION;
translate_to_jni_exception(THREAD, throwable, hotspot_env, jni_env);
returntrue;
}
returnfalse;
}
jboolean JVMCIEnv::transfer_pending_exception(JavaThread* THREAD, JVMCIEnv* peer_env) {
if (is_hotspot()) {
returntransfer_pending_exception_to_jni(THREAD, this, peer_env);
}
jthrowable ex = nullptr;
{
JNIAccessMark jni(this, THREAD);
ex = jni()->ExceptionOccurred();
if (ex != nullptr) {
jni()->ExceptionClear();
}
}
if (ex != nullptr) {
translate_from_jni_exception(THREAD, ex, peer_env, this);
returntrue;
}
returnfalse;
}
JVMCIEnv::~JVMCIEnv() {
if (_init_error_msg != nullptr) {
// The memory allocated in libjvmci was not allocated with os::malloc
// so must not be freed with os::free.
permit_forbidden_function::free((void*)_init_error_msg);
}
if (_init_error != JNI_OK) {
return;
}
if (_throw_to_caller) {
if (is_hotspot()) {
// Nothing to do
} else {
Thread* thread = Thread::current();
if (thread->is_Java_thread()) {
JavaThread* THREAD = JavaThread::cast(thread); // For exception macros.
if (HAS_PENDING_EXCEPTION) {
Handle throwable = Handle(THREAD, PENDING_EXCEPTION);
CLEAR_PENDING_EXCEPTION;
translate_to_jni_exception(THREAD, throwable, nullptr, this);
}
}
}
} else {
if (_pop_frame_on_close) {
// Pop the JNI local frame that was pushed when entering this JVMCIEnv scope.
JNIAccessMark jni(this);
jni()->PopLocalFrame(nullptr);
}
if (has_pending_exception()) {
char message[256];
jio_snprintf(message, 256, "Uncaught exception exiting %s JVMCIEnv scope entered at %s:%d",
is_hotspot() ? "HotSpot" : "libjvmci", _file, _line);
JVMCIRuntime::fatal_exception(this, message);
}
if (_detach_on_close) {
_runtime->DetachCurrentThread(JavaThread::current());
}
}
}
jboolean JVMCIEnv::has_pending_exception() {
if (is_hotspot()) {
JavaThread* THREAD = JavaThread::current(); // For exception macros.
return HAS_PENDING_EXCEPTION;
} else {
JNIAccessMark jni(this);
returnjni()->ExceptionCheck();
}
}
voidJVMCIEnv::clear_pending_exception() {
if (is_hotspot()) {
JavaThread* THREAD = JavaThread::current(); // For exception macros.
CLEAR_PENDING_EXCEPTION;
} else {
JNIAccessMark jni(this);
jni()->ExceptionClear();
}
}
intJVMCIEnv::get_length(JVMCIArray array) {
if (is_hotspot()) {
returnHotSpotJVMCI::resolve(array)->length();
} else {
JNIAccessMark jni(this);
returnjni()->GetArrayLength(get_jarray(array));
}
}
JVMCIObject JVMCIEnv::get_object_at(JVMCIObjectArray array, int index) {
if (is_hotspot()) {
oop result = HotSpotJVMCI::resolve(array)->obj_at(index);
returnwrap(result);
} else {
JNIAccessMark jni(this);
jobject result = jni()->GetObjectArrayElement(get_jobjectArray(array), index);
returnwrap(result);
}
}
voidJVMCIEnv::put_object_at(JVMCIObjectArray array, int index, JVMCIObject value) {
if (is_hotspot()) {
HotSpotJVMCI::resolve(array)->obj_at_put(index, HotSpotJVMCI::resolve(value));
} else {
JNIAccessMark jni(this);
jni()->SetObjectArrayElement(get_jobjectArray(array), index, get_jobject(value));
}
}
jboolean JVMCIEnv::get_bool_at(JVMCIPrimitiveArray array, int index) {
if (is_hotspot()) {
returnHotSpotJVMCI::resolve(array)->bool_at(index);
} else {
JNIAccessMark jni(this);
jboolean result;
jni()->GetBooleanArrayRegion(array.as_jbooleanArray(), index, 1, &result);
return result;
}
}
voidJVMCIEnv::put_bool_at(JVMCIPrimitiveArray array, int index, jboolean value) {
if (is_hotspot()) {
HotSpotJVMCI::resolve(array)->bool_at_put(index, value);
} else {
JNIAccessMark jni(this);
jni()->SetBooleanArrayRegion(array.as_jbooleanArray(), index, 1, &value);
}
}
jbyte JVMCIEnv::get_byte_at(JVMCIPrimitiveArray array, int index) {
if (is_hotspot()) {
returnHotSpotJVMCI::resolve(array)->byte_at(index);
} else {
JNIAccessMark jni(this);
jbyte result;
jni()->GetByteArrayRegion(array.as_jbyteArray(), index, 1, &result);
return result;
}
}
voidJVMCIEnv::put_byte_at(JVMCIPrimitiveArray array, int index, jbyte value) {
if (is_hotspot()) {
HotSpotJVMCI::resolve(array)->byte_at_put(index, value);
} else {
JNIAccessMark jni(this);
jni()->SetByteArrayRegion(array.as_jbyteArray(), index, 1, &value);
}
}
jint JVMCIEnv::get_int_at(JVMCIPrimitiveArray array, int index) {
if (is_hotspot()) {
returnHotSpotJVMCI::resolve(array)->int_at(index);
} else {
JNIAccessMark jni(this);
jint result;
jni()->GetIntArrayRegion(array.as_jintArray(), index, 1, &result);
return result;
}
}
voidJVMCIEnv::put_int_at(JVMCIPrimitiveArray array, int index, jint value) {
if (is_hotspot()) {
HotSpotJVMCI::resolve(array)->int_at_put(index, value);
} else {
JNIAccessMark jni(this);
jni()->SetIntArrayRegion(array.as_jintArray(), index, 1, &value);
}
}
jlong JVMCIEnv::get_long_at(JVMCIPrimitiveArray array, int index) {
if (is_hotspot()) {
returnHotSpotJVMCI::resolve(array)->long_at(index);
} else {
JNIAccessMark jni(this);
jlong result;
jni()->GetLongArrayRegion(array.as_jlongArray(), index, 1, &result);
return result;
}
}
voidJVMCIEnv::put_long_at(JVMCIPrimitiveArray array, int index, jlong value) {
if (is_hotspot()) {
HotSpotJVMCI::resolve(array)->long_at_put(index, value);
} else {
JNIAccessMark jni(this);
jni()->SetLongArrayRegion(array.as_jlongArray(), index, 1, &value);
}
}
voidJVMCIEnv::copy_bytes_to(JVMCIPrimitiveArray src, jbyte* dest, int offset, jsize length) {
if (length == 0) {
return;
}
if (is_hotspot()) {
memcpy(dest, HotSpotJVMCI::resolve(src)->byte_at_addr(offset), length);
} else {
JNIAccessMark jni(this);
jni()->GetByteArrayRegion(src.as_jbyteArray(), offset, length, dest);
}
}
voidJVMCIEnv::copy_bytes_from(jbyte* src, JVMCIPrimitiveArray dest, int offset, jsize length) {
if (length == 0) {
return;
}
if (is_hotspot()) {
memcpy(HotSpotJVMCI::resolve(dest)->byte_at_addr(offset), src, length);
} else {
JNIAccessMark jni(this);
jni()->SetByteArrayRegion(dest.as_jbyteArray(), offset, length, src);
}
}
voidJVMCIEnv::copy_longs_from(jlong* src, JVMCIPrimitiveArray dest, int offset, jsize length) {
if (length == 0) {
return;
}
if (is_hotspot()) {
memcpy(HotSpotJVMCI::resolve(dest)->long_at_addr(offset), src, length * sizeof(jlong));
} else {
JNIAccessMark jni(this);
jni()->SetLongArrayRegion(dest.as_jlongArray(), offset, length, src);
}
}
jboolean JVMCIEnv::is_boxing_object(BasicType type, JVMCIObject object) {
if (is_hotspot()) {
returnjava_lang_boxing_object::is_instance(HotSpotJVMCI::resolve(object), type);
} else {
JNIAccessMark jni(this);
returnjni()->IsInstanceOf(get_jobject(object), JNIJVMCI::box_class(type));
}
}
// Get the primitive value from a Java boxing object. It's hard error to
// pass a non-primitive BasicType.
jvalue JVMCIEnv::get_boxed_value(BasicType type, JVMCIObject object) {
jvalue result;
if (is_hotspot()) {
if (java_lang_boxing_object::get_value(HotSpotJVMCI::resolve(object), &result) == T_ILLEGAL) {
ShouldNotReachHere();
}
} else {
JNIAccessMark jni(this);
jfieldID field = JNIJVMCI::box_field(type);
switch (type) {
case T_BOOLEAN: result.z = jni()->GetBooleanField(get_jobject(object), field); break;
case T_BYTE: result.b = jni()->GetByteField(get_jobject(object), field); break;
case T_SHORT: result.s = jni()->GetShortField(get_jobject(object), field); break;
case T_CHAR: result.c = jni()->GetCharField(get_jobject(object), field); break;
case T_INT: result.i = jni()->GetIntField(get_jobject(object), field); break;
case T_LONG: result.j = jni()->GetLongField(get_jobject(object), field); break;
case T_FLOAT: result.f = jni()->GetFloatField(get_jobject(object), field); break;
case T_DOUBLE: result.d = jni()->GetDoubleField(get_jobject(object), field); break;
default:
ShouldNotReachHere();
}
}
return result;
}
// Return the BasicType of the object if it's a boxing object, otherwise return T_ILLEGAL.
BasicType JVMCIEnv::get_box_type(JVMCIObject object) {
if (is_hotspot()) {
returnjava_lang_boxing_object::basic_type(HotSpotJVMCI::resolve(object));
} else {
JNIAccessMark jni(this);
jclass clazz = jni()->GetObjectClass(get_jobject(object));
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_BOOLEAN))) return T_BOOLEAN;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_BYTE))) return T_BYTE;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_SHORT))) return T_SHORT;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_CHAR))) return T_CHAR;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_INT))) return T_INT;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_LONG))) return T_LONG;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_FLOAT))) return T_FLOAT;
if (jni()->IsSameObject(clazz, JNIJVMCI::box_class(T_DOUBLE))) return T_DOUBLE;
return T_ILLEGAL;
}
}
// Create a boxing object of the appropriate primitive type.
JVMCIObject JVMCIEnv::create_box(BasicType type, jvalue* value, JVMCI_TRAPS) {
switch (type) {
case T_BOOLEAN:
case T_BYTE:
case T_CHAR:
case T_SHORT:
case T_INT:
case T_LONG:
case T_FLOAT:
case T_DOUBLE:
break;
default:
JVMCI_THROW_MSG_(IllegalArgumentException, "Only boxes for primitive values can be created", JVMCIObject());
}
JavaThread* THREAD = JavaThread::current(); // For exception macros.
if (is_hotspot()) {
oop box = java_lang_boxing_object::create(type, value, CHECK_(JVMCIObject()));
returnHotSpotJVMCI::wrap(box);
} else {
JNIAccessMark jni(this, THREAD);
jobject box = jni()->NewObjectA(JNIJVMCI::box_class(type), JNIJVMCI::box_constructor(type), value);
assert(box != nullptr, "");
returnwrap(box);
}
}
constchar* JVMCIEnv::as_utf8_string(JVMCIObject str) {
if (is_hotspot()) {
returnjava_lang_String::as_utf8_string(HotSpotJVMCI::resolve(str));
} else {
JNIAccessMark jni(this);
jstring jstr = str.as_jstring();
int length = jni()->GetStringLength(jstr);
int utf8_length = jni()->GetStringUTFLength(jstr);
char* result = NEW_RESOURCE_ARRAY(char, utf8_length + 1);
jni()->GetStringUTFRegion(jstr, 0, length, result);
return result;
}
}
#defineDO_THROW(name) \
void JVMCIEnv::throw_##name(constchar* msg) { \
if (is_hotspot()) { \
JavaThread* THREAD = JavaThread::current(); \
THROW_MSG(HotSpotJVMCI::name::symbol(), msg); \
} else { \
JNIAccessMark jni(this); \
jni()->ThrowNew(JNIJVMCI::name::clazz(), msg); \
} \
}
DO_THROW(InternalError)
DO_THROW(ArrayIndexOutOfBoundsException)
DO_THROW(IllegalStateException)
DO_THROW(NullPointerException)
DO_THROW(IllegalArgumentException)
DO_THROW(InvalidInstalledCodeException)
DO_THROW(UnsatisfiedLinkError)
DO_THROW(UnsupportedOperationException)
DO_THROW(OutOfMemoryError)
DO_THROW(NoClassDefFoundError)
#undef DO_THROW
voidJVMCIEnv::fthrow_error(constchar* file, int line, constchar* format, ...) {
constint max_msg_size = 1024;
va_list ap;
va_start(ap, format);
char msg[max_msg_size];
os::vsnprintf(msg, max_msg_size, format, ap);
va_end(ap);
JavaThread* THREAD = JavaThread::current();
if (is_hotspot()) {
Handle h_loader;
Exceptions::_throw_msg(THREAD, file, line, vmSymbols::jdk_vm_ci_common_JVMCIError(), msg, h_loader );
} else {
JNIAccessMark jni(this, THREAD);
jni()->ThrowNew(JNIJVMCI::JVMCIError::clazz(), msg);
}
}
jboolean JVMCIEnv::call_HotSpotJVMCIRuntime_isGCSupported (JVMCIObject runtime, jint gcIdentifier) {
JavaThread* THREAD = JavaThread::current(); // For exception macros.
if (is_hotspot()) {
JavaCallArguments jargs;
jargs.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(runtime)));
jargs.push_int(gcIdentifier);
JavaValue result(T_BOOLEAN);
JavaCalls::call_special(&result,
HotSpotJVMCI::HotSpotJVMCIRuntime::klass(),
vmSymbols::isGCSupported_name(),
vmSymbols::int_bool_signature(), &jargs, CHECK_0);
return result.get_jboolean();
} else {
JNIAccessMark jni(this, THREAD);
jboolean result = jni()->CallNonvirtualBooleanMethod(runtime.as_jobject(),
JNIJVMCI::HotSpotJVMCIRuntime::clazz(),
JNIJVMCI::HotSpotJVMCIRuntime::isGCSupported_method(),
gcIdentifier);
if (jni()->ExceptionCheck()) {
returnfalse;
}
return result;
}
}
jboolean JVMCIEnv::call_HotSpotJVMCIRuntime_isIntrinsicSupported (JVMCIObject runtime, jint intrinsicIdentifier) {
JavaThread* THREAD = JavaThread::current(); // For exception macros.
if (is_hotspot()) {
JavaCallArguments jargs;
jargs.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(runtime)));
jargs.push_int(intrinsicIdentifier);
JavaValue result(T_BOOLEAN);
JavaCalls::call_special(&result,
HotSpotJVMCI::HotSpotJVMCIRuntime::klass(),
vmSymbols::isIntrinsicSupported_name(),
vmSymbols::int_bool_signature(), &jargs, CHECK_0);
return result.get_jboolean();
} else {
JNIAccessMark jni(this, THREAD);
jboolean result = jni()->CallNonvirtualBooleanMethod(runtime.as_jobject(),
JNIJVMCI::HotSpotJVMCIRuntime::clazz(),
JNIJVMCI::HotSpotJVMCIRuntime::isIntrinsicSupported_method(),
intrinsicIdentifier);
if (jni()->ExceptionCheck()) {
returnfalse;
}
return result;
}
}