diff --git a/wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java b/wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java index 1ec47e0aa57d..e829f66954ae 100644 --- a/wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java +++ b/wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java @@ -290,7 +290,7 @@ private WasmTestStatus runTestCase(WasmCase testCase) { contextBuilder.option("wasm.StoreConstantsPolicy", WasmTestOptions.STORE_CONSTANTS_POLICY); System.out.println("wasm.StoreConstantsPolicy: " + WasmTestOptions.STORE_CONSTANTS_POLICY); } - + contextBuilder.option("wasm.KeepDataSections", "true"); contextBuilder.option("wasm.Builtins", includedExternalModules()); final String commandLineArgs = testCase.options().getProperty("command-line-args"); if (commandLineArgs != null) { diff --git a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java index fecc680a2c23..ca1ff792c2fb 100644 --- a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java +++ b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java @@ -174,6 +174,11 @@ private void readSymbolSections() { fail(Failure.MALFORMED_SECTION_ID, "invalid section ID: " + sectionID); } assertIntEqual(offset - startOffset, size, String.format("Declared section (0x%02X) size is incorrect", sectionID), Failure.SECTION_SIZE_MISMATCH); + if (sectionID == Section.DATA && !wasmContext.getContextOptions().keepDataSections()) { + removeSection(startOffset, size); + module.setData(data); + offset = startOffset; + } } if (!hasCodeSection) { assertIntEqual(module.numFunctions(), module.importedFunctions().size(), Failure.FUNCTIONS_CODE_INCONSISTENT_LENGTHS); diff --git a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryStreamParser.java b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryStreamParser.java index 23194ea84d86..c87202d68470 100644 --- a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryStreamParser.java +++ b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryStreamParser.java @@ -40,6 +40,7 @@ */ package org.graalvm.wasm; +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; import com.oracle.truffle.api.nodes.ExplodeLoop; import org.graalvm.wasm.constants.GlobalModifier; @@ -337,4 +338,16 @@ public static byte peekLeb128Length(byte[] data, int initialOffset) { } return length; } + + @TruffleBoundary + protected void removeSection(int startOffset, int size) { + final int endOffset = startOffset + size; + final byte[] updatedData = new byte[data.length - size]; + System.arraycopy(data, 0, updatedData, 0, startOffset); + final int remainingLength = data.length - endOffset; + if (remainingLength != 0) { + System.arraycopy(data, endOffset, updatedData, startOffset, remainingLength); + } + data = updatedData; + } } diff --git a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmContextOptions.java b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmContextOptions.java index d17eaf297f57..edc9d737f7c7 100644 --- a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmContextOptions.java +++ b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmContextOptions.java @@ -48,6 +48,7 @@ public class WasmContextOptions { @CompilationFinal private boolean saturatingFloatToInt; @CompilationFinal private boolean signExtensionOps; + @CompilationFinal private boolean keepDataSections; @CompilationFinal private OptionValues optionValues; @@ -63,6 +64,7 @@ public static WasmContextOptions fromOptionValues(OptionValues optionValues) { private void setOptionValues() { this.saturatingFloatToInt = readBooleanOption(WasmOptions.SaturatingFloatToInt); this.signExtensionOps = readBooleanOption(WasmOptions.SignExtensionOps); + this.keepDataSections = readBooleanOption(WasmOptions.KeepDataSections); } private boolean readBooleanOption(OptionKey key) { @@ -77,11 +79,16 @@ public boolean isSignExtensionOps() { return signExtensionOps; } + public boolean keepDataSections() { + return keepDataSections; + } + @Override public int hashCode() { int hash = 5; hash = 53 * hash + (this.saturatingFloatToInt ? 1 : 0); hash = 53 * hash + (this.signExtensionOps ? 1 : 0); + hash = 53 * hash + (this.keepDataSections ? 1 : 0); return hash; } @@ -103,6 +110,9 @@ public boolean equals(Object obj) { if (this.signExtensionOps != other.signExtensionOps) { return false; } + if (this.keepDataSections != other.keepDataSections) { + return false; + } return true; } } diff --git a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmModule.java b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmModule.java index fecd7e875b2c..76818122e53f 100644 --- a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmModule.java +++ b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmModule.java @@ -63,7 +63,7 @@ public final class WasmModule extends SymbolTable implements TruffleObject { @CompilationFinal(dimensions = 1) private CodeEntry[] codeEntries; - @CompilationFinal(dimensions = 1) private final byte[] data; + @CompilationFinal(dimensions = 1) private byte[] data; @CompilationFinal private boolean isParsed; private WasmModule(String name, byte[] data, ModuleLimits limits, Source source) { @@ -113,6 +113,10 @@ public byte[] data() { return data; } + public void setData(byte[] data) { + this.data = data; + } + public Source source() { return source; } diff --git a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmOptions.java b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmOptions.java index c4d7500f8d4d..1186ef95e865 100644 --- a/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmOptions.java +++ b/wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmOptions.java @@ -80,4 +80,7 @@ public enum ConstantsStorePolicy { @Option(help = "Use sign-extension operators", category = OptionCategory.EXPERT, stability = OptionStability.EXPERIMENTAL, usageSyntax = "false|true") // public static final OptionKey SignExtensionOps = new OptionKey<>(false); + + @Option(help = "Prevents the removal of data sections from wasm binaries. This option should only be used for testing.", category = OptionCategory.INTERNAL, stability = OptionStability.EXPERIMENTAL, usageSyntax = "false|true") // + public static final OptionKey KeepDataSections = new OptionKey<>(false); }