Skip to content

Commit a1962e6

Browse files
committed
Outline call_indirect/call_ref not-a-function error cases
1 parent 231c7a9 commit a1962e6

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/nodes/WasmFunctionNode.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -585,33 +585,15 @@ public Object executeBodyFromOffset(WasmInstance instance, VirtualFrame frame, i
585585
functionCandidate = popReference(frame, --stackPointer);
586586
elementIndex = -1;
587587
}
588-
final WasmFunctionInstance functionInstance;
589-
final WasmFunction function;
590-
final CallTarget target;
591-
final WasmContext functionInstanceContext;
592-
if (functionCandidate == WasmConstant.NULL) {
593-
enterErrorBranch();
594-
if (opcode == Bytecode.CALL_INDIRECT_U8 || opcode == Bytecode.CALL_INDIRECT_I32) {
595-
throw WasmException.format(Failure.UNINITIALIZED_ELEMENT, this, "Table element at index %d is uninitialized.", elementIndex);
596-
} else {
597-
assert opcode == Bytecode.CALL_REF_U8 || opcode == Bytecode.CALL_REF_I32;
598-
throw WasmException.format(Failure.NULL_FUNCTION_REFERENCE, this, "Function reference is null");
599-
}
600-
} else if (functionCandidate instanceof WasmFunctionInstance) {
601-
functionInstance = (WasmFunctionInstance) functionCandidate;
602-
function = functionInstance.function();
603-
target = functionInstance.target();
604-
functionInstanceContext = functionInstance.context();
605-
} else {
606-
enterErrorBranch();
607-
if (opcode == Bytecode.CALL_INDIRECT_U8 || opcode == Bytecode.CALL_INDIRECT_I32) {
608-
throw WasmException.format(Failure.UNSPECIFIED_TRAP, this, "Unknown table element type: %s", functionCandidate);
609-
} else {
610-
assert opcode == Bytecode.CALL_REF_U8 || opcode == Bytecode.CALL_REF_I32;
611-
throw WasmException.format(Failure.UNSPECIFIED_TRAP, this, "Unknown function object: %s", functionCandidate);
612-
}
588+
589+
if (!(functionCandidate instanceof WasmFunctionInstance functionInstance)) {
590+
throw callIndirectNotAFunctionError(opcode, functionCandidate, elementIndex);
613591
}
614592

593+
final WasmFunction function = functionInstance.function();
594+
final CallTarget target = functionInstance.target();
595+
final WasmContext functionInstanceContext = functionInstance.context();
596+
615597
// Target function instance must be from the same context.
616598
assert functionInstanceContext == WasmContext.get(this);
617599

@@ -1778,6 +1760,26 @@ private void failFunctionTypeCheck(WasmFunction function, int expectedFunctionTy
17781760
function.typeIndex(), function.name(), expectedFunctionTypeIndex, module.name());
17791761
}
17801762

1763+
@HostCompilerDirectives.InliningCutoff
1764+
private WasmException callIndirectNotAFunctionError(int opcode, Object functionCandidate, int elementIndex) {
1765+
enterErrorBranch();
1766+
if (functionCandidate == WasmConstant.NULL) {
1767+
if (opcode == Bytecode.CALL_INDIRECT_U8 || opcode == Bytecode.CALL_INDIRECT_I32) {
1768+
throw WasmException.format(Failure.UNINITIALIZED_ELEMENT, this, "Table element at index %d is uninitialized.", elementIndex);
1769+
} else {
1770+
assert opcode == Bytecode.CALL_REF_U8 || opcode == Bytecode.CALL_REF_I32;
1771+
throw WasmException.format(Failure.NULL_FUNCTION_REFERENCE, this, "Function reference is null");
1772+
}
1773+
} else {
1774+
if (opcode == Bytecode.CALL_INDIRECT_U8 || opcode == Bytecode.CALL_INDIRECT_I32) {
1775+
throw WasmException.format(Failure.UNSPECIFIED_TRAP, this, "Unknown table element type: %s", functionCandidate);
1776+
} else {
1777+
assert opcode == Bytecode.CALL_REF_U8 || opcode == Bytecode.CALL_REF_I32;
1778+
throw WasmException.format(Failure.UNSPECIFIED_TRAP, this, "Unknown function object: %s", functionCandidate);
1779+
}
1780+
}
1781+
}
1782+
17811783
private void check(int v, int limit) {
17821784
// This is a temporary hack to hoist values out of the loop.
17831785
if (v >= limit) {

0 commit comments

Comments
 (0)