Skip to content

Commit

Permalink
Implemented removal of data sections after parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
flohuemer committed May 23, 2022
1 parent 1221b29 commit c4cbb42
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
public class WasmContextOptions {
@CompilationFinal private boolean saturatingFloatToInt;
@CompilationFinal private boolean signExtensionOps;
@CompilationFinal private boolean keepDataSections;

@CompilationFinal private OptionValues optionValues;

Expand All @@ -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<Boolean> key) {
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -113,6 +113,10 @@ public byte[] data() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}

public Source source() {
return source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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<Boolean> KeepDataSections = new OptionKey<>(false);
}

0 comments on commit c4cbb42

Please sign in to comment.