Repository navigation
safetensors: copy only whole elements on the unaligned path - #3965
apollo-2006 wants to merge 1 commit into
Conversation
`convert_slice` rounds the element count down:
let elem_count = data.len() / size_in_bytes;
so the `Vec<T>` built on the unaligned path holds `elem_count * size_of::<T>()`
bytes. The copy that fills it moved `data.len()` bytes instead:
let mut c: Vec<T> = Vec::with_capacity(elem_count);
std::ptr::copy_nonoverlapping(data.as_ptr(), c.as_mut_ptr() as *mut u8, data.len());
Whenever `data.len()` is not a multiple of the element size, that writes past
the end of the allocation. `Tensor::from_raw_buffer` is public and safe and
passes caller-supplied bytes straight through, so a seven-byte F32 buffer at an
unaligned address is enough. Miri:
error: Undefined Behavior: memory access failed: attempting to access 7
bytes, but got alloc42051 which is only 4 bytes from the end of the
allocation
--> candle-core/src/safetensors.rs:133:12
candle_core::safetensors::convert_slice::<f32>
Copy `elem_count * size_of::<T>()` bytes instead. That is exactly the capacity
allocated, and it matches the aligned branch, which already exposes only the
first `elem_count` whole elements and ignores a trailing partial one. Well
formed input is unaffected, since its length is a multiple of the element size.
`convert_slice_with_cast` carries the same pattern and is changed the same way.
Its only caller path is a safetensors tensor view, and the parser checks that a
tensor's byte span agrees with its shape, so no reachable trigger for that one
could be constructed. It is corrected for consistency.
Distinct from huggingface#3534, which is about a shape that disagrees with the storage
element count and surfaces as a panic rather than as undefined behaviour. The
overflow here happens during the copy, before any shape is validated, and the
test case has a shape that matches its element count.
Adds four tests. The trailing-bytes case reports the overflow under Miri without
this change; the other three cover correct data on the aligned and unaligned
paths and pass either way.
|
@ivarflakstad you have been merging most of candle-core lately, so you are probably the right person for the one thing I left open in the description. I fixed the copy to match the allocation rather than rejecting a non-multiple length, because the aligned branch already truncates a trailing partial element silently and changing that is a wider contract question. I want to check that was the right call. It is not settled by the fixes for #3534 either. Both open ones, #3572 and #3615, check the declared shape inside So: is silently dropping a trailing partial element the contract you want for |
convert_slicerounds the element count down:so the
Vec<T>built on the unaligned path holdselem_count * size_of::<T>()bytes. The copy that fills it moveddata.len()bytes instead:Whenever
data.len()is not a multiple of the element size, that writes past the end of the allocation.Reachable from safe code
Tensor::from_raw_bufferis public and passes caller-supplied bytes straight through, so a seven-byte F32 buffer at an unaligned address is enough to trigger it. It returnsOkwith no indication anything went wrong. Miri:This is a write, not a read.
Fix
Copy
elem_count * size_of::<T>()bytes. That is exactly the capacity allocated, and it matches the aligned branch, which already exposes only the firstelem_countwhole elements and ignores a trailing partial one. The two branches now agree. Well formed input is unaffected, since its length is a multiple of the element size.I kept this to making the copy match the allocation rather than rejecting a non-multiple length outright, because the aligned branch already silently truncates and changing that is a wider behaviour question. Happy to make both branches reject instead if you would prefer that contract.
convert_slice_with_castcarries the same pattern and is changed the same way. Being straight about its reachability: its only caller path is a safetensors tensor view, and the parser checks that a tensor's byte span agrees with its shape, so I could not construct a trigger for that one. It is corrected for consistency, not because I have a repro.Not a duplicate of #3534
#3534 is about a declared shape disagreeing with the storage element count, and its own text notes it "yields a panic, not undefined behavior". This overflow happens during the copy, before any shape is looked at, and the failing test uses shape
[1]withelem_count == 1, so the shapes agree. A fix for #3534 would not address it.Tests
candle-core/tests/safetensors_convert_tests.rs. The unaligned path is forced by placing the payload at an odd address, which is never 2-, 4- or 8-aligned on any allocator, so the tests do not depend on allocator behaviour.unaligned_trailing_bytes_do_not_overflowunaligned_data_loads_correct_valuesunaligned_wide_dtype_loads_correct_valuesaligned_data_loads_correct_valuesThe three that pass either way are controls showing correct data still loads correctly on both branches.
Verified:
cargo test -p candle-core232 passed 0 failed,cargo clippy -p candle-core --all-targets -- -D warningsclean,cargo fmt --checkclean, andcargo +nightly miri test -p candle-core --test safetensors_convert_testsreports no undefined behaviour with the change and one error without it.