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

Add safe Memory::read and Memory::write methods #2528

Merged

Conversation

theduke
Copy link
Contributor

@theduke theduke commented Dec 20, 2020

Introduce Memory::read and Memory::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.

@theduke theduke force-pushed the issue-2484-safe-memory-access branch from ffceb5b to 7fac8d1 Compare December 20, 2020 01:11
@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Dec 20, 2020
@github-actions
Copy link

Subscribe to Label Action

cc @peterhuene

This issue or pull request has been labeled: "wasmtime:api"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Member

@alexcrichton alexcrichton left a 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 Show resolved Hide resolved
@@ -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.
Copy link
Member

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.

Copy link
Contributor Author

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.

crates/wasmtime/src/externals.rs Outdated Show resolved Hide resolved
@theduke theduke force-pushed the issue-2484-safe-memory-access branch 3 times, most recently from 8aea7ed to 3d10b20 Compare January 4, 2021 20:24
Copy link
Member

@alexcrichton alexcrichton left a 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?

crates/wasmtime/src/externals.rs Outdated Show resolved Hide resolved
crates/wasmtime/src/externals.rs Outdated Show resolved Hide resolved
@theduke
Copy link
Contributor Author

theduke commented Jan 26, 2021

I implemented your suggestion, the code definitely is more elegant this way.

I also added a _private field to the error for extensibility and moved the test to tests/all/externals.rs since most tests seem to live there.

@theduke theduke force-pushed the issue-2484-safe-memory-access branch from 3d10b20 to 74fbb3c Compare January 26, 2021 04:04
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.
@theduke theduke force-pushed the issue-2484-safe-memory-access branch from 74fbb3c to e0e2bc1 Compare January 26, 2021 04:07
@alexcrichton
Copy link
Member

Looks great to me, thanks!

@alexcrichton alexcrichton merged commit f4faa04 into bytecodealliance:main Jan 26, 2021
@theduke theduke deleted the issue-2484-safe-memory-access branch January 27, 2021 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Safe Memory Access
2 participants