Skip to content

Commit

Permalink
Prevent recursive initialization, and fix topological sort during lin…
Browse files Browse the repository at this point in the history
…king.
  • Loading branch information
axel22 committed Dec 8, 2019
1 parent 68833d1 commit 51f4430
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
zero-memory = false
interpreter-iterations = 1
sync-noinline-iterations = 1
sync-inline-iterations = 1
async-iterations = 0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
int 0
int 12
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
;; SOFTWARE.
;;
(module
(type (;0;) (func))
(import "memory" "memory" (memory (;0;) 4))
(export "memory" (memory 0))
(func $init (type 0)
i32.const 0
i32.const 12
i32.store
)
(start $init)
)
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public boolean equals(Object object) {
return false;
}
final ImportDescriptor that = (ImportDescriptor) object;
return that.moduleName.equals(this.memberName) && that.memberName.equals(this.memberName);
return that.moduleName.equals(this.moduleName) && that.memberName.equals(this.memberName);
}
}
32 changes: 26 additions & 6 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@
import java.util.function.Consumer;

public class Linker {
private enum LinkState {
notLinked,
inProgress,
linked
}

private final WasmLanguage language;
private final ResolutionDag resolutionDag;
private @CompilerDirectives.CompilationFinal boolean linked;
private @CompilerDirectives.CompilationFinal LinkState linkState;

public Linker(WasmLanguage language) {
this.language = language;
this.resolutionDag = new ResolutionDag();
this.linkState = LinkState.notLinked;
}

// TODO: Many of the following methods should work on all the modules in the context, instead of
Expand All @@ -79,7 +86,7 @@ public void tryLink() {
// compilation, and this check will fold away.
// If the code is compiled synchronously, then this check will persist in the compiled code.
// We nevertheless invalidate the compiled code that reaches this point.
if (!linked) {
if (linkState == LinkState.notLinked) {
// TODO: Once we support multi-threading, add adequate synchronization here.
tryLinkOutsidePartialEvaluation();
CompilerDirectives.transferToInterpreterAndInvalidate();
Expand All @@ -90,7 +97,8 @@ public void tryLink() {
private void tryLinkOutsidePartialEvaluation() {
// Some Truffle configurations allow that the code gets compiled before executing the code.
// We therefore check the link state again.
if (!linked) {
if (linkState == LinkState.notLinked) {
linkState = LinkState.inProgress;
Map<String, WasmModule> modules = WasmContext.getCurrent().modules();
for (WasmModule module : modules.values()) {
linkFunctions(module);
Expand All @@ -106,7 +114,7 @@ private void tryLinkOutsidePartialEvaluation() {
}
}
resolutionDag.clear();
linked = true;
linkState = LinkState.linked;
}
}

Expand Down Expand Up @@ -364,6 +372,11 @@ static class DataDecl extends Decl {
this.dataSectionId = dataSectionId;
}

@Override
public String toString() {
return String.format("data %d in %s", dataSectionId, moduleName);
}

@Override
public int hashCode() {
return moduleName.hashCode() ^ dataSectionId;
Expand All @@ -380,13 +393,20 @@ public boolean equals(Object object) {
}

static class Resolver {
final Decl element;
final Decl[] dependencies;
final Runnable action;

Resolver(Decl[] dependencies, Runnable action) {
Resolver(Decl element, Decl[] dependencies, Runnable action) {
this.element = element;
this.dependencies = dependencies;
this.action = action;
}

@Override
public String toString() {
return "Resolver(" + element + ")";
}
}

private final Map<Decl, Resolver> resolutions;
Expand All @@ -396,7 +416,7 @@ static class Resolver {
}

void resolveLater(Decl element, Decl[] dependencies, Runnable action) {
resolutions.put(element, new Resolver(dependencies, action));
resolutions.put(element, new Resolver(element, dependencies, action));
}

void clear() {
Expand Down

0 comments on commit 51f4430

Please sign in to comment.