$ show redox-os/relibc
relibc is a from-scratch implementation of the C standard library and POSIX API written in Rust, serving as the primary libc for Redox OS with a secondary Linux compatibility target. It is not an end-user application but a system library and process runtime: the build produces libc.a and libc.so, crt0/crti/crtn startup objects, and a complete dynamic linker (ld_so) handling ELF loading, symbol resolution, TLS/TCB setup, and dlopen. All C-facing functions are exported as #[unsafe(no_mangle)] extern "C" entry points across per-header modules such as stdio, unistd, pthread, time, signal, wchar, netdb, and crypt. Architecturally, the codebase is organized per C header under src/header/, with a platform abstraction layer (Pal trait in src/platform/pal/) whose Linux and Redox backends dispatch POSIX calls through raw syscalls or Redox scheme-based redox-rt bridges. Process startup flows through src/start.rs (relibc_start_v1), which initializes TLS/TCB state, copies argv/envp, and runs constructor arrays. The project also includes futex-based synchronization primitives, a variadic printf/scanf implementation, wide-character support, a DNS stub resolver, timezone parsing, password-hashing algorithms (Argon2, scrypt, bcrypt, PBKDF2), and a qsort backend. C headers are generated from Rust definitions via cbindgen, augmented with hand-written include/ macros and setjmp/longjmp assembly ported across 15+ architectures. The project demonstrates strong systems engineering: an extensive C conformance test suite (hundreds of golden-output tests), POSIX specification citations throughout, ABI layout assertions against the libc crate, and clean trait-based platform abstraction. However, coverage is incomplete: many headers are stubbed or return ENOSYS (async I/O, named semaphores, parts of sched, net_if), several functions remain todo!()/unimplemented!(), and the test Makefile lists known-failing tests. Linux support is a compatibility surface rather than a complete drop-in libc, and much of the code is Redox-specific (scheme paths, redox-rt, Redox ABI versioned entry points). The repository is actively maintained (Rust edition 2024, recent commits) but carries medium technical drift risk: it depends on unstable nightly Rust features, git-patched dependencies, and a custom toolchain; lazy PLT binding is only fully implemented on x86_64, and unsafe-heavy global state (static mut, SyncUnsafeCell) plus vendored musl/Rust-std code create ongoing maintenance and soundness burden. Overall this is high-craftsmanship systems code with several genuinely extractable components, tempered by its incomplete POSIX surface and tight OS coupling.
The LLM identified 6 files worth highlighting for reuse:
Complete Argon2 password-hashing implementation exported as a C ABI function for libc's crypt() family. The algorithm is self-contained, depending only on core Rust primitives, so it can be extracted into any Rust authentication or security project with minimal modification. Demonstrates careful memory-hard hashing design and non-obvious crypto implementation skill.
Self-contained qsort backend implementing introsort with heapsort fallback, insertion sort, and Dutch-national-flag three-way partitioning for duplicate-heavy datasets. Generic comparator-based sorting with C qsort-compatible semantics; demonstrates genuine algorithmic craftsmanship and is directly reusable outside relibc.
Parser for POSIX TZ environment strings (e.g., 'EST5EDT,M3.2.0,M11.1.0') producing DST transition rules. Standalone, well-documented domain logic that any time library, scheduler, or calendar application could reuse; this is rare, non-obvious parsing logic not available as a commodity library.
DNS message encoder/decoder handling query construction and answer record parsing. Originated from old Rust std but is actively maintained here and includes a documented CVE-2024-21342 hardening fix. Lifts cleanly into any DNS stub resolver or network diagnostic tool; demonstrates careful binary protocol handling.
Macro-driven generation of wire serialization/deserialization for kernel ioctl structures, including counted arrays and arch-specific pointer-width handling used for DRM and Redox ioctls. The macro pattern is generic across kernel-driver FFI projects and can be reused for any architecture-sensitive ioctl marshalling layer with minimal adaptation.
Kind-generic NUL-terminated string wrappers (NulStr<Thin/Wide>, CStr, WStr, OwnedThinCStr) providing layout-compatible pointer casting between char* and wchar_t* while reducing unsafe pointer arithmetic in C FFI code. A reusable safety layer for any project that must interoperate with C string APIs.