From 52444b565aa3fa5719df3acf490eb11b9337511c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 26 Nov 2023 13:30:56 +0000 Subject: Test WasmBinaryFormatModules directly --- .../structure/WasmBinaryFormatModuleTests.java | 391 --------------------- .../binary/WasmBinaryFormatModulesTests.java | 391 +++++++++++++++++++++ 2 files changed, 391 insertions(+), 391 deletions(-) delete mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java deleted file mode 100644 index 881663e2..00000000 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java +++ /dev/null @@ -1,391 +0,0 @@ -package org.shedlang.compiler.backends.wasm.structure; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; -import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; -import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Const; -import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmInitFunc; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; -import org.shedlang.compiler.backends.wasm.structure.ast.types.*; -import org.shedlang.compiler.testing.TestProcessRunner; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; - -import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump; - -@ExtendWith({SnapshotResolver.class}) -public class WasmBinaryFormatModuleTests { - @Test - public void canSerializeEmptyModule(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY; - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithTypes(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addType(new WasmFuncType(List.of(), List.of())) - .addType(new WasmFuncType( - List.of(WasmValueType.I32, WasmValueType.I64), - List.of(WasmValueType.F32, WasmValueType.F64) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithFunctions(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addFunction(new WasmFunction( - "f1", - List.of( - new WasmLocal("paramA", WasmValueType.F32), - new WasmLocal("paramB", WasmValueType.F64) - ), - List.of( - new WasmLocal("localA", WasmValueType.I64), - new WasmLocal("localB", WasmValueType.F64) - ), - List.of(WasmValueType.I32), - new WasmExpression(List.of(new WasmI32Const(0))) - )) - .addFunction(new WasmFunction( - "f2", - List.of( - new WasmLocal("paramA", WasmValueType.F32) - ), - List.of( - new WasmLocal("localA", WasmValueType.I64) - ), - List.of(WasmValueType.I64), - new WasmExpression(List.of(new WasmI64Const(0))) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithFunctionImports(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addImport(new WasmImport( - "ModuleName1", - "EntityName1", - new WasmImportDescriptor.Func("Identifier1", new WasmFuncType(List.of(), List.of())) - )) - .addImport(new WasmImport( - "ModuleName2", - "EntityName2", - new WasmImportDescriptor.Func("Identifier2", new WasmFuncType(List.of(WasmValueType.I32), List.of())) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addFunction(new WasmFunction( - "f", - List.of( - new WasmLocal("paramA", WasmValueType.F32) - ), - List.of(), - List.of(WasmValueType.I32), - new WasmExpression(List.of(new WasmI32Const(0))) - )) - .addImport(new WasmImport( - "ModuleName1", - "EntityName1", - new WasmImportDescriptor.Func("Identifier1", new WasmFuncType(List.of(), List.of())) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithTableImports(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addImport(new WasmImport( - "ModuleName1", - "EntityName1", - new WasmImportDescriptor.Table(new WasmTableType( - WasmReferenceType.EXTERN_REF, - WasmLimits.min(1) - )) - )) - .addImport(new WasmImport( - "ModuleName2", - "EntityName2", - new WasmImportDescriptor.Table(new WasmTableType( - WasmReferenceType.FUNC_REF, - WasmLimits.range(2, 5) - )) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithMemoryImports(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addImport(new WasmImport( - "ModuleName1", - "EntityName1", - new WasmImportDescriptor.Memory(new WasmMemoryType( - WasmLimits.min(1) - )) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithGlobalImports(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addImport(new WasmImport( - "ModuleName1", - "EntityName1", - new WasmImportDescriptor.Global( - "Global1", - new WasmGlobalType(WasmValueType.I32, WasmMutability.MUTABLE) - ) - )) - .addImport(new WasmImport( - "ModuleName2", - "EntityName2", - new WasmImportDescriptor.Global( - "Global2", - new WasmGlobalType(WasmValueType.I64, WasmMutability.IMMUTABLE) - ) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithTables(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addTable(new WasmTable(new WasmTableType( - WasmReferenceType.FUNC_REF, - WasmLimits.min(1) - ))) - .addTable(new WasmTable(new WasmTableType( - WasmReferenceType.EXTERN_REF, - WasmLimits.range(2, 3) - ))); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithMemories(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addMemory(new WasmMemory(new WasmMemoryType( - WasmLimits.min(1) - ))); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithGlobals(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addGlobal(new WasmGlobal( - "Global1", - new WasmGlobalType(WasmValueType.I32, WasmMutability.MUTABLE), - new WasmExpression(List.of(new WasmI32Const(7))) - )) - .addGlobal(new WasmGlobal( - "Global2", - new WasmGlobalType(WasmValueType.I64, WasmMutability.IMMUTABLE), - new WasmExpression(List.of(new WasmI64Const(9))) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithElementSegments(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addFunction(new WasmFunction( - "f1", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )) - .addFunction(new WasmFunction( - "f2", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )) - .addFunction(new WasmFunction( - "f3", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )) - .addTable(new WasmTable(new WasmTableType( - WasmReferenceType.FUNC_REF, - WasmLimits.min(1) - ))) - .addElementSegment(WasmElementSegment.funcIndicesActive(List.of( - new WasmFuncIndex(0), - new WasmFuncIndex(2) - ))); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithDataSegments(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addMemory(new WasmMemory(new WasmMemoryType(WasmLimits.min(10)))) - .addDataSegment(new WasmDataSegment( - ".data", - new byte[] { 1, 2, 3, 4 }, - new WasmExpression(List.of(new WasmI32Const(0))), - WasmAlignment.ALIGN_32_BIT, - List.of( - new WasmDataElement( - "data1", - 0, - 2 - ), - new WasmDataElement( - "data2", - 2, - 2 - ) - ) - )) - .addDataSegment(new WasmDataSegment( - ".rodata", - new byte[] { 4, 5, 6, 7 }, - new WasmExpression(List.of(new WasmI32Const(4))), - WasmAlignment.NONE, - List.of( - new WasmDataElement( - "rodata1", - 0, - 4 - ) - ) - )); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithInitFuncs(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addFunction(new WasmFunction( - "f1", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )) - .addFunction(new WasmFunction( - "f2", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )) - .addFunction(new WasmFunction( - "f3", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )) - .addInitFunc(new WasmInitFunc(0, new WasmFuncName("f3"))) - .addInitFunc(new WasmInitFunc(4, new WasmFuncName("f1"))); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - @Test - public void canSerializeModuleWithExceptionTags(Snapshotter snapshotter) throws IOException, InterruptedException { - var module = WasmModule.EMPTY - .addTag( - WasmTagType.EXCEPTION, - "Error", - List.of(WasmValueType.I32, WasmValueType.I64) - ) - .addTag( - WasmTagType.EXCEPTION, - "ErrorDifferentFieldTypes", - List.of(WasmValueType.I64) - ) - .addTag( - WasmTagType.EXCEPTION, - "ErrorSameFieldTypes", - List.of(WasmValueType.I32, WasmValueType.I64) - ); - - var result = serializeModule(module); - - snapshotter.assertSnapshot(result); - } - - private String serializeModule(WasmModule module) throws IOException, InterruptedException { - Files.createDirectories(Path.of("tests-tmp")); - var temporaryDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmBinaryFormat"); - var modulePath = temporaryDirectory.resolve("module.wasm"); - System.out.println(modulePath); - - try (var outputStream = new FileOutputStream(modulePath.toFile())) { - WasmBinaryFormat.serializeModule(module, outputStream); - } - - validate(modulePath); - - return objdump(modulePath); - } - - private void validate(Path path) throws IOException, InterruptedException { - TestProcessRunner.runCaptureOutput(List.of("wasm-validate", "--enable-exceptions", path.toString())) - .throwOnError(); - } -} diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java new file mode 100644 index 00000000..afc44c79 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java @@ -0,0 +1,391 @@ +package org.shedlang.compiler.backends.wasm.structure.binary; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Const; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmInitFunc; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; +import org.shedlang.compiler.backends.wasm.structure.ast.types.*; +import org.shedlang.compiler.testing.TestProcessRunner; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump; + +@ExtendWith({SnapshotResolver.class}) +public class WasmBinaryFormatModulesTests { + @Test + public void canSerializeEmptyModule(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY; + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithTypes(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addType(new WasmFuncType(List.of(), List.of())) + .addType(new WasmFuncType( + List.of(WasmValueType.I32, WasmValueType.I64), + List.of(WasmValueType.F32, WasmValueType.F64) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithFunctions(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addFunction(new WasmFunction( + "f1", + List.of( + new WasmLocal("paramA", WasmValueType.F32), + new WasmLocal("paramB", WasmValueType.F64) + ), + List.of( + new WasmLocal("localA", WasmValueType.I64), + new WasmLocal("localB", WasmValueType.F64) + ), + List.of(WasmValueType.I32), + new WasmExpression(List.of(new WasmI32Const(0))) + )) + .addFunction(new WasmFunction( + "f2", + List.of( + new WasmLocal("paramA", WasmValueType.F32) + ), + List.of( + new WasmLocal("localA", WasmValueType.I64) + ), + List.of(WasmValueType.I64), + new WasmExpression(List.of(new WasmI64Const(0))) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithFunctionImports(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addImport(new WasmImport( + "ModuleName1", + "EntityName1", + new WasmImportDescriptor.Func("Identifier1", new WasmFuncType(List.of(), List.of())) + )) + .addImport(new WasmImport( + "ModuleName2", + "EntityName2", + new WasmImportDescriptor.Func("Identifier2", new WasmFuncType(List.of(WasmValueType.I32), List.of())) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addFunction(new WasmFunction( + "f", + List.of( + new WasmLocal("paramA", WasmValueType.F32) + ), + List.of(), + List.of(WasmValueType.I32), + new WasmExpression(List.of(new WasmI32Const(0))) + )) + .addImport(new WasmImport( + "ModuleName1", + "EntityName1", + new WasmImportDescriptor.Func("Identifier1", new WasmFuncType(List.of(), List.of())) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithTableImports(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addImport(new WasmImport( + "ModuleName1", + "EntityName1", + new WasmImportDescriptor.Table(new WasmTableType( + WasmReferenceType.EXTERN_REF, + WasmLimits.min(1) + )) + )) + .addImport(new WasmImport( + "ModuleName2", + "EntityName2", + new WasmImportDescriptor.Table(new WasmTableType( + WasmReferenceType.FUNC_REF, + WasmLimits.range(2, 5) + )) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithMemoryImports(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addImport(new WasmImport( + "ModuleName1", + "EntityName1", + new WasmImportDescriptor.Memory(new WasmMemoryType( + WasmLimits.min(1) + )) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithGlobalImports(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addImport(new WasmImport( + "ModuleName1", + "EntityName1", + new WasmImportDescriptor.Global( + "Global1", + new WasmGlobalType(WasmValueType.I32, WasmMutability.MUTABLE) + ) + )) + .addImport(new WasmImport( + "ModuleName2", + "EntityName2", + new WasmImportDescriptor.Global( + "Global2", + new WasmGlobalType(WasmValueType.I64, WasmMutability.IMMUTABLE) + ) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithTables(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addTable(new WasmTable(new WasmTableType( + WasmReferenceType.FUNC_REF, + WasmLimits.min(1) + ))) + .addTable(new WasmTable(new WasmTableType( + WasmReferenceType.EXTERN_REF, + WasmLimits.range(2, 3) + ))); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithMemories(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addMemory(new WasmMemory(new WasmMemoryType( + WasmLimits.min(1) + ))); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithGlobals(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addGlobal(new WasmGlobal( + "Global1", + new WasmGlobalType(WasmValueType.I32, WasmMutability.MUTABLE), + new WasmExpression(List.of(new WasmI32Const(7))) + )) + .addGlobal(new WasmGlobal( + "Global2", + new WasmGlobalType(WasmValueType.I64, WasmMutability.IMMUTABLE), + new WasmExpression(List.of(new WasmI64Const(9))) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithElementSegments(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addFunction(new WasmFunction( + "f1", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of()) + )) + .addFunction(new WasmFunction( + "f2", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of()) + )) + .addFunction(new WasmFunction( + "f3", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of()) + )) + .addTable(new WasmTable(new WasmTableType( + WasmReferenceType.FUNC_REF, + WasmLimits.min(1) + ))) + .addElementSegment(WasmElementSegment.funcIndicesActive(List.of( + new WasmFuncIndex(0), + new WasmFuncIndex(2) + ))); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithDataSegments(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addMemory(new WasmMemory(new WasmMemoryType(WasmLimits.min(10)))) + .addDataSegment(new WasmDataSegment( + ".data", + new byte[] { 1, 2, 3, 4 }, + new WasmExpression(List.of(new WasmI32Const(0))), + WasmAlignment.ALIGN_32_BIT, + List.of( + new WasmDataElement( + "data1", + 0, + 2 + ), + new WasmDataElement( + "data2", + 2, + 2 + ) + ) + )) + .addDataSegment(new WasmDataSegment( + ".rodata", + new byte[] { 4, 5, 6, 7 }, + new WasmExpression(List.of(new WasmI32Const(4))), + WasmAlignment.NONE, + List.of( + new WasmDataElement( + "rodata1", + 0, + 4 + ) + ) + )); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithInitFuncs(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addFunction(new WasmFunction( + "f1", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of()) + )) + .addFunction(new WasmFunction( + "f2", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of()) + )) + .addFunction(new WasmFunction( + "f3", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of()) + )) + .addInitFunc(new WasmInitFunc(0, new WasmFuncName("f3"))) + .addInitFunc(new WasmInitFunc(4, new WasmFuncName("f1"))); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + @Test + public void canSerializeModuleWithExceptionTags(Snapshotter snapshotter) throws IOException, InterruptedException { + var module = WasmModule.EMPTY + .addTag( + WasmTagType.EXCEPTION, + "Error", + List.of(WasmValueType.I32, WasmValueType.I64) + ) + .addTag( + WasmTagType.EXCEPTION, + "ErrorDifferentFieldTypes", + List.of(WasmValueType.I64) + ) + .addTag( + WasmTagType.EXCEPTION, + "ErrorSameFieldTypes", + List.of(WasmValueType.I32, WasmValueType.I64) + ); + + var result = serializeModule(module); + + snapshotter.assertSnapshot(result); + } + + private String serializeModule(WasmModule module) throws IOException, InterruptedException { + Files.createDirectories(Path.of("tests-tmp")); + var temporaryDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmBinaryFormat"); + var modulePath = temporaryDirectory.resolve("module.wasm"); + System.out.println(modulePath); + + try (var outputStream = new FileOutputStream(modulePath.toFile())) { + WasmBinaryFormatModules.serializeModule(module, outputStream); + } + + validate(modulePath); + + return objdump(modulePath); + } + + private void validate(Path path) throws IOException, InterruptedException { + TestProcessRunner.runCaptureOutput(List.of("wasm-validate", "--enable-exceptions", path.toString())) + .throwOnError(); + } +} -- cgit v1.2.3