Skip to content

Commit

Permalink
Add opt-in link-at-build-time option to fail fast on NoClassDefFound
Browse files Browse the repository at this point in the history
Works around oracle#6253 till a proper JVCMI fix is available.
  • Loading branch information
zakkak committed Nov 18, 2024
1 parent 24f5a78 commit 5e3ab9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -51,6 +51,11 @@ static final class Options {
@APIOption(name = "link-at-build-time-paths")//
@Option(help = "file:doc-files/LinkAtBuildTimePathsHelp.txt")//
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> LinkAtBuildTimePaths = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build());

// FIXME: Remove once we have a proper way to handle missing classes through JVMCI (see
// https://github.com/oracle/graal/issues/6253)
@Option(help = "Fail fast in case of missing classes when linking at build time.") //
public static final HostedOptionKey<Boolean> LinkAtBuildTimeFailFast = new HostedOptionKey<>(false);
}

private final ClassLoaderSupport classLoaderSupport;
Expand Down Expand Up @@ -118,4 +123,8 @@ private String linkAtBuildTimeReason(Class<?> clazz) {
Set<OptionOrigin> origins = (Set<OptionOrigin>) reason;
return origins.stream().map(OptionOrigin::toString).collect(Collectors.joining(" and "));
}

public static boolean failFast() {
return Options.LinkAtBuildTimeFailFast.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -287,7 +287,12 @@ protected void maybeEagerlyResolve(int cpi, int bytecode) {
try {
super.maybeEagerlyResolve(cpi, bytecode);
} catch (UnresolvedElementException e) {
if (e.getCause() instanceof LambdaConversionException || e.getCause() instanceof LinkageError || e.getCause() instanceof IllegalAccessError) {
Throwable cause = e.getCause();
if (cause instanceof NoClassDefFoundError && linkAtBuildTime && LinkAtBuildTimeSupport.failFast()) {
String message = "Error during parsing of method " + method.format("%H.%n(%P)") + ". " +
LinkAtBuildTimeSupport.singleton().errorMessageFor(method.getDeclaringClass());
throw new UnresolvedElementException(message, cause);
} else if (cause instanceof LambdaConversionException || cause instanceof LinkageError) {
/*
* Ignore LinkageError, LambdaConversionException or IllegalAccessError if
* thrown from eager resolution attempt. This is usually followed by a call to
Expand Down Expand Up @@ -1081,7 +1086,7 @@ private Object loadConstantDynamic(int cpi, int opcode) {
* Therefore, we cannot just treat it as "safe at build time". The class
* initialization is also completely useless because the invoking class must be
* already initialized by the time the boostrap method is executed.
*
*
* We replicate the implementation of the bootstrap method here without doing
* the class initialization.
*/
Expand Down

0 comments on commit 5e3ab9f

Please sign in to comment.