Skip to content

Commit 4464493

Browse files
committed
Use AbstractImageHeapList to restrict SubstrateDiagnostics.thunks runtime type.
1 parent 448b0de commit 4464493

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateDiagnostics.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static com.oracle.svm.core.option.RuntimeOptionKey.RuntimeOptionKeyFlag.RelevantForCompilationIsolates;
2929

3030
import java.util.Arrays;
31-
import java.util.List;
3231

3332
import org.graalvm.collections.EconomicMap;
3433
import org.graalvm.nativeimage.CurrentIsolate;
@@ -101,9 +100,9 @@
101100
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.Independent;
102101
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.InitialLayerOnly;
103102
import com.oracle.svm.core.traits.SingletonTraits;
103+
import com.oracle.svm.core.util.AbstractImageHeapList;
104104
import com.oracle.svm.core.util.CounterSupport;
105105
import com.oracle.svm.core.util.ImageHeapList;
106-
import com.oracle.svm.core.util.RuntimeImageHeapList;
107106
import com.oracle.svm.core.util.TimeUtils;
108107
import com.oracle.svm.core.util.VMError;
109108

@@ -1283,7 +1282,7 @@ public static class DiagnosticThunkRegistry {
12831282
@Platforms(Platform.HOSTED_ONLY.class) //
12841283
final int runtimeCompilationPosition;
12851284

1286-
private final List<DiagnosticThunk> thunks = ImageHeapList.create(DiagnosticThunk.class);
1285+
private final AbstractImageHeapList<DiagnosticThunk> thunks = ImageHeapList.create(DiagnosticThunk.class);
12871286
private int[] initialInvocationCount;
12881287

12891288
@Fold
@@ -1368,12 +1367,7 @@ int size() {
13681367
}
13691368

13701369
DiagnosticThunk getThunk(int index) {
1371-
/*
1372-
* Use an explicit cast to aid open-world analysis. Otherwise, this may trigger false
1373-
* positive violations of @RestrictHeapAccess since some implementations of List.get()
1374-
* can allocate.
1375-
*/
1376-
return ((RuntimeImageHeapList<DiagnosticThunk>) thunks).get(index);
1370+
return thunks.get(index);
13771371
}
13781372

13791373
int getInitialInvocationCount(int index) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.util;
26+
27+
import java.util.AbstractList;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
import com.oracle.svm.core.heap.RestrictHeapAccess;
32+
import com.oracle.svm.core.util.ImageHeapList.HostedImageHeapList;
33+
34+
/**
35+
* Super type for {@link HostedImageHeapList} and {@link RuntimeImageHeapList} that allows declaring
36+
* a more restricted type for fields initialized with {@link ImageHeapList#create(Class)} to aid
37+
* open-world analysis. When such a field is analysed in an open-world it gets injected all possible
38+
* subtypes of its declared type. Declaring the type as {@link AbstractImageHeapList} allows us to
39+
* only inject {@link RuntimeImageHeapList}.This enables a more precise analysis and avoids for
40+
* example triggering false-positive violations of @{@link RestrictHeapAccess}: if the field was
41+
* declared as {@link List} then implementations of {@link List} for which simple access operations
42+
* can allocate, such as {@link ArrayList#get(int)}, could become reachable from code annotated
43+
* with @{@link RestrictHeapAccess}.
44+
*/
45+
public abstract sealed class AbstractImageHeapList<E> extends AbstractList<E> permits HostedImageHeapList, RuntimeImageHeapList {
46+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/ImageHeapList.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package com.oracle.svm.core.util;
2626

2727
import java.lang.reflect.Array;
28-
import java.util.AbstractList;
2928
import java.util.ArrayList;
3029
import java.util.Arrays;
3130
import java.util.Comparator;
@@ -57,17 +56,17 @@
5756
public final class ImageHeapList {
5857

5958
@Platforms(Platform.HOSTED_ONLY.class) //
60-
public static <E> List<E> create(Class<E> elementClass) {
59+
public static <E> AbstractImageHeapList<E> create(Class<E> elementClass) {
6160
return create(elementClass, null);
6261
}
6362

6463
@Platforms(Platform.HOSTED_ONLY.class) //
65-
public static List<?> createGeneric(Class<?> elementClass) {
64+
public static AbstractImageHeapList<?> createGeneric(Class<?> elementClass) {
6665
return create(elementClass, null);
6766
}
6867

6968
@Platforms(Platform.HOSTED_ONLY.class) //
70-
public static <E> List<E> create(Class<E> elementClass, Comparator<E> comparator) {
69+
public static <E> AbstractImageHeapList<E> create(Class<E> elementClass, Comparator<E> comparator) {
7170
VMError.guarantee(!BuildPhaseProvider.isAnalysisFinished(), "Trying to create an ImageHeapList after analysis.");
7271
return new HostedImageHeapList<>(elementClass, comparator);
7372
}
@@ -76,7 +75,7 @@ private ImageHeapList() {
7675
}
7776

7877
@Platforms(Platform.HOSTED_ONLY.class) //
79-
public static final class HostedImageHeapList<E> extends AbstractList<E> {
78+
public static final class HostedImageHeapList<E> extends AbstractImageHeapList<E> {
8079
private final Comparator<E> comparator;
8180
private final List<E> hostedList;
8281
private final RuntimeImageHeapList<E> runtimeList;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/RuntimeImageHeapList.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
package com.oracle.svm.core.util;
2626

27-
import java.util.AbstractList;
28-
2927
import org.graalvm.nativeimage.Platform;
3028
import org.graalvm.nativeimage.Platforms;
3129

@@ -34,7 +32,7 @@
3432
/**
3533
* The immutable runtime list view for an {@link ImageHeapList}.
3634
*/
37-
public final class RuntimeImageHeapList<E> extends AbstractList<E> {
35+
public final class RuntimeImageHeapList<E> extends AbstractImageHeapList<E> {
3836

3937
E[] elementData;
4038

0 commit comments

Comments
 (0)