Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ThrowMissingRegistrationErrors with GraalVM >= 23.1 #36378

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,034 changes: 80 additions & 954 deletions .github/workflows/ci-actions-incremental.yml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ public NativeImageInvokerInfo build() {
// Enabled by default in GraalVM 24.0.0.
nativeImageArgs.add("--strict-image-heap");
}
// This feature will become available as non-experimental in the next release (GraalVM for Java 22,
// GraalVM 24.0.0), and will be promoted to the default behavior as the community adopts it.
addExperimentalVMOption(nativeImageArgs, "-H:ThrowMissingRegistrationErrors=");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid this change could break downstream code. I would put a package filter that handles only Quarkus classes and Quarkus-managed libraries in code that goes to users. Then, we can ask the users in the next release to add --throw-missing-registration-errors= to their build.

We can use the unfiltered version for internal tests (ideally used with our dev builds). In tests, it is ideally used in combination with -H:MissingRegistrationReportingMode=Exit to make sure metadata is never missed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input @vjovanov.

FYI when running without -H:MissingRegistrationReportingMode=Exit the Quarkus binary exits with error code 1 without printing anything.

When adding -H:MissingRegistrationReportingMode=Exit I get to see the exception:

org.graalvm.nativeimage.MissingReflectionRegistrationError: The program tried to reflectively access method sun.security.provider.NativePRNG#<init>(java.security.SecureRandomParameters) without it being registered for runtime reflection. Add sun.security.provider.NativePRNG#<init>(java.security.SecureRandomParameters) to the reflection metadata to solve this problem. See https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection for help.

My understanding is that Quarkus catches the exception at some point which results in the error message not being shown, yet it still fails.

Copy link

@vjovanov vjovanov Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that Quarkus catches the exception at some point which results in the error message not being shown, yet it still fails.

I would say the same. That is why we introduced -H:MissingRegistrationReportingMode=Exit.
To speed up the migration you can run the programs with -H:MissingRegistrationReportingMode=Warn and it will only print the missing elements.

}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import io.quarkus.runtime.configuration.ConfigDiagnostic;
import io.quarkus.runtime.configuration.ConfigRecorder;
import io.quarkus.runtime.configuration.DisableableConfigSource;
import io.quarkus.runtime.configuration.NativeConfigBuilder;
import io.quarkus.runtime.configuration.QuarkusConfigValue;
import io.quarkus.runtime.configuration.RuntimeConfigBuilder;
import io.quarkus.runtime.configuration.RuntimeOverrideConfigSource;
Expand Down Expand Up @@ -168,6 +169,14 @@ void buildTimeRunTimeConfig(
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(builderClassName));
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void nativeConfig(
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(NativeConfigBuilder.class.getName()));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(NativeConfigBuilder.class.getName()));
}

@BuildStep(onlyIfNot = { IsNormal.class }) // for dev or test
void runtimeOverrideConfig(
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.runtime.configuration;

import static io.smallrye.config.PropertiesConfigSourceLoader.inFileSystem;

import java.nio.file.Paths;

import io.smallrye.config.SmallRyeConfigBuilder;

/**
* Do not register classpath resources lookup in native mode to avoid missing resources registration errors, which
* became a strict check during native image execution.
* <p>
* Configuration coming from classpath resources is recoded during build time in a low ordinal source, so the
* configuration will still be available. If the users change the ordinals of the sources in runtime, it may
* cause unexpected values to be returned by the config, but this has always been the case. The classpath configuration
* resources were never registered in the native image.
*/
public class NativeConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
builder
.setAddDefaultSources(false)
.setAddSystemSources(true)
.setAddPropertiesSources(false);

builder.withSources(inFileSystem(
Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(), 260,
builder.getClassLoader()));

return builder;
}

