Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/exelib/common/libargs.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2020 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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
21
*******************************************************************************/
22
23
/*------------------------------------------------------------------
24
* libargs. : handle VM init args
25
*------------------------------------------------------------------*/
26
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <jni.h>
31
#include "j9.h"
32
#include "j9port.h"
33
#include "j9lib.h"
34
#include "j9exelibnls.h"
35
#include "exelib_internal.h"
36
#include "util_api.h"
37
38
/*------------------------------------------------------------------
39
*
40
*------------------------------------------------------------------*/
41
#if defined(LOGGING)
42
#define LOG(x) printf x
43
#else
44
#define LOG(x)
45
#endif
46
/*------------------------------------------------------------------
47
*
48
*------------------------------------------------------------------*/
49
50
typedef struct J9LinkedString {
51
struct J9LinkedString *next;
52
char data[1];
53
} J9LinkedString;
54
55
typedef struct {
56
I_32 size;
57
I_32 count;
58
JavaVMOption *vmOptions;
59
J9PortLibrary *portLib;
60
J9LinkedString *strings;
61
} J9VMOptionsTable;
62
63
#define J9CMD_EXACT_MATCH 0
64
#define J9CMD_PARTIAL_MATCH 1
65
typedef struct J9CmdLineAction {
66
char *cmdLineName;
67
UDATA partialMatch;
68
char *internalName;
69
I_32 (*actionFunction)(J9PortLibrary *portLib, int *argc, char *argv[], void **vmOptionsTable, struct J9CmdLineAction *);
70
} J9CmdLineAction;
71
72
I_32 vmOptionsTableAddExeName(void **vmOptionsTable, char *argv0);
73
int vmOptionsTableGetCount(void **vmOptionsTable);
74
I_32 vmOptionsTableInit(J9PortLibrary *portLib, void **vmOptionsTable, int initialCount);
75
I_32 vmOptionsTableAddOptionWithCopy(void **vmOptionsTable, char *optionString, void *extraInfo);
76
I_32 vmOptionsTableAddOption(void **vmOptionsTable, char *optionString, void *extraInfo);
77
JavaVMOption *vmOptionsTableGetOptions(void **vmOptionsTable);
78
void vmOptionsTableDestroy(void **vmOptionsTable);
79
80
static char *allocString(J9VMOptionsTable *table, UDATA size);
81
static I_32 cmdline_get_jcl(J9PortLibrary *portLib, int *argc, char *argv[], void **vmOptionsTable, J9CmdLineAction *action);
82
static I_32 copyTable(J9PortLibrary *portLib, J9VMOptionsTable *table, J9VMOptionsTable *oldTable);
83
static I_32 buildNewTable(J9PortLibrary *portLib, int initialCount, J9VMOptionsTable *oldTable, J9VMOptionsTable **newTable);
84
85
#define NO_DEFAULT_VALUE 0
86
#define PRIVATE_OPTION NULL
87
88
/*------------------------------------------------------------------
89
*
90
*------------------------------------------------------------------*/
91
92
#if defined(J9VM_INTERP_VERBOSE)
93
static char *verboseOptions[] = {
94
"\n -verbose[:{class",
95
"|gcterse",
96
"|gc",
97
#ifdef J9VM_OPT_DYNAMIC_LOAD_SUPPORT
98
"|dynload",
99
#endif
100
"|sizes",
101
"|stack|debug}]\n"
102
};
103
#endif /* J9VM_INTERP_VERBOSE */
104
105
/*------------------------------------------------------------------
106
* create a new table with specified size
107
*------------------------------------------------------------------*/
108
I_32
109
vmOptionsTableInit(J9PortLibrary *portLib, void **vmOptionsTable, int initialCount)
110
{
111
return buildNewTable(portLib, initialCount, NULL, (J9VMOptionsTable **)vmOptionsTable);
112
}
113
114
/*------------------------------------------------------------------
115
* destroy the table
116
*------------------------------------------------------------------*/
117
void
118
vmOptionsTableDestroy(void **vmOptionsTable)
119
{
120
J9VMOptionsTable *table = *vmOptionsTable;
121
J9PortLibrary *portLib = table->portLib;
122
J9LinkedString *this = table->strings;
123
PORT_ACCESS_FROM_PORT(portLib);
124
125
/* free the linked list of strings */
126
while (NULL != this) {
127
J9LinkedString *next = this->next;
128
j9mem_free_memory(this);
129
this = next;
130
}
131
132
j9mem_free_memory(table);
133
}
134
135
/*------------------------------------------------------------------
136
* get the number of options in the table
137
*------------------------------------------------------------------*/
138
int
139
vmOptionsTableGetCount(void **vmOptionsTable)
140
{
141
J9VMOptionsTable *table = *vmOptionsTable;
142
143
return table->count;
144
}
145
146
/*------------------------------------------------------------------
147
*
148
*------------------------------------------------------------------*/
149
JavaVMOption *
150
vmOptionsTableGetOptions(void **vmOptionsTable)
151
{
152
J9VMOptionsTable *table = *vmOptionsTable;
153
154
return table->vmOptions;
155
}
156
157
/*------------------------------------------------------------------
158
*
159
*------------------------------------------------------------------*/
160
I_32
161
vmOptionsTableAddOption(void **vmOptionsTable, char *optionString, void *extraInfo)
162
{
163
J9VMOptionsTable *table = *vmOptionsTable;
164
J9VMOptionsTable *newTable = NULL;
165
166
if (table->count == table->size) {
167
I_32 returnCode = buildNewTable(table->portLib, table->size * 2, table, &newTable);
168
if (J9CMDLINE_OK != returnCode) {
169
return returnCode;
170
}
171
172
vmOptionsTableDestroy(vmOptionsTable);
173
table = newTable;
174
*vmOptionsTable = table;
175
}
176
177
table->vmOptions[table->count].optionString = optionString;
178
table->vmOptions[table->count].extraInfo = extraInfo;
179
180
table->count++;
181
return J9CMDLINE_OK;
182
}
183
184
/*------------------------------------------------------------------
185
*
186
*------------------------------------------------------------------*/
187
I_32
188
vmOptionsTableAddExeName(void **vmOptionsTable, char *argv0)
189
{
190
J9VMOptionsTable *table = *vmOptionsTable;
191
PORT_ACCESS_FROM_PORT(table->portLib);
192
char *value = NULL;
193
char *define = NULL;
194
#define EXE_PROPERTY "-Dcom.ibm.oti.vm.exe="
195
196
if (j9sysinfo_get_executable_name(argv0, &value)) {
197
return J9CMDLINE_GENERIC_ERROR;
198
}
199
200
define = allocString(*vmOptionsTable, sizeof(EXE_PROPERTY) + strlen(value)); /* sizeof() includes room for the terminating \0 */
201
if (NULL == define) {
202
j9mem_free_memory(value);
203
return J9CMDLINE_OUT_OF_MEMORY;
204
}
205
206
strcpy(define, EXE_PROPERTY);
207
strcat(define, value);
208
/* Do /not/ delete executable name string; system-owned. */
209
return vmOptionsTableAddOption(vmOptionsTable, define, NULL);
210
}
211
212
static char *
213
allocString(J9VMOptionsTable *table, UDATA size)
214
{
215
PORT_ACCESS_FROM_PORT(table->portLib);
216
J9LinkedString *node = j9mem_allocate_memory(size + sizeof(J9LinkedString *), OMRMEM_CATEGORY_VM);
217
218
if (NULL == node) {
219
return NULL;
220
}
221
222
node->next = table->strings;
223
table->strings = node;
224
225
return node->data;
226
}
227
228
/*------------------------------------------------------------------
229
* create a new table, copying from old if valid
230
*------------------------------------------------------------------*/
231
static I_32
232
buildNewTable(J9PortLibrary *portLib, int initialCount, J9VMOptionsTable *oldTable, J9VMOptionsTable **newTable)
233
{
234
J9VMOptionsTable *table = NULL;
235
int mallocSize = sizeof(J9VMOptionsTable) + (sizeof(JavaVMOption) * initialCount);
236
I_32 returnCode = 0;
237
238
PORT_ACCESS_FROM_PORT(portLib);
239
240
LOG(("buildNewTable(%d,%p)\n", initialCount, oldTable));
241
LOG(("buildNewtable(): malloc size = %d\n", mallocSize));
242
243
table = j9mem_allocate_memory(mallocSize, OMRMEM_CATEGORY_VM);
244
if (NULL == table) {
245
return J9CMDLINE_OUT_OF_MEMORY;
246
}
247
*newTable = table;
248
249
table->size = initialCount;
250
table->count = 0;
251
table->portLib = portLib;
252
table->vmOptions = (JavaVMOption *) (((char *) table) + sizeof(J9VMOptionsTable));
253
table->strings = NULL;
254
255
LOG(("buildNewtable(): table: %#010X\n",table));
256
LOG(("buildNewtable(): options: %#010X\n",table->vmOptions));
257
258
if (NULL == oldTable) {
259
return J9CMDLINE_OK;
260
}
261
262
LOG(("buildNewtable(): copying table\n"));
263
returnCode = copyTable(table->portLib, table, oldTable);
264
if (J9CMDLINE_OK != returnCode) {
265
return returnCode;
266
}
267
268
LOG(("buildNewtable(): table->size = %d\n",table->size));
269
return J9CMDLINE_OK;
270
}
271
272
/*------------------------------------------------------------------
273
* create a new table, copying from old if valid
274
*------------------------------------------------------------------*/
275
static I_32
276
copyTable(J9PortLibrary *portLib, J9VMOptionsTable *table, J9VMOptionsTable *oldTable)
277
{
278
int i = 0;
279
280
LOG(("copyTable(%p, %p, %d)\n", table, oldTable, copyCount));
281
282
table->strings = oldTable->strings;
283
oldTable->strings = NULL;
284
table->count = oldTable->count;
285
286
for (i = 0; i < oldTable->count; i++) {
287
table->vmOptions[i].optionString = oldTable->vmOptions[i].optionString;
288
table->vmOptions[i].extraInfo = oldTable->vmOptions[i].extraInfo;
289
}
290
291
LOG(("copyTable(): table->count = %d\n",table->count));
292
293
return J9CMDLINE_OK;
294
}
295
296
/*------------------------------------------------------------------
297
*
298
*------------------------------------------------------------------*/
299
I_32
300
vmOptionsTableAddOptionWithCopy(void **vmOptionsTable, char *optionString, void *extraInfo)
301
{
302
char *copyOptionString = allocString(*vmOptionsTable, strlen(optionString) + 1);
303
304
if (NULL == copyOptionString) {
305
return J9CMDLINE_OUT_OF_MEMORY;
306
}
307
strcpy(copyOptionString, optionString);
308
return vmOptionsTableAddOption(vmOptionsTable, copyOptionString, extraInfo);
309
}
310
311
#if defined(WIN32)
312
/* On Windows, the subdirectory containing the redirector hasn't changed with Java versions. */
313
#define J9JAVA_REDIRECTOR_SUBDIR "\\bin\\j9vm\\"
314
#elif (JAVA_SPEC_VERSION >= 10) || ((JAVA_SPEC_VERSION == 9) && defined(OPENJ9_BUILD)) || defined(OSX)
315
/* On other platforms, the subdirectory containing the redirector is common in recent Java versions. */
316
#define J9JAVA_REDIRECTOR_SUBDIR "/lib/j9vm/"
317
#else /* WIN32 */
318
/* In Java 8 and early versions of Java 9, the path depends on the platform architecture. */
319
#if defined(J9HAMMER)
320
#define JVM_ARCH_DIR "amd64"
321
#elif defined(J9ARM)
322
#define JVM_ARCH_DIR "arm"
323
#elif defined(J9AARCH64)
324
#define JVM_ARCH_DIR "aarch64"
325
#elif defined(J9X86)
326
#define JVM_ARCH_DIR "i386"
327
#elif defined(S39064) || defined(J9ZOS39064)
328
#define JVM_ARCH_DIR "s390x"
329
#elif defined(S390) || defined(J9ZOS390)
330
#define JVM_ARCH_DIR "s390"
331
#elif defined(RS6000) || defined(LINUXPPC)
332
#if !defined(PPC64)
333
#define JVM_ARCH_DIR "ppc"
334
#elif defined(J9VM_ENV_LITTLE_ENDIAN)
335
#define JVM_ARCH_DIR "ppc64le"
336
#else /* J9VM_ENV_LITTLE_ENDIAN */
337
#define JVM_ARCH_DIR "ppc64"
338
#endif /* PPC64 */
339
#elif defined(RISCV64)
340
#define JVM_ARCH_DIR "riscv64"
341
#else
342
#error "Must define an architecture"
343
#endif
344
#define J9JAVA_REDIRECTOR_SUBDIR "/lib/" JVM_ARCH_DIR "/j9vm/"
345
#endif /* WIN32 */
346
347
#define JAVAHOMEDIR "-Djava.home="
348
#define JAVAHOMEDIR_LEN (sizeof(JAVAHOMEDIR) - 1)
349
350
/**
351
* This function scans for -Djava.home=xxx/jre. If found, it uses it to construct a path
352
* to redirector jvm.dll and put it in the passed in char buffer:
353
* --> [java.home]/bin/j9vm/
354
* This function is meant to be used by thrstatetest and shrtest so the test can be invoked
355
* in a sdk shape independent way.
356
*/
357
BOOLEAN
358
cmdline_fetchRedirectorDllDir(struct j9cmdlineOptions *args, char *result)
359
{
360
char *tmpresult = NULL;
361
struct j9cmdlineOptions *arg = args;
362
int argc = arg->argc;
363
char **argv = arg->argv;
364
int i = 0;
365
366
/* Find the last -Djava.home= in the args */
367
for (i = 0; i < argc; i++) {
368
char *javaHomeDir = strstr(argv[i], JAVAHOMEDIR);
369
if (NULL != javaHomeDir) {
370
tmpresult = javaHomeDir + JAVAHOMEDIR_LEN;
371
}
372
}
373
374
if (NULL == tmpresult) {
375
printf("ERROR: -Djava.home was not specified on command line.\n");
376
return FALSE;
377
}
378
379
strcpy(result, tmpresult);
380
strcat(result, J9JAVA_REDIRECTOR_SUBDIR);
381
382
return TRUE;
383
}
384
385