newlib: correct RTEMS scalar type widths#5308
Closed
physwkim wants to merge 1 commit into
Closed
Conversation
On RTEMS, `time_t`, `dev_t`, `ino_t`, `rlim_t` and `clock_t` are all 8 bytes
wide; `libc` declares them 4. `clockid_t` is signed, `libc` declares it
unsigned. Measured with `arm-rtems6-gcc` for `armv7-rtems-eabihf` against the
`xilinx_zynq_a9_qemu` BSP (RTEMS 6.0.0, newlib 1b3dcfd, gcc 13.3.0), from
`<sys/types.h>` alone:
TC rlim_t size=8 align=8 signed=1
TC dev_t size=8 align=8 signed=0
TC ino_t size=8 align=8 signed=0
TC time_t size=8 align=8 signed=1
TC clock_t size=8 align=8 signed=0
TC clockid_t size=4 align=4 signed=1
`time_t` is the one with teeth: it makes `struct timespec` 8 bytes where
RTEMS's is 16, so every `clock_gettime` writes 4 bytes past the end of the
caller's slot, and `std`'s `SystemTime`/`Instant` read `tv_nsec` as 0.
`clock_t` is per-arch in this tree, so the RTEMS value goes in
`newlib/rtems/` and `newlib/arm/`'s copy is gated with
`not(target_os = "rtems")`; no non-RTEMS newlib target changes.
Author
|
@rustbot label stable-nominated |
3 tasks
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Six scalar types are wrong for
target_os = "rtems". Measured with anarm-rtems6toolchain and run onarmv7-rtems-eabihfunderqemu-system-arm, including only<sys/types.h>and<time.h>:libctime_ti32c_longlongdev_tu32u64ino_tu32u64rlim_tu32i64clock_tc_longc_ulonglongclockid_tc_ulongc_intThe other 13 newlib scalars were measured too and are correct,
off_t(already
i64) included.Impact
libc::timespecis{ time_t, c_long }, so withtime_t = i32it is 8 byteswhere RTEMS's is 16.
std'sSystemTime::now/Instant::nowpass aMaybeUninit<libc::timespec>toclock_gettime, so every clock read writes4 bytes past the end of that stack slot (measured: 12 bytes written into an
8-byte slot; a canary 8 bytes past it is clobbered), and
tv_nseclands wherethe 8-byte view never reads it:
So on RTEMS today every sub-second duration silently measures zero — every
Instant-based timeout, rate limit and watchdog reads 0 with no panic and nodiagnostic. That is the half that makes this urgent; the out-of-bounds write is
the half that makes it a soundness bug.
After the patch:
timespec16/align 8,subsec_nanos=15715519,Instant::elapsed()nonzero. Derived layouts then match the target exactly —timeval16,itimerspec32,struct stat104 withst_size@40.Changes
time_t,dev_t,ino_t,rlim_t,clockid_tgain atarget_os = "rtems"arm in
src/unix/newlib/mod.rs, which already selects bytarget_osbeforefalling through to
target_arch.clock_tis declared per-arch, so the RTEMSvalue goes in
src/unix/newlib/rtems/(already glob-imported for RTEMS) andnewlib/arm's copy is gated#[cfg(not(target_os = "rtems"))].No non-RTEMS target changes.
timespec/timeval/itimerspecneed noseparate fix — they follow from
time_t.Verification
const _: () = assert!(...)file over all seven types: 0 errors on thisbranch, 16 errors on
main.riscv32imc-esp-espidfbuilds, unchanged.armv7-sony-vita-newlibeabihfandarmv6k-nintendo-3dsfail identically before and after (pre-existingE0573atsrc/unix/mod.rs:936).cargo +nightly fmt --all -- --checkclean.2faafecb, gcc 13.3.0, newlib1b3dcfd, BSPxilinx_zynq_a9_qemu. Standalone reproduction available (one crate, an RSBtoolchain and
qemu-system-arm, nothing else) — happy to attach.Relation to #5132
#5132 proposes
time_t = i64for the non-espidf/vita branch, which covers thetime_trow here and is correct. It leavesdev_t/ino_tatu32— measured8 on RTEMS — and does not touch
rlim_t,clock_torclockid_t. Happy torebase this down to the four types it does not cover if it lands first;
flagging so the two are not merged blind to each other.
Must reach 0.2 to change anything for
std:library/std/Cargo.tomldepends onlibc 0.2.x.@rustbot label stable-nominated
Sibling: #5307 (RTEMS socket address types) — unrelated defect, but both add to
src/unix/newlib/rtems/mod.rs, so whichever lands second needs a one-hunkcontext rebase there.