Skip to content
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

AS generates invalid .wast #120

Closed
carllocos opened this issue Jul 7, 2023 · 11 comments · Fixed by #122
Closed

AS generates invalid .wast #120

carllocos opened this issue Jul 7, 2023 · 11 comments · Fixed by #122
Assignees
Labels
bug Something isn't working

Comments

@carllocos
Copy link
Contributor

I wrote an AssemnlyScript application (see mirror-led.ts and config.ts in zip) that cannot be compiled to wasm. In particular, wat2wasm fails to compile the obtained wast file to wasm due to several type errors. Some of the errors that I get are:upload.wast:612:6: error: type mismatch in if true branch, expected [i32] but got [], and upload.wast:7658:4: error: type mismatch at end of function, expected [] but got [i32, i32]. All the files generated by asc can be found in the directory output of the zip.

This compilation issue comes as a surprise because the asc generates alongside the Wast file also a wasm file indicating that no type error occurred in the process. So this may mean that our wat2wasm is behind some language features.

This type error issue only manifest when compiling via asc without any optimisation. Since we also want to debug without optimisation I will have to further investigate this issue.

src_files.zip

@carllocos carllocos added the bug Something isn't working label Jul 7, 2023
@carllocos carllocos self-assigned this Jul 7, 2023
@tolauwae
Copy link
Member

tolauwae commented Jul 7, 2023

It works on my machine with AssemblyScript 0.27.5. I used the template and ran:

npm install
npm run build
wat2wasm bin/main.debug.wast --no-canonicalize-leb128s --disable-bulk-memory -v

@carllocos
Copy link
Contributor Author

@tolauwae did you also use no optimisation? (i.e., remove the -O3s from the asc command) because I also have version 0.27.5 and with optimisation the wasm gets generated but without optimisation, I get the type error.

@tolauwae
Copy link
Member

tolauwae commented Jul 7, 2023

I used the debug profile of the template which doesn't optimize. And it works. What is your exact asc command?

@carllocos
Copy link
Contributor Author

carllocos commented Jul 7, 2023

@tolauwae

full command

 asc path/to/src.ts --exportTable --disable bulk-memory --sourceMap --debug --outFile tmp/upload.wasm --textFile tmp/upload.wast`

Something crucial I forgot to mention
The type error occurring here above may occur unbeknownst to the user. For some reason, the plugin does not automatically stop even if the wat2wasm fails and thus proceeds with the next step i.e., flashing the upload.wasm to the MCU. Surprisingly this will succeed and the reason is that the plugin will search for a tmp/upload.wasm to flash which will be present in the tmp/ directory. This is because asc also generated besides the wast file a upload.wasm file that is put in the tmp/ dir. In other words, from the user perspective flashing will be successful since the wasm of asc is used. The only way I noticed the wat2wasm failure is by putting a breakpoint on this line. The stderror contained the type errors that I showed here above.

I also tried to use the template since it may not have the compilation error but I fail to properly configure it. Can you please confirm whether the following steps are correct:

  • run npm install and npm run build in the root directory of the template
  • add a .vscode/launch.json file into the root directory of the template
  • change the program key of launch.json to point to the main.ts of the template
  • replace this line of the plugin with const command = `cd ${path.dirname(this.sourceFilePath.toString())} ; asc ${this.sourceFilePath} --config asconfig.json --target debug`; so that asc uses the config of the template

@tolauwae
Copy link
Member

tolauwae commented Jul 7, 2023

There is no need to recompile with wat2wasm. I haven't checked if there is a size difference, but I don't imagine the wasm from asc is larger than the wasm from wat2wasm.

@tolauwae
Copy link
Member

tolauwae commented Jul 7, 2023

Compiled file with your command also works for me.

@tolauwae
Copy link
Member

tolauwae commented Jul 9, 2023

I also tried to use the template since it may not have the compilation error but I fail to properly configure it. Can you please confirm whether the following steps are correct:

Quickly tried it myself and AS doesn't seem to work right now in the plugin. Don't know what broke.

replace this line of the plugin with const command = cd ${path.dirname(this.sourceFilePath.toString())} ; asc ${this.sourceFilePath} --config asconfig.json --target debug; so that asc uses the config of the template

You don't have to change this. It should work regardless, but doesn't strangely. I tried it without changing any code on the main branch.

@tolauwae
Copy link
Member

tolauwae commented Jul 9, 2023

It wasn't working on my machine because the plugin used the globally installed asc instead of the local. #122 fixes this.

@tolauwae
Copy link
Member

tolauwae commented Jul 9, 2023

In other words, I still can't replicate your issue. The wast2wasm can compile the .wast generated by AS.

@tolauwae
Copy link
Member

Nevermind. I must not have run it exactly the same. I get the error now as well.

@carllocos
Copy link
Contributor Author

So I found the problem, the asc compiler was missing the --runtime stub option.

To be honest I do not see immediately how that option relates to valid wast, especially because the invalid wast type was already previously reported and fixed by the maintainers of asc (see AssemblyScript/assemblyscript#310).

I will push my fix as part of this PR #122. However, I now encountered another problem for which I still need to create a new issue (depending on the answer to this comment TOPLLab/WARDuino#197 (comment)) and for which the fix maybe should be included also in the same PR.

carllocos added a commit that referenced this issue Jul 14, 2023
To compile ASC without any optimisation I removed the `03s` argument
tolauwae pushed a commit that referenced this issue Aug 4, 2023
To compile ASC without any optimisation I removed the `03s` argument
@tolauwae tolauwae linked a pull request Aug 11, 2023 that will close this issue
@tolauwae tolauwae moved this to Under Review in WARDuino Roadmap Aug 12, 2023
@tolauwae tolauwae assigned tolauwae and carllocos and unassigned carllocos Aug 12, 2023
@tolauwae tolauwae changed the title asc generates invalid Wast AS generates invalid .wast Aug 12, 2023
tolauwae added a commit that referenced this issue Sep 27, 2023
* Use locally installed `asc` instead of global

* fix #120 and compile AS without optimisation

To compile ASC without any optimisation I removed the `03s` argument

* fix regex to only be true for source maps

AssemblyScript compiler can generate lines that contain the `@` symbol but have nothing to do with the source mapping (e.g., '000287d: 6e67 2e55 5446 382e 656e 636f 6465 4076  ng.UTF8.encode@v') which causes `extractLineInfo` to fail on those lines.
This commit modifies the regex to only be true for lines that start with `@ {` which ensures that `extractLineInfo` is applied to real source map lines.

* 🔨 Use AS source map directly

---------

Co-authored-by: Carlos Rojas <carlosj.rojascastillo@gmail.com>
@github-project-automation github-project-automation bot moved this from Under Review to Done in WARDuino Roadmap Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants