Closed GreenGood closed 3 years ago
Does it work in a browser like Chrome?
Yes, ive downloaded it from website. May be this error due the fact that i don
t passed imported functions?
I`ve already compiled it with https://github.com/migueldeicaza/WasmerSharp and there were error with missing imported functions.
Thanks for the follow-up!
Recent releases of WebAssembly for .NET don't require imported functions to be supplied until after compilation, so it's definitely an issue with the compile process. Some of the spec tests are also encountering a similar error, so it's nice to see something "real world" triggering it to help me prioritize my efforts.
I'll take a closer look when I have time this weekend.
The issue is an unreachable
immediately before the end
on a function that's expected to return something. The value stack is empty when end
is reached but it's also not reachable so who cares, right? The problem is WebAssembly for .NET expects a rational stack at all times so it doesn't like this condition.
I'm trying to think of an uncomplicated way to solve this...
There is a band-aid fix you can do if you don't mind cloning this project's repo and editing its code: In End.cs, you can paste this around line 35, after the check for OpCode == return:
if (context.Previous == OpCode.Unreachable)
{
context.Emit(OpCodes.Ret);
return;
}
This gets a successful compile with the WASM file you've provided. You'll still need to satisfy its imports to get it to run, and you may run into other issues... but it's a step in the right direction.
The above fix isn't enough to get it to pass the official spec tests around this behavior, which test a variety of edge cases. I think I'd want to fix those before I post an official update to the library.
First of all, thank you for this great project, I have found it while researching a possibility to transpile many supported languages but mainly C/C++ to .NET due to similarities between WASM and IL. It will hopefully be soon compatible with basic WASM spec to allow us do some previously thought to be impossible stuff, mainly significantly improve porting!
I have also been testing some real-world WASM files, particularly a networking library (with dependency on libprotobuf), where I have gone trough modifiying the library and libprotobuf code to replace any thread/atomic references with extern imports, that free the code from --shared-memory feature which requires even more advanced WASM spec (not currently supported by this project).
While trying to load this WASM (after changes above now with just basic WASM spec features and created using newest Emscripten) I am however faced with exception in WebAssembly.Runtime.Compile.FromBinary, where it seems that a value is expected to be returned from function, but there is none on stack.
I have tried to naively fix the Emit code, but the I get InvalidProgramException after creating an compiled instance and trying to validate the compilation result. This could be due to my hack-fix above, or some other incompatabilities in the WASM file. (I have provided stub imports in this phase, so the code compiles using WebAssembly.Runtime.Compile, so I do not expect the code to actually function at the moment.)
I have attached the WASM in case @RyanLamansky would like to quickly take a look at what could be the parsing and/or compilation issue.
Thanks and looking forward to updates!
I am not that familiar with WASM spec, but it seems that the compilation issue I have run into is caused by functions that return a value and contain a "unreachable" instruction followed by "end". The compilator assumes that any "end" instruction not preceeded "return" in these functions should return a value. This may be fixed by also checking for "unreachable" as prev instruction in End.Compile().
However when I do this, the exceptions during compilation dissapear but I still get InvalidProgramException while trying to JIT/execute the generated assembly. I could provide the neccessary stubs/imports if needed.
UPDATE: I have been looking at the InvalidProgramException issue, I think I have found the problem. MemoryReadInstruction emits "Unaligned" opcode, but according to MS documentation the only allowed value for the parameter are 1, 2 and 4. In some cases, the compiler emits value 8, which causes JIT to complain with the exception.
@yself Tracking reachability of instructions wasn't something I expected to have to to do when I started this project, so there aren't any data models in place to help support it. It's fixable, but it requires more time than I have available in the near future.
I didn't quite understand how the alignment options work, so I'm not surprised that it does it wrong. If you can make a PR with the fix and at least one unit test, I will merge it 🙂
I retested your WASM file with the current version of WebAssembly for .NET and the compiler was able to process it. So, maybe it works now? It has a lot of imports that need to be satisfied.
Let me know if you're still having issues.
Hello. I have wasm assembly that doesn`t compile into instance
var instance = Compile.FromBinary<dynamic>(Environment.CurrentDirectory + "\\wasm.wasm")(new ImportDictionary())
This line throwing error
WebAssembly.Runtime.StackSizeIncorrectException: 'End requires at 1 value on the stack, found 0.'
wasm.zip