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

interpret/memory: fix safety comment for large array memset optimization #120387

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
/// to the allocation it points to. Supports both shared and mutable references, as the actual
/// checking is offloaded to a helper closure.
///
/// If this returns `None`, the size is 0; it can however return `Some` even for size 0.
/// Returns `None` if and only if the size is 0.
fn check_and_deref_ptr<T>(
&self,
ptr: Pointer<Option<M::Provenance>>,
Expand Down Expand Up @@ -1214,10 +1214,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let size_in_bytes = size.bytes_usize();
// For particularly large arrays (where this is perf-sensitive) it's common that
// we're writing a single byte repeatedly. So, optimize that case to a memset.
if size_in_bytes == 1 && num_copies >= 1 {
// SAFETY: `src_bytes` would be read from anyway by copies below (num_copies >= 1).
// Since size_in_bytes = 1, then the `init.no_bytes_init()` check above guarantees
// that this read at type `u8` is OK -- it must be an initialized byte.
if size_in_bytes == 1 {
debug_assert!(num_copies >= 1); // we already handled the zero-sized cases above.
// SAFETY: `src_bytes` would be read from anyway by `copy` below (num_copies >= 1).
let value = *src_bytes;
dest_bytes.write_bytes(value, (size * num_copies).bytes_usize());
Copy link
Contributor

Choose a reason for hiding this comment

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

This line seems more dangerous and we don't have a comment on it at all 😆 I'll try to add one this week

} else if src_alloc_id == dest_alloc_id {
Expand Down
Loading