-
Notifications
You must be signed in to change notification settings - Fork 746
/
Copy pathredirector.c
1554 lines (1365 loc) · 46 KB
/
redirector.c
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 2001
*
* 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
*******************************************************************************/
#if 0
#define DEBUG
#endif
#ifdef WIN32
#include <windows.h>
#include <tchar.h>
#include <io.h>
#endif /* WIN32 */
#include "j9.h"
#include "jni.h"
#include "exelib_api.h"
#include "j9exelibnls.h"
#include "j9arch.h"
#include "jvminit.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#if !defined(WIN32)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#if defined(J9ZOS39064)
#include "omrutil.h"
#include "omriarv64.h"
#endif /* defined(J9ZOS39064) */
extern void lookupJVMFunctions(void *vmdll);
typedef jint (JNICALL *CreateVM)(JavaVM**, void**, void*);
typedef jint (JNICALL *InitArgs)(void*);
typedef jint (JNICALL *GetVMs)(JavaVM**, jsize, jsize*);
typedef jint (JNICALL *DetachThread)(JavaVM *);
typedef jint (JNICALL *DestroyVM)(JavaVM *);
static JavaVMInitArgs *args = NULL;
static CreateVM globalCreateVM=NULL;
static InitArgs globalInitArgs=NULL;
static GetVMs globalGetVMs=NULL;
static DestroyVM globalDestroyVM=NULL;
static JavaVM * globalVM = NULL;
#if defined(AIXPPC)
/* Used to keep track of whether or not opening of the "main redirector" has been attempted.
* Avoiding an infinite loop when libjvm.a is soft linked to libjvm.so
*/
static int attempted_to_open_main = 0;
int openLibraries(const char *libraryDir);
#else /* defined(AIXPPC) */
static int openLibraries(const char *libraryDir);
#endif /* defined(AIXPPC) */
static const char *isPackagedWithCompressedRefs(void);
static BOOLEAN isPackagedWithSubdir(const char *subdir);
static void showVMChoices(void);
#ifdef WIN32
#define J9_MAX_PATH _MAX_PATH
static HINSTANCE j9vm_dllHandle = (HINSTANCE) 0;
#else
#define J9_MAX_PATH PATH_MAX
static void *j9vm_dllHandle = NULL;
#endif
/* define a size for the buffer which will hold the directory name containing the libjvm.so */
#define J9_VM_DIR_LENGTH 32
/*
* Keep this structure synchronized with gc_policy_name table in parseGCPolicy()
*/
typedef enum gc_policy{
GC_POLICY_OPTTHRUPUT,
GC_POLICY_OPTAVGPAUSE,
GC_POLICY_GENCON,
GC_POLICY_BALANCED,
GC_POLICY_METRONOME,
GC_POLICY_NOGC
} gc_policy;
#if defined(LINUX) || defined(OSX)
/* defining _GNU_SOURCE allows the use of dladdr() in dlfcn.h */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#define __USE_GNU 1
#include <dlfcn.h>
#endif
#ifdef AIXPPC
#include <stdlib.h>
#include <sys/ldr.h>
#include <load.h>
#include <dlfcn.h>
#endif /* defined(AIXPPC) */
#if defined(J9ZOS390)
#include <dlfcn.h>
#include <dll.h>
#include "atoe.h"
#include <stdlib.h>
#include <errno.h>
#define dlsym dllqueryfn
#define dlopen(a,b) dllload(a)
#define dlclose dllfree
#define getj9bin getj9binZOS
#define J9FSTAT fstat
#endif /* defined(J9ZOS390) */
#ifndef PATH_MAX
#define PATH_MAX 1023
#endif
#define ENVVAR_JAVA_OPTIONS "_JAVA_OPTIONS"
#define ENVVAR_OPENJ9_JAVA_OPTIONS "OPENJ9_JAVA_OPTIONS"
#define ENVVAR_IBM_JAVA_OPTIONS "IBM_JAVA_OPTIONS"
#if defined(AIXPPC)
static J9StringBuffer* getLibraryNameWithPath(J9StringBuffer *buffer);
#endif /* defined(AIXPPC) */
static J9StringBuffer* getjvmBin(BOOLEAN removeSubdir);
static void chooseJVM(JavaVMInitArgs *args, char *retBuffer, size_t bufferLength);
static void addToLibpath(const char *dir);
static J9StringBuffer *findDir(const char *libraryDir);
static J9StringBuffer* jvmBufferCat(J9StringBuffer* buffer, const char* string);
static J9StringBuffer* jvmBufferEnsure(J9StringBuffer* buffer, UDATA len);
static char* jvmBufferData(J9StringBuffer* buffer);
static void jvmBufferFree(J9StringBuffer* buffer);
static BOOLEAN parseGCPolicy(char *buffer, int *value);
#define MIN_GROWTH 128
#define XMX "-Xmx"
/* We use forward slashes here because J9VM_LIB_ARCH_DIR is not used on Windows. */
#if (JAVA_SPEC_VERSION >= 9) || defined(OSX)
/* On OSX, <arch> doesn't exist, so OPENJ9_ARCH_DIR shouldn't be included in J9VM_LIB_ARCH_DIR. */
#define J9VM_LIB_ARCH_DIR "/lib/"
#else /* (JAVA_SPEC_VERSION >= 9) || defined(OSX) */
#define J9VM_LIB_ARCH_DIR "/lib/" OPENJ9_ARCH_DIR "/"
#endif /* (JAVA_SPEC_VERSION >= 9) || defined(OSX) */
#if defined(WIN32)
#define DIR_SLASH_CHAR '\\'
#else
#define DIR_SLASH_CHAR '/'
#endif
#if defined(DEBUG)
#define DBG_MSG(x) printf x
#else
#define DBG_MSG(x)
#endif
/*
* Remove one segment of a path, optionally keeping a trailing DIR_SLASH_CHAR.
*/
static void
truncatePath(char *inputPath, BOOLEAN keepSlashChar) {
char *lastOccurence = strrchr(inputPath, DIR_SLASH_CHAR);
/* strrchr() returns NULL if it cannot find the character */
if (NULL != lastOccurence) {
lastOccurence[keepSlashChar ? 1 : 0] = '\0';
}
}
#if (JAVA_SPEC_VERSION == 8) || defined(AIXPPC)
/*
* Remove the suffix from string if present.
*/
static void
removeSuffix(char *string, const char *suffix)
{
size_t stringLength = strlen(string);
size_t suffixLength = strlen(suffix);
if (stringLength >= suffixLength) {
char *tail = &string[stringLength - suffixLength];
if (0 == strcmp(tail, suffix)) {
*tail = '\0';
}
}
}
#endif /* (JAVA_SPEC_VERSION == 8) || defined(AIXPPC) */
static void
addToLibpath(const char *dir)
{
#if defined(J9ZOS390)
char *oldPath, *newPath;
int rc, newSize;
char *putenvPath;
int putenvSize;
int putenvErrno;
if (!dir) {
return;
}
if (dir[0] == '\0') {
return;
}
oldPath = getenv("LIBPATH");
DBG_MSG(("\nLIBPATH before = %s\n", oldPath ? oldPath : "<empty>"));
newSize = (oldPath ? strlen(oldPath) : 0) + strlen(dir) + 2; /* 1 for :, 1 for \0 terminator */
newPath = malloc(newSize);
if(!newPath) {
fprintf(stderr, "addToLibpath malloc(%d) 1 failed, aborting\n", newSize);
abort();
}
/* prepend the new path */
strcpy(newPath, dir);
if (oldPath) {
strcat(newPath, ":");
strcat(newPath, oldPath);
}
putenvSize = newSize + strlen("LIBPATH=");
putenvPath = malloc(putenvSize);
if(!putenvPath) {
fprintf(stderr, "addToLibpath malloc(%d) 2 failed, aborting\n", putenvSize);
abort();
}
strcpy(putenvPath,"LIBPATH=");
strcat(putenvPath, newPath);
rc = putenv(putenvPath);
putenvErrno = errno;
free(putenvPath);
#ifdef DEBUG
printf("\nLIBPATH after = %s\n", getenv("LIBPATH"));
#endif
free(newPath);
if (rc != 0) {
fprintf(stderr, "addToLibpath putenv(%s) failed: %s\n", putenvPath, strerror(putenvErrno));
abort();
}
#endif
}
void
freeGlobals(void)
{
#if defined(WIN32)
if (NULL != j9vm_dllHandle) {
FreeLibrary(j9vm_dllHandle);
}
#else
int rc = 0;
if (NULL != j9vm_dllHandle) {
rc = dlclose(j9vm_dllHandle);
if (0 != rc) {
printf("Error closing jvm library: \"%s\"\n", dlerror());
}
j9vm_dllHandle = NULL;
}
#endif
if (NULL != args) {
free(args);
args = NULL;
}
}
static void
showVMChoices(void)
{
if (isPackagedWithCompressedRefs()) {
fprintf(stdout, "\nThe following options control global VM configuration:\n\n");
fprintf(stdout, " -Xcompressedrefs use compressed heap references\n");
}
}
static int xcompressed = -1;
static int xnocompressed = -1;
/**
* Checks if specified option is part of the envOptions string.
* Verifies that it is surrounded by whitespace (or is at the
* beginning or end of the envOptions string).
*
* Note: This is not as robust as parseOptionsFileText() in
* jvminit.c which accounts for quoted strings and such.
*
* @param envOptions Null-terminated string of options.
* @param option Null-terminated option to find.
*
* @return TRUE if option was found, FALSE otherwise.
*/
static BOOLEAN
hasEnvOption(const char *envOptions, const char *option)
{
BOOLEAN success = FALSE;
const char *start = strstr(envOptions, option);
UDATA optionLength = strlen(option);
while (NULL != start) {
if ((start == envOptions) || isspace(start[-1])) {
const char *end = start + optionLength;
if ((*end == '\0') || isspace(*end)) {
success = TRUE;
break;
}
}
start = strstr(start + 1, option);
}
return success;
}
/**
* Searching for most right occurrence of option in the options line
* The option should be first in the command line or has a space before
* There is no check what is behind the discovered option
*
* @param envOptions Null-terminated string of options
* @param option Null-terminated option to find
* @return pointer to discovered option on options line (NULL means not found)
*/
static char *
findStartOfMostRightOption(const char *envOptions, const char *option)
{
char *result = strstr(envOptions, option);
UDATA optionSize = strlen(option);
if (NULL != result) {
if ((result == envOptions) || isspace(result[-1])) {
char *cursor = result;
char *next = NULL;
while (NULL != (next = strstr(cursor + optionSize, option))) {
if (isspace(next[-1])) {
result = next;
}
cursor = next;
}
}
}
return result;
}
/* Scan the next unsigned number off of the argument string.
* Store the result in *result
* Answer 0 on success
*/
UDATA
scan_u64(char **scan_start, U_64* result)
{
/* supporting 0x prefix might be nice (or octal) */
U_64 total = 0;
UDATA rc = 1;
char *c = *scan_start;
/* isdigit isn't properly supported everywhere */
while ( *c >= '0' && *c <= '9' ) {
UDATA digitValue = *c - '0';
if (total > ((U_64)-1) / 10 ) {
return 2;
}
total *= 10;
if ( total > ((U_64)-1) - digitValue ) {
return 2;
}
total += digitValue;
rc = 0; /* we found at least one digit */
c++;
}
*scan_start = c;
*result = total;
return rc;
}
/**
* Parse memory size value where it is a decimal number possibly followed by postfix
* postfix must be 'k', 'm', 'g' in capitals or lower case
* Note: this function does not check what located behind parsed option.
* This is done for compatibility with parsing inside GC
*
* @param option pointer to start point to scan
* @return parsed memory size (0 if parsing was not successful)
*/
static U_64
parseMemorySizeValue(char *option)
{
U_64 result = 0;
char *cursor = option;
if (NULL != option) {
if (0 == scan_u64(&cursor, &result)) {
UDATA shiftFactor = 0;
BOOLEAN parsingError = FALSE;
switch (*cursor) {
case 'k':
case 'K':
shiftFactor = 10;
break;
case 'm':
case 'M':
shiftFactor = 20;
break;
case 'g':
case 'G':
shiftFactor = 30;
break;
case ' ':
case '\0':
break;
default:
parsingError = TRUE;
result = 0;
break;
}
if (!parsingError && (0 != shiftFactor)) {
if (result <= (((U_64)-1) >> shiftFactor)) {
result <<= shiftFactor;
} else {
result = 0;
}
}
}
}
return result;
}
#define GC_POLICY_OPTION "-Xgcpolicy:"
#define LENGTH_GC_POLICY_OPTION (sizeof(GC_POLICY_OPTION) - 1)
static void
checkEnvOptions(char *envOptions, int *gcPolicy, char **xcompressedstr, char **xnocompressedstr, char **xjvmstr, int *xjvm, char **namedVM, size_t *nameLength, char **xmxstr)
{
char *gcPolicyString = findStartOfMostRightOption(envOptions, GC_POLICY_OPTION);
if (NULL == gcPolicyString) {
if (hasEnvOption(envOptions, "-XX:+UseNoGC")) {
gcPolicyString = GC_POLICY_OPTION "nogc";
}
}
if (NULL != gcPolicyString) {
parseGCPolicy(gcPolicyString + LENGTH_GC_POLICY_OPTION, gcPolicy);
}
if (hasEnvOption(envOptions, VMOPT_XCOMPRESSEDREFS)) {
xcompressed = 0;
*xcompressedstr = VMOPT_XCOMPRESSEDREFS;
}
if (hasEnvOption(envOptions, VMOPT_XXUSECOMPRESSEDOOPS)) {
xcompressed = 0;
*xcompressedstr = VMOPT_XXUSECOMPRESSEDOOPS;
}
if (hasEnvOption(envOptions, VMOPT_XNOCOMPRESSEDREFS)) {
xnocompressed = 0;
*xnocompressedstr = VMOPT_XNOCOMPRESSEDREFS;
}
if (hasEnvOption(envOptions, VMOPT_XXNOUSECOMPRESSEDOOPS)) {
xnocompressed = 0;
*xnocompressedstr = VMOPT_XXNOUSECOMPRESSEDOOPS;
}
*xjvmstr = strstr(envOptions, VMOPT_XJVM);
if (NULL != *xjvmstr) {
char *space = NULL;
xjvm = 0;
*namedVM = *xjvmstr + 6;
/* make sure that we don't include the rest of the env var by saving the length until the next space */
space = strstr(*namedVM, " ");
if (NULL == space) {
*nameLength = strlen(*namedVM);
} else {
*nameLength = (size_t)(space - *namedVM);
}
}
*xmxstr = findStartOfMostRightOption(envOptions, XMX);
if (NULL != *xmxstr) {
*xmxstr += sizeof(XMX) - 1;
}
}
/**
* @param args The VM command line arguments
* @param retBuffer The buffer which will be populated with the directory name (must be big enough to contain the name and the NULL byte)
* @param bufferLength The side of the retBuffer
*
* Returns the name of the directory containing the libjvm.so VM library in the caller-provided retBuffer (with a terminating NULL byte)
*
* Exits and prints an error message on error. Returns on success.
*/
static void
chooseJVM(JavaVMInitArgs *args, char *retBuffer, size_t bufferLength)
{
char *envOptions = NULL;
int i;
int xjvm = -1;
char *namedVM = NULL;
const char *basePointer = NULL;
char *optionUsed = NULL;
size_t nameLength = 0;
char *xjvmstr = NULL;
char *xcompressedstr = NULL;
char *xnocompressedstr = NULL;
int ignoreUnrecognizedEnabled = 0;
int gcPolicy = GC_POLICY_GENCON;
char *xmxstr = NULL;
U_64 requestedHeapSize = 0;
/*
* The command line is handled below but look into the multiple JAVA_OPTIONS environment variables here, since it is a special case.
* First look at OPENJ9_JAVA_OPTIONS, or IBM_JAVA_OPTIONS if OPENJ9_JAVA_OPTIONS isn't defined.
*/
#if (JAVA_SPEC_VERSION != 8) || defined(OPENJ9_BUILD)
envOptions = getenv(ENVVAR_JAVA_OPTIONS);
if (NULL != envOptions) {
checkEnvOptions(envOptions, &gcPolicy, &xcompressedstr, &xnocompressedstr, &xjvmstr, &xjvm, &namedVM, &nameLength, &xmxstr);
}
#endif /* (JAVA_SPEC_VERSION != 8) || defined(OPENJ9_BUILD) */
envOptions = getenv(ENVVAR_OPENJ9_JAVA_OPTIONS);
if (NULL == envOptions) {
envOptions = getenv(ENVVAR_IBM_JAVA_OPTIONS);
}
if (NULL != envOptions) {
checkEnvOptions(envOptions, &gcPolicy, &xcompressedstr, &xnocompressedstr, &xjvmstr, &xjvm, &namedVM, &nameLength, &xmxstr);
}
for( i=0; i < args->nOptions; i++ ) {
if ( 0 == strcmp(args->options[i].optionString, VMOPT_XCOMPRESSEDREFS) || 0 == strcmp(args->options[i].optionString, VMOPT_XXUSECOMPRESSEDOOPS) ) {
xcompressed = i+1;
xcompressedstr = args->options[i].optionString;
} else if( 0 == strcmp(args->options[i].optionString, VMOPT_XNOCOMPRESSEDREFS) || 0 == strcmp(args->options[i].optionString, VMOPT_XXNOUSECOMPRESSEDOOPS) ) {
xnocompressed = i+1;
xnocompressedstr = args->options[i].optionString;
} else if( 0 == strncmp(args->options[i].optionString, VMOPT_XJVM, 6) ) {
if ( (NULL != xjvmstr) && (0 != strcmp(xjvmstr, args->options[i].optionString)) ) {
fprintf( stdout, "incompatible options specified: %s %s\n", xjvmstr, args->options[i].optionString );
exit(-1);
}
xjvm = i+1;
namedVM = args->options[i].optionString + 6;
xjvmstr = args->options[i].optionString;
} else if (0 == strncmp(args->options[i].optionString, XMX, sizeof(XMX)-1)) {
xmxstr = args->options[i].optionString + sizeof(XMX)-1;
} else if ((0 == strcmp(args->options[i].optionString, "-XXvm:ignoreUnrecognized")) || (JNI_TRUE == args->ignoreUnrecognized)) {
ignoreUnrecognizedEnabled = 1;
} else if (0 == strncmp(args->options[i].optionString, GC_POLICY_OPTION, LENGTH_GC_POLICY_OPTION)) {
parseGCPolicy(args->options[i].optionString + LENGTH_GC_POLICY_OPTION, &gcPolicy);
}
}
#if (JAVA_SPEC_VERSION != 8) || defined(OPENJ9_BUILD)
/* _JAVA_OPTIONS overrides command line options. */
envOptions = getenv(ENVVAR_JAVA_OPTIONS);
if (NULL != envOptions) {
checkEnvOptions(envOptions, &gcPolicy, &xcompressedstr, &xnocompressedstr, &xjvmstr, &xjvm, &namedVM, &nameLength, &xmxstr);
}
#endif /* (JAVA_SPEC_VERSION != 8) || defined(OPENJ9_BUILD) */
requestedHeapSize = parseMemorySizeValue(xmxstr);
/* check for conflicts.
* The check is based on increasing 'optEnabled' counter for every mutually exclusive sidecar related
* arguments. If optEnabled is found to be bigger than 1, we have a problem.
*/
if ( xjvm != -1 ) {
/*
* xjvm overrides most other options. If the user specified -Xjvm: trust that they know what they're doing.
* 1) redirector invokes the chosen sidecar (through -Xjvm:[sidecar]) without raising any conflict.
* 2) the chosen sidecar is invoked and re-parses the command arguments.
* 3) sidecar init will check and raise any feature conflict.
*/
xcompressed = -1;
xcompressedstr = NULL;
xnocompressed = -1;
xnocompressedstr = NULL;
}
/*
* Decode which VM directory to use.
* If running in Mixed References mode, the 'default' (OPENJ9_NOCR_JVM_DIR) directory is used.
*/
basePointer = OPENJ9_NOCR_JVM_DIR;
#if !(defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS))
if ((xnocompressed != -1) && (xcompressed < xnocompressed)) {
basePointer = OPENJ9_NOCR_JVM_DIR;
optionUsed = xnocompressedstr;
} else if ((xcompressed != -1) && (xnocompressed < xcompressed)) {
basePointer = OPENJ9_CR_JVM_DIR;
optionUsed = xcompressedstr;
} else if (xjvm != -1) {
optionUsed = xjvmstr;
basePointer = namedVM;
} else {
/*
* If compressedrefs VM is included to the package
* and requested heap size is smaller then maximum heap size recommended for compressed refs VM
* set VM to be launched to Compressed References VM
*/
if (isPackagedWithSubdir(OPENJ9_CR_JVM_DIR)) {
U_64 maxHeapForCR = 0;
#if defined(J9ZOS39064)
maxHeapForCR = zosGetMaxHeapSizeForCR();
#else /* defined(J9ZOS39064) */
maxHeapForCR = MAXIMUM_HEAP_SIZE_RECOMMENDED_FOR_COMPRESSEDREFS;
#endif /* defined(J9ZOS39064) */
/* Sizes Table for Segregated heap does not support 4-bit shift so do not use it for Metronome */
if ((0 != maxHeapForCR) && (GC_POLICY_METRONOME == gcPolicy)) {
maxHeapForCR = MAXIMUM_HEAP_SIZE_RECOMMENDED_FOR_3BIT_SHIFT_COMPRESSEDREFS;
}
if (requestedHeapSize <= maxHeapForCR) {
basePointer = OPENJ9_CR_JVM_DIR;
}
}
}
/*
* Jazz 31002 : if -XXvm:ignoreUnrecognized is specified and that the targeted VM
* (eg. compressed ref vm) can't be found, revert to default VM.
*/
if (!isPackagedWithSubdir(basePointer) && (1 == ignoreUnrecognizedEnabled)) {
basePointer = OPENJ9_NOCR_JVM_DIR;
}
#endif /* !(defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS)) */
/* if we didn't set the string length already, do it now for the comparison and copy */
if (0 == nameLength) {
nameLength = strlen(basePointer);
}
/* now, make sure that it will fit in the buffer */
if (nameLength < bufferLength) {
/* it will fit, so do the copy */
memcpy(retBuffer, basePointer, nameLength);
retBuffer[nameLength] = '\0';
} else {
/* won't fit, so set the error */
fprintf(stdout, "Failed to choose VM (buffer too small) - aborting\n");
exit(-1);
}
/* check that the chosen VM exists */
if (!isPackagedWithSubdir(retBuffer) ) {
fprintf(stdout, "Selected VM [%s] ", retBuffer);
if ( NULL != optionUsed ) {
fprintf(stdout, "by option %s ", optionUsed);
}
fprintf(stdout, "does not exist.\n");
#if defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS)
fprintf(stdout,
"This JVM package includes both the '-Xcompressedrefs' and the '-Xnocompressedrefs' "
"configurations, however the VM directory could not be found. Please download the latest "
"JVM package or build with the most recent changes and run the JVM again.\n"
);
#else
/* direct user to OpenJ9 build configurations to properly generate the requested build. */
if (0 == strcmp(OPENJ9_NOCR_JVM_DIR, basePointer)) {
fprintf(stdout,
"This JVM package only includes the '-Xcompressedrefs' configuration. Please run "
"the VM without specifying the '-Xnocompressedrefs' option or by specifying the "
"'-Xcompressedrefs' option.\nTo compile the other configuration, please run configure "
"with '--with-noncompressedrefs'.\n"
);
} else if (0 == strcmp(OPENJ9_CR_JVM_DIR, basePointer)) {
fprintf(stdout,
"This JVM package only includes the '-Xnocompressedrefs' configuration. Please run "
"the VM without specifying the '-Xcompressedrefs' option or by specifying the "
"'-Xnocompressedrefs' option.\nTo compile the other configuration, please run configure "
"without '--with-noncompressedrefs'.\n"
);
}
#endif /* defined(OMR_GC_COMPRESSED_POINTERS) && defined(OMR_GC_FULL_POINTERS) */
exit(-1);
}
}
/**
* jint JNICALL JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *vm_args)
* Load and initialize a virtual machine instance.
* This provides an invocation API that runs the J9 VM in BFU/sidecar mode
*
* @param pvm pointer to the location where the JavaVM interface
* pointer will be placed
* @param penv pointer to the location where the JNIEnv interface
* pointer for the main thread will be placed
* @param vm_args java virtual machine initialization arguments
*
* @returns zero on success; otherwise, return a negative number
*
* DLL: jvm
*/
jint JNICALL
JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *vm_args)
{
char *envOptions = NULL;
jint result;
int i;
jint openedLibraries = 0;
char namedVM[J9_VM_DIR_LENGTH];
#if defined(J9ZOS390)
/* since we need to perform some output and look up env vars, we will require the a2e library to be initialized. This will happen again, when the VM is
actually initialized, but the iconv_init call uses a static flag to make sure that it is only initialized once, so this is safe */
iconv_init();
#endif
/* no tracing for this function, since it's unlikely to be used once the VM is running and the trace engine is initialized */
args = (JavaVMInitArgs *)malloc( sizeof( JavaVMInitArgs) );
args->version = ((JavaVMInitArgs *)vm_args)->version;
args->nOptions = ((JavaVMInitArgs *)vm_args)->nOptions;
args->options = ((JavaVMInitArgs *)vm_args)->options;
args->ignoreUnrecognized = ((JavaVMInitArgs *)vm_args)->ignoreUnrecognized;
memset(namedVM, 0, J9_VM_DIR_LENGTH);
chooseJVM(args, namedVM, J9_VM_DIR_LENGTH);
envOptions = getenv(ENVVAR_OPENJ9_JAVA_OPTIONS);
if (NULL == envOptions) {
envOptions = getenv(ENVVAR_IBM_JAVA_OPTIONS);
}
if ((NULL != envOptions) && hasEnvOption(envOptions, "-locateVM")) {
J9StringBuffer *buffer = findDir(namedVM);
fprintf(stdout, "%s\n", jvmBufferData(buffer));
free(buffer);
exit(0);
} else {
for (i = 0; i < args->nOptions; i++) {
if (0 == strcmp(args->options[i].optionString, "-locateVM")) {
J9StringBuffer *buffer = findDir(namedVM);
fprintf(stdout, "%s\n", jvmBufferData(buffer));
free(buffer);
exit(0);
}
}
}
openedLibraries = openLibraries(namedVM);
if(openedLibraries == JNI_ERR) {
fprintf(stdout, "Failed to find VM - aborting\n");
exit(-1);
}
#if defined(LINUXPPC) && !defined(LINUXPPC64)
/*
This is a work-around for a segfault on shut-down on old LinuxPPC GlibC variants (ie: 2.2.5 - where this was observed)
The core problem may be a VM bug since this library is finalized 3 times when once is expected but it looks like a GlibC
bug after first-pass investigation. More investigation will be required when time permits.
Refer to CMVC 103003 for the background of this work-around.
*/
dlopen("libjava.so", RTLD_NOW);
#endif
#ifdef DEBUG
fprintf(stdout, "Calling... args=%p, pvm=%p(%p), penv=%p(%p)\n", args, pvm, *pvm, penv, *penv);
fflush(stdout);
#endif
result = globalCreateVM(pvm, penv, args);
#ifdef DEBUG
fprintf(stdout, "Finished, result=%d args=%p, pvm=%p(%p), penv=%p(%p)\n", result, args, pvm, *pvm, penv, *penv);
fflush(stdout);
#endif
if (result == JNI_OK) {
globalVM = *pvm;
/* TODO - intercept shutdown
JavaVM * vm = (JavaVM*)BFUjavaVM;
*pvm = vm;
memcpy(&globalInvokeInterface, *vm, sizeof(J9InternalVMFunctions));
globalDestroyVM = globalInvokeInterface.DestroyJavaVM;
globalInvokeInterface.DestroyJavaVM = DestroyJavaVM;
*vm = (struct JNIInvokeInterface_ *) &globalInvokeInterface;
*/
} else {
freeGlobals();
}
return result;
}
/**
* jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
* Return pointers to all the virtual machine instances that have been
* created.
* This provides an invocation API that runs the J9 VM in BFU/sidecar mode
*
* @param vmBuf pointer to the buffer where the pointer to virtual
* machine instances will be placed
* @param bufLen the length of the buffer
* @param nVMs a pointer to an integer
*
* @returns zero on success; otherwise, return a negative number
*
* DLL: jvm
*/
jint JNICALL
JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
{
jint result = JNI_OK;
#if defined(J9ZOS390)
/**
* Since any code that looks at strings will fail without iconv_init and testing on z/OS is somewhat inconvenient, this
* call to initialize the a2e library has been added to the beginning of all the exposed entry points in the file. A static
* in iconv_init ensures that only the first init call actually is honoured.
*/
iconv_init();
#endif
if(NULL != globalGetVMs) {
result = globalGetVMs(vmBuf, bufLen, nVMs);
} else {
/* if this is NULL, then no VM has been started yet. This implies we should return 0 */
/* below logic pulled from jniinv.c JNI_GetCreatedJavaVMs */
#if defined (LINUXPPC64) || (defined (AIXPPC) && defined (PPC64)) || defined (J9ZOS39064)
/* there was a bug in Sovereign VMs on these platforms where jsize was defined to
* be 64-bits, rather than the 32-bits required by the JNI spec. Provide backwards
* compatibility if the JAVA_JSIZE_COMPAT environment variable is set
*/
if (getenv("JAVA_JSIZE_COMPAT")) {
*(jlong*)nVMs = (jlong)0;
} else {
*nVMs = 0;
}
#else
*nVMs = 0;
#endif
}
return result;
}
/**
* jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *vm_args)
* Return a default configuration for the java virtual machine
* implementation.
* This provides an invocation API that runs the J9 VM in BFU/sidecar mode
*
* @param vm_args pointer to a vm-specific initialization structure
* into which the default arguments are filled.
*
* @returns zero on success; otherwise, return a negative number
*
* DLL: jvm
*/
jint JNICALL
JNI_GetDefaultJavaVMInitArgs(void *vm_args)
{
/* problem: this function cannot be resolved until the command line
* is parsed, but must return an answer anyway - fake what normal
* J9 does in this case.
*/
#if defined(J9ZOS390)
/**
* Since any code that looks at strings will fail without iconv_init and testing on z/OS is somewhat inconvenient, this
* call to initialize the a2e library has been added to the beginning of all the exposed entry points in the file. A static
* in iconv_init ensures that only the first init call actually is honoured.
*/
iconv_init();
#endif
if (NULL != globalInitArgs) {
return globalInitArgs(vm_args);
} else {
jint jniVersion = ((JavaVMInitArgs *)vm_args)->version;
switch (jniVersion) {
case JNI_VERSION_1_1:
#if defined(OPENJ9_BUILD)
((JDK1_1InitArgs *)vm_args)->javaStackSize = J9_OS_STACK_SIZE;
#endif /* defined(OPENJ9_BUILD) */
break;
case JNI_VERSION_1_2:
case JNI_VERSION_1_4:
case JNI_VERSION_1_6:
case JNI_VERSION_1_8:
#if JAVA_SPEC_VERSION >= 9
case JNI_VERSION_9:
#endif /* JAVA_SPEC_VERSION >= 9 */
#if JAVA_SPEC_VERSION >= 10
case JNI_VERSION_10:
#endif /* JAVA_SPEC_VERSION >= 10 */
#if JAVA_SPEC_VERSION >= 19
case JNI_VERSION_19:
#endif /* JAVA_SPEC_VERSION >= 19 */
#if JAVA_SPEC_VERSION >= 20
case JNI_VERSION_20:
#endif /* JAVA_SPEC_VERSION >= 20 */
#if JAVA_SPEC_VERSION >= 21
case JNI_VERSION_21:
#endif /* JAVA_SPEC_VERSION >= 21 */
#if JAVA_SPEC_VERSION >= 24
case JNI_VERSION_24:
#endif /* JAVA_SPEC_VERSION >= 24 */
return JNI_OK;
default:
break;
}
return JNI_EVERSION;
}
}
#define strsubdir(subdir) (isPackagedWithSubdir((subdir)) ? (subdir) : NULL)
/*
* Returns the subdirectory name if a compressed references VM is included in the package containing the redirector
*/
static const char *
isPackagedWithCompressedRefs(void)
{
return strsubdir(OPENJ9_CR_JVM_DIR);
}
static BOOLEAN
isPackagedWithSubdir(const char *subdir)
{
J9StringBuffer *buffer = NULL;
#if defined(WIN32)
wchar_t unicodeDLLName[J9_MAX_PATH];
#else /* WIN32 */
size_t jvmBinLength = 0;
struct stat statBuf;
#endif /* WIN32 */
BOOLEAN rc = FALSE;
/* find root directory for this install (e.g. jre/bin/ or jre/lib/<arch>/ dir WITH trailing slash) */
/* Beginning with Java 9 b150, this will be sdk/bin/ or sdk/lib/ */
buffer = getjvmBin(TRUE);
if (NULL == buffer) {
return FALSE;
}
#if ! defined(WIN32)
/* remember the length before appending subdir */
jvmBinLength = strlen(jvmBufferData(buffer));
#endif /* ! WIN32 */
buffer = jvmBufferCat(buffer, subdir);
#if defined(WIN32)
MultiByteToWideChar(OS_ENCODING_CODE_PAGE, OS_ENCODING_MB_FLAGS, jvmBufferData(buffer), -1, unicodeDLLName, (int)strlen(jvmBufferData(buffer)) + 1);
rc = (INVALID_FILE_ATTRIBUTES != GetFileAttributesW(unicodeDLLName));
#else /* WIN32 */
rc = (-1 != stat(jvmBufferData(buffer), &statBuf));
if ((FALSE == rc) && (jvmBinLength > 0)) {
/* Failed to find the subdir in .../bin, look in .../lib (or .../lib/<arch>). */
/* overwrite the trailing slash in jvmBin, removing /subdir */
jvmBufferData(buffer)[jvmBinLength - 1] = '\0';
/* remove /bin, /lib or /<arch> */