This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
jvmciRuntime.cpp
1712 lines (1513 loc) · 65.2 KB
/
jvmciRuntime.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) 2012, 2020, 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 "precompiled.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "compiler/compileBroker.hpp"
#include "gc/shared/oopStorage.inline.hpp"
#include "jvmci/jniAccessMark.inline.hpp"
#include "jvmci/jvmciCompilerToVM.hpp"
#include "jvmci/jvmciRuntime.hpp"
#include "jvmci/metadataHandles.hpp"
#include "logging/log.hpp"
#include "memory/oopFactory.hpp"
#include "memory/universe.hpp"
#include "oops/constantPool.inline.hpp"
#include "oops/klass.inline.hpp"
#include "oops/method.inline.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/oop.inline.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/methodHandles.hpp"
#include "runtime/atomic.hpp"
#include "runtime/biasedLocking.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/reflectionUtils.hpp"
#include "runtime/sharedRuntime.hpp"
#if INCLUDE_G1GC
#include "gc/g1/g1ThreadLocalData.hpp"
#endif // INCLUDE_G1GC
// Simple helper to see if the caller of a runtime stub which
// entered the VM has been deoptimized
static bool caller_is_deopted() {
JavaThread* thread = JavaThread::current();
RegisterMap reg_map(thread, false);
frame runtime_frame = thread->last_frame();
frame caller_frame = runtime_frame.sender(®_map);
assert(caller_frame.is_compiled_frame(), "must be compiled");
return caller_frame.is_deoptimized_frame();
}
// Stress deoptimization
static void deopt_caller() {
if ( !caller_is_deopted()) {
JavaThread* thread = JavaThread::current();
RegisterMap reg_map(thread, false);
frame runtime_frame = thread->last_frame();
frame caller_frame = runtime_frame.sender(®_map);
Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint);
assert(caller_is_deopted(), "Must be deoptimized");
}
}
// Manages a scope for a JVMCI runtime call that attempts a heap allocation.
// If there is a pending nonasync exception upon closing the scope and the runtime
// call is of the variety where allocation failure returns NULL without an
// exception, the following action is taken:
// 1. The pending nonasync exception is cleared
// 2. NULL is written to JavaThread::_vm_result
// 3. Checks that an OutOfMemoryError is Universe::out_of_memory_error_retry().
class RetryableAllocationMark: public StackObj {
private:
JavaThread* _thread;
public:
RetryableAllocationMark(JavaThread* thread, bool activate) {
if (activate) {
assert(!thread->in_retryable_allocation(), "retryable allocation scope is non-reentrant");
_thread = thread;
_thread->set_in_retryable_allocation(true);
} else {
_thread = NULL;
}
}
~RetryableAllocationMark() {
if (_thread != NULL) {
_thread->set_in_retryable_allocation(false);
JavaThread* THREAD = _thread;
if (HAS_PENDING_EXCEPTION) {
oop ex = PENDING_EXCEPTION;
// Do not clear probable async exceptions.
CLEAR_PENDING_NONASYNC_EXCEPTION;
oop retry_oome = Universe::out_of_memory_error_retry();
if (ex->is_a(retry_oome->klass()) && retry_oome != ex) {
ResourceMark rm;
fatal("Unexpected exception in scope of retryable allocation: " INTPTR_FORMAT " of type %s", p2i(ex), ex->klass()->external_name());
}
_thread->set_vm_result(NULL);
}
}
}
};
JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance_common(JavaThread* thread, Klass* klass, bool null_on_fail))
JRT_BLOCK;
assert(klass->is_klass(), "not a class");
Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
InstanceKlass* h = InstanceKlass::cast(klass);
{
RetryableAllocationMark ram(thread, null_on_fail);
h->check_valid_for_instantiation(true, CHECK);
oop obj;
if (null_on_fail) {
if (!h->is_initialized()) {
// Cannot re-execute class initialization without side effects
// so return without attempting the initialization
return;
}
} else {
// make sure klass is initialized
h->initialize(CHECK);
}
// allocate instance and return via TLS
obj = h->allocate_instance(CHECK);
thread->set_vm_result(obj);
}
JRT_BLOCK_END;
SharedRuntime::on_slowpath_allocation_exit(thread);
JRT_END
JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array_common(JavaThread* thread, Klass* array_klass, jint length, bool null_on_fail))
JRT_BLOCK;
// Note: no handle for klass needed since they are not used
// anymore after new_objArray() and no GC can happen before.
// (This may have to change if this code changes!)
assert(array_klass->is_klass(), "not a class");
oop obj;
if (array_klass->is_typeArray_klass()) {
BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type();
RetryableAllocationMark ram(thread, null_on_fail);
obj = oopFactory::new_typeArray(elt_type, length, CHECK);
} else {
Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive
Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
RetryableAllocationMark ram(thread, null_on_fail);
obj = oopFactory::new_objArray(elem_klass, length, CHECK);
}
thread->set_vm_result(obj);
// This is pretty rare but this runtime patch is stressful to deoptimization
// if we deoptimize here so force a deopt to stress the path.
if (DeoptimizeALot) {
static int deopts = 0;
// Alternate between deoptimizing and raising an error (which will also cause a deopt)
if (deopts++ % 2 == 0) {
if (null_on_fail) {
return;
} else {
ResourceMark rm(THREAD);
THROW(vmSymbols::java_lang_OutOfMemoryError());
}
} else {
deopt_caller();
}
}
JRT_BLOCK_END;
SharedRuntime::on_slowpath_allocation_exit(thread);
JRT_END
JRT_ENTRY(void, JVMCIRuntime::new_multi_array_common(JavaThread* thread, Klass* klass, int rank, jint* dims, bool null_on_fail))
assert(klass->is_klass(), "not a class");
assert(rank >= 1, "rank must be nonzero");
Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
RetryableAllocationMark ram(thread, null_on_fail);
oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
thread->set_vm_result(obj);
JRT_END
JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array_common(JavaThread* thread, oopDesc* element_mirror, jint length, bool null_on_fail))
RetryableAllocationMark ram(thread, null_on_fail);
oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK);
thread->set_vm_result(obj);
JRT_END
JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance_common(JavaThread* thread, oopDesc* type_mirror, bool null_on_fail))
InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror));
if (klass == NULL) {
ResourceMark rm(THREAD);
THROW(vmSymbols::java_lang_InstantiationException());
}
RetryableAllocationMark ram(thread, null_on_fail);
// Create new instance (the receiver)
klass->check_valid_for_instantiation(false, CHECK);
if (null_on_fail) {
if (!klass->is_initialized()) {
// Cannot re-execute class initialization without side effects
// so return without attempting the initialization
return;
}
} else {
// Make sure klass gets initialized
klass->initialize(CHECK);
}
oop obj = klass->allocate_instance(CHECK);
thread->set_vm_result(obj);
JRT_END
extern void vm_exit(int code);
// Enter this method from compiled code handler below. This is where we transition
// to VM mode. This is done as a helper routine so that the method called directly
// from compiled code does not have to transition to VM. This allows the entry
// method to see if the nmethod that we have just looked up a handler for has
// been deoptimized while we were in the vm. This simplifies the assembly code
// cpu directories.
//
// We are entering here from exception stub (via the entry method below)
// If there is a compiled exception handler in this method, we will continue there;
// otherwise we will unwind the stack and continue at the caller of top frame method
// Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
// control the area where we can allow a safepoint. After we exit the safepoint area we can
// check to see if the handler we are going to return is now in a nmethod that has
// been deoptimized. If that is the case we return the deopt blob
// unpack_with_exception entry instead. This makes life for the exception blob easier
// because making that same check and diverting is painful from assembly language.
JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, CompiledMethod*& cm))
// Reset method handle flag.
thread->set_is_method_handle_return(false);
Handle exception(thread, ex);
cm = CodeCache::find_compiled(pc);
assert(cm != NULL, "this is not a compiled method");
// Adjust the pc as needed/
if (cm->is_deopt_pc(pc)) {
RegisterMap map(thread, false);
frame exception_frame = thread->last_frame().sender(&map);
// if the frame isn't deopted then pc must not correspond to the caller of last_frame
assert(exception_frame.is_deoptimized_frame(), "must be deopted");
pc = exception_frame.pc();
}
#ifdef ASSERT
assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
assert(oopDesc::is_oop(exception()), "just checking");
// Check that exception is a subclass of Throwable, otherwise we have a VerifyError
if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
if (ExitVMOnVerifyError) vm_exit(-1);
ShouldNotReachHere();
}
#endif
// Check the stack guard pages and reenable them if necessary and there is
// enough space on the stack to do so. Use fast exceptions only if the guard
// pages are enabled.
bool guard_pages_enabled = thread->stack_overflow_state()->reguard_stack_if_needed();
if (JvmtiExport::can_post_on_exceptions()) {
// To ensure correct notification of exception catches and throws
// we have to deoptimize here. If we attempted to notify the
// catches and throws during this exception lookup it's possible
// we could deoptimize on the way out of the VM and end back in
// the interpreter at the throw site. This would result in double
// notifications since the interpreter would also notify about
// these same catches and throws as it unwound the frame.
RegisterMap reg_map(thread);
frame stub_frame = thread->last_frame();
frame caller_frame = stub_frame.sender(®_map);
// We don't really want to deoptimize the nmethod itself since we
// can actually continue in the exception handler ourselves but I
// don't see an easy way to have the desired effect.
Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint);
assert(caller_is_deopted(), "Must be deoptimized");
return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
}
// ExceptionCache is used only for exceptions at call sites and not for implicit exceptions
if (guard_pages_enabled) {
address fast_continuation = cm->handler_for_exception_and_pc(exception, pc);
if (fast_continuation != NULL) {
// Set flag if return address is a method handle call site.
thread->set_is_method_handle_return(cm->is_method_handle_return(pc));
return fast_continuation;
}
}
// If the stack guard pages are enabled, check whether there is a handler in
// the current method. Otherwise (guard pages disabled), force an unwind and
// skip the exception cache update (i.e., just leave continuation==NULL).
address continuation = NULL;
if (guard_pages_enabled) {
// New exception handling mechanism can support inlined methods
// with exception handlers since the mappings are from PC to PC
// debugging support
// tracing
if (log_is_enabled(Info, exceptions)) {
ResourceMark rm;
stringStream tempst;
assert(cm->method() != NULL, "Unexpected null method()");
tempst.print("compiled method <%s>\n"
" at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT,
cm->method()->print_value_string(), p2i(pc), p2i(thread));
Exceptions::log_exception(exception, tempst.as_string());
}
// for AbortVMOnException flag
NOT_PRODUCT(Exceptions::debug_check_abort(exception));
// Clear out the exception oop and pc since looking up an
// exception handler can cause class loading, which might throw an
// exception and those fields are expected to be clear during
// normal bytecode execution.
thread->clear_exception_oop_and_pc();
bool recursive_exception = false;
continuation = SharedRuntime::compute_compiled_exc_handler(cm, pc, exception, false, false, recursive_exception);
// If an exception was thrown during exception dispatch, the exception oop may have changed
thread->set_exception_oop(exception());
thread->set_exception_pc(pc);
// The exception cache is used only for non-implicit exceptions
// Update the exception cache only when another exception did
// occur during the computation of the compiled exception handler
// (e.g., when loading the class of the catch type).
// Checking for exception oop equality is not
// sufficient because some exceptions are pre-allocated and reused.
if (continuation != NULL && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) {
cm->add_handler_for_exception_and_pc(exception, pc, continuation);
}
}
// Set flag if return address is a method handle call site.
thread->set_is_method_handle_return(cm->is_method_handle_return(pc));
if (log_is_enabled(Info, exceptions)) {
ResourceMark rm;
log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT
" for exception thrown at PC " PTR_FORMAT,
p2i(thread), p2i(continuation), p2i(pc));
}
return continuation;
JRT_END
// Enter this method from compiled code only if there is a Java exception handler
// in the method handling the exception.
// We are entering here from exception stub. We don't do a normal VM transition here.
// We do it in a helper. This is so we can check to see if the nmethod we have just
// searched for an exception handler has been deoptimized in the meantime.
address JVMCIRuntime::exception_handler_for_pc(JavaThread* thread) {
oop exception = thread->exception_oop();
address pc = thread->exception_pc();
// Still in Java mode
DEBUG_ONLY(ResetNoHandleMark rnhm);
CompiledMethod* cm = NULL;
address continuation = NULL;
{
// Enter VM mode by calling the helper
ResetNoHandleMark rnhm;
continuation = exception_handler_for_pc_helper(thread, exception, pc, cm);
}
// Back in JAVA, use no oops DON'T safepoint
// Now check to see if the compiled method we were called from is now deoptimized.
// If so we must return to the deopt blob and deoptimize the nmethod
if (cm != NULL && caller_is_deopted()) {
continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
}
assert(continuation != NULL, "no handler found");
return continuation;
}
JRT_BLOCK_ENTRY(void, JVMCIRuntime::monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock))
SharedRuntime::monitor_enter_helper(obj, lock, thread);
JRT_END
JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* thread, oopDesc* obj, BasicLock* lock))
assert(thread->last_Java_sp(), "last_Java_sp must be set");
assert(oopDesc::is_oop(obj), "invalid lock object pointer dected");
SharedRuntime::monitor_exit_helper(obj, lock, thread);
JRT_END
// Object.notify() fast path, caller does slow path
JRT_LEAF(jboolean, JVMCIRuntime::object_notify(JavaThread *thread, oopDesc* obj))
// Very few notify/notifyAll operations find any threads on the waitset, so
// the dominant fast-path is to simply return.
// Relatedly, it's critical that notify/notifyAll be fast in order to
// reduce lock hold times.
if (!SafepointSynchronize::is_synchronizing()) {
if (ObjectSynchronizer::quick_notify(obj, thread, false)) {
return true;
}
}
return false; // caller must perform slow path
JRT_END
// Object.notifyAll() fast path, caller does slow path
JRT_LEAF(jboolean, JVMCIRuntime::object_notifyAll(JavaThread *thread, oopDesc* obj))
if (!SafepointSynchronize::is_synchronizing() ) {
if (ObjectSynchronizer::quick_notify(obj, thread, true)) {
return true;
}
}
return false; // caller must perform slow path
JRT_END
JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message))
JRT_BLOCK;
TempNewSymbol symbol = SymbolTable::new_symbol(exception);
SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message);
JRT_BLOCK_END;
return caller_is_deopted();
JRT_END
JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass))
JRT_BLOCK;
ResourceMark rm(thread);
TempNewSymbol symbol = SymbolTable::new_symbol(exception);
SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, klass->external_name());
JRT_BLOCK_END;
return caller_is_deopted();
JRT_END
JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass))
JRT_BLOCK;
ResourceMark rm(thread);
const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass);
TempNewSymbol symbol = SymbolTable::new_symbol(exception);
SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message);
JRT_BLOCK_END;
return caller_is_deopted();
JRT_END
JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline))
ttyLocker ttyl;
if (obj == NULL) {
tty->print("NULL");
} else if (oopDesc::is_oop_or_null(obj, true) && (!as_string || !java_lang_String::is_instance(obj))) {
if (oopDesc::is_oop_or_null(obj, true)) {
char buf[O_BUFLEN];
tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj));
} else {
tty->print(INTPTR_FORMAT, p2i(obj));
}
} else {
ResourceMark rm;
assert(obj != NULL && java_lang_String::is_instance(obj), "must be");
char *buf = java_lang_String::as_utf8_string(obj);
tty->print_raw(buf);
}
if (newline) {
tty->cr();
}
JRT_END
#if INCLUDE_G1GC
JRT_LEAF(void, JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj))
G1ThreadLocalData::satb_mark_queue(thread).enqueue(obj);
JRT_END
JRT_LEAF(void, JVMCIRuntime::write_barrier_post(JavaThread* thread, void* card_addr))
G1ThreadLocalData::dirty_card_queue(thread).enqueue(card_addr);
JRT_END
#endif // INCLUDE_G1GC
JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child))
bool ret = true;
if(!Universe::heap()->is_in(parent)) {
tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent));
parent->print();
ret=false;
}
if(!Universe::heap()->is_in(child)) {
tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child));
child->print();
ret=false;
}
return (jint)ret;
JRT_END
JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* thread, jlong where, jlong format, jlong value))
ResourceMark rm;
const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where;
char *detail_msg = NULL;
if (format != 0L) {
const char* buf = (char*) (address) format;
size_t detail_msg_length = strlen(buf) * 2;
detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length);
jio_snprintf(detail_msg, detail_msg_length, buf, value);
}
report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg);
JRT_END
JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread))
oop exception = thread->exception_oop();
assert(exception != NULL, "npe");
thread->set_exception_oop(NULL);
thread->set_exception_pc(0);
return exception;
JRT_END
PRAGMA_DIAG_PUSH
PRAGMA_FORMAT_NONLITERAL_IGNORED
JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, const char* format, jlong v1, jlong v2, jlong v3))
ResourceMark rm;
tty->print(format, v1, v2, v3);
JRT_END
PRAGMA_DIAG_POP
static void decipher(jlong v, bool ignoreZero) {
if (v != 0 || !ignoreZero) {
void* p = (void *)(address) v;
CodeBlob* cb = CodeCache::find_blob(p);
if (cb) {
if (cb->is_nmethod()) {
char buf[O_BUFLEN];
tty->print("%s [" INTPTR_FORMAT "+" JLONG_FORMAT "]", cb->as_nmethod_or_null()->method()->name_and_sig_as_C_string(buf, O_BUFLEN), p2i(cb->code_begin()), (jlong)((address)v - cb->code_begin()));
return;
}
cb->print_value_on(tty);
return;
}
if (Universe::heap()->is_in(p)) {
oop obj = oop(p);
obj->print_value_on(tty);
return;
}
tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v);
}
}
PRAGMA_DIAG_PUSH
PRAGMA_FORMAT_NONLITERAL_IGNORED
JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3))
ResourceMark rm;
const char *buf = (const char*) (address) format;
if (vmError) {
if (buf != NULL) {
fatal(buf, v1, v2, v3);
} else {
fatal("<anonymous error>");
}
} else if (buf != NULL) {
tty->print(buf, v1, v2, v3);
} else {
assert(v2 == 0, "v2 != 0");
assert(v3 == 0, "v3 != 0");
decipher(v1, false);
}
JRT_END
PRAGMA_DIAG_POP
JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline))
union {
jlong l;
jdouble d;
jfloat f;
} uu;
uu.l = value;
switch (typeChar) {
case 'Z': tty->print(value == 0 ? "false" : "true"); break;
case 'B': tty->print("%d", (jbyte) value); break;
case 'C': tty->print("%c", (jchar) value); break;
case 'S': tty->print("%d", (jshort) value); break;
case 'I': tty->print("%d", (jint) value); break;
case 'F': tty->print("%f", uu.f); break;
case 'J': tty->print(JLONG_FORMAT, value); break;
case 'D': tty->print("%lf", uu.d); break;
default: assert(false, "unknown typeChar"); break;
}
if (newline) {
tty->cr();
}
JRT_END
JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* thread, oopDesc* obj))
return (jint) obj->identity_hash();
JRT_END
JRT_ENTRY(jint, JVMCIRuntime::test_deoptimize_call_int(JavaThread* thread, int value))
deopt_caller();
return (jint) value;
JRT_END
// private static JVMCIRuntime JVMCI.initializeRuntime()
JVM_ENTRY_NO_ENV(jobject, JVM_GetJVMCIRuntime(JNIEnv *env, jclass c))
JNI_JVMCIENV(thread, env);
if (!EnableJVMCI) {
JVMCI_THROW_MSG_NULL(InternalError, "JVMCI is not enabled");
}
JVMCIENV->runtime()->initialize_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
JVMCIObject runtime = JVMCIENV->runtime()->get_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
return JVMCIENV->get_jobject(runtime);
JVM_END
void JVMCIRuntime::call_getCompiler(TRAPS) {
THREAD_JVMCIENV(JavaThread::current());
JVMCIObject jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_CHECK);
initialize(JVMCIENV);
JVMCIENV->call_HotSpotJVMCIRuntime_getCompiler(jvmciRuntime, JVMCI_CHECK);
}
void JVMCINMethodData::initialize(
int nmethod_mirror_index,
const char* name,
FailedSpeculation** failed_speculations)
{
_failed_speculations = failed_speculations;
_nmethod_mirror_index = nmethod_mirror_index;
if (name != NULL) {
_has_name = true;
char* dest = (char*) this->name();
strcpy(dest, name);
} else {
_has_name = false;
}
}
void JVMCINMethodData::add_failed_speculation(nmethod* nm, jlong speculation) {
jlong index = speculation >> JVMCINMethodData::SPECULATION_LENGTH_BITS;
guarantee(index >= 0 && index <= max_jint, "Encoded JVMCI speculation index is not a positive Java int: " INTPTR_FORMAT, index);
int length = speculation & JVMCINMethodData::SPECULATION_LENGTH_MASK;
if (index + length > (uint) nm->speculations_size()) {
fatal(INTPTR_FORMAT "[index: " JLONG_FORMAT ", length: %d out of bounds wrt encoded speculations of length %u", speculation, index, length, nm->speculations_size());
}
address data = nm->speculations_begin() + index;
FailedSpeculation::add_failed_speculation(nm, _failed_speculations, data, length);
}
oop JVMCINMethodData::get_nmethod_mirror(nmethod* nm, bool phantom_ref) {
if (_nmethod_mirror_index == -1) {
return NULL;
}
if (phantom_ref) {
return nm->oop_at_phantom(_nmethod_mirror_index);
} else {
return nm->oop_at(_nmethod_mirror_index);
}
}
void JVMCINMethodData::set_nmethod_mirror(nmethod* nm, oop new_mirror) {
assert(_nmethod_mirror_index != -1, "cannot set JVMCI mirror for nmethod");
oop* addr = nm->oop_addr_at(_nmethod_mirror_index);
assert(new_mirror != NULL, "use clear_nmethod_mirror to clear the mirror");
assert(*addr == NULL, "cannot overwrite non-null mirror");
*addr = new_mirror;
// Since we've patched some oops in the nmethod,
// (re)register it with the heap.
MutexLocker ml(CodeCache_lock, Mutex::_no_safepoint_check_flag);
Universe::heap()->register_nmethod(nm);
}
void JVMCINMethodData::clear_nmethod_mirror(nmethod* nm) {
if (_nmethod_mirror_index != -1) {
oop* addr = nm->oop_addr_at(_nmethod_mirror_index);
*addr = NULL;
}
}
void JVMCINMethodData::invalidate_nmethod_mirror(nmethod* nm) {
oop nmethod_mirror = get_nmethod_mirror(nm, /* phantom_ref */ false);
if (nmethod_mirror == NULL) {
return;
}
// Update the values in the mirror if it still refers to nm.
// We cannot use JVMCIObject to wrap the mirror as this is called
// during GC, forbidding the creation of JNIHandles.
JVMCIEnv* jvmciEnv = NULL;
nmethod* current = (nmethod*) HotSpotJVMCI::InstalledCode::address(jvmciEnv, nmethod_mirror);
if (nm == current) {
if (!nm->is_alive()) {
// Break the link from the mirror to nm such that
// future invocations via the mirror will result in
// an InvalidInstalledCodeException.
HotSpotJVMCI::InstalledCode::set_address(jvmciEnv, nmethod_mirror, 0);
HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
} else if (nm->is_not_entrant()) {
// Zero the entry point so any new invocation will fail but keep
// the address link around that so that existing activations can
// be deoptimized via the mirror (i.e. JVMCIEnv::invalidate_installed_code).
HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
}
}
}
JVMCIRuntime::JVMCIRuntime(int id) {
_init_state = uninitialized;
_shared_library_javavm = NULL;
_id = id;
_metadata_handles = new MetadataHandles();
JVMCI_event_1("created new JVMCI runtime %d (" PTR_FORMAT ")", id, p2i(this));
}
// Handles to objects in the Hotspot heap.
static OopStorage* object_handles() {
return Universe::vm_global();
}
jobject JVMCIRuntime::make_global(const Handle& obj) {
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
assert(oopDesc::is_oop(obj()), "not an oop");
oop* ptr = object_handles()->allocate();
jobject res = NULL;
if (ptr != NULL) {
assert(*ptr == NULL, "invariant");
NativeAccess<>::oop_store(ptr, obj());
res = reinterpret_cast<jobject>(ptr);
} else {
vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR,
"Cannot create JVMCI oop handle");
}
MutexLocker ml(JVMCI_lock);
return res;
}
void JVMCIRuntime::destroy_global(jobject handle) {
// Assert before nulling out, for better debugging.
assert(is_global_handle(handle), "precondition");
oop* oop_ptr = reinterpret_cast<oop*>(handle);
NativeAccess<>::oop_store(oop_ptr, (oop)NULL);
object_handles()->release(oop_ptr);
MutexLocker ml(JVMCI_lock);
}
bool JVMCIRuntime::is_global_handle(jobject handle) {
const oop* ptr = reinterpret_cast<oop*>(handle);
return object_handles()->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY;
}
jmetadata JVMCIRuntime::allocate_handle(const methodHandle& handle) {
MutexLocker ml(JVMCI_lock);
return _metadata_handles->allocate_handle(handle);
}
jmetadata JVMCIRuntime::allocate_handle(const constantPoolHandle& handle) {
MutexLocker ml(JVMCI_lock);
return _metadata_handles->allocate_handle(handle);
}
void JVMCIRuntime::release_handle(jmetadata handle) {
MutexLocker ml(JVMCI_lock);
_metadata_handles->chain_free_list(handle);
}
// Function for redirecting shared library JavaVM output to tty
static void _log(const char* buf, size_t count) {
tty->write((char*) buf, count);
}
// Function for shared library JavaVM to flush tty
static void _flush_log() {
tty->flush();
}
// Function for shared library JavaVM to exit HotSpot on a fatal error
static void _fatal() {
fatal("Fatal error in JVMCI shared library");
}
JNIEnv* JVMCIRuntime::init_shared_library_javavm() {
JavaVM* javaVM = (JavaVM*) _shared_library_javavm;
if (javaVM == NULL) {
MutexLocker locker(JVMCI_lock);
// Check again under JVMCI_lock
javaVM = (JavaVM*) _shared_library_javavm;
if (javaVM != NULL) {
return NULL;
}
char* sl_path;
void* sl_handle = JVMCI::get_shared_library(sl_path, true);
jint (*JNI_CreateJavaVM)(JavaVM **pvm, void **penv, void *args);
typedef jint (*JNI_CreateJavaVM_t)(JavaVM **pvm, void **penv, void *args);
JNI_CreateJavaVM = CAST_TO_FN_PTR(JNI_CreateJavaVM_t, os::dll_lookup(sl_handle, "JNI_CreateJavaVM"));
if (JNI_CreateJavaVM == NULL) {
fatal("Unable to find JNI_CreateJavaVM in %s", sl_path);
}
ResourceMark rm;
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_2;
vm_args.ignoreUnrecognized = JNI_TRUE;
JavaVMOption options[4];
jlong javaVM_id = 0;
// Protocol: JVMCI shared library JavaVM should support a non-standard "_javavm_id"
// option whose extraInfo info field is a pointer to which a unique id for the
// JavaVM should be written.
options[0].optionString = (char*) "_javavm_id";
options[0].extraInfo = &javaVM_id;
options[1].optionString = (char*) "_log";
options[1].extraInfo = (void*) _log;
options[2].optionString = (char*) "_flush_log";
options[2].extraInfo = (void*) _flush_log;
options[3].optionString = (char*) "_fatal";
options[3].extraInfo = (void*) _fatal;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
JNIEnv* env = NULL;
int result = (*JNI_CreateJavaVM)(&javaVM, (void**) &env, &vm_args);
if (result == JNI_OK) {
guarantee(env != NULL, "missing env");
_shared_library_javavm = javaVM;
JVMCI_event_1("created JavaVM[%ld]@" PTR_FORMAT " for JVMCI runtime %d", javaVM_id, p2i(javaVM), _id);
return env;
} else {
fatal("JNI_CreateJavaVM failed with return value %d", result);
}
}
return NULL;
}
void JVMCIRuntime::init_JavaVM_info(jlongArray info, JVMCI_TRAPS) {
if (info != NULL) {
typeArrayOop info_oop = (typeArrayOop) JNIHandles::resolve(info);
if (info_oop->length() < 4) {
JVMCI_THROW_MSG(ArrayIndexOutOfBoundsException, err_msg("%d < 4", info_oop->length()));
}
JavaVM* javaVM = (JavaVM*) _shared_library_javavm;
info_oop->long_at_put(0, (jlong) (address) javaVM);
info_oop->long_at_put(1, (jlong) (address) javaVM->functions->reserved0);
info_oop->long_at_put(2, (jlong) (address) javaVM->functions->reserved1);
info_oop->long_at_put(3, (jlong) (address) javaVM->functions->reserved2);
}
}
#define JAVAVM_CALL_BLOCK \
guarantee(thread != NULL && _shared_library_javavm != NULL, "npe"); \
ThreadToNativeFromVM ttnfv(thread); \
JavaVM* javavm = (JavaVM*) _shared_library_javavm;
jint JVMCIRuntime::AttachCurrentThread(JavaThread* thread, void **penv, void *args) {
JAVAVM_CALL_BLOCK
return javavm->AttachCurrentThread(penv, args);
}
jint JVMCIRuntime::AttachCurrentThreadAsDaemon(JavaThread* thread, void **penv, void *args) {
JAVAVM_CALL_BLOCK
return javavm->AttachCurrentThreadAsDaemon(penv, args);
}
jint JVMCIRuntime::DetachCurrentThread(JavaThread* thread) {
JAVAVM_CALL_BLOCK
return javavm->DetachCurrentThread();
}
jint JVMCIRuntime::GetEnv(JavaThread* thread, void **penv, jint version) {
JAVAVM_CALL_BLOCK
return javavm->GetEnv(penv, version);
}
#undef JAVAVM_CALL_BLOCK \
void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
if (is_HotSpotJVMCIRuntime_initialized()) {
if (JVMCIENV->is_hotspot() && UseJVMCINativeLibrary) {
JVMCI_THROW_MSG(InternalError, "JVMCI has already been enabled in the JVMCI shared library");
}
}
initialize(JVMCIENV);
// This should only be called in the context of the JVMCI class being initialized
JVMCIObject result = JVMCIENV->call_HotSpotJVMCIRuntime_runtime(JVMCI_CHECK);
result = JVMCIENV->make_global(result);
OrderAccess::storestore(); // Ensure handle is fully constructed before publishing
_HotSpotJVMCIRuntime_instance = result;
JVMCI::_is_initialized = true;
}
void JVMCIRuntime::initialize(JVMCIEnv* JVMCIENV) {
// Check first without JVMCI_lock
if (_init_state == fully_initialized) {
return;
}
MutexLocker locker(JVMCI_lock);
// Check again under JVMCI_lock
if (_init_state == fully_initialized) {
return;
}
while (_init_state == being_initialized) {
JVMCI_event_1("waiting for initialization of JVMCI runtime %d", _id);
JVMCI_lock->wait();
if (_init_state == fully_initialized) {
JVMCI_event_1("done waiting for initialization of JVMCI runtime %d", _id);
return;
}
}
JVMCI_event_1("initializing JVMCI runtime %d", _id);
_init_state = being_initialized;
{
MutexUnlocker unlock(JVMCI_lock);
JavaThread* THREAD = JavaThread::current();
HandleMark hm(THREAD);
ResourceMark rm(THREAD);
if (JVMCIENV->is_hotspot()) {
HotSpotJVMCI::compute_offsets(CHECK_EXIT);
} else {
JNIAccessMark jni(JVMCIENV);
JNIJVMCI::initialize_ids(jni.env());
if (jni()->ExceptionCheck()) {
jni()->ExceptionDescribe();
fatal("JNI exception during init");
}
}
if (!JVMCIENV->is_hotspot()) {
JNIAccessMark jni(JVMCIENV, THREAD);
JNIJVMCI::register_natives(jni.env());
}
create_jvmci_primitive_type(T_BOOLEAN, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_BYTE, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_CHAR, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_SHORT, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_INT, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_LONG, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_FLOAT, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_DOUBLE, JVMCI_CHECK_EXIT_((void)0));
create_jvmci_primitive_type(T_VOID, JVMCI_CHECK_EXIT_((void)0));
if (!JVMCIENV->is_hotspot()) {
JVMCIENV->copy_saved_properties();
}
}
_init_state = fully_initialized;
JVMCI_event_1("initialized JVMCI runtime %d", _id);
JVMCI_lock->notify_all();
}
JVMCIObject JVMCIRuntime::create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS) {
Thread* THREAD = Thread::current();
// These primitive types are long lived and are created before the runtime is fully set up
// so skip registering them for scanning.
JVMCIObject mirror = JVMCIENV->get_object_constant(java_lang_Class::primitive_mirror(type), false, true);
if (JVMCIENV->is_hotspot()) {
JavaValue result(T_OBJECT);
JavaCallArguments args;
args.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(mirror)));
args.push_int(type2char(type));
JavaCalls::call_static(&result, HotSpotJVMCI::HotSpotResolvedPrimitiveType::klass(), vmSymbols::fromMetaspace_name(), vmSymbols::primitive_fromMetaspace_signature(), &args, CHECK_(JVMCIObject()));
return JVMCIENV->wrap(JNIHandles::make_local((oop)result.get_jobject()));
} else {
JNIAccessMark jni(JVMCIENV);
jobject result = jni()->CallStaticObjectMethod(JNIJVMCI::HotSpotResolvedPrimitiveType::clazz(),
JNIJVMCI::HotSpotResolvedPrimitiveType_fromMetaspace_method(),
mirror.as_jobject(), type2char(type));
if (jni()->ExceptionCheck()) {
return JVMCIObject();
}
return JVMCIENV->wrap(result);
}
}