Skip to content

Fix tcsetattr and Timespec layout for the libc backend on 32-bit MIPS musl - #1766

Open
kuesttman wants to merge 2 commits into
bytecodealliance:mainfrom
SolydSys:musl-mips-fixes-main
Open

kuesttman wants to merge 2 commits into
bytecodealliance:mainfrom
SolydSys:musl-mips-fixes-main

Conversation

@kuesttman

Copy link
Copy Markdown

Fixes #1765.

Two independent fixes for mips*-unknown-linux-musl, which uses the libc backend:

  1. tcsetattr request codes on MIPS with musl. The MIPS adjustment (optional_actions - TCSETS) assumes glibc, where TCSANOW == TCSETS on MIPS. musl defines TCSANOW as 0, so every tcsetattr call failed with ENOTTY. The adjustment now applies only when target_env != "musl". New test: test_termios_optional_actions, which checks Now, Drain and Flush on a pty.
  2. Timespec layout on 32-bit musl. With musl's 64-bit time_t ABI (libc_unstable_musl_v1_2 in the libc crate), tv_nsec is padded to 64 bits, with the padding before it on big-endian. With Nsecs = c_long, libc read tv_nsec from rustix's uninitialized padding on big-endian, so poll/ppoll timeouts lost their sub-second part. Nsecs is now i64 for the libc backend on 32-bit musl, which matches both endiannesses. For this case, the field-offset test is replaced by timespec_musl32_matches_libc, which reads the values back through c::timespec.

Testing:

  • mips-unknown-linux-musl, run on hardware (MIPS32r2 big-endian, musl 1.2.5, built with nightly -Zbuild-std and --cfg libc_unstable_musl_v1_2): tests/termios passes except ttyname::test_ttyname_not_tty, which also fails without this change (environment). The lib timespec tests pass. Before the change, 6 termios tests fail.
  • x86_64-unknown-linux-gnu, with and without use-libc: the lib timespec tests and tests/termios pass.

Not addressed: 32-bit musl with the libc crate default (32-bit time_t). See the issue.

On MIPS, glibc defines `TCSANOW`/`TCSADRAIN`/`TCSAFLUSH` as `TCSETS`,
`TCSETSW` and `TCSETSF`, so the libc backend subtracted `TCSETS` from
`optional_actions` to get the offset. musl defines `TCSANOW` as 0 on
every architecture, so on `mips*-unknown-linux-musl` the computed request
was `TCSETS2 - TCSETS`, an invalid ioctl that fails with `ENOTTY`. The
`TCSETS` fallback then issued `ioctl(fd, 0)`, which also fails, so every
`tcsetattr` call returned `ENOTTY`.

Only apply the MIPS adjustment when not targeting musl, and add a test that
exercises every `OptionalActions` value on a pseudoterminal.
With a 64-bit `time_t` (musl >= 1.2 ABI, `libc_unstable_musl_v1_2` in the
`libc` crate), musl's `struct timespec` on 32-bit targets pads `tv_nsec` to
64 bits: `{ i64 tv_sec; u32 pad; i32 tv_nsec }` on big-endian and
`{ i64 tv_sec; i32 tv_nsec; u32 pad }` on little-endian. rustix used
`Nsecs = c_long`, giving `{ i64 tv_sec; i32 tv_nsec; u32 pad }` on every
endianness. Both are 16 bytes, so the pointer cast in
`as_libc_timespec_ptr` compiles, but on big-endian (e.g.
`mips-unknown-linux-musl`) libc reads `tv_nsec` from rustix's uninitialized
padding: `poll`/`ppoll` timeouts lose their sub-second part, and a garbage
value would make the call fail with `EINVAL`.

Use a 64-bit `Nsecs` for the libc backend on 32-bit musl, which matches
both layouts, and replace the field-offset layout test for this case with
a test that reads the values back through `c::timespec`.
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.

libc backend on mips-unknown-linux-musl: tcsetattr fails with ENOTTY and Timespec timeouts lose tv_nsec

1 participant