|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""CRC32 over a raw memory range. |
| 5 | +
|
| 6 | +The daemon computes a CRC of each host-tier slot after D2H + sync |
| 7 | +(offload integrity) and verifies it before H2D (restore integrity). |
| 8 | +This catches: |
| 9 | + - DMA tearing / partial writes on the offload side |
| 10 | + - Bit-flips in pinned RAM between offload and restore |
| 11 | + - Stale-pointer reads (e.g., slot index races) — the CRC won't |
| 12 | + match for unrelated bytes |
| 13 | +
|
| 14 | +Implementation: prefer the SIMD-accelerated Rust path (gms_rust_ring, |
| 15 | +~25 GB/s on modern x86), fall back to stdlib zlib.crc32 (~500 MB/s). |
| 16 | +Both produce the same CRC32 (IEEE 802.3 polynomial). |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import ctypes |
| 22 | +import zlib |
| 23 | + |
| 24 | +try: |
| 25 | + from gms_rust_ring import crc32_at_ptr as _rust_crc32_at_ptr |
| 26 | + |
| 27 | + _HAS_RUST = True |
| 28 | +except ImportError: |
| 29 | + _HAS_RUST = False |
| 30 | + |
| 31 | + |
| 32 | +def crc32_at_ptr(ptr: int, size: int) -> int: |
| 33 | + """CRC32 (IEEE) of `size` bytes starting at raw pointer `ptr`. |
| 34 | +
|
| 35 | + Caller MUST hold a reference to the memory keeping it live for |
| 36 | + the duration of this call. In daemon use, the pointer is a |
| 37 | + cudaHostAlloc'd pinned buffer owned by a HostTier slot. |
| 38 | +
|
| 39 | + Returns the 32-bit CRC as a Python int.""" |
| 40 | + if size <= 0: |
| 41 | + return 0 |
| 42 | + if _HAS_RUST: |
| 43 | + return int(_rust_crc32_at_ptr(int(ptr), int(size))) |
| 44 | + # Fallback: zero-copy view via ctypes. |
| 45 | + buf = (ctypes.c_ubyte * size).from_address(int(ptr)) |
| 46 | + return zlib.crc32(buf) & 0xFFFFFFFF |
0 commit comments