Skip to content

Commit c012082

Browse files
committed
Add opt-in link-at-build-time option to fail fast on NoClassDefFound
Works around #6253 till a proper JVCMI fix is available.
1 parent 9ee08bd commit c012082

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/LinkAtBuildTimeSupport.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,6 +51,11 @@ static final class Options {
5151
@APIOption(name = "link-at-build-time-paths")//
5252
@Option(help = "file:doc-files/LinkAtBuildTimePathsHelp.txt")//
5353
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> LinkAtBuildTimePaths = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build());
54+
55+
// FIXME: Remove once we have a proper way to handle missing classes through JVMCI (see
56+
// https://github.com/oracle/graal/issues/6253)
57+
@Option(help = "Fail fast in case of missing classes when linking at build time.") //
58+
public static final HostedOptionKey<Boolean> LinkAtBuildTimeFailFast = new HostedOptionKey<>(false);
5459
}
5560

5661
private final ClassLoaderSupport classLoaderSupport;
@@ -118,4 +123,8 @@ private String linkAtBuildTimeReason(Class<?> clazz) {
118123
Set<OptionOrigin> origins = (Set<OptionOrigin>) reason;
119124
return origins.stream().map(OptionOrigin::toString).collect(Collectors.joining(" and "));
120125
}
126+
127+
public static boolean failFast() {
128+
return Options.LinkAtBuildTimeFailFast.getValue();
129+
}
121130
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/SharedGraphBuilderPhase.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -288,7 +288,12 @@ protected void maybeEagerlyResolve(int cpi, int bytecode) {
288288
try {
289289
super.maybeEagerlyResolve(cpi, bytecode);
290290
} catch (UnresolvedElementException e) {
291-
if (e.getCause() instanceof LambdaConversionException || e.getCause() instanceof LinkageError || e.getCause() instanceof IllegalAccessError) {
291+
Throwable cause = e.getCause();
292+
if (cause instanceof NoClassDefFoundError && linkAtBuildTime && LinkAtBuildTimeSupport.failFast()) {
293+
String message = "Error during parsing of method " + method.format("%H.%n(%P)") + ". " +
294+
LinkAtBuildTimeSupport.singleton().errorMessageFor(method.getDeclaringClass());
295+
throw new UnresolvedElementException(message, cause);
296+
} else if (cause instanceof LambdaConversionException || cause instanceof LinkageError) {
292297
/*
293298
* Ignore LinkageError, LambdaConversionException or IllegalAccessError if
294299
* thrown from eager resolution attempt. This is usually followed by a call to
@@ -1084,7 +1089,7 @@ private Object loadConstantDynamic(int cpi, int opcode) {
10841089
* Therefore, we cannot just treat it as "safe at build time". The class
10851090
* initialization is also completely useless because the invoking class must be
10861091
* already initialized by the time the boostrap method is executed.
1087-
*
1092+
*
10881093
* We replicate the implementation of the bootstrap method here without doing
10891094
* the class initialization.
10901095
*/

0 commit comments

Comments
 (0)