-
Notifications
You must be signed in to change notification settings - Fork 34
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
rust: support memory manipulation #609
Conversation
Codecov Report
@@ Coverage Diff @@
## master #609 +/- ##
==========================================
+ Coverage 99.32% 99.35% +0.03%
==========================================
Files 72 73 +1
Lines 10691 11388 +697
==========================================
+ Hits 10619 11315 +696
- Misses 72 73 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
e1b3750
to
3934809
Compare
1377814
to
ca8ecb2
Compare
e45448f
to
4eb205c
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.
Copying memory back and forth does not look like good idea. Rust should some kind of mutable slice allowing free access to wasm memory (read/write).
Right. We can definitely have a separate function exposing the underlying memory as an unsafe API, but this is the safe API. It is modelled after other Rust VMs. |
1a2d6d3
to
ae1fa64
Compare
Why exposing the underlying memory is unsafe? |
f9e523c
to
e0952b3
Compare
Exposing a slice obtained via |
Looks good, but still not sure what's best to allow for slice ranges #609 (comment) |
Squashed and added #609 (comment) as a second commit. We can decide this tomorrow. |
bindings/rust/src/lib.rs
Outdated
let offset = offset as usize; | ||
let memory_size = self.memory_size(); | ||
// Empty slices are allowed, but ensure both starting and ending offsets are valid. | ||
if memory_size == 0 || offset > memory_size || (offset + size) > memory_size { |
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.
offset > memory_size
condition is redundant then, when it is true (offset + size) > memory_size
will always be true.
if memory_size == 0 || offset > memory_size || (offset + size) > memory_size { | |
if memory_size == 0 || (offset + size) > memory_size { |
Also for the case of (memory 0)
it would be perhaps logical to allow (0, 0) slice, then the condition should make an error only the absence of memory
if memory_size == 0 || offset > memory_size || (offset + size) > memory_size { | |
if !sys::fizzy_module_has_memory(self.get_module()) || (offset + size) > memory_size { |
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.
offset > memory_size condition is redundant then, when it is true (offset + size) > memory_size will always be true.
Was a bad attempt for overflow check. Decided to just use offset.checked_add(size).is_none()
for overflow check.
bindings/rust/src/lib.rs
Outdated
) -> Result<core::ops::Range<usize>, ()> { | ||
// This should be safe given usize::BITS >= u32::BITS, see https://doc.rust-lang.org/std/primitive.usize.html. | ||
let offset = offset as usize; | ||
let has_memory = unsafe { sys::fizzy_module_has_memory(self.get_module()) }; |
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.
Actually with solution in #728, I would cache this in the Instance { has_memory: bool }
.
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.
Can be for now, but I see some design issues with it.
- The
fizzy_get_instance_memory_size
is called twice. - You almost always wants memory size and pointer in the same time. C should return both in single call.
Introduce unsafe API to acquire memory slices and a safe API to set/get memory.
No description provided.