-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathVMJ9.cpp
9664 lines (8321 loc) · 347 KB
/
VMJ9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#define J9_EXTERNAL_TO_VM
#if defined(J9VM_OPT_JITSERVER)
#include "env/VMJ9Server.hpp"
#endif /* defined(J9VM_OPT_JITSERVER) */
#include "env/VMJ9.h"
#include <algorithm>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include "bcnames.h"
#include "cache.h"
#include "fastJNI.h"
#include "j9cfg.h"
#include "j9comp.h"
#include "j9consts.h"
#include "j9jitnls.h"
#include "j9list.h"
#include "j9modron.h"
#include "j9port.h"
#include "j9protos.h"
#include "j9version.h"
#include "jilconsts.h"
#include "jitprotos.h"
#include "locknursery.h"
#include "stackwalk.h"
#include "thrtypes.h"
#include "vmaccess.h"
#undef min
#undef max
#include "codegen/AheadOfTimeCompile.hpp"
#include "codegen/CodeGenerator.hpp"
#include "env/KnownObjectTable.hpp"
#include "codegen/GCStackMap.hpp"
#include "codegen/GCStackAtlas.hpp"
#include "codegen/Instruction.hpp"
#include "codegen/Snippet.hpp"
#include "compile/Compilation.hpp"
#include "compile/CompilationTypes.hpp"
#include "compile/Method.hpp"
#include "compile/ResolvedMethod.hpp"
#include "compile/VirtualGuard.hpp"
#include "control/OptionsUtil.hpp"
#include "control/CompilationController.hpp"
#include "control/CompilationStrategy.hpp"
#include "env/StackMemoryRegion.hpp"
#include "compile/CompilationException.hpp"
#include "runtime/CodeCacheExceptions.hpp"
#include "exceptions/JITShutDown.hpp"
#include "exceptions/DataCacheError.hpp"
#include "exceptions/AOTFailure.hpp"
#include "env/exports.h"
#include "env/CompilerEnv.hpp"
#include "env/CHTable.hpp"
#include "env/ClassLoaderTable.hpp"
#include "env/IO.hpp"
#include "env/PersistentCHTable.hpp"
#include "env/PersistentInfo.hpp"
#include "env/jittypes.h"
#include "env/VMAccessCriticalSection.hpp"
#include "env/VerboseLog.hpp"
#include "infra/Monitor.hpp"
#include "infra/MonitorTable.hpp"
#include "il/DataTypes.hpp"
#include "il/J9DataTypes.hpp"
#include "il/LabelSymbol.hpp"
#include "il/MethodSymbol.hpp"
#include "il/Node.hpp"
#include "il/NodePool.hpp"
#include "il/Node_inlines.hpp"
#include "il/ParameterSymbol.hpp"
#include "il/RegisterMappedSymbol.hpp"
#include "il/ResolvedMethodSymbol.hpp"
#include "il/StaticSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "ilgen/IlGeneratorMethodDetails_inlines.hpp"
#include "ilgen/IlGenRequest.hpp"
#include "infra/SimpleRegex.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/TransformUtil.hpp"
#include "runtime/asmprotos.h"
#include "runtime/CodeCache.hpp"
#include "runtime/CodeCacheManager.hpp"
#include "runtime/codertinit.hpp"
#include "runtime/DataCache.hpp"
#include "runtime/HookHelpers.hpp"
#include "runtime/RelocationRuntime.hpp"
#include "control/CompilationRuntime.hpp"
#include "control/CompilationThread.hpp"
#include "control/MethodToBeCompiled.hpp"
#include "env/J2IThunk.hpp"
#include "env/j9fieldsInfo.h"
#include "env/j9method.h"
#include "env/ut_j9jit.h"
#include "optimizer/J9EstimateCodeSize.hpp"
#include "runtime/J9VMAccess.hpp"
#include "runtime/IProfiler.hpp"
#include "runtime/HWProfiler.hpp"
#include "runtime/J9Profiler.hpp"
#include "env/J9JitMemory.hpp"
#include "infra/Bit.hpp" //for trailingZeroes
#include "VMHelpers.hpp"
#include "env/JSR292Methods.h"
#include "infra/String.hpp"
#ifdef LINUX
#include <signal.h>
#endif
#ifdef J9_CODE_CACHE_RECLAMATION
#include "runtime/CodeCacheReclamation.h"
#endif
#if defined(_MSC_VER)
#include <malloc.h>
#endif
#ifndef J9_FINDKNOWNCLASS_FLAG_EXISTING_ONLY
#define J9_FINDKNOWNCLASS_FLAG_EXISTING_ONLY 2
#endif
#define PSEUDO_RANDOM_NUMBER_PREFIX "#num"
#define PSEUDO_RANDOM_STRING_PREFIX "#str"
#define PSEUDO_RANDOM_STRING_PREFIX_LEN 4
#define PSEUDO_RANDOM_SUFFIX '#'
#define REFERENCEFIELD "referent"
#define REFERENCEFIELDLEN 8
#define REFERENCERETURNTYPE "Ljava/lang/Object;"
#define REFERENCERETURNTYPELEN 18
#if SOLARIS || AIXPPC || LINUX
#include <strings.h>
#endif
#define notImplemented(A) TR_ASSERT(0, "TR_FrontEnd::%s is undefined", (A) )
#define FIELD_OFFSET_NOT_FOUND -1
struct TR_MethodToBeCompiled;
enum { IS_VOLATILE=1 };
extern "C" J9Method * getNewInstancePrototype(J9VMThread *context);
// psuedo-call to asm function
extern "C" void _getSTFLEBits(int numDoubleWords, uint64_t * bits); /* 390 asm stub */
extern "C" bool _isPSWInProblemState(); /* 390 asm stub */
TR::FILE *fileOpen(TR::Options *options, J9JITConfig *jitConfig, char *name, char *permission, bool b1)
{
PORT_ACCESS_FROM_ENV(jitConfig->javaVM);
char tmp[1025];
char *formattedTmp = NULL;
if (!options->getOption(TR_EnablePIDExtension))
{
formattedTmp = TR_J9VMBase::getJ9FormattedName(jitConfig, PORTLIB, tmp, sizeof(tmp), name, NULL, false);
}
else
{
formattedTmp = TR_J9VMBase::getJ9FormattedName(jitConfig, PORTLIB, tmp, sizeof(tmp), name, options->getSuffixLogsFormat(), true);
}
if (NULL != formattedTmp)
{
return j9jit_fopen(formattedTmp, permission, b1);
}
return NULL;
}
// Returns -1 if given vmThread is not a compilation thread
int32_t
TR_J9VMBase::getCompThreadIDForVMThread(void *vmThread)
{
int32_t id = -1; // unknown
if (vmThread)
{
if (_vmThread == (J9VMThread*)vmThread)
{
if (_vmThreadIsCompilationThread == TR_yes)
{
TR_ASSERT(_compInfoPT, "if this is a verified fe for a comp thread, _compInfoPT must exist");
id = _compInfoPT->getCompThreadId();
}
else if (_vmThreadIsCompilationThread == TR_maybe) // Let's find out the compilation thread and cache it
{
_compInfoPT = _compInfo->getCompInfoForThread((J9VMThread*)vmThread);
id = _compInfoPT ? _compInfoPT->getCompThreadId() : -1;
}
}
else // Thread given as parameter is unrelated to this frontEnd
{
TR::CompilationInfoPerThread * cipt = _compInfo->getCompInfoForThread((J9VMThread*)vmThread);
id = (cipt ? cipt->getCompThreadId() : -1);
}
}
return id;
}
bool
TR_J9VM::stackWalkerMaySkipFrames(TR_OpaqueMethodBlock *method, TR_OpaqueClassBlock *methodClass)
{
if(!method)
return false;
TR::VMAccessCriticalSection stackWalkerMaySkipFrames(this);
// Maybe I should be using isSameMethod ?
if (vmThread()->javaVM->jlrMethodInvoke == NULL || ((J9Method *)method) == vmThread()->javaVM->jlrMethodInvoke )
{
return true;
}
if(!methodClass)
{
return false;
}
#if defined(J9VM_OPT_SIDECAR)
if ( ( vmThread()->javaVM->srMethodAccessor != NULL) &&
TR_J9VM::isInstanceOf(methodClass, (TR_OpaqueClassBlock*) J9VM_J9CLASS_FROM_JCLASS(vmThread(), vmThread()->javaVM->srMethodAccessor), false) )
{
return true;
}
if ( ( vmThread()->javaVM->srConstructorAccessor != NULL) &&
TR_J9VM::isInstanceOf(methodClass, (TR_OpaqueClassBlock*) J9VM_J9CLASS_FROM_JCLASS(vmThread(), vmThread()->javaVM->srConstructorAccessor), false) )
{
return true;
}
#endif // J9VM_OPT_SIDECAR
return false;
}
bool
TR_J9VMBase::isMethodBreakpointed(TR_OpaqueMethodBlock *method)
{
return jitIsMethodBreakpointed(vmThread(), (J9Method *)method);
}
bool
TR_J9VMBase::instanceOfOrCheckCast(J9Class *instanceClass, J9Class* castClass)
{
return ::instanceOfOrCheckCast(instanceClass, castClass);
}
bool
TR_J9VMBase::instanceOfOrCheckCastNoCacheUpdate(J9Class *instanceClass, J9Class* castClass)
{
return ::instanceOfOrCheckCastNoCacheUpdate(instanceClass, castClass);
}
// Points to address: what if vmThread is not the compilation thread.
// What if the compilation thread does not have the classUnloadMonitor.
// What if jitConfig does not exist.
// What if we already have the compilationShouldBeInterrupted flag set.
// How do we set the error code when we throw an exception
// IMPORTANT: acquireVMAccessIfNeeded could throw an exception,
// hence it is important to call this within a try block.
bool acquireVMaccessIfNeeded(J9VMThread *vmThread, TR_YesNoMaybe isCompThread)
{
bool haveAcquiredVMAccess = false;
if (TR::Options::getCmdLineOptions() == 0 || // if options haven't been created yet, there is no danger
TR::Options::getCmdLineOptions()->getOption(TR_DisableNoVMAccess))
return false; // don't need to acquire VM access
// we need to test if the thread has VM access
TR_ASSERT(vmThread, "vmThread must be not null");
// We need to acquire VMaccess only for the compilation thread
if (isCompThread == TR_no)
{
TR_ASSERT((vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS), "Must have vm access if this is not a compilation thread");
return false;
}
// scan all compilation threads
J9JITConfig *jitConfig = vmThread->javaVM->jitConfig;
TR::CompilationInfo *compInfo = TR::CompilationInfo::get(jitConfig);
TR::CompilationInfoPerThread *compInfoPT = compInfo->getCompInfoForThread(vmThread);
// We need to acquire VMaccess only for the compilation thread
if (isCompThread == TR_maybe)
{
if (!compInfoPT)
{
TR_ASSERT((vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS), "Must have vm access if this is not a compilation thread");
return false;
}
}
// At this point we know we deal with a compilation thread
TR_ASSERT(compInfoPT, "A compilation thread must have an associated compilationInfo");
if (!(vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS)) // I don't already have VM access
{
if (0 == vmThread->javaVM->internalVMFunctions->internalTryAcquireVMAccessWithMask(vmThread, J9_PUBLIC_FLAGS_HALT_THREAD_ANY_NO_JAVA_SUSPEND))
{
haveAcquiredVMAccess = true;
}
else // the GC is having exclusive VM access
{
// compilationShouldBeInterrupted flag is reset by the compilation
// thread when a new compilation starts.
// must test if we have the class unload monitor
//vmThread->osThread == classUnloadMonitor->owner()
bool hadClassUnloadMonitor = false;
#if !defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
if (TR::Options::getCmdLineOptions()->getOption(TR_EnableHCR) || TR::Options::getCmdLineOptions()->getOption(TR_FullSpeedDebug))
#endif
hadClassUnloadMonitor = TR::MonitorTable::get()->readReleaseClassUnloadMonitor(compInfoPT->getCompThreadId()) >= 0;
#if defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
// We must have had classUnloadMonitor by the way we architected the application
TR_ASSERT(hadClassUnloadMonitor, "Comp thread must hold classUnloadMonitor when compiling without VMaccess");
#endif
//--- GC CAN INTERVENE HERE ---
// fprintf(stderr, "Have released class unload monitor temporarily\n"); fflush(stderr);
// At this point we must not hold any JIT monitors that can also be accessed by the GC
// As we don't know how the GC code will be modified in the future we will
// scan the entire list of known monitors
#if defined(DEBUG) || defined(PROD_WITH_ASSUMES)
TR::Monitor * heldMonitor = TR::MonitorTable::get()->monitorHeldByCurrentThread();
TR_ASSERT(!heldMonitor, "Current thread must not hold TR::Monitor %p called %s when trying to acquire VM access\n",
heldMonitor, TR_J9VMBase::get(jitConfig, NULL)->getJ9MonitorName((J9ThreadMonitor*)heldMonitor->getVMMonitor()));
#endif // #if defined(DEBUG) || defined(PROD_WITH_ASSUMES)
TR_ASSERT_FATAL(!compInfo->getCompilationMonitor()->owned_by_self(),
"Current VM thread [%p] holds the comp monitor [%p] while attempting to acquire VM access", vmThread, compInfo->getCompilationMonitor());
TR::Compilation *comp = compInfoPT->getCompilation();
if ((comp && comp->getOptions()->realTimeGC()) ||
TR::Options::getCmdLineOptions()->realTimeGC())
compInfoPT->waitForGCCycleMonitor(false); // used only for real-time
acquireVMAccessNoSuspend(vmThread); // blocking. Will wait for the entire GC
if (hadClassUnloadMonitor)
{
TR::MonitorTable::get()->readAcquireClassUnloadMonitor(compInfoPT->getCompThreadId());
//fprintf(stderr, "Have acquired class unload monitor again\n"); fflush(stderr);
}
// Now we can check if the GC has done some unloading or redefinition happened
if (compInfoPT->compilationShouldBeInterrupted())
{
// bail out
// fprintf(stderr, "Released classUnloadMonitor and will throw an exception because GC unloaded classes\n"); fflush(stderr);
//TR::MonitorTable::get()->readReleaseClassUnloadMonitor(0); // Main code should do it.
// releaseVMAccess(vmThread);
if (comp)
{
comp->failCompilation<TR::CompilationInterrupted>("Compilation interrupted by GC unloading classes");
}
else // I am not sure we are in a compilation; better release the monitor
{
if (hadClassUnloadMonitor)
TR::MonitorTable::get()->readReleaseClassUnloadMonitor(compInfoPT->getCompThreadId()); // Main code should do it.
throw TR::CompilationInterrupted();
}
}
else // GC did not do any unloading
{
haveAcquiredVMAccess = true;
}
}
}
/*
* At shutdown time the compilation thread executes Java code and it may receive a sample (see D174900)
* Only abort the compilation if we're explicitly prepared to handle it.
*/
if (compInfoPT->compilationShouldBeInterrupted())
{
TR_ASSERT(compInfoPT->compilationShouldBeInterrupted() != GC_COMP_INTERRUPT, "GC should not have cut in _compInfoPT=%p\n", compInfoPT);
// in prod builds take some corrective action
throw J9::JITShutdown();
}
return haveAcquiredVMAccess;
}
void releaseVMaccessIfNeeded(J9VMThread *vmThread, bool haveAcquiredVMAccess)
{
if (haveAcquiredVMAccess)
{
TR_ASSERT((vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS), "Must have VM access"); // I don't already have VM access
releaseVMAccess(vmThread);
}
}
bool
TR_J9VMBase::isAnyMethodTracingEnabled(TR_OpaqueMethodBlock *method)
{
return isMethodTracingEnabled(method);
}
int32_t * TR_J9VMBase::staticStringEnableCompressionFieldAddr = NULL;
bool
TR_J9VMBase::releaseClassUnloadMonitorAndAcquireVMaccessIfNeeded(TR::Compilation *comp, bool *hadClassUnloadMonitor)
{
// When allocating a new code cache we must not hold any monitor because
// of deadlock possibilities
// This must be executed on the compilation thread so options must exist at this point
TR_ASSERT(comp, "Compilation object must always be given as parameter\n");
*hadClassUnloadMonitor = false;
bool hadVMAccess = true;
if (!TR::Options::getCmdLineOptions()->getOption(TR_DisableNoVMAccess))
{
TR_ASSERT(_vmThreadIsCompilationThread != TR_no, "releaseClassUnloadMonitorAndAcquireVMaccessIfNeeded should only be called for compilation threads\n");
// We need to acquire VMaccess only for the compilation thread
TR_ASSERT(vmThread()==_jitConfig->javaVM->internalVMFunctions->currentVMThread(_jitConfig->javaVM),
"fe for thread %p is used on thread %p", vmThread(),
_jitConfig->javaVM->internalVMFunctions->currentVMThread(_jitConfig->javaVM));
TR_ASSERT(_compInfoPT, "_compInfoPT must exist here\n");
TR_ASSERT(_compInfo->getCompInfoForThread(vmThread()) == _compInfoPT, "assertion failure");
if (_vmThreadIsCompilationThread == TR_maybe)
{
_vmThreadIsCompilationThread = TR_yes;
}
#if defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
*hadClassUnloadMonitor = TR::MonitorTable::get()->readReleaseClassUnloadMonitor(_compInfoPT->getCompThreadId()) >= 0;
TR_ASSERT(*hadClassUnloadMonitor, "releaseClassUnloadMonitorAndAcquireVMaccessIfNeeded without classUnloadMonitor\n");
#else
// We need to use classUnloadMonitor to stop the Compilation Thread when a class is redefined
if (TR::Options::getCmdLineOptions()->getOption(TR_EnableHCR) || TR::Options::getCmdLineOptions()->getOption(TR_FullSpeedDebug))
{
*hadClassUnloadMonitor = TR::MonitorTable::get()->readReleaseClassUnloadMonitor(_compInfoPT->getCompThreadId()) >= 0;
TR_ASSERT(*hadClassUnloadMonitor, "releaseClassUnloadMonitorAndAcquireVMaccessIfNeeded without classUnloadMonitor\n");
}
#endif
if (!(vmThread()->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS)) // I don't already have VM access
{
hadVMAccess = false;
#if defined(DEBUG) || defined(PROD_WITH_ASSUMES)
TR::Monitor * heldMonitor = TR::MonitorTable::get()->monitorHeldByCurrentThread();
TR_ASSERT(!heldMonitor, "Current thread must not hold TR::Monitor %p called %s when trying to acquire VM access\n",
heldMonitor, getJ9MonitorName((J9ThreadMonitor*)heldMonitor->getVMMonitor()));
#endif // #if defined(DEBUG) || defined(PROD_WITH_ASSUMES)
//--- GC can happen here ---
acquireVMAccessNoSuspend(vmThread());
// If the GC unloaded some classes, or HCR happened we must abort this compilation
if (_compInfoPT->compilationShouldBeInterrupted())
{
//releaseVMAccess(vmThread()); // release VM access before blocking on the next acquire operation
//if (*hadClassUnloadMonitor)
// TR::MonitorTable::get()->readAcquireClassUnloadMonitor(_compInfoPT->getCompThreadId());
comp->failCompilation<TR::CompilationInterrupted>("Compilation interrupted");
// After the exception throw we will release the classUnloadMonitor and reacquire the VM access
}
}
else
{
hadVMAccess = true;
}
}
return hadVMAccess;
}
void
TR_J9VMBase::acquireClassUnloadMonitorAndReleaseVMAccessIfNeeded(TR::Compilation *comp, bool hadVMAccess, bool hadClassUnloadMonitor)
{
if (TR::Options::getCmdLineOptions() && // if options haven't been created yet, there is no danger
!TR::Options::getCmdLineOptions()->getOption(TR_DisableNoVMAccess))
{
TR_ASSERT(vmThread()->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS, "vmThread must have vmAccess at this point");
if (_compInfoPT->compilationShouldBeInterrupted())
{
comp->failCompilation<TR::CompilationInterrupted>("Compilation interrupted");
}
#if defined(J9VM_GC_DYNAMIC_CLASS_UNLOADING)
if (hadClassUnloadMonitor)
TR::MonitorTable::get()->readAcquireClassUnloadMonitor(_compInfoPT->getCompThreadId());
#else
// We need to use classUnloadMonitor to stop the Compilation Thread when a class is redefined
if ((TR::Options::getCmdLineOptions()->getOption(TR_EnableHCR) || TR::Options::getCmdLineOptions()->getOption(TR_FullSpeedDebug)) && hadClassUnloadMonitor)
TR::MonitorTable::get()->readAcquireClassUnloadMonitor(_compInfoPT->getCompThreadId());
#endif // J9VM_GC_DYNAMIC_CLASS_UNLOADING
if (!hadVMAccess)
releaseVMAccess(vmThread());
}
}
bool
TR_J9SharedCacheVM::shouldDelayAotLoad()
{
return isAOT_DEPRECATED_DO_NOT_USE();
}
bool
TR_J9SharedCacheVM::isResolvedDirectDispatchGuaranteed(TR::Compilation *comp)
{
return isAotResolvedDirectDispatchGuaranteed(comp);
}
bool
TR_J9VMBase::isAotResolvedDirectDispatchGuaranteed(TR::Compilation *comp)
{
return comp->getOption(TR_UseSymbolValidationManager)
&& comp->cg()->guaranteesResolvedDirectDispatchForSVM();
}
bool
TR_J9SharedCacheVM::isResolvedVirtualDispatchGuaranteed(TR::Compilation *comp)
{
return isAotResolvedVirtualDispatchGuaranteed(comp);
}
bool
TR_J9VMBase::isAotResolvedVirtualDispatchGuaranteed(TR::Compilation *comp)
{
return comp->getOption(TR_UseSymbolValidationManager)
&& comp->cg()->guaranteesResolvedVirtualDispatchForSVM();
}
J9Class *
TR_J9VMBase::matchRAMclassFromROMclass(J9ROMClass * clazz, TR::Compilation * comp)
{
TR::VMAccessCriticalSection matchRAMclassFromROMclass(this);
J9UTF8 *utf8 = J9ROMCLASS_CLASSNAME(clazz);
J9Class *ramClass = jitGetClassInClassloaderFromUTF8(vmThread(), ((TR_ResolvedJ9Method *)comp->getCurrentMethod())->getClassLoader(),
(char *) J9UTF8_DATA(utf8), J9UTF8_LENGTH(utf8));
if (!ramClass)
{
ramClass = jitGetClassInClassloaderFromUTF8(vmThread(), (J9ClassLoader *) vmThread()->javaVM->systemClassLoader,
(char *) J9UTF8_DATA(utf8), J9UTF8_LENGTH(utf8));
}
return ramClass;
}
J9VMThread *
TR_J9VMBase::getCurrentVMThread()
{
return _vmThread ? _vmThread : _jitConfig->javaVM->internalVMFunctions->currentVMThread(_jitConfig->javaVM);
}
bool
TR_J9VMBase::createGlobalFrontEnd(J9JITConfig * jitConfig, TR::CompilationInfo * compInfo)
{
TR_ASSERT(!jitConfig->compilationInfo, "Global front end already exists");
TR_J9VM * vmWithoutThreadInfo = 0;
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
TR_ASSERT(!jitConfig->aotCompilationInfo, "Global AOT front end already exists");
TR_J9SharedCacheVM * aotVMWithoutThreadInfo = 0;
#endif
try
{
vmWithoutThreadInfo = new (PERSISTENT_NEW) TR_J9VM(jitConfig, compInfo, NULL);
if (!vmWithoutThreadInfo) throw std::bad_alloc();
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
try
{
aotVMWithoutThreadInfo = new (PERSISTENT_NEW) TR_J9SharedCacheVM(jitConfig, compInfo, NULL);
if (!aotVMWithoutThreadInfo) throw std::bad_alloc();
}
catch (...)
{
vmWithoutThreadInfo->~TR_J9VM();
jitPersistentFree(vmWithoutThreadInfo);
throw;
}
#endif
}
catch (...)
{
return false;
}
jitConfig->compilationInfo = vmWithoutThreadInfo;
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
jitConfig->aotCompilationInfo = aotVMWithoutThreadInfo;
#endif
return true;
}
TR_J9VMBase *
TR_J9VMBase::get(J9JITConfig * jitConfig, J9VMThread * vmThread, VM_TYPE vmType)
{
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT) && !defined (J9VM_OPT_SHARED_CLASSES)
#error "Unsupported AOT configuration"
#endif
TR_ASSERT(vmThread || vmType==DEFAULT_VM, "Specific VM type ==> must supply vmThread");
TR_J9VMBase * vmWithoutThreadInfo = static_cast<TR_J9VMBase *>(jitConfig->compilationInfo);
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
TR_J9VMBase * aotVMWithoutThreadInfo = static_cast<TR_J9VMBase *>(jitConfig->aotCompilationInfo);
#endif
if (vmThread)
{
// Check if this thread has cached the frontend inside
#if defined(J9VM_OPT_JITSERVER)
if (vmType==J9_SERVER_VM || vmType==J9_SHARED_CACHE_SERVER_VM)
{
TR_ASSERT(vmWithoutThreadInfo->_compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::SERVER, "J9_SERVER_VM and J9_SHARED_CACHE_SERVER_VM should only be instantiated in JITServer::SERVER mode");
TR::CompilationInfoPerThread *compInfoPT = NULL;
// Get the compInfoPT from the cached J9_VM with thread info
// or search using the compInfo from the J9_VM without thread info
if (vmThread->jitVMwithThreadInfo)
{
TR_J9VMBase * vmWithThreadInfo = static_cast<TR_J9VMBase *>(vmThread->jitVMwithThreadInfo);
compInfoPT = vmWithThreadInfo->_compInfoPT;
}
if (!compInfoPT && vmWithoutThreadInfo->_compInfo)
{
compInfoPT = vmWithoutThreadInfo->_compInfo->getCompInfoForThread(vmThread);
}
TR_ASSERT(compInfoPT, "Tried to create a TR_J9ServerVM without compInfoPT");
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
if (vmType==J9_SHARED_CACHE_SERVER_VM)
{
TR_J9SharedCacheServerVM *sharedCacheServerVM = compInfoPT->getSharedCacheServerVM();
if (!sharedCacheServerVM)
{
PORT_ACCESS_FROM_JITCONFIG(jitConfig);
void * alloc = j9mem_allocate_memory(sizeof(TR_J9SharedCacheServerVM), J9MEM_CATEGORY_JIT);
if (alloc)
sharedCacheServerVM = new (alloc) TR_J9SharedCacheServerVM(jitConfig, vmWithoutThreadInfo->_compInfo, vmThread);
if (sharedCacheServerVM)
{
sharedCacheServerVM->_vmThreadIsCompilationThread = TR_yes;
sharedCacheServerVM->_compInfoPT = compInfoPT;
compInfoPT->setSharedCacheServerVM(sharedCacheServerVM);
}
else
throw std::bad_alloc();
}
return sharedCacheServerVM;
}
else
#endif /* defined(J9VM_INTERP_AOT_COMPILE_SUPPORT) */
{
#if !defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
TR_ASSERT((vmType != J9_SHARED_CACHE_SERVER_VM), "vmType cannot be J9_SHARED_CACHE_SERVER_VM when J9VM_INTERP_AOT_COMPILE_SUPPORT is disabled");
#endif /* !defined(J9VM_INTERP_AOT_COMPILE_SUPPORT) */
TR_J9ServerVM *serverVM = compInfoPT->getServerVM();
if (!serverVM)
{
PORT_ACCESS_FROM_JITCONFIG(jitConfig);
void * alloc = j9mem_allocate_memory(sizeof(TR_J9ServerVM), J9MEM_CATEGORY_JIT);
if (alloc)
serverVM = new (alloc) TR_J9ServerVM(jitConfig, vmWithoutThreadInfo->_compInfo, vmThread);
if (serverVM)
{
serverVM->_vmThreadIsCompilationThread = TR_yes;
serverVM->_compInfoPT = compInfoPT;
compInfoPT->setServerVM(serverVM);
}
else
throw std::bad_alloc();
}
return serverVM;
}
}
#endif /* defined(J9VM_OPT_JITSERVER) */
#if defined(J9VM_INTERP_AOT_COMPILE_SUPPORT)
if (vmType==AOT_VM)
{
TR_J9VMBase * aotVMWithThreadInfo = static_cast<TR_J9VMBase *>(vmThread->aotVMwithThreadInfo);
if (!aotVMWithThreadInfo)
{
PORT_ACCESS_FROM_JITCONFIG(jitConfig);
void * alloc = j9mem_allocate_memory(sizeof(TR_J9SharedCacheVM), J9MEM_CATEGORY_JIT);
if (alloc)
{
aotVMWithThreadInfo = new (alloc) TR_J9SharedCacheVM(jitConfig, vmWithoutThreadInfo->_compInfo, vmThread);
}
if (aotVMWithThreadInfo)
{
vmThread->aotVMwithThreadInfo = aotVMWithThreadInfo;
if (vmWithoutThreadInfo->_compInfo)
{
TR::CompilationInfoPerThread *compInfoPT = vmWithoutThreadInfo->_compInfo->getCompInfoForThread(vmThread);
aotVMWithThreadInfo->_vmThreadIsCompilationThread = (compInfoPT ? TR_yes : TR_no);
aotVMWithThreadInfo->_compInfoPT = compInfoPT;
}
}
else
aotVMWithThreadInfo = aotVMWithoutThreadInfo; // may be incorrect because they are of different type
}
return aotVMWithThreadInfo;
}
else // We need to create a J9_VM
#endif /* defined(J9VM_INTERP_AOT_COMPILE_SUPPORT) */
{
TR_J9VMBase * vmWithThreadInfo = (TR_J9VMBase *)vmThread->jitVMwithThreadInfo;
TR_ASSERT(vmType==DEFAULT_VM || vmType==J9_VM, "assertion failure");
if (!vmWithThreadInfo) // This thread has not cached a frontend
{
PORT_ACCESS_FROM_JITCONFIG(jitConfig);
void * alloc = j9mem_allocate_memory(sizeof(TR_J9VM), J9MEM_CATEGORY_JIT);
if (alloc)
{
vmWithThreadInfo = new (alloc) TR_J9VM(jitConfig, vmWithoutThreadInfo->_compInfo, vmThread); // allocate the frontend
}
if (vmWithThreadInfo)
{
vmThread->jitVMwithThreadInfo = vmWithThreadInfo; // cache it
// Cache ths compilation thread as well
if (vmWithoutThreadInfo->_compInfo)
{
TR::CompilationInfoPerThread *compInfoPT = vmWithoutThreadInfo->_compInfo->getCompInfoForThread(vmThread);
vmWithThreadInfo->_vmThreadIsCompilationThread = (compInfoPT ? TR_yes : TR_no);
vmWithThreadInfo->_compInfoPT = compInfoPT;
}
}
else
vmWithThreadInfo = vmWithoutThreadInfo;
}
return vmWithThreadInfo;
}
}
return vmWithoutThreadInfo;
}
TR_J9VMBase::TR_J9VMBase(
J9JITConfig * jitConfig,
TR::CompilationInfo * compInfo,
J9VMThread * vmThread
)
: TR_FrontEnd(),
_vmThread(vmThread),
_portLibrary(jitConfig->javaVM->portLibrary),
_jitConfig(jitConfig),
_vmFunctionTable(jitConfig->javaVM->internalVMFunctions),
_compInfo(compInfo),
_iProfiler(0),
_hwProfilerShouldNotProcessBuffers(TR::Options::_hwProfilerRIBufferProcessingFrequency),
_bufferStart(NULL),
_vmThreadIsCompilationThread(TR_maybe),
_compInfoPT(NULL),
_shouldSleep(false)
{
for (int32_t i = 0; i < UT_MODULE_INFO.count; ++i)
if (UT_ACTIVE[i])
{
setTraceIsEnabled(true);
break;
}
if (TR::Options::getCmdLineOptions() && TR::Options::getCmdLineOptions()->getOption(TR_FullSpeedDebug))
setFSDIsEnabled(true);
_sharedCache = NULL;
if (TR::Options::sharedClassCache()
#if defined(J9VM_OPT_JITSERVER)
|| (compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::SERVER)
#endif /* defined(J9VM_OPT_JITSERVER) */
)
// shared classes and AOT must be enabled, or we should be on the JITServer with remote AOT enabled
{
#if defined(J9VM_OPT_JITSERVER)
if (compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::SERVER)
{
_sharedCache = new (compInfo->persistentMemory()) TR_J9JITServerSharedCache(this);
}
else
#endif /* defined(J9VM_OPT_JITSERVER) */
{
_sharedCache = new (PERSISTENT_NEW) TR_J9SharedCache(this);
}
if (!_sharedCache)
{
TR::Options::getAOTCmdLineOptions()->setOption(TR_NoStoreAOT);
TR::Options::getAOTCmdLineOptions()->setOption(TR_NoLoadAOT);
static_cast<TR_JitPrivateConfig *>(jitConfig->privateConfig)->aotValidHeader = TR_no;
TR_J9SharedCache::setSharedCacheDisabledReason(TR_J9SharedCache::J9_SHARED_CACHE_FAILED_TO_ALLOCATE);
}
else
{
TR_PersistentMemory * persistentMemory = (TR_PersistentMemory *)(jitConfig->scratchSegment);
TR_PersistentClassLoaderTable *loaderTable = persistentMemory->getPersistentInfo()->getPersistentClassLoaderTable();
_sharedCache->setPersistentClassLoaderTable(loaderTable);
}
}
}
void
TR_J9VMBase::freeSharedCache()
{
if (_sharedCache) // shared classes and AOT must be enabled
{
#if defined(J9VM_OPT_JITSERVER)
if (_compInfo && (_compInfo->getPersistentInfo()->getRemoteCompilationMode() != JITServer::SERVER))
#endif /* defined(J9VM_OPT_JITSERVER) */
{
TR_ASSERT(TR::Options::sharedClassCache(), "Found shared cache with option disabled");
}
jitPersistentFree(_sharedCache);
_sharedCache = NULL;
}
}
TR::CompilationInfo * getCompilationInfo(J9JITConfig * jitConfig)
{
return TR::CompilationInfo::get(jitConfig);
}
J9VMThread *
TR_J9VMBase::vmThread()
{
TR_ASSERT(_vmThread, "TR_J9VMBase::vmThread() TR_J9VMBase was created without thread information");
return _vmThread;
}
void
TR_J9VMBase::acquireCompilationLock()
{
if (_compInfo)
_compInfo->acquireCompilationLock();
}
void
TR_J9VMBase::releaseCompilationLock()
{
if (_compInfo)
_compInfo->releaseCompilationLock();
}
void
TR_J9VMBase::acquireLogMonitor()
{
if (_compInfo)
_compInfo->acquireLogMonitor();
}
void
TR_J9VMBase::releaseLogMonitor()
{
if (_compInfo)
_compInfo->releaseLogMonitor();
}
bool
TR_J9VMBase::isAsyncCompilation()
{
return _compInfo ? _compInfo->asynchronousCompilation() : false;
}
uintptr_t
TR_J9VMBase::getProcessID()
{
PORT_ACCESS_FROM_ENV(_jitConfig->javaVM);
TR::VMAccessCriticalSection getProcessID(this);
uintptr_t result = j9sysinfo_get_pid();
return result;
}
// static method
char *
TR_J9VMBase::getJ9FormattedName(
J9JITConfig *jitConfig,
J9PortLibrary *portLibrary,
char *buf,
size_t bufLength,
char *name,
char *format,
bool suffix)
{
PORT_ACCESS_FROM_ENV(jitConfig->javaVM);
J9VMThread *vmThread = jitConfig->javaVM->internalVMFunctions->currentVMThread(jitConfig->javaVM);
I_64 curTime = j9time_current_time_millis();
J9StringTokens *tokens = j9str_create_tokens(curTime);
if (tokens == NULL)
{
return NULL;
}
char tmp[1025];
size_t nameLength = strlen(name);
uintptr_t substLength = j9str_subst_tokens(tmp, sizeof(tmp), name, tokens);
if (substLength >= std::min(sizeof(tmp), bufLength))
{
j9str_free_tokens(tokens);
return NULL; // not enough room for the name or the token expansion
}
if (strcmp(tmp, name) != 0) // only append if there isn't a format specifier
{
memcpy(buf, tmp, substLength + 1); // +1 to get the null terminator
}
else
{
memcpy(buf, name, nameLength);
char *suffixBuf = &buf[nameLength];
if (format)
j9str_subst_tokens(suffixBuf, bufLength - nameLength, format, tokens);
else if (suffix)
{
// We have to break the string up to prevent CMVC keyword expansion
j9str_subst_tokens(suffixBuf, bufLength - nameLength, ".%Y" "%m" "%d." "%H" "%M" "%S.%pid", tokens);
}
else
{
buf = name;
}
}
j9str_free_tokens(tokens);
return buf;
}
char *
TR_J9VMBase::getFormattedName(char *buf, int32_t bufLength, char *name, char *format, bool suffix)
{
return getJ9FormattedName(_jitConfig, _portLibrary, buf, bufLength, name, format, suffix);
}
void
TR_J9VMBase::invalidateCompilationRequestsForUnloadedMethods(TR_OpaqueClassBlock *clazz, bool hotCodeReplacement)
{
// Only called from jitHookClassUnload so we don't need to acquire VM access
if (_compInfo)
_compInfo->invalidateRequestsForUnloadedMethods(clazz, vmThread(), hotCodeReplacement);
}
// -----------------------------------------------------------------------------
void
TR_J9VM::initializeHasResumableTrapHandler()
{
TR_ASSERT(_compInfo,"compInfo not defined");
if(!TR::Options::getCmdLineOptions()->getOption(TR_NoResumableTrapHandler) &&
portLibCall_sysinfo_has_resumable_trap_handler())
{
_compInfo->setHasResumableTrapHandler();
}
else
{
// In the case where the user specified -Xrs initializeHasResumableTrapHandler
// will be called twice and we must unset the flag as it will have been set on
// during the first call.
_compInfo->setHasResumableTrapHandler(false);
}
}
bool
TR_J9VMBase::hasResumableTrapHandler()
{
return _compInfo ? _compInfo->getHasResumableTrapHandler() : false;
}