Skip to content

Add typed syscall/errno layer (Wave 1 unit 1) - #5

Merged
CierCier merged 5 commits into
mainfrom
typed-syscall-errno-layer
Jul 20, 2026
Merged

CierCier merged 5 commits into
mainfrom
typed-syscall-errno-layer

Conversation

@CierCier

@CierCier CierCier commented Jul 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Runtime-migration Wave 1, unit 1: typed syscall/errno layer over the generated sys_* wrappers.

  • std/sys/errno.ag (new): 42 errno constants (EPERM..ERANGE, EDEADLK..ELOOP, EOVERFLOW, ENOSYS, ETIMEDOUT), is_errno(i64)/errno_of(i64) classifiers for the raw -4095..-1 return encoding, and errno_name(i64) -> str (accepts positive errno or raw negative returns; unknown -> "EUNKNOWN", 0 -> "EOK").
  • std/sys/result.ag (new): SysResult { bool ok; i64 value; i64 errno; } tagged struct following the std/optional.ag pattern (no payload enums), with from_raw/success/failure constructors and is_ok/is_err/err_is/unwrap_or/err_name methods. Typed front-ends taking proper pointer/length types: try_read, try_write, try_open, try_openat, try_close, try_mmap, try_munmap, try_mremap, try_lseek, try_kill, try_getpid, exit_group, plus the PROT_*/MAP_*/O_*/SEEK_*/AT_FDCWD/STDIN_FD..STDERR_FD constants they need.
  • tests/errno_test.ag (new): 76 assertions — success paths (write to fd 1, getpid, kill(self, 0), anonymous mmap + mremap grow + munmap with content checks, open/read/lseek/close on /dev/null and /dev/zero incl. openat(AT_FDCWD, ...)) and failure paths (close(-1) -> EBADF, read/write/lseek on bad fd -> EBADF, missing path -> ENOENT, mmap without sharing flags -> EINVAL, unaligned munmap/mremap -> EINVAL, out-of-range pid -> ESRCH). Ends with a real exit_group(tests_failed) after fflush(stdout), so the success path of exit_group is exercised for real.

std/sys/linux.ag is untouched (generated file); all new hand-written code lives in the two new modules per the migration doc.

Design notes

  • Front-ends are prefixed try_ instead of bare POSIX names: Silver pub functions emit unmangled C-ABI symbols, and defining read/write/open/... would interpose libc symbols with an incompatible sret struct-return ABI while binaries still link libc (until Wave 2 unit 11).
  • try_mremap deliberately has no new_address parameter, so MREMAP_FIXED is unsupported (documented in the source); the kernel receives 0.

Testing

  • cargo test -p agc: 218 + 2 passed, 0 failed.
  • tests/errno_test.ag: 76/76 pass, exit 0.
  • Regression sweep: vec_test, map_test, str_key_map_test, memory_pentest all exit 0; syscall_test exits 42 by design (it calls sys_exit(42) — pre-existing behavior, unchanged).
  • Note: the code-review skill could not be invoked from this agent (disable-model-invocation); a manual self-review pass was done instead (removed a misleading libc-ism MAP_FAILED constant, documented the MREMAP_FIXED limitation).

🤖 Generated with Claude Code

Greptile Summary

This PR introduces std/sys/errno.ag and std/sys/result.ag as a typed syscall/errno layer over the generated sys_* i64 wrappers, following the existing std/optional.ag tagged-struct pattern. A 76-assertion test suite in tests/errno_test.ag exercises both success and failure paths end-to-end with real syscalls.

  • errno.ag: 42 errno constants, is_errno/errno_of raw-return classifiers, and errno_name with correct range-guarded normalization for negative inputs.
  • result.ag: SysResult tagged struct with from_raw/success/failure constructors and typed try_* front-ends; try_ prefix is deliberate to avoid interposing on libc symbols while the toolchain still links libc.
  • errno_test.ag: Covers the full API surface including mmap/mremap/munmap lifecycle with content verification, openat(AT_FDCWD, …), and a representative set of error paths (EBADF, ENOENT, EINVAL, ESRCH).

Confidence Score: 5/5

Safe to merge — the two new modules are purely additive, touch no existing code, and are thoroughly tested by a 76-assertion suite exercising real syscalls end-to-end.

The errno classification logic, SysResult decoding, and all typed try_* wrappers are correct. The test suite covers both happy and error paths including mmap lifecycle and failure-path errno assertions.

No files require special attention. The bootstrap binary and .agm artifacts are compiled outputs of the new source files.

Important Files Changed

