Repository navigation
Fix Windows/MSVC handling of large files #2748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aydgelee
wants to merge
6
commits into
WebAssembly:main
Choose a base branch
from
aydgelee:fix/windows-large-file-io
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
180b6bc
Address Windows large-file review feedback
aydgelee cc1a0e4
Merge upstream main and simplify Windows large-file IO
aydgelee 56f6c8c
Use explicit resource deleters in file IO regression tests
aydgelee cbe0a75
Restrict the large-file Windows regression to MSVC x64
aydgelee 7d99dbb
Merge remote-tracking branch 'upstream/main' into fix/windows-large-f…
aydgelee 2d6c072
Simplify large-file regressions and cover seeks above 4GB
aydgelee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /* | ||
| * Copyright 2026 WebAssembly Community Group participants | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <climits> | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include "wabt/stream.h" | ||
|
|
||
| #if COMPILER_IS_MSVC && defined(_WIN64) | ||
| #ifndef NOMINMAX | ||
| #define NOMINMAX | ||
| #endif | ||
| #include <io.h> | ||
| #include <windows.h> | ||
| #include <winioctl.h> | ||
| #endif | ||
|
|
||
| using namespace wabt; | ||
|
|
||
| namespace { | ||
|
|
||
| struct FileCloser { | ||
| void operator()(FILE* file) const { fclose(file); } | ||
| }; | ||
| } // namespace | ||
|
|
||
| #if SIZE_MAX > UINT32_MAX | ||
| TEST(FileStream, SeekAndPatchAbove4GB) { | ||
| std::unique_ptr<FILE, FileCloser> file(tmpfile()); | ||
| ASSERT_NE(nullptr, file); | ||
| #if COMPILER_IS_MSVC | ||
| // Keep the disk footprint small when seeking beyond the end on NTFS. | ||
| HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(file.get()))); | ||
| DWORD bytes_returned; | ||
| if (!DeviceIoControl(handle, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, | ||
| &bytes_returned, nullptr)) { | ||
| GTEST_SKIP() << "This large-file test requires sparse-file support"; | ||
| } | ||
| #endif | ||
| constexpr size_t kOffset = size_t{1} << 32; | ||
| FileStream stream(file.get()); | ||
| const std::array<uint8_t, 1> initial{0x12}; | ||
| stream.WriteDataAt(kOffset, initial); | ||
| ASSERT_EQ(Result::Ok, stream.result()); | ||
| const std::array<uint8_t, 1> patch{0xab}; | ||
| stream.WriteDataAt(kOffset, patch); | ||
| ASSERT_EQ(Result::Ok, stream.result()); | ||
| stream.Flush(); | ||
| #if COMPILER_IS_MSVC | ||
| ASSERT_EQ(0, _fseeki64(file.get(), 0, SEEK_END)); | ||
| EXPECT_EQ(kOffset + 1, static_cast<size_t>(_ftelli64(file.get()))); | ||
| ASSERT_EQ(0, _fseeki64(file.get(), kOffset, SEEK_SET)); | ||
| #else | ||
| ASSERT_EQ(0, fseek(file.get(), 0, SEEK_END)); | ||
| EXPECT_EQ(kOffset + 1, static_cast<size_t>(ftell(file.get()))); | ||
| ASSERT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); | ||
| #endif | ||
| EXPECT_EQ(0xab, fgetc(file.get())); | ||
| EXPECT_EQ(EOF, fgetc(file.get())); | ||
| EXPECT_EQ(0, ferror(file.get())); | ||
| } | ||
| #endif | ||
|
|
||
| TEST(FileStream, LargeWriteAndPatch) { | ||
| std::unique_ptr<FILE, FileCloser> file(tmpfile()); | ||
| ASSERT_NE(nullptr, file); | ||
| constexpr size_t kChunkSize = 64 * 1024 * 1024; | ||
| std::vector<uint8_t> data(kChunkSize + 17, 0x5a); | ||
| data.front() = 0x12; | ||
| data.back() = 0x34; | ||
| FileStream stream(file.get()); | ||
| stream.WriteData(data); | ||
| ASSERT_EQ(Result::Ok, stream.result()); | ||
|
|
||
| // Patch inside the large output, exercising the file-seek path. | ||
| const std::array<uint8_t, 3> patch{0xab, 0xcd, 0xef}; | ||
| stream.WriteDataAt(kChunkSize - 1, patch); | ||
| ASSERT_EQ(Result::Ok, stream.result()); | ||
| std::copy(patch.begin(), patch.end(), data.begin() + kChunkSize - 1); | ||
| stream.Flush(); | ||
| ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | ||
|
|
||
| std::array<uint8_t, 4096> buffer; | ||
| size_t offset = 0; | ||
| while (offset < data.size()) { | ||
| size_t count = std::min(buffer.size(), data.size() - offset); | ||
| ASSERT_EQ(count, fread(buffer.data(), 1, count, file.get())); | ||
| ASSERT_TRUE(std::equal(buffer.begin(), buffer.begin() + count, | ||
| data.begin() + offset)); | ||
| offset += count; | ||
| } | ||
| EXPECT_EQ(EOF, fgetc(file.get())); | ||
| EXPECT_EQ(0, ferror(file.get())); | ||
| } | ||
|
|
||
| #if !COMPILER_IS_MSVC | ||
| TEST(FileStream, RejectUnrepresentableSeekOffset) { | ||
| std::unique_ptr<FILE, FileCloser> file(tmpfile()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess maybe on linux/UNIX we should use |
||
| ASSERT_NE(nullptr, file); | ||
| FileStream stream(file.get()); | ||
| const std::array<uint8_t, 1> data{0xab}; | ||
| const size_t offset = static_cast<size_t>(LONG_MAX) + 1; | ||
| stream.WriteDataAt(offset, data); | ||
| EXPECT_EQ(Result::Error, stream.result()); | ||
| stream.Flush(); | ||
| rewind(file.get()); | ||
| EXPECT_EQ(EOF, fgetc(file.get())); | ||
| } | ||
| #endif | ||
|
|
||
| #if COMPILER_IS_MSVC && defined(_WIN64) | ||
| TEST(FileStream, ReadAndPatchAbove2GBOnWindows64) { | ||
| const char* filename = "large_file.dat"; | ||
| struct TempFile { | ||
| const char* path = nullptr; | ||
| ~TempFile() { | ||
| if (path) { | ||
| remove(path); | ||
| } | ||
| } | ||
| } temp; | ||
| // Exclusive creation avoids overwriting an existing file in the test dir. | ||
| std::unique_ptr<FILE, FileCloser> file(fopen(filename, "w+bx")); | ||
| ASSERT_NE(nullptr, file); | ||
| temp.path = filename; | ||
|
|
||
| // Only sparse-file setup needs the Windows API; stdio handles the file IO. | ||
| HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(file.get()))); | ||
| DWORD bytes_returned; | ||
| if (!DeviceIoControl(handle, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, | ||
| &bytes_returned, nullptr)) { | ||
| GTEST_SKIP() << "This large-file test requires sparse-file support"; | ||
| } | ||
|
|
||
| // A sparse file avoids allocating 2GB of disk space. Only Windows x64 runs | ||
| // this test because ReadFile still needs a buffer of roughly 2GB. | ||
| constexpr size_t kOffset = static_cast<size_t>(INT_MAX) + 1; | ||
| ASSERT_EQ(0, _fseeki64(file.get(), kOffset, SEEK_SET)); | ||
| ASSERT_NE(EOF, fputc(0x12, file.get())); | ||
| ASSERT_EQ(0, fflush(file.get())); | ||
|
|
||
| { | ||
| FileStream stream(file.get()); | ||
| const std::array<uint8_t, 1> patch{0xab}; | ||
| stream.WriteDataAt(kOffset, patch); | ||
| ASSERT_EQ(Result::Ok, stream.result()); | ||
| stream.Flush(); | ||
| } | ||
| file.reset(); | ||
|
|
||
| std::vector<uint8_t> data; | ||
| ASSERT_EQ(Result::Ok, ReadFile(filename, &data)); | ||
| ASSERT_EQ(kOffset + 1, data.size()); | ||
| EXPECT_EQ(0, data.front()); | ||
| EXPECT_EQ(0, data[kOffset - 1]); | ||
| EXPECT_EQ(0xab, data.back()); | ||
| } | ||
| #endif | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.