Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/gtestMain.cpp
41140 views
1
/*
2
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include <stdio.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#ifdef __APPLE__
29
# include <dlfcn.h>
30
#endif
31
32
#ifdef _WIN32
33
#include <windows.h>
34
#else
35
#include <pthread.h>
36
#endif
37
38
#include "jni.h"
39
#include "unittest.hpp"
40
41
#include "runtime/thread.inline.hpp"
42
43
// Default value for -new-thread option: true on AIX because we run into
44
// problems when attempting to initialize the JVM on the primordial thread.
45
#ifdef _AIX
46
const static bool DEFAULT_SPAWN_IN_NEW_THREAD = true;
47
#else
48
const static bool DEFAULT_SPAWN_IN_NEW_THREAD = false;
49
#endif
50
51
static bool is_prefix(const char* prefix, const char* str) {
52
return strncmp(str, prefix, strlen(prefix)) == 0;
53
}
54
55
static bool is_suffix(const char* suffix, const char* str) {
56
size_t suffix_len = strlen(suffix);
57
size_t str_len = strlen(str);
58
if (str_len < suffix_len) {
59
return false;
60
}
61
return strncmp(str + (str_len - suffix_len), suffix, suffix_len) == 0;
62
}
63
64
65
static int init_jvm(int argc, char **argv, bool disable_error_handling) {
66
// don't care about the program name
67
argc--;
68
argv++;
69
70
int extra_jvm_args = disable_error_handling ? 4 : 2;
71
int num_jvm_options = argc + extra_jvm_args;
72
73
JavaVMOption* options = new JavaVMOption[num_jvm_options];
74
options[0].optionString = (char*) "-Dsun.java.launcher.is_altjvm=true";
75
options[1].optionString = (char*) "-XX:+ExecutingUnitTests";
76
77
if (disable_error_handling) {
78
// don't create core files or hs_err files executing assert tests
79
options[2].optionString = (char*) "-XX:+SuppressFatalErrorMessage";
80
options[3].optionString = (char*) "-XX:-CreateCoredumpOnCrash";
81
}
82
83
for (int i = 0; i < argc; i++) {
84
options[extra_jvm_args + i].optionString = argv[i];
85
}
86
87
JavaVMInitArgs args;
88
args.version = JNI_VERSION_1_8;
89
args.nOptions = num_jvm_options;
90
args.options = options;
91
args.ignoreUnrecognized = JNI_FALSE;
92
93
JavaVM* jvm;
94
JNIEnv* env;
95
96
int ret = JNI_CreateJavaVM(&jvm, (void**)&env, &args);
97
if (ret == JNI_OK) {
98
// CreateJavaVM leaves WXExec context, while gtests
99
// calls internal functions assuming running in WXWwrite.
100
// Switch to WXWrite once for all test cases.
101
MACOS_AARCH64_ONLY(Thread::current()->enable_wx(WXWrite));
102
}
103
return ret;
104
}
105
106
static bool is_same_vm_test(const char* name) {
107
return is_suffix("_vm", name) && !is_suffix("_other_vm", name);
108
}
109
110
class JVMInitializerListener : public ::testing::EmptyTestEventListener {
111
private:
112
int _argc;
113
char** _argv;
114
bool _is_initialized;
115
116
void initialize_jvm() {
117
}
118
119
public:
120
JVMInitializerListener(int argc, char** argv) :
121
_argc(argc), _argv(argv), _is_initialized(false) {
122
}
123
124
virtual void OnTestStart(const ::testing::TestInfo& test_info) {
125
const char* name = test_info.name();
126
if (!_is_initialized && is_same_vm_test(name)) {
127
// we want to have hs_err and core files when we execute regular tests
128
int ret_val = init_jvm(_argc, _argv, false);
129
if (ret_val != 0) {
130
ADD_FAILURE() << "Could not initialize the JVM";
131
exit(1);
132
}
133
_is_initialized = true;
134
}
135
}
136
};
137
138
static char* get_java_home_arg(int argc, char** argv) {
139
for (int i = 0; i < argc; i++) {
140
if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) {
141
return argv[i+1];
142
}
143
if (is_prefix("--jdk=", argv[i])) {
144
return argv[i] + strlen("--jdk=");
145
}
146
if (is_prefix("-jdk:", argv[i])) {
147
return argv[i] + strlen("-jdk:");
148
}
149
}
150
return NULL;
151
}
152
153
static bool get_spawn_new_main_thread_arg(int argc, char** argv) {
154
// -new-thread[=(true|false)]
155
for (int i = 0; i < argc; i++) {
156
if (is_prefix("-new-thread", argv[i])) {
157
const char* v = argv[i] + strlen("-new-thread");
158
if (strlen(v) == 0) {
159
return true;
160
} else {
161
if (strcmp(v, "=true") == 0) {
162
return true;
163
} else if (strcmp(v, "=false") == 0) {
164
return false;
165
} else {
166
fprintf(stderr, "Invalid value for -new-thread (%s)", v);
167
}
168
}
169
}
170
}
171
return DEFAULT_SPAWN_IN_NEW_THREAD;
172
}
173
174
static int num_args_to_skip(char* arg) {
175
if (strcmp(arg, "-jdk") == 0) {
176
return 2; // skip the argument after -jdk as well
177
}
178
if (is_prefix("--jdk=", arg)) {
179
return 1;
180
}
181
if (is_prefix("-jdk:", arg)) {
182
return 1;
183
}
184
if (is_prefix("-new-thread", arg)) {
185
return 1;
186
}
187
return 0;
188
}
189
190
static char** remove_test_runner_arguments(int* argcp, char **argv) {
191
int argc = *argcp;
192
char** new_argv = (char**) malloc(sizeof(char*) * argc);
193
int new_argc = 0;
194
195
int i = 0;
196
while (i < argc) {
197
int args_to_skip = num_args_to_skip(argv[i]);
198
if (args_to_skip == 0) {
199
new_argv[new_argc] = argv[i];
200
i++;
201
new_argc++;
202
} else {
203
i += num_args_to_skip(argv[i]);
204
}
205
}
206
207
*argcp = new_argc;
208
return new_argv;
209
}
210
211
static void runUnitTestsInner(int argc, char** argv) {
212
::testing::InitGoogleMock(&argc, argv);
213
::testing::GTEST_FLAG(death_test_style) = "threadsafe";
214
215
bool is_vmassert_test = false;
216
bool is_othervm_test = false;
217
// death tests facility is used for both regular death tests, other vm and vmassert tests
218
if (::testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) {
219
// when we execute death test, filter value equals to test name
220
const char* test_name = ::testing::GTEST_FLAG(filter).c_str();
221
const char* const othervm_suffix = "_other_vm"; // TEST_OTHER_VM
222
const char* const vmassert_suffix = "_vm_assert"; // TEST_VM_ASSERT(_MSG)
223
if (is_suffix(othervm_suffix, test_name)) {
224
is_othervm_test = true;
225
} else if (is_suffix(vmassert_suffix, test_name)) {
226
is_vmassert_test = true;
227
}
228
}
229
230
char* java_home = get_java_home_arg(argc, argv);
231
if (java_home == NULL) {
232
fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n");
233
exit(1);
234
}
235
#ifndef _WIN32
236
int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME
237
setenv("JAVA_HOME", java_home, overwrite);
238
239
// workaround for JDK-7131356
240
#ifdef __APPLE__
241
size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1;
242
char* path = new char[len];
243
snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home);
244
dlopen(path, RTLD_NOW | RTLD_GLOBAL);
245
#endif // __APPLE__
246
247
#else // _WIN32
248
const char* java_home_var = "_ALT_JAVA_HOME_DIR";
249
size_t len = strlen(java_home) + strlen(java_home_var) + 2;
250
char * envString = new char[len];
251
sprintf_s(envString, len, "%s=%s", java_home_var, java_home);
252
_putenv(envString);
253
#endif // _WIN32
254
argv = remove_test_runner_arguments(&argc, argv);
255
256
if (is_vmassert_test || is_othervm_test) {
257
// both vmassert and other vm tests require inited jvm
258
// but only vmassert tests disable hs_err and core file generation
259
if (init_jvm(argc, argv, is_vmassert_test) != 0) {
260
abort();
261
}
262
} else {
263
::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
264
listeners.Append(new JVMInitializerListener(argc, argv));
265
}
266
267
int result = RUN_ALL_TESTS();
268
if (result != 0) {
269
fprintf(stderr, "ERROR: RUN_ALL_TESTS() failed. Error %d\n", result);
270
exit(2);
271
}
272
}
273
274
// Thread support for -new-thread option
275
276
struct args_t {
277
int argc; char** argv;
278
};
279
280
#define STACK_SIZE 0x200000
281
282
#ifdef _WIN32
283
284
static DWORD WINAPI thread_wrapper(void* p) {
285
const args_t* const p_args = (const args_t*) p;
286
runUnitTestsInner(p_args->argc, p_args->argv);
287
return 0;
288
}
289
290
static void run_in_new_thread(const args_t* args) {
291
HANDLE hdl;
292
hdl = CreateThread(NULL, STACK_SIZE, thread_wrapper, (void*)args, 0, NULL);
293
if (hdl == NULL) {
294
fprintf(stderr, "Failed to create main thread\n");
295
exit(2);
296
}
297
WaitForSingleObject(hdl, INFINITE);
298
}
299
300
#else
301
302
extern "C" void* thread_wrapper(void* p) {
303
const args_t* const p_args = (const args_t*) p;
304
runUnitTestsInner(p_args->argc, p_args->argv);
305
return 0;
306
}
307
308
static void run_in_new_thread(const args_t* args) {
309
pthread_t tid;
310
pthread_attr_t attr;
311
312
pthread_attr_init(&attr);
313
pthread_attr_setstacksize(&attr, STACK_SIZE);
314
315
if (pthread_create(&tid, &attr, thread_wrapper, (void*)args) != 0) {
316
fprintf(stderr, "Failed to create main thread\n");
317
exit(2);
318
}
319
320
if (pthread_join(tid, NULL) != 0) {
321
fprintf(stderr, "Failed to join main thread\n");
322
exit(2);
323
}
324
}
325
326
#endif
327
328
extern "C"
329
JNIEXPORT void JNICALL runUnitTests(int argc, char** argv) {
330
const bool spawn_new_main_thread = get_spawn_new_main_thread_arg(argc, argv);
331
if (spawn_new_main_thread) {
332
args_t args;
333
args.argc = argc;
334
args.argv = argv;
335
run_in_new_thread(&args);
336
} else {
337
runUnitTestsInner(argc, argv);
338
}
339
}
340
341