-
Notifications
You must be signed in to change notification settings - Fork 19
Issue 139 #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
andreaTP
wants to merge
3
commits into
bytecodealliance:main
Choose a base branch
from
andreaTP:issue-139
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Issue 139 #141
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
919 changes: 529 additions & 390 deletions
919
compiler/src/main/java/run/endive/compiler/internal/EmitterMap.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
machine-tests/src/test/java/run/endive/testing/CallManyParamsTryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package run.endive.testing; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.util.function.Function; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import run.endive.compiler.MachineFactoryCompiler; | ||
| import run.endive.corpus.CorpusResources; | ||
| import run.endive.runtime.Instance; | ||
| import run.endive.runtime.InterpreterMachine; | ||
| import run.endive.wasm.Parser; | ||
| import run.endive.wasm.WasmModule; | ||
|
|
||
| /** | ||
| * Tests that a call to a function with >253 params inside a try_table | ||
| * does not corrupt values below the try scope. | ||
| * Regression test for a latent bug in computeMaxTempSlots (CALL missing). | ||
| */ | ||
| public class CallManyParamsTryTest { | ||
|
|
||
| private static final WasmModule MODULE = | ||
| Parser.parse(CorpusResources.getResource("compiled/call_many_params_try.wat.wasm")); | ||
|
|
||
| private static Stream<Arguments> machineImplementations() { | ||
| return Stream.of( | ||
| Arguments.of( | ||
| "interpreter", | ||
| (Function<Instance.Builder, Instance.Builder>) | ||
| (b) -> b.withMachineFactory(InterpreterMachine::new)), | ||
| Arguments.of( | ||
| "compiler", | ||
| (Function<Instance.Builder, Instance.Builder>) | ||
| (b) -> b.withMachineFactory(MachineFactoryCompiler::compile))); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("machineImplementations") | ||
| public void callManyBelowTry( | ||
| String name, Function<Instance.Builder, Instance.Builder> machineInject) { | ||
| var instance = machineInject.apply(Instance.builder(MODULE)).build(); | ||
| assertEquals(42, instance.export("call-many-below-try").apply()[0]); | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
machine-tests/src/test/java/run/endive/testing/KotlinFoldCharsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package run.endive.testing; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
|
||
| import java.util.function.Function; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import run.endive.compiler.MachineFactoryCompiler; | ||
| import run.endive.corpus.CorpusResources; | ||
| import run.endive.runtime.HostFunction; | ||
| import run.endive.runtime.ImportValues; | ||
| import run.endive.runtime.Instance; | ||
| import run.endive.runtime.InterpreterMachine; | ||
| import run.endive.wasm.Parser; | ||
| import run.endive.wasm.WasmModule; | ||
| import run.endive.wasm.types.ExternalType; | ||
| import run.endive.wasm.types.FunctionImport; | ||
| import run.endive.wasm.types.FunctionType; | ||
|
|
||
| /** | ||
| * Reproducer for https://github.com/bytecodealliance/endive/issues/139 | ||
| * | ||
| * A Kotlin/Wasm module traps on the compiler with "null array reference" | ||
| * at array.len in kotlin.String.foldChars, while the interpreter runs it fine. | ||
| */ | ||
| public class KotlinFoldCharsTest { | ||
|
|
||
| private static final WasmModule MODULE = | ||
| Parser.parse(CorpusResources.getResource("compiled/trap.kt.wasm")); | ||
|
|
||
| private static Stream<Arguments> machineImplementations() { | ||
| return Stream.of( | ||
| Arguments.of( | ||
| "interpreter", | ||
| (Function<Instance.Builder, Instance.Builder>) | ||
| (b) -> b.withMachineFactory(InterpreterMachine::new)), | ||
| Arguments.of( | ||
| "compiler", | ||
| (Function<Instance.Builder, Instance.Builder>) | ||
| (b) -> b.withMachineFactory(MachineFactoryCompiler::compile))); | ||
| } | ||
|
|
||
| private static ImportValues buildImports(WasmModule module) { | ||
| ImportValues.Builder imports = ImportValues.builder(); | ||
| for (int i = 0; i < module.importSection().importCount(); i++) { | ||
| if (module.importSection().getImport(i).importType() != ExternalType.FUNCTION) { | ||
| continue; | ||
| } | ||
| FunctionImport imported = (FunctionImport) module.importSection().getImport(i); | ||
| FunctionType type = module.typeSection().getType(imported.typeIndex()); | ||
| imports.addFunction( | ||
| new HostFunction( | ||
| imported.module(), | ||
| imported.name(), | ||
| type, | ||
| (instance, callArgs) -> { | ||
| if (imported.name().equals("call_host")) { | ||
| byte[] reply = "err\nNo such method nope".getBytes(UTF_8); | ||
| instance.memory().write((int) callArgs[2], reply); | ||
| return new long[] {reply.length}; | ||
| } | ||
| return new long[type.returns().size()]; | ||
| })); | ||
| } | ||
| return imports.build(); | ||
| } | ||
|
|
||
| // Issue #139: interpreter succeeds, compiler traps with "null array reference" | ||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("machineImplementations") | ||
| public void kotlinWasmStartDoesNotTrap( | ||
| String name, Function<Instance.Builder, Instance.Builder> machineInject) { | ||
| var instance = | ||
| machineInject | ||
| .apply( | ||
| Instance.builder(MODULE) | ||
| .withImportValues(buildImports(MODULE)) | ||
| .withStart(false)) | ||
| .build(); | ||
|
|
||
| assertDoesNotThrow(() -> instance.export("_start").apply()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a clear indication for developers, we should make sure this doesn't spill to user land in absence of bugs.