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

feat(wasi-types) Extract types from wasi to a new wasi-types crate #2411

Merged
merged 5 commits into from
Jun 11, 2021

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Jun 8, 2021

Description

This has been discussed with @MarkMcCaskey many times in the past already.

Because we are very likely to re-use those types in other crates (it
already happens in private repos), we want to extract the WASI
types from the wasi crate.

This patch extracts the wasi/src/syscalls/types.rs module into its
own wasi-types crate. This new crate takes this opportunity to
classify the numerous types in submodules. All exported public types
are flatten.

What's missing is the documentation, let's address in another round of
PR.

It's really a copy-pasta patch, nothing fancy. We must agree on the module
namings, that's probably the most difficult part.

The Makefile has not been updated because there is nothing to test with this crate.

Review

  • Add a short description of the change to the CHANGELOG.md file

Because we are very likely to re-use those types in other crates (it
already happens in private repos already), we want to extract the WASI
types from the `wasi` crate.

This patch extracts the `wasi/src/syscalls/types.rs` module into its
own `wasi-types` crate. This new crate takes this opportunity to
classify the numerous types in submodules. All exported public types
are flatten.

What's missing is the documentation, let's address in another round of
PR.
@Hywan Hywan added 🎉 enhancement New feature! 📦 lib-wasi About wasmer-wasi labels Jun 8, 2021
@Hywan Hywan requested a review from MarkMcCaskey June 8, 2021 13:18
@Hywan Hywan self-assigned this Jun 8, 2021
@Hywan Hywan requested a review from syrusakbary as a code owner June 8, 2021 13:18
Copy link
Contributor

@MarkMcCaskey MarkMcCaskey left a comment

Choose a reason for hiding this comment

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

I gave my feedback on Slack, I'll summarize it below.

  • Not a huge fan of splitting into so many files but it's fine.
  • It'd be better if wasmer-wasi-types didn't depend on wasmer, I'd say it's worth it to convert all uses of WasmPtr (there are 1 or 2) to u32s to avoid this. There is a way to do a more proper fix but it's a lot of work and the work-around is super simple.

Otherwise, looks good to me 👍

Another point of feedback, the WASI docs don't use C-style naming for the types like we use anymore, it may be worth it update it but we can do that later. I'm not sure to what extent a witx generated crate would be useful, but perhaps that's something we can do, too, to avoid having to keep up with it.

@Hywan
Copy link
Contributor Author

Hywan commented Jun 8, 2021

wasmer-wasi-types depends on wasmer to get ValueType. Can you tell me why it is so?

@MarkMcCaskey
Copy link
Contributor

@Hywan Is it possible to move ValueType to wasmer-types? I wouldn't expect it to be at the wasmer level at all to be honest, it's a very low level trait meant to convey essentially what Copy does with an extra twist: namely that a type is safe to convert to/from bytes freely and that all bit patterns are valid

@Hywan
Copy link
Contributor Author

Hywan commented Jun 10, 2021

Ideally, wasi-types must not fetch wasmer because it will fetch much more, and we can't compile everything to wasm32-wasi. I'll update the PR to move ValueType to wasmer-types, so that we can remove the dependency to wasmer.

@Hywan
Copy link
Contributor Author

Hywan commented Jun 10, 2021

Please @MarkMcCaskey, can you review cfd4008 carefully please :-)?

@Hywan
Copy link
Contributor Author

Hywan commented Jun 10, 2021

bors try

bors bot added a commit that referenced this pull request Jun 10, 2021
@bors
Copy link
Contributor

bors bot commented Jun 10, 2021

try

Build failed:

bors bot added a commit that referenced this pull request Jun 10, 2021
2411: feat(wasi-types) Extract types from `wasi` to a new `wasi-types` crate r=Hywan a=Hywan

# Description

This has been discussed with @MarkMcCaskey many times in the past already.

Because we are very likely to re-use those types in other crates (it
already happens in private repos), we want to extract the WASI
types from the `wasi` crate.

This patch extracts the `wasi/src/syscalls/types.rs` module into its
own `wasi-types` crate. This new crate takes this opportunity to
classify the numerous types in submodules. All exported public types
are flatten.

What's missing is the documentation, let's address in another round of
PR.

It's really a copy-pasta patch, nothing fancy. We must agree on the module
namings, that's probably the most difficult part.

The `Makefile` has not been updated because there is nothing to test with this crate.

# Review

- [x] Add a short description of the change to the CHANGELOG.md file


Co-authored-by: Ivan Enderlin <ivan@mnt.io>
@wasmerio wasmerio deleted a comment from bors bot Jun 10, 2021
@Hywan
Copy link
Contributor Author

Hywan commented Jun 10, 2021

bors try

bors bot added a commit that referenced this pull request Jun 10, 2021
@bors
Copy link
Contributor

bors bot commented Jun 10, 2021

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct __wasi_ciovec_t {
pub buf: u32,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This changed from WasmPtr<u8, Array> to u32 to remove the need to use WasmPtr in wasmer-wasi-types. But I believe it's complicated to create this type now. Imagine you have the following code:

let mut vec: Vec<u8> = vec![0; 2];
let iov = __wasi_ciovec_t {
    buf: vec.as_mut_ptr(),
    buf_len: vec.len() as u32,
};

it fails to compile because buf is a u32, not a *mut u8. Casting a pointer to a usize is unstable for the moment (it requires the const_raw_ptr_to_usize_cast feature). It's not ideal.

Any recommandation @MarkMcCaskey?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A workaround is to:

    buf: vec.as_mut_ptr() as usize as u32,

I'm not super happy with that though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Casting a pointer to a usize is unstable for the moment

Really? I've done it all the time and never seen this. I wonder if it's because it's *mut? 🤔

I think your work around is what I'd expect here. We can have a WasmUsize type or something, but even that is confusing because with Wasm64, Wasm will have both 64 and 32 bit pointers depending on context. I'm unsure to what extent that will happen in practice given the target triples are all wasm32-*, maybe it'll be fully separated into wasm64-* and wasm32-*.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's out of the scope of this PR, but yeah, I agree that we must use a type alias to represent the memory size, something like wasm_usize/WasmUsize is an excellent idea.

Comment on lines -86 to -90

#[inline(always)]
pub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<String> {
self.0.get_utf8_string(memory, str_len)
}
Copy link
Contributor

@MarkMcCaskey MarkMcCaskey Jun 11, 2021

Choose a reason for hiding this comment

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

I think we want to undelete this as I believe it's a publicly visible API. I'm not sure to what extent it's useful as this is the WASI WasmPtr, but if it's publicly visible, we probably shouldn't remove it

Otherwise the PR looks good to ship 👍

Copy link
Contributor

@MarkMcCaskey MarkMcCaskey left a comment

Choose a reason for hiding this comment

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

Looks good to me

@MarkMcCaskey
Copy link
Contributor

bors r+

@bors
Copy link
Contributor

bors bot commented Jun 11, 2021

@bors bors bot merged commit 8582873 into wasmerio:master Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! 📦 lib-wasi About wasmer-wasi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants