Skip to content

Commit 215b306

Browse files
committed
unique: add truncate
1 parent 14fe0bf commit 215b306

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

blog/2026-02-23-safe-buffer-writing.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ fn main(
2020

2121
Every single invocation is writing a value to a mutable buffer. From the rust perspective, that buffer is just a `&mut [u32]` reference, meaning we have exclusive mutable access to the value and can safely write to it. From the shader perspective, the access is mutable but in no way exclusive as the buffer is shared between all invocations. Which from the rust perspective is a very unsound thing to do.
2222

23+
<!-- truncate -->
24+
2325
The code above demonstrates how safe code is able to make aliased writes: Both the 0th and 1st invocation write to index 0, the 2nd and 3rd invocation write to slot 1, and so on. And aliased writes are very much UB.
2426

2527
I wanted to [redesign our entry point interface](https://github.com/Rust-GPU/rust-gpu/issues/449) from scratch anyway for quite some time, to make it easier to use and eliminate soundness holes like this one. The easiest path forward would be some newtype that offers an `unsafe fn write(&self, index: usize, t: T)` function and offload the guarantee of no aliased writes to the end user. But I also wanted to investigate if there was some API that allows you to write to buffers *safely*, even with some limitations in flexibility.

0 commit comments

Comments
 (0)