Skip to content

Commit

Permalink
Reinitialized the com.google.protobuf.UnsafeUtil class at runtime
Browse files Browse the repository at this point in the history
Also fix the Unsafe accessor.
Fix quarkusio#30293.

This has been tested on GraalVM CE and EE.
  • Loading branch information
cescoffier committed Oct 24, 2023
1 parent 4fb9864 commit 03857b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -79,6 +82,20 @@ interface Target_io_grpc_ServiceProviders_PriorityAccessor<T> { // 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 {
}

0 comments on commit 03857b4

Please sign in to comment.