Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to runtime initialization instead of rerun #45611

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.quarkus.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;

public class NioSocketImplProcessor {

// Workaround till https://github.com/oracle/graal/pull/10431 gets merged and backported to all supported versions
@BuildStep
RuntimeInitializedClassBuildItem reinitializeClass() {
return new RuntimeInitializedClassBuildItem("sun.nio.ch.NioSocketImpl");
RuntimeReinitializedClassBuildItem reinitializeClass() {
return new RuntimeReinitializedClassBuildItem("sun.nio.ch.NioSocketImpl");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
/**
* A class that will be reinitialized at runtime in native mode. This will result in the static
* initializer running twice.
*
* @deprecated Starting with Mandrel/GraalVM 23.1 for JDK 21 this is functionally the same with
* {@link RuntimeInitializedClassBuildItem}.
*/
@Deprecated(since = "3.18")
public final class RuntimeReinitializedClassBuildItem extends MultiBuildItem {

private final String className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.UnsafeAccessedFieldBuildItem;
import io.quarkus.deployment.pkg.NativeConfig;
import io.quarkus.gizmo.AssignableResultHandle;
import io.quarkus.gizmo.BranchResult;
import io.quarkus.gizmo.BytecodeCreator;
import io.quarkus.gizmo.CatchBlockCreator;
Expand Down Expand Up @@ -176,29 +177,50 @@ public void write(String s, byte[] bytes) {
// hack in reinitialization of process info classes
if (!runtimeReinitializedClassBuildItems.isEmpty()) {
MethodCreator runtimeReinitializedClasses = file
.getMethodCreator("runtimeReinitializedClasses", void.class)
.getMethodCreator("runtimeReinitializedClasses", Class[].class)
.setModifiers(Modifier.PRIVATE | Modifier.STATIC);

ResultHandle thisClass = runtimeReinitializedClasses.loadClassFromTCCL(GRAAL_FEATURE);
ResultHandle cl = runtimeReinitializedClasses.invokeVirtualMethod(
ofMethod(Class.class, "getClassLoader", ClassLoader.class),
thisClass);
ResultHandle quarkus = runtimeReinitializedClasses.load("Quarkus");
ResultHandle imageSingleton = runtimeReinitializedClasses.invokeStaticMethod(IMAGE_SINGLETONS_LOOKUP,
runtimeReinitializedClasses.loadClassFromTCCL(RUNTIME_CLASS_INITIALIZATION_SUPPORT));
for (RuntimeReinitializedClassBuildItem runtimeReinitializedClass : runtimeReinitializedClassBuildItems) {
ResultHandle classesArray = runtimeReinitializedClasses.newArray(Class.class,
runtimeReinitializedClasses.load(runtimeReinitializedClassBuildItems.size()));
for (int i = 0; i < runtimeReinitializedClassBuildItems.size(); i++) {
TryBlock tc = runtimeReinitializedClasses.tryBlock();
ResultHandle clazz = tc.invokeStaticMethod(
ofMethod(Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class),
tc.load(runtimeReinitializedClass.getClassName()), tc.load(false), cl);
tc.invokeInterfaceMethod(RERUN_INITIALIZATION, imageSingleton, clazz, quarkus);

tc.load(runtimeReinitializedClassBuildItems.get(i).getClassName()), tc.load(false), cl);
tc.writeArrayValue(classesArray, i, clazz);
CatchBlockCreator cc = tc.addCatch(Throwable.class);
cc.invokeVirtualMethod(ofMethod(Throwable.class, "printStackTrace", void.class), cc.getCaughtException());
}
runtimeReinitializedClasses.returnVoid();
runtimeReinitializedClasses.returnValue(classesArray);

overallCatch.invokeStaticMethod(runtimeReinitializedClasses.getMethodDescriptor());
ResultHandle classes = overallCatch.invokeStaticMethod(runtimeReinitializedClasses.getMethodDescriptor());

ResultHandle graalVMVersion = overallCatch.invokeStaticMethod(GRAALVM_VERSION_GET_CURRENT);
BranchResult graalVm23_1Test = overallCatch
.ifGreaterEqualZero(overallCatch.invokeVirtualMethod(GRAALVM_VERSION_COMPARE_TO, graalVMVersion,
overallCatch.marshalAsArray(int.class, overallCatch.load(23), overallCatch.load(1))));
/* GraalVM >= 23.1 */
try (BytecodeCreator greaterEqual23_1 = graalVm23_1Test.trueBranch()) {
greaterEqual23_1.invokeStaticMethod(INITIALIZE_CLASSES_AT_RUN_TIME, classes);
}
/* GraalVM < 23.1 */
try (BytecodeCreator less23_1 = graalVm23_1Test.falseBranch()) {
ResultHandle quarkus = less23_1.load("Quarkus");
ResultHandle imageSingleton = less23_1.invokeStaticMethod(IMAGE_SINGLETONS_LOOKUP,
less23_1.loadClassFromTCCL(RUNTIME_CLASS_INITIALIZATION_SUPPORT));
ResultHandle arraySize = less23_1.arrayLength(classes);
AssignableResultHandle index = less23_1.createVariable(int.class);
less23_1.assign(index, less23_1.load(0));
try (BytecodeCreator loop = less23_1.whileLoop(c -> c.ifIntegerLessThan(index, arraySize)).block()) {
loop.invokeInterfaceMethod(RERUN_INITIALIZATION, imageSingleton, loop.readArrayValue(classes, index),
quarkus);
loop.assign(index, loop.increment(index));
}
}
}

// Ensure registration of fields being accessed through unsafe is done last to ensure that the class
Expand Down
Loading