Skip to content

Fix memset into the first field or element of an aggregate - #705

Merged
LegNeato merged 3 commits into
Rust-GPU:mainfrom
greenhat:fix-memset-first-field
Dec 1, 2026
Merged

LegNeato merged 3 commits into
Rust-GPU:mainfrom
greenhat:fix-memset-first-field

Conversation

@greenhat

@greenhat greenhat commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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:

#[repr(C)]
struct S {
    arr: [u32; 4],
    t: u32,
}

fn reset(s: &mut S) {
    s.arr = [0; 4]; // `s.arr` keeps its old values
}

let mut table = [[1.0f32; 3]; 3];
table[0] = [0.0; 3]; // row 0 is still 1.0

There is no error and no warning.

memset (builder/builder_methods.rs) now adjusts the destination with adjust_pointer_for_sized_access, like memcpy does. A fill that still cannot be done (e.g. the smaller field of a union) is a compile error instead of being ignored.

The new difftest lang/core/array/fill_offset_zero fails without the fix. The compiletest init_array_union_field covers the error.

The fix and the tests were written with an AI coding agent. I reviewed and checked it.

Details

The code emitted for reset before the fix:

%1 = OpFunction %2 None %3
%4 = OpFunctionParameter %5
%6 = OpLabel
     OpReturn
     OpFunctionEnd

After:

%1 = OpFunction %2 None %3
%4 = OpFunctionParameter %5
%6 = OpLabel
%8 = OpInBoundsAccessChain %9 %4 %10
     OpStore %8 %11
     OpReturn
     OpFunctionEnd

Only the fills that rustc lowers to memset are affected: a constant whose bytes are all equal (0, 0.0, u32::MAX) or a run-time u8. [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 OpStore now takes the array constant directly, without the OpCompositeExtract and OpCompositeConstruct sequence 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 union case:

error: memset of 8 bytes into `[u32; 4]` is not supported yet
  --> $DIR/init_array_union_field.rs:14:5
   |
LL |     words.two = [0; 2];
   |     ^^^^^^^^^^^^^^^^^^

I used self.err for it, like memset does for unsupported flags. Let me know if you prefer a zombie there.

memset with an undefined fill byte now returns early. rustc emits 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 reach memset_dynamic_pattern, which needs Int8, so code that built before would fail. The compiletest init_array_uninit covers it. Its clear_whole case did not build before.

memset with a size of 0 now returns early, like memcpy. memset with a run-time size (memset_dynamic_size) is not changed.

I ran the difftest on lavapipe only.

@greenhat
greenhat force-pushed the fix-memset-first-field branch 4 times, most recently from a6b89af to 94bf139 Compare November 17, 2026 08:59

@LegNeato LegNeato left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.err rather 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 undef fill 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.
@greenhat
greenhat force-pushed the fix-memset-first-field branch from 94bf139 to 9fa9b5d Compare November 30, 2026 13:26
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.
@greenhat

Copy link
Copy Markdown
Contributor Author

Thank you for the review! I addressed the notes in two new commits.

Return immediately after self.err rather than continuing to emit potentially incorrect stores.

Done.

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.

You're right, a multiple is not enough: with S { arr: [u32; 4], t: u32 }, write_bytes(&raw mut s.arr[3], 0, 2) stored through arr[4].

Proving the bounds in memset would need the access chain behind the pointer and the length of its array, which is a lot of code for this path. So I went the other way: memset now stores one value of a type whose size is equal to the fill size, and every other constant-size fill is a compile error. This also rejects fills that were right on main, e.g. write_bytes(&raw mut a[1], 0, 2). As far as I can tell, only the core::intrinsics::write_bytes intrinsic reaches this path. Let me know if you want to keep these fills.

The bug itself is not in memset: *core::intrinsics::offset(&raw mut s.arr[3], 1) = 0 also stores through arr[4]. ptr_offset_strided adds the offset to the last index of the access chain without a bounds check. I can open a separate issue for it if you want.

Document why eliding an undef fill is sound.

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.

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.

Done in the last commit:

  • writes larger than the pointee are an error now - write_bytes_larger_than_pointee;
  • the run-time fill byte - a new difftest, fill_runtime_byte;
  • zero-sized types - write_bytes_zero_size;
  • a run-time size does not compile, on main as well - write_bytes_dynamic_size has the current errors.

@greenhat
greenhat requested a review from LegNeato November 30, 2026 16:26

@LegNeato LegNeato left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks! 🍻

@LegNeato
LegNeato added this pull request to the merge queue Dec 1, 2026
Merged via the queue into Rust-GPU:main with commit e907ee2 Dec 1, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants