You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(cuda.core): declare C++ bool returns as cpp_bool in _rt - #3642
Cython modules in cuda.core don't call each other's C++ code directly. They call it through function pointers whose types come from _rt.pxd, not from the C++ header. If the .pxd says a function returns bint (a C int) but the C++ function returns bool, the caller reads a full 32-bit register where the callee only set the low byte.
On MSVC, a bool return sets only AL. The rest of EAX is whatever it held before. If those upper bits are non-zero, a false is read as true. For example, MSVC can return false with a bare xor al, al while EAX still holds the low bits of a pointer. The caller then sees a non-zero int.
Four functions in _rt.pxd and _rt.pyx have this mismatch:
get_event_timing_enabled
get_event_is_blocking_sync
get_event_ipc_enabled
mem_allocation_is_imported
They are declared bint but return C++ bool. They compile to movzx today, which zero-extends AL into EAX, so they behave correctly by luck. Nothing is broken now. A different compiler version, optimization level, or inlining decision could make any of them return a wrong value on Windows. That kind of failure would be platform-specific and hard to diagnose.
This PR declares all four as cpp_bool, so the declared return type matches the C++ signature. It also adds a comment in _rt.pxd saying that a C++ bool return must be declared cpp_bool, never bint, and why. The change is mechanical and has no behavior change with current compilers.
Consumer modules call the functions declared in _rt.pxd through Cython
function pointers typed from the .pxd, not from the C++ header. Four
functions that return C++ bool were declared bint (int), so callers read
all of EAX while MSVC sets only AL for a bool return. The event getters
and mem_allocation_is_imported happen to compile to movzx today, but have
the same mismatch, and any future bool-returning function that MSVC
compiles to a bare `xor al, al` would read as true on Windows.
Declare all four as cpp_bool, and note the rule in _rt.pxd.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add a pre-commit hook that collects the functions returning bool in the
C++ sources under cuda_core/cuda/core and rejects any Cython declaration
of them that returns bint. A consumer calls a cimported function through
a pointer typed from the .pxd, and MSVC sets only the low byte of the
return register for a bool, so a bint declaration can read false as true
on Windows.
The check scans the whole tree on every run because the mismatch spans a
C++ header and a Cython file, so passing only the changed files would
miss an edit to one side.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Corrected boolean results for event settings and imported memory allocation status, preventing these values from being reported incorrectly on Windows.
Improved consistency in how these status values are returned, helping applications receive accurate information when checking event configuration and memory allocation details.
Four Cython declarations now use cpp_bool to match their C++ return types. A new checker finds mismatched C++ bool and Cython bint declarations. A pre-commit hook runs the checker for relevant source changes.
The checker scans C++ and Cython declarations, reports mismatches with file paths and line numbers, and returns a failure status when it finds violations. The pre-commit hook runs the checker for matching source changes.
Suggested reviewers:mdboom
Change: Refactor · Severity of issue fixed: Low
Merge Risk: Low · up to 44bea
The Cython return type changes are safe and should not change behavior. The new pre-commit checker matches names without regard to namespace. It could wrongly reject a valid binding in the future, which would block a commit but not affect runtime behavior.
Security architecture risk: Minimal · up to 44bea
The change makes four native Boolean results agree with their C++ definitions. It preserves the existing handle-export rules and adds a source check for future mismatches. The review found no new access path or weakened control in the inspected change.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
inferred — The security-relevant runtime effect is limited to how existing callers interpret event and allocation metadata for IPC export. The change adds no export caller or new handle-sharing path.
Trust Boundaries and Controls
observed — The existing event path checks that the event is open and, when creating a descriptor, checks IPC eligibility before asking the CUDA driver for an export handle. The existing memory path checks IPC eligibility and rejects imported allocations before entering the export loop.
observed — The hook invokes a fixed script without passing filenames. Its command-line path scans the configured CUDA source subtree as text and reports declaration matches through diagnostics and an exit code.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Reviewing files that changed from the base of the PR and between a30444c and 44bea03.
Files selected for processing: 4
.pre-commit-config.yaml
cuda_core/cuda/core/_rt.pxd
cuda_core/cuda/core/_rt.pyx
toolshed/check_cython_bool_returns.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Heavy lift
Do not identify C++ functions by their unqualified name alone.
If a header contains flags::get() returning bool and counts::get() returning int, a correct Cython declaration of counts::get() as bint fails this check. The collector stores only get, and this lookup ignores the extern block's namespace.
Match the declaration to its C++ namespace and overload before rejecting its return type. Otherwise, the hook rejects valid bindings.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. The pre-commit hook seems useful. But is there a way we would also test something against the compiled modules to make sure they are behaving correctly with this change?
LGTM. The pre-commit hook seems useful. But is there a way we would also test something against the compiled modules to make sure they are behaving correctly with this change?
We are using unit tests to check behavior. Do you have something else in mind?
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
bugSomething isn't workingcuda.coreEverything related to the cuda.core module
2 participants
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.
Cython modules in
cuda.coredon't call each other's C++ code directly. They call it through function pointers whose types come from_rt.pxd, not from the C++ header. If the.pxdsays a function returnsbint(a Cint) but the C++ function returnsbool, the caller reads a full 32-bit register where the callee only set the low byte.On MSVC, a
boolreturn sets only AL. The rest of EAX is whatever it held before. If those upper bits are non-zero, afalseis read astrue. For example, MSVC can returnfalsewith a barexor al, alwhile EAX still holds the low bits of a pointer. The caller then sees a non-zeroint.Four functions in
_rt.pxdand_rt.pyxhave this mismatch:get_event_timing_enabledget_event_is_blocking_syncget_event_ipc_enabledmem_allocation_is_importedThey are declared
bintbut return C++bool. They compile tomovzxtoday, which zero-extends AL into EAX, so they behave correctly by luck. Nothing is broken now. A different compiler version, optimization level, or inlining decision could make any of them return a wrong value on Windows. That kind of failure would be platform-specific and hard to diagnose.This PR declares all four as
cpp_bool, so the declared return type matches the C++ signature. It also adds a comment in_rt.pxdsaying that a C++boolreturn must be declaredcpp_bool, neverbint, and why. The change is mechanical and has no behavior change with current compilers.🤖 Generated with Claude Code