Skip to content

Fix Windows/MSVC handling of large files - #2748

Merged
sbc100 merged 7 commits into
WebAssembly:mainfrom
aydgelee:fix/windows-large-file-io
Oct 11, 2026
Merged

sbc100 merged 7 commits into
WebAssembly:mainfrom
aydgelee:fix/windows-large-file-io

Conversation

@aydgelee

@aydgelee aydgelee commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Proposed commit message

Fix large-file I/O on Windows/MSVC

Use 64-bit file metadata and seek/tell APIs while retaining shared stdio
read/write paths and checking file sizes before allocation. Add sparse-file
regressions for reads above 2GB and seek/backpatch operations above 4GB.

Problem

Windows/MSVC builds can fail to read large WAT inputs because a 64-bit process does not make long, fseek, ftell, or the default _stat file-size fields 64-bit. Actual use of inputs larger than 4GB exposed this bug. The earlier 2,978,116,509-byte business sample was a validation example, not the largest input used.

This fixes file I/O, not the parser's whole-file memory requirements. It does not introduce a streaming parser or promise arbitrary multi-GB buffers in a 32-bit process.

Changes

  • Under MSVC, use _stat64, _fseeki64, and _ftelli64 in common.cc; retain the file-size return type and reject sizes that cannot fit in size_t before allocation.
  • Use _fseeki64 in stream.cc without an extra SeekFile helper. On non-MSVC builds, check the actual seek offset against LONG_MAX before fseek.
  • Retain shared fread/fwrite paths. Microsoft's published UCRT source already bounds low-level transfers internally; an _read limit is not a limit on a whole fread call.
  • Add large-write/backpatch and unrepresentable-offset coverage, a sparse seek/backpatch regression at offset 4,294,967,296 (file length 4,294,967,297, without a 4GB buffer), and an MSVC x64 whole-file read regression at 2,147,483,649 bytes.
  • The read regression uses exclusive creation of a narrow filename in the current test directory, stdio I/O, and Windows APIs only to enable sparse storage. It needs roughly 2GB of memory because it exercises the actual whole-file ReadFile path.
  • Synchronize upstream main through b835f1ce in merge commit c28985b4. The upstream ByteSpan API and unified 8MB stack reserve from #2815 are retained; there is no separate stack-size change in this PR.

Validation

  • All 17 GitHub checks passed on code revision 2d6c0725, including CI and CIFuzz.
  • Windows x64 CI on that revision passed all 138 unit tests, C API tests, and the full test suite. Both large-file regressions actually ran: the >4GB seek/backpatch test in 5ms and the 2GB+ read test in 1131ms.
  • Local macOS validation on 2d6c0725 included a warnings-as-errors build, all 138 unit tests, roundtrip tests, and C API examples. Deliberately narrowing the seek offset to 32 bits failed the >4GB regression; the correct implementation passed. That diagnostic modification was not committed.
  • All 17 GitHub checks also passed on the newly synchronized head c28985b490142d276c1fae6ab9a53b77126000d1: CI and CIFuzz both completed successfully, including the s390x check. There are no failed or pending checks on this head.
  • The synchronized head c28985b4 also passed a local macOS Debug warnings-as-errors build, all 138 unit tests, C API examples, and the full test suite. For the wasm2c compiler, the local run explicitly set the Command Line Tools SDK path; no source change was needed. The generated-file/working-tree check was clean.
  • Windows x64 CI on c28985b4 passed its complete job. Logs confirm all 138 unit tests passed and both large-file regressions ran successfully, not skipped: >4GB seek/backpatch in 5ms and 2GB+ whole-file read in 1112ms.

Earlier Windows x64 business-sample validation was on 180b6bc7: a 2,978,116,509-byte WAT input produced a 92,039,608-byte wasm output with exit code 0 and an 8MB stack reserve. That is historical evidence, not a rerun of the current revision or the reported >4GB real-world input.

The MSVC 64-bit file interfaces are also available in 32-bit builds, but size_t and whole-file memory limits still apply. No new Windows x86 execution result is claimed. Linux/UNIX fseeko and _FILE_OFFSET_BITS=64 portability work is left for a separate follow-up, as suggested in review.

@sbc100

sbc100 commented May 28, 2026

Copy link
Copy Markdown
Member

For the 64-bit file ops can we do something simpler like?

#ifdef _MSC_VER
#define ftell _ftelli64
#define fseek _fseeki64
#endif

For the stack size do you know why we would need such a high value? IIUC the 8mb limit on linux seems to be enough there.

@aydgelee

aydgelee commented May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

For the file APIs, I agree the goal is simply to use the 64-bit MSVC CRT variants. I avoided globally redefining ftell / fseek because that changes all uses in the translation unit through macros. Keeping the _fseeki64 / _ftelli64 usage local to the large-file read path makes the change more explicit and avoids affecting unrelated code.

