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

Make WasmPtr::get_utf8_string return String #1979

Merged
merged 3 commits into from
Jan 4, 2021

Conversation

MarkMcCaskey
Copy link
Contributor

@MarkMcCaskey MarkMcCaskey commented Jan 4, 2021

The old functionality is readded as an unsafe method,
WasmPtr::get_utf8_str.

It's important to note that WasmPtr::get_utf8_string isn't entirely sound and technically should be marked unsafe as well, but this change is a massive improvement over what we had before.

For future reference, the reason WasmPtr::get_utf8_string still has some soundness issues is that we can't guarantee exclusive access to the memory while parsing in the string, we temporarily hold a &[u8] and hope it doesn't get mutated.

It's possible to implement this method in a more correct way by copying each byte as we read it into a Vec and converting that into a String.

EDIT:
fixed in b592ed8; WasmPtr::get_utf8_string is sound and uses no unsafe internally now.

Review

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

The old functionality is readded as an `unsafe` method,
`WasmPtr::get_utf8_str`.

It's important to note that `WasmPtr::get_utf8_string` isn't _entirely_
sound and technically should be marked `unsafe` as well, but this change
is a massive improvement over what we had before.

For future reference, the reason `WasmPtr::get_utf8_string` still has some
soundness issues is that we can't guarantee exclusive access to the memory
while parsing in the string, we temporarily hold a `&[u8]` and hope it
doesn't get mutated. It's possible to implement this method in a more
correct way by copying each byte as we read it into a `Vec` and converting
that into a String.
@MarkMcCaskey MarkMcCaskey added the 🔈soundness Bugs causing an unsound API label Jan 4, 2021
///
/// Additionally, if `memory` is dynamic, the caller must also ensure that `memory`
/// is not grown while the reference is held.
pub unsafe fn get_utf8_str<'a>(self, memory: &'a Memory, str_len: u32) -> Option<&'a str> {
Copy link
Member

Choose a reason for hiding this comment

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

Can we mark this function as deprecated? @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.

We could, but I don't know if we want to! It's dangerous to use, but it is more efficient as it doesn't do any memory allocations, so it's nice to have, it just needs to be used very carefully.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's merge then!

@MarkMcCaskey
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Jan 4, 2021
1979: Make `WasmPtr::get_utf8_string` return `String` r=MarkMcCaskey a=MarkMcCaskey

The old functionality is readded as an `unsafe` method,
`WasmPtr::get_utf8_str`.

~It's important to note that `WasmPtr::get_utf8_string` isn't _entirely_ sound and technically should be marked `unsafe` as well, but this change is a massive improvement over what we had before.~

~For future reference, the reason `WasmPtr::get_utf8_string` still has some soundness issues is that we can't guarantee exclusive access to the memory while parsing in the string, we temporarily hold a `&[u8]` and hope it doesn't get mutated.~ 

~It's possible to implement this method in a more correct way by copying each byte as we read it into a `Vec` and converting that into a String.~

EDIT: 
fixed in b592ed8; `WasmPtr::get_utf8_string` is sound and uses no `unsafe` internally now.

# Review

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


Co-authored-by: Mark McCaskey <[email protected]>
@MarkMcCaskey
Copy link
Contributor Author

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 4, 2021

Already running a review

@MarkMcCaskey
Copy link
Contributor Author

bors r-

@bors
Copy link
Contributor

bors bot commented Jan 4, 2021

Canceled.

@MarkMcCaskey
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Jan 4, 2021
1979: Make `WasmPtr::get_utf8_string` return `String` r=MarkMcCaskey a=MarkMcCaskey

The old functionality is readded as an `unsafe` method,
`WasmPtr::get_utf8_str`.

~It's important to note that `WasmPtr::get_utf8_string` isn't _entirely_ sound and technically should be marked `unsafe` as well, but this change is a massive improvement over what we had before.~

~For future reference, the reason `WasmPtr::get_utf8_string` still has some soundness issues is that we can't guarantee exclusive access to the memory while parsing in the string, we temporarily hold a `&[u8]` and hope it doesn't get mutated.~ 

~It's possible to implement this method in a more correct way by copying each byte as we read it into a `Vec` and converting that into a String.~

EDIT: 
fixed in b592ed8; `WasmPtr::get_utf8_string` is sound and uses no `unsafe` internally now.

# Review

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


Co-authored-by: Mark McCaskey <[email protected]>
@syrusakbary syrusakbary merged commit b05e2f5 into master Jan 4, 2021
@bors bors bot deleted the fix/wasm-ptr-soundness branch January 4, 2021 19:23
@MarkMcCaskey
Copy link
Contributor Author

bors r-

@bors
Copy link
Contributor

bors bot commented Jan 4, 2021

Canceled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔈soundness Bugs causing an unsound API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants