Skip to content

Commit ffdf96d

Browse files
committed
Initialize ImageReader during boot
This is to avoid being affected by a potential invalid java.home path Fixes: eclipse-openj9#21105 Signed-off-by: Gengchen Tuo <gengchen.tuo@ibm.com>
1 parent c305965 commit ffdf96d

File tree

5 files changed

+202
-2
lines changed

5 files changed

+202
-2
lines changed

jcl/src/java.base/share/classes/java/lang/J9VMInternals.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static void completeInitialization() {
173173
Runtime.getRuntime().addShutdownHook(new Thread(runnable, "CommonCleanerShutdown", true, false, false, null)); //$NON-NLS-1$
174174
}
175175
/*[ENDIF] JAVA_SPEC_VERSION >= 9 */
176-
/*[IF CRAC_SUPPORT]*/
176+
/*[IF CRAC_SUPPORT]*/
177177
if (openj9.internal.criu.InternalCRIUSupport.isCRaCSupportEnabled()) {
178178
// export java.base/jdk.crac unconditionally
179179
J9VMInternals.class.getModule().implAddExports("jdk.crac"); //$NON-NLS-1$
@@ -184,7 +184,17 @@ private static void completeInitialization() {
184184
om.get().implAddExports("jdk.crac.management"); //$NON-NLS-1$
185185
}
186186
}
187-
/*[ENDIF] CRAC_SUPPORT */
187+
/*[ENDIF] CRAC_SUPPORT */
188+
/*[IF (11 <= JAVA_SPEC_VERSION) & (JAVA_SPEC_VERSION <= 17)]*/
189+
/* ImageReader should be initialized before main() is called to
190+
* avoid being affected by a potential invalid java.home path.
191+
*/
192+
try {
193+
jdk.internal.jimage.ImageReaderFactory.getImageReader();
194+
} catch (java.io.UncheckedIOException e) {
195+
// Ignored deliberately.
196+
}
197+
/*[ENDIF] (11 <= JAVA_SPEC_VERSION) & (JAVA_SPEC_VERSION <= 17) */
188198
}
189199

