Repository navigation
[stdlib] Read and write files over 2 GiB on macOS - #7617
Conversation
NathanSWard
left a comment
There was a problem hiding this comment.
Hi @Alex-Wengg thanks for taking a stab at implementing this.
I left a round of feedback that will need to be addressed, then I'll be able to sync this in and get it merged internally.
I need to think a second on the best way to test this since allocation a 2GiB buffer in a unit test - might be too heavy weight for what we want, though I'm not 100% of a better way to test this.
|
Thanks for the review, All inline comments are addressed: the read/write cap now lives in |
NathanSWard
left a comment
There was a problem hiding this comment.
Thanks for addressing the comments!
Just as a heads up - after I sync this in internally I'm going to fixup the tests a bit to make sure they only run on macOS and I'm going to use some memory-mapping to prevent the need for allocation the 2Gib+ buffers on the heap.
Appreciate you taking the time to work on this!
|
@Alex-Wengg - do you mind fixing up the changelog merge conflict and then I'll get this synced in? Thanks! |
macOS fails a `read` or `write` of more than `INT_MAX` bytes with `EINVAL`, where Linux transfers fewer bytes instead. `FileHandle` passed the whole remaining count to each call, so on macOS `read_bytes()` and `read()` of more than 2 GiB raised "Invalid argument", `write_all()` and `write_once()` raised the same, and `write_bytes()` aborted. Cap each `read` and `write` call in `FileHandle` at `INT_MAX` bytes. The existing loops pick up the rest, and `write_once()` and `read()` into a `Span` already document that they can transfer fewer bytes than asked. Linux caps each call at `0x7FFFF000` bytes itself, so nothing changes there. Assisted-by: AI Signed-off-by: Alex-Wengg <[email protected]>
Move the per-call cap into `_platform_read()` and `_platform_write()`, which every `read` and `write` call in `FileHandle` now goes through, and apply it only on macOS, where a count over `c_int.MAX` fails with `EINVAL`. Other platforms pass the whole length again. `_platform_read()` is generic over the buffer's element type, so `read()` passes its typed buffer without a bitcast. In the tests, use `c_int` instead of `Int32` and `String(from_utf8_lossy=)`, say in the docstrings how much each test allocates, and add a TODO for the hardcoded `/dev/null` (modular#7618). Assisted-by: AI Signed-off-by: Alex-Wengg <[email protected]>
63563d3 to
4e0d1fd
Compare
|
@NathanSWard i hae pushed the merge conflict . i had thought copilot was gonna address the conflicts there. |
|
!sync |
|
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
|
Landed in deca448! Thank you for your contribution 🎉 |
[External] [stdlib] Read and write files over 2 GiB on macOS ## Linked issue Associated issue: #7616 ## Type of change - [x] Bug fix (non-breaking change that fixes an issue) ## Motivation On macOS, a `FileHandle` read or write of more than 2 GiB fails: `read_bytes()`, `read()`, `write_once()` and `write_all()` raise "Invalid argument", and `write_bytes()` aborts. ## What changed macOS rejects a `read` or `write` of more than `INT_MAX` bytes with `EINVAL` instead of transferring fewer. `FileHandle` now sends every `read` and `write` through `_platform_read()`/`_platform_write()`, which cap each call at `c_int.MAX` on macOS only; the existing loops do the rest, and other platforms pass the full length as before. `FileHandle.write()` has the same problem in `RawFileDescriptor.write_bytes()`, which I left alone because #5962 rewrites it. ## Testing - New `test_file_read_over_int_max` and `test_file_write_over_int_max` allocate `c_int.MAX + 1`-byte buffers (over 4 GiB in the read test, over 2 GiB in the write test). They fail on main and pass with this change; the rest of `test_file.mojo` and the other IO tests still pass. - Tested on macOS 27 arm64 with Mojo 1.3.0.dev2026111316 and a precompiled stdlib from this branch; not with Bazel or on Linux. ## Checklist - [ ] The linked issue above has been reviewed by a maintainer and is agreed-upon, or this is a trivial fix that does not need prior approval - [x] PR is small and focused - [ ] I ran `./bazelw run format` to format my changes (ran `mojo format` from the nightly; no changes) - [x] I added or updated tests to cover my changes - [x] If AI tools assisted with this contribution, I have included an `Assisted-by:` trailer in my commit message or this PR description Assisted-by: AI ORIGINAL_AUTHOR=A&W <[email protected]> ORIGINAL_USER=@Alex-Wengg PUBLIC_PR_HEAD=4e0d1fda2de5227962aee52c1c6ea7e53419effa --------- Co-authored-by: A&W <[email protected]> Co-authored-by: Nathan Ward <[email protected]> Pull-Request: #7617 MODULAR_ORIG_COMMIT_REV_ID: eb2ec8c37cbe91293d66caf2023e360278eeb7c0
Linked issue
Fixes #7616
Type of change
Motivation
On macOS, a
FileHandleread or write of more than 2 GiB fails:read_bytes(),read(),write_once()andwrite_all()raise "Invalid argument", andwrite_bytes()aborts.What changed
macOS rejects a
readorwriteof more thanINT_MAXbytes withEINVALinstead of transferring fewer.FileHandlenow sends everyreadandwritethrough_platform_read()/_platform_write(), which cap each call atc_int.MAXon macOS only; the existing loops do the rest, and other platforms pass the full length as before.FileHandle.write()has the same problem inRawFileDescriptor.write_bytes(), which I left alone because #5962 rewrites it.Testing
test_file_read_over_int_maxandtest_file_write_over_int_maxallocatec_int.MAX + 1-byte buffers (over 4 GiB in the read test, over 2 GiB in the write test). They fail on main and pass with this change; the rest oftest_file.mojoand the other IO tests still pass.Checklist
agreed-upon, or this is a trivial fix that does not need prior
approval
./bazelw run formatto format my changes (ranmojo formatfrom the nightly; no changes)Assisted-by:trailer in my commit message or this PR descriptionAssisted-by: AI