Skip to content

fix(cuda.core): declare C++ bool returns as cpp_bool in _rt - #3642

Merged
juenglin merged 2 commits into
NVIDIA:mainfrom
juenglin:fix-rt-cpp-bool-returns
Nov 30, 2026
Merged

juenglin merged 2 commits into
NVIDIA:mainfrom
juenglin:fix-rt-cpp-bool-returns

Conversation

@juenglin

@juenglin juenglin commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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.

🤖 Generated with Claude Code

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>
@copy-pr-bot

copy-pr-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Nov 27, 2026
@juenglin juenglin added the bug Something isn't working label Nov 27, 2026
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>
@juenglin juenglin self-assigned this Nov 27, 2026
@juenglin juenglin added this to the cuda.core 1.4.0 milestone Nov 27, 2026
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test 44bea03

@juenglin
juenglin marked this pull request as ready for review November 27, 2026 16:50
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

Summary by CodeRabbit

  • Bug Fixes
    • 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.

Changes: Cython Boolean Return Alignment

Layer / File(s) Summary
Align Cython return declarations
cuda_core/cuda/core/_rt.pxd, cuda_core/cuda/core/_rt.pyx
The event metadata getters and imported-allocation getter change from bint to cpp_bool. Comments describe the type requirement and a Windows risk.
Detect mismatched declarations
toolshed/check_cython_bool_returns.py, .pre-commit-config.yaml
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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

Commits

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.

Comment on lines +71 to +72
target = cname.rsplit("::", 1)[-1] if cname else name
if target in bool_functions:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

@github-actions

Copy link
Copy Markdown
Contributor

@mdboom mdboom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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?

@juenglin
juenglin merged commit e8ac1a1 into NVIDIA:main Nov 30, 2026
228 of 230 checks passed
@juenglin

Copy link
Copy Markdown
Contributor Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants