Skip to content

Commit

Permalink
fix: refactor graalvm substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
scrocquesel committed Nov 9, 2024
1 parent 78ddc2a commit 5d177e7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Delete;
import com.oracle.svm.core.annotate.KeepOriginal;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;

import software.amazon.awssdk.checksums.SdkChecksum;
import software.amazon.awssdk.checksums.internal.CrcCloneOnMarkChecksum;
import software.amazon.awssdk.checksums.internal.CrcCombineOnMarkChecksum;
import software.amazon.awssdk.checksums.internal.SdkCrc32CChecksum;
import software.amazon.awssdk.crt.checksums.CRC64NVME;
import software.amazon.awssdk.http.auth.aws.crt.internal.signer.DefaultAwsCrtV4aHttpSigner;
import software.amazon.awssdk.http.auth.aws.internal.scheme.DefaultAwsV4aAuthScheme;
import software.amazon.awssdk.http.auth.aws.scheme.AwsV4aAuthScheme;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4aHttpSigner;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.identity.spi.IdentityProvider;
import software.amazon.awssdk.identity.spi.IdentityProviders;
import software.amazon.awssdk.utils.DependencyValidate;

public class CrtSubstitutions {
static final String SOFTWARE_AMAZON_AWSSDK_CRT_PACKAGE = "software.amazon.awssdk.crt";
Expand All @@ -34,6 +42,15 @@ public boolean getAsBoolean() {
}
}

static final class IsCrtPresent implements BooleanSupplier {
private static final IsCrtAbsent IS_CRT_ABSENT = new IsCrtAbsent();

@Override
public boolean getAsBoolean() {
return !IS_CRT_ABSENT.getAsBoolean();
}
}

static final class IsHttpAuthAwsCrtAbsent implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
Expand All @@ -43,43 +60,62 @@ public boolean getAsBoolean() {
}
}

@TargetClass(value = software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Crc32CChecksum.class, onlyWith = IsCrtAbsent.class)
static final class Target_SignerCrc32CChecksum {
/**
* aws sdk tries to create a java 9-based, then crt-based, then fallback to sdk-based checksum
* We can safely use the java 9-based in all cases (java 17 is the minimum since quarkus 3.7)
*/
@TargetClass(value = software.amazon.awssdk.checksums.internal.CrcChecksumProvider.class)
@Substitute()
static final class Target_CrcChecksumProvider {

@Alias
private Checksum crc32c;
private static final String CRT_MODULE = null;
@Alias
private static final String CRT_CRC64NVME_PATH = null;

@Substitute
public Target_SignerCrc32CChecksum() {
crc32c = software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkCrc32CChecksum.create();
public static SdkChecksum crc32cImplementation() {

return createJavaCrc32C();
}

/**
* optimize the original version which use reflection to create j.u.z.CRC32C
*/
@Substitute
private Checksum cloneChecksum(Checksum checksum) {
return (Checksum) ((software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkCrc32CChecksum) checksum)
.clone();
static SdkChecksum createJavaCrc32C() {
return new CrcCombineOnMarkChecksum(new java.util.zip.CRC32C(), SdkCrc32CChecksum::combine);
}
}

@TargetClass(value = software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Crc32Checksum.class, onlyWith = IsCrtAbsent.class)
static final class Target_SignerCrc32Checksum {

@Alias
private Checksum crc32;
@Delete
static SdkChecksum createCrtCrc32C(){
return null;
}

@Substitute
public Target_SignerCrc32Checksum() {
crc32 = software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkCrc32Checksum.create();
@Delete
static SdkChecksum createSdkBasedCrc32C(){
return null;
}

@Substitute
private Checksum cloneChecksum(Checksum checksum) {

return (Checksum) ((software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkCrc32Checksum) checksum)
.clone();
@TargetElement(onlyWith = IsCrtAbsent.class)
static SdkChecksum crc64NvmeCrtImplementation() {
throw new RuntimeException(
"Could not load " + CRT_CRC64NVME_PATH + ". Add dependency on '" + CRT_MODULE
+ "' module to enable CRC64NVME feature.");
}

@Substitute
@TargetElement(name = "crc64NvmeCrtImplementation", onlyWith = IsCrtPresent.class)
static SdkChecksum crc64NvmeCrtImplementationCrtPresent() {
return new CrcCloneOnMarkChecksum(new software.amazon.awssdk.crt.checksums.CRC64NVME());
}
}

/**
* aws sdk tries to create a crt-based, then sdk-based checksum
* Force the sdk-based when Crt is absent
*/
@TargetClass(value = software.amazon.awssdk.core.checksums.Crc32Checksum.class, onlyWith = IsCrtAbsent.class)
static final class Target_Crc32Checksum {
@Alias
Expand All @@ -97,6 +133,28 @@ private Checksum cloneChecksum(Checksum checksum) {
}
}

/**
* Crc64NvmeChecksum depends on CRC64NVME which require a static init of CRT
* Replace the implementation with a fail fast path
*/
@TargetClass(value = software.amazon.awssdk.checksums.internal.Crc64NvmeChecksum.class, onlyWith = IsCrtAbsent.class)
public final class Target_Crc64NvmeChecksum {
@Alias
private static final String CRT_CRC64NVME_PATH = "";
@Alias
private static final String CRT_MODULE = "";

@Substitute
public Target_Crc64NvmeChecksum() {
DependencyValidate.requireClass(CRT_CRC64NVME_PATH, CRT_MODULE, "CRC64NVME");
}

@Substitute
public Checksum cloneChecksum(Checksum checksum) {
throw new IllegalStateException("Unsupported checksum");
}
}

@TargetClass(value = DefaultAwsCrtV4aHttpSigner.class, onlyWith = IsHttpAuthAwsCrtAbsent.class)
@Delete
final class Delete_DefaultAwsCrtV4aHttpSigner {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ public class AwsCrtProcessor {
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void runtimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> runtimeInitializedClass) {
// CRT and all types that statically init CRT
// https://github.com/search?q=repo%3Aawslabs%2Faws-crt-java+static+%7B+new+CRT%28%29%3B+%7D&type=code
Stream.of(
"software.amazon.awssdk.crt.CRT",
"software.amazon.awssdk.crt.CrtRuntimeException",
"software.amazon.awssdk.crt.CrtResource",
"software.amazon.awssdk.crt.Log",
"software.amazon.awssdk.crt.io.Uri",
"software.amazon.awssdk.crt.checksums.CRC32",
"software.amazon.awssdk.crt.checksums.CRC32C")
"software.amazon.awssdk.crt.checksums.CRC32C",
"software.amazon.awssdk.crt.checksums.CRC64NVME",
"software.amazon.awssdk.crt.utils.StringUtils")
.map(RuntimeInitializedClassBuildItem::new)
.forEach(runtimeInitializedClass::produce);
}
Expand Down

0 comments on commit 5d177e7

Please sign in to comment.