Path: blob/master/test/hotspot/gtest/gtestMain.cpp
64435 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}6263static int init_jvm(int argc, char **argv, bool disable_error_handling, JavaVM** jvm_ptr) {64// don't care about the program name65argc--;66argv++;6768int extra_jvm_args = disable_error_handling ? 4 : 2;69int num_jvm_options = argc + extra_jvm_args;7071JavaVMOption* options = new JavaVMOption[num_jvm_options];72options[0].optionString = (char*) "-Dsun.java.launcher.is_altjvm=true";73options[1].optionString = (char*) "-XX:+ExecutingUnitTests";7475if (disable_error_handling) {76// don't create core files or hs_err files executing assert tests77options[2].optionString = (char*) "-XX:+SuppressFatalErrorMessage";78options[3].optionString = (char*) "-XX:-CreateCoredumpOnCrash";79}8081for (int i = 0; i < argc; i++) {82options[extra_jvm_args + i].optionString = argv[i];83}8485JavaVMInitArgs args;86args.version = JNI_VERSION_1_8;87args.nOptions = num_jvm_options;88args.options = options;89args.ignoreUnrecognized = JNI_FALSE;9091JNIEnv* env;9293int ret = JNI_CreateJavaVM(jvm_ptr, (void**)&env, &args);94if (ret == JNI_OK) {95// CreateJavaVM leaves WXExec context, while gtests96// calls internal functions assuming running in WXWwrite.97// Switch to WXWrite once for all test cases.98MACOS_AARCH64_ONLY(Thread::current()->enable_wx(WXWrite));99}100return ret;101}102103static bool is_same_vm_test(const char* name) {104return is_suffix("_vm", name) && !is_suffix("_other_vm", name);105}106107class JVMInitializerListener : public ::testing::EmptyTestEventListener {108private:109int _argc;110char** _argv;111JavaVM* _jvm;112113public:114JVMInitializerListener(int argc, char** argv) :115_argc(argc), _argv(argv), _jvm(nullptr) {116}117118virtual void OnTestStart(const ::testing::TestInfo& test_info) {119const char* name = test_info.name();120if (_jvm == nullptr && is_same_vm_test(name)) {121// we want to have hs_err and core files when we execute regular tests122int ret_val = init_jvm(_argc, _argv, false, &_jvm);123if (ret_val != 0) {124ADD_FAILURE() << "Could not initialize the JVM: " << ret_val;125exit(1);126}127}128}129130void destroy_jvm() {131if (_jvm != NULL) {132int ret = _jvm->DestroyJavaVM();133if (ret != 0) {134fprintf(stderr, "Warning: DestroyJavaVM error %d\n", ret);135}136}137}138};139140static char* get_java_home_arg(int argc, char** argv) {141for (int i = 0; i < argc; i++) {142if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) {143return argv[i+1];144}145if (is_prefix("--jdk=", argv[i])) {146return argv[i] + strlen("--jdk=");147}148if (is_prefix("-jdk:", argv[i])) {149return argv[i] + strlen("-jdk:");150}151}152return NULL;153}154155static bool get_spawn_new_main_thread_arg(int argc, char** argv) {156// -new-thread[=(true|false)]157for (int i = 0; i < argc; i++) {158if (is_prefix("-new-thread", argv[i])) {159const char* v = argv[i] + strlen("-new-thread");160if (strlen(v) == 0) {161return true;162} else {163if (strcmp(v, "=true") == 0) {164return true;165} else if (strcmp(v, "=false") == 0) {166return false;167} else {168fprintf(stderr, "Invalid value for -new-thread (%s)", v);169}170}171}172}173return DEFAULT_SPAWN_IN_NEW_THREAD;174}175176static int num_args_to_skip(char* arg) {177if (strcmp(arg, "-jdk") == 0) {178return 2; // skip the argument after -jdk as well179}180if (is_prefix("--jdk=", arg)) {181return 1;182}183if (is_prefix("-jdk:", arg)) {184return 1;185}186if (is_prefix("-new-thread", arg)) {187return 1;188}189return 0;190}191192static char** remove_test_runner_arguments(int* argcp, char **argv) {193int argc = *argcp;194char** new_argv = (char**) malloc(sizeof(char*) * argc);195int new_argc = 0;196197int i = 0;198while (i < argc) {199int args_to_skip = num_args_to_skip(argv[i]);200if (args_to_skip == 0) {201new_argv[new_argc] = argv[i];202i++;203new_argc++;204} else {205i += num_args_to_skip(argv[i]);206}207}208209*argcp = new_argc;210return new_argv;211}212213// This is generally run once for a set of tests. But if that set includes a vm_assert or214// other_vm test, then a new process is forked, and runUnitTestsInner is called, passing215// just that test as the one to be executed.216//217// When we execute a vm_assert or other_vm test we create and initialize the JVM below.218//219// A vm_assert test crashes the VM so no cleanup is needed, but for other_vm we call220// DestroyJavaVM via the TEST_OTHER_VM macro prior to the call to exit().221//222// For same_vm tests we use an event listener to create the JVM when the first same_vm223// test is executed. Once all tests are completed we can then call DestroyJavaVM on that224// JVM directly.225static void runUnitTestsInner(int argc, char** argv) {226::testing::InitGoogleMock(&argc, argv);227::testing::GTEST_FLAG(death_test_style) = "threadsafe";228229bool is_vmassert_test = false;230bool is_othervm_test = false;231// death tests facility is used for both regular death tests, other vm and vmassert tests232if (::testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) {233// when we execute death test, filter value equals to test name234const char* test_name = ::testing::GTEST_FLAG(filter).c_str();235const char* const othervm_suffix = "_other_vm"; // TEST_OTHER_VM236const char* const vmassert_suffix = "_vm_assert"; // TEST_VM_ASSERT(_MSG)237if (is_suffix(othervm_suffix, test_name)) {238is_othervm_test = true;239} else if (is_suffix(vmassert_suffix, test_name)) {240is_vmassert_test = true;241}242}243244char* java_home = get_java_home_arg(argc, argv);245if (java_home == NULL) {246fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n");247exit(1);248}249#ifndef _WIN32250int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME251setenv("JAVA_HOME", java_home, overwrite);252253// workaround for JDK-7131356254#ifdef __APPLE__255size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1;256char* path = new char[len];257snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home);258dlopen(path, RTLD_NOW | RTLD_GLOBAL);259#endif // __APPLE__260261#else // _WIN32262const char* java_home_var = "_ALT_JAVA_HOME_DIR";263size_t len = strlen(java_home) + strlen(java_home_var) + 2;264char * envString = new char[len];265sprintf_s(envString, len, "%s=%s", java_home_var, java_home);266_putenv(envString);267#endif // _WIN32268argv = remove_test_runner_arguments(&argc, argv);269270271JVMInitializerListener* jvm_listener = NULL;272273if (is_vmassert_test || is_othervm_test) {274JavaVM* jvm = NULL;275// both vmassert and other vm tests require inited jvm276// but only vmassert tests disable hs_err and core file generation277int ret;278if ((ret = init_jvm(argc, argv, is_vmassert_test, &jvm)) != 0) {279fprintf(stderr, "ERROR: JNI_CreateJavaVM failed: %d\n", ret);280abort();281}282} else {283::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();284jvm_listener = new JVMInitializerListener(argc, argv);285listeners.Append(jvm_listener);286}287288int result = RUN_ALL_TESTS();289290// vm_assert and other_vm tests never reach this point as they either abort, or call291// exit() - see TEST_OTHER_VM macro. We will reach here when all same_vm tests have292// completed for this run, so we can terminate the VM used for that case.293294if (result != 0) {295fprintf(stderr, "ERROR: RUN_ALL_TESTS() failed. Error %d\n", result);296exit(2);297}298299if (jvm_listener != NULL) {300jvm_listener->destroy_jvm();301}302}303304// Thread support for -new-thread option305306struct args_t {307int argc; char** argv;308};309310#define STACK_SIZE 0x200000311312#ifdef _WIN32313314static DWORD WINAPI thread_wrapper(void* p) {315const args_t* const p_args = (const args_t*) p;316runUnitTestsInner(p_args->argc, p_args->argv);317return 0;318}319320static void run_in_new_thread(const args_t* args) {321HANDLE hdl;322hdl = CreateThread(NULL, STACK_SIZE, thread_wrapper, (void*)args, 0, NULL);323if (hdl == NULL) {324fprintf(stderr, "Failed to create main thread\n");325exit(2);326}327WaitForSingleObject(hdl, INFINITE);328}329330#else331332extern "C" void* thread_wrapper(void* p) {333const args_t* const p_args = (const args_t*) p;334runUnitTestsInner(p_args->argc, p_args->argv);335return 0;336}337338static void run_in_new_thread(const args_t* args) {339pthread_t tid;340pthread_attr_t attr;341342pthread_attr_init(&attr);343pthread_attr_setstacksize(&attr, STACK_SIZE);344345if (pthread_create(&tid, &attr, thread_wrapper, (void*)args) != 0) {346fprintf(stderr, "Failed to create main thread\n");347exit(2);348}349350if (pthread_join(tid, NULL) != 0) {351fprintf(stderr, "Failed to join main thread\n");352exit(2);353}354}355356#endif357358extern "C"359JNIEXPORT void JNICALL runUnitTests(int argc, char** argv) {360const bool spawn_new_main_thread = get_spawn_new_main_thread_arg(argc, argv);361if (spawn_new_main_thread) {362args_t args;363args.argc = argc;364args.argv = argv;365run_in_new_thread(&args);366} else {367runUnitTestsInner(argc, argv);368}369}370371372