-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 'fn write_u16s' to Memory #70397
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @RalfJung |
src/librustc_mir/interpret/memory.rs
Outdated
let size = Size::from_bytes(src.size_hint().0 as u64); | ||
let size = Size::from_bytes(u64::try_from(src.size_hint().0).unwrap()); |
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.
Good thought, but I already took care of that in #70226 so this will just cause merge conflict. Better to not do this change.
src/librustc_mir/interpret/memory.rs
Outdated
assert!(src.size_hint().1.is_some()); // Check that the iterator has an upper bound. | ||
let size = Size::from_bytes(u64::try_from(src.size_hint().0).unwrap()); |
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.
Please don't call size_hint
twice.
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.
The following snippet copied from here does the right thing:
let (lower, upper) = src.size_hint();
let len = upper.expect("can only write bounded iterators");
assert_eq!(lower, len, "can only write iterators with a precise length");
src/librustc_mir/interpret/memory.rs
Outdated
let size = Size::from_bytes(u64::try_from(src.size_hint().0).unwrap()); | ||
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(2).unwrap())? { | ||
Some(ptr) => ptr, | ||
None => return Ok(()), // zero-sized access |
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.
Hm... in this case we fail to check that the iterator is truly empty. And write_bytes
above has the same bug in fact.
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.
The check that is needed here is something like
src.next().expect_none("iterator said it was empty but returned an element")
src/librustc_mir/interpret/memory.rs
Outdated
let tcx = self.tcx.tcx; | ||
let allocation = self.get_raw_mut(ptr.alloc_id)?; | ||
|
||
for (idx, item) in src.enumerate() { |
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.
This fails to check that the size_hint
did not lie.
I think it is better to loop up to len
and make sure the iterator did not lie:
for idx in 0..len {
let val = Scalar::from_u16(src.next().unwrap());
...
}
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Please rebase, a PR just landed that conflicts with this one. |
☔ The latest upstream changes (presumably #70404) made this pull request unmergeable. Please resolve the merge conflicts. |
src/librustc_mir/interpret/memory.rs
Outdated
} | ||
Ok(()) |
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.
After the loop, please make sure that the iterator is empty like here.
src/librustc_mir/interpret/memory.rs
Outdated
let val = Scalar::from_u16( | ||
src.next().expect("iterator was shorter than it said it would be"), | ||
); | ||
let offset_ptr = ptr.offset(Size::from_bytes(idx.checked_mul(2).unwrap()), &tcx)?; |
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.
let offset_ptr = ptr.offset(Size::from_bytes(idx.checked_mul(2).unwrap()), &tcx)?; | |
let offset_ptr = ptr.offset(Size::from_bytes(idx) * 2, &tcx)?; // `Size` multiplication |
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.
Looking good. :) Please squash, then I'll r+.
I force-pushed the squashed commit. Thank you for all the reviews! 😸 |
@bors r+ |
📌 Commit 4538f89 has been approved by |
add 'fn write_u16s' to Memory Added new function `Memory::write_u16s`. Needed in `MIRI` for implementing helper function to write wide_str to memory (for Windows).
Rollup of 6 pull requests Successful merges: - rust-lang#70384 (Refactor object file handling) - rust-lang#70397 (add 'fn write_u16s' to Memory) - rust-lang#70413 (Fix incorrect pattern warning "unreachable pattern") - rust-lang#70428 (`error_bad_item_kind`: add help text) - rust-lang#70429 (Clean up E0459 explanation) - rust-lang#70437 (Miri float->int casts: be explicit that this is saturating) Failed merges: r? @ghost
Added new function
Memory::write_u16s
. Needed inMIRI
for implementing helper function to write wide_str to memory (for Windows).