Skip to content

feat:add new target to support wasm freestanding #3072

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

Conversation

hunjixin
Copy link

@hunjixin hunjixin commented Aug 15, 2022

resolve #3068

@hunjixin hunjixin force-pushed the feat/add_new_target_wasm_unknow branch 2 times, most recently from 54cf07c to 56f1495 Compare August 16, 2022 05:14
@hunjixin
Copy link
Author

@deadprogram plan to use this feature?

@dgryski
Copy link
Member

dgryski commented Aug 20, 2022

I want to bikeshed the tag. unknow_wasm should probaby be unknown_wasm at least (to fix the spelling error), but I wonder if we want something like barewasm (similar to baremetal) to indicate exactly what we're building for?

@hunjixin hunjixin changed the title feat:add new target to support wasm unknow feat:add new target to support wasm unknown Aug 22, 2022
@hunjixin hunjixin marked this pull request as draft August 22, 2022 03:28
@hunjixin
Copy link
Author

@dgryski barewasm seems a reasonable tag name. I think developers are very much in need of #3068. Although WASM was originally designed for browser front-end development, new use cases are being raised for VMs in the blockchain dev communities, and various cross-platform algorithm implementations. One of the most important things is that WASM can take good advantage of the existing technical capabilities of various languages.
This PR aims to better support the Go standard library and the existing Go code on github to be ported to wasm with minimal changes. In fact, Rust achieves this goal through a similar approach (like this PR). As a result, A lot of developers use this feature in RUST to build the WASM projects. If TinyGo can merge this approach, I believe it will help the TinyGo project to have an even larger adoption rate in regard to WASM projects.

@codefromthecrypt
Copy link
Contributor

for the bikeshed I think 'freestanding' is in use for wasm for something similar (maybe zig?) would be cool to use common conventions esp if there aren't strong feelings about it. @mathetake do you have any opinion?

@mathetake
Copy link
Contributor

rust: unknown, zig: freestanding, so either works for me

@hunjixin
Copy link
Author

hunjixin commented Aug 24, 2022

for the bikeshed I think 'freestanding' is in use for wasm for something similar (maybe zig?) would be cool to use common conventions esp if there aren't strong feelings about it. @mathetake do you have any opinion?

its better to use freestanding than unknown. @dgryski any better idea

zig cc -target wasm32-freestanding test.c -o test -lc

@hunjixin hunjixin changed the title feat:add new target to support wasm unknown feat:add new target to support wasm freestanding Aug 29, 2022
@hunjixin hunjixin force-pushed the feat/add_new_target_wasm_unknow branch 2 times, most recently from c3d6e00 to 793144e Compare August 29, 2022 03:25
@hunjixin
Copy link
Author

hunjixin commented Sep 4, 2022

@codefromthecrypt please help to retry the ci?

@deadprogram
Copy link
Member

From the looks of those CI errors, someone needs to rebase this branch against the latest dev

@hunjixin hunjixin force-pushed the feat/add_new_target_wasm_unknow branch from 793144e to 3eb4033 Compare September 5, 2022 03:39
@hunjixin hunjixin changed the base branch from release to dev September 5, 2022 03:40
@RubixDev
Copy link

RubixDev commented Sep 27, 2022

How far is the progress on this? I would really like to use this to make an shfmt plugin for dprint.

@hunjixin
Copy link
Author

hunjixin commented Sep 28, 2022

How far is the progress on this? I would really like to use this to make an shfmt plugin for dprint.

dprint?this pr remove all print support

@RubixDev
Copy link

dprint is a code formatter written in Rust that supports plugins in bare wasm. The only thing These wasm files have to do is export some functions that dprint will then call, but it does not provide WASI or emscripten or any other interface. The dprint wasm plugins written in Rust all target wasm32-unknown-unknown. It has nothing to do with printing.

@hunjixin
Copy link
Author

dprint is a code formatter written in Rust that supports plugins in bare wasm. The only thing These wasm files have to do is export some functions that dprint will then call, but it does not provide WASI or emscripten or any other interface. The dprint wasm plugins written in Rust all target wasm32-unknown-unknown. It has nothing to do with printing.

