{"modulePath":"github.com/byteforge-run/tester-utils","version":"v1.19.0","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package executable\n\n## Constants\n\n```go\nconst DefaultMemoryLimitInBytes int64 = 2 * 1024 * 1024 * 1024\n```\nDefaultMemoryLimitInBytes is the default memory limit (2GB)\n\n## Variables\n\n```go\nvar (\n\tErrSandboxMemoryExceeded = errors.New(\"sandbox: memory limit exceeded (RLIMIT_AS)\")\n\tErrSandboxCPUExceeded    = errors.New(\"sandbox: cpu time limit exceeded (RLIMIT_CPU)\")\n\tErrSandboxForkBlocked    = errors.New(\"sandbox: process count limit exceeded (RLIMIT_NPROC)\")\n\tErrSandboxSyscallBlocked = errors.New(\"sandbox: blocked syscall (seccomp)\")\n\tErrSandboxFileTooLarge   = errors.New(\"sandbox: file size limit exceeded (RLIMIT_FSIZE)\")\n)\n```\nSandbox error sentinels — distinguishable from business logic errors.\n\n```go\nvar ErrMemoryLimitExceeded = errors.New(\"process exceeded memory limit\")\n```\nErrMemoryLimitExceeded is returned when a process exceeds its memory limit\n\n## Types\n\n```go\ntype Executable struct {\n\t// Path is the path to the executable.\n\tPath string\n\n\t// TimeoutInMilliseconds is the maximum time the process can run.\n\tTimeoutInMilliseconds int\n\n\t// MemoryLimitInBytes sets the maximum memory the process can use (Linux only).\n\t// If exceeded, the process will be killed and an error will be returned.\n\t// Defaults to 2GB. Set to 0 to disable memory limiting.\n\tMemoryLimitInBytes int64\n\n\t// ShouldUsePty controls whether the executable's standard streams should be set to PTY instead of pipes.\n\tShouldUsePty bool\n\n\t// WorkingDir can be set before calling Start or Run to customize the working directory of the executable.\n\tWorkingDir string\n\n\t// Process is the os.Process object for the executable.\n\t// TODO: See if this actually needs to be exported?\n\tProcess *os.Process\n\t// contains filtered or unexported fields\n}\n```\nExecutable represents a program that can be executed\n\n```go\nfunc NewExecutable(path string) *Executable\n```\nNewExecutable returns an Executable\n\n```go\nfunc NewVerboseExecutable(path string, loggerFunc func(string)) *Executable\n```\nNewVerboseExecutable returns an Executable struct with a logger configured\n\n```go\nfunc (e *Executable) Clone() *Executable\n```\n\n```go\nfunc (e *Executable) HasExited() bool\n```\n\n```go\nfunc (e *Executable) Kill() error\n```\nKill terminates the program\n\n```go\nfunc (e *Executable) Run(args ...string) (ExecutableResult, error)\n```\nRun starts the specified command, waits for it to complete and returns the result.\n\n```go\nfunc (e *Executable) RunWithStdin(stdin []byte, args ...string) (ExecutableResult, error)\n```\nRunWithStdin starts the specified command, sends input, waits for it to complete and returns the result.\n\n```go\nfunc (e *Executable) SendLine(line string) error\n```\nSendLine writes a line to the process's stdin (for interactive mode). Automatically appends a newline character.\n\n```go\nfunc (e *Executable) Start(args ...string) error\n```\nStart starts the specified command but does not wait for it to complete.\n\n```go\nfunc (e *Executable) Wait() (ExecutableResult, error)\n```\nWait waits for the program to finish and returns the result.\n\n```go\nfunc (e *Executable) WriteStdin(data []byte) error\n```\nWriteStdin writes data to the process's stdin (for interactive mode). The process must be started with Start() first.\n\n```go\ntype ExecutableResult struct {\n\tStdout   []byte\n\tStderr   []byte\n\tExitCode int\n\tMeta     SandboxMeta // structured metadata; zero value when Sandbox == nil\n}\n```\nExecutableResult holds the result of an executable run\n\n```go\ntype SandboxConfig struct {\n\n\t// MaxMemoryBytes limits virtual address space via RLIMIT_AS (set to 3× this value).\n\t// 0 = unlimited. Recommended for tinyshop: 100MB.\n\tMaxMemoryBytes int64\n\n\t// MaxCPUTime limits cumulative CPU time via RLIMIT_CPU (not wall clock).\n\t// 0 = unlimited. Recommended for tinyshop: 3s.\n\tMaxCPUTime time.Duration\n\n\t// MaxProcesses limits child process count via RLIMIT_NPROC (includes threads).\n\t// 0 = unlimited. Recommended for tinyshop: 5 (allows Python internal threads).\n\tMaxProcesses int\n\n\t// MaxFileSize limits single file write size via RLIMIT_FSIZE.\n\t// 0 = unlimited. Recommended for tinyshop: 10MB.\n\tMaxFileSize int64\n\n\t// MaxOpenFiles limits simultaneously open file descriptors via RLIMIT_NOFILE.\n\t// 0 = unlimited. Recommended for tinyshop: 32.\n\tMaxOpenFiles int\n\n\t// EnvWhitelist restricts child process environment to only these variables.\n\t// nil = use v1 blacklist behavior (filter BYTEFORGE_SECRET* only).\n\t// Recommended for tinyshop: [\"LANG\", \"LC_ALL\", \"PYTHONIOENCODING\", \"PATH\"]\n\tEnvWhitelist []string\n\n\t// UseTempWorkDir creates a temporary directory, copies WorkingDir content into it,\n\t// and uses it as the child's cwd. Cleaned up after evaluation.\n\tUseTempWorkDir bool\n\n\t// SeccompProfile selects a preset syscall filter policy.\n\tSeccompProfile SeccompProfile\n\n\t// NoNewPrivs calls prctl(PR_SET_NO_NEW_PRIVS, 1) to prevent setuid privilege escalation.\n\t// Forced to true when SeccompProfile != SeccompOff.\n\tNoNewPrivs bool\n}\n```\nSandboxConfig configures a process-level sandbox for the child process. A nil value means no sandbox — v1 behavior is completely unchanged. Linux only; other platforms are no-op (logs a warning on startup).\n\n```go\nfunc DefaultSandboxConfig() *SandboxConfig\n```\nDefaultSandboxConfig returns the recommended defaults for tinyshop script-runner courses. All values can be individually overridden after construction.\n\n\tMaxMemoryBytes:  100MB (RLIMIT_AS safeguard + RSS polling)\n\tMaxCPUTime:      3s (RLIMIT_CPU kernel safeguard)\n\tMaxProcesses:    5 (allows Python internal threads, blocks fork bombs)\n\tMaxFileSize:     10MB\n\tMaxOpenFiles:    32\n\tEnvWhitelist:    [LANG, LC_ALL, PYTHONIOENCODING, PATH]\n\tUseTempWorkDir:  true\n\tSeccompProfile:  SeccompScriptRunner\n\tNoNewPrivs:      true\n\n```go\ntype SandboxMeta struct {\n\t// Trigger identifies what caused process termination.\n\tTrigger SandboxTrigger\n\n\t// CPUTime is the cumulative CPU time used.\n\tCPUTime time.Duration\n\n\t// WallTime is the wall-clock elapsed time.\n\tWallTime time.Duration\n\n\t// MaxRSSKB is the peak physical memory in KB (from /proc polling max).\n\tMaxRSSKB int64\n\n\t// Message is a human-friendly description for the student.\n\tMessage string\n}\n```\nSandboxMeta records structured metadata about a sandboxed execution, separated from the student's stdout/stderr. Zero value when Sandbox == nil (v1 behavior unchanged).\n\n```go\ntype SandboxTrigger int\n```\nSandboxTrigger identifies the source of process termination. Zero value = SandboxTriggerNone (student code exited normally).\n\n```go\nconst (\n\tSandboxTriggerNone           SandboxTrigger = iota // student code exited on its own\n\tSandboxTriggerMemoryExceeded                       // sandbox OOM kill\n\tSandboxTriggerCPUExceeded                          // RLIMIT_CPU hard limit\n\tSandboxTriggerTimeout                              // wall clock timeout\n\tSandboxTriggerSeccomp                              // blocked syscall (seccomp SIGSYS)\n\tSandboxTriggerForkBlocked                          // RLIMIT_NPROC (fork returned EAGAIN)\n\tSandboxTriggerFileTooLarge                         // RLIMIT_FSIZE (SIGXFSZ)\n\tSandboxTriggerOutputExceeded                       // output exceeded maxOutputBytes\n)\n```\n\n```go\nfunc (t SandboxTrigger) String() string\n```\n\n```go\ntype SeccompProfile int\n```\nSeccompProfile is a preset syscall filtering policy.\n\n```go\nconst (\n\t// SeccompOff installs no seccomp filter.\n\tSeccompOff SeccompProfile = iota\n\n\t// SeccompScriptRunner for pure-compute + stdio courses (tinyshop/pyforge):\n\t//   Blocked: socket / fork / execve / chmod / setuid / ptrace / mount\n\t//   Allowed: file IO / mmap / clock / Python internal syscalls\n\tSeccompScriptRunner\n\n\t// SeccompCLITester for courses needing FS + fork (TinyGit):\n\t//   Blocked: socket / setuid / ptrace / mount / reboot\n\t//   Allowed: fork / exec / fcntl / open / write / most non-network syscalls\n\tSeccompCLITester\n)\n```\n\n","path":"github.com/byteforge-run/tester-utils/executable","name":"executable","synopsis":"","isRedistributable":true}
