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

@ -69,8 +69,8 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJDWP, \
EXTRA_HEADER_DIRS := \ EXTRA_HEADER_DIRS := \
include \ include \
libjdwp/export \ libjdwp/export \
java.base:libjava \ java.base:libjava, \
java.base:libjvm, \ JDK_LIBS := java.base:libjvm, \
LIBS_linux := $(LIBDL), \ LIBS_linux := $(LIBDL), \
LIBS_macosx := -liconv, \ LIBS_macosx := -liconv, \
LIBS_aix := -liconv, \ LIBS_aix := -liconv, \

View File

@ -473,9 +473,9 @@ void os::init_system_properties_values() {
if (pslash != nullptr) { if (pslash != nullptr) {
*pslash = '\0'; // Get rid of /{client|server|hotspot}. *pslash = '\0'; // Get rid of /{client|server|hotspot}.
} }
#ifdef STATIC_BUILD if (is_vm_statically_linked()) {
strcat(buf, "/lib"); strcat(buf, "/lib");
#endif }
Arguments::set_dll_dir(buf); Arguments::set_dll_dir(buf);
@ -1093,19 +1093,20 @@ void *os::Bsd::dlopen_helper(const char *filename, int mode, char *ebuf, int ebu
#ifdef __APPLE__ #ifdef __APPLE__
void * os::dll_load(const char *filename, char *ebuf, int ebuflen) { void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
#ifdef STATIC_BUILD if (is_vm_statically_linked()) {
return os::get_default_process_handle(); return os::get_default_process_handle();
#else }
log_info(os)("attempting shared library load of %s", filename); log_info(os)("attempting shared library load of %s", filename);
return os::Bsd::dlopen_helper(filename, RTLD_LAZY, ebuf, ebuflen); return os::Bsd::dlopen_helper(filename, RTLD_LAZY, ebuf, ebuflen);
#endif // STATIC_BUILD
} }
#else #else
void * os::dll_load(const char *filename, char *ebuf, int ebuflen) { void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
#ifdef STATIC_BUILD if (is_vm_statically_linked()) {
return os::get_default_process_handle(); return os::get_default_process_handle();
#else }
log_info(os)("attempting shared library load of %s", filename); log_info(os)("attempting shared library load of %s", filename);
void* result; void* result;
@ -1269,7 +1270,6 @@ void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
} }
return nullptr; return nullptr;
#endif // STATIC_BUILD
} }
#endif // !__APPLE__ #endif // !__APPLE__

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -37,6 +37,7 @@
#include "memory/universe.hpp" #include "memory/universe.hpp"
#include "oops/oop.inline.hpp" #include "oops/oop.inline.hpp"
#include "runtime/handles.inline.hpp" #include "runtime/handles.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/os.hpp" #include "runtime/os.hpp"
#include "runtime/stubCodeGenerator.hpp" #include "runtime/stubCodeGenerator.hpp"
#include "runtime/stubRoutines.hpp" #include "runtime/stubRoutines.hpp"
@ -785,13 +786,13 @@ bool Disassembler::load_library(outputStream* st) {
os::jvm_path(buf, sizeof(buf)); os::jvm_path(buf, sizeof(buf));
int jvm_offset = -1; int jvm_offset = -1;
int lib_offset = -1; int lib_offset = -1;
#ifdef STATIC_BUILD
char* p = strrchr(buf, '/'); if (is_vm_statically_linked()) {
*p = '\0'; char* p = strrchr(buf, '/');
strcat(p, "/lib/"); *p = '\0';
lib_offset = jvm_offset = (int)strlen(buf); strcat(p, "/lib/");
#else lib_offset = jvm_offset = (int)strlen(buf);
{ } else {
// Match "libjvm" instead of "jvm" on *nix platforms. Creates better matches. // Match "libjvm" instead of "jvm" on *nix platforms. Creates better matches.
// Match "[lib]jvm[^/]*" in jvm_path. // Match "[lib]jvm[^/]*" in jvm_path.
const char* base = buf; const char* base = buf;
@ -805,7 +806,6 @@ bool Disassembler::load_library(outputStream* st) {
if (p != nullptr) jvm_offset = p - base + 3; // this points to 'j' in libjvm. if (p != nullptr) jvm_offset = p - base + 3; // this points to 'j' in libjvm.
#endif #endif
} }
#endif
// Find the disassembler shared library. // Find the disassembler shared library.
// Search for several paths derived from libjvm, in this order: // Search for several paths derived from libjvm, in this order:

View File

@ -180,6 +180,9 @@ JVM_IsContinuationsSupported(void);
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
JVM_IsForeignLinkerSupported(void); JVM_IsForeignLinkerSupported(void);
JNIEXPORT jboolean JNICALL
JVM_IsStaticallyLinked(void);
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
JVM_InitializeFromArchive(JNIEnv* env, jclass cls); JVM_InitializeFromArchive(JNIEnv* env, jclass cls);

View File

@ -3448,6 +3448,10 @@ JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE; return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
JVM_END JVM_END
JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
JVM_END
// String support /////////////////////////////////////////////////////////////////////////// // String support ///////////////////////////////////////////////////////////////////////////
JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -59,6 +59,9 @@ extern void vm_shutdown_during_initialization(const char* error, const char* mes
extern void vm_exit_during_cds_dumping(const char* error, const char* message = nullptr); extern void vm_exit_during_cds_dumping(const char* error, const char* message = nullptr);
// This is defined in linkType.cpp due to linking restraints
extern bool is_vm_statically_linked();
/** /**
* With the integration of the changes to handle the version string * With the integration of the changes to handle the version string
* as defined by JEP-223, most of the code related to handle the version * as defined by JEP-223, most of the code related to handle the version

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.
*
* 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 "precompiled.hpp"
#include "runtime/java.hpp"
// 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.
bool is_vm_statically_linked(void) {
#ifdef STATIC_BUILD
return true;
#else
return false;
#endif
}

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

View File

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

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -116,44 +116,41 @@ AWT_OnLoad(JavaVM *vm, void *reserved)
} }
jvm = vm; jvm = vm;
#ifndef STATIC_BUILD
/* Get address of this library and the directory containing it. */
dladdr((void *)AWT_OnLoad, &dlinfo);
realpath((char *)dlinfo.dli_fname, buf);
len = strlen(buf);
p = strrchr(buf, '/');
#endif
/* /*
* The code below is responsible for * The code below is responsible for
* loading appropriate awt library, i.e. libawt_xawt or libawt_headless * loading appropriate awt library, i.e. libawt_xawt or libawt_headless
*/ */
#ifdef MACOSX #ifdef MACOSX
tk = LWAWT_PATH; tk = LWAWT_PATH;
#else #else
tk = XAWT_PATH; tk = XAWT_PATH;
#endif
#ifndef MACOSX
if (AWTIsHeadless()) { if (AWTIsHeadless()) {
tk = HEADLESS_PATH; tk = HEADLESS_PATH;
} }
#endif #endif
#ifndef STATIC_BUILD if (!JVM_IsStaticallyLinked()) {
/* Calculate library name to load */ /* Get address of this library and the directory containing it. */
strncpy(p, tk, MAXPATHLEN-len-1); dladdr((void *)AWT_OnLoad, &dlinfo);
#endif realpath((char *)dlinfo.dli_fname, buf);
len = strlen(buf);
p = strrchr(buf, '/');
#ifndef STATIC_BUILD /* Calculate library name to load */
jstring jbuf = JNU_NewStringPlatform(env, buf); strncpy(p, tk, MAXPATHLEN-len-1);
CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");
JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load", jstring jbuf = JNU_NewStringPlatform(env, buf);
"(Ljava/lang/String;)V", CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");
jbuf); JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",
"(Ljava/lang/String;)V",
jbuf);
awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
}
awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
#endif
return JNI_VERSION_1_2; return JNI_VERSION_1_2;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,6 +23,7 @@
* questions. * questions.
*/ */
#include "jvm.h"
#include "util.h" #include "util.h"
#include "utf_util.h" #include "utf_util.h"
#include "transport.h" #include "transport.h"
@ -121,7 +122,11 @@ static void *
loadTransportLibrary(const char *libdir, const char *name) loadTransportLibrary(const char *libdir, const char *name)
{ {
char buf[MAXPATHLEN*2+100]; char buf[MAXPATHLEN*2+100];
#ifndef STATIC_BUILD
if (JVM_IsStaticallyLinked()) {
return (dbgsysLoadLibrary(NULL, buf, sizeof(buf)));
}
void *handle; void *handle;
char libname[MAXPATHLEN+2]; char libname[MAXPATHLEN+2];
const char *plibdir; const char *plibdir;
@ -145,9 +150,6 @@ loadTransportLibrary(const char *libdir, const char *name)
/* dlopen (unix) / LoadLibrary (windows) the transport library */ /* dlopen (unix) / LoadLibrary (windows) the transport library */
handle = dbgsysLoadLibrary(libname, buf, sizeof(buf)); handle = dbgsysLoadLibrary(libname, buf, sizeof(buf));
return handle; return handle;
#else
return (dbgsysLoadLibrary(NULL, buf, sizeof(buf)));
#endif
} }
/* /*