From 291a96f7de73b66258ccf5380689b791e677c228 Mon Sep 17 00:00:00 2001 From: sodove Date: Tue, 24 Nov 2026 15:31:26 +0500 Subject: runtime: read PE files in blocks (RT-PE-BLOCK-READ) Based on the block-read proposal and Wine measurements from cobrabm12. Add a Windows regression for a read failure after a valid prefix. --- runtime/src/pe_loader.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index c29a288a9..b89a50aa0 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -10,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -75,9 +76,16 @@ std::string path_utf8(const std::filesystem::path& path) { } std::vector read_file(const std::filesystem::path& p) { - std::ifstream f(p, std::ios::binary); - if (!f) throw std::runtime_error("cannot open " + path_utf8(p)); - return {std::istreambuf_iterator(f), {}}; + std::unique_ptr file( + _wfsopen(p.c_str(), L"rb", _SH_DENYNO), std::fclose); + if (!file) throw std::runtime_error("cannot open " + path_utf8(p)); + + std::vector bytes; + std::array block; + while (const auto count = std::fread(block.data(), 1, block.size(), file.get())) + bytes.insert(bytes.end(), block.data(), block.data() + count); + if (std::ferror(file.get())) throw std::runtime_error("cannot read " + path_utf8(p)); + return bytes; } template -- cgit v1.2.3