1 /*
   2  * Copyright (c) 2016, 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 #include "prims/jni.h"
  33 #include "unittest.hpp"
  34 
  35 extern "C" {
  36 
  37 static bool is_prefix(const char* prefix, const char* str) {
  38   return strncmp(str, prefix, strlen(prefix)) == 0;
  39 }
  40 
  41 static bool is_suffix(const char* suffix, const char* str) {
  42   size_t suffix_len = strlen(suffix);
  43   size_t str_len = strlen(str);
  44   if (str_len < suffix_len) {
  45       return false;
  46   }
  47   return strncmp(str + (str_len - suffix_len), suffix, suffix_len) == 0;
  48 }
  49 
  50 
  51 static int init_jvm(int argc, char **argv, bool disable_error_handling) {
  52   // don't care about the program name
  53   argc--;
  54   argv++;
  55 
  56   int extra_jvm_args = disable_error_handling ? 4 : 2;
  57   int num_jvm_options = argc + extra_jvm_args;
  58 
  59   JavaVMOption* options = new JavaVMOption[num_jvm_options];
  60   options[0].optionString = (char*) "-Dsun.java.launcher.is_altjvm=true";
  61   options[1].optionString = (char*) "-XX:+ExecutingUnitTests";
  62 
  63   if (disable_error_handling) {
  64     // don't create core files or hs_err files executing assert tests
  65     options[2].optionString = (char*) "-XX:+SuppressFatalErrorMessage";
  66     options[3].optionString = (char*) "-XX:-CreateCoredumpOnCrash";
  67   }
  68 
  69   for (int i = 0; i < argc; i++) {
  70     options[extra_jvm_args + i].optionString = argv[i];
  71   }
  72 
  73   JavaVMInitArgs args;
  74   args.version = JNI_VERSION_1_8;
  75   args.nOptions = num_jvm_options;
  76   args.options = options;
  77 
  78   JavaVM* jvm;
  79   JNIEnv* env;
  80 
  81   return JNI_CreateJavaVM(&jvm, (void**)&env, &args);
  82 }
  83 
  84 class JVMInitializerListener : public ::testing::EmptyTestEventListener {
  85  private:
  86   int _argc;
  87   char** _argv;
  88   bool _is_initialized;
  89 
  90   void initialize_jvm() {
  91   }
  92 
  93  public:
  94   JVMInitializerListener(int argc, char** argv) :
  95     _argc(argc), _argv(argv), _is_initialized(false) {
  96   }
  97 
  98   virtual void OnTestStart(const ::testing::TestInfo& test_info) {
  99     const char* name = test_info.name();
 100     if (!_is_initialized && is_suffix("_test_vm", name)) {
 101       // we want to have hs_err and core files when we execute regular tests
 102       ASSERT_EQ(0, init_jvm(_argc, _argv, false)) << "Could not initialize the JVM";
 103       _is_initialized = true;
 104     }
 105   }
 106 };
 107 
 108 static char* get_java_home_arg(int argc, char** argv) {
 109   for (int i = 0; i < argc; i++) {
 110     if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) {
 111       return argv[i+1];
 112     }
 113     if (is_prefix("--jdk=", argv[i])) {
 114       return argv[i] + strlen("--jdk=");
 115     }
 116     if (is_prefix("-jdk:", argv[i])) {
 117       return argv[i] + strlen("-jdk:");
 118     }
 119   }
 120   return NULL;
 121 }
 122 
 123 static int num_args_to_skip(char* arg) {
 124   if (strcmp(arg, "-jdk") == 0) {
 125     return 2; // skip the argument after -jdk as well
 126   }
 127   if (is_prefix("--jdk=", arg)) {
 128     return 1;
 129   }
 130   if (is_prefix("-jdk:", arg)) {
 131     return 1;
 132   }
 133   return 0;
 134 }
 135 
 136 static char** remove_test_runner_arguments(int* argcp, char **argv) {
 137   int argc = *argcp;
 138   char** new_argv = (char**) malloc(sizeof(char*) * argc);
 139   int new_argc = 0;
 140 
 141   int i = 0;
 142   while (i < argc) {
 143     int args_to_skip = num_args_to_skip(argv[i]);
 144     if (args_to_skip == 0) {
 145       new_argv[new_argc] = argv[i];
 146       i++;
 147       new_argc++;
 148     } else {
 149       i += num_args_to_skip(argv[i]);
 150     }
 151   }
 152 
 153   *argcp = new_argc;
 154   return new_argv;
 155 }
 156 
 157 JNIEXPORT void JNICALL runUnitTests(int argc, char** argv) {
 158   ::testing::InitGoogleTest(&argc, argv);
 159   ::testing::GTEST_FLAG(death_test_style) = "threadsafe";
 160 
 161   bool is_vmassert_test = false;
 162   bool is_othervm_test = false;
 163   // death tests facility is used for both regular death tests, other vm and vmassert tests
 164   if (::testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) {
 165     // when we execute death test, filter value equals to test name
 166     const char* test_name = ::testing::GTEST_FLAG(filter).c_str();
 167     const char* const othervm_suffix = "_other_vm_test"; // TEST_OTHER_VM
 168     const char* const vmassert_suffix = "_vm_assert_test"; // TEST_VM_ASSERT(_MSG)
 169     if (is_suffix(othervm_suffix, test_name)) {
 170       is_othervm_test = true;
 171     } else if (is_suffix(vmassert_suffix, test_name)) {
 172       is_vmassert_test = true;
 173     }
 174   }
 175 
 176   char* java_home = get_java_home_arg(argc, argv);
 177   if (java_home == NULL) {
 178     fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n");
 179     exit(1);
 180   }
 181 #ifndef _WIN32
 182   int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME
 183   setenv("JAVA_HOME", java_home, overwrite);
 184 
 185 // workaround for JDK-7131356
 186 #ifdef __APPLE__
 187   size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1;
 188   char* path = new char[len];
 189   snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home);
 190   dlopen(path, RTLD_NOW | RTLD_GLOBAL);
 191 #endif // __APPLE__
 192 
 193 #else  // _WIN32
 194   char* java_home_var = "_ALT_JAVA_HOME_DIR";
 195   size_t len = strlen(java_home) + strlen(java_home_var) + 2;
 196   char * envString = new char[len];
 197   sprintf_s(envString, len, "%s=%s", java_home_var, java_home);
 198   _putenv(envString);
 199 #endif // _WIN32
 200   argv = remove_test_runner_arguments(&argc, argv);
 201 
 202   if (is_vmassert_test || is_othervm_test) {
 203     // both vmassert and other vm tests require inited jvm
 204     // but only vmassert tests disable hs_err and core file generation
 205     if (init_jvm(argc, argv, is_vmassert_test) != 0) {
 206       abort();
 207     }
 208   } else {
 209     ::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
 210     listeners.Append(new JVMInitializerListener(argc, argv));
 211   }
 212 
 213   int result = RUN_ALL_TESTS();
 214   if (result != 0) {
 215     fprintf(stderr, "ERROR: RUN_ALL_TESTS() failed. Error %d\n", result);
 216     exit(2);
 217   }
 218 }
 219 
 220 } // extern "C"