@Override
public int priority() {
return Integer.MIN_VALUE + 100;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package io.quarkus.runtime.configuration;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;
import org.graalvm.nativeimage.MissingReflectionRegistrationError;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
Expand All @@ -9,9 +17,11 @@
import com.oracle.svm.core.annotate.TargetElement;

import io.smallrye.common.constraint.Assert;
import io.smallrye.config.AbstractLocationConfigSourceLoader;
import io.smallrye.config.ConfigMappingInterface;
import io.smallrye.config.ConfigMappingLoader;
import io.smallrye.config.ConfigMappingMetadata;
import io.smallrye.config._private.ConfigMessages;

final class Substitutions {
@TargetClass(ConfigProviderResolver.class)
Expand Down Expand Up @@ -97,4 +107,52 @@ public byte[] getClassBytes() {
return null;
}
}

@TargetClass(value = AbstractLocationConfigSourceLoader.class)
static final class Target_AbstractLocationConfigSourceLoader {
@Alias
protected native List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader);

@Alias
protected native List<ConfigSource> tryFileSystem(final URI uri, final int ordinal);

@Alias
protected native List<ConfigSource> tryJar(final URI uri, final int ordinal);

@Alias
protected native List<ConfigSource> tryHttpResource(final URI uri, final int ordinal);

@Alias
private static Converter<URI> URI_CONVERTER = null;

@Substitute
protected List<ConfigSource> loadConfigSources(final String[] locations, final int ordinal,
final ClassLoader classLoader) {
if (locations == null || locations.length == 0) {
return Collections.emptyList();
}

final List<ConfigSource> configSources = new ArrayList<>();
for (String location : locations) {
final URI uri = URI_CONVERTER.convert(location);
if (uri.getScheme() == null) {
configSources.addAll(tryFileSystem(uri, ordinal));
try {
configSources.addAll(tryClassPath(uri, ordinal, classLoader));
} catch (MissingReflectionRegistrationError e) {
// ignore
}
} else if (uri.getScheme().equals("file")) {
configSources.addAll(tryFileSystem(uri, ordinal));
} else if (uri.getScheme().equals("jar")) {
configSources.addAll(tryJar(uri, ordinal));
} else if (uri.getScheme().startsWith("http")) {
configSources.addAll(tryHttpResource(uri, ordinal));
} else {
throw ConfigMessages.msg.schemeNotSupported(uri.getScheme());
}
}
return configSources;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
public class SystemOnlySourcesConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
return builder.setAddDefaultSources(false).addSystemSources();
return builder.setAddDefaultSources(false)
.setAddPropertiesSources(false)
.addSystemSources();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.config.yaml.runtime.YamlConfigBuilder;
import io.quarkus.config.yaml.runtime.YamlInClassPathConfigBuilder;
import io.quarkus.config.yaml.runtime.YamlInFileSystemConfigBuilder;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
import io.quarkus.deployment.pkg.NativeConfig;
import io.smallrye.config.SmallRyeConfig;

public final class ConfigYamlProcessor {
Expand All @@ -24,12 +26,18 @@ public FeatureBuildItem feature() {
}

@BuildStep
public void yamlConfig(
void yamlConfig(
NativeConfig nativeConfig,
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {

staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(YamlConfigBuilder.class));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlConfigBuilder.class));
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(YamlInFileSystemConfigBuilder.class));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlInFileSystemConfigBuilder.class));

if (!nativeConfig.enabled()) {
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(YamlInClassPathConfigBuilder.class));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(YamlInClassPathConfigBuilder.class));
}
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.source.yaml.YamlConfigSourceLoader;

public class YamlConfigBuilder implements ConfigBuilder {
/**
* Only for JVM mode.
*
* @see io.quarkus.runtime.configuration.RuntimeConfigBuilder
*/
public class YamlInClassPathConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
return builder.withSources(new YamlConfigSourceLoader.InFileSystem())
.withSources(new YamlConfigSourceLoader.InClassPath());
return builder.withSources(new YamlConfigSourceLoader.InClassPath());
}

@Override
public int priority() {
return Integer.MIN_VALUE + 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.config.yaml.runtime;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

import io.quarkus.runtime.configuration.ConfigBuilder;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.source.yaml.YamlConfigSourceLoader;

public class YamlInFileSystemConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
// Removes Yaml source providers added by the ServiceLoader to avoid double registration
List<ConfigSourceProvider> toRemove = new ArrayList<>();
for (ConfigSourceProvider sourceProvider : builder.getSourceProviders()) {
if (sourceProvider instanceof YamlConfigSourceLoader) {
toRemove.add(sourceProvider);
}
}
builder.getSourceProviders().removeAll(toRemove);
return builder.withSources(new YamlConfigSourceLoader.InFileSystem());
}
}
Loading