From dcb3411793171a1f938369ec5256c557e5629394 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Fri, 17 May 2024 15:32:01 +0300 Subject: [PATCH] Reinitialize shaded `com.google.protobuf.UnsafeUtil` class Adaptation of https://github.com/quarkusio/quarkus/pull/36642 for the shaded `com.google.protobuf.UnsafeUtil` class in kafka-clients. Fixes: https://github.com/quarkusio/quarkus/issues/40100 --- .../client/deployment/KafkaProcessor.java | 21 ++++++++++--------- .../kafka/graal/KafkaSubstitutions.java | 17 +++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/extensions/kafka-client/deployment/src/main/java/io/quarkus/kafka/client/deployment/KafkaProcessor.java b/extensions/kafka-client/deployment/src/main/java/io/quarkus/kafka/client/deployment/KafkaProcessor.java index 393c85fdf3cd2..41f3415a761c6 100644 --- a/extensions/kafka-client/deployment/src/main/java/io/quarkus/kafka/client/deployment/KafkaProcessor.java +++ b/extensions/kafka-client/deployment/src/main/java/io/quarkus/kafka/client/deployment/KafkaProcessor.java @@ -76,12 +76,12 @@ import io.quarkus.deployment.builditem.LogCategoryBuildItem; import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem; import io.quarkus.deployment.builditem.RuntimeConfigSetupCompleteBuildItem; +import io.quarkus.deployment.builditem.nativeimage.NativeImageConfigBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageSecurityProviderBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassConditionBuildItem; -import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; import io.quarkus.deployment.logging.LogCleanupFilterBuildItem; import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem; @@ -482,15 +482,16 @@ UnremovableBeanBuildItem ensureJsonParserAvailable() { } @BuildStep - public void registerRuntimeInitializedClasses(BuildProducer producer) { - // Classes using java.util.Random, which need to be runtime initialized - producer.produce( - new RuntimeInitializedClassBuildItem("org.apache.kafka.common.security.authenticator.SaslClientAuthenticator")); - producer.produce(new RuntimeInitializedClassBuildItem( - "org.apache.kafka.common.security.oauthbearer.internals.expiring.ExpiringCredentialRefreshingLogin")); - // VerificationKeyResolver is value on static map in OAuthBearerValidatorCallbackHandler - producer.produce(new RuntimeInitializedClassBuildItem( - "org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallbackHandler")); + NativeImageConfigBuildItem nativeImageConfiguration() { + NativeImageConfigBuildItem.Builder builder = NativeImageConfigBuildItem.builder() + // Classes using java.util.Random, which need to be runtime initialized + .addRuntimeInitializedClass("org.apache.kafka.common.security.authenticator.SaslClientAuthenticator") + .addRuntimeInitializedClass( + "org.apache.kafka.common.security.oauthbearer.internals.expiring.ExpiringCredentialRefreshingLogin") + // VerificationKeyResolver is value on static map in OAuthBearerValidatorCallbackHandler + .addRuntimeInitializedClass("org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallbackHandler") + .addRuntimeReinitializedClass("org.apache.kafka.shaded.com.google.protobuf.UnsafeUtil"); + return builder.build(); } @BuildStep diff --git a/extensions/kafka-client/runtime/src/main/java/io/smallrye/reactive/kafka/graal/KafkaSubstitutions.java b/extensions/kafka-client/runtime/src/main/java/io/smallrye/reactive/kafka/graal/KafkaSubstitutions.java index 852c6ca247a6e..8325fdd9c472f 100644 --- a/extensions/kafka-client/runtime/src/main/java/io/smallrye/reactive/kafka/graal/KafkaSubstitutions.java +++ b/extensions/kafka-client/runtime/src/main/java/io/smallrye/reactive/kafka/graal/KafkaSubstitutions.java @@ -1,10 +1,13 @@ package io.smallrye.reactive.kafka.graal; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; +import sun.misc.Unsafe; + @TargetClass(className = "org.apache.kafka.common.network.SaslChannelBuilder") final class Target_org_apache_kafka_common_network_SaslChannelBuilder { @@ -17,6 +20,20 @@ private static String defaultKerberosRealm() throws ClassNotFoundException, NoSu } +@TargetClass(className = "org.apache.kafka.shaded.com.google.protobuf.UnsafeUtil") +final class Target_org_apache_kafka_shaded_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); + } + } +} + class KafkaSubstitutions { }