Skip to content

Commit 9ce1672

Browse files
committed
[GR-47406] Correctly handle printing certain JSON values in agent trace files.
PullRequest: graal/15884
2 parents a6b2623 + 2d30af2 commit 9ce1672

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

substratevm/src/com.oracle.svm.agent/src/com/oracle/svm/agent/tracing/TraceFileWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ private static void printArray(JsonWriter json, Object[] array) throws IOExcepti
9999
}
100100

101101
private static void printValue(JsonWriter json, Object value) throws IOException {
102-
String s = null;
102+
Object s = null;
103103
if (value instanceof byte[]) {
104104
s = Base64.getEncoder().encodeToString((byte[]) value);
105105
} else if (value != null) {
106-
s = value.toString();
106+
s = value;
107107
}
108-
json.quote(s);
108+
json.printValue(s);
109109
}
110110

111111
private void traceEntry(String s) throws IOException {

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/command/ConfigurationGenerateCommand.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ protected static void generate(Iterator<String> argumentsIterator, boolean accep
146146
boolean builtinCallerFilter = true;
147147
boolean builtinHeuristicFilter = true;
148148
List<URI> callerFilters = new ArrayList<>();
149+
List<URI> accessFilters = new ArrayList<>();
149150

150151
ConfigurationFileCollection omittedCollection = new ConfigurationFileCollection();
151152
ConfigurationFileCollection inputCollection = new ConfigurationFileCollection();
@@ -220,6 +221,9 @@ protected static void generate(Iterator<String> argumentsIterator, boolean accep
220221
case "--caller-filter-file":
221222
callerFilters.add(requirePathUri(option, value));
222223
break;
224+
case "--access-filter-file":
225+
accessFilters.add(requirePathUri(option, value));
226+
break;
223227
case "--":
224228
if (acceptTraceFileArgs) {
225229
argumentsIterator.forEachRemaining(arg -> traceInputs.add(Paths.get(arg).toUri()));
@@ -248,15 +252,12 @@ protected static void generate(Iterator<String> argumentsIterator, boolean accep
248252
callersFilterHierarchyFilterNode = AccessAdvisor.copyBuiltinCallerFilterTree();
249253
callersFilter = new ComplexFilter(callersFilterHierarchyFilterNode);
250254
}
251-
for (URI uri : callerFilters) {
252-
try {
253-
FilterConfigurationParser parser = new FilterConfigurationParser(callersFilter);
254-
parser.parseAndRegister(uri);
255-
} catch (Exception e) {
256-
throw new ConfigurationUsageException("Cannot parse filter file " + uri + ": " + e);
257-
}
258-
}
259-
callersFilter.getHierarchyFilterNode().removeRedundantNodes();
255+
parseFilterFiles(callersFilter, callerFilters);
256+
}
257+
ComplexFilter accessFilter = null;
258+
if (!accessFilters.isEmpty()) {
259+
accessFilter = new ComplexFilter(AccessAdvisor.copyBuiltinAccessFilterTree());
260+
parseFilterFiles(accessFilter, accessFilters);
260261
}
261262

262263
ConfigurationSet configurationSet;
@@ -287,6 +288,9 @@ protected static void generate(Iterator<String> argumentsIterator, boolean accep
287288
if (callersFilter != null) {
288289
advisor.setCallerFilterTree(callersFilter);
289290
}
291+
if (accessFilter != null) {
292+
advisor.setAccessFilterTree(accessFilter);
293+
}
290294

291295
TraceProcessor processor = new TraceProcessor(advisor);
292296
for (URI uri : traceInputs) {
@@ -331,6 +335,17 @@ protected static void generate(Iterator<String> argumentsIterator, boolean accep
331335
}
332336
}
333337

338+
private static void parseFilterFiles(ComplexFilter filter, List<URI> filterFiles) {
339+
for (URI uri : filterFiles) {
340+
try {
341+
new FilterConfigurationParser(filter).parseAndRegister(uri);
342+
} catch (Exception e) {
343+
throw new ConfigurationUsageException("Cannot parse filter file " + uri + ": " + e);
344+
}
345+
}
346+
filter.getHierarchyFilterNode().removeRedundantNodes();
347+
}
348+
334349
@SuppressWarnings("fallthrough")
335350
private static void failIfAgentLockFilesPresent(ConfigurationFileCollection... collections) {
336351
Set<String> paths = null;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/json/JsonWriter.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,21 @@ public JsonWriter appendFieldSeparator() throws IOException {
8282
}
8383

8484
public JsonWriter appendKeyValue(String key, Object value) throws IOException {
85-
return quote(key).appendFieldSeparator().quote(value);
85+
return quote(key).appendFieldSeparator().printValue(value);
8686
}
8787

8888
@SuppressWarnings({"unchecked", "rawtypes"})
89-
public void print(Object value) throws IOException {
89+
public JsonWriter print(Object value) throws IOException {
9090
if (value instanceof Map map) {
9191
printMap(map); // Must always be <String, Object>
9292
} else if (value instanceof Iterator it) {
9393
printIterator(it);
9494
} else if (value instanceof List list) {
9595
printIterator(list.iterator());
9696
} else {
97-
quote(value);
97+
printValue(value);
9898
}
99+
return this;
99100
}
100101

101102
@SuppressWarnings("unchecked")
@@ -130,15 +131,26 @@ private void printIterator(Iterator<?> iter) throws IOException {
130131
append(']');
131132
}
132133

133-
public JsonWriter quote(Object o) throws IOException {
134+
public JsonWriter printValue(Object o) throws IOException {
134135
if (o == null) {
135136
return append("null");
136-
} else if (Boolean.TRUE.equals(o)) {
137-
return append("true");
138-
} else if (Boolean.FALSE.equals(o)) {
139-
return append("false");
140-
} else if (o instanceof Number) {
137+
} else if (o instanceof Boolean || o instanceof Byte || o instanceof Short || o instanceof Integer || o instanceof Long) {
138+
/*
139+
* Note that sub-integer values here most likely become Integer objects when parsing,
140+
* and comparisons such as equals() or compareTo() on boxed values only work on the
141+
* exact same type. (Boolean values, however, should be deserialized as Boolean).
142+
*/
141143
return append(o.toString());
144+
} else if (o instanceof Float f) {
145+
if (f.isNaN() || f.isInfinite()) {
146+
return quote(f.toString()); // cannot express, best we can do without failing
147+
}
148+
return append(f.toString());
149+
} else if (o instanceof Double d) {
150+
if (d.isNaN() || d.isInfinite()) {
151+
return quote(d.toString()); // cannot express, best we can do without failing
152+
}
153+
return append(d.toString());
142154
} else {
143155
return quote(o.toString());
144156
}

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/BundleSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,8 @@ private static Manifest createManifest() {
860860
private static final String substitutionMapDstField = "dst";
861861

862862
private static void printPathMapping(Map.Entry<Path, Path> entry, JsonWriter w) throws IOException {
863-
w.append('{').quote(substitutionMapSrcField).append(':').quote(entry.getKey());
864-
w.append(',').quote(substitutionMapDstField).append(':').quote(entry.getValue());
863+
w.append('{').quote(substitutionMapSrcField).append(':').printValue(entry.getKey());
864+
w.append(',').quote(substitutionMapDstField).append(':').printValue(entry.getValue());
865865
w.append('}');
866866
}
867867

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/ImageHeapConnectedComponentsPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void printSummaryInfoForEveryObjectInConnectedComponents(PrintWriter out)
216216
for (Iterator<ConnectedComponent> iterator = connectedComponents.iterator(); iterator.hasNext();) {
217217
ConnectedComponent connectedComponent = iterator.next();
218218
writer.append('{').newline();
219-
writer.quote("componentId").append(':').quote(connectedComponent.getId()).append(',').newline();
219+
writer.quote("componentId").append(':').printValue(connectedComponent.getId()).append(',').newline();
220220
writer.quote("sizeInBytes").append(':').append(String.valueOf(connectedComponent.getSizeInBytes())).append(',').newline();
221221
writer.quote("objects").append(":[");
222222
List<ObjectInfo> objects = connectedComponent.getObjects();

0 commit comments

Comments
 (0)