Raise OverflowError on out-of-range c_int/c_byte kernel arguments#2402
Open
rparolin wants to merge 2 commits into
Open
Raise OverflowError on out-of-range c_int/c_byte kernel arguments#2402rparolin wants to merge 2 commits into
rparolin wants to merge 2 commits into
Conversation
An out-of-range Python int passed as a c_int/c_byte kernel argument was silently narrowed (e.g. 2**32+5 -> 5). The feeders now range-check via PyLong_AsLongAndOverflow + an explicit 32-bit/8-bit bounds check and raise OverflowError; feed() is declared 'except? -1' so Cython propagates the exception instead of swallowing it. Adds a GPU regression test. Addresses Glasswing finding V9.1; behavior reviewed and shaped by @leofang (single code path via PyLong_AsLongAndOverflow). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
rparolin
added a commit
to rparolin/cuda-python
that referenced
this pull request
Jul 21, 2026
…#2402) The out-of-range c_int/c_byte -> OverflowError change is a distinct behavior change; moved to a standalone PR so it can be reviewed separately from this hardening batch. No functional change to the remaining hardening work. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A reviewer asked why the manual check uses INT_MIN/INT_MAX rather than the function's long-based overflow flag. Add a comment: the target slot is 32-bit (8-bit for c_byte), and long is 64-bit on LP64, so overflow alone misses 2**31..2**63 -- the explicit int-range check catches those. Comment-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
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.
Summary
An out-of-range Python int passed as a
c_int/c_bytekernel argument was silently narrowed — e.g.2**32 + 5was stored as5, so the kernel ran with a value the caller never intended, with no error or warning.The
c_int/c_byteparam feeders now range-check the value and raiseOverflowErrorinstead. Implementation notes:PyLong_AsLongAndOverflow(flags out-of-longvalues through itsoverflowout-param) plus an explicit 32-bit / 8-bit bounds check.feed()is declaredexcept? -1in the.pxdso Cython actually propagates the exception — without that, the raise would be silently swallowed (the same class of bug).Behavior change (please confirm)
This is intentionally stricter than
ctypes' own behavior, which silently wraps out-of-range ints. Raising was chosen deliberately over matchingctypes.Tests
test_kernelParams_c_int_out_of_range_raises(GPU): an in-range value still launches;2**32+5(c_int) and200(c_byte) now raiseOverflowError. Verified on Python 3.14.Context
Addresses Glasswing finding V9.1. Behavior and implementation reviewed/shaped by @leofang (the single-code-path
PyLong_AsLongAndOverflowform was his call). Split out from the hardening batch in #2399 so it can be reviewed on its own.