Skip to content

[stdlib] Read and write files over 2 GiB on macOS - #7617

Merged
modularbot merged 2 commits into
modular:mainfrom
Alex-Wengg:fix/macos-file-io-int-max
Nov 18, 2026
Merged

modularbot merged 2 commits into
modular:mainfrom
Alex-Wengg:fix/macos-file-io-int-max

Conversation

@Alex-Wengg

@Alex-Wengg Alex-Wengg commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Fixes #7616

Type of change

  • 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
  • PR is small and focused
  • I ran ./bazelw run format to format my changes (ran mojo format from the nightly; no changes)
  • I added or updated tests to cover my changes
  • 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

@Alex-Wengg
Alex-Wengg requested review from a team as code owners November 15, 2026 04:31
@github-actions github-actions Bot added mojo-stdlib Tag for issues related to standard library waiting-on-review mojo-docs labels Nov 15, 2026
@NathanSWard NathanSWard self-assigned this Nov 15, 2026

@NathanSWard NathanSWard left a comment

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.

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.

Comment thread Mojo/stdlib/std/io/file.mojo Resolved
Comment thread Mojo/stdlib/test/builtin/test_file.mojo Resolved
Comment thread Mojo/stdlib/test/builtin/test_file.mojo Resolved
Comment thread Mojo/stdlib/test/builtin/test_file.mojo Resolved
Comment thread Mojo/stdlib/test/builtin/test_file.mojo Resolved
Comment thread Mojo/stdlib/std/io/file.mojo Resolved
@Alex-Wengg

Copy link
Copy Markdown
Contributor Author

Thanks for the review, All inline comments are addressed: the read/write cap now lives in _platform_read()/_platform_write() and applies only on macOS.
On testing: I can gate these tests to macOS to keep the 2 GiB buffers off Linux CI

@NathanSWard NathanSWard left a comment

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.

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!

@NathanSWard

Copy link
Copy Markdown
Contributor

@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]>
@Alex-Wengg
Alex-Wengg force-pushed the fix/macos-file-io-int-max branch from 63563d3 to 4e0d1fd Compare November 16, 2026 18:56
@Alex-Wengg

Copy link
Copy Markdown
Contributor Author

@NathanSWard i hae pushed the merge conflict . i had thought copilot was gonna address the conflicts there.

@NathanSWard

Copy link
Copy Markdown
Contributor

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Nov 16, 2026
@modularbot

Copy link
Copy Markdown
Collaborator

✅🟣 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.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally labels Nov 17, 2026
@modularbot

Copy link
Copy Markdown
Collaborator

Landed in deca448! Thank you for your contribution 🎉

modularbot pushed a commit that referenced this pull request Nov 18, 2026
[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
@modularbot
modularbot merged commit c40a0db into modular:main Nov 18, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported-internally Signals that a given pull request has been imported internally. merged-internally Indicates that this pull request has been merged internally mojo-docs mojo-stdlib Tag for issues related to standard library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] macOS: FileHandle reads and writes of more than 2 GiB fail with "Invalid argument"

3 participants