java_md.c revision 5694
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <windows.h>
#include <io.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <wtypes.h>
#include <commctrl.h>
#include <jni.h>
#include "java.h"
#include "version_comp.h"
#define JVM_DLL "jvm.dll"
#define JAVA_DLL "java.dll"
/*
* Prototypes.
*/
/* We supports warmup for UI stack that is performed in parallel
* to VM initialization.
* This helps to improve startup of UI application as warmup phase
* might be long due to initialization of OS or hardware resources.
* It is not CPU bound and therefore it does not interfere with VM init.
* Obviously such warmup only has sense for UI apps and therefore it needs
* to be explicitly requested by passing -Dsun.awt.warmup=true property
*
* Implementation launches new thread after VM starts and use it to perform
* warmup code (platform dependent).
* This thread is later reused as AWT toolkit thread as graphics toolkit
* often assume that they are used from the same thread they were launched on.
*
* At the moment we only support warmup for D3D. It only possible on windows
* and only if other flags do not prohibit this (e.g. OpenGL support requested).
*/
#ifndef JAVA_ARGS /* turn off AWT preloading for javac, jar, etc */
/* CR6999872: fastdebug crashes if awt library is loaded before JVM is
* initialized*/
#if !defined(DEBUG)
#define ENABLE_AWT_PRELOAD
#endif
#endif
#ifdef ENABLE_AWT_PRELOAD
/* "AWT was preloaded" flag;
* turned on by AWTPreload().
*/
int awtPreloaded = 0;
/* Calls a function with the name specified
* the function must be int(*fn)(void).
*/
int AWTPreload(const char *funcName);
/* stops AWT preloading */
void AWTPreloadStop();
/* D3D preloading */
/* -1: not initialized; 0: OFF, 1: ON */
int awtPreloadD3D = -1;
/* command line parameter to swith D3D preloading on */
#define PARAM_PRELOAD_D3D "-Dsun.awt.warmup"
#define PARAM_NODDRAW "-Dsun.java2d.noddraw"
#define PARAM_OPENGL "-Dsun.java2d.opengl"
#define D3D_PRELOAD_FUNC "preloadD3D"
/* Extracts value of a parameter with the specified name
* from command line argument (returns pointer in the argument).
* Returns NULL if the argument does not contains the parameter.
* e.g.:
* GetParamValue("theParam", "theParam=value") returns pointer to "value".
*/
/* arg[nameLen] is valid (may contain final NULL) */
}
}
return NULL;
}
/* Checks if commandline argument contains property specified
* Returns -1 if the argument does not contain the parameter;
* Returns 1 if the argument contains the parameter and its value is "true";
* Returns 0 if the argument contains the parameter and its value is "false".
*/
if (paramValue != NULL) {
return 1;
}
return 0;
}
}
return -1;
}
#endif /* ENABLE_AWT_PRELOAD */
IsJavaw()
{
return _isjavaw;
}
/*
* Returns the arch path, to get the current arch use the
* macro GetArch, nbits here is ignored for now.
*/
const char *
GetArchPath(int nbits)
{
#ifdef _M_AMD64
return "amd64";
return "ia64";
#else
return "i386";
#endif
}
/*
*
*/
void
char * jvmtype;
int i = 0;
int running = CURRENT_DATA_MODEL;
for (i = 0; i < *pargc ; i++) {
wanted = 64;
continue;
}
wanted = 32;
continue;
}
}
exit(1);
}
/* Find out where the JRE is that we will be using. */
exit(2);
}
/* Find the specified JVM type */
exit(1);
}
exit(4);
}
jvmpath[0] = '\0';
exit(4);
}
/* If we got here, jvmpath has been correctly initialized. */
/* Check if we need preload AWT */
#ifdef ENABLE_AWT_PRELOAD
for (i = 0; i < *pargc ; i++) {
/* Tests the "turn on" parameter only if not set yet. */
if (awtPreloadD3D < 0) {
awtPreloadD3D = 1;
}
}
/* Test parameters which can disable preloading if not already disabled. */
if (awtPreloadD3D != 0) {
{
awtPreloadD3D = 0;
/* no need to test the rest of the parameters */
break;
}
}
}
#endif /* ENABLE_AWT_PRELOAD */
}
static jboolean
{
// Only do this once
static int loaded = 0;
char crtpath[MAXPATHLEN];
if (!loaded) {
/*
* The Microsoft C Runtime Library needs to be loaded first. A copy is
* assumed to be present in the "JRE path" directory. If it is not found
* there (or "JRE path" fails to resolve), skip the explicit load and let
* nature take its course, which is likely to be a failure to execute.
* This is clearly completely specific to the exact compiler version
* which isn't very nice, but its hardly the only place.
* No attempt to look for compiler versions in between 2003 and 2010
* as we aren't supporting building with those.
*/
#ifdef _MSC_VER
#if _MSC_VER < 1400
#define CRT_DLL "msvcr71.dll"
#endif
#if _MSC_VER >= 1600
#define CRT_DLL "msvcr100.dll"
#endif
#ifdef CRT_DLL
return JNI_FALSE;
}
if (LoadLibrary(crtpath) == 0) {
return JNI_FALSE;
}
}
}
#endif /* CRT_DLL */
#endif /* _MSC_VER */
loaded = 1;
}
return JNI_TRUE;
}
/*
* Find path to JRE based on .exe's location or registry settings.
*/
{
char javadll[MAXPATHLEN];
struct stat s;
/* Is JRE co-located with the application? */
return JNI_TRUE;
}
/* Does this app ship a private JRE in <apphome>\jre directory? */
return JNI_TRUE;
}
}
/* Look for a public JRE on this machine. */
return JNI_TRUE;
}
return JNI_FALSE;
}
/*
* Given a JRE location and a JVM type, construct what the name the
* JVM shared library will be. Return true, if such a library
* exists, false otherwise.
*/
static jboolean
{
struct stat s;
} else {
}
return JNI_TRUE;
} else {
return JNI_FALSE;
}
}
/*
* Load a jvm from "jvmpath" and initialize the invocation functions.
*/
{
/*
* The Microsoft C Runtime Library needs to be loaded first. A copy is
* assumed to be present in the "JRE path" directory. If it is not found
* there (or "JRE path" fails to resolve), skip the explicit load and let
* nature take its course, which is likely to be a failure to execute.
*
*/
LoadMSVCRT();
/* Load the Java VM DLL */
return JNI_FALSE;
}
/* Now get the function addresses */
ifn->CreateJavaVM =
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* If app is "c:\foo\bin\javac", then put "c:\foo" into buf.
*/
{
char *cp;
/* This happens if the application is in a drive root, and
* there is no bin directory. */
buf[0] = '\0';
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* Helpers to look in the registry for a public JRE.
*/
/* Same for 1.5.0, 1.5.1, 1.5.2 etc. */
#define JRE_KEY "Software\\JavaSoft\\Java Runtime Environment"
static jboolean
{
return JNI_TRUE;
}
}
return JNI_FALSE;
}
static jboolean
{
char version[MAXPATHLEN];
/*
* Note: There is a very similar implementation of the following
* registry reading code in the Windows java control panel (javacp.cpl).
* If there are bugs here, a similar bug probably exists there. Hence,
* changes here require inspection there.
*/
/* Find the current version of the JRE */
return JNI_FALSE;
}
return JNI_FALSE;
}
);
return JNI_FALSE;
}
/* Find directory where the current version is installed. */
return JNI_FALSE;
}
return JNI_FALSE;
}
if (JLI_IsTraceLauncher()) {
char micro[MAXPATHLEN];
sizeof(micro))) {
printf("Warning: Can't read MicroVersion\n");
micro[0] = '\0';
}
}
return JNI_TRUE;
}
/*
* Support for doing cheap, accurate interval timing.
*/
static LARGE_INTEGER counterFrequency;
{
if (!counterInitialized) {
}
if (!counterAvailable) {
return 0;
}
}
{
if (!counterAvailable || !counterInitialized) {
return 0;
}
}
/*
* windows snprintf does not guarantee a null terminator in the buffer,
* if the computed size is equal to or greater than the buffer size,
* as well as error conditions, this function guarantees a null terminator
* under all these conditions. An unreasonable buffer size will return
* an error value.
*/
{
if (size <= 0)
return -1;
/* force a null terminator, if something is amiss */
return rc;
}
void
JLI_ReportErrorMessage(const char* fmt, ...) {
if (IsJavaw()) {
char *message;
/* get the length of the string we need */
message[n]='\0';
} else {
}
}
/*
* Just like JLI_ReportErrorMessage, except that it concatenates the system
* error message if any, its upto the calling routine to correctly
* format the separation of the messages.
*/
void
JLI_ReportErrorMessageSys(const char *fmt, ...)
{
int save_errno = errno;
int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
errtext = "";
n = 0;
} else {
if (n > 2) { /* Drop final CR, LF */
errtext[n] = '\0';
}
}
} else { /* C runtime error that has no corresponding DOS error code */
}
if (IsJavaw()) {
char *message;
int mlen;
/* get the length of the string we need */
if (freeit) {
}
if (freeit) {
}
} else {
if (freeit) {
}
}
if (freeit) {
}
}
if (IsJavaw()) {
/*
* This code should be replaced by code which opens a window with
* the exception detail message, for now atleast put a dialog up.
*/
} else {
}
}
}
/*
* Determine if there is an acceptable JRE in the registry directory top_key.
* Upon locating the "best" one, return a fully qualified path to it.
* "Best" is defined as the most advanced JRE meeting the constraints
* contained in the manifest_info. If no JRE in this directory meets the
* constraints, return NULL.
*
* It doesn't matter if we get an error reading the registry, or we just
* don't find anything interesting in the directory. We just return NULL
* in either case.
*/
static char *
char name[MAXNAMELEN];
int len;
/*
* searching for the best available version.
*/
index++;
}
}
/*
* Extract "JavaHome" from the "best" registry directory and return
* that path. If no appropriate version was located, or there is an
* error in extracting the "JavaHome" string, return null.
*/
return (NULL);
else {
!= ERROR_SUCCESS) {
return (NULL);
}
len = MAXNAMELEN;
!= ERROR_SUCCESS) {
return (NULL);
}
return (JLI_StringDup(name));
}
}
/*
* This is the global entry point. It examines the host for the optimal
* JRE to be used by scanning a set of registry entries. This set of entries
* is hardwired on Windows as "Software\JavaSoft\Java Runtime Environment"
* under the set of roots "{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }".
*
* This routine simply opens each of these registry directories before passing
* control onto ProcessDir().
*/
char *
char *path;
int key_index;
== ERROR_SUCCESS)
return (path);
}
}
return NULL;
}
/*
* Local helper routine to isolate a single token (option or argument)
* from the command line.
*
* This routine accepts a pointer to a character pointer. The first
* token (as defined by MSDN command-line argument syntax) is isolated
* from that string.
*
* Upon return, the input character pointer pointed to by the parameter s
* is updated to point to the remainding, unscanned, portion of the string,
* or to a null character if the entire string has been consummed.
*
* This function returns a pointer to a null-terminated string which
* contains the isolated first token, or to the null character if no
* token could be isolated.
*
* Note the side effect of modifying the input string s by the insertion
* of a null character, making it two strings.
*
* See "Parsing C Command-Line Arguments" in the MSDN Library for the
* parsing rule details. The rule summary from that specification is:
*
* * Arguments are delimited by white space, which is either a space or a tab.
*
* * A string surrounded by double quotation marks is interpreted as a single
* argument, regardless of white space contained within. A quoted string can
* be embedded in an argument. Note that the caret (^) is not recognized as
* an escape character or delimiter.
*
* * A double quotation mark preceded by a backslash, \", is interpreted as a
* literal double quotation mark (").
*
* * Backslashes are interpreted literally, unless they immediately precede a
* double quotation mark.
*
* * If an even number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\), and the double quotation mark (") is interpreted as a
* string delimiter.
*
* * If an odd number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\) and the double quotation mark is interpreted as an
* escape sequence by the remaining backslash, causing a literal double
* quotation mark (") to be placed in argv.
*/
static char*
nextarg(char** s) {
char *p = *s;
char *head;
int slashes = 0;
int inquote = 0;
/*
* Strip leading whitespace, which MSDN defines as only space or tab.
* (Hence, no locale specific "isspace" here.)
*/
while (*p != (char)0 && (*p == ' ' || *p == '\t'))
p++;
head = p; /* Save the start of the token to return */
/*
* Isolate a token from the command line.
*/
p++;
else if (*p == '"')
}
/*
* If the token isolated isn't already terminated in a "char zero",
* then replace the whitespace character with one and move to the
* next character.
*/
if (*p != (char)0)
*p++ = (char)0;
/*
* Update the parameter to point to the head of the remaining string
* reflecting the command line and return a pointer to the leading
* token which was isolated from the command line.
*/
*s = p;
return (head);
}
/*
* Local helper routine to return a string equivalent to the input string
* s, but with quotes removed so the result is a string as would be found
* in argv[]. The returned string should be freed by a call to JLI_MemFree().
*
* The rules for quoting (and escaped quotes) are:
*
* 1 A double quotation mark preceded by a backslash, \", is interpreted as a
* literal double quotation mark (").
*
* 2 Backslashes are interpreted literally, unless they immediately precede a
* double quotation mark.
*
* 3 If an even number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\), and the double quotation mark (") is interpreted as a
* string delimiter.
*
* 4 If an odd number of backslashes is followed by a double quotation mark,
* then one backslash (\) is placed in the argv array for every pair of
* backslashes (\\) and the double quotation mark is interpreted as an
* escape sequence by the remaining backslash, causing a literal double
* quotation mark (") to be placed in argv.
*/
static char*
unquote(const char *s) {
const char *p = s; /* Pointer to the tail of the original string */
while (*p != '\0') {
if (*p == '"') {
p++;
} else if (*p == '\\') {
const char *q = p + JLI_StrSpn(p,"\\");
if (*q == '"')
do {
*pun++ = '\\';
p += 2;
} while (*p == '\\' && p < q);
else
while (p < q)
*pun++ = *p++;
} else {
*pun++ = *p++;
}
}
*pun = '\0';
return un;
}
/*
* Given a path to a jre to execute, this routine checks if this process
* is indeed that jre. If not, it exec's that jre.
*
* We want to actually check the paths rather than just the version string
* built into the executable, so that given version specification will yield
* the exact same Java environment, regardless of the version of the arbitrary
* launcher we start with.
*/
void
const char *progname = GetProgramName();
/*
* Resolve the real path to the currently running launcher.
*/
exit(1);
}
/*
* If the path to the selected JRE directory is a match to the initial
* portion of the path to the currently executing JRE, we have a winner!
* If so, just return.
*/
return; /* I am the droid you were looking for */
/*
* If this isn't the selected version, exec the selected version.
*/
/*
* Although Windows has an execv() entrypoint, it doesn't actually
* overlay a process: it can only create a new process and terminate
* the old process. Therefore, any processes waiting on the initial
* process wake up and they shouldn't. Hence, a chain of pseudo-zombie
* processes must be retained to maintain the proper wait semantics.
* Fortunately the image size of the launcher isn't too large at this
* time.
*
* If it weren't for this semantic flaw, the code below would be ...
*
* execv(path, argv);
* JLI_ReportErrorMessage("Error: Exec of %s failed\n", path);
* exit(1);
*
* The incorrect exec semantics could be addressed by:
*
* exit((int)spawnv(_P_WAIT, path, argv));
*
* this from completely working. All the Windows POSIX process creation
* interfaces are implemented as wrappers around the native Windows
* function CreateProcess(). CreateProcess() takes a single string
* to specify command line options and arguments, so the POSIX routine
* wrappers build a single string from the argv[] array and in the
* process, any quoting information is lost.
*
* The solution to this to get the original command line, to process it
* to remove the new multiple JRE options (if any) as was done for argv
* in the common SelectVersion() routine and finally to pass it directly
* to the native CreateProcess() Windows process control interface.
*/
{
char *cmdline;
char *p;
char *np;
char *ocl;
char *ccl;
char *unquoted;
/*
* The following code block gets and processes the original command
* line, replacing the argv[0] equivalent in the command line with
* the path to the new executable and removing the appropriate
* Multiple JRE support options. Note that similar logic exists
* in the platform independent SelectVersion routine, but is
* replicated here due to the syntax of CreateProcess().
*
* The magic "+ 4" characters added to the command line length are
* 2 possible quotes around the path (argv[0]), a space after the
* path and a terminating null character.
*/
ocl = GetCommandLine();
else
while (*np != (char)0) { /* While more command-line */
if (*p != (char)0) { /* If a token was isolated */
if (*p != (char)0) /* If a token was isolated */
}
} else { /* End of options */
JLI_MemFree((void *)unquoted);
break;
}
JLI_MemFree((void *)unquoted);
}
}
JLI_MemFree((void *)ccl);
if (JLI_IsTraceLauncher()) {
JLI_MemFree((void *)ccl);
}
/*
* The following code is modeled after a model presented in the
* Microsoft Technical Article "Moving Unix Applications to
* Windows NT" (March 6, 1994) and "Creating Processes" on MSDN
* (Februrary 2005). It approximates UNIX spawn semantics with
* the parent waiting for termination of the child.
*/
(DWORD)0, /* creation flags */
exit(1);
}
exitCode = 1;
} else {
exitCode = 1;
}
}
}
/*
* Wrapper for platform dependent unsetenv function.
*/
int
{
int ret;
return (ret);
}
/* --- Splash Screen shared library support --- */
static const char* SPLASHSCREEN_SO = "\\bin\\splashscreen.dll";
void* SplashProcAddress(const char* name) {
return NULL;
}
return NULL;
}
if (!hSplashLib) {
}
if (hSplashLib) {
} else {
return NULL;
}
}
void SplashFreeLibrary() {
if (hSplashLib) {
hSplashLib = NULL;
}
}
const char *
return "%I64d";
}
/*
* Block current thread and continue execution in a new thread
*/
int
int rslt = 0;
unsigned thread_id;
#define STACK_SIZE_PARAM_IS_A_RESERVATION (0x10000)
#endif
/*
* STACK_SIZE_PARAM_IS_A_RESERVATION is what we want, but it's not
* supported on older version of Windows. Try first with the flag; and
* if that fails try again without the flag. See MSDN document or HotSpot
* source (os_win32.cpp) for details.
*/
(unsigned)stack_size,
args,
&thread_id);
if (thread_handle == NULL) {
(unsigned)stack_size,
args,
0,
&thread_id);
}
/* AWT preloading (AFTER main thread start) */
#ifdef ENABLE_AWT_PRELOAD
/* D3D preloading */
if (awtPreloadD3D != 0) {
char *envValue;
/* D3D routines checks env.var J2D_D3D if no appropriate
* command line params was specified
*/
awtPreloadD3D = 0;
}
/* Test that AWT preloading isn't disabled by J2D_D3D_PRELOAD env.var */
awtPreloadD3D = 0;
}
if (awtPreloadD3D < 0) {
/* If awtPreloadD3D is still undefined (-1), test
* if it is turned on by J2D_D3D_PRELOAD env.var.
* By default it's turned OFF.
*/
awtPreloadD3D = 0;
awtPreloadD3D = 1;
}
}
}
if (awtPreloadD3D) {
}
#endif /* ENABLE_AWT_PRELOAD */
if (thread_handle) {
} else {
}
#ifdef ENABLE_AWT_PRELOAD
if (awtPreloaded) {
}
#endif /* ENABLE_AWT_PRELOAD */
return rslt;
}
/* Unix only, empty on windows. */
void SetJavaLauncherPlatformProps() {}
/*
* The implementation for finding classes from the bootstrap
* class loader, refer to java.h
*/
{
if (findBootClass == NULL) {
/* need to use the demangled entry point */
"JVM_FindClassFromBootLoader");
if (findBootClass == NULL) {
return NULL;
}
}
}
void
{
/*
* Required for javaw mode MessageBox output as well as for
* HotSpot -XX:+ShowMessageBoxOnError in java mode, an empty
* flag field is sufficient to perform the basic UI initialization.
*/
}
/* ============================== */
/* AWT preloading */
#ifdef ENABLE_AWT_PRELOAD
typedef int FnPreloadStart(void);
typedef void FnPreloadStop(void);
/*
* Starts AWT preloading
*/
int AWTPreload(const char *funcName)
{
int result = -1;
/* load AWT library once (if several preload function should be called) */
if (hPreloadAwt == NULL) {
/* awt.dll is not loaded yet */
char libraryPath[MAXPATHLEN];
int jrePathLen = 0;
while (1) {
/* awt.dll depends on jvm.dll & java.dll;
* jvm.dll is already loaded, so we need only java.dll;
* java.dll depends on MSVCRT lib & verify.dll.
*/
break;
}
/* save path length */
/* load msvcrt 1st */
LoadMSVCRT();
/* load verify.dll */
break;
}
/* restore jrePath */
libraryPath[jrePathLen] = 0;
/* load java.dll */
break;
}
/* restore jrePath */
libraryPath[jrePathLen] = 0;
/* load awt.dll */
if (hPreloadAwt == NULL) {
break;
}
/* get "preloadStop" func ptr */
break;
}
}
if (hPreloadAwt != NULL) {
/* don't forget to stop preloading */
awtPreloaded = 1;
}
}
return result;
}
/*
* Terminates AWT preloading
*/
void AWTPreloadStop() {
if (fnPreloadStop != NULL) {
}
}
#endif /* ENABLE_AWT_PRELOAD */
int
{
}
void
{
// stubbed out for windows and *nixes.
}
void
{
// stubbed out for windows and *nixes.
}
/*
* on windows, we return a false to indicate this option is not applicable
*/
ProcessPlatformOption(const char *arg)
{
return JNI_FALSE;
}
/*
* At this point we have the arguments to the application, and we need to
* check with original stdargs in order to compare which of these truly
* needs expansion. cmdtoargs will specify this if it finds a bare
* (unquoted) argument containing a glob character(s) ie. * or ?
*/
{
int stdargc;
if (argc == 0) {
}
// the holy grail we need to compare with.
stdargs = JLI_GetStdArgs();
stdargc = JLI_GetStdArgc();
// sanity check, this should never happen
JLI_TraceLauncher("passing arguments as-is.\n");
}
// sanity check, match the args we have, to the holy grail
// sanity check, ensure that the first argument of the arrays are the same
// some thing is amiss the args don't match
JLI_TraceLauncher("Warning: app args parsing error\n");
JLI_TraceLauncher("passing arguments as-is\n");
}
// make a copy of the args which will be expanded in java if required.
? stdargs[j].has_wildcard
: JNI_FALSE;
if (needs_expansion == JNI_FALSE)
// indicator char + String + NULL terminator, the java method will strip
// out the first character, the indicator character, so no matter what
// we add the indicator
}
if (!needs_expansion) {
// clean up any allocated memory and return back the old arguments
for (i = 0 ; i < argc ; i++) {
JLI_MemFree(nargv[i]);
}
}
"expandArgs",
// expand the arguments that require expansion, the java method will strip
// out the indicator character.
for (i = 0; i < argc; i++) {
JLI_MemFree(nargv[i]);
}
return outArray;
}