Repository navigation
Fix memset into the first field or element of an aggregate - #705
Conversation
a6b89af to
94bf139
Compare
LegNeato
left a comment
There was a problem hiding this comment.
Thanks for tracking this down! I think reusing adjust_pointer_for_sized_access is the right approach. A few things I'd like to tighten up before merging:
- Return immediately after
self.errrather than continuing to emit potentially incorrect stores. - Make sure the fallback when pointer adjustment fails is sound. The size being a multiple of the pointee size doesn't necessarily mean we can represent the requested access.
- Document why eliding an
undeffill is sound. - Add coverage for writes larger than the pointee, dynamically sized fills, and zero-sized types. It would also be good to test the runtime fill-byte path.
Some of these may be preexisting issues but I'd like to make sure we aren't introducing new ways to silently miscompile.
`rustc` lowers `[v; N]` to `memset` when all bytes of `v` are equal. For a destination at offset 0 of a bigger aggregate (`s.arr = [0; 4]`, `table[0] = [0.0; 3]`), the pointer still has the whole aggregate as its pointee type, so the store count was 0 and nothing was written. `memset` now looks for the field or element of the fill's size first, with `adjust_pointer_for_sized_access`, like `memcpy`, and returns early for a size of 0. If there is no such field or element and the size is not a multiple of the pointee size (e.g. the smaller field of a union), the fill is an error instead of being ignored.
94bf139 to
9fa9b5d
Compare
A size that is a multiple of the pointee size does not show that the fill
stays inside one array: with `S { arr: [u32; 4], t: u32 }`,
`write_bytes(&raw mut s.arr[3], 0, 2)` stored through `arr[4]`.
`memset` now stores one value of a type whose size is equal to the fill
size. Every other constant-size fill is an error, and nothing is emitted
after the error.
Also document why a fill with an undefined byte is skipped.
- `fill_runtime_byte`: a fill byte that is only known at run time. It runs on Ash, since it needs `Int8`. - `write_bytes_larger_than_pointee`: a fill larger than the pointee is an error. - `write_bytes_zero_size`: a zero-sized pointee and a count of zero. - `write_bytes_dynamic_size`: a run-time size does not compile.
|
Thank you for the review! I addressed the notes in two new commits.
Done.
You're right, a multiple is not enough: with Proving the bounds in The bug itself is not in
Done. An undefined value "evaluates to any value of the given type" (https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#UndefinedValue), so the value that is already there is a valid result of the store.
Done in the last commit:
|
A repeat array assigned to the first field of a struct, or to the first element of an array of arrays, writes nothing in a shader:
There is no error and no warning.
memset(builder/builder_methods.rs) now adjusts the destination withadjust_pointer_for_sized_access, likememcpydoes. A fill that still cannot be done (e.g. the smaller field of aunion) is a compile error instead of being ignored.The new difftest
lang/core/array/fill_offset_zerofails without the fix. The compiletestinit_array_union_fieldcovers the error.The fix and the tests were written with an AI coding agent. I reviewed and checked it.
Details
The code emitted for
resetbefore the fix:After:
Only the fills that
rustclowers tomemsetare affected: a constant whose bytes are all equal (0,0.0,u32::MAX) or a run-timeu8.[1; 4], element 1 instead of element 0, and a struct whose only field is the array were right before.The code emitted for a fill of a whole variable and for a fill at a non-zero offset does not change. For a struct whose only field is the array, the
OpStorenow takes the array constant directly, without theOpCompositeExtractandOpCompositeConstructsequence before it. The shader modules of the other difftests are byte-identical.The struct cases were a compile error (
memset on structs not implemented yet) before e3a26ef. I checked it by reverting that change locally. The array of arrays case wrote nothing before it as well.The error for the
unioncase:I used
self.errfor it, likememsetdoes for unsupported flags. Let me know if you prefer a zombie there.memsetwith an undefined fill byte now returns early.rustcemits it for[const { MaybeUninit::uninit() }; N]. Before the fix such a fill of the first field wrote nothing by accident, which is right for it. With the pointer adjustment it would reachmemset_dynamic_pattern, which needsInt8, so code that built before would fail. The compiletestinit_array_uninitcovers it. Itsclear_wholecase did not build before.memsetwith a size of 0 now returns early, likememcpy.memsetwith a run-time size (memset_dynamic_size) is not changed.I ran the difftest on lavapipe only.