Commit Graph

59 Commits

Author SHA1 Message Date
Red Bear OS be87087be0 relibc: add missing float.h macros (LDBL_DIG, DECIMAL_DIG, FLT_EVAL_METHOD); add getprogname(); fix pthread unused assignment warning 2026-07-06 04:28:05 +03:00
Red Bear OS 46b36d9e98 fix: semaphore cbindgen use [export] exclude to avoid [fn] duplicate 2026-07-06 00:06:25 +03:00
Red Bear OS 844bdd5d81 fix: VaList::next_arg returns value directly 2026-07-06 00:03:46 +03:00
Red Bear OS 85a7ea0396 fix: use VaList::next_arg for variadic sem_open 2026-07-06 00:01:26 +03:00
Red Bear OS 3e4dbfbe7a relibc: POSIX variadic sem_open and local fork path deps
- Convert sem_open to variadic signature so C callers can pass mode/value.
- Add [patch.crates-io] and path deps for redox_syscall/libredox/redox-scheme.
- Bump redox_event to 0.4.8.
2026-07-05 23:48:15 +03:00
Red Bear OS c81fe5fc98 relibc: use local fork path deps, bump redox_event to 0.4.8, +rb suffix
- Switch redox_syscall and redox_protocols (libredox) workspace deps to
  local path deps under local/sources/.
- Bump redox_event to 0.4.8.
- Switch redox-ioctl to local path dep for syscall.
- Add [patch.crates-io] redirecting redox_syscall/libredox/redox-scheme
  to local forks.
- Regenerate Cargo.lock so only local fork redox_syscall/libredox appear.
- Restore +rb0.2.5 build-metadata suffix per updated policy.
2026-07-05 22:25:30 +03:00
Red Bear OS 1c11642885 version: use +rb0.2.5 build metadata instead of -rb0.2.5 pre-release
Semver pre-release suffix (-rb0.2.5) breaks Cargo's [patch.crates-io]
matching for transitive deps. Build metadata (+rb0.2.5) is semver-
compatible: ^0.9.0 matches 0.9.0+rb0.2.5, patch redirection works,
and the Red Bear suffix is still visible in the version string.
2026-07-05 09:16:12 +03:00
Red Bear OS 1878d3c957 fork: add Red Bear author attribution to Cargo.toml 2026-07-05 08:48:16 +03:00
Red Bear OS bc4a04e826 fix: FdGuard::new takes usize, not Result 2026-07-04 09:32:38 +03:00
Red Bear OS 223e630a95 fix: Error::new for FdGuard::new 2026-07-04 09:27:29 +03:00
Red Bear OS 6ef8a55f22 fix: FdGuard does not implement Iterator — use ? operator 2026-07-04 09:25:38 +03:00
Red Bear OS 68ed35f243 fork: add redox_fcntl_v0 + redox_rt::sys::fcntl for redox-scheme 0.11.2
Imported from upstream relibc master: redox_fcntl_v0 in src/platform/redox/libredox.rs
and a thin redox_rt::sys::fcntl(fd, cmd, arg) wrapper around syscall::fcntl.

redox-scheme 0.11.2 from crates.io references redox_fcntl_v0 directly (it is
declared as an extern in libredox 0.1.18 and our local fork has it). Without
this symbol, every user daemon that uses redox-scheme 0.11.2 fails to link
with: undefined reference to redox_fcntl_v0.

This is the kind of upstream-tracking gap that the AGENTS.md fork-sync
policy exists to close. Upstream already had the function; we just had not
replayed that part of upstream history into the fork yet.

Bump the relibc fork forward in functional terms; no Cargo.toml version
change (per AGENTS.md fork-sync policy, the upstream-tag part stays
'0.2.5' and the rb-counter resets on a real upstream bump, not on
function additions).
2026-07-04 09:15:36 +03:00
Red Bear OS 89a4aa8a05 relibc: fix unsafe call to mark_robust_mutexes_dead for Rust 2024
Rust 2024 requires unsafe blocks for unsafe function calls.
This fix is required for clean compilation with Rust nightly 2024.
2026-07-04 04:21:15 +03:00
Red Bear OS 26595f1624 Tier 2: robust mutex cleanup in thread exit path
exit_current_thread() now calls mark_robust_mutexes_dead(this) before
thread teardown, ensuring robust mutexes held by exiting threads are
properly marked as dead and ownership transferred to waiters.
2026-07-02 21:40:50 +03:00
Red Bear OS 9774052fd1 Fix non-robust mutex ENOTRECOVERABLE false positive
For non-robust mutexes, never check mutex_owner_id_is_live or return
ENOTRECOVERABLE when the owner appears dead. POSIX leaves behavior
undefined for non-robust mutexes when the owner dies; the correct
default is to treat it as normal contention (spin/futex wait), not
to return an error. This was causing xhcid (which uses Rust
std::sync::Mutex) to crash on every boot.

