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

CLI: Add WASI support #597

Merged
merged 11 commits into from Feb 7, 2023

Conversation

OLUWAMUYIWA
Copy link
Contributor

PR adds wasi support to wasmi_cli.

Notable changes:

  • --dir flag: allows pre-opening of directories with which hosts protect guests from accessing only directories they want. i.e. wasi sandboxing in action.
  • --env flag: allows users to set environment variables
  • --tcplisten flag: pre-open sockets. wasi sandboxing.

Note

  • All three resources (directories, environment vars, sockets) are pre-provided to the guest program through the above flags.
  • All three flags can occur multiple times in any order.
  • wasmi_cli makes WASI available to all programs.

@paritytech-cicd-pr
Copy link

paritytech-cicd-pr commented Dec 30, 2022

BENCHMARKS

NATIVEWASMTIME
BENCHMARKMASTERPRDIFFMASTERPRDIFFWASMTIME OVERHEAD
execute/
bare_call_0
1.40ms 1.46ms 🔴 4.33% 1.18ms 1.18ms 🟢 0.35% 🟢 -19%
execute/
bare_call_0/typed
1.02ms 1.00ms 🟢 -1.79% 820.87µs 762.49µs 🟢 -7.12% 🟢 -24%
execute/
bare_call_1
1.43ms 1.49ms 🔴 4.72% 1.38ms 1.38ms ⚪ 0.23% 🟢 -7%
execute/
bare_call_16
2.38ms 2.47ms 🔴 3.69% 4.29ms 4.28ms ⚪ -0.24% 🟡 73%
execute/
bare_call_16/typed
1.61ms 1.52ms 🟢 -5.77% 2.28ms 2.28ms ⚪ 0.14% 🟢 50%
execute/
bare_call_1/typed
1.12ms 1.12ms ⚪ 0.49% 1.18ms 1.17ms ⚪ -0.73% 🟢 5%
execute/
bare_call_4
1.58ms 1.57ms ⚪ 0.00% 2.02ms 2.04ms ⚪ 0.64% 🟢 29%
execute/
bare_call_4/typed
1.12ms 1.12ms ⚪ -0.44% 1.25ms 1.25ms ⚪ -0.37% 🟢 12%
execute/
br_table
1.12ms 1.12ms ⚪ 0.03% 1.44ms 1.42ms ⚪ -0.61% 🟢 27%
execute/
count_until
654.48µs 653.95µs ⚪ 0.76% 2.14ms 2.15ms ⚪ 0.04% 🔴 228%
execute/
factorial_iterative
327.02µs 329.66µs ⚪ 0.78% 911.58µs 909.10µs ⚪ -0.22% 🔴 176%
execute/
factorial_recursive
654.84µs 647.35µs ⚪ -1.05% 1.34ms 1.34ms ⚪ 0.20% 🔴 107%
execute/
fib_iterative
1.44ms 1.35ms 🟢 -5.97% 5.01ms 5.01ms ⚪ -0.06% 🔴 270%
execute/
fib_recursive
5.91ms 5.91ms ⚪ 0.01% 12.02ms 11.94ms ⚪ -0.73% 🔴 102%
execute/
global_bump
976.50µs 961.63µs ⚪ -1.01% 2.57ms 2.57ms ⚪ -0.21% 🔴 167%
execute/
global_const
737.10µs 737.61µs ⚪ -0.04% 2.40ms 2.40ms ⚪ 0.34% 🔴 226%
execute/
host_calls
26.35µs 26.11µs ⚪ -1.51% 39.07µs 39.10µs ⚪ 0.11% 🟢 50%
execute/
memory_fill
1.25ms 1.26ms ⚪ 0.16% 4.03ms 4.03ms ⚪ 0.05% 🔴 221%
execute/
memory_sum
1.25ms 1.19ms ⚪ -4.11% 4.01ms 4.01ms ⚪ -0.10% 🔴 236%
execute/
memory_vec_add
2.50ms 2.50ms ⚪ -0.11% 7.64ms 7.69ms ⚪ 0.66% 🔴 208%
execute/
recursive_is_even
1.21ms 1.14ms 🟢 -5.87% 2.25ms 2.19ms 🟢 -2.22% 🟡 93%
execute/
recursive_ok
152.52µs 152.15µs ⚪ -0.13% 318.57µs 310.37µs 🟢 -2.81% 🔴 104%
execute/
recursive_scan
182.88µs 181.81µs ⚪ -0.58% 399.94µs 390.86µs 🟢 -2.24% 🔴 115%
execute/
recursive_trap
15.07µs 15.04µs ⚪ -0.28% 32.14µs 31.43µs 🟢 -2.25% 🔴 109%
execute/
regex_redux
534.77µs 533.25µs ⚪ -0.25% 1.40ms 1.38ms ⚪ -0.83% 🔴 159%
execute/
rev_complement
473.75µs 472.77µs ⚪ -0.70% 1.40ms 1.39ms ⚪ -0.27% 🔴 195%
execute/
tiny_keccak
329.98µs 329.06µs ⚪ -0.33% 1.15ms 1.16ms 🔴 1.20% 🔴 252%
execute/
trunc_f2i
723.97µs 723.95µs ⚪ -0.04% 2.14ms 2.14ms ⚪ 0.00% 🔴 196%
instantiate/
wasm_kernel
64.08µs 65.21µs 🔴 2.56% 70.79µs 70.37µs ⚪ -0.55% 🟢 8%
translate/
erc1155
210.56µs 207.60µs ⚪ -1.38% 394.42µs 389.30µs 🟢 -1.50% 🟡 88%
translate/
erc20
104.30µs 103.29µs ⚪ -0.96% 192.91µs 191.38µs ⚪ -0.59% 🟡 85%
translate/
erc721
147.37µs 146.48µs ⚪ -0.72% 281.48µs 279.32µs ⚪ -0.77% 🟡 91%
translate/
spidermonkey
0.00ns 0.00ns 🟢 -1.68% 0.00ns 0.00ns ⚪ 0.70% 🟢 0%
translate/
wasm_kernel
3.81ms 3.79ms ⚪ -0.89% 7.29ms 7.19ms ⚪ -0.89% 🟡 89%

