|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Test warnings when unpickling IPC-enabled Buffer objects.""" |
| 5 | + |
| 6 | +import warnings |
| 7 | + |
| 8 | +from cuda.core import Buffer |
| 9 | +from cuda.core._utils.cuda_utils import reset_ipc_pickle_warning, warn_ipc_buffer_unpickle |
| 10 | + |
| 11 | +NBYTES = 64 |
| 12 | + |
| 13 | + |
| 14 | +def test_warn_on_buffer_unpickle(ipc_device, ipc_memory_resource): |
| 15 | + """Unpickling an IPC buffer warns about the trust boundary.""" |
| 16 | + mr = ipc_memory_resource |
| 17 | + buf = mr.allocate(NBYTES, stream=ipc_device.default_stream) |
| 18 | + |
| 19 | + with warnings.catch_warnings(record=True) as w: |
| 20 | + warnings.simplefilter("always") |
| 21 | + reset_ipc_pickle_warning() |
| 22 | + Buffer._reduce_helper(mr, buf.ipc_descriptor) |
| 23 | + |
| 24 | + assert len(w) == 1, f"Expected 1 warning, got {len(w)}: {[str(x.message) for x in w]}" |
| 25 | + warning = w[0] |
| 26 | + assert warning.category is UserWarning |
| 27 | + assert "unpickl" in str(warning.message).lower() |
| 28 | + assert "ipc" in str(warning.message).lower() |
| 29 | + assert "trusted" in str(warning.message).lower() |
| 30 | + |
| 31 | + |
| 32 | +def test_ipc_pickle_warning_emitted_only_once(ipc_device, ipc_memory_resource): |
| 33 | + """The unpickle trust-boundary warning is emitted at most once per process.""" |
| 34 | + mr = ipc_memory_resource |
| 35 | + buf1 = mr.allocate(NBYTES, stream=ipc_device.default_stream) |
| 36 | + buf2 = mr.allocate(NBYTES, stream=ipc_device.default_stream) |
| 37 | + |
| 38 | + with warnings.catch_warnings(record=True) as w: |
| 39 | + warnings.simplefilter("always") |
| 40 | + reset_ipc_pickle_warning() |
| 41 | + Buffer._reduce_helper(mr, buf1.ipc_descriptor) |
| 42 | + Buffer._reduce_helper(mr, buf2.ipc_descriptor) |
| 43 | + warn_ipc_buffer_unpickle() |
| 44 | + |
| 45 | + ipc_warnings = [x for x in w if "unpickl" in str(x.message).lower()] |
| 46 | + assert len(ipc_warnings) == 1 |
0 commit comments