Fix strided_scan reading/writing out-of-bounds - #4430
Open
zcbenz wants to merge 2 commits into
Open
Conversation
The strided scan kernel writes out[i * stride + j] for i < shape[axis] and j < stride, so it needs shape[axis] * stride elements. Scan::eval_gpu sized the output with in.data_size() while handing the kernel in.strides(), so a size one axis carrying a padded stride, as a sliced view has, made the kernel write past its allocation. Take the no copy path only when the scanned axis fits and let the rest fall to the existing contiguous copy. The CUDA scan has the same dispatch and the same kernel bound, so it changes too.
4 tasks
zcbenz
force-pushed
the
strided-scan-bounds
branch
from
August 30, 2026 01:31
b478775 to
eeb297c
Compare
Contributor
|
Hi, the pointer forming here is UB (https://eel.is/c++draft/expr.add) ffor lanes where the result points beyond the position immediately after the buffer. in += offset + global_index_x + read_offset_x;
out += offset + global_index_x + read_offset_x;This is only pointer-formation UB, as the later guard correctly prevents the pointers from being dereferenced. |
Member
Author
|
We don't quite care about undefined behaviors when writing GPU kernels, and UB is often abused when we can get a performance gain. Out-of-bound pointers are especially common in GPU kernels. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The GPU
strided_scankernels can read/write out-of-bounds when the scanned axis is a slice. The test is from #4254.Note that this is only a correctness fix, and does not try to optimize the mentioned case which currently has most of the threads wasted. At the moment I'm conservative on complicating the kernel for a crafted edge case.