Skip to content

Commit 3146957

Browse files
committed
Disallow MethodPointer for null targets entirely.
1 parent 3957e9b commit 3146957

File tree

13 files changed

+33
-53
lines changed

13 files changed

+33
-53
lines changed

substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMNativeImageCodeCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private void linkCompiledBatches(BatchExecutor executor, DebugContext debug, int
207207
stackMapDumper.close();
208208

209209
HostedMethod firstMethod = (HostedMethod) getFirstCompilation().getMethods()[0];
210-
buildRuntimeMetadata(MethodPointer.factory(firstMethod), WordFactory.signed(textSectionInfo.getCodeSize()));
210+
buildRuntimeMetadata(new MethodPointer(firstMethod), WordFactory.signed(textSectionInfo.getCodeSize()));
211211
}
212212

213213
private void llvmOptimize(DebugContext debug, String outputPath, String inputPath) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public final class InvalidMethodPointerHandler {
5050
public static final Method INVALID_VTABLE_ENTRY_HANDLER_METHOD = ReflectionUtil.lookupMethod(InvalidMethodPointerHandler.class, "invalidVTableEntryHandler");
5151
public static final String INVALID_VTABLE_ENTRY_MSG = "Fatal error: Virtual method call used an illegal vtable entry that was seen as unused by the static analysis";
5252

53-
public static final Method METHOD_POINTER_INVALID_HANDLER_METHOD = ReflectionUtil.lookupMethod(InvalidMethodPointerHandler.class, "methodPointerInvalidHandler");
54-
public static final String METHOD_POINTER_INVALID_MSG = "Fatal error: Method pointer invoked on a method that is null, was not registered for compilation, or was not seen as invoked by the static analysis";
53+
public static final Method METHOD_POINTER_NOT_COMPILED_HANDLER_METHOD = ReflectionUtil.lookupMethod(InvalidMethodPointerHandler.class, "methodPointerNotCompiledHandler");
54+
public static final String METHOD_POINTER_NOT_COMPILED_MSG = "Fatal error: Method pointer invoked on a method that was not compiled because it was not seen as invoked by the static analysis nor was it directly registered for compilation";
5555

5656
@StubCallingConvention
5757
@NeverInline("We need a separate frame that stores all registers")
@@ -63,10 +63,10 @@ private static void invalidVTableEntryHandler() {
6363

6464
@StubCallingConvention
6565
@NeverInline("We need a separate frame that stores all registers")
66-
private static void methodPointerInvalidHandler() {
66+
private static void methodPointerNotCompiledHandler() {
6767
Pointer callerSP = KnownIntrinsics.readCallerStackPointer();
6868
CodePointer callerIP = KnownIntrinsics.readReturnAddress();
69-
failFatally(callerSP, callerIP, METHOD_POINTER_INVALID_MSG);
69+
failFatally(callerSP, callerIP, METHOD_POINTER_NOT_COMPILED_MSG);
7070
}
7171

7272
private static void failFatally(Pointer callerSP, CodePointer callerIP, String message) {

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/DeoptimizationFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ public void beforeCompilation(BeforeCompilationAccess a) {
126126
CompilationAccessImpl config = (CompilationAccessImpl) a;
127127
config.registerAsImmutable(ImageSingletons.lookup(DeoptimizationSupport.class));
128128
HostedMetaAccess metaAccess = config.getMetaAccess();
129-
DeoptimizationSupport.setDeoptStubPointer(MethodPointer.factory(metaAccess.lookupJavaMethod(deoptStubMethod)));
129+
DeoptimizationSupport.setDeoptStubPointer(new MethodPointer(metaAccess.lookupJavaMethod(deoptStubMethod)));
130130
}
131131
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/classinitialization/ClassInitializationFeature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private static ClassInitializationInfo buildRuntimeInitializationInfo(FeatureImp
341341
/* Synthesize a VerifyError to be thrown at run time. */
342342
AnalysisMethod throwVerifyError = access.getMetaAccess().lookupJavaMethod(ExceptionSynthesizer.throwExceptionMethod(VerifyError.class));
343343
access.registerAsCompiled(throwVerifyError);
344-
return new ClassInitializationInfo(MethodPointer.factory(throwVerifyError));
344+
return new ClassInitializationInfo(new MethodPointer(throwVerifyError));
345345
} catch (Throwable t) {
346346
/*
347347
* All other linking errors will be reported as NoClassDefFoundError when initialization
@@ -360,7 +360,7 @@ private static ClassInitializationInfo buildRuntimeInitializationInfo(FeatureImp
360360
if (classInitializer != null) {
361361
assert classInitializer.getCode() != null;
362362
access.registerAsCompiled(classInitializer);
363-
classInitializerFunction = MethodPointer.factory(classInitializer);
363+
classInitializerFunction = new MethodPointer(classInitializer);
364364
}
365365
return new ClassInitializationInfo(classInitializerFunction);
366366
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/CEntryPointCallStubSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public AnalysisMethod registerJavaStubForMethod(AnalysisMethod method) {
110110
if (value == null) {
111111
assert !bb.getUniverse().sealed();
112112
AnalysisMethod nativeStub = registerStubForMethod(method, () -> CEntryPointData.create(method));
113-
CFunctionPointer nativeStubAddress = MethodPointer.factory(nativeStub);
113+
CFunctionPointer nativeStubAddress = new MethodPointer(nativeStub);
114114
String stubName = SubstrateUtil.uniqueShortName(method);
115115
ResolvedJavaType holderClass = bb.getMetaAccess().lookupJavaType(IsolateLeaveStub.class).getWrapped();
116116
CEntryPointJavaCallStubMethod stub = new CEntryPointJavaCallStubMethod(method.getWrapped(), stubName, holderClass, nativeStubAddress);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/CEntryPointLiteralFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Object apply(Object source) {
8181
* Only during compilation and native image writing, we do the actual
8282
* replacement.
8383
*/
84-
return MethodPointer.factory(hStub);
84+
return new MethodPointer(hStub);
8585
}
8686
}
8787
return source;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void layoutMethods(DebugContext debug, String imageName, BigBang bb, Fork
103103
codeCacheSize = NumUtil.roundUp(codeCacheSize + compilation.getTargetCodeSize(), SubstrateOptions.codeAlignment());
104104
}
105105

106-
buildRuntimeMetadata(MethodPointer.factory(firstMethod), WordFactory.unsigned(codeCacheSize));
106+
buildRuntimeMetadata(new MethodPointer(firstMethod), WordFactory.unsigned(codeCacheSize));
107107
}
108108
}
109109

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,10 @@ private void markFunctionRelocationSite(final ProgbitsSectionImpl sectionImpl, f
574574
assert info.getRelocationSize() == functionPointerRelocationSize : "Function relocation: " + info.getRelocationSize() + " should be " + functionPointerRelocationSize + " bytes.";
575575
// References to functions are via relocations to the symbol for the function.
576576
MethodPointer methodPointer = (MethodPointer) info.getTargetObject();
577-
HostedMethod target = null;
578-
boolean valid = methodPointer.isValid();
579-
if (valid) {
580-
ResolvedJavaMethod method = methodPointer.getMethod();
581-
target = (method instanceof HostedMethod) ? (HostedMethod) method : heap.getUniverse().lookup(method);
582-
valid = target.isCompiled();
583-
}
584-
if (!valid) {
585-
target = metaAccess.lookupJavaMethod(InvalidMethodPointerHandler.METHOD_POINTER_INVALID_HANDLER_METHOD);
577+
ResolvedJavaMethod method = methodPointer.getMethod();
578+
HostedMethod target = (method instanceof HostedMethod) ? (HostedMethod) method : heap.getUniverse().lookup(method);
579+
if (!target.isCompiled()) {
580+
target = metaAccess.lookupJavaMethod(InvalidMethodPointerHandler.METHOD_POINTER_NOT_COMPILED_HANDLER_METHOD);
586581
}
587582
// A reference to a method. Mark the relocation site using the symbol name.
588583
sectionImpl.markRelocationSite(offset, RelocationKind.getDirect(functionPointerRelocationSize), localSymbolNameForMethod(target), false, 0L);
@@ -976,6 +971,6 @@ final class MethodPointerInvalidHandlerFeature implements Feature {
976971
@Override
977972
public void beforeAnalysis(BeforeAnalysisAccess a) {
978973
FeatureImpl.BeforeAnalysisAccessImpl access = (FeatureImpl.BeforeAnalysisAccessImpl) a;
979-
access.registerAsCompiled(InvalidMethodPointerHandler.METHOD_POINTER_INVALID_HANDLER_METHOD);
974+
access.registerAsCompiled(InvalidMethodPointerHandler.METHOD_POINTER_NOT_COMPILED_HANDLER_METHOD);
980975
}
981976
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/MethodPointer.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,36 @@
2626

2727
import static com.oracle.svm.core.util.VMError.shouldNotReachHere;
2828

29+
import java.util.Objects;
30+
2931
import org.graalvm.nativeimage.c.function.CFunctionPointer;
3032
import org.graalvm.word.ComparableWord;
3133

32-
import com.oracle.svm.core.InvalidMethodPointerHandler;
33-
3434
import jdk.vm.ci.meta.ResolvedJavaMethod;
3535

3636
/**
3737
* A pointer to the compiled code of a method.
3838
*/
39-
public class MethodPointer implements CFunctionPointer {
40-
private static final MethodPointer INVALID = new MethodPointer(null);
41-
39+
public final class MethodPointer implements CFunctionPointer {
4240
private final ResolvedJavaMethod method;
4341

44-
public static CFunctionPointer factory(ResolvedJavaMethod method) {
45-
return (method != null) ? new MethodPointer(method) : INVALID;
46-
}
47-
48-
protected MethodPointer(ResolvedJavaMethod method) {
42+
public MethodPointer(ResolvedJavaMethod method) {
43+
Objects.requireNonNull(method);
4944
this.method = method;
5045
}
5146

52-
public boolean isValid() {
53-
return (method != null);
54-
}
55-
5647
public ResolvedJavaMethod getMethod() {
57-
assert isValid();
5848
return method;
5949
}
6050

61-
/**
62-
* Always {@code false} because even a pointer to {@code null} or to a method that is not
63-
* compiled will eventually be replaced by
64-
* {@link InvalidMethodPointerHandler#METHOD_POINTER_INVALID_HANDLER_METHOD}.
65-
*/
6651
@Override
6752
public boolean isNull() {
6853
return false;
6954
}
7055

7156
@Override
7257
public boolean isNonNull() {
73-
return !isNull();
58+
return true;
7459
}
7560

7661
@Override

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/UniverseBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ private void buildHubs() {
925925
* We install a CodePointer in the vtable; when generating relocation info, we will
926926
* know these point into .text
927927
*/
928-
vtable[idx] = MethodPointer.factory(type.vtable[idx]);
928+
vtable[idx] = new MethodPointer(type.vtable[idx]);
929929
}
930930

931931
// pointer maps in Dynamic Hub

0 commit comments

Comments
 (0)