1 /*
  2  * Copyright (c) 1998, 2019, 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 #ifndef _JAVA_H_
 27 #define _JAVA_H_
 28 
 29 #include <stdio.h>
 30 #include <stdlib.h>
 31 #include <string.h>
 32 #include <limits.h>
 33 
 34 #include <jni.h>
 35 #include <jvm.h>
 36 
 37 /*
 38  * Get system specific defines.
 39  */
 40 #include "emessages.h"
 41 #include "java_md.h"
 42 #include "jli_util.h"
 43 #include "manifest_info.h"
 44 #include "wildcard.h"
 45 #include "splashscreen.h"
 46 
 47 # define KB (1024UL)
 48 # define MB (1024UL * KB)
 49 # define GB (1024UL * MB)
 50 
 51 #define CURRENT_DATA_MODEL (CHAR_BIT * sizeof(void*))
 52 
 53 /*
 54  * The following environment variable is used to influence the behavior
 55  * of the jre exec'd through the SelectVersion routine.  The command line
 56  * options which specify the version are not passed to the exec'd version,
 57  * because that jre may be an older version which wouldn't recognize them.
 58  * This environment variable is known to this (and later) version and serves
 59  * to suppress the version selection code.  This is not only for efficiency,
 60  * but also for correctness, since any command line options have been
 61  * removed which would cause any value found in the manifest to be used.
 62  * This would be incorrect because the command line options are defined
 63  * to take precedence.
 64  *
 65  * The value associated with this environment variable is the MainClass
 66  * name from within the executable jar file (if any). This is strictly a
 67  * performance enhancement to avoid re-reading the jar file manifest.
 68  *
 69  */
 70 #define ENV_ENTRY "_JAVA_VERSION_SET"
 71 
 72 #define SPLASH_FILE_ENV_ENTRY "_JAVA_SPLASH_FILE"
 73 #define SPLASH_JAR_ENV_ENTRY "_JAVA_SPLASH_JAR"
 74 #define JDK_JAVA_OPTIONS "JDK_JAVA_OPTIONS"
 75 
 76 #ifdef INCLUDE_TSAN
 77 typedef void (*TsanSymbolizeAddFrameFunc)(
 78     void *ctx, const char *function, const char *file, int line, int column);
 79 typedef void (JNICALL *TsanSymbolize_t)(uint64_t, TsanSymbolizeAddFrameFunc, void *);
 80 extern TsanSymbolize_t tsan_symbolize_func;
 81 #endif
 82 
 83 /*
 84  * Pointers to the needed JNI invocation API, initialized by LoadJavaVM.
 85  */
 86 typedef jint (JNICALL *CreateJavaVM_t)(JavaVM **pvm, void **env, void *args);
 87 typedef jint (JNICALL *GetDefaultJavaVMInitArgs_t)(void *args);
 88 typedef jint (JNICALL *GetCreatedJavaVMs_t)(JavaVM **vmBuf, jsize bufLen, jsize *nVMs);
 89 
 90 typedef struct {
 91     CreateJavaVM_t CreateJavaVM;
 92     GetDefaultJavaVMInitArgs_t GetDefaultJavaVMInitArgs;
 93     GetCreatedJavaVMs_t GetCreatedJavaVMs;
 94 #ifdef INCLUDE_TSAN
 95     TsanSymbolize_t TsanSymbolize;
 96 #endif
 97 } InvocationFunctions;
 98 
 99 JNIEXPORT int JNICALL
