Path: blob/master/test/hotspot/gtest/gtestMain.cpp
41140 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include <stdio.h>25#include <string.h>26#include <stdlib.h>27#ifdef __APPLE__28# include <dlfcn.h>29#endif3031#ifdef _WIN3232#include <windows.h>33#else34#include <pthread.h>35#endif3637#include "jni.h"38#include "unittest.hpp"3940#include "runtime/thread.inline.hpp"4142// Default value for -new-thread option: true on AIX because we run into43// problems when attempting to initialize the JVM on the primordial thread.44#ifdef _AIX45const static bool DEFAULT_SPAWN_IN_NEW_THREAD = true;46#else47const static bool DEFAULT_SPAWN_IN_NEW_THREAD = false;48#endif4950static bool is_prefix(const char* prefix, const char* str) {51return strncmp(str, prefix, strlen(prefix)) == 0;52}5354static bool is_suffix(const char* suffix, const char* str) {55size_t suffix_len = strlen(suffix);56size_t str_len = strlen(str);57if (str_len < suffix_len) {58return false;59}60return strncmp(str + (str_len - suffix_len), suffix, suffix_len) == 0;61}626364static int init_jvm(int argc, char **argv, bool disable_error_handling) {65// don't care about the program name66argc--;67argv++;6869int extra_jvm_args = disable_error_handling ? 4 : 2;70int num_jvm_options = argc + extra_jvm_args;7172JavaVMOption* options = new JavaVMOption[num_jvm_options];73options[0].optionString = (char*) "-Dsun.java.launcher.is_altjvm=true";74options[1].optionString = (char*) "-XX:+ExecutingUnitTests";7576if (disable_error_handling) {77// don't create core files or hs_err files executing assert tests78options[2].optionString = (char*) "-XX:+SuppressFatalErrorMessage";79options[3].optionString = (char*) "-XX:-CreateCoredumpOnCrash";80}8182for (int i = 0; i < argc; i++) {83options[extra_jvm_args + i].optionString = argv[i];84}8586JavaVMInitArgs args;87args.version = JNI_VERSION_1_8;88args.nOptions = num_jvm_options;89args.options = options;90args.ignoreUnrecognized = JNI_FALSE;9192JavaVM* jvm;93JNIEnv* env;9495int ret = JNI_CreateJavaVM(&jvm, (void**)&env, &args);96if (ret == JNI_OK) {97// CreateJavaVM leaves WXExec context, while gtests98// calls internal functions assuming running in WXWwrite.99// Switch to WXWrite once for all test cases.100MACOS_AARCH64_ONLY(Thread::current()->enable_wx(WXWrite));101}102return ret;103}104105static bool is_same_vm_test(const char* name) {106return is_suffix("_vm", name) && !is_suffix("_other_vm", name);107}108109class JVMInitializerListener : public ::testing::EmptyTestEventListener {110private:111int _argc;112char** _argv;113bool _is_initialized;114115void initialize_jvm() {116}117118public:119JVMInitializerListener(int argc, char** argv) :120_argc(argc), _argv(argv), _is_initialized(false) {121}122123virtual void OnTestStart(const ::testing::TestInfo& test_info) {124const char* name = test_info.name();125if (!_is_initialized && is_same_vm_test(name)) {126// we want to have hs_err and core files when we execute regular tests127int ret_val = init_jvm(_argc, _argv, false);128if (ret_val != 0) {129ADD_FAILURE() << "Could not initialize the JVM";130exit(1);131}132_is_initialized = true;133}134}135};136137static char* get_java_home_arg(int argc, char** argv) {138for (int i = 0; i < argc; i++) {139if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) {140return argv[i+1];141}142if (is_prefix("--jdk=", argv[i])) {143return argv[i] + strlen("--jdk=");144}145if (is_prefix("-jdk:", argv[i])) {146return argv[i] + strlen("-jdk:");147}148}149return NULL;150}151152static bool get_spawn_new_main_thread_arg(int argc, char** argv) {153// -new-thread[=(true|false)]154for (int i = 0; i < argc; i++) {155if (is_prefix("-new-thread", argv[i])) {156const char* v = argv[i] + strlen("-new-thread");157if (strlen(v) == 0) {158return true;159} else {160if (strcmp(v, "=true") == 0) {161return true;162} else if (strcmp(v, "=false") == 0) {163return false;164} else {165fprintf(stderr, "Invalid value for -new-thread (%s)", v);166}167}168}169}170return DEFAULT_SPAWN_IN_NEW_THREAD;171}172173static int num_args_to_skip(char* arg) {174if (strcmp(arg, "-jdk") == 0) {175return 2; // skip the argument after -jdk as well176}177if (is_prefix("--jdk=", arg)) {178return 1;179}180if (is_prefix("-jdk:", arg)) {181return 1;182}183if (is_prefix("-new-thread", arg)) {184return 1;185}186return 0;187}188189static char** remove_test_runner_arguments(int* argcp, char **argv) {190int argc = *argcp;191char** new_argv = (char**) malloc(sizeof(char*) * argc);192int new_argc = 0;193194int i = 0;195while (i < argc) {196int args_to_skip = num_args_to_skip(argv[i]);197if (args_to_skip == 0) {198new_argv[new_argc] = argv[i];199i++;200new_argc++;201} else {202i += num_args_to_skip(argv[i]);203}204}205206*argcp = new_argc;207return new_argv;208}209210static void runUnitTestsInner(int argc, char** argv) {211::testing::InitGoogleMock(&argc, argv);212::testing::GTEST_FLAG(death_test_style) = "threadsafe";213214bool is_vmassert_test = false;215bool is_othervm_test = false;216// death tests facility is used for both regular death tests, other vm and vmassert tests217if (::testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) {218// when we execute death test, filter value equals to test name219const char* test_name = ::testing::GTEST_FLAG(filter).c_str();220const char* const othervm_suffix = "_other_vm"; // TEST_OTHER_VM221const char* const vmassert_suffix = "_vm_assert"; // TEST_VM_ASSERT(_MSG)222if (is_suffix(othervm_suffix, test_name)) {223is_othervm_test = true;224} else if (is_suffix(vmassert_suffix, test_name)) {225is_vmassert_test = true;226}227}228229char* java_home = get_java_home_arg(argc, argv);230if (java_home == NULL) {231fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n");232exit(1);233}234#ifndef _WIN32235int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME236setenv("JAVA_HOME", java_home, overwrite);237238// workaround for JDK-7131356239#ifdef __APPLE__240size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1;241char* path = new char[len];242snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home);243dlopen(path, RTLD_NOW | RTLD_GLOBAL);244#endif // __APPLE__245246#else // _WIN32247const char* java_home_var = "_ALT_JAVA_HOME_DIR";248size_t len = strlen(java_home) + strlen(java_home_var) + 2;249char * envString = new char[len];250sprintf_s(envString, len, "%s=%s", java_home_var, java_home);251_putenv(envString);252#endif // _WIN32253argv = remove_test_runner_arguments(&argc, argv);254255if (is_vmassert_test || is_othervm_test) {256// both vmassert and other vm tests require inited jvm257// but only vmassert tests disable hs_err and core file generation258if (init_jvm(argc, argv, is_vmassert_test) != 0) {259abort();260}261} else {262::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();263listeners.Append(new JVMInitializerListener(argc, argv));264}265266int result = RUN_ALL_TESTS();267if (result != 0) {268fprintf(stderr, "ERROR: RUN_ALL_TESTS() failed. Error %d\n", result);269exit(2);270}271}272273// Thread support for -new-thread option274275struct args_t {276int argc; char** argv;277};278279#define STACK_SIZE 0x200000280281#ifdef _WIN32282283static DWORD WINAPI thread_wrapper(void* p) {284const args_t* const p_args = (const args_t*) p;285runUnitTestsInner(p_args->argc, p_args->argv);286return 0;287}288289static void run_in_new_thread(const args_t* args) {290HANDLE hdl;291hdl = CreateThread(NULL, STACK_SIZE, thread_wrapper, (void*)args, 0, NULL);292if (hdl == NULL) {293fprintf(stderr, "Failed to create main thread\n");294exit(2);295}296WaitForSingleObject(hdl, INFINITE);297}298299#else300301extern "C" void* thread_wrapper(void* p) {302const args_t* const p_args = (const args_t*) p;303runUnitTestsInner(p_args->argc, p_args->argv);304return 0;305}306307static void run_in_new_thread(const args_t* args) {308pthread_t tid;309pthread_attr_t attr;310311pthread_attr_init(&attr);312pthread_attr_setstacksize(&attr, STACK_SIZE);313314if (pthread_create(&tid, &attr, thread_wrapper, (void*)args) != 0) {315fprintf(stderr, "Failed to create main thread\n");316exit(2);317}318319if (pthread_join(tid, NULL) != 0) {320fprintf(stderr, "Failed to join main thread\n");321exit(2);322}323}324325#endif326327extern "C"328JNIEXPORT void JNICALL runUnitTests(int argc, char** argv) {329const bool spawn_new_main_thread = get_spawn_new_main_thread_arg(argc, argv);330if (spawn_new_main_thread) {331args_t args;332args.argc = argc;333args.argv = argv;334run_in_new_thread(&args);335} else {336runUnitTestsInner(argc, argv);337}338}339340341