#!/usr/bin/env python3 """Bounded Linux API observation, not a benchmark or crash test.""" import ctypes import datetime import errno import hashlib import json import os from pathlib import Path import platform import sys import subprocess import tempfile READ, WRITE = 22, 23 # Linux UAPI MADV_POPULATE_READ/WRITE. PAGE = os.sysconf("SC_PAGE_SIZE") libc = ctypes.CDLL(None, use_errno=True) libc.mmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_long] libc.mmap.restype = ctypes.c_void_p libc.madvise.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int] libc.madvise.restype = ctypes.c_int libc.munmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t] libc.munmap.restype = ctypes.c_int def presence(address, count): with open("/proc/self/pagemap", "rb", buffering=0) as stream: data = os.pread(stream.fileno(), count * 8, (address // PAGE) * 8) if len(data) != count * 8: raise RuntimeError("Incomplete pagemap read") return [bool(int.from_bytes(data[i:i + 8], sys.byteorder) >> 63) for i in range(0, len(data), 8)] def observe(kind, protection, advice, repetition): count = 2 if kind == "shared-beyond-eof" else 1 length = PAGE * count with tempfile.TemporaryFile() as backing: payload = b"P" * PAGE if kind != "anonymous": backing.write(payload) backing.flush() # Linux MAP_PRIVATE|MAP_ANONYMOUS, MAP_SHARED or MAP_PRIVATE. flags = 34 if kind == "anonymous" else (1 if kind == "shared-beyond-eof" else 2) fd = -1 if kind == "anonymous" else backing.fileno() address = libc.mmap(None, length, protection, flags, fd, 0) if address == ctypes.c_void_p(-1).value: raise OSError(ctypes.get_errno(), "mmap failed") try: before = presence(address, count) ctypes.set_errno(0) result = libc.madvise(address, length, advice) error = ctypes.get_errno() if result == -1 else 0 after = presence(address, count) unchanged = None if kind == "anonymous" else ( os.pread(fd, PAGE + 1, 0) == payload and os.fstat(fd).st_size == PAGE) return { "repetition": repetition, "mapping": kind, "protection": protection, "mapping_flags": flags, "advice": "READ" if advice == READ else "WRITE", "return_value": result, "errno": error, "errno_name": errno.errorcode.get(error, "OK"), "present_before": before, "present_after": after, "file_bytes_and_size_unchanged": unchanged, } finally: if libc.munmap(address, length): raise OSError(ctypes.get_errno(), "munmap failed") def main(): if platform.system() != "Linux": raise RuntimeError("This probe requires Linux") rows = [observe(kind, prot, advice, repetition) for repetition in range(1, 4) for kind, prot in (("anonymous", 1), ("anonymous", 3), ("private-file", 3), ("shared-beyond-eof", 3)) for advice in (READ, WRITE)] print(json.dumps({ "schema_version": 1, "captured_utc": datetime.datetime.now(datetime.timezone.utc).isoformat(), "source_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "environment": {"kernel": platform.release(), "architecture": platform.machine(), "python": platform.python_version(), "libc": platform.libc_ver(), "page_size_bytes": PAGE, "temporary_directory": tempfile.gettempdir(), "backing_filesystem": subprocess.run( ["findmnt", "-T", tempfile.gettempdir(), "-n", "-o", "FSTYPE"], check=True, text=True, capture_output=True).stdout.strip()}, "repetitions": 3, "cases_per_repetition": 8, "limits": "One host, small mappings, no concurrent mutation, memory pressure, latency, huge-page, crash or persistence test. No address beyond EOF is dereferenced. Presence is sampled, not a future residency guarantee. File-byte equality does not measure dirty state or allocation.", "observations": rows, }, indent=2)) if __name__ == "__main__": main()