cosmo-compat 10.0.0

Libc shims where Cosmopolitan and Rust's std disagree — link it into a cosmo target and the fixes apply themselves
Documentation
//! Shims for places where cosmopolitan's libc and Rust's std disagree.
//!
//! Link this into any cosmo target and the fixes apply themselves -- each is a
//! `#[no_mangle]` definition that the linker prefers over the one in
//! libcosmo.a, because an archive member is only pulled in to satisfy a symbol
//! that is still undefined.

// no_std so one shim serves both worlds: cosmo-ld wraps 123 libc symbols on
// every cosmo link, so even a no_std binary has to supply the translators, and
// nothing here needs more than core.
#![cfg_attr(not(test), no_std)]
use core::ffi::{c_char, c_int, c_void};

// Stage-2 normalization (docs/DESIGN.md): translate OS constants at the libc
// boundary so a binary compiled with Linux numbering runs on the host cosmo
// landed it on. `gen` is generated by tools/gen-shim.py; `shim` holds the
// hand-written __wrap_* functions cosmo-ld routes calls into.
mod gen;
mod net;
mod socketpair;
pub mod xlate;
pub mod shim;

/// Keep the two plain overrides alive through `lto = "fat"`.
///
/// Nothing in the Rust call graph references them -- std reaches both through
/// an `extern "C"` declaration LTO cannot see -- so fat LTO internalizes them
/// away and the libc bugs they exist to fix come back, silently and only in
/// release builds. (The `__wrap_*` functions do not need this: `--wrap` makes
/// the linker demand them, so losing one is a link error rather than a quiet
/// regression.) `#[used]` emits `llvm.used`, which LTO honours.
#[used]
static KEEP_SHIMS: (
   unsafe extern "C" fn(c_int, *mut c_char, usize) -> c_int,
   unsafe extern "C" fn(c_int, c_int, *mut c_int, c_int) -> c_int,
) = (__xpg_strerror_r, waitid);

extern "C" {
   fn strerror(errnum: c_int) -> *const c_char;
   fn strlen(s: *const c_char) -> usize;
   // Declared with the signature the runtime actually exports; anything else
   // trips suspicious_runtime_symbol_definitions, and a mismatched redeclaration
   // of a symbol the compiler may lower calls to is worth not guessing at.
   fn memcpy(d: *mut c_void, s: *const c_void, n: usize) -> *mut c_void;
}

/// POSIX XSI `strerror_r`, which is what Rust's std actually calls.
///
/// std links this symbol and requires the XSI contract: return an `int`, zero
/// on success. Cosmopolitan declares it `char *__xpg_strerror_r(int, char *,
/// size_t)` -- the GNU contract -- so std reinterprets the returned pointer as
/// an int, finds the sign bit set often enough to matter, and panics with
/// "strerror_r failure". The effect is that formatting *any* io::Error blows
/// up, which takes out ordinary error paths well before anything exotic.
///
/// Copying out of cosmo's `strerror` and returning 0 restores the contract.
#[no_mangle]
pub unsafe extern "C" fn __xpg_strerror_r(
   errnum: c_int,
   buf: *mut c_char,
   buflen: usize,
) -> c_int {
   if buf.is_null() || buflen == 0 {
      return 22; // EINVAL, per XSI
   }
   let msg = strerror(errnum);
   if msg.is_null() {
      *buf = 0;
      return 0;
   }
   let n = core::cmp::min(strlen(msg), buflen - 1);
   memcpy(buf.cast(), msg.cast(), n);
   *buf.add(n) = 0;
   0
}

extern "C" {
   fn waitpid(pid: c_int, status: *mut c_int, options: c_int) -> c_int;
}

/// `waitid`, which cosmopolitan does not provide but std's `Process::wait`
/// references unconditionally (its pidfd path, `P_PIDFD`, is compiled in for
/// every linux target). Linking is the requirement; the semantics only matter
/// if the function is ever reached, and on cosmo std never creates a pidfd, so
/// it is not. Implemented over `waitpid` for the `P_PID`/`P_ALL` shapes anyway:
/// `siginfo_t.si_status` receives the raw wait status and `si_pid` the pid, at
/// the Linux offsets std reads (`si_pid` at byte 16, `si_status` at 24).
#[no_mangle]
pub unsafe extern "C" fn waitid(idtype: c_int, id: c_int, infop: *mut c_int, options: c_int) -> c_int {
   const P_ALL: c_int = 0;
   const P_PID: c_int = 1;
   const WNOWAIT: c_int = 0x0100_0000;
   let pid = match idtype { P_ALL => -1, P_PID => id, _ => return -1 };
   let mut status: c_int = 0;
   let r = waitpid(pid, &mut status, options & !WNOWAIT & 0xff);
   if r < 0 { return -1; }
   if !infop.is_null() {
      *infop.add(4) = r;        // si_pid
      *infop.add(6) = status;   // si_status
   }
   0
}

/// Built standalone by cargo-cosmo (tools/cargo-cosmo, `ensure_inject`): a
/// staticlib needs a panic handler of its own. The shim never panics on
/// purpose; if it does, abort rather than unwind through C.
#[cfg(all(cosmo_compat_inject, not(test)))]
#[panic_handler]
fn inject_panic(_: &core::panic::PanicInfo) -> ! {
   extern "C" { fn abort() -> !; }
   unsafe { abort() }
}