-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add safe Memory::read and Memory::write methods #2528
Add safe Memory::read and Memory::write methods #2528
Conversation
ffceb5b
to
7fac8d1
Compare
Subscribe to Label Actioncc @peterhuene
This issue or pull request has been labeled: "wasmtime:api"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
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.
Thanks for this!
crates/wasmtime/src/externals.rs
Outdated
@@ -864,6 +902,38 @@ impl Memory { | |||
MemoryType::from_wasmtime_memory(&self.wasmtime_export.memory.memory) | |||
} | |||
|
|||
/// Copies memory contents at the given offset into a buffer. | |||
/// | |||
/// Returns an error if the copy failed. |
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.
Could this documentation detail the error cases as well? It also might be good to mention that the size of the copy is the size of the buffer itself.
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.
I expanded the docs for both read
and write
a bit.
8aea7ed
to
3d10b20
Compare
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.
I think there may be some rustfmt issues too?
I implemented your suggestion, the code definitely is more elegant this way. I also added a |
3d10b20
to
74fbb3c
Compare
This commit introduces two new methods on `Memory` that enable reading and writing memory contents without requiring `unsafe`. The methods return a new `MemoryError` if the memory access fails.
74fbb3c
to
e0e2bc1
Compare
Looks great to me, thanks! |
Introduce
Memory::read
andMemory::write
methods for accessing memory contents safely.Also adds a test and mentions the the new methods in the
Memory
docs.Fixes #2484.
Currently most fallible methods use
anyhow::Error
.I have instead added a new
MemoryError
, since recovering from such errors is possible and it's more expressive.(future proofed with
#[non_exhaustive]
).Could to change it to a
anyhow::Error
if that's the preferred strategy (or wrap the MemoryError with anyhow).I also already added a
MemoryError::Locked
variant, which is not required yet, but apparently will be once threading support lands. Happy to remove it or slap on a#[doc(hidden)]
to prevent confusion.