Skip to content

fix(core): reject mismatched UUID in register() even under -O - #2704

Merged
Andy-Jost merged 5 commits into
NVIDIA:mainfrom
waterWang:fix-2697-register-uuid-assert
Oct 31, 2026
Merged

Andy-Jost merged 5 commits into
NVIDIA:mainfrom
waterWang:fix-2697-register-uuid-assert

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Description

closes #2697

DeviceMemoryResource.register() and PinnedMemoryResource.register()
validated the supplied UUID against the resource's own with a bare
assert in MP_register (cuda_core/cuda/core/_memory/_ipc.pyx).
Since CPython strips assertions when run with -O, the check silently
disappears in optimized interpreters: the resource is registered under
a foreign key and its own UUID is rewritten to the attacker-
controlled value (mr.uuid reports the new value from then on).

Changes

  • _ipc.pyx MP_register: replace assert self.uuid is None or self.uuid == uuid with an explicit ValueError when the supplied
    UUID does not match the resource's own.
  • _ipc.pyx MP_from_allocation_handle: convert the internal
    assert registered is self to an explicit RuntimeError, matching
    the maintainer direction to remove bare asserts from the library
    (they are behavioral no-ops under -O).
  • cuda_core/tests/memory_ipc/test_errors.py: add
    test_register_rejects_mismatched_uuid covering the rejection and
    verifying the resource's UUID is not rewritten on failure.

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

…#2697)

DeviceMemoryResource.register() and PinnedMemoryResource.register()
validated the supplied UUID against the resource's own with a bare
`assert`, which CPython removes under `-O`. A mismatched UUID was then
accepted, registered as a foreign registry key, and rewritten into the
resource's own UUID, silently corrupting the IPC registry.

Replace the assertion with an explicit ValueError so the check is
enforced regardless of the interpreter's assertion mode, and convert
the internal register() result check in MP_from_allocation_handle to
an explicit RuntimeError as well.

Signed-off-by: waterWang <waterWang@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 26, 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 Aug 26, 2026
@Andy-Jost Andy-Jost added bug Something isn't working P1 Medium priority - Should do labels Oct 23, 2026
@Andy-Jost Andy-Jost added this to the cuda.core 1.3.0 milestone Oct 23, 2026

@Andy-Jost Andy-Jost 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.

