Skip to content

od: skip past 4 GiB across inputs on 32-bit targets - #17030

Open
og-orca wants to merge 1 commit into
uutils:mainfrom
og-orca:fix/16998-od-skip-32bit
Open

og-orca wants to merge 1 commit into
uutils:mainfrom
og-orca:fix/16998-od-skip-32bit

Conversation

@og-orca

@og-orca og-orca commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #16998

On Linux, skip_in_file hands the skip to discard_n_bytes as n_skip as usize. On a 32-bit target a 4 GiB skip turns into 0, so the 5-byte header gets spliced for 0 bytes, od moves on to payload with the full 4 GiB still to skip, and you get FINAL instead of DRAFT.

Just using usize::MAX there wasn't enough. When I traced it under qemu-i386, splice with a length of 0xFFFFFFFF came back with EINVAL, and discard_n_bytes reports that as Ok(0), which looks like EOF. So the same wrong output came out. The change:

  • the splice length is now n_skip capped at isize::MAX;
  • if splice discarded all it was asked to, whatever is left of n_skip goes through read_and_discard, as in the existing Err branch;
  • a short Ok still means the input ended, same as before.

On 64-bit nothing changes for skips up to isize::MAX.

Before and after, on i686 with the sparse files from the issue:

$ od -Ad -tc -j4G -N5 header payload
  main: 4294967296   F   I   N   A   L
  fix:  4294967296   D   R   A   F   T

Test: test_skip_past_u32_max_across_inputs builds the issue's sparse payload and checks the output. It only runs on Linux and Android, since that is the only place the splice path exists. I ran it like this:

  • i686: built --target i686-unknown-linux-musl on macOS (linked with rust-lld), then ran the test binary in a linux/386 Debian container under qemu-i386 user emulation. On main the new test fails with F I N A L vs D R A F T. With the fix all 130 test_od tests pass (1 ignored, unchanged).
  • aarch64 Linux (native, 64-bit): all 130 test_od tests pass, the new one included.
  • macOS: 120 test_od tests and 57 uu_od unit tests pass.

cargo fmt --check is clean. cargo clippy -D warnings is clean for uu_od and the od tests, both on macOS and with --target i686-unknown-linux-gnu.

I haven't run it on a real 32-bit kernel, only under qemu-i386 on a 64-bit one.

@oech3

oech3 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Is it able to fix uucore side instead of od?

Comment thread src/uu/od/src/multifile_reader.rs Outdated
// `n_skip` may not fit in a `usize` on 32-bit targets, and splice(2)
// rejects a length above `isize::MAX`. Splice at most that many bytes,
// then read and discard the rest.
let max = isize::MAX as usize;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This restricts performance on 64 bit target. No?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isize::MAX cap is gone. Each splice call now asks for at most MAX_ROOTLESS_PIPE_SIZE, and one call can't move more than a pipe buffer anyway (the broker pipe is set to that size). 64-bit doesn't lose anything. It's still one loop of splices, with no read() fallback.

Comment thread src/uu/od/src/multifile_reader.rs Outdated
}
{
// `n_skip` may not fit in a `usize` on 32-bit targets, and splice(2)
// rejects a length above `isize::MAX`. Splice at most that many bytes,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we have MAX_ROOTLESS_PIPE_SIZE we could use at uucore::pipes side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done. It's the chunk size now, inside uucore::pipes::discard_n_bytes.

@og-orca
og-orca marked this pull request as ready for review November 30, 2026 18:25
Comment thread src/uu/od/src/multifile_reader.rs Outdated
Ok(spliced) if spliced < n => return Ok(n_skip - spliced as u64),
Ok(spliced) | Err(spliced) => spliced as u64,
};
let read = uucore::io::read_and_discard(curr, n_skip - spliced, SKIP_BUFFER_SIZE)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on 32-bit the remaining ~2 GiB then goes through read(), could we loop on discard_n_bytes instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. discard_n_bytes already looped, it just took a usize. With a u64 length and 1 MiB chunks it splices the whole skip on 32-bit too, and read() only runs when splice can't be used. I checked it on i686 under qemu. The new test fails on main and passes now.

@og-orca

og-orca commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Yes, I moved it there. discard_n_bytes now takes a u64 and asks each splice for at most MAX_ROOTLESS_PIPE_SIZE bytes, the same way wc does in count_bytes_using_splice. The only change left in od is passing n_skip without the as usize cast.

@og-orca
og-orca requested review from oech3 and sylvestre November 30, 2026 22:17
Comment thread src/uucore/src/lib/features/pipes.rs Outdated
///
/// Each splice asks for at most `MAX_ROOTLESS_PIPE_SIZE` bytes. A call never
/// moves more than one pipe buffer, and splice rejects a length above
/// `isize::MAX` (which `n` can exceed on 32-bit targets).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 14d3860, the doc comment on discard_n_bytes is back to the original three lines.

