Skip to content

Commit 244d484

Browse files
committed
[GR-48281] InternalResource lookup fails on platform where ProcessProperties#getExecutableName is not available.
PullRequest: graal/15370
2 parents 88d0d9f + 0e45efe commit 244d484

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,21 @@ final class Target_com_oracle_truffle_polyglot_InternalResourceCache {
13741374
*/
13751375
@Alias @RecomputeFieldValue(kind = Kind.Reset) //
13761376
private static volatile Pair<Path, Boolean> cacheRoot;
1377+
1378+
@Alias @RecomputeFieldValue(kind = Kind.Custom, declClass = UseInternalResourcesComputer.class, isFinal = true) //
1379+
private static boolean useInternalResources;
1380+
1381+
private static final class UseInternalResourcesComputer implements FieldValueTransformerWithAvailability {
1382+
@Override
1383+
public ValueAvailability valueAvailability() {
1384+
return ValueAvailability.BeforeAnalysis;
1385+
}
1386+
1387+
@Override
1388+
public Object transform(Object receiver, Object originalValue) {
1389+
return TruffleBaseFeature.Options.CopyLanguageResources.getValue();
1390+
}
1391+
}
13771392
}
13781393

13791394
@TargetClass(className = "com.oracle.truffle.polyglot.InternalResourceCache$ResettableCachedRoot", onlyWith = TruffleBaseFeature.IsEnabled.class)

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceCache.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,31 @@ private static Path findCacheRootOnNativeImage() {
302302
Pair<Path, Boolean> res = cacheRoot;
303303
if (res == null) {
304304
assert ImageInfo.inImageRuntimeCode() : "Can be called only in the native-image execution time.";
305-
Path executable = Path.of(ProcessProperties.getExecutableName());
306-
Path parent = executable.getParent();
307-
if (parent == null) {
308-
throw new IllegalStateException("Could not locate native-image resources.");
309-
}
310-
Path cache = parent.resolve("resources");
305+
Path executable = getExecutablePath();
306+
Path cache = executable.resolveSibling("resources");
311307
res = Pair.create(cache, false);
312308
cacheRoot = res;
313309
}
314310
return res.getLeft();
315311
}
316312

313+
private static Path getExecutablePath() {
314+
assert ImageInfo.inImageRuntimeCode();
315+
if (useInternalResources) {
316+
return Path.of(ProcessProperties.getExecutableName());
317+
} else {
318+
throw new IllegalArgumentException("Lookup an executable name is restricted. " +
319+
"To enable it, use '-H:+CopyLanguageResources' during the native image build.");
320+
}
321+
}
322+
323+
/**
324+
* Recomputed before the analyses by a substitution in the {@code TruffleBaseFeature} based on
325+
* the {@code CopyLanguageResources} option value. The field must not be declared as
326+
* {@code final} to make the substitution function correctly.
327+
*/
328+
private static boolean useInternalResources = true;
329+
317330
/**
318331
* Collects optional internal resources for native-image build. This method is called
319332
* reflectively by the {@code TruffleBaseFeature#initializeTruffleReflectively}.

0 commit comments

Comments
 (0)