-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathvmargs.c
1750 lines (1591 loc) · 60.2 KB
/
vmargs.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 (c) 1991, 2018 IBM Corp. and others
*
* 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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <string.h>
#if defined(WIN32)
#include <windows.h>
#define J9_MAX_ENV 32767
#endif
#include "vmargs_api.h"
#include "vm_api.h"
#include "j2senls.h"
#include "j2sever.h"
#include "j9argscan.h"
#include "jvminit.h"
#if !defined(MAX_PATH)
#if !defined(WIN32)
#include <limits.h>
#if defined(J9ZOS390)
#define MAX_PATH _POSIX_PATH_MAX
#else
#define MAX_PATH PATH_MAX
#endif /* defined(J9ZOS390)*/
#endif /* !defined(WIN32) */
#endif /* !defined(MAX_PATH) */
#include "j9vmutilnls.h"
#include "util_internal.h"
#include "ut_j9util.h"
#include "verbose_api.h"
#if defined(J9ZOS390)
#include "atoe.h"
#endif
#if defined(RS6000) || defined(LINUX)
#define J9UNIX
#endif
#define MAP_TWO_COLONS_TO_ONE 8
#define EXACT_MAP_NO_OPTIONS 16
#define EXACT_MAP_WITH_OPTIONS 32
#define MAP_MEMORY_OPTION 64
#define STARTSWITH_MAP_NO_OPTIONS 128
#define MAP_ONE_COLON_TO_TWO 256
#define MAP_WITH_INCLUSIVE_OPTIONS 512
#if !defined(J9JAVA_PATH_SEPARATOR)
#if defined(WIN32)
#define J9JAVA_PATH_SEPARATOR ";"
#define ALT_JAVA_HOME_DIR_STR L"_ALT_JAVA_HOME_DIR"
#else
#define J9JAVA_PATH_SEPARATOR ":"
#endif
#endif /* !defined(J9JAVA_PATH_SEPARATOR) */
#define JAVA_HOME_EQUALS "-Djava.home="
#define JAVA_LIB_PATH_EQUALS "-Djava.library.path="
#define JAVA_EXT_DIRS_EQUALS "-Djava.ext.dirs="
#define JAVA_USER_DIR_EQUALS "-Duser.dir="
#define SUN_NIO_MAXDIRECTMEMORYSIZE_EQUALS "-Dsun.nio.MaxDirectMemorySize="
#define OPTIONS_DEFAULT "options.default"
#define LARGE_STRING_BUF_SIZE 256
static char * getStartOfOptionValue(J9VMInitArgs* j9vm_args, IDATA element, const char *optionName);
IDATA
findArgInVMArgs(J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args, UDATA match, const char* optionName, const char* optionValue, UDATA doConsumeArgs) {
UDATA optionCntr;
IDATA optionLength, charCntr, returnVal = -1;
char* cursor, *valueString, *testString, *actualString;
char testChar;
UDATA success, hasBeenSet, endCharIsValid;
/* match is magically split into sections */
UDATA stopAtIndex = match >> STOP_AT_INDEX_SHIFT;
UDATA searchForward = (match & SEARCH_FORWARD);
UDATA realMatch = (match & REAL_MATCH_MASK);
UDATA searchToIndex = 0;
UDATA searchFromIndex = 0;
PORT_ACCESS_FROM_PORT(portLibrary);
if (optionName==NULL || 0==j9vm_args->nOptions) {
return returnVal;
}
hasBeenSet = FALSE;
optionLength = strlen(optionName);
if (searchForward) {
if (stopAtIndex < j9vm_args->nOptions) {
searchFromIndex = stopAtIndex; /* stopAtIndex is misnamed if searching forwards - actually "startFromIndex" - the macro adds 1 for you*/
searchToIndex = j9vm_args->nOptions - 1;
} else {
return -1;
}
} else {
searchFromIndex = 0;
if ((stopAtIndex > 0) && (stopAtIndex < j9vm_args->nOptions)) {
searchToIndex = stopAtIndex - 1;
} else {
searchToIndex = j9vm_args->nOptions - 1;
}
}
if (searchForward) {
optionCntr = searchFromIndex;
} else {
optionCntr = searchToIndex;
}
while ((optionCntr <= searchToIndex) && (optionCntr >= searchFromIndex)) {
testString = getOptionString(j9vm_args, optionCntr); /* may return mapped value */
actualString = j9vm_args->actualVMArgs->options[optionCntr].optionString;
if (testString!=NULL) {
success = FALSE;
/* counts how many chars of testString match the chars of optionName - result is charCntr */
for (charCntr = 0; testString[charCntr] && (testString[charCntr] == optionName[charCntr]); ++charCntr);
switch(realMatch) {
case EXACT_MATCH :
success = ((testString[charCntr] == '\0') && (optionName[charCntr] == '\0'));
break;
case STARTSWITH_MATCH : /* Will return true if exact match too */
success = (charCntr == optionLength);
break;
case EXACT_MEMORY_MATCH:
case OPTIONAL_LIST_MATCH :
if (charCntr == optionLength) { /* startswith or exact match */
if (HAS_MAPPING(j9vm_args, optionCntr) && (realMatch == EXACT_MEMORY_MATCH)) {
/* once we get here, we know we found the right index, but because the specified option and the mapped
option may be different lengths we have to use the specified option length to correctly get the value. */
IDATA mapnameLen = strlen(MAPPING_MAPNAME(j9vm_args, optionCntr));
testChar = actualString[mapnameLen];
} else {
testChar = testString[charCntr];
}
if (realMatch == EXACT_MEMORY_MATCH) {
success = (testChar >= '0' && testChar <= '9');
} else
if (realMatch == OPTIONAL_LIST_MATCH) {
success = (testChar == '\0' || testChar == ':');
}
}
break;
case OPTIONAL_LIST_MATCH_USING_EQUALS:
if (charCntr == optionLength) {
testChar = testString[charCntr];
success = (('\0' == testChar) || ('=' == testChar));
}
break;
}
if (success) {
/* Search for a specific parameter after the optionName. eg. -Xverify:none. Only use with STARTSWITH_MATCH or OPTIONAL_LIST_MATCH. */
/* Assumes that a value will terminate in a comma, space or EOL */
/* Search is NOT case sensitive and also does not find substrings eg. searching for "no" in -Xverify:none will not succeed */
if (optionValue && ((realMatch==STARTSWITH_MATCH) || (realMatch==OPTIONAL_LIST_MATCH))) {
success = FALSE;
optionValueOperations(PORTLIB, j9vm_args, optionCntr, GET_OPTION, &valueString, 0, ':', 0, NULL); /* assumes ':' */
if (valueString) {
cursor = strrchr(valueString, ':'); /* Skip past any additional sub-options eg. -Xdump:java:<value> */
if (NULL != cursor) {
++cursor;
} else {
cursor = valueString;
}
while (cursor) {
if (try_scan(&cursor, optionValue)) { /* do a case-insensitive check of current option */
endCharIsValid = (*cursor == '\0' || *cursor == '=' || *cursor == ',' || *cursor == ' ');
if ((cursor >= valueString) && endCharIsValid) {
success = TRUE;
break;
}
}
cursor = strchr(cursor, ',');
if (NULL != cursor) {
cursor += 1; /* skip to next option */
}
}
}
}
if (success) {
if (doConsumeArgs) { /* If we are consuming args, we need to sweep the whole array for duplicates */
if (hasBeenSet) {
/* duplicate cmd-line value which is going to be ignored */
j9vm_args->j9Options[optionCntr].flags = NOT_CONSUMABLE_ARG /* override any flags that already exist */
| (j9vm_args->j9Options[optionCntr].flags & ARG_MEMORY_ALLOCATION);
} else {
j9vm_args->j9Options[optionCntr].flags |= ARG_CONSUMED;
}
} else {
return optionCntr; /* If we're not consuming args, no need to look further */
}
if (!hasBeenSet) {
returnVal = optionCntr;
hasBeenSet = TRUE;
}
}
}
}
if (searchForward) {
++optionCntr;
} else {
--optionCntr;
}
}
return returnVal;
}
/* This function returns the option string at a given index.
It is important to use this function rather than accessing the array directly, otherwise mappings will not work */
char*
getOptionString(J9VMInitArgs* j9vm_args, IDATA index) {
if ( HAS_MAPPING(j9vm_args, index) ) {
return MAPPING_J9NAME(j9vm_args, index);
} else {
return j9vm_args->actualVMArgs->options[index].optionString;
}
}
/* Support function for optionValueOperations.
Searches the entire command-line and "builds" an values list from a number of options. Required as mapped values need to break "duplicate" rules.
Eg. -Xcomp means -Xjit:count=0. So, for "j9 -Xcomp -Xjit:lowopt", -Xcomp would be considered a duplicate. This is wrong.
In this case, GET_COMPOUND should return "lowopt,count=0\0" and GET_COMPOUND_OPTS should return "lowopt\0count=0\0\0".
Note that cmd-line options added as a consequence of environment variables are treated the same as mapped options. */
static IDATA
getCompoundOptions(J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args, IDATA element, IDATA action, char** returnString, UDATA bufSize, char delim, char separator) {
IDATA bufferLeft = bufSize - ((action == GET_COMPOUND_OPTS) ? 1 : 0); /* Extra NULL required */
IDATA currElement, errorFound = 0;
char* delimPos, *cursor;
char optionSearchStr[LARGE_STRING_BUF_SIZE];
char sepChar = (separator ? separator : ',');
UDATA firstElementIsMapped = FALSE;
PORT_ACCESS_FROM_PORT(portLibrary);
memset(*returnString, 0, bufSize); /* Just to be sure! */
/* Get first option value */
if ((errorFound = optionValueOperations(PORTLIB, j9vm_args, element, GET_OPTION, returnString, bufferLeft, delim, 0, NULL)) != OPTION_OK) {
return errorFound;
}
if ((bufferLeft -= (strlen(*returnString)+1)) < 0) {
return OPTION_BUFFER_OVERFLOW;
}
if (element==0) goto allOptionsFound; /* If we are the 0th element, don't bother to search for others */
/* Find the name of the option to use as a search key for other options */
if ( HAS_MAPPING(j9vm_args, element)) {
strncpy(optionSearchStr, MAPPING_J9NAME(j9vm_args, element), (LARGE_STRING_BUF_SIZE - 1));
firstElementIsMapped = TRUE;
} else {
strncpy(optionSearchStr, j9vm_args->actualVMArgs->options[element].optionString, (LARGE_STRING_BUF_SIZE-1));
firstElementIsMapped = FROM_ENVVAR(j9vm_args, element);
}
if (!(delimPos = strchr(optionSearchStr, delim))) /* If call is made to GET_COMPOUND on an option without the delimiter, we can't continue */
return OPTION_ERROR;
*(++delimPos) = '\0';
/* Search for duplicate args in cmd line which may have values we want */
currElement = element;
while (currElement > 0) {
currElement = findArgInVMArgs( PORTLIB, j9vm_args, (STARTSWITH_MATCH | (currElement << STOP_AT_INDEX_SHIFT)), optionSearchStr, NULL, FALSE);
if (currElement < 0)
break;
/* If first element was mapped, find all other mapped values PLUS rightmost non-mapped value. Otherwise, find all mapped values. */
if ( HAS_MAPPING(j9vm_args, currElement) || FROM_ENVVAR(j9vm_args, currElement) || firstElementIsMapped) {
UDATA cursorLen, returnStrLen;
if ((errorFound = optionValueOperations(PORTLIB, j9vm_args, currElement, GET_OPTION, &cursor, 0, delim, 0, NULL)) != OPTION_OK) { /* get correct values string */
return errorFound;
}
cursorLen = strlen(cursor);
bufferLeft -= (cursorLen + 1); /* +1 because of separator */
/* insert at start of buffer and add separator */
returnStrLen = strlen(*returnString) + (bufferLeft < 0 ? bufferLeft : 0); /* If bufferLeft has gone negative, truncate end of string */
memmove(*returnString + (cursorLen+1), *returnString, returnStrLen); /* Shift right minus null-terminator. The buffer is zero'd so this is safe. */
strncpy(*returnString, cursor, cursorLen);
(*returnString)[cursorLen] = sepChar;
if (firstElementIsMapped) {
firstElementIsMapped = FALSE;
}
if (bufferLeft < 0) {
return OPTION_BUFFER_OVERFLOW;
}
}
}
allOptionsFound :
/* If GET_COMPOUND_OPTS, change all the separators to \0 */
if (action == GET_COMPOUND_OPTS) {
for (cursor = *returnString; *cursor; cursor++) {
if (*cursor == ',') {
*cursor = '\0';
}
}
*(++cursor) = '\0'; /* Explicitly null-terminate "array" */
}
return OPTION_OK;
}
/* Covers the range of operations that can be performed on a given option string.
GET_OPTION returns the value of a command-line option. Eg "-Xfoo:bar" returns "bar".
GET_OPTION_OPT returns the value of a value. Eg. "-Xfoo:bar:wibble" returns "wibble".
GET_OPTIONS returns a list of values separated by NULLs. Eg. "-Xfoo:bar=1,wibble=2" returns "bar=1\0wibble=2"
GET_COMPOUND returns all the option values from the command-line for a given option, separated by commas. Based on certain rules.
GET_COMPOUND_OPTS returns all the option values from the command-line for a given option, separated by \0s. Based on certain rules.
GET_MAPPED_OPTION returns the value of a mapped option. Eg. if -Xsov1 maps to Xj91 then "-Xsov1:foo" returns "foo"
GET_MEM_VALUE returns a rounded value of a memory option. Eg. "-Xfoo32k" returns (32 * 1024).
GET_INT_VALUE returns the exact integer value of an option. Eg. -Xfoo5 returns 5
GET_PRC_VALUE returns the a decimal value between 0 and 1 represented as a percentage. Eg. -Xmaxf1.0 returns 100.
For GET_OPTIONS, the function expects to get a string buffer to write to and a buffer size.
For GET_OPTION, GET_OPTION_OPT and GET_MAPPED_OPTION, the result is returned either as a pointer in valuesBuffer or, if valuesBuffer is a real buffer, it copies the result.
For GET_MEM_VALUE, GET_INT_VALUE and GET_PRC_VALUE the function enters the result in a UDATA provided.
Unless absolutely necessary, don't call this method directly - use the macros defined in jvminit.h.
*** SEE testOptionValueOps FOR UNIT TESTS. TO RUN, ADD "#define JVMINIT_UNIT_TEST" TO SOURCE TEMPLATE ***
*/
IDATA
optionValueOperations(J9PortLibrary *portLibrary, J9VMInitArgs* j9vm_args, IDATA element, IDATA action, char** valuesBuffer, UDATA bufSize, char delim, char separator, void* reserved) {
char *values, *scanStart;
char *cursor;
IDATA sepCount = 0;
UDATA value, oldValue, done;
IDATA errorFound = 0;
UDATA mapType;
double dvalue = 0.0;
uintptr_t rc = 0;
PORT_ACCESS_FROM_PORT(portLibrary);
if (element < 0) { /* If invalid element... don't even try */
done = TRUE;
} else {
done = FALSE;
}
if (valuesBuffer) {
if ((action == GET_MEM_VALUE) || (action == GET_INT_VALUE) || (action == GET_PRC_VALUE) || (action == GET_DBL_VALUE)) {
if (!(*valuesBuffer)) {
return OPTION_ERROR; /* *valuesBuffer is an input value. Make sure it is not NULL. */
}
} else {
if (bufSize > 0) { /* bufSize > 0 indicates *valuesBuffer is a real buffer, which is return value*/
if (*valuesBuffer) {
memset(*valuesBuffer, 0, bufSize);
} else {
return OPTION_ERROR;
}
} else {
*valuesBuffer = NULL; /* valuesBuffer will return just a pointer. Init to NULL. */
}
}
} else {
return OPTION_ERROR;
}
while (!done) {
switch (action) {
case GET_OPTION :
cursor = NULL;
/* For EXACT_MAP_WITH_OPTIONS and MAP_ONE_COLON_TO_TWO no special mapping code is required */
if (HAS_MAPPING(j9vm_args, element)) {
if (!(MAPPING_FLAGS(j9vm_args, element) & (EXACT_MAP_WITH_OPTIONS | EXACT_MAP_NO_OPTIONS | MAP_ONE_COLON_TO_TWO))) {
action = GET_MAPPED_OPTION;
continue;
}
if (MAPPING_FLAGS(j9vm_args, element) & EXACT_MAP_NO_OPTIONS) {
cursor = MAPPING_J9NAME(j9vm_args, element);
}
}
if (!cursor)
cursor = j9vm_args->actualVMArgs->options[element].optionString;
if (cursor && (cursor = strchr(cursor, delim))) {
if (bufSize > 0) {
strncpy(*valuesBuffer, ++cursor, (bufSize-1));
if (strlen(cursor) > (bufSize-1)) {
return OPTION_BUFFER_OVERFLOW;
}
} else {
*valuesBuffer = ++cursor;
}
}
done = TRUE;
break;
case GET_OPTION_OPT :
/* Note - currently won't work with MAP_WITH_INCLUSIVE_OPTIONS */
if ((errorFound = optionValueOperations(PORTLIB, j9vm_args, element, GET_OPTION, &values, 0, delim, 0, NULL)) != OPTION_OK) { /* get correct values string */
return errorFound;
}
if (values && (cursor = strchr(values, separator))) {
if (bufSize > 0) {
strncpy(*valuesBuffer, ++cursor, (bufSize-1));
if (strlen(cursor) > (bufSize-1)) {
return OPTION_BUFFER_OVERFLOW;
}
} else {
*valuesBuffer = ++cursor;
}
}
done = TRUE;
break;
case GET_OPTIONS :
/* Takes a buffer and copies in the values, each one separated by a single NULL. Should have at least \0\0 at the end, hence bufSize-1 */
errorFound = optionValueOperations(PORTLIB, j9vm_args, element, GET_OPTION, valuesBuffer, bufSize-1, delim, 0, NULL); /* get correct values string */
if (*valuesBuffer) {
cursor = *valuesBuffer;
while (*cursor) {
if (*cursor == separator) {
*cursor = '\0';
}
++cursor;
}
*(++cursor) = '\0'; /* explicity null-terminate the "array". There is no danger of walking beyond the buffer here because of bufSize-1 in call above. */
}
if (errorFound != OPTION_OK) {
return errorFound;
}
done = TRUE;
break;
case GET_COMPOUND :
case GET_COMPOUND_OPTS :
if ((errorFound = getCompoundOptions(PORTLIB, j9vm_args, element, action, valuesBuffer, bufSize, delim, separator)) != OPTION_OK) {
return errorFound;
}
done = TRUE;
break;
case GET_MAPPED_OPTION :
if ( !HAS_MAPPING(j9vm_args, element) )
return OPTION_ERROR;
mapType = MAPPING_FLAGS(j9vm_args, element);
switch (mapType) {
case INVALID_OPTION :
case STARTSWITH_MAP_NO_OPTIONS :
break;
case MAP_TWO_COLONS_TO_ONE :
cursor = j9vm_args->actualVMArgs->options[element].optionString;
cursor = strchr(cursor, ':');
if (NULL != cursor) {
cursor = strchr(++cursor, ':');
if (NULL != cursor) {
if (bufSize > 0) {
strncpy(*valuesBuffer, ++cursor, (bufSize-1));
if (strlen(cursor) > (bufSize-1)) {
return OPTION_BUFFER_OVERFLOW;
}
} else {
*valuesBuffer = ++cursor;
}
}
}
break;
case MAP_WITH_INCLUSIVE_OPTIONS :
/* Has to build the result from part of the j9Name and then the actual value on the comand-line */
if (bufSize > 0) {
char* mapName = MAPPING_MAPNAME(j9vm_args, element);
/* To get the value of the *actual* arg specified on the command-line, the delimiter may not be the one specified */
char delimChar = mapName[strlen(mapName)-1];
char* j9Name = MAPPING_J9NAME(j9vm_args, element);
char* includeStr = strchr(j9Name, ':');
char* actualOption = j9vm_args->actualVMArgs->options[element].optionString;
IDATA bufferLeft = (bufSize-1);
cursor = *valuesBuffer;
if (includeStr) {
strncpy(cursor, includeStr+1, bufferLeft);
cursor += strlen(cursor);
}
if ((bufferLeft -= strlen(*valuesBuffer)) <= 0) {
return OPTION_BUFFER_OVERFLOW;
}
if (actualOption && (actualOption = strchr(actualOption, delimChar))) {
strncpy(cursor, ++actualOption, bufferLeft);
}
if (actualOption && (((IDATA)strlen(actualOption)) > bufferLeft)) {
return OPTION_BUFFER_OVERFLOW;
}
} else {
return OPTION_ERROR; /* MAP_WITH_INCLUSIVE_OPTIONS cannot work without a buffer to build the result. Use COPY_OPTION_VALUE. */
}
break;
}
done = TRUE;
break;
case GET_DBL_VALUE:
*(double *)reserved = 0.0;
cursor = getStartOfOptionValue(j9vm_args, element, *valuesBuffer);
rc = scan_double(&cursor, &dvalue);
if (*cursor != '\0') {
return OPTION_MALFORMED;
}
if (OPTION_OK != rc) {
return rc;
}
*(double *)reserved = dvalue;
done = TRUE;
break;
case GET_MEM_VALUE :
case GET_INT_VALUE :
case GET_PRC_VALUE :
*((UDATA*)(reserved)) = 0;
scanStart = getStartOfOptionValue(j9vm_args, element, *valuesBuffer);
cursor = scanStart;
if (scan_udata(&cursor, &value)) {
return OPTION_MALFORMED;
}
/* For GET_INT_VALUE, operation is simple. Return value below or error. */
if (action==GET_INT_VALUE) {
if (*cursor != '\0') {
return OPTION_MALFORMED;
}
} else
/* GET_PRC_VALUE should return value as a percentage (100 * value) and start with zero. Used for options such as -Xmine. */
if (action==GET_PRC_VALUE) {
if (value > 1) {
return OPTION_OVERFLOW;
} else {
char decimalSep = *cursor;
IDATA intValue = value;
value = 0;
if (decimalSep == '.' || decimalSep == ',') {
char floatingBuffer[3] = "00";
char* floatingBufferPtr = floatingBuffer;
UDATA i = 0;
/* Advance the cursor past the decimal point */
cursor++;
/* Copy up to two digits after decimal point */
for (i = 0; i < 2; i++) {
if (*cursor >= '0' && *cursor <= '9') {
floatingBuffer[i] = *cursor;
cursor++;
} else {
break;
}
}
if (scan_udata(&floatingBufferPtr, &value)) {
return OPTION_MALFORMED;
}
}
if (intValue == 1) { /* Case for 1.0, cannot return 0! */
if (value > 0) {
return OPTION_OVERFLOW;
}
value = 100;
}
}
} else {
switch (*cursor) {
case '\0':
oldValue = value;
value = (value + sizeof(UDATA) - 1) & ~(sizeof(UDATA) - 1); /* round to nearest pointer value */
if (value < oldValue) return OPTION_OVERFLOW;
break;
case 'k':
case 'K':
if (value <= (((UDATA)-1) >> 10)) {
value <<= 10;
} else {
return OPTION_OVERFLOW;
}
cursor++;
break;
case 'm':
case 'M':
if (value <= (((UDATA)-1) >> 20)) {
value <<= 20;
} else {
return OPTION_OVERFLOW;
}
cursor++;
break;
case 'g':
case 'G':
if (value <= (((UDATA)-1) >> 30)) {
value <<= 30;
} else {
return OPTION_OVERFLOW;
}
cursor++;
break;
default:
return OPTION_MALFORMED;
}
}
if ((*cursor != '\0')) {
/* CMVC 146992: issue a warning so that the user knows they have bad options */
char *cmdlineoption = *valuesBuffer;
if ( HAS_MAPPING(j9vm_args, element) ) {
/* If we have a mapping, then we ignore the option name passed in. Instead we want the one that corresponds to the actual cmd line */
cmdlineoption = &(j9vm_args->actualVMArgs->options[element].optionString[ strlen(MAPPING_MAPNAME(j9vm_args, element)) ]);
}
j9nls_printf(PORTLIB, J9NLS_WARNING, J9NLS_VMUTIL_MALFORMED_MEMORY_OPTION, cmdlineoption, cursor-scanStart, scanStart, cursor);
}
*((UDATA*)(reserved)) = value; /* Cannot return IDATA, as values are UDATA, so return as reserved */
done = TRUE;
break;
default :
done = TRUE; /* should never get here! */
} /* switch */
} /* while */
return OPTION_OK;
}
static char *
getStartOfOptionValue(J9VMInitArgs* j9vm_args, IDATA element, const char *optionName)
{
const char *option = optionName;
char *value = NULL;
if (HAS_MAPPING(j9vm_args, element)) {
option = MAPPING_MAPNAME(j9vm_args, element);
}
Assert_Util_true(NULL != option);
value = j9vm_args->actualVMArgs->options[element].optionString + strlen(option);
return value;
}
static UDATA
convertMemorySizeToBytes(J9PortLibrary * portLib, char *sizeString, UDATA *result)
{
UDATA value = 0;
UDATA oldValue = 0;
char *cursor = sizeString;
*result = 0;
if (scan_udata(&cursor, &value)) {
return OPTION_MALFORMED;
}
switch (*cursor) {
case '\0':
oldValue = value;
value = (value + sizeof(UDATA) - 1) & ~(sizeof(UDATA) - 1); /* round to nearest pointer value */
if (value < oldValue) {
return OPTION_OVERFLOW;
}
break;
case 'k':
case 'K':
if (value <= (((UDATA)-1) >> 10)) {
value <<= 10;
} else {
return OPTION_OVERFLOW;
}
cursor++;
break;
case 'm':
case 'M':
if (value <= (((UDATA)-1) >> 20)) {
value <<= 20;
} else {
return OPTION_OVERFLOW;
}
cursor++;
break;
case 'g':
case 'G':
if (value <= (((UDATA)-1) >> 30)) {
value <<= 30;
} else {
return OPTION_OVERFLOW;
}
cursor++;
break;
default:
return OPTION_MALFORMED;
}
*result = value;
return 0;
}
IDATA
addOptionsDefaultFile(J9PortLibrary * portLib, J9JavaVMArgInfoList *vmArgumentsList, char *optionsDirectory, UDATA verboseFlags)
{
char optionsArgumentBuffer[sizeof(VMOPT_XOPTIONSFILE_EQUALS)+MAX_PATH + sizeof(OPTIONS_DEFAULT) + 1]; /* add an extra byte to detect overflow */
char* argString = NULL;
size_t argumentLength = -1;
UDATA resultLength = 0;
PORT_ACCESS_FROM_PORT(portLib);
resultLength = j9str_printf(PORTLIB, optionsArgumentBuffer, sizeof(optionsArgumentBuffer), VMOPT_XOPTIONSFILE_EQUALS "%s" DIR_SEPARATOR_STR OPTIONS_DEFAULT, optionsDirectory);
if (resultLength > (sizeof(VMOPT_XOPTIONSFILE_EQUALS) + MAX_PATH)) {
return -1; /* overflow */
}
return addXOptionsFile(portLib, optionsArgumentBuffer, vmArgumentsList, verboseFlags);
}
IDATA
addXjcl(J9PortLibrary * portLib, J9JavaVMArgInfoList *vmArgumentsList, UDATA j2seVersion)
{
char *dllName = NULL;
size_t dllNameLength = -1;
size_t argumentLength = -1;
char *argString = NULL;
UDATA j2seReleaseValue = j2seVersion & J2SE_RELEASE_MASK;
J9JavaVMArgInfo *optArg = NULL;
PORT_ACCESS_FROM_PORT(portLib);
if (J2SE_SHAPE_RAW == (j2seVersion & J2SE_SHAPE_MASK)) {
Assert_Util_unreachable();
}
if (J2SE_V11 <= j2seReleaseValue) {
dllName = J9_JAVA_SE_11_DLL_NAME;
dllNameLength = sizeof(J9_JAVA_SE_11_DLL_NAME);
} else if (J2SE_V10 <= j2seReleaseValue) {
dllName = J9_JAVA_SE_10_DLL_NAME;
dllNameLength = sizeof(J9_JAVA_SE_10_DLL_NAME);
} else if (J2SE_19 <= j2seReleaseValue) {
dllName = J9_JAVA_SE_9_DLL_NAME;
dllNameLength = sizeof(J9_JAVA_SE_9_DLL_NAME);
} else if (J2SE_18 <= j2seReleaseValue) {
/* Java 8 uses DLL name for Java 7 */
dllName = J9_JAVA_SE_7_BASIC_DLL_NAME;
dllNameLength = sizeof(J9_JAVA_SE_7_BASIC_DLL_NAME);
} else { /* Java 6, 7 */
Assert_Util_unreachable();
}
argumentLength = sizeof(VMOPT_XJCL_COLON) + dllNameLength - 1; /* sizeof called twice, each includes the \0 */
argString = j9mem_allocate_memory(argumentLength, OMRMEM_CATEGORY_VM);
if (NULL == argString) {
return -1;
}
j9str_printf(PORTLIB, argString, argumentLength, VMOPT_XJCL_COLON "%s", dllName);
optArg = newJavaVMArgInfo(vmArgumentsList, argString, ARG_MEMORY_ALLOCATION|CONSUMABLE_ARG);
if (NULL == optArg) {
j9mem_free_memory(argString);
return -1;
}
return 0;
}
IDATA
addBootLibraryPath(J9PortLibrary * portLib, J9JavaVMArgInfoList *vmArgumentsList, char *propertyNameEquals, char *j9binPath, char *jrebinPath)
{
char *optionsArgumentBuffer = NULL;
/* argumentLength include the space for the \0 because of the sizeof() */
size_t argumentLength = 0;
J9JavaVMArgInfo *optArg = NULL;
PORT_ACCESS_FROM_PORT(portLib);
argumentLength = strlen(propertyNameEquals) + strlen(j9binPath) + sizeof(J9JAVA_PATH_SEPARATOR) + strlen(jrebinPath); /* the sizeof operation adds the space for the terminating null */
optionsArgumentBuffer = j9mem_allocate_memory(argumentLength, OMRMEM_CATEGORY_VM);
if (NULL == optionsArgumentBuffer) {
return -1;
}
j9str_printf(PORTLIB, optionsArgumentBuffer, argumentLength, "%s%s" J9JAVA_PATH_SEPARATOR "%s", propertyNameEquals, j9binPath, jrebinPath);
optArg = newJavaVMArgInfo(vmArgumentsList, optionsArgumentBuffer, ARG_MEMORY_ALLOCATION|CONSUMABLE_ARG);
if (NULL == optArg) {
j9mem_free_memory(optionsArgumentBuffer);
return -1;
}
return 0;
}
#define MAX_LIBPATH_SUBSTRINGS 16
/**
* construct an argument -Djava.library.path= +
* - path to jre/bin, then
* Windows:
* - the system directory as returned from getSystemDirectory()
* - the Windows directory as returned from getWindowsDirectory()
* - the PATH variable
* - the current directory
* AIX:
* - contents of LIBPATH variable
* - contents of LD_LIBRARY_PATH variable
* - /usr/lib
* Linux:
* - same as AIX, but without LIBPATH
* z/OS:
* - contents of LIBPATH variable
* - /usr/lib
* z/TPF:
* - same as Linux
*/
IDATA
addJavaLibraryPath(J9PortLibrary * portLib, J9JavaVMArgInfoList *vmArgumentsList,
UDATA argEncoding, BOOLEAN jvmInSubdir, char *j9binPath, char *jrebinPath,
const char *libpathValue, const char *ldLibraryPathValue)
{
#if defined(J9UNIX) || defined(J9ZOS390)
IDATA envVarSize = 0;
#endif
char *substringBuffer[MAX_LIBPATH_SUBSTRINGS];
BOOLEAN allocated[MAX_LIBPATH_SUBSTRINGS] = {FALSE};
char *pathBuffer = NULL;
int substringIndex = 0;
J9JavaVMArgInfo *optArg = NULL;
size_t substringLength = 0;
char *optionsArgumentBuffer = NULL;
IDATA status = -1;
PORT_ACCESS_FROM_PORT(portLib);
substringBuffer[substringIndex] = JAVA_LIB_PATH_EQUALS;
substringIndex += 1;
substringLength += strlen(JAVA_LIB_PATH_EQUALS);
substringBuffer[substringIndex] = j9binPath;
substringIndex += 1;
substringLength += strlen(j9binPath);
if (jvmInSubdir) {
substringBuffer[substringIndex] = J9JAVA_PATH_SEPARATOR;
substringIndex += 1;
substringLength += strlen(J9JAVA_PATH_SEPARATOR);
substringBuffer[substringIndex] = jrebinPath;
substringIndex += 1;
substringLength += strlen(jrebinPath);
}
#ifdef WIN32
{
char dir[MAX_PATH];
wchar_t unicodeDir[MAX_PATH];
UDATA rc = 0;
BOOL isWow64 = FALSE;
/* CMVC 177267 : Append system and windows directory */
memset(unicodeDir, 0, MAX_PATH);
IsWow64Process(GetCurrentProcess(), &isWow64);
if (TRUE == isWow64) {
rc = GetSystemWow64DirectoryW(unicodeDir, MAX_PATH);
} else {
rc = GetSystemDirectoryW(unicodeDir, MAX_PATH);
}
if (0 != rc) {
size_t dirLen = 0;
memset(dir, 0, MAX_PATH);
WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS, unicodeDir, -1, dir, MAX_PATH, NULL, NULL);
dirLen = strlen(dir) + 1;
pathBuffer = j9mem_allocate_memory(dirLen, OMRMEM_CATEGORY_VM);
if (NULL == pathBuffer) {
substringBuffer[substringIndex] = NULL; /* null terminate the list */
goto freeBuffers;
}
strncpy(pathBuffer, dir, dirLen);
substringBuffer[substringIndex] = J9JAVA_PATH_SEPARATOR;
substringIndex += 1;
substringLength += strlen(J9JAVA_PATH_SEPARATOR);
substringBuffer[substringIndex] = pathBuffer;
allocated[substringIndex] = TRUE;
substringLength += (dirLen-1); /* remove the null */
substringIndex;
substringIndex += 1;
}
memset(unicodeDir, 0, MAX_PATH);
if (0 != GetWindowsDirectoryW(unicodeDir, MAX_PATH)) {
size_t dirLen = 0;
memset(dir, 0, MAX_PATH);
WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS, unicodeDir, -1, dir, MAX_PATH, NULL, NULL);
dirLen = (strlen(dir) + 1);
pathBuffer = j9mem_allocate_memory(dirLen, OMRMEM_CATEGORY_VM);
if (NULL == pathBuffer) {
substringBuffer[substringIndex] = NULL; /* null terminate the list */
goto freeBuffers;
}
strncpy(pathBuffer, dir, dirLen);
substringBuffer[substringIndex] = J9JAVA_PATH_SEPARATOR;
substringIndex += 1;
substringLength += strlen(J9JAVA_PATH_SEPARATOR);
substringBuffer[substringIndex] = pathBuffer;
allocated[substringIndex] = TRUE;
substringLength += (dirLen - 1); /* remove the null */
substringIndex;
substringIndex += 1;
}
}
pathBuffer = j9mem_allocate_memory(J9_MAX_ENV, OMRMEM_CATEGORY_VM);
if (NULL == pathBuffer) {
substringBuffer[substringIndex] = NULL; /* null terminate the list */
goto freeBuffers;
}
memset(pathBuffer, 0, J9_MAX_ENV);
if (0 == j9sysinfo_get_env("PATH", pathBuffer, J9_MAX_ENV)) {
substringBuffer[substringIndex] = J9JAVA_PATH_SEPARATOR;
substringIndex += 1;
substringLength += strlen(J9JAVA_PATH_SEPARATOR);
substringBuffer[substringIndex] = pathBuffer;
allocated[substringIndex] = TRUE;
substringLength += strlen(pathBuffer);
substringIndex;
substringIndex += 1;
}
#else /* defined(J9UNIX) || defined(J9ZOS390) */
if (NULL != libpathValue) {
size_t pathLen = strlen(libpathValue) + 1;
pathBuffer = j9mem_allocate_memory(pathLen, OMRMEM_CATEGORY_VM);
if (NULL == pathBuffer) {
substringBuffer[substringIndex] = NULL; /* null terminate the list */
goto freeBuffers;
}
strcpy(pathBuffer, libpathValue);
substringBuffer[substringIndex] = J9JAVA_PATH_SEPARATOR;
substringIndex += 1;
substringLength += strlen(J9JAVA_PATH_SEPARATOR);
substringBuffer[substringIndex] = pathBuffer;
allocated[substringIndex] = TRUE;
substringLength += strlen(pathBuffer);
substringIndex += 1;
}
if (NULL != ldLibraryPathValue) {
size_t pathLen = strlen(ldLibraryPathValue) + 1;
pathBuffer = j9mem_allocate_memory(pathLen, OMRMEM_CATEGORY_VM);
if (NULL == pathBuffer) {
substringBuffer[substringIndex] = NULL; /* null terminate the list */
goto freeBuffers;
}
strcpy(pathBuffer, ldLibraryPathValue);
substringBuffer[substringIndex] = J9JAVA_PATH_SEPARATOR;
substringIndex += 1;
substringLength += strlen(J9JAVA_PATH_SEPARATOR);
substringBuffer[substringIndex] = pathBuffer;
allocated[substringIndex] = TRUE;
substringLength += strlen(pathBuffer);
substringIndex += 1;
}
#endif /* defined(J9UNIX) || defined(J9ZOS390) */
#ifdef J9UNIX
#if defined(J9VM_ENV_DATA64)
/* JAZZ103 117105: 64-bit JDKs on Linux and AIX should add /usr/lib64 to java.library.path ahead of /usr/lib. */
#define USRLIB64 ":/usr/lib64"
substringBuffer[substringIndex] = USRLIB64;
substringIndex += 1;
substringLength += (sizeof(USRLIB64) - 1) ;
#undef USRLIB64
#endif /* defined(J9VM_ENV_DATA64) */
substringBuffer[substringIndex] = ":/usr/lib";
substringIndex += 1;
substringLength += strlen(":/usr/lib");
#endif
#ifdef WIN32
/* CMVC 177267, RTC 87362 : On windows, current directory is added at the end */
substringBuffer[substringIndex] = ";.";
substringIndex += 1;
substringLength += strlen(";.");
#endif
substringBuffer[substringIndex] = NULL; /* null terminate the list */
optionsArgumentBuffer = j9mem_allocate_memory(substringLength + 1, OMRMEM_CATEGORY_VM);
if (NULL == optionsArgumentBuffer) {
goto freeBuffers;
}
*optionsArgumentBuffer = '\0';
Assert_Util_true(substringIndex < MAX_LIBPATH_SUBSTRINGS); /* programming error */
/* note that the buffer size is calculated from the sizes of the substrings. In this case strcat() cannot overflow. */
for (substringIndex = 0; NULL != substringBuffer[substringIndex]; ++substringIndex) {
strcat(optionsArgumentBuffer, substringBuffer[substringIndex]);
}