Skip to content

Commit 4ebf2fa

Browse files
committed
Disallow MethodPointer for null targets entirely.
1 parent cfc913e commit 4ebf2fa

File tree

4 files changed

+19
-38
lines changed

4 files changed

+19
-38
lines changed

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.hosted/src/com/oracle/svm/hosted/image/NativeImage.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,10 @@ private void markFunctionRelocationSite(final ProgbitsSectionImpl sectionImpl, f
571571
assert info.getRelocationSize() == functionPointerRelocationSize : "Function relocation: " + info.getRelocationSize() + " should be " + functionPointerRelocationSize + " bytes.";
572572
// References to functions are via relocations to the symbol for the function.
573573
MethodPointer methodPointer = (MethodPointer) info.getTargetObject();
574-
HostedMethod target = null;
575-
boolean valid = methodPointer.isValid();
576-
if (valid) {
577-
ResolvedJavaMethod method = methodPointer.getMethod();
578-
target = (method instanceof HostedMethod) ? (HostedMethod) method : heap.getUniverse().lookup(method);
579-
valid = target.isCompiled();
580-
}
581-
if (!valid) {
582-
target = metaAccess.lookupJavaMethod(InvalidMethodPointerHandler.METHOD_POINTER_INVALID_HANDLER_METHOD);
574+
ResolvedJavaMethod method = methodPointer.getMethod();
575+
HostedMethod target = (method instanceof HostedMethod) ? (HostedMethod) method : heap.getUniverse().lookup(method);
576+
if (!target.isCompiled()) {
577+
target = metaAccess.lookupJavaMethod(InvalidMethodPointerHandler.METHOD_POINTER_NOT_COMPILED_HANDLER_METHOD);
583578
}
584579
// A reference to a method. Mark the relocation site using the symbol name.
585580
sectionImpl.markRelocationSite(offset, RelocationKind.getDirect(functionPointerRelocationSize), localSymbolNameForMethod(target), false, 0L);

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,10 @@ private void addNonDataRelocation(RelocatableBuffer buffer, int index, Relocated
250250

251251
RelocatedPointer target = pointer;
252252
MethodPointer methodPointer = ((MethodPointer) target);
253-
boolean valid = methodPointer.isValid();
254-
if (valid) {
255-
ResolvedJavaMethod method = methodPointer.getMethod();
256-
HostedMethod hMethod = (method instanceof HostedMethod) ? (HostedMethod) method : heap.getUniverse().lookup(method);
257-
valid = hMethod.isCompiled();
258-
}
259-
if (!valid) {
260-
target = ImageSingletons.lookup(MethodPointerInvalidHandlerFeature.class).getHandler();
253+
ResolvedJavaMethod method = methodPointer.getMethod();
254+
HostedMethod hMethod = (method instanceof HostedMethod) ? (HostedMethod) method : heap.getUniverse().lookup(method);
255+
if (!hMethod.isCompiled()) {
256+
target = ImageSingletons.lookup(MethodPointerNotCompiledHandlerFeature.class).getHandler();
261257
}
262258
int pointerSize = ConfigurationValues.getTarget().wordSize;
263259
addDirectRelocationWithoutAddend(buffer, index, pointerSize, target);
@@ -411,13 +407,13 @@ private void writeObject(ObjectInfo info, RelocatableBuffer buffer) {
411407
}
412408

413409
@AutomaticFeature
414-
final class MethodPointerInvalidHandlerFeature implements Feature {
410+
final class MethodPointerNotCompiledHandlerFeature implements Feature {
415411
private CFunctionPointer handler;
416412

417413
@Override
418414
public void beforeAnalysis(BeforeAnalysisAccess a) {
419415
FeatureImpl.BeforeAnalysisAccessImpl access = (FeatureImpl.BeforeAnalysisAccessImpl) a;
420-
AnalysisMethod notCompiledMethod = access.getMetaAccess().lookupJavaMethod(InvalidMethodPointerHandler.METHOD_POINTER_INVALID_HANDLER_METHOD);
416+
AnalysisMethod notCompiledMethod = access.getMetaAccess().lookupJavaMethod(InvalidMethodPointerHandler.METHOD_POINTER_NOT_COMPILED_HANDLER_METHOD);
421417
access.registerAsCompiled(notCompiledMethod);
422418
handler = MethodPointer.factory(notCompiledMethod);
423419
}

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,38 @@
2929
import org.graalvm.nativeimage.c.function.CFunctionPointer;
3030
import org.graalvm.word.ComparableWord;
3131

32-
import com.oracle.svm.core.InvalidMethodPointerHandler;
32+
import com.oracle.svm.core.util.VMError;
3333

3434
import jdk.vm.ci.meta.ResolvedJavaMethod;
3535

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

4442
public static CFunctionPointer factory(ResolvedJavaMethod method) {
45-
return (method != null) ? new MethodPointer(method) : INVALID;
43+
VMError.guarantee(method != null, "MethodPointer cannot have null target method");
44+
return new MethodPointer(method);
4645
}
4746

4847
protected MethodPointer(ResolvedJavaMethod method) {
48+
assert method != null;
4949
this.method = method;
5050
}
5151

52-
public boolean isValid() {
53-
return (method != null);
54-
}
55-
5652
public ResolvedJavaMethod getMethod() {
57-
assert isValid();
5853
return method;
5954
}
6055

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-
*/
6656
@Override
6757
public boolean isNull() {
6858
return false;
6959
}
7060

7161
@Override
7262
public boolean isNonNull() {
73-
return !isNull();
63+
return true;
7464
}
7565

7666
@Override

0 commit comments

Comments
 (0)