Skip to content

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

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
theduke:issue-2484-safe-memory-access
Jan 26, 2021
Merged

Add safe Memory::read and Memory::write methods#2528
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
theduke:issue-2484-safe-memory-access

Conversation

@theduke
Copy link
Copy Markdown
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
Copy Markdown

Subscribe to Label Action

cc @peterhuene

Details 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
Copy Markdown
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!

Comment thread crates/wasmtime/src/externals.rs Outdated
Comment thread crates/wasmtime/src/externals.rs Outdated

/// Copies memory contents at the given offset into a buffer.
///
/// Returns an error if the copy failed.
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread crates/wasmtime/src/externals.rs Outdated
@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
Copy Markdown
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?

Comment thread crates/wasmtime/src/externals.rs Outdated
Comment thread crates/wasmtime/src/externals.rs Outdated
@theduke
Copy link
Copy Markdown
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
Copy Markdown
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