From 03857b461147d3bf244f906059ae43cae2e01e1b Mon Sep 17 00:00:00 2001 From: Clement Escoffier Date: Mon, 23 Oct 2023 17:51:53 +0200 Subject: [PATCH] Reinitialized the com.google.protobuf.UnsafeUtil class at runtime Also fix the Unsafe accessor. Fix https://github.com/quarkusio/quarkus/issues/30293. This has been tested on GraalVM CE and EE. --- .../common/deployment/GrpcCommonProcessor.java | 3 ++- .../common/runtime/graal/GrpcSubstitutions.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java b/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java index 477ad5690a4a6..82d43e7c7919f 100644 --- a/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java +++ b/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java @@ -75,7 +75,8 @@ NativeImageConfigBuildItem nativeImageConfiguration() { .addRuntimeInitializedClass("io.grpc.netty.Utils") .addRuntimeInitializedClass("io.grpc.netty.NettyServerBuilder") .addRuntimeInitializedClass("io.grpc.netty.NettyChannelBuilder") - .addRuntimeInitializedClass("io.grpc.internal.RetriableStream"); + .addRuntimeInitializedClass("io.grpc.internal.RetriableStream") + .addRuntimeReinitializedClass("com.google.protobuf.UnsafeUtil"); return builder.build(); } diff --git a/extensions/grpc-common/runtime/src/main/java/io/quarkus/grpc/common/runtime/graal/GrpcSubstitutions.java b/extensions/grpc-common/runtime/src/main/java/io/quarkus/grpc/common/runtime/graal/GrpcSubstitutions.java index 349eb09e264b8..1cdaed9d44fc5 100644 --- a/extensions/grpc-common/runtime/src/main/java/io/quarkus/grpc/common/runtime/graal/GrpcSubstitutions.java +++ b/extensions/grpc-common/runtime/src/main/java/io/quarkus/grpc/common/runtime/graal/GrpcSubstitutions.java @@ -2,6 +2,7 @@ import static io.grpc.InternalServiceProviders.getCandidatesViaHardCoded; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -12,6 +13,8 @@ import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; +import sun.misc.Unsafe; + @SuppressWarnings("unused") @TargetClass(className = "io.grpc.ServiceProviders") final class Target_io_grpc_ServiceProviders { // NOSONAR @@ -79,6 +82,20 @@ interface Target_io_grpc_ServiceProviders_PriorityAccessor { // NOSONAR int getPriority(T provider); } +@TargetClass(className = "com.google.protobuf.UnsafeUtil") +final class Target_com_google_protobuf_UnsafeUtil { + @Substitute + static sun.misc.Unsafe getUnsafe() { + try { + Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + return (Unsafe) theUnsafe.get(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} + @SuppressWarnings("unused") class GrpcSubstitutions { }