1 /*
2  * Copyright (c) 1998, 2020, 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 "java.h"
27 #include "jvm_md.h"
28 #include <dirent.h>
29 #include <dlfcn.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #ifdef __DragonFly__
39 #include <sys/sysctl.h>
40 #endif
41 #ifdef __FreeBSD__
42 #include <sys/sysctl.h>
43 #include <sys/procctl.h>
44 #ifndef PROC_STACKGAP_DISABLE
45 #define PROC_STACKGAP_DISABLE	0x0002
46 #endif
47 #ifndef PROC_STACKGAP_CTL
48 #define PROC_STACKGAP_CTL	17
49 #endif
50 #endif /* __FreeBSD__ */
51 #include "manifest_info.h"
52 
53 
54 #define JVM_DLL "libjvm.so"
55 #define JAVA_DLL "libjava.so"
56 #ifdef AIX
57 #define LD_LIBRARY_PATH "LIBPATH"
58 #else
59 #define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
60 #endif
61 
62 /* help jettison the LD_LIBRARY_PATH settings in the future */
63 #ifndef SETENV_REQUIRED
64 #define SETENV_REQUIRED
65 #endif
66 
67 /*
68  * Flowchart of launcher execs and options processing on unix
69  *
70  * The selection of the proper vm shared library to open depends on
71  * several classes of command line options, including vm "flavor"
72  * options (-client, -server).
73  * The vm selection options are not passed to the running
74  * virtual machine; they must be screened out by the launcher.
75  *
76  * The version specification (if any) is processed first by the
77  * platform independent routine SelectVersion.  This may result in
78  * the exec of the specified launcher version.
79  *
80  * Previously the launcher modified the LD_LIBRARY_PATH appropriately for the
81  * desired data model path, regardless if data models matched or not. The
82  * launcher subsequently exec'ed the desired executable, in order to make the
83  * LD_LIBRARY_PATH path available, for the runtime linker.
84  *
85  * Now, in most cases,the launcher will dlopen the target libjvm.so. All
86  * required libraries are loaded by the runtime linker, using the
87  * $RPATH/$ORIGIN baked into the shared libraries at compile time. Therefore,
88  * in most cases, the launcher will only exec, if the data models are
89  * mismatched, and will not set any environment variables, regardless of the
90  * data models.
91  *
92  * However, if the environment contains a LD_LIBRARY_PATH, this will cause the
93  * launcher to inspect the LD_LIBRARY_PATH. The launcher will check
94  *  a. if the LD_LIBRARY_PATH's first component is the path to the desired
95  *     libjvm.so
96  *  b. if any other libjvm.so is found in any of the paths.
97  * If case b is true, then the launcher will set the LD_LIBRARY_PATH to the
98  * desired JRE and reexec, in order to propagate the environment.
99  *
100  *  Main
101  *  (incoming argv)
102  *  |
103  * \|/
104  * CreateExecutionEnvironment
105  * (determines desired data model)
106  *  |
107  *  |
108  * \|/
109  *  Have Desired Model ? --> NO --> Exit(with error)
110  *  |
111  *  |
112  * \|/
113  * YES
114  *  |
115  *  |
116  * \|/
117  * CheckJvmType
118  * (removes -client, -server, etc.)
119  *  |
120  *  |
121  * \|/
122  * TranslateDashJArgs...
123  * (Prepare to pass args to vm)
124  *  |
125  *  |
126  * \|/
127  * ParseArguments
128  *   |
129  *   |
130  *  \|/
131  * RequiresSetenv
132  * Is LD_LIBRARY_PATH
133  * and friends set ? --> NO --> Continue
134  *  YES
135  *   |
136  *   |
137  *  \|/
138  * Path is desired JRE ? YES --> Continue
139  *  NO
140  *   |
141  *   |
142  *  \|/
143  * Paths have well known
144  * jvm paths ?       --> NO --> Error/Exit
145  *  YES
146  *   |
147  *   |
148  *  \|/
149  *  Does libjvm.so exist
150  *  in any of them ? --> NO  --> Continue
151  *   YES
152  *   |
153  *   |
154  *  \|/
155  *  Set the LD_LIBRARY_PATH
156  *   |
157  *   |
158  *  \|/
159  * Re-exec
160  *   |
161  *   |
162  *  \|/
163  * Main
164  */
165 
166 /* Store the name of the executable once computed */
167 static char *execname = NULL;
168 
169 /*
170  * execname accessor from other parts of platform dependent logic
171  */
172 const char *
GetExecName()173 GetExecName() {
174     return execname;
175 }
176 
177 #ifdef SETENV_REQUIRED
178 static jboolean
JvmExists(const char * path)179 JvmExists(const char *path) {
180     char tmp[PATH_MAX + 1];
181     struct stat statbuf;
182     JLI_Snprintf(tmp, PATH_MAX, "%s/%s", path, JVM_DLL);
183     if (stat(tmp, &statbuf) == 0) {
184         return JNI_TRUE;
185     }
186     return JNI_FALSE;
187 }
188 /*
189  * contains a lib/{server,client}/libjvm.so ?
190  */
191 static jboolean
ContainsLibJVM(const char * env)192 ContainsLibJVM(const char *env) {
193     /* the usual suspects */
194     char clientPattern[] = "lib/client";
195     char serverPattern[] = "lib/server";
196     char *envpath;
197     char *path;
198     char* save_ptr = NULL;
199     jboolean clientPatternFound;
200     jboolean serverPatternFound;
201 
202     /* fastest path */
203     if (env == NULL) {
204         return JNI_FALSE;
205     }
206 
207     /* to optimize for time, test if any of our usual suspects are present. */
208     clientPatternFound = JLI_StrStr(env, clientPattern) != NULL;
209     serverPatternFound = JLI_StrStr(env, serverPattern) != NULL;
210     if (clientPatternFound == JNI_FALSE && serverPatternFound == JNI_FALSE) {
211         return JNI_FALSE;
212     }
213 
214     /*
215      * we have a suspicious path component, check if it contains a libjvm.so
216      */
217     envpath = JLI_StringDup(env);
218     for (path = strtok_r(envpath, ":", &save_ptr); path != NULL; path = strtok_r(NULL, ":", &save_ptr)) {
219         if (clientPatternFound && JLI_StrStr(path, clientPattern) != NULL) {
220             if (JvmExists(path)) {
221                 JLI_MemFree(envpath);
222                 return JNI_TRUE;
223             }
224         }
225         if (serverPatternFound && JLI_StrStr(path, serverPattern)  != NULL) {
226             if (JvmExists(path)) {
227                 JLI_MemFree(envpath);
228                 return JNI_TRUE;
229             }
230         }
231     }
232     JLI_MemFree(envpath);
233     return JNI_FALSE;
234 }
235 
236 /*
237  * Test whether the environment variable needs to be set, see flowchart.
238  */
239 static jboolean
RequiresSetenv(const char * jvmpath)240 RequiresSetenv(const char *jvmpath) {
241     char jpath[PATH_MAX + 1];
242     char *llp;
243     char *dmllp = NULL;
244     char *p; /* a utility pointer */
245 
246 #ifdef AIX
247     /* We always have to set the LIBPATH on AIX because ld doesn't support $ORIGIN. */
248     return JNI_TRUE;
249 #endif
250 
251     llp = getenv("LD_LIBRARY_PATH");
252     /* no environment variable is a good environment variable */
253     if (llp == NULL && dmllp == NULL) {
254         return JNI_FALSE;
255     }
256 #ifdef __linux
257     /*
258      * On linux, if a binary is running as sgid or suid, glibc sets
259      * LD_LIBRARY_PATH to the empty string for security purposes. (In contrast,
260      * on Solaris the LD_LIBRARY_PATH variable for a privileged binary does not
261      * lose its settings; but the dynamic linker does apply more scrutiny to the
262      * path.) The launcher uses the value of LD_LIBRARY_PATH to prevent an exec
263      * loop, here and further downstream. Therefore, if we are running sgid or
264      * suid, this function's setting of LD_LIBRARY_PATH will be ineffective and
265      * we should case a return from the calling function.  Getting the right
266      * libraries will be handled by the RPATH. In reality, this check is
267      * redundant, as the previous check for a non-null LD_LIBRARY_PATH will
268      * return back to the calling function forthwith, it is left here to safe
269      * guard against any changes, in the glibc's existing security policy.
270      */
271     if ((getgid() != getegid()) || (getuid() != geteuid())) {
272         return JNI_FALSE;
273     }
274 #endif /* __linux */
275 
276 #ifdef _BSDONLY_SOURCE
277     /*
278      * The BSD's (except MacOSX which doesn't include this file), also clear
279      * LD_LIBRARY_PATH when a binary is running setuid or setgid.
280      */
281     if (issetugid()) {
282      return JNI_FALSE;
283     }
284 #endif /* _BSDONLY_SOURCE */
285 
286     /*
287      * Prevent recursions. Since LD_LIBRARY_PATH is the one which will be set by
288      * previous versions of the JRE, thus it is the only path that matters here.
289      * So we check to see if the desired JRE is set.
290      */
291     JLI_StrNCpy(jpath, jvmpath, PATH_MAX);
292     p = JLI_StrRChr(jpath, '/');
293     *p = '\0';
294     if (llp != NULL && JLI_StrNCmp(llp, jpath, JLI_StrLen(jpath)) == 0) {
295         return JNI_FALSE;
296     }
297 
298     /* scrutinize all the paths further */
299     if (llp != NULL &&  ContainsLibJVM(llp)) {
300         return JNI_TRUE;
301     }
302     if (dmllp != NULL && ContainsLibJVM(dmllp)) {
303         return JNI_TRUE;
304     }
305     return JNI_FALSE;
306 }
307 #endif /* SETENV_REQUIRED */
308 
309 void
CreateExecutionEnvironment(int * pargc,char *** pargv,char jrepath[],jint so_jrepath,char jvmpath[],jint so_jvmpath,char jvmcfg[],jint so_jvmcfg)310 CreateExecutionEnvironment(int *pargc, char ***pargv,
311                            char jrepath[], jint so_jrepath,
312                            char jvmpath[], jint so_jvmpath,
313                            char jvmcfg[],  jint so_jvmcfg) {
314 
315     char * jvmtype = NULL;
316     int argc = *pargc;
317     char **argv = *pargv;
318 
319 #ifdef SETENV_REQUIRED
320     jboolean mustsetenv = JNI_FALSE;
321     char *runpath = NULL; /* existing effective LD_LIBRARY_PATH setting */
322     char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */
323     char* newpath = NULL; /* path on new LD_LIBRARY_PATH */
324     char* lastslash = NULL;
325     char** newenvp = NULL; /* current environment */
326     size_t new_runpath_size;
327 #endif  /* SETENV_REQUIRED */
328 
329     /* Compute/set the name of the executable */
330     SetExecname(*pargv);
331 
332     /* Check to see if the jvmpath exists */
333     /* Find out where the JRE is that we will be using. */
334     if (!GetJREPath(jrepath, so_jrepath, JNI_FALSE)) {
335         JLI_ReportErrorMessage(JRE_ERROR1);
336         exit(2);
337     }
338     JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%sjvm.cfg",
339             jrepath, FILESEP, FILESEP);
340     /* Find the specified JVM type */
341     if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) {
342         JLI_ReportErrorMessage(CFG_ERROR7);
343         exit(1);
344     }
345 
346     jvmpath[0] = '\0';
347     jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE);
348     if (JLI_StrCmp(jvmtype, "ERROR") == 0) {
349         JLI_ReportErrorMessage(CFG_ERROR9);
350         exit(4);
351     }
352 
353     if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath)) {
354         JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath);
355         exit(4);
356     }
357     /*
358      * we seem to have everything we need, so without further ado
359      * we return back, otherwise proceed to set the environment.
360      */
361 #ifdef SETENV_REQUIRED
362     mustsetenv = RequiresSetenv(jvmpath);
363     JLI_TraceLauncher("mustsetenv: %s\n", mustsetenv ? "TRUE" : "FALSE");
364 
365     if (mustsetenv == JNI_FALSE) {
366         return;
367     }
368 #else
369     return;
370 #endif /* SETENV_REQUIRED */
371 
372 #ifdef SETENV_REQUIRED
373     if (mustsetenv) {
374         /*
375          * We will set the LD_LIBRARY_PATH as follows:
376          *
377          *     o          $JVMPATH (directory portion only)
378          *     o          $JRE/lib
379          *     o          $JRE/../lib
380          *
381          * followed by the user's previous effective LD_LIBRARY_PATH, if
382          * any.
383          */
384 
385         runpath = getenv(LD_LIBRARY_PATH);
386 
387         /* runpath contains current effective LD_LIBRARY_PATH setting */
388         { /* New scope to declare local variable */
389             char *new_jvmpath = JLI_StringDup(jvmpath);
390             new_runpath_size = ((runpath != NULL) ? JLI_StrLen(runpath) : 0) +
391                     2 * JLI_StrLen(jrepath) +
392                     JLI_StrLen(new_jvmpath) + 52;
393             new_runpath = JLI_MemAlloc(new_runpath_size);
394             newpath = new_runpath + JLI_StrLen(LD_LIBRARY_PATH "=");
395 
396 
397             /*
398              * Create desired LD_LIBRARY_PATH value for target data model.
399              */
400             {
401                 /* remove the name of the .so from the JVM path */
402                 lastslash = JLI_StrRChr(new_jvmpath, '/');
403                 if (lastslash)
404                     *lastslash = '\0';
405 
406                 sprintf(new_runpath, LD_LIBRARY_PATH "="
407                         "%s:"
408                         "%s/lib:"
409                         "%s/../lib",
410                         new_jvmpath,
411                         jrepath,
412                         jrepath
413                         );
414 
415                 JLI_MemFree(new_jvmpath);
416 
417                 /*
418                  * Check to make sure that the prefix of the current path is the
419                  * desired environment variable setting, though the RequiresSetenv
420                  * checks if the desired runpath exists, this logic does a more
421                  * comprehensive check.
422                  */
423                 if (runpath != NULL &&
424                         JLI_StrNCmp(newpath, runpath, JLI_StrLen(newpath)) == 0 &&
425                         (runpath[JLI_StrLen(newpath)] == 0 ||
426                         runpath[JLI_StrLen(newpath)] == ':')) {
427                     JLI_MemFree(new_runpath);
428                     return;
429                 }
430             }
431         }
432 
433         /*
434          * Place the desired environment setting onto the prefix of
435          * LD_LIBRARY_PATH.  Note that this prevents any possible infinite
436          * loop of execv() because we test for the prefix, above.
437          */
438         if (runpath != 0) {
439             /* ensure storage for runpath + colon + NULL */
440             if ((JLI_StrLen(runpath) + 1 + 1) > new_runpath_size) {
441                 JLI_ReportErrorMessageSys(JRE_ERROR11);
442                 exit(1);
443             }
444             JLI_StrCat(new_runpath, ":");
445             JLI_StrCat(new_runpath, runpath);
446         }
447 
448         if (putenv(new_runpath) != 0) {
449             /* problem allocating memory; LD_LIBRARY_PATH not set properly */
450             exit(1);
451         }
452 
453         /*
454          * Unix systems document that they look at LD_LIBRARY_PATH only
455          * once at startup, so we have to re-exec the current executable
456          * to get the changed environment variable to have an effect.
457          */
458 
459         newenvp = environ;
460     }
461 #endif /* SETENV_REQUIRED */
462     {
463         char *newexec = execname;
464         JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
465         (void) fflush(stdout);
466         (void) fflush(stderr);
467 #ifdef SETENV_REQUIRED
468         if (mustsetenv) {
469             execve(newexec, argv, newenvp);
470         } else {
471             execv(newexec, argv);
472         }
473 #else /* !SETENV_REQUIRED */
474         execv(newexec, argv);
475 #endif /* SETENV_REQUIRED */
476         JLI_ReportErrorMessageSys(JRE_ERROR4, newexec);
477     }
478     exit(1);
479 }
480 
481 
482 static jboolean
GetJVMPath(const char * jrepath,const char * jvmtype,char * jvmpath,jint jvmpathsize)483 GetJVMPath(const char *jrepath, const char *jvmtype,
484            char *jvmpath, jint jvmpathsize)
485 {
486     struct stat s;
487 
488     if (JLI_StrChr(jvmtype, '/')) {
489         JLI_Snprintf(jvmpath, jvmpathsize, "%s/" JVM_DLL, jvmtype);
490     } else {
491         JLI_Snprintf(jvmpath, jvmpathsize, "%s/lib/%s/" JVM_DLL, jrepath, jvmtype);
492     }
493 
494     JLI_TraceLauncher("Does `%s' exist ... ", jvmpath);
495 
496     if (stat(jvmpath, &s) == 0) {
497         JLI_TraceLauncher("yes.\n");
498         return JNI_TRUE;
499     } else {
500         JLI_TraceLauncher("no.\n");
501         return JNI_FALSE;
502     }
503 }
504 
505 /*
506  * Find path to JRE based on .exe's location or registry settings.
507  */
508 static jboolean
GetJREPath(char * path,jint pathsize,jboolean speculative)509 GetJREPath(char *path, jint pathsize, jboolean speculative)
510 {
511     char libjava[MAXPATHLEN];
512     struct stat s;
513 
514     if (GetApplicationHome(path, pathsize)) {
515         /* Is JRE co-located with the application? */
516         JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path);
517         if (access(libjava, F_OK) == 0) {
518             JLI_TraceLauncher("JRE path is %s\n", path);
519             return JNI_TRUE;
520         }
521         /* ensure storage for path + /jre + NULL */
522         if ((JLI_StrLen(path) + 4  + 1) > (size_t) pathsize) {
523             JLI_TraceLauncher("Insufficient space to store JRE path\n");
524             return JNI_FALSE;
525         }
526         /* Does the app ship a private JRE in <apphome>/jre directory? */
527         JLI_Snprintf(libjava, sizeof(libjava), "%s/jre/lib/" JAVA_DLL, path);
528         if (access(libjava, F_OK) == 0) {
529             JLI_StrCat(path, "/jre");
530             JLI_TraceLauncher("JRE path is %s\n", path);
531             return JNI_TRUE;
532         }
533     }
534 
535     if (GetApplicationHomeFromDll(path, pathsize)) {
536         JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path);
537         if (stat(libjava, &s) == 0) {
538             JLI_TraceLauncher("JRE path is %s\n", path);
539             return JNI_TRUE;
540         }
541     }
542 
543     if (!speculative)
544       JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
545     return JNI_FALSE;
546 }
547 
548 jboolean
LoadJavaVM(const char * jvmpath,InvocationFunctions * ifn)549 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
550 {
551     void *libjvm;
552 
553     JLI_TraceLauncher("JVM path is %s\n", jvmpath);
554 
555     libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
556     if (libjvm == NULL) {
557         JLI_ReportErrorMessage(DLL_ERROR1, __LINE__);
558         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
559         return JNI_FALSE;
560     }
561 
562     ifn->CreateJavaVM = (CreateJavaVM_t)
563         dlsym(libjvm, "JNI_CreateJavaVM");
564     if (ifn->CreateJavaVM == NULL) {
565         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
566         return JNI_FALSE;
567     }
568 
569     ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)
570         dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs");
571     if (ifn->GetDefaultJavaVMInitArgs == NULL) {
572         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
573         return JNI_FALSE;
574     }
575 
576     ifn->GetCreatedJavaVMs = (GetCreatedJavaVMs_t)
577         dlsym(libjvm, "JNI_GetCreatedJavaVMs");
578     if (ifn->GetCreatedJavaVMs == NULL) {
579         JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
580         return JNI_FALSE;
581     }
582 
583     return JNI_TRUE;
584 }
585 
586 /*
587  * Compute the name of the executable
588  *
589  * In order to re-exec securely we need the absolute path of the
590  * executable. On Solaris getexecname(3c) may not return an absolute
591  * path so we use dladdr to get the filename of the executable and
592  * then use realpath to derive an absolute path. From Solaris 9
593  * onwards the filename returned in DL_info structure from dladdr is
594  * an absolute pathname so technically realpath isn't required.
595  * On Linux we read the executable name from /proc/self/exe.
596  * On FreeBSD, we get the executable name via sysctl(3).
597  * As a fallback, and for platforms other than Solaris, Linux, and
598  * FreeBSD we use FindExecName to compute the executable name.
599  */
600 const char*
SetExecname(char ** argv)601 SetExecname(char **argv)
602 {
603     char* exec_path = NULL;
604 #if defined(__linux__)
605     {
606         const char* self = "/proc/self/exe";
607         char buf[PATH_MAX+1];
608         int len = readlink(self, buf, PATH_MAX);
609         if (len >= 0) {
610             buf[len] = '\0';            /* readlink(2) doesn't NUL terminate */
611             exec_path = JLI_StringDup(buf);
612         }
613     }
614 #elif defined(__FreeBSD__) || defined(__DragonFly__)
615     {
616         char buf[PATH_MAX+1];
617         int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
618         size_t len = sizeof(buf);
619         if (sysctl(name, 4, buf, &len, NULL, 0) == 0 && len > 0) {
620             buf[len] = '\0';
621             exec_path = JLI_StringDup(buf);
622         }
623     }
624 #else /* !__linux__ && !__FreeBSD__ */
625     {
626         /* Not implemented */
627     }
628 #endif
629 
630     if (exec_path == NULL) {
631         exec_path = FindExecName(argv[0]);
632     }
633     execname = exec_path;
634     return exec_path;
635 }
636 
637 /* --- Splash Screen shared library support --- */
638 static const char* SPLASHSCREEN_SO = JNI_LIB_NAME("splashscreen");
639 static void* hSplashLib = NULL;
640 
SplashProcAddress(const char * name)641 void* SplashProcAddress(const char* name) {
642     if (!hSplashLib) {
643         int ret;
644         char jrePath[MAXPATHLEN];
645         char splashPath[MAXPATHLEN];
646 
647         if (!GetJREPath(jrePath, sizeof(jrePath), JNI_FALSE)) {
648             JLI_ReportErrorMessage(JRE_ERROR1);
649             return NULL;
650         }
651         ret = JLI_Snprintf(splashPath, sizeof(splashPath), "%s/lib/%s",
652                      jrePath, SPLASHSCREEN_SO);
653 
654         if (ret >= (int) sizeof(splashPath)) {
655             JLI_ReportErrorMessage(JRE_ERROR11);
656             return NULL;
657         }
658         if (ret < 0) {
659             JLI_ReportErrorMessage(JRE_ERROR13);
660             return NULL;
661         }
662         hSplashLib = dlopen(splashPath, RTLD_LAZY | RTLD_GLOBAL);
663         JLI_TraceLauncher("Info: loaded %s\n", splashPath);
664     }
665     if (hSplashLib) {
666         void* sym = dlsym(hSplashLib, name);
667         return sym;
668     } else {
669         return NULL;
670     }
671 }
672 
673 /*
674  * Signature adapter for pthread_create() or thr_create().
675  */
ThreadJavaMain(void * args)676 static void* ThreadJavaMain(void* args) {
677     return (void*)(intptr_t)JavaMain(args);
678 }
679 
680 /*
681  * Block current thread and continue execution in a new thread.
682  */
683 int
CallJavaMainInNewThread(jlong stack_size,void * args)684 CallJavaMainInNewThread(jlong stack_size, void* args) {
685     int rslt;
686     pthread_t tid;
687     pthread_attr_t attr;
688     pthread_attr_init(&attr);
689     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
690 
691     if (stack_size > 0) {
692         pthread_attr_setstacksize(&attr, stack_size);
693     }
694     pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads
695 
696     if (pthread_create(&tid, &attr, ThreadJavaMain, args) == 0) {
697         void* tmp;
698         pthread_join(tid, &tmp);
699         rslt = (int)(intptr_t)tmp;
700     } else {
701        /*
702         * Continue execution in current thread if for some reason (e.g. out of
703         * memory/LWP)  a new thread can't be created. This will likely fail
704         * later in JavaMain as JNI_CreateJavaVM needs to create quite a
705         * few new threads, anyway, just give it a try..
706         */
707         rslt = JavaMain(args);
708     }
709 
710     pthread_attr_destroy(&attr);
711     return rslt;
712 }
713 
714 /* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */
715 #define MAX_PID_STR_SZ   20
716 
717 int
JVMInit(InvocationFunctions * ifn,jlong threadStackSize,int argc,char ** argv,int mode,char * what,int ret)718 JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
719         int argc, char **argv,
720         int mode, char *what, int ret)
721 {
722 #ifdef __FreeBSD__
723     /*
724      * Kernel stack guard pages interfere with the JVM's guard pages on the
725      * thread stacks and prevent correct stack overflow detection and the
726      * use of reserved pages to allow critical sections to complete.
727      *
728      * Attempt to disable the kernel stack guard pages here before any threads
729      * are created.
730      */
731     int arg = PROC_STACKGAP_DISABLE;
732     procctl(P_PID, getpid(), PROC_STACKGAP_CTL, &arg);
733 #endif
734     ShowSplashScreen();
735     return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
736 }
737 
738 void
PostJVMInit(JNIEnv * env,jclass mainClass,JavaVM * vm)739 PostJVMInit(JNIEnv *env, jclass mainClass, JavaVM *vm)
740 {
741     // stubbed out for windows and *nixes.
742 }
743 
744 void
RegisterThread()745 RegisterThread()
746 {
747     // stubbed out for windows and *nixes.
748 }
749 
750 /*
751  * on unix, we return a false to indicate this option is not applicable
752  */
753 jboolean
ProcessPlatformOption(const char * arg)754 ProcessPlatformOption(const char *arg)
755 {
756     return JNI_FALSE;
757 }
758