Link to pipeline

@codecov-commenter
Copy link

codecov-commenter commented Dec 30, 2022

Codecov Report

Merging #597 (4126082) into master (683dde1) will decrease coverage by 0.62%.
The diff coverage is 19.32%.

@@            Coverage Diff             @@
##           master     #597      +/-   ##
==========================================
- Coverage   81.13%   80.51%   -0.62%     
==========================================
  Files          93       94       +1     
  Lines        7719     7807      +88     
==========================================
+ Hits         6263     6286      +23     
- Misses       1456     1521      +65     
Impacted Files Coverage Δ
crates/cli/src/main.rs 22.31% <0.00%> (-7.32%) ⬇️
crates/cli/tests/run.rs 88.46% <88.46%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Robbepop Robbepop mentioned this pull request Dec 30, 2022
crates/cli/Cargo.toml Outdated Show resolved Hide resolved
crates/cli/src/main.rs Show resolved Hide resolved
crates/cli/src/main.rs Show resolved Hide resolved
#[clap()]
func_name: Option<String>,
#[clap(long, value_parser)]
invoke: Option<String>,
Copy link
Collaborator

@Robbepop Robbepop Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The invoke flag needs to take a custom value of InvokeArgs which consist of the function name and its arguments since it does not make sense to have function arguments without an associated function name. The default _start function in WASI never takes arguments.

struct InvokeArgs {
    func_name: Option<String>,
    func_args: Vec<String>,
}

As with the KeyValue type we also need a parse function for this type that is going to be used.
Maybe it is possible to simply derive clap::Parse instead of providing a custom parse_invoke_args function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Will Address it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if clap::Parse derive work we should be fine with:

#[derive(Parser)]
struct InvokeArgs {
    #[clap(value_name = "FUNC_NAME")]
    func_name: Option<String>,
    #[clap()]
    func_args: Vec<String>,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default _start function in WASI never takes arguments.

Something to consider: A user might name their exported function _start or " " and invoke with arguments.

Copy link
Collaborator

@Robbepop Robbepop Dec 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--invoke _start 42 true and --invoke " " 42 true may be working for those cases, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the code now work so that function arguments can only be provided together with --invoke and a function name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Both " " and _start can take arguments.

Does the code now work so that function arguments can only be provided together with --invoke and a function name?

No. I just understood you. Would have to use a custom parser or use groups. I'll look into it. Maybe I don't need the new struct though. Seen the use of groups in nextest cli.

Copy link
Collaborator

@Robbepop Robbepop Dec 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I know parse groups are usually used for excluding certain options. For example: Among these flags the user may pick at most one. I think the most straight forward approach is to have InvokeArgs as a custom type and simply support it being a CLI flag such as PathBuf or SocketAddr. I am sure clap supports custom types somehow.

Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to execute the following command using this PR:

> cargo run --package wasmi_cli -- --invoke ./crates/wasmi/benches/wat/count_until.wat count_until 100
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s
     Running `target\debug\wasmi_cli.exe --invoke .\crates\wasmi\benches\wat\count_until.wat count_until 100`
Error: failed to read Wasm file "count_until"

So the input path ./crates/wasmi/benches/wat/count_until.wat gets cut down to just count_until and therefore no file can be found.

@OLUWAMUYIWA
Copy link
Contributor Author

Hey @Robbepop , please review whenever you're free

Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the work on this PR! I see this heading strongly into a very good direction. :)

crates/cli/tests/run.rs Outdated Show resolved Hide resolved
crates/cli/tests/run.rs Outdated Show resolved Hide resolved
crates/cli/src/main.rs Outdated Show resolved Hide resolved
crates/cli/src/main.rs Show resolved Hide resolved
crates/cli/src/main.rs Outdated Show resolved Hide resolved
crates/cli/src/main.rs Outdated Show resolved Hide resolved
crates/cli/src/main.rs Show resolved Hide resolved
OLUWAMUYIWA and others added 3 commits February 7, 2023 04:52
Co-authored-by: Robin Freyler <robbepop@web.de>
Co-authored-by: Robin Freyler <robbepop@web.de>
@Robbepop
Copy link
Collaborator

Robbepop commented Feb 7, 2023

Since we already are way into the review process for this PR it is no longer a draft. I will convert it now.

@Robbepop Robbepop marked this pull request as ready for review February 7, 2023 08:40
Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All in all looks good to me!
Thanks a ton for this amazing work @OLUWAMUYIWA ! 🚀
There are just a few minor nits but it will be vastly simpler for the whole process if I fix those after merging the PR.
The PR is good as is. :)
I also checked out the PR locally and toyed around with it a little. Seeing a wasmi "Hello, World!" was quite an experience for me. :)

Let us merge WASI support for the wasmi CLI tool. 😎

The next big milestone for us is integrating the WASI testsuite and making it pass so that we can remove the experimental flag on our WASI support. My estimation is that passing the WASI testsuite won't be too hard since we are basically building on the shoulders of giants already.

@Robbepop Robbepop merged commit 5994eba into wasmi-labs:master Feb 7, 2023
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.

None yet

4 participants