so you are planning to use golang formatter to generate wasm plugin?

@RubixDev
Copy link

Yes. I want to create a wasm binary including the shfmt shell formatter that is written in Go. Thus the plugin itself must also be written in Go, as it depends on shfmt.

But back to the original question, how far is the progress on this? Is it already somehow usable? I am sure this target is what I need for my use case.

@hunjixin
Copy link
Author

hunjixin commented Sep 28, 2022

Yes. I want to create a wasm binary including the shfmt shell formatter that is written in Go. Thus the plugin itself must also be written in Go, as it depends on shfmt.

But back to the original question, how far is the progress on this? Is it already somehow usable? I am sure this target is what I need for my use case.

there are still some behaviors that cannot be determined which method is better, especially the time part,should be panic or follow behavior suggested by @codefromthecrypt. If the current demand is strong, you can also consider this idea https://github.com/ipfs-force-community/go_tinygo_patch

for using increasing value for time return, this could make ramdom worked, but increase risk because randomness can be predicted.

codefromthecrypt pushed a commit to tetratelabs/wazero that referenced this pull request Sep 29, 2022
It is more often the case that projects are enabling a freestanding
target, and that may or may not have an exporting memory depending on
how that's interpreted. This adds the ability to inspect memories
similar to how you can already inspect compiled code prior to
instantiation. For example, you can enforce an ABI constraint that
"memory" must be exported even if WASI is not in use.

See tinygo-org/tinygo#3072

cc @hunjixin

Signed-off-by: Adrian Cole <[email protected]>
@hunjixin hunjixin force-pushed the feat/add_new_target_wasm_unknow branch from 3eb4033 to dcc39e2 Compare September 29, 2022 12:38
@jsimnz
Copy link

jsimnz commented Nov 29, 2022

Any update on this? Happy to contribute if needed, or are you still trying to establish behavior?

@hunjixin
Copy link
Author

Any update on this? Happy to contribute if needed, or are you still trying to establish behavior?

This pr should work normally, but I don't know how to make tinygo accept this target. The behavioral aspect tends to keep the existing implemented behavior. There is a security risk in using self increasing time. development usually use time.Now as a random seed. especially in the financial smart contract.

@codefromthecrypt
Copy link
Contributor

We're nearing a year since @hunjixin raised this. The last question was "how to make tinygo accept this target"

Could someone lay out what is required to get this in, sounds like getting the build to pass is a given. What else can be checked off?

@deadprogram
Copy link
Member

Valid point @codefromthecrypt

@dgryski @aykevl what are your thoughts on this? Also while keeping in mind upcoming changes for Go1.21+

@dajuguan
Copy link

when I build it tinygo build -o main.wasm -target=unknow_wasm main.go, and run it with wasmer. The following error shows:

error: Unable to instantiate the WebAssembly module
╰─▶ 1: Error while importing "env"."tinygo_getCurrentStackPointer": unknown import. Expected Function(FunctionType { params: [], results: [I32] })

@deadprogram
Copy link
Member

I would like to have this sort of functionality myself to be able to run TinyGo compiled code on WAMR.

I think since the naming of wasm32-unknown-unknown is the most standard, so probably -target=wasm32-unknown and build tag wasm32_unknown would be the best option since there are more people doing this sort of thing with WASM in C or Rust at the moment.

Regarding getting this change accepted, there needs to be some tests showing that it works as expected. Some tests similar to the wasi target is the most likely place to look for inspiration.

Thanks for working on this, and let me know how I can help out.

deadprogram added a commit that referenced this pull request Feb 12, 2024
deadprogram added a commit that referenced this pull request Feb 13, 2024
deadprogram added a commit that referenced this pull request Feb 14, 2024
deadprogram added a commit that referenced this pull request Feb 15, 2024
@deadprogram
Copy link
Member

Now that #4125 has been merged, I am closing this one. Thanks again everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add new target like wasm32-unknown-unknown in rust
8 participants