100 JLI_Launch(int argc, char ** argv,              /* main argc, argc */
101         int jargc, const char** jargv,          /* java args */
102         int appclassc, const char** appclassv,  /* app classpath */
103         const char* fullversion,                /* full version defined */
104         const char* dotversion,                 /* dot version defined */
105         const char* pname,                      /* program name */
106         const char* lname,                      /* launcher name */
107         jboolean javaargs,                      /* JAVA_ARGS */
108         jboolean cpwildcard,                    /* classpath wildcard */
109         jboolean javaw,                         /* windows-only javaw */
110         jint     ergo_class                     /* ergnomics policy */
111 );
112 
113 /*
114  * Prototypes for launcher functions in the system specific java_md.c.
115  */
116 
117 jboolean
118 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn);
119 
120 void
121 GetXUsagePath(char *buf, jint bufsize);
122 
123 jboolean
124 GetApplicationHome(char *buf, jint bufsize);
125 
126 jboolean
127 GetApplicationHomeFromDll(char *buf, jint bufsize);
128 
129 #define GetArch() GetArchPath(CURRENT_DATA_MODEL)
130 
131 /*
132  * Different platforms will implement this, here
133  * pargc is a pointer to the original argc,
134  * pargv is a pointer to the original argv,
135  * jrepath is an accessible path to the jre as determined by the call
136  * so_jrepath is the length of the buffer jrepath
137  * jvmpath is an accessible path to the jvm as determined by the call
138  * so_jvmpath is the length of the buffer jvmpath
139  */
140 void CreateExecutionEnvironment(int *argc, char ***argv,
141                                 char *jrepath, jint so_jrepath,
142                                 char *jvmpath, jint so_jvmpath,
143                                 char *jvmcfg,  jint so_jvmcfg);
144 
145 /* Reports an error message to stderr or a window as appropriate. */
146 JNIEXPORT void JNICALL
147 JLI_ReportErrorMessage(const char * message, ...);
148 
149 /* Reports a system error message to stderr or a window */
150 JNIEXPORT void JNICALL
151 JLI_ReportErrorMessageSys(const char * message, ...);
152 
153 /* Reports an error message only to stderr. */
154 JNIEXPORT void JNICALL
155 JLI_ReportMessage(const char * message, ...);
156 
157 /* Reports a message only to stdout. */
158 void JLI_ShowMessage(const char * message, ...);
159 
160 /*
161  * Reports an exception which terminates the vm to stderr or a window
162  * as appropriate.
163  */
164 JNIEXPORT void JNICALL
165 JLI_ReportExceptionDescription(JNIEnv * env);
166 void PrintMachineDependentOptions();
167 
168 /*
169  * Block current thread and continue execution in new thread.
170  */
171 int CallJavaMainInNewThread(jlong stack_size, void* args);
172 
173 /* sun.java.launcher.* platform properties. */
174 void SetJavaCommandLineProp(char* what, int argc, char** argv);
175 
176 /*
177  * Functions defined in java.c and used in java_md.c.
178  */
179 jint ReadKnownVMs(const char *jvmcfg, jboolean speculative);
180 char *CheckJvmType(int *argc, char ***argv, jboolean speculative);
181 void AddOption(char *str, void *info);
182 jboolean IsWhiteSpaceOption(const char* name);
183 
184 // Utility function defined in args.c
185 int isTerminalOpt(char *arg);
186 jboolean IsJavaw();
187 
188 int ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
189                    int argc, char** argv,
190                    int mode, char *what, int ret);
191 
192 int JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
193                    int argc, char** argv,
194                    int mode, char *what, int ret);
195 
196 /*
197  * Initialize platform specific settings
198  */
199 void InitLauncher(jboolean javaw);
200 
201 /*
202  * For MacOSX and Windows/Unix compatibility we require these
203  * entry points, some of them may be stubbed out on Windows/Unixes.
204  */
205 void     PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm);
206 void     ShowSplashScreen();
207 void     RegisterThread();
208 /*
209  * this method performs additional platform specific processing and
210  * should return JNI_TRUE to indicate the argument has been consumed,
211  * otherwise returns JNI_FALSE to allow the calling logic to further
212  * process the option.
213  */
214 jboolean ProcessPlatformOption(const char *arg);
215 
216 /*
217  * This allows for finding classes from the VM's bootstrap class loader directly,
218  * FindClass uses the application class loader internally, this will cause
219  * unnecessary searching of the classpath for the required classes.
220  *
221  */
222 typedef jclass (JNICALL FindClassFromBootLoader_t(JNIEnv *env,
223                                                   const char *name));
224 jclass FindBootStrapClass(JNIEnv *env, const char *classname);
225 
226 jobjectArray CreateApplicationArgs(JNIEnv *env, char **strv, int argc);
227 jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
228 jclass GetLauncherHelperClass(JNIEnv *env);
229 
230 /*
231  * Entry point.
232  */
233 int JavaMain(void* args);
234 
235 enum LaunchMode {               // cf. sun.launcher.LauncherHelper
236     LM_UNKNOWN = 0,
237     LM_CLASS,
238     LM_JAR,
239     LM_MODULE,
240     LM_SOURCE
241 };
242 
243 static const char *launchModeNames[]
244     = { "Unknown", "Main class", "JAR file", "Module", "Source" };
245 
246 typedef struct {
247     int    argc;
248     char **argv;
249     int    mode;
250     char  *what;
251     InvocationFunctions ifn;
252 } JavaMainArgs;
253 
254 #define NULL_CHECK_RETURN_VALUE(NCRV_check_pointer, NCRV_return_value) \
255     do { \
256         if ((NCRV_check_pointer) == NULL) { \
257             JLI_ReportErrorMessage(JNI_ERROR); \
258             return NCRV_return_value; \
259         } \
260     } while (JNI_FALSE)
261 
262 #define NULL_CHECK0(NC0_check_pointer) \
263     NULL_CHECK_RETURN_VALUE(NC0_check_pointer, 0)
264 
265 #define NULL_CHECK(NC_check_pointer) \
266     NULL_CHECK_RETURN_VALUE(NC_check_pointer, )
267 
268 #define CHECK_EXCEPTION_RETURN_VALUE(CER_value) \
269     do { \
270         if ((*env)->ExceptionOccurred(env)) { \
271             return CER_value; \
272         } \
273     } while (JNI_FALSE)
274 
275 #define CHECK_EXCEPTION_RETURN() \
276     do { \
277         if ((*env)->ExceptionOccurred(env)) { \
278             return; \
279         } \
280     } while (JNI_FALSE)
281 
282 #endif /* _JAVA_H_ */