Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ if (BUILD_TESTS)
src/test-intrusive-list.cc
src/test-literal.cc
src/test-option-parser.cc
src/test-stream.cc
src/test-filenames.cc
src/test-utf8.cc
src/test-wast-parser.cc
Expand Down
17 changes: 14 additions & 3 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
#include <fcntl.h>
#include <io.h>
#include <stdlib.h>
#include <limits>
#define PATH_MAX _MAX_PATH
#define stat _stat
#define stat _stat64
Comment thread
aydgelee marked this conversation as resolved.
#define fseek _fseeki64
#define ftell _ftelli64
#define S_IFREG _S_IFREG
#endif

Expand Down Expand Up @@ -129,7 +132,7 @@ Result ReadFile(std::string_view filename, std::vector<uint8_t>* out_data) {
return res;
}

long size = ftell(infile);
auto size = ftell(infile);
if (size < 0) {
perror("ftell failed");
fclose(infile);
Expand All @@ -142,7 +145,15 @@ Result ReadFile(std::string_view filename, std::vector<uint8_t>* out_data) {
return Result::Error;
}

out_data->resize(size);
#if COMPILER_IS_MSVC
if (static_cast<uint64_t>(size) >
static_cast<uint64_t>(std::numeric_limits<size_t>::max())) {
fprintf(stderr, "%s: file too large to read into memory\n", filename_cstr);
fclose(infile);
return Result::Error;
}
#endif
out_data->resize(static_cast<size_t>(size));
if (size != 0 && fread(out_data->data(), size, 1, infile) != 1) {
fprintf(stderr, "%s: fread failed: %s\n", filename_cstr, strerror(errno));
fclose(infile);
Expand Down
15 changes: 14 additions & 1 deletion src/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include <cassert>
#include <cctype>
#include <cerrno>
#include <climits>

#if COMPILER_IS_MSVC
#define fseek _fseeki64
#endif

#define DUMP_OCTETS_PER_LINE 16
#define DUMP_OCTETS_PER_GROUP 2
Expand Down Expand Up @@ -273,8 +278,16 @@ Result FileStream::WriteDataImpl(size_t at, ByteSpan data) {
return Result::Ok;
}
if (at != offset_) {
#if !COMPILER_IS_MSVC
// fseek takes a long offset on this path; do not truncate a larger size_t.
if (at > static_cast<size_t>(LONG_MAX)) {
errno = EINVAL;
ERROR("fseek offset=%" PRIzd " failed, errno=%d\n", at, errno);
return Result::Error;
}
#endif
if (fseek(file_, at, SEEK_SET) != 0) {
ERROR("fseek offset=%" PRIzd " failed, errno=%d\n", data.size(), errno);
ERROR("fseek offset=%" PRIzd " failed, errno=%d\n", at, errno);
return Result::Error;
}
offset_ = at;
Expand Down
178 changes: 178 additions & 0 deletions src/test-stream.cc
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());

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.

I guess maybe on linux/UNIX we should use fseeko and set _FILE_OFFSET_BITS=64? But that can be followup of course

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
Loading