{"record":{"id":"d9344cd087d73b5d","repo":"pola-rs/polars","slug":"could-not-execute-mprotect-for-cpuid-check","errorCode":null,"errorMessage":"could not execute mprotect for CPUID check","messagePattern":"could not execute mprotect for CPUID check","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_cpu_check.py","lineNumber":199,"sourceCode":"            # On some platforms PROT_WRITE + PROT_EXEC is forbidden, so we first\n            # only write and then mprotect into PROT_EXEC.\n            libc = _open_posix_libc()\n            mprotect = libc.mprotect\n            mprotect.argtypes = (ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int)\n            mprotect.restype = ctypes.c_int\n\n            self.mmap = mmap.mmap(\n                -1,\n                size,\n                mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,\n                mmap.PROT_READ | mmap.PROT_WRITE,\n            )\n            self.addr = ctypes.addressof(ctypes.c_void_p.from_buffer(self.mmap))\n            self.mmap.write(code)\n\n            if mprotect(self.addr, size, mmap.PROT_READ | mmap.PROT_EXEC) != 0:\n                msg = \"could not execute mprotect for CPUID check\"\n                raise RuntimeError(msg)\n\n        func_type = CFUNCTYPE(None, POINTER(CPUID_struct), c_uint32, c_uint32)\n        self.func_ptr = func_type(self.addr)\n\n    def __call__(self, eax: int, ecx: int = 0) -> CPUID_struct:\n        struct = CPUID_struct()\n        self.func_ptr(struct, eax, ecx)\n        return struct\n\n    def __del__(self) -> None:\n        if _IS_WINDOWS:\n            self.win.VirtualFree.restype = c_long\n            self.win.VirtualFree.argtypes = [c_void_p, c_size_t, c_ulong]\n            self.win.VirtualFree(self.addr, 0, _MEM_RELEASE)\n\n\ndef _read_cpu_flags() -> dict[str, bool]:\n    if not _SUPPORTS_CPUID:","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_cpu_check.py#L181-L217","documentation":"On non-Windows platforms the CPUID check writes machine code into an mmap buffer and then calls `mprotect` to make it executable. A non-zero return from `mprotect` raises `RuntimeError` with this message. It means the kernel refused to make the probe page PROT_READ|PROT_EXEC.","triggerScenarios":"Constructing the CPUID check object on Linux/macOS where `mprotect(addr, size, PROT_READ|PROT_EXEC)` returns non-zero — commonly under SELinux/AppArmor, PaX/GRSEC W^X enforcement, or seccomp filters forbidding exec-memory.","commonSituations":"Hardened CI runners and containers with W^X policies; sandboxed environments (gVisor, Firecracker) with restrictive seccomp; distributions with SELinux enforcing.","solutions":["Relax the W^X/selinux/seccomp policy for the process or use an image without exec-memory restrictions.","Run in a standard environment (no grsecurity/PaX) or a supported container base image.","Use a polars build/variant that does not require the runtime CPUID probe."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    import polars as pl\nexcept RuntimeError as e:\n    if 'mprotect' in str(e):\n        raise RuntimeError('environment blocks executable memory (W^X policy); relax SELinux/seccomp or use a standard image') from e\n    raise","preventionTips":["Use container images without W^X enforcement (no grsecurity/PaX) for polars workloads.","Check SELinux/AppArmor/seccomp policies in CI before installing native-code packages.","Test `import polars` in the target environment early in setup."],"tags":["python","polars","runtimeerror","mprotect","security-policy"],"backgroundTag":"unsupported-platform","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}