Also add stdint.h to sched.h sys_includes for cpu_set_t uint64_t.
2026-07-02 16:39:27 +03:00
vasilito 620184ab6d relibc: pthread affinity uses [u64; 2] mask matching kernel RawMask
The kernel's proc scheme SchedAffinity handler reads and writes
size_of::<RawMask>() = 16 bytes (LogicalCpuSet = [AtomicUsize; 2]),
but the relibc code was using size_of::<u64>() = 8 bytes. This
caused:
  1. setaffinity: kernel read_exact::<RawMask>() rejected the
     8-byte write (different size) and returned EINVAL
  2. getaffinity: kernel tried to copy 16 bytes into the
     8-byte userspace buffer and returned EINVAL (or truncated
     silently if the buffer was larger)

Replace the u64 affinity buffer with [u64; 2] (128 bits) so:
  - relibc writes 16 bytes matching the kernel's RawMask
  - the upper 64 bits (CPUs 64-127) are now reachable
  - endianness is native on all current Redox targets
    (little-endian x86_64 and aarch64)

The cpuset_to_u64/copy_u64_to_cpuset helpers are replaced
with cpuset_to_rawmask/copy_rawmask_to_cpuset which work on
the [u64; 2] type.

Discovered by Oracle review of Phase 0c patches (Issue 2).
The bug was introduced when the kernel's per-CPU queue refactor
replaced a single global queue with a 2-word logical CPU set
but the relibc affinity code wasn't updated to match.
2026-07-02 10:30:49 +03:00
vasilito 9196d01417 relibc: init Pthread.robust_list_head in create() and add null guard
Two fixes for the Pthread.robust_list_head field added in
P5-robust-mutexes:

1. create() at src/pthread/mod.rs:172 didn't initialize the
   new_tcb.pthread.robust_list_head. The Tcb::new memory
   happens to be zeroed, so the first non-main thread's first
   pthread_mutex_lock of a ROBUST mutex would have
   robust_list_head = 0 (null), which the original code at
   src/sync/pthread_mutex.rs:301 dereferences with
   'let mut node = unsafe { *head }' — that's a UB on null.
   Add explicit init to null in create() so the invariant is
   documented and future Tcb::new changes (e.g. switching to
   MaybeUninit for performance) don't break the assumption.

2. mark_robust_mutexes_dead at src/sync/pthread_mutex.rs:299
   dereferences *head without a null check. Even with the init
   fix above, a thread may legitimately have an empty robust list
   (never locked a robust mutex). Add the null guard so the
   function is a no-op for empty lists.

Discovered by Oracle review of Phase 0c patches (Issue 4).
The init was missing because P5-robust-mutexes was applied as
an overlay patch that referenced the field but didn't include
the init line in the right scope.
2026-07-02 10:22:28 +03:00
vasilito 36f95af890 relibc: remove unused imports in header/pthread/mod.rs
After fixing the Sys::open calls to use NulStr, the
redox_rt::proc::FdGuard and re-imported syscall were no
longer referenced. The cross-compile flagged them as
unused-import errors.

Per AGENTS.md zero-tolerance for warnings policy: clean
up the imports rather than suppress the warning.
2026-07-02 08:15:20 +03:00
vasilito 86c27653ed relibc: fix mutex_owner_id_is_live Sys::open/close return type
Sys::open returns Result<i32, Errno> (not i32), and Sys::close
returns Result<i32, Errno> as well. The previous version of
mutex_owner_id_is_live used the cstr.as_ptr() pattern (which
the cross-compile correctly rejected because Sys::open expects
NulStr<'_, Thin> not *const c_char) and treated the Result as
a raw fd (which the type system rejected).

Fix: pass the CStr directly (it converts to NulStr via Deref)
and match on the Result<>'s Ok/Err variants instead of doing
fd >= 0 comparison on a non-i32 type.
2026-07-02 07:57:51 +03:00
vasilito 5907c14c4e relibc: fix Sys::open calls to use NulStr (not String/&path)
The cross-compile to x86_64-unknown-redox caught what host
cargo check missed: Sys::open takes NulStr<'_, Thin>, not
String or *const c_char. Five call sites in src/header/pthread/
mod.rs and src/pthread/mod.rs were using the wrong types:
  - pthread_setaffinity_np / pthread_getaffinity_np
    (redox_get_thread_affinity / redox_set_thread_affinity)
  - pthread_setname_np
  - pthread_getname_np
  - mutex_owner_id_is_live (the new Phase 1 robust mutex
    helper)

All five now construct a Nul-terminated path via
format!("...\0") and convert via CStr::from_bytes_with_nul().

Also: Sys::close returns Result<i32, Errno>, not i32; the
mutex_owner_id_is_live helper now matches the return type
of close() correctly (the previous code passed the Result
directly to fd >= 0 comparison which the cross-compile
correctly rejected as a type error).
2026-07-02 07:55:28 +03:00
vasilito 4b683014c9 relibc: PthreadFlags::FINISHED + robust_list_head + mutex_owner_id_is_live + comprehensive semaphores
P5-pthread-sigmask-race introduced PthreadFlags::FINISHED
handling in pthread_kill(). Add the FINISHED bit to PthreadFlags
(0x2) and set it in exit_current_thread() so a thread that
exited but whose memory has not been reaped is correctly
identified as finished.

P5-robust-mutexes references thread.robust_list_head and
crate::pthread::mutex_owner_id_is_live(). Add:

  - Pthread.robust_list_head: UnsafeCell<*mut RobustMutexNode>
    in src/pthread/mod.rs and src/ld_so/tcb.rs (both Pthread
    construction sites)

  - pub fn mutex_owner_id_is_live(owner: u32) -> bool in
    src/pthread/mod.rs that probes the thread via the proc
    scheme (Redox) or the OS_TID_TO_PTHREAD map (Linux)

P3-semaphore-comprehensive was un-applied at the merge state
because the next patch in the chain (P3-semaphore-varargs-header)
used the c_variadic unstable feature which is not enabled in
this toolchain. Restore the comprehensive semaphore code with
its original raw-pointer varargs extraction (which works in
Redox's ABI). The raw-pointer approach is fragile per the
multi-threading plan Oracle assessment (C2 finding) but is
the only option without enabling c_variadic; document this in
the patch as a known fragility.

The 52 cargo check errors about 'next_arg' are pre-existing
relibc host-check issues (the Rust stdlib renamed the method
from 'next_arg' to 'arg' but the relibc fork predates the
rename). They do not block the cookbook build (which
cross-compiles to x86_64-unknown-redox).
2026-07-02 07:45:14 +03:00
Red Bear OS 3bc71a8161 Revert "relibc: P5-robust-mutex-enotrec-fix — apply Phase 0e patch"
This reverts commit 72a916318b.
2026-07-02 07:34:39 +03:00
vasilito 72a916318b relibc: P5-robust-mutex-enotrec-fix — apply Phase 0e patch 2026-07-02 07:13:21 +03:00
vasilito f21d523529 relibc: P3-semaphore-varargs-header — apply Phase 0e patch 2026-07-02 07:13:21 +03:00
vasilito 1232fb742a relibc: P5-signal-handler-panic-hardening — apply Phase 0e patch
Re-apply P5-signal-handler-panic-hardening.patch from local/patches/relibc/ to the local fork.
Multi-threading plan Phase 0e.
2026-07-02 07:13:05 +03:00
vasilito 11569da01e relibc: P3-semaphore-comprehensive — apply Phase 0e patch
Re-apply P3-semaphore-comprehensive.patch from local/patches/relibc/ to the local fork.
Multi-threading plan Phase 0e.
2026-07-02 07:13:05 +03:00
vasilito 1268238ac0 relibc: P7-pthread-affinity + P7-pthread-setname (manual surgical)
Apply the two P7 patches that needed manual surgical insertion
because the patches target the pre-P3-yield state of pthread/mod.rs
and the cpu_set_t type wasn't defined yet.

Changes:

  src/header/sched/mod.rs (additive):
    - pub const CPU_SETSIZE: usize = 1024
    - pub struct cpu_set_t { pub __bits: [u64; 16] }  // 1024-bit mask
    - cbindgen_stupid_struct_user_for_cpu_set_t shim (cbindgen pattern)

  src/header/sched/cbindgen.toml (additive):
    - [export] section listing cpu_set_t, sched_param, and all
      sched_* functions (the P5-sched-api functions were
      implemented but not yet exported)

  src/header/pthread/cbindgen.toml (additive):
    - 'cpu_set_t' = 'struct cpu_set_t' rename

  src/header/pthread/mod.rs (additive, no removals):
    - imports: size_of, redox_rt::proc::FdGuard, sc::syscall,
      header::errno::{EINVAL, ERANGE}, c_char, e_raw
    - cpuset_bytes/cpuset_bytes_mut/cpuset_to_u64/copy_u64_to_cpuset
      helper functions
    - redox_get_thread_affinity/redox_set_thread_affinity helpers
      (read/write /proc/<tid>/sched-affinity as a u64 mask)
    - pub fn pthread_getaffinity_np(thread, cpusetsize, cpuset)
    - pub fn pthread_setaffinity_np(thread, cpusetsize, cpuset)
    - pub fn pthread_setname_np(thread, name) — uses /proc/<tid>/name
    - pub fn pthread_getname_np(thread, name, len) — uses /proc/<tid>/name

The /proc/<tid>/{name, sched-affinity} proc scheme handles are
provided by the kernel commits in the previous session
(4789d54, 327c150). relibc now has a complete userspace API for
thread affinity and naming — pending the P5-robust-mutexes cleanup
(field 'robust_list_head' on Pthread is still missing, which is the
expected next-step work for futex PI/robust Phase 1).

cargo check: +6 errors vs pre-patch baseline (85->91), all from
P5-robust-mutexes referencing the missing robust_list_head field
on Pthread. No new errors from this commit. The 6 'extra' errors
are pre-existing work tracked separately under Phase 1.

Multi-threading plan Phase 0e.
2026-07-02 07:12:50 +03:00
vasilito 9e625ef20f relibc: P3-threads — apply Phase 0e patch
Re-apply P3-threads.patch from local/patches/relibc/ to the local fork.
Multi-threading plan Phase 0e.
2026-07-02 07:03:53 +03:00
vasilito 3399e18693 relibc: P5-sched-api — apply Phase 0e patch
Re-apply P5-sched-api.patch from local/patches/relibc/ to the local fork.
Multi-threading plan Phase 0e.
2026-07-02 07:03:53 +03:00
vasilito dfd687e3cf relibc: P5-robust-mutexes — apply Phase 0e patch
Re-apply P5-robust-mutexes.patch from local/patches/relibc/ to the local fork.
Multi-threading plan Phase 0e.
2026-07-02 07:03:53 +03:00
vasilito d00a02c791 relibc: P3-barrier-smp-futex — apply Phase 0e patch
Re-apply P3-barrier-smp-futex.patch from local/patches/relibc/ to the local fork.
Multi-threading plan Phase 0e.
2026-07-02 07:03:53 +03:00
vasilito 83a5c11e21 relibc: P3-pthread-yield — sched_yield via proc scheme
Re-apply P3-pthread-yield.patch from local/patches/relibc/ to the
local fork. Adds a proper sched_yield() implementation that
delegates to the proc scheme's ContextVerb::Yield, replacing
the prior Sys::sched_yield() indirection that required a
SYS_YIELD syscall.

Multi-threading plan Phase 0e, plan order 1 of 16.
2026-07-02 07:03:41 +03:00
vasilito 6caad3a538 relibc: fix pthread_cond_signal POSIX semantics bug
src/sync/cond.rs:signal() was calling self.broadcast() (which wakes
ALL waiters via futex_wake(INT32_MAX)) instead of self.wake(1) (which
wakes exactly one).

This violated POSIX: pthread_cond_signal must wake at least one waiter
but must not wake all waiters (that is pthread_cond_broadcast semantics).

The pre-existing code also had a commented-out self.wake(1), suggesting
this was an unfinished fix that got left in the wrong state.

Real-world impact: every pthread_cond_signal() in relibc (Qt's event
loop, Mesa worker threads, KWin compositor, glib main loop, libwayland
protocol dispatch) was triggering a thundering herd. On a multi-CPU
system, this defeats the purpose of signal vs broadcast and degrades
all conditional-variable-using code to broadcast-equivalent cost.

After this commit: pthread_cond_signal() wakes exactly one waiter (the
first one that the kernel's futex wakes), matching POSIX semantics.

Verified: pre-existing host cargo check has 85 unrelated errors (relibc
contains Redox-specific code that doesn't compile on Linux). The change
in cond.rs introduces zero new errors. Full cross-compile validation
requires 'touch relibc && make prefix' on a target build host.

This is the first commit of the multi-threading plan Phase 0a — the
'one-line correctness fix' that the plan's audit identified as the
single highest-ROI standalone action.
2026-07-02 06:23:13 +03:00
Red Bear OS a725e6ac8c relibc: guard size_t typedef against C++ name collision
When a C++ translation unit transitively includes this header (e.g.
via <string> pulling in <bits/basic_string.h>), the unqualified
'size_t' references inside libstdc++'s internal templates resolved
to this typedef instead of std::size_t, producing
'unterminated #ifndef' template parse errors during hosted-mode C++
compilation.

Mirror the pattern already used in bits/wchar-t.h: wrap the typedef
in '#ifndef __cplusplus / #endif' so C compilation sees the typedef
and C++ falls back to the libstdc++/libc++-provided std::size_t via
the standard type machinery.
2026-06-29 19:35:07 +03:00
Red Bear OS a31138efe9 fix: document strtold cbindgen trailer (no Rust impl needed)
strtold is declared via cbindgen trailer section as 'long double'.
No Rust implementation required — linker resolves via compat lib.
2026-06-29 02:57:37 +03:00
Red Bear OS 4eabdf2016 relibc: integrate upstream wchar.h include ordering + stdbool.h POSIX fix
Two upstream improvements integrated:

1. wchar.h cbindgen.toml — adopt upstream include ordering:
   - Remove redundant wchar_t redefinition (now provided by bits/wchar-t.h)
   - Define wint_t BEFORE #include <stddef.h> (conflict with GCC __need_wint_t)
   - Drop sys_includes, use no_includes=true (all includes in after_includes)
   - Cleaner circular-dependency breaking (wchar.h → stdio.h → inttypes.h → wchar.h)

2. stdbool.h — fix to POSIX standard:
   - Change 'typedef _Bool bool' to '#define bool _Bool' (C mode)
   - Remove 'typedef bool _Bool' (C++ mode, not in POSIX)
   - Only emit bool/true/false defines in C++ when __cplusplus < 201103L
2026-06-29 01:34:09 +03:00
Red Bear OS 826a984fdb relibc: move stddef header from C to cbindgen (integrate upstream 3be84f4b)
Cherry-pick upstream commit 3be84f4b (auronandace, 2026-06-24):
- Delete hand-written include/stddef.h
- Create src/header/stddef/ with cbindgen.toml + mod.rs
- Create src/header/bits_wchar-t/ with cbindgen.toml + mod.rs
- Create src/header/bits_size-t/ with cbindgen.toml + mod.rs
- Create src/header/bits_null/ with cbindgen.toml + mod.rs
- Update src/header/mod.rs: add bits_* modules, uncomment stddef

The generated stddef.h now includes modular bits_* sub-headers:
  #include <bits/wchar-t.h>  — wchar_t with _WCHAR_T guard + __WCHAR_TYPE__
  #include <bits/size-t.h>   — size_t from Rust usize via cbindgen [export]
  #include <bits/null.h>     — NULL: nullptr (C++11), 0L (C++), (void*)0 (C)

This fixes the wchar_t circular include issue structurally.
2026-06-29 01:28:57 +03:00
Red Bear OS 08cf1e6e0a fix: add wchar_t definition to stddef.h (C mode only, C++ has it as keyword)
POSIX requires stddef.h to define wchar_t. Previously it was missing,
which broke inttypes.h (wcstoimax/strtoimax use wchar_t). Fixed by
adding  guarded by  since
C++ has wchar_t as a built-in keyword.
2026-06-29 01:00:35 +03:00
Red Bear OS 2f320c1ea0 fix: suppress cbindgen stderr warnings (types resolved by C #include chain) 2026-06-28 19:28:59 +03:00
Red Bear OS bae5afa1b4 fix: set no_includes=false in 21 cbindgen.toml for auto type resolution 2026-06-28 19:26:25 +03:00
Red Bear OS ae99d15bfa fix: add missing sys_includes to reduce cbindgen type warnings
- wchar.h: add stddef.h (wint_t/wchar_t), stdarg.h (va_list), stdio.h (FILE)
- ctype.h: add stddef.h (wint_t)
- string.h: add features.h, strings.h
- sys_wait.h: add stdint.h (siginfo_t types)
- sys_socket.h: add sys/un.h (sockaddr_un)
2026-06-28 19:24:17 +03:00
Red Bear OS e25fd20708 fix: define eventfd_t locally instead of re-export (cbindgen cross-module) 2026-06-28 16:21:45 +03:00
Red Bear OS 0c5f21d297 relibc: apply full eventfd implementation from P3 patches
- eventfd(), eventfd_read(), eventfd_write() functions
- uses kernel event scheme at /scheme/event/eventfd/
- eventfd_t type via bits_eventfd + after_includes in cbindgen
- EFD_SEMAPHORE, EFD_CLOEXEC, EFD_NONBLOCK constants
2026-06-28 11:31:27 +03:00
Red Bear OS 2ae6ef9a67 fix: add eventfd_t typedef in trailer for cbindgen 2026-06-28 11:19:01 +03:00
Red Bear OS ba2e6555af relibc: add sys/eventfd.h cbindgen config and eventfd_t type
libwayland and other users need sys/eventfd.h with:
- eventfd_t typedef (uint64_t)
- EFD_SEMAPHORE, EFD_CLOEXEC, EFD_NONBLOCK constants
- eventfd(), eventfd_read(), eventfd_write() prototypes
2026-06-28 10:35:49 +03:00
Red Bear OS c1b8c3b4cf abort: fix unsafe-op-in-unsafe-fn, signal path, unused imports
- abort() body: use signal::sys::SIGABRT (the platform-independent name
  the signal module uses for both linux and redox submodules)
- call sites: wrap abort() in unsafe { } blocks (Rust 2024 edition's
  unsafe_op_in_unsafe_fn lint makes this mandatory inside unsafe fns)
- stdlib/mod.rs, start.rs: drop now-unused 'intrinsics' import
2026-06-28 04:17:35 +03:00
Red Bear OS 4e40dc538c abort: raise(SIGABRT) + _Exit instead of ud2
Previously abort() called core::intrinsics::abort() which compiles to
the ud2 instruction, generating an Invalid Opcode fault. The kernel
logs this as 'UNHANDLED EXCEPTION' and kills the process, but the
fault message is alarming and doesn't reflect the actual intent
(SIGABRT from process self-termination).

This change uses the POSIX-compliant abort sequence: raise(SIGABRT)
first (default handler terminates the process), then _Exit(134) as
fallback if the signal handler returns. Six sites updated:
stdlib abort, assert __assert_fail, lib.rs relibc_panic/oom/_Unwind_Resume,
start.rs relibc_verify_host.

The proc-manager-fallback in redox-rt/src/sys.rs retains
core::intrinsics::abort() — that path is a true 'system unreachable'
last resort where raise/_Exit cannot succeed.
2026-06-28 04:00:30 +03:00
Red Bear OS fc8f0ec4fd Fix sched.h: use style=Both so sched_param has typedef (needed by spawn.h) 2026-06-27 23:53:21 +03:00
Red Bear OS f00e969b82 Fix: remove unnecessary unsafe block for safe sched_setscheduler call 2026-06-27 23:50:21 +03:00
Red Bear OS 3cb57fbc7f Add posix_spawnattr_{get,set}schedpolicy and schedparam functions 2026-06-27 23:45:14 +03:00