-
Notifications
You must be signed in to change notification settings - Fork 818
wasm2c: uvwasi adapter layer for wasm2c output #2782
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
shravanrn
wants to merge
1
commit into
WebAssembly:main
Choose a base branch
from
UT-Security:wasi-adapter
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
Changes from all commits
Commits
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
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,5 @@ | ||
| *.o | ||
| hello-wasi | ||
| hello-wasi.wasm | ||
| hello-wasi-wasm.* | ||
| test.out | ||
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,21 @@ | ||
| WABT_ROOT=../../.. | ||
| CC=clang | ||
| CFLAGS=-I$(WABT_ROOT)/wasm2c -I$(WABT_ROOT)/third_party/uvwasi/include -O3 | ||
| LDFLAGS=-L$(WABT_ROOT)/build/_deps/libuv-build -L$(WABT_ROOT)/build/third_party/uvwasi | ||
| LDLIBS=-luvwasi_a -luv_a -lm -lpthread | ||
|
|
||
| all: hello-wasi | ||
|
|
||
| clean: | ||
| rm -rf hello-wasi hello-wasi.wasm hello-wasi-wasm.c hello-wasi-wasm.h test.out *.o | ||
|
|
||
| hello-wasi.wasm: hello-wasi.c | ||
| /opt/wasi-sdk/bin/clang -O3 $< -o $@ | ||
|
|
||
| hello-wasi-wasm.c: hello-wasi.wasm $(WABT_ROOT)/bin/wasm2c | ||
| $(WABT_ROOT)/bin/wasm2c $< -n hello_wasi -o $@ | ||
|
|
||
| hello-wasi: main.c hello-wasi-wasm.c $(WABT_ROOT)/wasm2c/wasm-rt-uvwasi-adapter.c $(WABT_ROOT)/wasm2c/wasm-rt-impl.c $(WABT_ROOT)/wasm2c/wasm-rt-mem-impl.c | ||
| $(CC) $(LDFLAGS) $(CFLAGS) $^ -o $@ $(LDLIBS) | ||
|
|
||
| .PHONY: all clean |
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,33 @@ | ||
| #include <assert.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <unistd.h> | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| printf("printing args\n"); | ||
| for (int i = 0; i < argc; i++) { | ||
| printf("[%d] %s \n", i, argv[i]); | ||
| } | ||
|
|
||
| printf("Writing to stdout\n"); | ||
| char* hello_str = strdup("hello world!"); | ||
| puts(hello_str); | ||
|
|
||
| printf("writing test.out\n"); | ||
| FILE* fp = fopen("test.out", "w+"); | ||
| assert(fp != NULL); | ||
| fprintf(fp, "hello filesystem\n"); | ||
| fclose(fp); | ||
| printf("removing test.out\n"); | ||
| assert(!unlink("test.out")); | ||
|
|
||
| printf("writing /tmp/test.out\n"); | ||
| fp = fopen("/tmp/test.out", "w+"); | ||
| assert(fp != NULL); | ||
| fprintf(fp, "hello filesystem\n"); | ||
| fclose(fp); | ||
| printf("removing /tmp/test.out\n"); | ||
| assert(!unlink("/tmp/test.out")); | ||
|
|
||
| return 0; | ||
| } |
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,49 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #include "hello-wasi-wasm.h" | ||
| #include "wasm-rt-uvwasi-adapter.h" | ||
| #include "uvwasi.h" | ||
|
|
||
| int main(int argc, const char** argv) { | ||
| w2c_hello__wasi hello = {0}; | ||
| struct w2c_wasi__snapshot__preview1 wasi = {0}; | ||
| uvwasi_t uvwasi = {0}; | ||
| uvwasi_options_t init_options = {0}; | ||
| uvwasi_preopen_t preopens[2] = {0}; | ||
|
|
||
| init_options.in = 0; | ||
| init_options.out = 1; | ||
| init_options.err = 2; | ||
| init_options.fd_table_size = 10; | ||
|
|
||
| extern const char** environ; | ||
| init_options.argc = argc; | ||
| init_options.argv = argv; | ||
| init_options.envp = (const char**)environ; | ||
|
|
||
| preopens[0].mapped_path = "/tmp"; | ||
| preopens[0].real_path = "/tmp"; | ||
| preopens[1].mapped_path = "."; | ||
| preopens[1].real_path = "."; | ||
| init_options.preopenc = 2; | ||
| init_options.preopens = preopens; | ||
| init_options.allocator = NULL; | ||
|
|
||
| wasm_rt_init(); | ||
| uvwasi_errno_t ret = uvwasi_init(&uvwasi, &init_options); | ||
| if (ret != UVWASI_ESUCCESS) { | ||
| fprintf(stderr, "uvwasi_init failed with error %u\n", ret); | ||
| return 1; | ||
| } | ||
|
|
||
| wasm2c_uvwasi_link(&wasi, &hello.w2c_memory, &uvwasi); | ||
| wasm2c_hello__wasi_instantiate(&hello, &wasi); | ||
| w2c_hello__wasi_0x5Fstart(&hello); | ||
| wasm2c_hello__wasi_free(&hello); | ||
|
|
||
| uvwasi_destroy(&uvwasi); | ||
| wasm_rt_free(); | ||
|
|
||
| return 0; | ||
| } |
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.
Usually tmp files goes to the
bin/build/orout/directories when wabt is compiled. Can these files goes there as well (and no need to add them to gitignore)?