Skip to content

Commit bfe3626

Browse files
committed
Properly trace negative resource queries
1 parent 676710a commit bfe3626

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Resources.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.graalvm.nativeimage.Platforms;
5555
import org.graalvm.nativeimage.impl.ConfigurationCondition;
5656

57+
import com.oracle.svm.core.AlwaysInline;
5758
import com.oracle.svm.core.BuildPhaseProvider;
5859
import com.oracle.svm.core.ClassLoaderSupport.ConditionWithOrigin;
5960
import com.oracle.svm.core.MissingRegistrationUtils;
@@ -437,6 +438,10 @@ public static ResourceStorageEntryBase getAtRuntime(String name) {
437438
* The {@code probe} parameter indicates whether the caller is probing for the existence of a
438439
* resource. If {@code probe} is true, failed resource lookups return will not throw missing
439440
* registration errors and may instead return {@link #MISSING_METADATA_MARKER}.
441+
* <p>
442+
* Tracing note: When this method is used for probing, only successful metadata matches will be
443+
* traced. If a probing result is {@link #MISSING_METADATA_MARKER}, the caller must explicitly
444+
* trace the missing metadata.
440445
*/
441446
public static ResourceStorageEntryBase getAtRuntime(Module module, String resourceName, boolean probe) {
442447
VMError.guarantee(ImageInfo.inImageRuntimeCode(), "This function should be used only at runtime.");
@@ -448,16 +453,20 @@ public static ResourceStorageEntryBase getAtRuntime(Module module, String resour
448453
if (missingResourceMatchesIncludePattern(resourceName, moduleName) || missingResourceMatchesIncludePattern(canonicalResourceName, moduleName)) {
449454
// This resource name matches a pattern/glob from the provided metadata, but no
450455
// resource with the name actually exists. Do not report missing metadata.
456+
traceResource(resourceName, moduleName);
451457
return null;
452458
}
459+
traceResourceMissingMetadata(resourceName, moduleName, probe);
453460
return missingMetadata(module, resourceName, probe);
454461
} else {
462+
// NB: Without exact reachability metadata, resource include patterns are not
463+
// stored in the image heap, so we cannot reliably identify if the resource was
464+
// included at build time. Assume it is missing.
465+
traceResourceMissingMetadata(resourceName, moduleName, probe);
455466
return null;
456467
}
457468
}
458-
if (MetadataTracer.Options.MetadataTracingSupport.getValue() && MetadataTracer.singleton().enabled()) {
459-
MetadataTracer.singleton().traceResource(resourceName, moduleName);
460-
}
469+
traceResource(resourceName, moduleName);
461470
if (!entry.getConditions().satisfied()) {
462471
return missingMetadata(module, resourceName, probe);
463472
}
@@ -487,6 +496,28 @@ public static ResourceStorageEntryBase getAtRuntime(Module module, String resour
487496
return unconditionalEntry;
488497
}
489498

499+
@AlwaysInline("tracing should fold away when disabled")
500+
private static void traceResource(String resourceName, String moduleName) {
501+
if (MetadataTracer.Options.MetadataTracingSupport.getValue() && MetadataTracer.singleton().enabled()) {
502+
MetadataTracer.singleton().traceResource(resourceName, moduleName);
503+
}
504+
}
505+
506+
@AlwaysInline("tracing should fold away when disabled")
507+
private static void traceResourceMissingMetadata(String resourceName, String moduleName) {
508+
traceResourceMissingMetadata(resourceName, moduleName, false);
509+
}
510+
511+
@AlwaysInline("tracing should fold away when disabled")
512+
private static void traceResourceMissingMetadata(String resourceName, String moduleName, boolean probe) {
513+
if (MetadataTracer.Options.MetadataTracingSupport.getValue() && MetadataTracer.singleton().enabled() && !probe) {
514+
// Do not trace missing metadata for probing queries, otherwise we'll trace an entry for
515+
// every module. The caller is responsible for tracing missing entries if it uses
516+
// probing.
517+
MetadataTracer.singleton().traceResource(resourceName, moduleName);
518+
}
519+
}
520+
490521
/**
491522
* Checks whether the given missing resource is matched by a pattern/glob registered at build
492523
* time. In such a case, we should not report missing metadata.
@@ -558,6 +589,7 @@ public static InputStream createInputStream(Module module, String resourceName)
558589
}
559590
ResourceStorageEntryBase entry = findResourceForInputStream(module, resourceName);
560591
if (entry == MISSING_METADATA_MARKER) {
592+
traceResourceMissingMetadata(resourceName, moduleName(module));
561593
MissingResourceRegistrationUtils.reportResourceAccess(module, resourceName);
562594
return null;
563595
} else if (entry == null) {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/metadata/MetadataTracer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public ConfigurationType traceJNIType(String className) {
164164
}
165165

166166
/**
167-
* Marks the given resource within the given (optional) module as reachable.
167+
* Marks the given resource within the given (optional) module as reachable. Use this method to
168+
* trace resource lookups covered by image metadata (including negative queries).
168169
*/
169170
public void traceResource(String resourceName, String moduleName) {
170171
assert enabled();

0 commit comments

Comments
 (0)