Skip to content

Commit 732ab41

Browse files
committed
cuda.core: validate accessed_by kinds before cuMemAdvise
Dry-run location-kind checks in the ManagedBuffer.accessed_by setter so bulk assignment cannot partially mutate driver state when an invalid Host NUMA variant appears in the target set (Glasswing V19.1).
1 parent c287369 commit 732ab41

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

cuda_core/cuda/core/_memory/_managed_buffer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from cuda.core._device import Device
1010
from cuda.core._host import Host
1111
from cuda.core._memory._buffer import Buffer
12+
from cuda.core._memory._managed_location import _coerce_location
1213
from cuda.core._memory._managed_memory_ops import (
1314
_advise_one,
1415
_do_single_discard_prefetch_py,
@@ -227,6 +228,11 @@ def accessed_by(self, locations: Iterable[Device | Host]) -> None:
227228
if not isinstance(loc, (Device, Host)):
228229
raise TypeError(f"accessed_by entries must be Device or Host, got {type(loc).__name__}")
229230
target.add(loc)
231+
for loc in target:
232+
spec = _coerce_location(loc)
233+
assert spec is not None
234+
if spec.kind not in ("device", "host"):
235+
raise ValueError(f"advise {_SET_ACCESSED_BY.name} does not support location_type='{spec.kind}'")
230236
current = set(_query_accessed_by(self))
231237
for loc in current - target:
232238
_advise_one(self, _UNSET_ACCESSED_BY, loc)

cuda_core/tests/memory/test_managed_ops.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,25 @@ def test_accessed_by_set_assignment(self, location_ops_device, external_managed_
426426
buf.accessed_by = set()
427427
assert device not in buf.accessed_by
428428

429+
def test_accessed_by_set_assignment_validates_kind_before_mutation(
430+
self, location_ops_device, external_managed_buffer
431+
):
432+
"""Invalid kinds in bulk assignment must not partially mutate driver state."""
433+
device = location_ops_device
434+
buf = external_managed_buffer
435+
buf.accessed_by = {device, Host()}
436+
assert device in buf.accessed_by
437+
assert Host() in buf.accessed_by
438+
439+
with pytest.raises(
440+
(ValueError, TypeError),
441+
match=r"does not support location_type='host_numa'|cuda-bindings 13\.0\+",
442+
):
443+
buf.accessed_by = {device, Host(numa_id=0)}
444+
445+
assert device in buf.accessed_by
446+
assert Host() in buf.accessed_by
447+
429448
def test_instance_prefetch(self, location_ops_device, managed_buffer):
430449
device = location_ops_device
431450
buf = managed_buffer

0 commit comments

Comments
 (0)