Upgrade to GraalVM 22.3 and introduce PreComputeFieldFeature

This new GraalVM feature replaces ConstantFieldFeature and
introduces various enhancements:
 - Leverage the new FieldValueTransformer API
 - Use GraalVM 22.3 graal-sdk dependency instead of svm one
 - Avoid using internal GraalVM APIs
 - No need to configure JPMS exports
 - Directly integrated in spring-core module
 - Simplified build configuration

Closes gh-29081
Closes gh-29080
Closes gh-29089
This commit is contained in:
Sébastien Deleuze 2022-10-16 18:15:01 +02:00
parent ba99672fd6
commit 0889e47608
16 changed files with 111 additions and 335 deletions

2
.gitignore vendored
View File

@ -21,8 +21,6 @@ derby.log
/build /build
buildSrc/build buildSrc/build
/spring-*/build /spring-*/build
/spring-core/graalvm/build
/spring-core/kotlin-coroutines/build
/framework-bom/build /framework-bom/build
/integration-tests/build /integration-tests/build
/src/asciidoc/build /src/asciidoc/build

View File

@ -120,7 +120,7 @@ dependencies {
api("org.glassfish.external:opendmk_jmxremote_optional_jar:1.0-b01-ea") api("org.glassfish.external:opendmk_jmxremote_optional_jar:1.0-b01-ea")
api("org.glassfish.tyrus:tyrus-container-servlet:2.0.1") api("org.glassfish.tyrus:tyrus-container-servlet:2.0.1")
api("org.glassfish:jakarta.el:4.0.2") api("org.glassfish:jakarta.el:4.0.2")
api("org.graalvm.nativeimage:svm:22.1.0.1") api("org.graalvm.sdk:graal-sdk:22.3.0")
api("org.hamcrest:hamcrest:2.2") api("org.hamcrest:hamcrest:2.2")
api("org.hibernate:hibernate-core-jakarta:5.6.12.Final") api("org.hibernate:hibernate-core-jakarta:5.6.12.Final")
api("org.hibernate:hibernate-validator:7.0.5.Final") api("org.hibernate:hibernate-validator:7.0.5.Final")

View File

@ -18,8 +18,6 @@ include "spring-context"
include "spring-context-indexer" include "spring-context-indexer"
include "spring-context-support" include "spring-context-support"
include "spring-core" include "spring-core"
include "graalvm-feature"
project(':graalvm-feature').projectDir = file('spring-core/graalvm')
include "spring-core-test" include "spring-core-test"
include "spring-expression" include "spring-expression"
include "spring-instrument" include "spring-instrument"

View File

@ -1,40 +0,0 @@
description = "Spring Core GraalVM feature"
configurations {
classesOnlyElements {
canBeConsumed = true
canBeResolved = false
}
}
artifacts {
classesOnlyElements(compileJava.destinationDirectory)
classesOnlyElements(sourceSets.main.resources.srcDirs)
}
tasks.withType(JavaCompile) {
options.compilerArgs += [
"--add-modules",
"jdk.internal.vm.ci",
"--add-exports",
"jdk.internal.vm.ci/jdk.vm.ci.meta=ALL-UNNAMED"
]
}
tasks.withType(Javadoc) {
enabled = false
}
eclipse.classpath.file {
whenMerged {
entries.find{ it.path ==~ '.*JRE_CONTAINER.*' }.each {
it.entryAttributes['module'] = true
it.entryAttributes['add-exports'] = 'jdk.internal.vm.ci/jdk.vm.ci.meta=ALL-UNNAMED'
it.entryAttributes['limit-modules'] = 'java.se,jdk.accessibility,jdk.attach,jdk.compiler,jdk.httpserver,jdk.jartool,jdk.jconsole,jdk.jdi,jdk.management,jdk.sctp,jdk.security.auth,jdk.security.jgss,jdk.unsupported,jdk.dynalink,jdk.incubator.foreign,jdk.incubator.vector,jdk.javadoc,jdk.jfr,jdk.jshell,jdk.management.jfr,jdk.net,jdk.nio.mapmode,jdk.unsupported.desktop,jdk.jsobject,jdk.xml.dom,jdk.internal.vm.ci'
}
}
}
dependencies {
compileOnly("org.graalvm.nativeimage:svm")
}

View File

@ -1,51 +0,0 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.graalvm;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.hosted.FeatureImpl.DuringSetupAccessImpl;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.nativeimage.hosted.Feature;
/**
* GraalVM {@link Feature} that substitutes field values that match a certain pattern
* with constants without causing build-time initialization.
*
* @author Phillip Webb
* @author Sebastien Deleuze
* @since 6.0
*/
@AutomaticFeature
class ConstantFieldFeature implements Feature {
@Override
public void duringSetup(DuringSetupAccess access) {
duringSetup((DuringSetupAccessImpl) access);
}
private void duringSetup(DuringSetupAccessImpl access) {
DebugContext debug = access.getDebugContext();
try (DebugContext.Scope scope = debug.scope("ConstantFieldFeature.duringSetup")) {
debug.log("Installing constant field substitution processor : " + scope);
ClassLoader classLoader = ConstantFieldFeature.class.getClassLoader();
ConstantFieldSubstitutionProcessor substitutionProcessor =
new ConstantFieldSubstitutionProcessor(debug, classLoader);
access.registerSubstitutionProcessor(substitutionProcessor);
}
}
}

View File

@ -1,106 +0,0 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.graalvm;
import java.lang.reflect.Field;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;
import com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor;
import com.oracle.svm.core.meta.SubstrateObjectConstant;
import com.oracle.svm.core.util.UserError;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaType;
import org.graalvm.compiler.debug.DebugContext;
/**
* {@link SubstitutionProcessor} to compute at build time the value of the
* boolean static fields identified by {@link #patterns} in order to allow
* efficient code shrinking without using class build time initialization.
*
* @author Phillip Webb
* @author Sebastien Deleuze
* @since 6.0
*/
class ConstantFieldSubstitutionProcessor extends SubstitutionProcessor {
// Later should be an explicit signal, like an annotation or even a Java keyword
private static Pattern[] patterns = {
Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#imageCode")),
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*Present"),
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*PRESENT"),
Pattern.compile(Pattern.quote("reactor.") + ".*#.*Available")
};
private final ThrowawayClassLoader throwawayClassLoader;
private ConcurrentMap<String, ResolvedJavaField> cache = new ConcurrentHashMap<>();
private Set<String> ignoredFields = ConcurrentHashMap.newKeySet();
ConstantFieldSubstitutionProcessor(DebugContext debug, ClassLoader applicationClassLoader) {
this.throwawayClassLoader = new ThrowawayClassLoader(applicationClassLoader);
}
@Override
public ResolvedJavaField lookup(ResolvedJavaField field) {
ResolvedJavaType declaringClass = field.getDeclaringClass();
if (field.getType().getJavaKind() == JavaKind.Boolean && field.isStatic()) {
String fieldIdentifier = declaringClass.toJavaName() + "#" + field.getName();
for (Pattern pattern : patterns) {
if (pattern.matcher(fieldIdentifier).matches() && !this.ignoredFields.contains(fieldIdentifier)) {
try {
return this.cache.computeIfAbsent(fieldIdentifier, key -> {
JavaConstant constant = lookupConstant(declaringClass.toJavaName(), field.getName());
// TODO Use proper logging only when --verbose is specified when https://github.com/oracle/graal/issues/4669 will be fixed
ConstantReadableJavaField readableJavaField = new ConstantReadableJavaField(field, constant);
System.out.println("Field " + fieldIdentifier + " set to " + constant.toValueString() + " at build time");
return readableJavaField;
});
}
catch (Throwable ex) {
this.ignoredFields.add(fieldIdentifier);
System.out.println("Processing of field " + fieldIdentifier + " skipped due the following error : " + ex.getMessage());
}
}
}
}
return super.lookup(field);
}
private JavaConstant lookupConstant(String className, String fieldName) {
try {
Class<?> throwawayClass = this.throwawayClassLoader.loadClass(className);
Field field = throwawayClass.getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(null);
if (!(value instanceof Boolean)) {
throw UserError.abort("Unable to get the value of " + className + "." + fieldName);
}
return SubstrateObjectConstant.forBoxedValue(JavaKind.Boolean, value);
}
catch (Exception ex) {
throw new IllegalStateException("Unable to read value from " + className + "." + fieldName, ex);
}
}
}

View File

@ -1,117 +0,0 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.graalvm;
import java.lang.annotation.Annotation;
import com.oracle.graal.pointsto.infrastructure.WrappedElement;
import com.oracle.svm.core.meta.ReadableJavaField;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaType;
/**
* {@link ReadableJavaField} for a constant value.
*
* @author Phillip Webb
* @since 6.0
*/
class ConstantReadableJavaField implements ReadableJavaField, WrappedElement {
private final ResolvedJavaField original;
private final JavaConstant constant;
public ConstantReadableJavaField(ResolvedJavaField original, JavaConstant constant) {
this.original = original;
this.constant = constant;
}
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return this.original.getAnnotation(annotationClass);
}
@Override
public Annotation[] getAnnotations() {
return this.original.getAnnotations();
}
@Override
public Annotation[] getDeclaredAnnotations() {
return this.original.getDeclaredAnnotations();
}
@Override
public ResolvedJavaType getDeclaringClass() {
return this.original.getDeclaringClass();
}
@Override
public int getModifiers() {
return this.original.getModifiers();
}
@Override
public String getName() {
return this.original.getName();
}
@Override
public int getOffset() {
return this.original.getOffset();
}
@Override
public JavaType getType() {
return this.original.getType();
}
@Override
public boolean isInternal() {
return this.original.isInternal();
}
@Override
public boolean isSynthetic() {
return this.original.isSynthetic();
}
@Override
public JavaConstant readValue(MetaAccessProvider metaAccess, JavaConstant receiver) {
return this.constant;
}
@Override
public boolean allowConstantFolding() {
return true;
}
@Override
public boolean injectFinalForRuntimeCompilation() {
return true;
}
@Override
public Object getWrapped() {
return this.original;
}
}

