Skip to content

Commit 7f20ae3

Browse files
committed
Fix handling of exception table in instrumented bytecode.
Move exceptionTableOffset field to WasmFunctionNode. No need to emit exception table offset in instrumented bytecode array.
1 parent 41168dd commit 7f20ae3

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,12 @@ private CodeEntry readFunction(int functionIndex, byte[] locals, byte[] resultTy
10891089
fail(Failure.SECTION_SIZE_MISMATCH, "END opcode expected");
10901090
}
10911091
}
1092+
1093+
if (offsetToLineIndexMap != null) {
1094+
// Make sure we notify a statement exit before leaving the function
1095+
bytecode.addNotify(-1, -1);
1096+
}
1097+
10921098
final int bytecodeEndOffset = bytecode.location();
10931099

10941100
final int exceptionTableOffset;
@@ -1098,11 +1104,12 @@ private CodeEntry readFunction(int functionIndex, byte[] locals, byte[] resultTy
10981104
} else {
10991105
exceptionTableOffset = 0;
11001106
}
1101-
bytecode.add(exceptionTableOffset);
1102-
1103-
final int functionEndOffset = bytecode.location();
11041107

11051108
if (offsetToLineIndexMap == null) {
1109+
bytecode.add(exceptionTableOffset);
1110+
1111+
final int functionEndOffset = bytecode.location();
1112+
11061113
bytecode.addCodeEntry(functionIndex, state.maxStackSize(), bytecodeEndOffset - bytecodeStartOffset, locals.length, resultTypes.length);
11071114
for (byte local : locals) {
11081115
bytecode.addByte(local);
@@ -1119,9 +1126,6 @@ private CodeEntry readFunction(int functionIndex, byte[] locals, byte[] resultTy
11191126

11201127
// Do not override the code entry offset when rereading the function.
11211128
module.setCodeEntryOffset(codeEntryIndex, functionEndOffset);
1122-
} else {
1123-
// Make sure we notify a statement exit before leaving the function
1124-
bytecode.addNotify(-1, -1);
11251129
}
11261130
return new CodeEntry(functionIndex, state.maxStackSize(), locals, resultTypes, callNodes, bytecodeStartOffset, bytecodeEndOffset, state.usesMemoryZero(), exceptionTableOffset);
11271131
}
@@ -3405,13 +3409,13 @@ private Vector128 readUnsignedInt128() {
34053409
* index map
34063410
*/
34073411
@TruffleBoundary
3408-
public byte[] createFunctionDebugBytecode(int functionIndex, EconomicMap<Integer, Integer> offsetToLineIndexMap) {
3412+
public Pair<CodeEntry, byte[]> createFunctionDebugBytecode(int functionIndex, EconomicMap<Integer, Integer> offsetToLineIndexMap) {
34093413
final RuntimeBytecodeGen bytecode = new RuntimeBytecodeGen();
34103414
final int codeEntryIndex = functionIndex - module.numImportedFunctions();
34113415
final CodeEntry codeEntry = BytecodeParser.readCodeEntry(module, module.bytecode(), codeEntryIndex);
34123416
offset = module.functionSourceCodeInstructionOffset(functionIndex);
34133417
final int endOffset = module.functionSourceCodeEndOffset(functionIndex);
3414-
readFunction(functionIndex, codeEntry.localTypes(), codeEntry.resultTypes(), endOffset, true, bytecode, codeEntryIndex, offsetToLineIndexMap);
3415-
return bytecode.toArray();
3418+
final CodeEntry result = readFunction(functionIndex, codeEntry.localTypes(), codeEntry.resultTypes(), endOffset, true, bytecode, codeEntryIndex, offsetToLineIndexMap);
3419+
return Pair.create(result, bytecode.toArray());
34163420
}
34173421
}

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmCodeEntry.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,15 @@ public final class WasmCodeEntry {
5454
private final int numLocals;
5555
private final int resultCount;
5656
private final boolean usesMemoryZero;
57-
private final int exceptionTableOffset;
5857

59-
public WasmCodeEntry(WasmFunction function, byte[] bytecode, byte[] localTypes, byte[] resultTypes, boolean usesMemoryZero, int exceptionTableOffset) {
58+
public WasmCodeEntry(WasmFunction function, byte[] bytecode, byte[] localTypes, byte[] resultTypes, boolean usesMemoryZero) {
6059
this.function = function;
6160
this.bytecode = bytecode;
6261
this.localTypes = localTypes;
6362
this.numLocals = localTypes.length;
6463
this.resultTypes = resultTypes;
6564
this.resultCount = resultTypes.length;
6665
this.usesMemoryZero = usesMemoryZero;
67-
this.exceptionTableOffset = exceptionTableOffset;
6866
}
6967

7068
public WasmFunction function() {
@@ -107,10 +105,6 @@ public boolean usesMemoryZero() {
107105
return usesMemoryZero;
108106
}
109107

110-
public int exceptionTableOffset() {
111-
return exceptionTableOffset;
112-
}
113-
114108
@Override
115109
public String toString() {
116110
return "wasm-code-entry:" + functionIndex();

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmInstantiator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ private CallTarget instantiateCodeEntry(WasmStore store, WasmModule module, Wasm
527527
}
528528

529529
private WasmFunctionRootNode instantiateCodeEntryRootNode(WasmStore store, WasmModule module, CodeEntry codeEntry, WasmFunction function) {
530-
final WasmCodeEntry wasmCodeEntry = new WasmCodeEntry(function, module.bytecode(), codeEntry.localTypes(), codeEntry.resultTypes(), codeEntry.usesMemoryZero(),
531-
codeEntry.exceptionTableOffset());
530+
final WasmCodeEntry wasmCodeEntry = new WasmCodeEntry(function, module.bytecode(), codeEntry.localTypes(), codeEntry.resultTypes(), codeEntry.usesMemoryZero());
532531
final FrameDescriptor frameDescriptor = createFrameDescriptor(codeEntry.localTypes(), codeEntry.maxStackSize());
533532
final Node[] callNodes = setupCallNodes(module, codeEntry);
534-
final WasmFixedMemoryImplFunctionNode functionNode = WasmFixedMemoryImplFunctionNode.create(module, wasmCodeEntry, codeEntry.bytecodeStartOffset(), codeEntry.bytecodeEndOffset(), callNodes);
533+
final WasmFixedMemoryImplFunctionNode functionNode = WasmFixedMemoryImplFunctionNode.create(module, wasmCodeEntry, codeEntry.bytecodeStartOffset(), codeEntry.bytecodeEndOffset(),
534+
codeEntry.exceptionTableOffset(), callNodes);
535535
final WasmFunctionRootNode rootNode;
536536
if (store.getContextOptions().memoryOverheadMode()) {
537537
rootNode = new WasmMemoryOverheadModeFunctionRootNode(language, frameDescriptor, module, functionNode, wasmCodeEntry);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,24 @@ public abstract class WasmFixedMemoryImplFunctionNode extends Node {
7070
private final WasmCodeEntry codeEntry;
7171
private final int bytecodeStartOffset;
7272
private final int bytecodeEndOffset;
73+
private final int exceptionTableOffset;
7374
private final Node[] callNodes;
7475

7576
private static final WasmFunctionBaseNode[] EMPTY_FUNCTION_BASE_NODES = new WasmFunctionBaseNode[0];
7677

7778
@Children private WasmFunctionBaseNode[] functionBaseNodes = EMPTY_FUNCTION_BASE_NODES;
7879

79-
protected WasmFixedMemoryImplFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, Node[] callNodes) {
80+
protected WasmFixedMemoryImplFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, int exceptionTableOffset, Node[] callNodes) {
8081
this.module = module;
8182
this.codeEntry = codeEntry;
8283
this.bytecodeStartOffset = bytecodeStartOffset;
8384
this.bytecodeEndOffset = bytecodeEndOffset;
85+
this.exceptionTableOffset = exceptionTableOffset;
8486
this.callNodes = callNodes;
8587
}
8688

87-
public static WasmFixedMemoryImplFunctionNode create(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, Node[] callNodes) {
88-
return WasmFixedMemoryImplFunctionNodeGen.create(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, callNodes);
89+
public static WasmFixedMemoryImplFunctionNode create(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, int exceptionTableOffset, Node[] callNodes) {
90+
return WasmFixedMemoryImplFunctionNodeGen.create(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, exceptionTableOffset, callNodes);
8991
}
9092

9193
@Specialization(guards = {"memoryCount() == 1"}, limit = "3")
@@ -115,7 +117,7 @@ protected int memoryCount() {
115117
@NeverDefault
116118
protected WasmFunctionBaseNode createSpecializedFunctionNode(WasmMemoryLibrary[] memoryLibs) {
117119
CompilerAsserts.neverPartOfCompilation();
118-
WasmInstrumentableFunctionNode instrumentableFunctionNode = new WasmInstrumentableFunctionNode(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, callNodes, memoryLibs);
120+
var instrumentableFunctionNode = new WasmInstrumentableFunctionNode(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, exceptionTableOffset, callNodes, memoryLibs);
119121
WasmFunctionBaseNode baseNode = new WasmFunctionBaseNode(instrumentableFunctionNode);
120122
functionBaseNodes = Arrays.copyOf(functionBaseNodes, functionBaseNodes.length + 1);
121123
functionBaseNodes[functionBaseNodes.length - 1] = insert(baseNode);
@@ -130,7 +132,7 @@ protected WasmFunctionBaseNode createDispatchedFunctionNode() {
130132
for (int memoryIndex = 0; memoryIndex < module.memoryCount(); memoryIndex++) {
131133
memoryLibs[memoryIndex] = insert(WasmMemoryLibrary.getFactory().createDispatched(3));
132134
}
133-
WasmInstrumentableFunctionNode instrumentableFunctionNode = new WasmInstrumentableFunctionNode(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, callNodes, memoryLibs);
135+
var instrumentableFunctionNode = new WasmInstrumentableFunctionNode(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, exceptionTableOffset, callNodes, memoryLibs);
134136
WasmFunctionBaseNode baseNode = new WasmFunctionBaseNode(instrumentableFunctionNode);
135137
functionBaseNodes = new WasmFunctionBaseNode[]{insert(baseNode)};
136138
notifyInserted(instrumentableFunctionNode);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,18 @@ public final class WasmFunctionNode<V128> extends Node implements BytecodeOSRNod
132132

133133
private final int bytecodeStartOffset;
134134
private final int bytecodeEndOffset;
135+
private final int exceptionTableOffset;
135136
@CompilationFinal(dimensions = 1) private final byte[] bytecode;
136137
@CompilationFinal private WasmNotifyFunction notifyFunction;
137138

138139
@Children private final WasmMemoryLibrary[] memoryLibs;
139140

140-
public WasmFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, Node[] callNodes, WasmMemoryLibrary[] memoryLibs) {
141+
public WasmFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, int exceptionTableOffset, Node[] callNodes, WasmMemoryLibrary[] memoryLibs) {
141142
this.module = module;
142143
this.codeEntry = codeEntry;
143144
this.bytecodeStartOffset = bytecodeStartOffset;
144145
this.bytecodeEndOffset = bytecodeEndOffset;
146+
this.exceptionTableOffset = exceptionTableOffset;
145147
this.bytecode = codeEntry.bytecode();
146148
this.callNodes = new Node[callNodes.length];
147149
for (int childIndex = 0; childIndex < callNodes.length; childIndex++) {
@@ -160,11 +162,12 @@ public WasmFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecode
160162
* @param notifyFunction The callback used by {@link Bytecode#NOTIFY} instructions to inform
161163
* instruments about statements in the bytecode
162164
*/
163-
WasmFunctionNode(WasmFunctionNode<V128> node, byte[] bytecode, WasmNotifyFunction notifyFunction) {
165+
WasmFunctionNode(WasmFunctionNode<V128> node, byte[] bytecode, int bytecodeStartOffset, int bytecodeEndOffset, int exceptionTableOffset, WasmNotifyFunction notifyFunction) {
164166
this.module = node.module;
165167
this.codeEntry = node.codeEntry;
166-
this.bytecodeStartOffset = 0;
167-
this.bytecodeEndOffset = bytecode.length;
168+
this.bytecodeStartOffset = bytecodeStartOffset;
169+
this.bytecodeEndOffset = bytecodeEndOffset;
170+
this.exceptionTableOffset = exceptionTableOffset;
168171
this.bytecode = bytecode;
169172
this.callNodes = new Node[node.callNodes.length];
170173
for (int childIndex = 0; childIndex < callNodes.length; childIndex++) {
@@ -1731,7 +1734,7 @@ public Object executeBodyFromOffset(WasmInstance instance, VirtualFrame frame, i
17311734
*/
17321735
final int source = offset;
17331736

1734-
offset = codeEntry.exceptionTableOffset();
1737+
offset = this.exceptionTableOffset;
17351738

17361739
if (offset == 0) {
17371740
// no exception table, directly throw to the next function on the call stack

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.graalvm.wasm.debugging.representation.DebugScopeDisplayValue;
5757
import org.graalvm.wasm.memory.WasmMemory;
5858
import org.graalvm.wasm.memory.WasmMemoryLibrary;
59+
import org.graalvm.wasm.parser.ir.CodeEntry;
5960

6061
import com.oracle.truffle.api.CompilerDirectives;
6162
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -89,10 +90,11 @@ public class WasmInstrumentableFunctionNode extends Node implements Instrumentab
8990

9091
@Child private WasmMemoryLibrary zeroMemoryLib;
9192

92-
public WasmInstrumentableFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, Node[] callNodes, WasmMemoryLibrary[] memoryLibs) {
93+
public WasmInstrumentableFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, int exceptionTableOffset, Node[] callNodes,
94+
WasmMemoryLibrary[] memoryLibs) {
9395
this.module = module;
9496
this.codeEntry = codeEntry;
95-
this.functionNode = new WasmFunctionNode<>(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, callNodes, memoryLibs);
97+
this.functionNode = new WasmFunctionNode<>(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, exceptionTableOffset, callNodes, memoryLibs);
9698
this.functionSourceLocation = module.functionSourceCodeStartOffset(codeEntry.functionIndex());
9799
this.zeroMemoryLib = module.memoryCount() > 0 ? memoryLibs[0] : null;
98100
}
@@ -201,8 +203,11 @@ public InstrumentableNode materializeInstrumentableNodes(Set<Class<? extends Tag
201203
final DebugLineSection debugLineSection = debugFunction.lineMap().getLineIndexMap(functionStartOffset, functionEndOffset);
202204
final WasmInstrumentationSupportNode support = new WasmInstrumentationSupportNode(debugLineSection, sourceSection.getSource());
203205
final BinaryParser binaryParser = new BinaryParser(module, context, module.codeSection());
204-
final byte[] bytecode = binaryParser.createFunctionDebugBytecode(functionIndex, debugLineSection.offsetToLineIndexMap());
205-
final WasmFunctionNode<?> functionNodeDuplicate = new WasmFunctionNode<>(functionNode, bytecode, support::notifyLine);
206+
final var bytecodePair = binaryParser.createFunctionDebugBytecode(functionIndex, debugLineSection.offsetToLineIndexMap());
207+
final CodeEntry bcInfo = bytecodePair.getLeft();
208+
final byte[] bytecode = bytecodePair.getRight();
209+
final WasmFunctionNode<?> functionNodeDuplicate = new WasmFunctionNode<>(functionNode, bytecode,
210+
bcInfo.bytecodeStartOffset(), bcInfo.bytecodeEndOffset(), bcInfo.exceptionTableOffset(), support::notifyLine);
206211
return new WasmInstrumentableFunctionNode(this, functionNodeDuplicate, support);
207212
}
208213
} finally {

0 commit comments

Comments
 (0)