8338768: Introduce runtime lookup to check for static builds

Co-authored-by: Magnus Ihse Bursie <ihse@openjdk.org>
Co-authored-by: Jiangli Zhou <jiangli@openjdk.org>
Reviewed-by: prr, jiangli, alanb
This commit is contained in:
Magnus Ihse Bursie 2024-09-02 09:14:36 +00:00
parent 9d7d85a6aa
commit a136a85b6f
12 changed files with 184 additions and 96 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -428,9 +428,9 @@ GetJVMPath(const char *jrepath, const char *jvmtype,
JLI_TraceLauncher("Does `%s' exist ... ", jvmpath);
#ifdef STATIC_BUILD
return JNI_TRUE;
#else
if (JLI_IsStaticallyLinked()) {
return JNI_TRUE;
}
if (stat(jvmpath, &s) == 0) {
JLI_TraceLauncher("yes.\n");
return JNI_TRUE;
@ -438,7 +438,6 @@ GetJVMPath(const char *jrepath, const char *jvmtype,
JLI_TraceLauncher("no.\n");
return JNI_FALSE;
}
#endif
}
/*
@ -451,18 +450,18 @@ GetJREPath(char *path, jint pathsize, jboolean speculative)
if (GetApplicationHome(path, pathsize)) {
/* Is JRE co-located with the application? */
#ifdef STATIC_BUILD
char jvm_cfg[MAXPATHLEN];
JLI_Snprintf(jvm_cfg, sizeof(jvm_cfg), "%s/lib/jvm.cfg", path);
if (access(jvm_cfg, F_OK) == 0) {
return JNI_TRUE;
if (JLI_IsStaticallyLinked()) {
char jvm_cfg[MAXPATHLEN];
JLI_Snprintf(jvm_cfg, sizeof(jvm_cfg), "%s/lib/jvm.cfg", path);
if (access(jvm_cfg, F_OK) == 0) {
return JNI_TRUE;
}
} else {
JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path);
if (access(libjava, F_OK) == 0) {
return JNI_TRUE;
}
}
#else
JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/" JAVA_DLL, path);
if (access(libjava, F_OK) == 0) {
return JNI_TRUE;
}
#endif
/* ensure storage for path + /jre + NULL */
if ((JLI_StrLen(path) + 4 + 1) > (size_t) pathsize) {
JLI_TraceLauncher("Insufficient space to store JRE path\n");
@ -481,23 +480,24 @@ GetJREPath(char *path, jint pathsize, jboolean speculative)
Dl_info selfInfo;
dladdr(&GetJREPath, &selfInfo);
#ifdef STATIC_BUILD
char jvm_cfg[MAXPATHLEN];
char *p = NULL;
strncpy(jvm_cfg, selfInfo.dli_fname, MAXPATHLEN);
p = strrchr(jvm_cfg, '/'); *p = '\0';
p = strrchr(jvm_cfg, '/');
if (strcmp(p, "/.") == 0) {
*p = '\0';
p = strrchr(jvm_cfg, '/'); *p = '\0';
if (JLI_IsStaticallyLinked()) {
char jvm_cfg[MAXPATHLEN];
char *p = NULL;
strncpy(jvm_cfg, selfInfo.dli_fname, MAXPATHLEN);
p = strrchr(jvm_cfg, '/'); *p = '\0';
p = strrchr(jvm_cfg, '/');
if (strcmp(p, "/.") == 0) {
*p = '\0';
p = strrchr(jvm_cfg, '/'); *p = '\0';
} else {
*p = '\0';
}
strncpy(path, jvm_cfg, pathsize);
strncat(jvm_cfg, "/lib/jvm.cfg", MAXPATHLEN);
if (access(jvm_cfg, F_OK) == 0) {
return JNI_TRUE;
}
}
else *p = '\0';
strncpy(path, jvm_cfg, pathsize);
strncat(jvm_cfg, "/lib/jvm.cfg", MAXPATHLEN);
if (access(jvm_cfg, F_OK) == 0) {
return JNI_TRUE;
}
#endif
char *realPathToSelf = realpath(selfInfo.dli_fname, path);
if (realPathToSelf != path) {
@ -549,11 +549,12 @@ LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
JLI_TraceLauncher("JVM path is %s\n", jvmpath);
#ifndef STATIC_BUILD
libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
#else
libjvm = dlopen(NULL, RTLD_FIRST);
#endif
if (!JLI_IsStaticallyLinked()) {
libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
} else {
libjvm = dlopen(NULL, RTLD_FIRST);
}
if (libjvm == NULL) {
JLI_ReportErrorMessage(DLL_ERROR1, __LINE__);
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
@ -603,14 +604,13 @@ SetExecname(char **argv)
char* exec_path = NULL;
{
Dl_info dlinfo;
#ifdef STATIC_BUILD
void *fptr;
fptr = (void *)&SetExecname;
#else
int (*fptr)();
fptr = (int (*)())dlsym(RTLD_DEFAULT, "main");
#endif
if (JLI_IsStaticallyLinked()) {
fptr = (void *)&SetExecname;
} else {
fptr = dlsym(RTLD_DEFAULT, "main");
}
if (fptr == NULL) {
JLI_ReportErrorMessage(DLL_ERROR3, dlerror());
return JNI_FALSE;

View file

@ -113,6 +113,9 @@ JLI_SetTraceLauncher();
jboolean JLI_IsTraceLauncher();
// This is defined in link_type.c due to linking restraints
jboolean JLI_IsStaticallyLinked();
/*
* JLI_List - a dynamic list of char*
*/

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* 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 "jni.h"
// This is in a separate file since it will need to be compiled to two different
// object files, depending on if we are going to build a static or a dynamic
// library.
jboolean JLI_IsStaticallyLinked(void) {
#ifdef STATIC_BUILD
return JNI_TRUE;
#else
return JNI_FALSE;
#endif
}