View File

@ -1,5 +0,0 @@
/**
* GraalVM implementation specific support which might change at any time, so not considered part of Spring Framework public API.
*/
package org.springframework.aot.graalvm;

View File

@ -58,11 +58,11 @@ task objenesisSourceJar(type: Jar) {
dependencies { dependencies {
javapoet("com.squareup:javapoet:${javapoetVersion}@jar") javapoet("com.squareup:javapoet:${javapoetVersion}@jar")
objenesis("org.objenesis:objenesis:${objenesisVersion}@jar") objenesis("org.objenesis:objenesis:${objenesisVersion}@jar")
graalvm(project(path: ":graalvm-feature", configuration: 'classesOnlyElements'))
api(files(javapoetRepackJar)) api(files(javapoetRepackJar))
api(files(objenesisRepackJar)) api(files(objenesisRepackJar))
api(project(":spring-jcl")) api(project(":spring-jcl"))
compileOnly("io.projectreactor.tools:blockhound") compileOnly("io.projectreactor.tools:blockhound")
compileOnly("org.graalvm.sdk:graal-sdk")
optional("net.sf.jopt-simple:jopt-simple") optional("net.sf.jopt-simple:jopt-simple")
optional("org.aspectj:aspectjweaver") optional("org.aspectj:aspectjweaver")
optional("org.jetbrains.kotlin:kotlin-reflect") optional("org.jetbrains.kotlin:kotlin-reflect")
@ -108,8 +108,6 @@ jar {
from(zipTree(objenesisRepackJar.archivePath)) { from(zipTree(objenesisRepackJar.archivePath)) {
include "org/springframework/objenesis/**" include "org/springframework/objenesis/**"
} }
from configurations.graalvm
} }
test { test {

View File

@ -0,0 +1,86 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.nativex.feature;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.regex.Pattern;
import org.graalvm.nativeimage.hosted.Feature;
/**
* GraalVM {@link Feature} that substitutes boolean field values that match a certain pattern
* with values pre-computed AOT without causing class build-time initialization.
*
* @author Sebastien Deleuze
* @author Phillip Webb
* @since 6.0
*/
class PreComputeFieldFeature implements Feature {
private static Pattern[] patterns = {
Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#imageCode")),
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*Present"),
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*PRESENT"),
Pattern.compile(Pattern.quote("reactor.") + ".*#.*Available")
};
private final ThrowawayClassLoader throwawayClassLoader = new ThrowawayClassLoader(PreComputeFieldFeature.class.getClassLoader());
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
access.registerSubtypeReachabilityHandler(this::iterateFields, Object.class);
}
/* This method is invoked for every type that is reachable. */
private void iterateFields(DuringAnalysisAccess access, Class<?> subtype) {
try {
for (Field field : subtype.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers) || field.isEnumConstant() ||
(field.getType() != boolean.class && field.getType() != Boolean.class)) {
continue;
}
String fieldIdentifier = field.getDeclaringClass().getName() + "#" + field.getName();
for (Pattern pattern : patterns) {
if (pattern.matcher(fieldIdentifier).matches()) {
try {
Object fieldValue = provideFieldValue(field);
access.registerFieldValueTransformer(field, (receiver, originalValue) -> fieldValue);
System.out.println("Field " + fieldIdentifier + " set to " + fieldValue + " at build time");
}
catch (Throwable ex) {
System.out.println("Processing of field " + fieldIdentifier + " skipped due the following error : " + ex.getMessage());
}
}
}
}
}
catch (NoClassDefFoundError ex) {
// Skip classes that have not all their field types in the classpath
}
}
/* This method is invoked when the field value is written to the image heap or the field is constant folded. */
private Object provideFieldValue(Field field) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class<?> throwawayClass = this.throwawayClassLoader.loadClass(field.getDeclaringClass().getName());
Field throwawayField = throwawayClass.getDeclaredField(field.getName());
throwawayField.setAccessible(true);
return throwawayField.get(null);
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.aot.graalvm; package org.springframework.aot.nativex.feature;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;

View File

@ -0,0 +1,9 @@
/**
* GraalVM native image features, not part of Spring Framework public API.
*/
@NonNullApi
@NonNullFields
package org.springframework.aot.nativex.feature;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.aot.graalvm; package org.springframework.aot.nativex.substitution;
import com.oracle.svm.core.annotate.Alias; import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.TargetClass; import com.oracle.svm.core.annotate.TargetClass;

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.aot.graalvm; package org.springframework.aot.nativex.substitution;
import java.beans.Customizer; import java.beans.Customizer;

View File

@ -0,0 +1,9 @@
/**
* GraalVM native image substitutions, not part of Spring Framework public API.
*/
@NonNullApi
@NonNullFields
package org.springframework.aot.nativex.substitution;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@ -1,5 +1,2 @@
Args = --initialize-at-build-time=org.springframework.aot.graalvm.ThrowawayClassLoader \ Args = --initialize-at-build-time=org.springframework.aot.nativex.feature.ThrowawayClassLoader \
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted=ALL-UNNAMED \ --features=org.springframework.aot.nativex.feature.PreComputeFieldFeature
--add-exports jdk.internal.vm.compiler/org.graalvm.compiler.debug=ALL-UNNAMED \
--add-exports jdk.internal.vm.ci/jdk.vm.ci.meta=ALL-UNNAMED \
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.meta=ALL-UNNAMED