Thank you, and sorry for the wait. The change is right. Three items before merge:

  1. test_register_rejects_mismatched_uuid runs for every kind in the ipc_memory_resource fixture, which now includes VirtualMemoryResource (#3028, merged after this PR was opened). That class has no register(), so the test errors there. Add the skip the neighbouring test uses: if not _is_pool(mr): pytest.skip("allocation handles and the registry are pool-only").
  2. The internal-invariant message: the convention in cuda_core/CLAUDE.md is RuntimeError("internal cuda.core error, please report: ..."), so the two kinds of error read the same way everywhere.
  3. A release-note entry in cuda_core/docs/source/release/1.3.0-notes.rst, section "Fixes and enhancements", in the style of its neighbours, and a Raises section on the two register() docstrings (ValueError for a mismatched UUID; RuntimeError when the resource is not IPC-enabled).

The branch is 281 commits behind main; a merge of main is enough, no rebase needed. If you prefer, I can push the merge and these three items to your branch (edits by maintainers are enabled on the PR); say the word. CI has not run yet: outside contributions need a maintainer to approve the workflow runs, and GitHub queues them when the branch is updated. I will approve them as soon as they appear.

@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test e603e0e

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor
Doc Preview CI
Preview removed because the pull request was closed or merged.

…(cuDSS 1.0 library names)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@Andy-Jost

Copy link
Copy Markdown
Contributor

I merged main into this branch to pick up #3188 (cuDSS 1.0 library names), which the pathfinder CI step needs now. No other change. CI runs again on the new head.

@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test 4eecdbc

@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 620afec and 736393d.

Files selected for processing: 1
  • cuda_core/tests/memory_ipc/test_errors.py
Files skipped from review as they are similar to previous changes: 1
  • cuda_core/tests/memory_ipc/test_errors.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Invalid IPC memory registration now raises clear errors when a resource mapping does not match or the resource is registered with a conflicting UUID.
    • Conflicting UUIDs are rejected without changing the resource’s existing UUID. Registration using its matching UUID remains available.
  • Documentation
    • Registration documentation now describes the errors raised when a resource is not IPC-enabled or already has a different UUID.

important: IPC registration now raises explicit exceptions for mismatched UUIDs and unexpected resources. The change also updates registration documentation, release notes, and test coverage.

Changes: IPC memory-resource registration

Layer / File(s) Summary
Registration checks and coverage
cuda_core/cuda/core/_memory/_ipc.pyx, cuda_core/tests/memory_ipc/test_errors.py, cuda_core/cuda/core/_memory/_device_memory_resource.pyi, cuda_core/cuda/core/_memory/_device_memory_resource.pyx, cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi, cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx, cuda_core/docs/source/release/1.3.0-notes.rst
MP_register raises ValueError when a resource has a different non-None UUID. MP_from_allocation_handle raises RuntimeError if registration returns a different resource. The test checks rejection, UUID preservation, and registration under the resource’s own UUID. The API documentation and release notes describe the exception conditions.

Assessment against linked issues

Objective Addressed Explanation
Reject registration under a mismatched UUID, including when Python runs with -O [2697] ✅

Possibly related issues

Suggested reviewers: andy-jost

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Moderate · up to 73639

Registering a resource under another resource’s existing UUID can still succeed instead of raising ValueError. Fix that path before merging.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@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: 2

🧹 Nitpick comments: 1
cuda_core/tests/memory_ipc/test_errors.py: 1

115-115: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: Assert the exception type without matching message text.

MP_register raises ValueError for the UUID mismatch. The message wording is not part of the public contract, and no competing ValueError exists on this exercised pool-resource path. Remove match="already has UUID".


ℹ️ Review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between d5f37e0 and 4eecdbc.

📒 Files selected for processing: 2
  • cuda_core/cuda/core/_memory/_ipc.pyx
  • cuda_core/tests/memory_ipc/test_errors.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

if not self.is_ipc_enabled:
raise RuntimeError("Memory resource is not IPC-enabled")
assert self.uuid is None or self.uuid == uuid
if self.uuid is not None and self.uuid != uuid:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

important: Check the resource UUID before the registry lookup. If a UUID already belongs to another resource, MP_register returns that resource at Lines 477–480. The new check never runs, so mr.register(other_uuid) accepts a UUID that differs from mr.uuid. Move the mismatch check before the existing-entry return.



@pytest.mark.human_authored
def test_register_rejects_mismatched_uuid(ipc_memory_resource):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '1,140p' cuda_core/tests/memory_ipc/test_errors.py
sed -n '425,475p' cuda_core/tests/conftest.py
rg -n 'ipc_memory_resource|supports.*vmm|VirtualMemoryResource' cuda_core/tests/memory_ipc/conftest.py cuda_core/tests/conftest.py

Repository: NVIDIA/cuda-python

Length of output: 8888


🏁 Script executed:

sed -n '470,510p' cuda_core/tests/conftest.py
sed -n '140,230p' cuda_core/cuda/core/_memory/_virtual_memory_resource.pyi
nl -ba cuda_core/tests/memory_ipc/test_errors.py | sed -n '85,125p'

Repository: NVIDIA/cuda-python

Length of output: 7957


important: Restrict this test to pool resources.

When VMM IPC is supported, ipc_memory_resource also yields VirtualMemoryResource. That resource has no uuid or register API, so the test fails at mr.uuid before it checks UUID rejection. Limit this test to the device and pinned fixture parameters.

--- "a/cuda_core/tests/memory_ipc/test_errors.py"
+++ "b/cuda_core/tests/memory_ipc/test_errors.py"
@@ -94,8 +94,9 @@
         DeviceMemoryResource.from_registry(key)
 
 
+@pytest.mark.parametrize("ipc_memory_resource", ["device", "pinned"], indirect=True)
 @pytest.mark.human_authored
 def test_register_rejects_mismatched_uuid(ipc_memory_resource):
     """register() rejects a UUID that does not match the resource's own.
 
     The check is an explicit ValueError (not a bare ``assert``) so it remains

Andy-Jost and others added 2 commits October 30, 2026 14:48
- test_register_rejects_mismatched_uuid skips non-pool resources:
  VirtualMemoryResource joined the ipc_memory_resource fixture and has no
  register().
- The internal-invariant error uses the "internal cuda.core error, please
  report" wording.
- register() on DeviceMemoryResource and PinnedMemoryResource documents
  its RuntimeError and ValueError, and the 1.3.0 release notes record
  the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 4eecdbc and 620afec.

Files selected for processing: 7
  • cuda_core/cuda/core/_memory/_device_memory_resource.pyi
  • cuda_core/cuda/core/_memory/_device_memory_resource.pyx
  • cuda_core/cuda/core/_memory/_ipc.pyx
  • cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi
  • cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx
  • cuda_core/docs/source/release/1.3.0-notes.rst
  • cuda_core/tests/memory_ipc/test_errors.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test 620afec

…register()

A rejected registration that cleared the UUID to None would have passed
the old check (uuid != other). CodeRabbit's suggestion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test 736393d

@Andy-Jost Andy-Jost self-assigned this Oct 31, 2026

@Andy-Jost Andy-Jost 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.

Thanks, @waterWang. The review items and the CodeRabbit note are addressed, and CI is green. Approving.

@Andy-Jost
Andy-Jost merged commit 026f513 into NVIDIA:main Oct 31, 2026
116 checks passed
github-actions Bot pushed a commit that referenced this pull request Oct 31, 2026
Removed preview folders for the following PRs:
- PR #2704
- PR #2718
- PR #3262
- PR #3298
- PR #3299
- PR #3303
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 P1 Medium priority - Should do

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: register() accepts a mismatched UUID under python -O

2 participants