For the stack size, one clarification: /STACK on Windows controls the PE stack reserve size, not memory committed at process startup. It is an upper bound for the thread stack address range, and pages are committed on demand as the stack grows.

The reason this is included here is that, after the 64-bit file I/O fix, the large WAT test no longer failed with value too large; instead, it hit:

0xC00000FD
STATUS_STACK_OVERFLOW

on Windows.

MSVC-linked executables typically default to a 1MB stack reserve, while Linux/macOS environments commonly have larger or configurable stack limits, for example an 8MB soft stack limit on many Linux systems. That likely explains why the same workload can pass on Linux/macOS but fail on Windows.

So the larger /STACK value is intended to raise the Windows stack reserve ceiling for very large WAT parsing workloads. It does not mean the process commits that amount of memory at startup.

@aydgelee
aydgelee force-pushed the fix/windows-large-file-io branch 2 times, most recently from 177c0d9 to e5e08dc Compare May 28, 2026 08:17
@aydgelee

Copy link
Copy Markdown
Contributor Author

CI only reported a clang-format issue. No behavior change. Applied the suggested formatting diff.

Comment thread CMakeLists.txt Resolved
@sbc100

sbc100 commented May 28, 2026 •

Copy link
Copy Markdown
Member

For the file APIs, I agree the goal is simply to use the 64-bit MSVC CRT variants. I avoided globally redefining ftell / fseek because that changes all uses in the translation unit through macros. Keeping the _fseeki64 / _ftelli64 usage local to the large-file read path makes the change more explicit and avoids affecting unrelated code.

I'd much rather do it by via redefining these macros, keeping the the code otherwise the same on all platforms.

There really aren't very many file I/O codepaths in wabt, and I imagine we want to be able to handle large files in all of them.

Use MSVC macro redirection for stat, fseek, and ftell so the ReadFile path stays shared across platforms.

Move the MSVC stack reserve setting into the common wabt_executable helper and reduce it to 8MB.
@aydgelee
aydgelee force-pushed the fix/windows-large-file-io branch from e5e08dc to 180b6bc Compare May 29, 2026 10:06
@aydgelee

Copy link
Copy Markdown
Contributor Author

Updated.

  • Switched MSVC large-file file I/O to macro-based redirection:
    • stat -> _stat64
    • fseek -> _fseeki64
    • ftell -> _ftelli64
  • Kept the main ReadFile path shared across platforms.
  • Kept the MSVC-only size_t bounds check and chunked fread for 2GB+ inputs.
  • Moved the MSVC stack reserve into wabt_executable().
  • Changed the stack reserve to /STACK:8388608.
  • Non-MSVC behavior is unchanged.

Windows x64 verification:

  • Commit: 180b6bc
  • wasm2wat.exe --version: 1.0.41
  • wat2wasm.exe --version: 1.0.41
  • unity_temp.wat: 2,978,116,509 bytes
  • roundtrip.wasm: 92,039,608 bytes
  • wat2wasm exit code: 0

No "value too large", no "unable to read file", and no STATUS_STACK_OVERFLOW with the 8MB stack reserve.

Comment thread src/stream.cc Resolved
@sbc100

sbc100 commented May 29, 2026

Copy link
Copy Markdown
Member

Why do we need to read and write the files in chunks on windows? But not on other platforms? Could you update the PR description to include that maybe?

@sbc100

sbc100 commented May 29, 2026

Copy link
Copy Markdown
Member

Are you seeing these error on 64-bit windows too? Or is this mostly a fix for 32-bit windows?

Nishuuzz added a commit to Nishuuzz/wabt that referenced this pull request Aug 9, 2026
CI showed 512 still faults on windows: msvc's default stack is 1MB,
which is not enough to reach the limit, so deeply nested input dies
before the parser can report anything.

Picking a limit that does fit in 1MB is not really an option. It would
have to be somewhere under 256, and real modules need more than that --
round-tripping sqlite needs 320 -- so it would start rejecting input
that parses fine today on Linux and macOS.

Asking for the 8MB those platforms already give us instead. This is the
same change WebAssembly#2748 makes for the same reason; happy to drop it here if
that lands first.
Use MSVC 64-bit file APIs with shared stdio paths. Remove the redundant SeekFile and chunked IO helpers after checking the UCRT implementation. Guard non-MSVC seek offsets and add Windows x64 coverage above 2GB. Keep the upstream 8MB stack setting.
@aydgelee aydgelee changed the title Fix Windows/MSVC handling of large WAT input files and increase stack reserve Fix Windows/MSVC handling of large files Oct 2, 2026
@aydgelee

aydgelee commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated the code and rewrote the PR description to match it.