Comment thread src/uucore/src/lib/features/pipes.rs Outdated
#[inline]
pub fn discard_n_bytes(fd: impl AsFd, n: usize) -> Result<usize, usize> {
pub fn discard_n_bytes(fd: impl AsFd, n: u64) -> Result<u64, u64> {
let chunk = |discarded: u64| (n - discarded).min(MAX_ROOTLESS_PIPE_SIZE as u64) as usize;

@oech3 oech3 Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is sad to have such trivial helper just for 2 operations. But I hane no better idea...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could do that at uucore::pipes::splice instead and restrict this hack to 32bit only to avoid using more CPU on 64 bit target.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 14d3860, the clamp is in uucore::pipes::splice now and the chunk helper is gone.

I measured it first. With a normal 64 KiB input pipe the 1 MiB chunk changed nothing (65536 splices for a 4 GiB skip, same time). With a 16 MiB input pipe it did 1024 splices instead of 64, so you were right about the extra calls.

I left the clamp on 64-bit too, because 64-bit hits the same EINVAL: head -c 10E file asks splice for more than isize::MAX and falls back to read/write. On i686 the same happens to head -c 3G today. So one min before the syscall fixes od, head and tail in one place.

"before" is db2156a, "after" is 14d3860, "main" is 2a072db.

i686 under qemu-i386
$ od -Ad -tc -j4G -N5 header payload
main  : 4294967296   F   I   N   A   L
before: 4294967296   D   R   A   F   T
after : 4294967296   D   R   A   F   T

$ QEMU_STRACE=1 head -c 3G header >/dev/null   # 3 GiB > isize::MAX on i686
before: splice(3,0,5,0,-1073741824,0) = -1 errno=22 (Invalid argument)
after : splice(3,0,5,0,2147483647,0) = 5
aarch64 Linux, native
$ python3 big_pipe.py | strace -e trace=splice od -An -c -j1G -N5   # 16 MiB input pipe
before:  D R A F T   splice calls: 1024   largest: 1048576 bytes
after:  D R A F T   splice calls: 64   largest: 16777216 bytes

$ strace -f -e trace=splice head -c 10E header | wc -c   # 10 EiB > isize::MAX
before: splice(3, NULL, 5, NULL, 11529215046068469760, 0) = -1 EINVAL (Invalid argument)   -> 5 bytes
after: splice(3, NULL, 5, NULL, 9223372036854775807, 0) = 5   -> 5 bytes

cargo fmt --check and cargo clippy -p uucore --features pipes -D warnings are clean. test_od, test_head and test_tail pass on aarch64 Linux, except the two tail permission-denied tests, which fail only because my container runs as root.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pipe size > 1 MiB requires root priv. But it is fine to adopt maximal size. I am wondering why kernel itself cannot cap given length.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I couldn't work this out on my own at first. I only got it after I went through the research with claude. Here is what I found:

Only root can grow a pipe past 1 MiB, so that case is rare.

The kernel does cap it, but only after one check. splice, read and write all go through rw_verify_area, which returns EINVAL when (ssize_t)count < 0:
https://github.com/torvalds/linux/blob/v7.4-rc5/fs/read_write.c#L438-L439
Only after that is the length cut to MAX_RW_COUNT (about 2 GiB):
https://github.com/torvalds/linux/blob/v7.4-rc5/fs/splice.c#L896-L897

The syscall returns ssize_t, so a count it could not report back is rejected instead of clamped. POSIX leaves a count above SSIZE_MAX implementation-defined, and Linux picked EINVAL. read() behaves the same, which is why Rust std clamps every read to isize::MAX (READ_LIMIT in library/std/src/sys/fd/unix.rs). The min in our splice wrapper does the same.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sometimes use rustix::io::read by some reason. I think we don't use it with too large buf, but it should be considered.

@og-orca
og-orca requested a review from oech3 December 1, 2026 18:38
@oech3

oech3 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Please squash commits.

Comment thread src/uucore/src/lib/features/pipes.rs Resolved
skip_in_file passed n_skip to discard_n_bytes as usize, so on a 32-bit
target a 4 GiB skip became 0 and od printed the wrong bytes.
discard_n_bytes now takes a u64.

splice returns EINVAL for a length above isize::MAX, so the splice
wrapper clamps len to it. This also lets head and tail splice counts
above 2 GiB on 32-bit (and above 8 EiB on 64-bit) instead of falling
back to read/write.
@og-orca
og-orca force-pushed the fix/16998-od-skip-32bit branch from 14d3860 to 487222b Compare December 1, 2026 18:59
@codspeed

codspeed Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 39 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 70 untouched benchmarks
⏩ 431 skipped benchmarks1


Comparing og-orca:fix/16998-od-skip-32bit (487222b) with main (48819c9)

Open in CodSpeed

Footnotes

  1. 431 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
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.

od: 32-bit Linux reports bytes from the wrong offset when skipping 4 GiB across files

3 participants