190200
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0"?>
2+
3+
<!--
4+
Copyright IBM Corp. and others 2025
5+
6+
This program and the accompanying materials are made available under
7+
the terms of the Eclipse Public License 2.0 which accompanies this
8+
distribution and is available at https://www.eclipse.org/legal/epl-2.0/
9+
or the Apache License, Version 2.0 which accompanies this distribution and
10+
is available at https://www.apache.org/licenses/LICENSE-2.0.
11+
12+
This Source Code may also be made available under the following
13+
Secondary Licenses when the conditions for such availability set
14+
forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
15+
General Public License, version 2 with the GNU Classpath
16+
Exception [1] and GNU General Public License, version 2 with the
17+
OpenJDK Assembly Exception [2].
18+
19+
[1] https://www.gnu.org/software/classpath/license.html
20+
[2] https://openjdk.org/legal/assembly-exception.html
21+
22+
SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
23+
-->
24+
<project name="ImageReader" default="build" basedir=".">
25+
<taskdef resource="net/sf/antcontrib/antlib.xml" />
26+
<description>
27+
Build cmdLineTests_imageReaderInitializationTest
28+
</description>
29+
30+
<import file="${TEST_ROOT}/functional/cmdLineTests/buildTools.xml" />
31+
32+
<!-- set properties for this build -->
33+
<property name="DEST" value="${BUILD_ROOT}/functional/cmdLineTests/imageReaderInitializationTest" />
34+
<property name="src" location="src" />
35+
<property name="build" location="bin" />
36+
37+
<target name="init">
38+
<mkdir dir="${DEST}" />
39+
<mkdir dir="${build}" />
40+
</target>
41+
42+
<target name="compile" depends="init" description="Using java ${JDK_VERSION} to compile the source">
43+
<echo>Ant version is ${ant.version}</echo>
44+
<echo>============COMPILER SETTINGS============</echo>
45+
<echo>===fork: yes</echo>
46+
<echo>===executable: ${compiler.javac}</echo>
47+
<echo>===debug: on</echo>
48+
<echo>===destdir: ${DEST}</echo>
49+
<javac srcdir="${src}" destdir="${build}" debug="true" fork="true" executable="${compiler.javac}" includeAntRuntime="false" encoding="ISO-8859-1">
50+
<src path="${src}" />
51+
</javac>
52+
</target>
53+
54+
<target name="dist" depends="compile" description="generate the distribution">
55+
<jar jarfile="${DEST}/imageReaderInitializationTest.jar" filesonly="true">
56+
<fileset dir="${build}" />
57+
<fileset dir="${src}" />
58+
</jar>
59+
<copy todir="${DEST}">
60+
<fileset dir="${src}/../">
61+
<include name="**/*.mk" />
62+
<include name="**/*.xml" />
63+
</fileset>
64+
</copy>
65+
</target>
66+
67+
<target name="clean" depends="dist" description="clean up">
68+
<!-- Delete the ${build} directory trees -->
69+
<delete dir="${build}" />
70+
</target>
71+
72+
<target name="build" depends="buildCmdLineTestTools">
73+
<antcall target="clean" inheritall="true" />
74+
</target>
75+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
2+
3+
<!--
4+
Copyright IBM Corp. and others 2025
5+
6+
This program and the accompanying materials are made available under
7+
the terms of the Eclipse Public License 2.0 which accompanies this
8+
distribution and is available at https://www.eclipse.org/legal/epl-2.0/
9+
or the Apache License, Version 2.0 which accompanies this distribution and
10+
is available at https://www.apache.org/licenses/LICENSE-2.0.
11+
12+
This Source Code may also be made available under the following
13+
Secondary Licenses when the conditions for such availability set
14+
forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
15+
General Public License, version 2 with the GNU Classpath
16+
Exception [1] and GNU General Public License, version 2 with the
17+
OpenJDK Assembly Exception [2].
18+
19+
[1] https://www.gnu.org/software/classpath/license.html
20+
[2] https://openjdk.org/legal/assembly-exception.html
21+
22+
SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
23+
-->
24+
25+
<!DOCTYPE suite SYSTEM "cmdlinetester.dtd">
26+
27+
<suite id="ImageReader Initialization Test" timeout="60">
28+
<test id="ImageReader Initialization Test">
29+
<command>$EXE$ -cp $RESJAR$ org.openj9.test.ImageReaderInitializationTest</command>
30+
<output type="success" caseSensitive="yes" regex="no">IllegalStateException</output>
31+
<output type="failure" caseSensitive="yes" regex="no">ExceptionInInitializerError</output>
32+
</test>
33+
</suite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
Copyright IBM Corp. and others 2025
4+
5+
This program and the accompanying materials are made available under
6+
the terms of the Eclipse Public License 2.0 which accompanies this
7+
distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8+
or the Apache License, Version 2.0 which accompanies this distribution and
9+
is available at https://www.apache.org/licenses/LICENSE-2.0.
10+
11+
This Source Code may also be made available under the following
12+
Secondary Licenses when the conditions for such availability set
13+
forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14+
General Public License, version 2 with the GNU Classpath
15+
Exception [1] and GNU General Public License, version 2 with the
16+
OpenJDK Assembly Exception [2].
17+
18+
[1] https://www.gnu.org/software/classpath/license.html
19+
[2] https://openjdk.org/legal/assembly-exception.html
20+
21+
SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
22+
-->
23+
<playlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../TKG/playlist.xsd">
24+
<include>../variables.mk</include>
25+
<test>
26+
<testCaseName>cmdLineTester_imageReaderInitializationTest</testCaseName>
27+
<variations>
28+
<variation>NoOptions</variation>
29+
</variations>
30+
<command>
31+
$(JAVA_COMMAND) $(CMDLINETESTER_JVM_OPTIONS) -Xdump \
32+
-DEXE=$(SQ)$(JAVA_COMMAND) $(JVM_OPTIONS)$(SQ) \
33+
-DRESJAR=$(Q)$(TEST_RESROOT)$(D)imageReaderInitializationTest.jar$(Q) \
34+
-jar $(CMDLINETESTER_JAR) -config $(Q)$(TEST_RESROOT)$(D)imageReaderInitializationTest.xml$(Q) \
35+
-explainExcludes -xids all,$(PLATFORM),$(VARIATION), -nonZeroExitWhenError; \
36+
${TEST_STATUS}
37+
</command>
38+
<levels>
39+
<level>sanity</level>
40+
</levels>
41+
<groups>
42+
<group>functional</group>
43+
</groups>
44+
</test>
45+
</playlist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright IBM Corp. and others 2025
3+
*
4+
* This program and the accompanying materials are made available under
5+
* the terms of the Eclipse Public License 2.0 which accompanies this
6+
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7+
* or the Apache License, Version 2.0 which accompanies this distribution and
8+
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9+
*
10+
* This Source Code may also be made available under the following
11+
* Secondary Licenses when the conditions for such availability set
12+
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13+
* General Public License, version 2 with the GNU Classpath
14+
* Exception [1] and GNU General Public License, version 2 with the
15+
* OpenJDK Assembly Exception [2].
16+
*
17+
* [1] https://www.gnu.org/software/classpath/license.html
18+
* [2] https://openjdk.org/legal/assembly-exception.html
19+
*
20+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
21+
*/
22+
package org.openj9.test;
23+
24+
/*
25+
* This test is to make sure that ImageReader is initialized during boot.
26+
* So in case an invalid java.home is set and an exception is thrown in
27+
* the code, the exception can be correctly thrown.
28+
*/
29+
public class ImageReaderInitializationTest {
30+
public static void main(String[] args) {
31+
System.setProperty("java.home", "/invalid/path/to/java");
32+
String javaHome = System.getProperty("java.home");
33+
if (javaHome.equals("/invalid/path/to/java")) {
34+
throw new IllegalStateException("Invalid Java home set, throwing expected IllegalStateException");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)