1 /* 2 * Copyright (c) 1997, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <windows.h> 27 #include <io.h> 28 #include <process.h> 29 #include <stdlib.h> 30 #include <stdio.h> 31 #include <stdarg.h> 32 #include <string.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <wtypes.h> 36 #include <commctrl.h> 37 38 #include <jni.h> 39 #include "java.h" 40 41 #define JVM_DLL "jvm.dll" 42 #define JAVA_DLL "java.dll" 43 44 /* 45 * Prototypes. 46 */ 47 static jboolean GetJVMPath(const char *jrepath, const char *jvmtype, 48 char *jvmpath, jint jvmpathsize); 49 static jboolean GetJREPath(char *path, jint pathsize); 50 51 #ifdef USE_REGISTRY_LOOKUP 52 jboolean GetPublicJREHome(char *buf, jint bufsize); 53 #endif 54 55 /* We supports warmup for UI stack that is performed in parallel 56 * to VM initialization. 57 * This helps to improve startup of UI application as warmup phase 58 * might be long due to initialization of OS or hardware resources. 59 * It is not CPU bound and therefore it does not interfere with VM init. 60 * Obviously such warmup only has sense for UI apps and therefore it needs 61 * to be explicitly requested by passing -Dsun.awt.warmup=true property 62 * (this is always the case for plugin/javaws). 63 * 64 * Implementation launches new thread after VM starts and use it to perform 65 * warmup code (platform dependent). 66 * This thread is later reused as AWT toolkit thread as graphics toolkit 67 * often assume that they are used from the same thread they were launched on. 68 * 69 * At the moment we only support warmup for D3D. It only possible on windows 70 * and only if other flags do not prohibit this (e.g. OpenGL support requested). 71 */ 72 #undef ENABLE_AWT_PRELOAD 73 #ifndef JAVA_ARGS /* turn off AWT preloading for javac, jar, etc */ 74 /* CR6999872: fastdebug crashes if awt library is loaded before JVM is 75 * initialized*/ 76 #if !defined(DEBUG) 77 #define ENABLE_AWT_PRELOAD 78 #endif 79 #endif 80 81 #ifdef ENABLE_AWT_PRELOAD 82 /* "AWT was preloaded" flag; 83 * turned on by AWTPreload(). 84 */ 85 int awtPreloaded = 0; 86 87 /* Calls a function with the name specified 88 * the function must be int(*fn)(void). 89 */ 90 int AWTPreload(const char *funcName); 91 /* stops AWT preloading */ 92 void AWTPreloadStop(); 93 94 /* D3D preloading */ 95 /* -1: not initialized; 0: OFF, 1: ON */ 96 int awtPreloadD3D = -1; 97 /* command line parameter to swith D3D preloading on */ 98 #define PARAM_PRELOAD_D3D "-Dsun.awt.warmup" 99 /* D3D/OpenGL management parameters */ 100 #define PARAM_NODDRAW "-Dsun.java2d.noddraw" 101 #define PARAM_D3D "-Dsun.java2d.d3d" 102 #define PARAM_OPENGL "-Dsun.java2d.opengl" 103 /* funtion in awt.dll (src/windows/native/sun/java2d/d3d/D3DPipelineManager.cpp) */ 104 #define D3D_PRELOAD_FUNC "preloadD3D" 105 106 /* Extracts value of a parameter with the specified name 107 * from command line argument (returns pointer in the argument). 108 * Returns NULL if the argument does not contains the parameter. 109 * e.g.: 110 * GetParamValue("theParam", "theParam=value") returns pointer to "value". 111 */ 112 const char * GetParamValue(const char *paramName, const char *arg) { 113 size_t nameLen = JLI_StrLen(paramName); 114 if (JLI_StrNCmp(paramName, arg, nameLen) == 0) { 115 /* arg[nameLen] is valid (may contain final NULL) */ 116 if (arg[nameLen] == '=') { 117 return arg + nameLen + 1; 118 } 119 } 120 return NULL; 121 } 122 123 /* Checks if commandline argument contains property specified 124 * and analyze it as boolean property (true/false). 125 * Returns -1 if the argument does not contain the parameter; 126 * Returns 1 if the argument contains the parameter and its value is "true"; 127 * Returns 0 if the argument contains the parameter and its value is "false". 128 */ 129 int GetBoolParamValue(const char *paramName, const char *arg) { 130 const char * paramValue = GetParamValue(paramName, arg); 131 if (paramValue != NULL) { 132 if (JLI_StrCaseCmp(paramValue, "true") == 0) { 133 return 1; 134 } 135 if (JLI_StrCaseCmp(paramValue, "false") == 0) { 136 return 0; 137 } 138 } 139 return -1; 140 } 141 #endif /* ENABLE_AWT_PRELOAD */ 142 143 144 static jboolean _isjavaw = JNI_FALSE; 145 146 147 jboolean 148 IsJavaw() 149 { 150 return _isjavaw; 151 } 152 153 /* 154 * 155 */ 156 void 157 CreateExecutionEnvironment(int *pargc, char ***pargv, 158 char *jrepath, jint so_jrepath, 159 char *jvmpath, jint so_jvmpath, 160 char *jvmcfg, jint so_jvmcfg) { 161 char * jvmtype; 162 int i = 0; 163 int running = CURRENT_DATA_MODEL; 164 165 int wanted = running; 166 167 char** argv = *pargv; 168 for (i = 1; i < *pargc ; i++) { 169 if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) { 170 wanted = 64; 171 continue; 172 } 173 if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) { 174 wanted = 32; 175 continue; 176 } 177 178 if (IsJavaArgs() && argv[i][0] != '-') 179 continue; 180 if (argv[i][0] != '-') 181 break; 182 } 183 if (running != wanted) { 184 JLI_ReportErrorMessage(JRE_ERROR2, wanted); 185 exit(1); 186 } 187 188 /* Find out where the JRE is that we will be using. */ 189 if (!GetJREPath(jrepath, so_jrepath)) { 190 JLI_ReportErrorMessage(JRE_ERROR1); 191 exit(2); 192 } 193 194 JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%sjvm.cfg", 195 jrepath, FILESEP, FILESEP); 196 197 /* Find the specified JVM type */ 198 if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) { 199 JLI_ReportErrorMessage(CFG_ERROR7); 200 exit(1); 201 } 202 203 jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE); 204 if (JLI_StrCmp(jvmtype, "ERROR") == 0) { 205 JLI_ReportErrorMessage(CFG_ERROR9); 206 exit(4); 207 } 208 209 jvmpath[0] = '\0'; 210 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) { 211 JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath); 212 exit(4); 213 } 214 /* If we got here, jvmpath has been correctly initialized. */ 215 216 /* Check if we need preload AWT */ 217 #ifdef ENABLE_AWT_PRELOAD 218 argv = *pargv; 219 for (i = 0; i < *pargc ; i++) { 220 /* Tests the "turn on" parameter only if not set yet. */ 221 if (awtPreloadD3D < 0) { 222 if (GetBoolParamValue(PARAM_PRELOAD_D3D, argv[i]) == 1) { 223 awtPreloadD3D = 1; 224 } 225 } 226 /* Test parameters which can disable preloading if not already disabled. */ 227 if (awtPreloadD3D != 0) { 228 if (GetBoolParamValue(PARAM_NODDRAW, argv[i]) == 1 229 || GetBoolParamValue(PARAM_D3D, argv[i]) == 0 230 || GetBoolParamValue(PARAM_OPENGL, argv[i]) == 1) 231 { 232 awtPreloadD3D = 0; 233 /* no need to test the rest of the parameters */ 234 break; 235 } 236 } 237 } 238 #endif /* ENABLE_AWT_PRELOAD */ 239 } 240 241 242 static jboolean 243 LoadMSVCRT() 244 { 245 // Only do this once 246 static int loaded = 0; 247 char crtpath[MAXPATHLEN]; 248 249 if (!loaded) { 250 /* 251 * The Microsoft C Runtime Library needs to be loaded first. A copy is 252 * assumed to be present in the "JRE path" directory. If it is not found 253 * there (or "JRE path" fails to resolve), skip the explicit load and let 254 * nature take its course, which is likely to be a failure to execute. 255 * The makefiles will provide the correct lib contained in quotes in the 256 * macro MSVCR_DLL_NAME. 257 */ 258 #ifdef MSVCR_DLL_NAME 259 if (GetJREPath(crtpath, MAXPATHLEN)) { 260 if (JLI_StrLen(crtpath) + JLI_StrLen("\\bin\\") + 261 JLI_StrLen(MSVCR_DLL_NAME) >= MAXPATHLEN) { 262 JLI_ReportErrorMessage(JRE_ERROR11); 263 return JNI_FALSE; 264 } 265 (void)JLI_StrCat(crtpath, "\\bin\\" MSVCR_DLL_NAME); /* Add crt dll */ 266 JLI_TraceLauncher("CRT path is %s\n", crtpath); 267 if (_access(crtpath, 0) == 0) { 268 if (LoadLibrary(crtpath) == 0) { 269 JLI_ReportErrorMessage(DLL_ERROR4, crtpath); 270 return JNI_FALSE; 271 } 272 } 273 } 274 #endif /* MSVCR_DLL_NAME */ 275 #ifdef MSVCP_DLL_NAME 276 if (GetJREPath(crtpath, MAXPATHLEN)) { 277 if (JLI_StrLen(crtpath) + JLI_StrLen("\\bin\\") + 278 JLI_StrLen(MSVCP_DLL_NAME) >= MAXPATHLEN) { 279 JLI_ReportErrorMessage(JRE_ERROR11); 280 return JNI_FALSE; 281 } 282 (void)JLI_StrCat(crtpath, "\\bin\\" MSVCP_DLL_NAME); /* Add prt dll */ 283 JLI_TraceLauncher("PRT path is %s\n", crtpath); 284 if (_access(crtpath, 0) == 0) { 285 if (LoadLibrary(crtpath) == 0) { 286 JLI_ReportErrorMessage(DLL_ERROR4, crtpath); 287 return JNI_FALSE; 288 } 289 } 290 } 291 #endif /* MSVCP_DLL_NAME */ 292 loaded = 1; 293 } 294 return JNI_TRUE; 295 } 296 297 298 /* 299 * Find path to JRE based on .exe's location or registry settings. 300 */ 301 jboolean 302 GetJREPath(char *path, jint pathsize) 303 { 304 char javadll[MAXPATHLEN]; 305 struct stat s; 306 307 if (GetApplicationHome(path, pathsize)) { 308 /* Is JRE co-located with the application? */ 309 JLI_Snprintf(javadll, sizeof(javadll), "%s\\bin\\" JAVA_DLL, path); 310 if (stat(javadll, &s) == 0) { 311 JLI_TraceLauncher("JRE path is %s\n", path); 312 return JNI_TRUE; 313 } 314 /* ensure storage for path + \jre + NULL */ 315 if ((JLI_StrLen(path) + 4 + 1) > (size_t) pathsize) { 316 JLI_TraceLauncher("Insufficient space to store JRE path\n"); 317 return JNI_FALSE; 318 } 319 /* Does this app ship a private JRE in <apphome>\jre directory? */ 320 JLI_Snprintf(javadll, sizeof (javadll), "%s\\jre\\bin\\" JAVA_DLL, path); 321 if (stat(javadll, &s) == 0) { 322 JLI_StrCat(path, "\\jre"); 323 JLI_TraceLauncher("JRE path is %s\n", path); 324 return JNI_TRUE; 325 } 326 } 327 328 /* Try getting path to JRE from path to JLI.DLL */ 329 if (GetApplicationHomeFromDll(path, pathsize)) { 330 JLI_Snprintf(javadll, sizeof(javadll), "%s\\bin\\" JAVA_DLL, path); 331 if (stat(javadll, &s) == 0) { 332 JLI_TraceLauncher("JRE path is %s\n", path); 333 return JNI_TRUE; 334 } 335 } 336 337 #ifdef USE_REGISTRY_LOOKUP 338 /* Lookup public JRE using Windows registry. */ 339 if (GetPublicJREHome(path, pathsize)) { 340 JLI_TraceLauncher("JRE path is %s\n", path); 341 return JNI_TRUE; 342 } 343 #endif 344 345 JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL); 346 return JNI_FALSE; 347 } 348 349 /* 350 * Given a JRE location and a JVM type, construct what the name the 351 * JVM shared library will be. Return true, if such a library 352 * exists, false otherwise. 353 */ 354 static jboolean 355 GetJVMPath(const char *jrepath, const char *jvmtype, 356 char *jvmpath, jint jvmpathsize) 357 { 358 struct stat s; 359 if (JLI_StrChr(jvmtype, '/') || JLI_StrChr(jvmtype, '\\')) { 360 JLI_Snprintf(jvmpath, jvmpathsize, "%s\\" JVM_DLL, jvmtype); 361 } else { 362 JLI_Snprintf(jvmpath, jvmpathsize, "%s\\bin\\%s\\" JVM_DLL, 363 jrepath, jvmtype); 364 } 365 if (stat(jvmpath, &s) == 0) { 366 return JNI_TRUE; 367 } else { 368 return JNI_FALSE; 369 } 370 } 371 372 /* 373 * Load a jvm from "jvmpath" and initialize the invocation functions. 374 */ 375 jboolean 376 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn) 377 { 378 HINSTANCE handle; 379 380 JLI_TraceLauncher("JVM path is %s\n", jvmpath); 381 382 /* 383 * The Microsoft C Runtime Library needs to be loaded first. A copy is 384 * assumed to be present in the "JRE path" directory. If it is not found 385 * there (or "JRE path" fails to resolve), skip the explicit load and let 386 * nature take its course, which is likely to be a failure to execute. 387 * 388 */ 389 LoadMSVCRT(); 390 391 /* Load the Java VM DLL */ 392 if ((handle = LoadLibrary(jvmpath)) == 0) { 393 JLI_ReportErrorMessage(DLL_ERROR4, (char *)jvmpath); 394 return JNI_FALSE; 395 } 396 397 /* Now get the function addresses */ 398 ifn->CreateJavaVM = 399 (void *)GetProcAddress(handle, "JNI_CreateJavaVM"); 400 ifn->GetDefaultJavaVMInitArgs = 401 (void *)GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs"); 402 if (ifn->CreateJavaVM == 0 || ifn->GetDefaultJavaVMInitArgs == 0) { 403 JLI_ReportErrorMessage(JNI_ERROR1, (char *)jvmpath); 404 return JNI_FALSE; 405 } 406 407 return JNI_TRUE; 408 } 409 410 /* 411 * Removes the trailing file name and one sub-folder from a path. 412 * If buf is "c:\foo\bin\javac", then put "c:\foo" into buf. 413 */ 414 jboolean 415 TruncatePath(char *buf) 416 { 417 char *cp; 418 *JLI_StrRChr(buf, '\\') = '\0'; /* remove .exe file name */ 419 if ((cp = JLI_StrRChr(buf, '\\')) == 0) { 420 /* This happens if the application is in a drive root, and 421 * there is no bin directory. */ 422 buf[0] = '\0'; 423 return JNI_FALSE; 424 } 425 *cp = '\0'; /* remove the bin\ part */ 426 return JNI_TRUE; 427 } 428 429 /* 430 * Retrieves the path to the JRE home by locating the executable file 431 * of the current process and then truncating the path to the executable 432 */ 433 jboolean 434 GetApplicationHome(char *buf, jint bufsize) 435 { 436 GetModuleFileName(NULL, buf, bufsize); 437 return TruncatePath(buf); 438 } 439 440 /* 441 * Retrieves the path to the JRE home by locating JLI.DLL and 442 * then truncating the path to JLI.DLL 443 */ 444 jboolean 445 GetApplicationHomeFromDll(char *buf, jint bufsize) 446 { 447 HMODULE module; 448 DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | 449 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; 450 451 if (GetModuleHandleEx(flags, (LPCSTR)&GetJREPath, &module) != 0) { 452 if (GetModuleFileName(module, buf, bufsize) != 0) { 453 return TruncatePath(buf); 454 } 455 } 456 return JNI_FALSE; 457 } 458 459 /* 460 * Support for doing cheap, accurate interval timing. 461 */ 462 static jboolean counterAvailable = JNI_FALSE; 463 static jboolean counterInitialized = JNI_FALSE; 464 static LARGE_INTEGER counterFrequency; 465 466 jlong CounterGet() 467 { 468 LARGE_INTEGER count; 469 470 if (!counterInitialized) { 471 counterAvailable = QueryPerformanceFrequency(&counterFrequency); 472 counterInitialized = JNI_TRUE; 473 } 474 if (!counterAvailable) { 475 return 0; 476 } 477 QueryPerformanceCounter(&count); 478 return (jlong)(count.QuadPart); 479 } 480 481 jlong Counter2Micros(jlong counts) 482 { 483 if (!counterAvailable || !counterInitialized) { 484 return 0; 485 } 486 return (counts * 1000 * 1000)/counterFrequency.QuadPart; 487 } 488 /* 489 * windows snprintf does not guarantee a null terminator in the buffer, 490 * if the computed size is equal to or greater than the buffer size, 491 * as well as error conditions. This function guarantees a null terminator 492 * under all these conditions. An unreasonable buffer or size will return 493 * an error value. Under all other conditions this function will return the 494 * size of the bytes actually written minus the null terminator, similar 495 * to ansi snprintf api. Thus when calling this function the caller must 496 * ensure storage for the null terminator. 497 */ 498 int 499 JLI_Snprintf(char* buffer, size_t size, const char* format, ...) { 500 int rc; 501 va_list vl; 502 if (size == 0 || buffer == NULL) 503 return -1; 504 buffer[0] = '\0'; 505 va_start(vl, format); 506 rc = vsnprintf(buffer, size, format, vl); 507 va_end(vl); 508 /* force a null terminator, if something is amiss */ 509 if (rc < 0) { 510 /* apply ansi semantics */ 511 buffer[size - 1] = '\0'; 512 return (int)size; 513 } else if (rc == size) { 514 /* force a null terminator */ 515 buffer[size - 1] = '\0'; 516 } 517 return rc; 518 } 519 520 void 521 JLI_ReportErrorMessage(const char* fmt, ...) { 522 va_list vl; 523 va_start(vl,fmt); 524 525 if (IsJavaw()) { 526 char *message; 527 528 /* get the length of the string we need */ 529 int n = _vscprintf(fmt, vl); 530 531 message = (char *)JLI_MemAlloc(n + 1); 532 _vsnprintf(message, n, fmt, vl); 533 message[n]='\0'; 534 MessageBox(NULL, message, "Java Virtual Machine Launcher", 535 (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); 536 JLI_MemFree(message); 537 } else { 538 vfprintf(stderr, fmt, vl); 539 fprintf(stderr, "\n"); 540 } 541 va_end(vl); 542 } 543 544 /* 545 * Just like JLI_ReportErrorMessage, except that it concatenates the system 546 * error message if any, its upto the calling routine to correctly 547 * format the separation of the messages. 548 */ 549 void 550 JLI_ReportErrorMessageSys(const char *fmt, ...) 551 { 552 va_list vl; 553 554 int save_errno = errno; 555 DWORD errval; 556 jboolean freeit = JNI_FALSE; 557 char *errtext = NULL; 558 559 va_start(vl, fmt); 560 561 if ((errval = GetLastError()) != 0) { /* Platform SDK / DOS Error */ 562 int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM| 563 FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER, 564 NULL, errval, 0, (LPTSTR)&errtext, 0, NULL); 565 if (errtext == NULL || n == 0) { /* Paranoia check */ 566 errtext = ""; 567 n = 0; 568 } else { 569 freeit = JNI_TRUE; 570 if (n > 2) { /* Drop final CR, LF */ 571 if (errtext[n - 1] == '\n') n--; 572 if (errtext[n - 1] == '\r') n--; 573 errtext[n] = '\0'; 574 } 575 } 576 } else { /* C runtime error that has no corresponding DOS error code */ 577 errtext = strerror(save_errno); 578 } 579 580 if (IsJavaw()) { 581 char *message; 582 int mlen; 583 /* get the length of the string we need */ 584 int len = mlen = _vscprintf(fmt, vl) + 1; 585 if (freeit) { 586 mlen += (int)JLI_StrLen(errtext); 587 } 588 589 message = (char *)JLI_MemAlloc(mlen); 590 _vsnprintf(message, len, fmt, vl); 591 message[len]='\0'; 592 593 if (freeit) { 594 JLI_StrCat(message, errtext); 595 } 596 597 MessageBox(NULL, message, "Java Virtual Machine Launcher", 598 (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); 599 600 JLI_MemFree(message); 601 } else { 602 vfprintf(stderr, fmt, vl); 603 if (freeit) { 604 fprintf(stderr, "%s", errtext); 605 } 606 } 607 if (freeit) { 608 (void)LocalFree((HLOCAL)errtext); 609 } 610 va_end(vl); 611 } 612 613 void JLI_ReportExceptionDescription(JNIEnv * env) { 614 if (IsJavaw()) { 615 /* 616 * This code should be replaced by code which opens a window with 617 * the exception detail message, for now atleast put a dialog up. 618 */ 619 MessageBox(NULL, "A Java Exception has occurred.", "Java Virtual Machine Launcher", 620 (MB_OK|MB_ICONSTOP|MB_APPLMODAL)); 621 } else { 622 (*env)->ExceptionDescribe(env); 623 } 624 } 625 626 /* 627 * Wrapper for platform dependent unsetenv function. 628 */ 629 int 630 UnsetEnv(char *name) 631 { 632 int ret; 633 char *buf = JLI_MemAlloc(JLI_StrLen(name) + 2); 634 buf = JLI_StrCat(JLI_StrCpy(buf, name), "="); 635 ret = _putenv(buf); 636 JLI_MemFree(buf); 637 return (ret); 638 } 639 640 /* --- Splash Screen shared library support --- */ 641 642 static const char* SPLASHSCREEN_SO = "\\bin\\splashscreen.dll"; 643 644 static HMODULE hSplashLib = NULL; 645 646 void* SplashProcAddress(const char* name) { 647 char libraryPath[MAXPATHLEN]; /* some extra space for JLI_StrCat'ing SPLASHSCREEN_SO */ 648 649 if (!GetJREPath(libraryPath, MAXPATHLEN)) { 650 return NULL; 651 } 652 if (JLI_StrLen(libraryPath)+JLI_StrLen(SPLASHSCREEN_SO) >= MAXPATHLEN) { 653 return NULL; 654 } 655 JLI_StrCat(libraryPath, SPLASHSCREEN_SO); 656 657 if (!hSplashLib) { 658 hSplashLib = LoadLibrary(libraryPath); 659 } 660 if (hSplashLib) { 661 return GetProcAddress(hSplashLib, name); 662 } else { 663 return NULL; 664 } 665 } 666 667 void SplashFreeLibrary() { 668 if (hSplashLib) { 669 FreeLibrary(hSplashLib); 670 hSplashLib = NULL; 671 } 672 } 673 674 /* 675 * Block current thread and continue execution in a new thread 676 */ 677 int 678 ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) { 679 int rslt = 0; 680 unsigned thread_id; 681 682 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION 683 #define STACK_SIZE_PARAM_IS_A_RESERVATION (0x10000) 684 #endif 685 686 /* 687 * STACK_SIZE_PARAM_IS_A_RESERVATION is what we want, but it's not 688 * supported on older version of Windows. Try first with the flag; and 689 * if that fails try again without the flag. See MSDN document or HotSpot 690 * source (os_win32.cpp) for details. 691 */ 692 HANDLE thread_handle = 693 (HANDLE)_beginthreadex(NULL, 694 (unsigned)stack_size, 695 continuation, 696 args, 697 STACK_SIZE_PARAM_IS_A_RESERVATION, 698 &thread_id); 699 if (thread_handle == NULL) { 700 thread_handle = 701 (HANDLE)_beginthreadex(NULL, 702 (unsigned)stack_size, 703 continuation, 704 args, 705 0, 706 &thread_id); 707 } 708 709 /* AWT preloading (AFTER main thread start) */ 710 #ifdef ENABLE_AWT_PRELOAD 711 /* D3D preloading */ 712 if (awtPreloadD3D != 0) { 713 char *envValue; 714 /* D3D routines checks env.var J2D_D3D if no appropriate 715 * command line params was specified 716 */ 717 envValue = getenv("J2D_D3D"); 718 if (envValue != NULL && JLI_StrCaseCmp(envValue, "false") == 0) { 719 awtPreloadD3D = 0; 720 } 721 /* Test that AWT preloading isn't disabled by J2D_D3D_PRELOAD env.var */ 722 envValue = getenv("J2D_D3D_PRELOAD"); 723 if (envValue != NULL && JLI_StrCaseCmp(envValue, "false") == 0) { 724 awtPreloadD3D = 0; 725 } 726 if (awtPreloadD3D < 0) { 727 /* If awtPreloadD3D is still undefined (-1), test 728 * if it is turned on by J2D_D3D_PRELOAD env.var. 729 * By default it's turned OFF. 730 */ 731 awtPreloadD3D = 0; 732 if (envValue != NULL && JLI_StrCaseCmp(envValue, "true") == 0) { 733 awtPreloadD3D = 1; 734 } 735 } 736 } 737 if (awtPreloadD3D) { 738 AWTPreload(D3D_PRELOAD_FUNC); 739 } 740 #endif /* ENABLE_AWT_PRELOAD */ 741 742 if (thread_handle) { 743 WaitForSingleObject(thread_handle, INFINITE); 744 GetExitCodeThread(thread_handle, &rslt); 745 CloseHandle(thread_handle); 746 } else { 747 rslt = continuation(args); 748 } 749 750 #ifdef ENABLE_AWT_PRELOAD 751 if (awtPreloaded) { 752 AWTPreloadStop(); 753 } 754 #endif /* ENABLE_AWT_PRELOAD */ 755 756 return rslt; 757 } 758 759 /* Unix only, empty on windows. */ 760 void SetJavaLauncherPlatformProps() {} 761 762 /* 763 * The implementation for finding classes from the bootstrap 764 * class loader, refer to java.h 765 */ 766 static FindClassFromBootLoader_t *findBootClass = NULL; 767 768 jclass FindBootStrapClass(JNIEnv *env, const char *classname) 769 { 770 HMODULE hJvm; 771 772 if (findBootClass == NULL) { 773 hJvm = GetModuleHandle(JVM_DLL); 774 if (hJvm == NULL) return NULL; 775 /* need to use the demangled entry point */ 776 findBootClass = (FindClassFromBootLoader_t *)GetProcAddress(hJvm, 777 "JVM_FindClassFromBootLoader"); 778 if (findBootClass == NULL) { 779 JLI_ReportErrorMessage(DLL_ERROR4, "JVM_FindClassFromBootLoader"); 780 return NULL; 781 } 782 } 783 return findBootClass(env, classname); 784 } 785 786 void 787 InitLauncher(boolean javaw) 788 { 789 INITCOMMONCONTROLSEX icx; 790 791 /* 792 * Required for javaw mode MessageBox output as well as for 793 * HotSpot -XX:+ShowMessageBoxOnError in java mode, an empty 794 * flag field is sufficient to perform the basic UI initialization. 795 */ 796 memset(&icx, 0, sizeof(INITCOMMONCONTROLSEX)); 797 icx.dwSize = sizeof(INITCOMMONCONTROLSEX); 798 InitCommonControlsEx(&icx); 799 _isjavaw = javaw; 800 JLI_SetTraceLauncher(); 801 } 802 803 804 /* ============================== */ 805 /* AWT preloading */ 806 #ifdef ENABLE_AWT_PRELOAD 807 808 typedef int FnPreloadStart(void); 809 typedef void FnPreloadStop(void); 810 static FnPreloadStop *fnPreloadStop = NULL; 811 static HMODULE hPreloadAwt = NULL; 812 813 /* 814 * Starts AWT preloading 815 */ 816 int AWTPreload(const char *funcName) 817 { 818 int result = -1; 819 /* load AWT library once (if several preload function should be called) */ 820 if (hPreloadAwt == NULL) { 821 /* awt.dll is not loaded yet */ 822 char libraryPath[MAXPATHLEN]; 823 size_t jrePathLen = 0; 824 HMODULE hJava = NULL; 825 HMODULE hVerify = NULL; 826 827 while (1) { 828 /* awt.dll depends on jvm.dll & java.dll; 829 * jvm.dll is already loaded, so we need only java.dll; 830 * java.dll depends on MSVCRT lib & verify.dll. 831 */ 832 if (!GetJREPath(libraryPath, MAXPATHLEN)) { 833 break; 834 } 835 836 /* save path length */ 837 jrePathLen = JLI_StrLen(libraryPath); 838 839 if (jrePathLen + JLI_StrLen("\\bin\\verify.dll") >= MAXPATHLEN) { 840 /* jre path is too long, the library path will not fit there; 841 * report and abort preloading 842 */ 843 JLI_ReportErrorMessage(JRE_ERROR11); 844 break; 845 } 846 847 /* load msvcrt 1st */ 848 LoadMSVCRT(); 849 850 /* load verify.dll */ 851 JLI_StrCat(libraryPath, "\\bin\\verify.dll"); 852 hVerify = LoadLibrary(libraryPath); 853 if (hVerify == NULL) { 854 break; 855 } 856 857 /* restore jrePath */ 858 libraryPath[jrePathLen] = 0; 859 /* load java.dll */ 860 JLI_StrCat(libraryPath, "\\bin\\" JAVA_DLL); 861 hJava = LoadLibrary(libraryPath); 862 if (hJava == NULL) { 863 break; 864 } 865 866 /* restore jrePath */ 867 libraryPath[jrePathLen] = 0; 868 /* load awt.dll */ 869 JLI_StrCat(libraryPath, "\\bin\\awt.dll"); 870 hPreloadAwt = LoadLibrary(libraryPath); 871 if (hPreloadAwt == NULL) { 872 break; 873 } 874 875 /* get "preloadStop" func ptr */ 876 fnPreloadStop = (FnPreloadStop *)GetProcAddress(hPreloadAwt, "preloadStop"); 877 878 break; 879 } 880 } 881 882 if (hPreloadAwt != NULL) { 883 FnPreloadStart *fnInit = (FnPreloadStart *)GetProcAddress(hPreloadAwt, funcName); 884 if (fnInit != NULL) { 885 /* don't forget to stop preloading */ 886 awtPreloaded = 1; 887 888 result = fnInit(); 889 } 890 } 891 892 return result; 893 } 894 895 /* 896 * Terminates AWT preloading 897 */ 898 void AWTPreloadStop() { 899 if (fnPreloadStop != NULL) { 900 fnPreloadStop(); 901 } 902 } 903 904 #endif /* ENABLE_AWT_PRELOAD */ 905 906 int 907 JVMInit(InvocationFunctions* ifn, jlong threadStackSize, 908 int argc, char **argv, 909 int mode, char *what, int ret) 910 { 911 ShowSplashScreen(); 912 return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret); 913 } 914 915 void 916 PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm) 917 { 918 // stubbed out for windows and *nixes. 919 } 920 921 void 922 RegisterThread() 923 { 924 // stubbed out for windows and *nixes. 925 } 926 927 /* 928 * on windows, we return a false to indicate this option is not applicable 929 */ 930 jboolean 931 ProcessPlatformOption(const char *arg) 932 { 933 return JNI_FALSE; 934 } 935 936 /* 937 * At this point we have the arguments to the application, and we need to 938 * check with original stdargs in order to compare which of these truly 939 * needs expansion. cmdtoargs will specify this if it finds a bare 940 * (unquoted) argument containing a glob character(s) ie. * or ? 941 */ 942 jobjectArray 943 CreateApplicationArgs(JNIEnv *env, char **strv, int argc) 944 { 945 int i, j, idx; 946 size_t tlen; 947 jobjectArray outArray, inArray; 948 char *arg, **nargv; 949 jboolean needs_expansion = JNI_FALSE; 950 jmethodID mid; 951 int stdargc; 952 StdArg *stdargs; 953 int *appArgIdx; 954 int isTool; 955 jclass cls = GetLauncherHelperClass(env); 956 NULL_CHECK0(cls); 957 958 if (argc == 0) { 959 return NewPlatformStringArray(env, strv, argc); 960 } 961 // the holy grail we need to compare with. 962 stdargs = JLI_GetStdArgs(); 963 stdargc = JLI_GetStdArgc(); 964 965 // sanity check, this should never happen 966 if (argc > stdargc) { 967 JLI_TraceLauncher("Warning: app args is larger than the original, %d %d\n", argc, stdargc); 968 JLI_TraceLauncher("passing arguments as-is.\n"); 969 return NewPlatformStringArray(env, strv, argc); 970 } 971 972 // sanity check, match the args we have, to the holy grail 973 idx = JLI_GetAppArgIndex(); 974 isTool = (idx == 0); 975 if (isTool) { idx++; } // skip tool name 976 JLI_TraceLauncher("AppArgIndex: %d points to %s\n", idx, stdargs[idx].arg); 977 978 appArgIdx = calloc(argc, sizeof(int)); 979 for (i = idx, j = 0; i < stdargc; i++) { 980 if (isTool) { // filter -J used by tools to pass JVM options 981 arg = stdargs[i].arg; 982 if (arg[0] == '-' && arg[1] == 'J') { 983 continue; 984 } 985 } 986 appArgIdx[j++] = i; 987 } 988 // sanity check, ensure same number of arguments for application 989 if (j != argc) { 990 JLI_TraceLauncher("Warning: app args count doesn't match, %d %d\n", j, argc); 991 JLI_TraceLauncher("passing arguments as-is.\n"); 992 JLI_MemFree(appArgIdx); 993 return NewPlatformStringArray(env, strv, argc); 994 } 995 996 // make a copy of the args which will be expanded in java if required. 997 nargv = (char **)JLI_MemAlloc(argc * sizeof(char*)); 998 for (i = 0; i < argc; i++) { 999 jboolean arg_expand; 1000 j = appArgIdx[i]; 1001 arg_expand = (JLI_StrCmp(stdargs[j].arg, strv[i]) == 0) 1002 ? stdargs[j].has_wildcard 1003 : JNI_FALSE; 1004 if (needs_expansion == JNI_FALSE) 1005 needs_expansion = arg_expand; 1006 1007 // indicator char + String + NULL terminator, the java method will strip 1008 // out the first character, the indicator character, so no matter what 1009 // we add the indicator 1010 tlen = 1 + JLI_StrLen(strv[i]) + 1; 1011 nargv[i] = (char *) JLI_MemAlloc(tlen); 1012 if (JLI_Snprintf(nargv[i], tlen, "%c%s", arg_expand ? 'T' : 'F', 1013 strv[i]) < 0) { 1014 return NULL; 1015 } 1016 JLI_TraceLauncher("%s\n", nargv[i]); 1017 } 1018 1019 if (!needs_expansion) { 1020 // clean up any allocated memory and return back the old arguments 1021 for (i = 0 ; i < argc ; i++) { 1022 JLI_MemFree(nargv[i]); 1023 } 1024 JLI_MemFree(nargv); 1025 JLI_MemFree(appArgIdx); 1026 return NewPlatformStringArray(env, strv, argc); 1027 } 1028 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, 1029 "expandArgs", 1030 "([Ljava/lang/String;)[Ljava/lang/String;")); 1031 1032 // expand the arguments that require expansion, the java method will strip 1033 // out the indicator character. 1034 NULL_CHECK0(inArray = NewPlatformStringArray(env, nargv, argc)); 1035 outArray = (*env)->CallStaticObjectMethod(env, cls, mid, inArray); 1036 for (i = 0; i < argc; i++) { 1037 JLI_MemFree(nargv[i]); 1038 } 1039 JLI_MemFree(nargv); 1040 JLI_MemFree(appArgIdx); 1041 return outArray; 1042 }