| 31 | class MmapMemorySource : public MemorySource { |
| 32 | public: |
| 33 | explicit MmapMemorySource(const std::filesystem::path& p) : name_(p.string()) { |
| 34 | file_ = CreateFileW(p.wstring().c_str(), |
| 35 | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 36 | nullptr, OPEN_EXISTING, |
| 37 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, |
| 38 | nullptr); |
| 39 | if (file_ == INVALID_HANDLE_VALUE) |
| 40 | throw_error("MmapMemorySource: CreateFile failed on '{}'", name_); |
| 41 | |
| 42 | LARGE_INTEGER sz{}; |
| 43 | if (!GetFileSizeEx(file_, &sz)) { |
| 44 | CloseHandle(file_); |
| 45 | throw_error("MmapMemorySource: GetFileSizeEx failed"); |
| 46 | } |
| 47 | size_ = static_cast<u64>(sz.QuadPart); |
| 48 | if (size_ == 0) { |
| 49 | CloseHandle(file_); |
| 50 | throw_error("MmapMemorySource: zero-byte file '{}'", name_); |
| 51 | } |
| 52 | |
| 53 | mapping_ = CreateFileMappingW(file_, nullptr, PAGE_READONLY, 0, 0, nullptr); |
| 54 | if (!mapping_) { |
| 55 | CloseHandle(file_); |
| 56 | throw_error("MmapMemorySource: CreateFileMapping failed (size={})", size_); |
| 57 | } |
| 58 | |
| 59 | view_ = MapViewOfFile(mapping_, FILE_MAP_READ, 0, 0, 0); |
| 60 | if (!view_) { |
| 61 | CloseHandle(mapping_); |
| 62 | CloseHandle(file_); |
| 63 | throw_error("MmapMemorySource: MapViewOfFile failed (size={})", size_); |
| 64 | } |
| 65 | |
| 66 | log::info("Mapped '{}' ({} bytes) into process address space at {}", |
| 67 | name_, size_, view_); |
| 68 | } |
| 69 | |
| 70 | ~MmapMemorySource() override { |
| 71 | if (view_) UnmapViewOfFile(view_); |
nothing calls this directly
no test coverage detected