Skip to content

Commit

Permalink
Reinitialize shaded com.google.protobuf.UnsafeUtil class
Browse files Browse the repository at this point in the history
Adaptation of #36642 for the
shaded `com.google.protobuf.UnsafeUtil` class in kafka-clients.

Fixes: #40100
  • Loading branch information
zakkak committed May 17, 2024
1 parent 455ec6c commit dcb3411
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -482,15 +482,16 @@ UnremovableBeanBuildItem ensureJsonParserAvailable() {
}

@BuildStep
public void registerRuntimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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 {

}

0 comments on commit dcb3411

Please sign in to comment.