upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
authorsodove <mixercraft@gmail.com>2026-11-24 15:31:26 +0500
committersodove <mixercraft@gmail.com>2026-11-24 15:31:26 +0500
commit291a96f7de73b66258ccf5380689b791e677c228 (patch)
treee57feabbdfede02e0e6ef7b5c7607a7abba42379 /runtime/src/pe_loader.cpp
parent21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff)
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.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp16
1 files changed, 12 insertions, 4 deletions
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 @@
2 2
3#include <windows.h> 3#include <windows.h>
4#include <intrin.h> 4#include <intrin.h>
5#include <share.h>
5#include <tlhelp32.h> 6#include <tlhelp32.h>
6 7
7#include <algorithm> 8#include <algorithm>
@@ -10,7 +11,7 @@
10#include <cstdio> 11#include <cstdio>
11#include <cstdlib> 12#include <cstdlib>
12#include <cstring> 13#include <cstring>
13#include <fstream> 14#include <memory>
14#include <mutex> 15#include <mutex>
15#include <stdexcept> 16#include <stdexcept>
16#include <system_error> 17#include <system_error>
@@ -75,9 +76,16 @@ std::string path_utf8(const std::filesystem::path& path) {
75} 76}
76 77
77std::vector<uint8_t> read_file(const std::filesystem::path& p) { 78std::vector<uint8_t> read_file(const std::filesystem::path& p) {
78 std::ifstream f(p, std::ios::binary); 79 std::unique_ptr<std::FILE, decltype(&std::fclose)> file(
79 if (!f) throw std::runtime_error("cannot open " + path_utf8(p)); 80 _wfsopen(p.c_str(), L"rb", _SH_DENYNO), std::fclose);
80 return {std::istreambuf_iterator<char>(f), {}}; 81 if (!file) throw std::runtime_error("cannot open " + path_utf8(p));
82
83 std::vector<uint8_t> bytes;
84 std::array<uint8_t, 64 * 1024> block;
85 while (const auto count = std::fread(block.data(), 1, block.size(), file.get()))
86 bytes.insert(bytes.end(), block.data(), block.data() + count);
87 if (std::ferror(file.get())) throw std::runtime_error("cannot read " + path_utf8(p));
88 return bytes;
81} 89}
82 90
83template <class T> 91template <class T>