Skip to content

Commit

Permalink
Ensure that data-section-based memory reinitialization works correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
axel22 committed Aug 4, 2020
1 parent 6afa71c commit ac0f9e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void readSymbolSections() {
skipCodeSection();
break;
case Section.DATA:
readDataSection(null);
readDataSection(null, null);
break;
default:
Assert.fail("invalid section ID: " + sectionID);
Expand Down Expand Up @@ -1134,7 +1134,7 @@ private void readGlobalSection() {
}
}

private void readDataSection(WasmInstance linkedInstance) {
private void readDataSection(WasmContext linkedContext, WasmInstance linkedInstance) {
int numDataSegments = readVectorLength();
for (int dataSegmentId = 0; dataSegmentId != numDataSegments; ++dataSegmentId) {
int memIndex = readUnsignedInt32();
Expand Down Expand Up @@ -1166,6 +1166,11 @@ private void readDataSection(WasmInstance linkedInstance) {
int byteLength = readVectorLength();

if (linkedInstance != null) {
if (offsetGlobalIndex != -1) {
int offsetGlobalAddress = linkedInstance.globalAddress(offsetGlobalIndex);
offsetAddress = linkedContext.globals().loadAsInt(offsetGlobalAddress);
}

// Reading of the data segment is called after linking, so initialize the memory
// directly.
final WasmMemory memory = linkedInstance.memory();
Expand Down Expand Up @@ -1493,13 +1498,13 @@ void resetGlobalState(WasmContext context, WasmInstance instance) {
}
}

void resetMemoryState(WasmInstance instance, boolean zeroMemory) {
void resetMemoryState(WasmContext context, WasmInstance instance, boolean zeroMemory) {
final WasmMemory memory = instance.memory();
if (memory != null && zeroMemory) {
memory.clear();
}
if (tryJumpToSection(Section.DATA)) {
readDataSection(instance);
readDataSection(context, instance);
}
}
}
3 changes: 2 additions & 1 deletion wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static void assignTypeEquivalenceClasses() {
public void resetModuleState(WasmContext context, WasmInstance instance, byte[] data, boolean zeroMemory) {
final BinaryParser reader = new BinaryParser(language, instance.module(), data);
reader.resetGlobalState(context, instance);
reader.resetMemoryState(instance, zeroMemory);
reader.resetMemoryState(context, instance, zeroMemory);
}

void resolveGlobalImport(WasmContext context, WasmInstance instance, ImportDescriptor importDescriptor, int globalIndex, byte valueType, byte mutability) {
Expand Down Expand Up @@ -208,6 +208,7 @@ void resolveGlobalImport(WasmContext context, WasmInstance instance, ImportDescr
}

int address = importedInstance.globalAddress(exportedGlobalIndex);
System.out.println("Yo! " + globalIndex + " -> " + address);
instance.setGlobalAddress(globalIndex, address);
};
final ImportGlobalSym importGlobalSym = new ImportGlobalSym(instance.name(), importDescriptor);
Expand Down

0 comments on commit ac0f9e1

Please sign in to comment.