On the chunking question: I checked Microsoft's published UCRT source package. stdio/fread.cpp already bounds its low-level reads to INT_MAX, and stdio/fwrite.cpp also splits large requests. The _read limit therefore does not justify an extra limit on a whole fread call. I removed both additional Windows-only chunked IO paths; the revision now keeps the normal fread / fwrite calls shared across platforms.

On the platform question: the reported failure and the earlier successful 2,978,116,509-byte WAT conversion were both on Windows x64, not a 32-bit Windows process. MSVC's long and its default file metadata/seek/tell interfaces still have 32-bit limits there; _stat64, _fseeki64, and _ftelli64 address that separately from process pointer size. The size_t check remains for buffers that cannot represent the file size.

I also merged current main, resolved the ByteSpan changes, and retained the upstream 8MB stack setting from #2815. The old 512MB description has been removed.

Local warnings-as-errors builds and all 137 unit tests pass. All 17 GitHub checks on the final commit cbe0a753 have now passed. Windows x64 CI passed the build, 137 unit tests, C API tests, and full test suite. The new regression successfully read a 2,147,483,649-byte sparse file and patched it beyond the 2GB boundary, specifically checking the simplified, unchunked IO path; it ran rather than being skipped. The test is explicitly gated on MSVC x64 to match the scope of the fix. The earlier business-sample result remains historical; I am not claiming it was rerun for this revision.

@aydgelee

aydgelee commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@sbc100 The revisions are ready for another review at cbe0a753.

The earlier stack-size and SeekFile threads have been addressed and marked resolved. The PR now uses macro-based MSVC file API redirection and shared stdio paths, with the redundant Windows-only chunking removed. The Windows x64 scope and UCRT findings are documented in the updated description.

All 17 GitHub checks passed, including the Windows x64 regression that reads and patches a file above 2GB. Could you take another look when convenient?

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm with couple of question about testing.

@zherczeg WDYT?

Comment thread src/test-stream.cc Resolved
Comment thread src/test-stream.cc Resolved

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It is a bit of unclear the target of this patch for me. Do we need to process > 4GB files? Is this a real use case?

Comment thread src/common.cc Resolved
Comment thread src/common.cc Resolved
@aydgelee

aydgelee commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@zherczeg Yes, this is a real use case: I have used input files larger than 4GB, and that workload is how I encountered the bug. The previously documented 2,978,116,509-byte sample is an earlier validation example, not the largest input I have needed to process.

The target is large-file IO on Windows/MSVC, including a 64-bit process, where the default file-size and seek/tell interfaces still use 32-bit sizes or offsets. This patch is not a streaming-parser redesign and does not promise that a 32-bit process can hold a multi-GB input in memory.

The follow-up is now pushed as 2d6c0725, including current upstream main. The temporary-file test setup and duplicate MSVC include block have been simplified, and the API-availability and memory questions have been answered in their respective threads.

I added a sparse-file seek/backpatch regression above 4GB that does not allocate a 4GB buffer. The existing whole-file read regression stays just above the signed 32-bit boundary to limit its memory cost. The larger real-world input is a reported use case, not a claim that I have rerun that input in CI.

Windows x64 CI passed the build, all 138 unit tests, C API tests, and full test suite. Both the >4GB seek/backpatch regression and the revised 2GB+ read regression ran successfully, rather than being skipped. Other checks are still completing. @sbc100 The testing follow-up is ready for another look.

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Comment thread src/test-stream.cc Resolved
@sbc100

sbc100 commented Oct 5, 2026

Copy link
Copy Markdown
Member

Can you suggest a short summary for the commit message for this change, either by updating the PR description of posting your proposed commit message here as a comment.

@aydgelee

aydgelee commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

@sbc100 Proposed commit message (also added to the PR description):

Fix large-file I/O on Windows/MSVC

Use 64-bit file metadata and seek/tell APIs while retaining shared stdio
read/write paths and checking file sizes before allocation. Add sparse-file
regressions for reads above 2GB and seek/backpatch operations above 4GB.

I have synchronized upstream main through b835f1ce in c28985b4. The Linux/UNIX fseeko / _FILE_OFFSET_BITS=64 suggestion is acknowledged in its original thread and left for a separate follow-up, keeping this PR focused on Windows/MSVC.

All 17 checks on the synchronized head c28985b490142d276c1fae6ab9a53b77126000d1 have now passed: CI and CIFuzz both completed successfully, including s390x. No checks remain pending or failed.

Windows x64 CI passed the complete job. Its logs confirm all 138 unit tests passed and both large-file regressions actually ran successfully, not skipped: >4GB seek/backpatch in 5ms and 2GB+ whole-file read in 1112ms. The synchronized head also passed the local macOS warnings-as-errors build, unit tests, C API examples, and full test suite.

@sbc100
sbc100 merged commit e5cc0c4 into WebAssembly:main Oct 11, 2026
17 checks passed
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.

3 participants