From 2242b12c09562b0227c8ee875750930f42b06b50 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 21 Dec 2023 16:37:44 +0000 Subject: Check that we're not using placeholder unrelocatable values --- .../wasm/structure/binary/WasmBinaryWriter.java | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java index bb931c65..0c35db7e 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriter.java @@ -60,7 +60,7 @@ public class WasmBinaryWriter { var funcIndex = indexTable.funcIndex(funcName); writeRelocatableIndex( WasmRelocationType.FUNCTION_INDEX_LEB, - funcIndex.value(), + OptionalInt.of(funcIndex.value()), new WasmRelocatableIndex.FuncIndex(funcName) ); } @@ -69,7 +69,7 @@ public class WasmBinaryWriter { var globalIndex = indexTable.globalIndex(globalName); writeRelocatableIndex( WasmRelocationType.GLOBAL_INDEX_LEB, - globalIndex.value(), + OptionalInt.of(globalIndex.value()), new WasmRelocatableIndex.GlobalIndex(globalName) ); } @@ -77,8 +77,7 @@ public class WasmBinaryWriter { public void writeMemoryAddressConst(WasmMemoryAddress index) throws IOException { writeRelocatableIndex( WasmRelocationType.MEMORY_ADDR_SLEB, - // TODO: use actual value? Or throw if not in relocatable section? - 0, + OptionalInt.empty(), new WasmRelocatableIndex.MemoryAddress(index) ); } @@ -90,8 +89,7 @@ public class WasmBinaryWriter { public void writeTableElementIndexConst(WasmFuncName funcName) throws IOException { writeRelocatableIndex( WasmRelocationType.TABLE_INDEX_SLEB, - // TODO: use actual value? Or throw if not in relocatable section? - 0, + OptionalInt.empty(), new WasmRelocatableIndex.TableElementIndex(funcName) ); } @@ -100,7 +98,7 @@ public class WasmBinaryWriter { var tagIndex = indexTable.tagIndex(tag); writeRelocatableIndex( WasmRelocationType.TAG_INDEX_LEB, - tagIndex.value(), + OptionalInt.of(tagIndex.value()), new WasmRelocatableIndex.TagIndex(tag) ); } @@ -109,14 +107,14 @@ public class WasmBinaryWriter { var typeIndex = indexTable.typeIndex(funcType); writeRelocatableIndex( WasmRelocationType.TYPE_INDEX_LEB, - typeIndex.value(), + OptionalInt.of(typeIndex.value()), new WasmRelocatableIndex.TypeIndex(funcType) ); } private void writeRelocatableIndex( WasmRelocationType relocationType, - int unrelocatedIndex, + OptionalInt unrelocatedIndex, WasmRelocatableIndex index ) throws IOException { // TODO: handle custom sections? @@ -125,11 +123,12 @@ public class WasmBinaryWriter { .orElse(false); if (isRelocatable) { + var value = unrelocatedIndex.orElse(0); var sectionContentsOffset = writer.position() - sectionContentsStart; if (relocationType.isSigned()) { - writePaddedSignedInt32(unrelocatedIndex); + writePaddedSignedInt32(value); } else { - writePaddedUnsignedInt32(unrelocatedIndex); + writePaddedUnsignedInt32(value); } if (!relocationEntries.containsKey(sectionIndex)) { @@ -141,11 +140,13 @@ public class WasmBinaryWriter { sectionContentsOffset, index )); + } else if (unrelocatedIndex.isEmpty()) { + throw new IllegalStateException("cannot write memory address outside of relocatable section"); } else { if (relocationType.isSigned()) { - writeSignedInt32(unrelocatedIndex); + writeSignedInt32(unrelocatedIndex.getAsInt()); } else { - writeUnsignedInt32(unrelocatedIndex); + writeUnsignedInt32(unrelocatedIndex.getAsInt()); } } } -- cgit v1.2.3