Filename Overview
std/sys/errno.ag New module: 42 errno constants, is_errno/errno_of classifiers, and errno_name lookup. Logic is correct; the negative-normalization guard in errno_name now correctly routes out-of-range negatives to EUNKNOWN before negating.
std/sys/result.ag New module: SysResult tagged struct with from_raw/success/failure constructors and typed try_* front-ends. Minor type inconsistency: AT_FDCWD is i64 while the dirfd parameter of try_openat is i32, forcing callers to cast. All else correct.
tests/errno_test.ag New 76-assertion test suite covering success and failure paths for errno classification, SysResult decoding, and all try_* wrappers. Exercises real /dev/null, /dev/zero, mmap/mremap/munmap lifecycle, and deliberately-bad-fd failure paths.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Caller
    participant try_fn as try_* (result.ag)
    participant SysResult
    participant sys_fn as sys_* (linux.ag)
    participant Kernel

    Caller->>try_fn: "try_read(fd: i32, buf: u8*, count: i64)"
    try_fn->>sys_fn: sys_read((i64)fd, (i64)buf, count)
    sys_fn->>Kernel: syscall(SYS_read, ...)
    Kernel-->>sys_fn: raw: i64
    sys_fn-->>try_fn: raw: i64
    try_fn->>SysResult: from_raw(raw)
    alt raw in [-4095, -1]
        SysResult-->>try_fn: "SysResult { ok=false, value=0, errno=-raw }"
    else "raw >= 0"
        SysResult-->>try_fn: "SysResult { ok=true, value=raw, errno=0 }"
    end
    try_fn-->>Caller: SysResult
    Caller->>SysResult: is_ok() / unwrap_or() / err_name()
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Caller
    participant try_fn as try_* (result.ag)
    participant SysResult
    participant sys_fn as sys_* (linux.ag)
    participant Kernel

    Caller->>try_fn: "try_read(fd: i32, buf: u8*, count: i64)"
    try_fn->>sys_fn: sys_read((i64)fd, (i64)buf, count)
    sys_fn->>Kernel: syscall(SYS_read, ...)
    Kernel-->>sys_fn: raw: i64
    sys_fn-->>try_fn: raw: i64
    try_fn->>SysResult: from_raw(raw)
    alt raw in [-4095, -1]
        SysResult-->>try_fn: "SysResult { ok=false, value=0, errno=-raw }"
    else "raw >= 0"
        SysResult-->>try_fn: "SysResult { ok=true, value=raw, errno=0 }"
    end
    try_fn-->>Caller: SysResult
    Caller->>SysResult: is_ok() / unwrap_or() / err_name()
Loading

Reviews (3): Last reviewed commit: "Update bootstrap compiler and stdlib pac..." | Re-trigger Greptile

Context used:

  • Context used - You are an expert systems programmer, compiler eng... (source)

Raw sys_* wrappers return i64 with -4095..-1 encoding negated errno.
This adds the typed layer on top:

- std/sys/errno.ag: 42 errno constants (EPERM..ERANGE plus EDEADLK..ELOOP,
  EOVERFLOW, ENOSYS, ETIMEDOUT), is_errno/errno_of raw-return classifiers,
  and errno_name(i64) -> str (accepts positive errno or raw negative).
- std/sys/result.ag: SysResult tagged struct (ok/value/errno) following the
  Optional/Result pattern, plus try_read/try_write/try_open/try_openat/
  try_close/try_mmap/try_munmap/try_mremap/try_lseek/try_kill/try_getpid/
  exit_group front-ends taking proper pointer/length types, and the
  PROT_*/MAP_*/O_*/SEEK_*/AT_FDCWD flag constants they need. The try_
  prefix avoids interposing libc symbols (pub fns emit unmangled C-ABI
  names) with an incompatible sret struct-return ABI.
- tests/errno_test.ag: 76 assertions covering success paths (write fd 1,
  getpid, kill(self,0), mmap/mremap/munmap of pages, open/read/lseek/close
  on /dev/null and /dev/zero) and failure paths (EBADF, ENOENT, EINVAL,
  ESRCH), ending with a real exit_group call carrying the failure count.

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI review requested due to automatic review settings July 20, 2026 15:12
Comment thread std/sys/errno.ag Resolved
Comment thread tests/errno_test.ag Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a first “typed syscall/errno” layer in the Silver stdlib, wrapping the generated sys_* Linux syscall stubs with a small SysResult tagged-struct API and a dedicated errno constants/name module, plus an integration-style test suite that exercises both success and failure paths.

Changes:

  • Add std/sys/errno.ag with errno constants plus helpers for Linux’s -4095..-1 raw syscall error encoding and symbolic name formatting.
  • Add std/sys/result.ag defining SysResult and try_* typed syscall front-ends (read/write/open/openat/close/mmap/munmap/mremap/lseek/kill/getpid/exit_group) plus needed flag constants.
  • Add tests/errno_test.ag covering decoding, wrappers, and representative syscall success/failure behaviors.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/errno_test.ag New test program validating errno decoding and SysResult + try_* syscall wrapper behavior.
std/sys/result.ag New typed syscall result API (SysResult) and try_* wrappers with syscall-related constants.
std/sys/errno.ag New errno constants plus helpers (is_errno, errno_of, errno_name) for raw syscall return decoding and display.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread std/sys/errno.ag
Comment on lines +75 to +79
pub str errno_name(i64 err) {
i64 e = err;
if (e < 0) {
e = -e;
}
Comment thread std/sys/result.ag
Comment on lines +204 to +206
pub void exit_group(i64 code) {
sys_exit_group(code);
}
Comment thread std/sys/result.ag
Comment on lines +15 to +17
import std.sys.errno;
import std.sys.linux;

@greptile-apps

greptile-apps Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@CierCier
CierCier merged commit d6ab644 into main Jul 20, 2026
1 check passed
@CierCier
CierCier deleted the typed-syscall-errno-layer branch July 21, 2026 10:16
CierCier added a commit that referenced this pull request Aug 1, 2026
Add typed syscall/errno layer (Wave 1 unit 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants