Skip to content

refactor: real_fn! macro + fix PTS fd leak (#35)#39

Merged
Bahtya merged 2 commits into
mainfrom
refactor/real-fn-macro
May 24, 2026
Merged

refactor: real_fn! macro + fix PTS fd leak (#35)#39
Bahtya merged 2 commits into
mainfrom
refactor/real-fn-macro

Conversation

@Bahtya
Copy link
Copy Markdown
Owner

@Bahtya Bahtya commented May 24, 2026

Summary

  • Replace 78 manual std::mem::transmute(real("sym\0")) call sites with a real_fn! macro that centralizes dlsym resolution and adds debug_assert! null checks
  • Fix Vfd::Real fd leak in remove_table_and_get_fds() — PTS slave fd was never closed, causing stress: 20 rapid bash sessions to fail at iteration 4 (PTY exhaustion)
  • Net reduction: 121 additions, 252 deletions (−131 lines)

Changes

src/preload.rsreal_fn! macro

  • New real_fn!("sym\0", Type) macro: calls real(), debug_assert! null check, transmute to target type
  • All 78 transmute+real call sites replaced (27 unique symbols, 16+ type signatures)
  • Multi-call sites (poll, read, write, chdir, etc.) resolve once and reuse the local variable
  • Existing wrappers like real_close(fd) kept with updated internals
  • 2 fdsan transmute calls intentionally untouched (different pattern)

src/vfd.rs — PTS fd leak fix

  • remove_table_and_get_fds() now also collects Vfd::Real fds (e.g. PTY slave on fd 0/1/2)
  • Deduplicates by real fd value (since 0/1/2 share the same pts fd)
  • Previously only collected Vfd::File fds, causing 1 fd leak per hermux session

Test results

  • 31/31 APK tests pass (was 30/31 — stress: 20 rapid bash sessions now passes)
  • 43 unit tests pass, 0 clippy warnings
  • No performance regression (all timing tests within ±5ms noise)

Closes #35

Bahtya

Bahtya added 2 commits May 24, 2026 15:37
#32 #34 #36)

- #31: Replace incorrect u32[32] fd_set casts with libc::FD_ISSET/FD_SET
  (correct u64[16] on aarch64). Add fd >= FD_SETSIZE bounds check.
- #32: Fix poll(timeout=-1) infinite busy-loop. IoWait stores deadline,
  yield_for_io accepts timeout, wake_io_ready expires timed-out coroutines.
- #34: vdiag! macro now gated by VPROC_DIAG env var (AtomicBool). Zero
  write() syscalls when unset.
- #36: Replace 1000×10ms usleep spin-lock in JNI createSubprocess with
  pthread_cond_timedwait (500ms deadline).

31/31 APK tests pass, hermux round-trip 1028→1017ms.

Bahtya
Replace 78 manual std::mem::transmute(real("sym\0")) call sites with a
real_fn! macro that centralizes dlsym resolution and adds debug_assert
null checks. Also fix Vfd::Real fd leak in remove_table_and_get_fds
that caused stress test failures (PTS slave fd was never closed).

Bahtya
@Bahtya Bahtya merged commit 18558d7 into main May 24, 2026
@Bahtya Bahtya deleted the refactor/real-fn-macro branch May 24, 2026 09:49
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.

refactor: extract real_fn helper for repeated transmute pattern

1 participant