Implementation:2akouwu Reverify run sandboxed
| Knowledge Sources | |
|---|---|
| Domains | Sandboxing, Native_Execution |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for running untrusted native code under platform confinement (POSIX rlimits / Windows Job Object), never raising for the child, provided by the Reverify library.
Description
The run_sandboxed function is the single entry point for all native execution in Reverify. It runs argv under the strongest confinement each platform offers: wall-clock timeout with hard kill of the process group, output cap (stdout/stderr truncated at a byte limit), isolated working directory, scrubbed environment (minimal PATH, no inherited secrets), stdin from /dev/null. On POSIX it sets rlimits (CPU, address space, file size, NPROC) via a pre-exec hook and gives the child its own session. On Windows it uses a Job Object (best-effort) to cap memory and kill the whole job on close. Fails closed: a timeout, flood, or spawn failure is reported, not raised.
Usage
Import this function when you need to:
- Run compiled candidate code for
functions_equiv_verifyorbehavioral_equiv. - Execute any untrusted native program with resource limits.
Code Reference
Source Location
- Repository: Reverify
- File: reverify/sandbox.py
- Lines:
SandboxLimitsat L49-58;SandboxResultdataclass;run_sandboxedat L252-322
Signature
@dataclass
class SandboxLimits:
wall_seconds: float = 10.0
cpu_seconds: Optional[int] = 5
memory_bytes: Optional[int] = 512 * 1024 * 1024
file_size_bytes: Optional[int] = 16 * 1024 * 1024
max_processes: Optional[int] = 64
output_bytes: int = 4 * 1024 * 1024
@dataclass
class SandboxResult:
returncode: Optional[int]
stdout: bytes
stderr: bytes
timed_out: bool
output_truncated: bool
killed: bool
reason: str
@property
def ok(self) -> bool:
"""returncode 0 and not timed_out/killed."""
def run_sandboxed(
argv: Sequence[str],
*,
cwd: Optional[str] = None,
input_bytes: Optional[bytes] = None,
limits: Optional[SandboxLimits] = None,
extra_env: Optional[Dict[str, str]] = None,
) -> SandboxResult:
"""Run argv under platform confinement; never raises for the child."""
Import
from reverify.sandbox import run_sandboxed, SandboxLimits, SandboxResult
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| argv | Sequence[str] | Yes | Command + args |
| cwd | str | No | Isolated working directory |
| input_bytes | bytes | No | Stdin bytes (default: /dev/null) |
| limits | SandboxLimits | No | Resource ceilings (default: SandboxLimits()) |
| extra_env | Dict[str, str] | No | Minimal env additions |
Outputs
| Name | Type | Description |
|---|---|---|
| return | SandboxResult | returncode, stdout, stderr, timed_out, output_truncated, killed, reason |
Usage Examples
Run a Program
from reverify.sandbox import run_sandboxed, SandboxLimits
result = run_sandboxed(["./candidate", "3", "5"],
limits=SandboxLimits(wall_seconds=5, memory_bytes=256*1024*1024))
if result.ok:
print(result.stdout.decode().strip())
else:
print(f"Failed: {result.reason}")