Stream: git-wasmtime

Topic: wasmtime / Issue #1888 wiggle: GuestPtr<[u8]> <=> GuestPt...


view this post on Zulip Wasmtime GitHub notifications bot (Jun 16 2020 at 18:38):

pchickey labeled Issue #1888:

This issue is specific to the Wiggle crate. It is a good beginner issue.

Feature

We don't have a way to convert between GuestPtr<[u8]> and GuestPtr<str>. I just came across a case where it would be useful - I have a GuestPtr<str> and I need to pass the underlying data as &[u8] to a function that will perform its own utf8 validation - and the opposite is probably useful as well. #### Benefit

Implementation

Add methods to impl GuestPtr<[T]> and impl GuestPtr<str> that return the other type.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 16 2020 at 18:38):

pchickey opened Issue #1888:

This issue is specific to the Wiggle crate. It is a good beginner issue.

Feature

We don't have a way to convert between GuestPtr<[u8]> and GuestPtr<str>. I just came across a case where it would be useful - I have a GuestPtr<str> and I need to pass the underlying data as &[u8] to a function that will perform its own utf8 validation - and the opposite is probably useful as well. #### Benefit

Implementation

Add methods to impl GuestPtr<[T]> and impl GuestPtr<str> that return the other type.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 16 2020 at 18:38):

pchickey edited Issue #1888:

This issue is specific to the Wiggle crate. It is a good beginner issue.

Feature

We don't have a way to convert between GuestPtr<[u8]> and GuestPtr<str>. I just came across a case where it would be useful - I have a GuestPtr<str> and I need to pass the underlying data as &[u8] to a function that will perform its own utf8 validation - and the opposite is probably useful as well.

Implementation

Add methods to impl GuestPtr<[T]> and impl GuestPtr<str> that return the other type.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 18 2020 at 11:04):

siyopao commented on Issue #1888:

Hi! Would it be okay if I took a shot at this? Although I may require a bit more explanation...

view this post on Zulip Wasmtime GitHub notifications bot (Jun 18 2020 at 16:30):

pchickey commented on Issue #1888:

Welcome @siyopao, thank you for volunteering!

In the wiggle crate, we have three impls of the Pointee trait that describe how we keep track of pointers to atomic values T, slices [T], and strings str. A string is just a [u8] which contains valid utf-8 codepoints.

One mechanic of GuestPtr is we check for validity of the contents not when the GuestPtr is constructed but when it is "dereferenced" - via read, as_slice, or as_str. So, the check for whether the contents of a GuestPtr<str> are valid utf-8 doesn't occur until GuestPtr::as_str is invoked.

If the user wishes to skip the utf-8 validity check, there is currently no way to take a GuestPtr<str> and treat it as a GuestPtr<[u8]>, these are kept separate in the type system even though they have the same type Pointer = (u32, u32); indicating the start of, and length of, the linear memory region.

So, I propose adding a method to impl GuestPtr<str> that has the signature pub fn as_byte_ptr(&self) -> GuestPtr<[u8]>, and a new impl GuestPtr<[u8]> that has a function with signature pub fn as_str_ptr(&self) -> GuestPtr<str>.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 18 2020 at 16:30):

pchickey edited a comment on Issue #1888:

Welcome @siyopao, thank you for volunteering!

In the wiggle crate, we have three impls of the Pointee trait that describe how we keep track of pointers to atomic values T, slices [T], and strings str. A string is just a [u8] which contains valid utf-8 codepoints.

One mechanic of GuestPtr is we check for validity of the contents not when the GuestPtr is constructed but when it is "dereferenced" - via read, as_slice, or as_str. So, the check for whether the contents of a GuestPtr<str> are valid utf-8 doesn't occur until GuestPtr::as_str is invoked.

If the user wishes to skip the utf-8 validity check, there is currently no way to take a GuestPtr<str> and treat it as a GuestPtr<[u8]>, these are kept separate in the type system even though they have the same type Pointer = (u32, u32); indicating the start of, and length of, the linear memory region.

So, I propose adding a method to impl GuestPtr<str> that has the signature pub fn as_byte_ptr(&self) -> GuestPtr<[u8]>, and a new impl GuestPtr<[u8]> that has a method with signature pub fn as_str_ptr(&self) -> GuestPtr<str>.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 18 2020 at 16:36):

pchickey edited Issue #1888:

This issue is specific to the Wiggle crate. It is a good beginner issue.

Feature

We don't have a way to convert between GuestPtr<[u8]> and GuestPtr<str>. I just came across a case where it would be useful - I have a GuestPtr<str> and I need to pass the underlying data as &[u8] to a function that will perform its own utf8 validation - and the opposite is probably useful as well.

Implementation

Add methods to impl GuestPtr<[u8]> and impl GuestPtr<str> that return the other type.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 18 2020 at 16:55):

pchickey edited a comment on Issue #1888:

Welcome @siyopao, thank you for volunteering!

In the wiggle crate, we have three impls of the Pointee trait that describe how we keep track of pointers to atomic values T, slices [T], and strings str. A string is just a [u8] which contains valid utf-8 codepoints.

One mechanic of GuestPtr is we check for validity of the contents not when the GuestPtr is constructed but when it is "dereferenced" - via read, as_slice, or as_str. So, the check for whether the contents of a GuestPtr<str> are valid utf-8 doesn't occur until GuestPtr::as_str is invoked.

If the user wishes to skip the utf-8 validity check, there is currently no way to take a GuestPtr<str> and treat it as a GuestPtr<[u8]>, these are kept separate in the type system even though they have the same type Pointer = (u32, u32); indicating the start of, and length of, the linear memory region they point to.

So, I propose adding a method to impl GuestPtr<str> that has the signature pub fn as_byte_ptr(&self) -> GuestPtr<[u8]>, and a new impl GuestPtr<[u8]> that has a method with signature pub fn as_str_ptr(&self) -> GuestPtr<str>.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 19 2020 at 01:31):

siyopao commented on Issue #1888:

Hello @pchickey! Wow, thank you for the in-depth explanation. Let me see what I can do :+1:

view this post on Zulip Wasmtime GitHub notifications bot (Jun 20 2020 at 01:59):

siyopao commented on Issue #1888:

Hi @pchickey. Again, thank you the the explanation. So I have started to take a look, and noticed that the implementation of GuestPtr<str> has a method

pub fn as_bytes(&self) -> GuestPtr<'a, [u8]> {
    GuestPtr::new(self.mem, self.pointer)
}

Is this basically what your proposed pub fn as_byte_ptr(&self) -> GuestPtr<[u8]> would look like, or am I totally off base? Thank you!

view this post on Zulip Wasmtime GitHub notifications bot (Jun 22 2020 at 17:33):

pchickey commented on Issue #1888:

If that typechecks then it looks correct to me.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 24 2020 at 04:08):

pchickey closed Issue #1888:

This issue is specific to the Wiggle crate. It is a good beginner issue.

Feature

We don't have a way to convert between GuestPtr<[u8]> and GuestPtr<str>. I just came across a case where it would be useful - I have a GuestPtr<str> and I need to pass the underlying data as &[u8] to a function that will perform its own utf8 validation - and the opposite is probably useful as well.

Implementation

Add methods to impl GuestPtr<[u8]> and impl GuestPtr<str> that return the other type.


Last updated: Dec 23 2024 at 12:05 UTC