Merge pull request #18129 from oneandonlydean/revert-15967-zero-scan

revert(agc): revert #15967 zero-scan skip, red on real Windows GPUs
This commit is contained in:
Dean authored and GitHub committed 2026-11-11 04:37:36 +13:00
commit c5488026ab
10 files changed
+8 -386

No files matched your search

@@ -120,8 +120,6 @@ bool UnchangedCommittedSince(std::span<const std::pair<std::uint64_t, std::uint6
std::uint64_t MarkWritten(std::uint64_t address, std::size_t bytes);
std::uint64_t MarkPossiblyWritten(std::uint64_t address, std::size_t bytes);
std::uint64_t MarkPossiblyWritten(std::uint64_t address, std::size_t bytes, std::uint64_t& order);
void ForgetKnownZero(std::uint64_t address, std::size_t bytes);
void MaterializeKnownZero(std::uint64_t address, std::size_t bytes);
std::uint64_t StoreOwnBytes(std::uint64_t address, std::size_t bytes, const std::function<void()>& store);
std::uint64_t StoreOwnBytes(std::uint64_t address, std::size_t bytes, const std::function<void(std::span<std::byte>)>& store);
std::uint64_t StoreOwnBytes(std::uint64_t address, std::span<const std::byte> source);
@@ -2664,17 +2664,14 @@ static std::uint64_t markWritten(std::uint64_t address, std::size_t bytes, bool
}
std::uint64_t MarkWritten(std::uint64_t address, std::size_t bytes) {
ForgetKnownZero(address, bytes);
return markWritten(address, bytes, true);
}
std::uint64_t MarkPossiblyWritten(std::uint64_t address, std::size_t bytes) {
ForgetKnownZero(address, bytes);
return markWritten(address, bytes, false);
}
std::uint64_t MarkPossiblyWritten(std::uint64_t address, std::size_t bytes, std::uint64_t& order) {
ForgetKnownZero(address, bytes);
return markWritten(address, bytes, false, &order);
}
@@ -3252,26 +3249,6 @@ void GuardRead(std::uint64_t address, std::size_t bytes) {
if (const auto guard = readGuard.load(std::memory_order_acquire)) guard(address, bytes);
}
void ForgetKnownZero(std::uint64_t address, std::size_t bytes) {
#ifdef _WIN32
GuestArena::GuestArenaForgetKnownZero_nid_no_patch(static_cast<std::uintptr_t>(address), bytes);
#else
static_cast<void>(address);
static_cast<void>(bytes);
#endif
}
void MaterializeKnownZero(std::uint64_t address, std::size_t bytes) {
#ifdef _WIN32
GuestArena::GuestArenaKnownZero_nid_no_patch(static_cast<std::uintptr_t>(address), bytes, [](void*, std::uintptr_t first, std::uintptr_t last) {
for (auto page = first; page < last; page += 4096) static_cast<void>(*reinterpret_cast<const volatile unsigned char*>(page));
}, nullptr);
#else
static_cast<void>(address);
static_cast<void>(bytes);
#endif
}
void FlushGpuWrites(std::uint64_t address, std::size_t bytes) {
if (const auto hook = flushHook.load(std::memory_order_acquire)) hook(address, bytes);
}
@@ -3553,7 +3553,6 @@ void Recorder::OnComplete(std::function<void()> action) {
bool Recorder::noteWrite(std::uint64_t address, std::size_t bytes, bool ownLabel, int value, std::int64_t word) {
CaptureTrace::Log("buffer-write batch=%llu address=%llx bytes=%zu label=%d", static_cast<unsigned long long>(submissions + 1), static_cast<unsigned long long>(address), bytes, ownLabel);
if (bytes == 0) return false;
GuestMemory::ForgetKnownZero(address, bytes);
ensureOpen();
const auto end = address + bytes;
open->writes.emplace_back(address, end);
@@ -3575,7 +3574,6 @@ bool Recorder::noteWrite(std::uint64_t address, std::size_t bytes, bool ownLabel
void Recorder::noteWriteOn(Batch& batch, std::uint64_t address, std::size_t bytes, bool ownLabel) {
if (bytes == 0) return;
if (&batch != open.get()) GuestMemory::ForgetKnownZero(address, bytes);
if (&batch == open.get()) {
if (!noteWrite(address, bytes, ownLabel)) return;
const std::array ranges{std::pair{address, address + bytes}};
@@ -288,14 +288,7 @@ void populatedPages(std::vector<Candidate>& maybe, const Mapping& mapping) {
std::vector<Candidate> possiblyNonzero(std::uint64_t begin, std::uint64_t end) {
std::vector<Candidate> maybe;
#ifdef _WIN32
std::vector<std::pair<std::uintptr_t, std::uintptr_t>> zero;
GuestArena::GuestArenaKnownZero_nid_no_patch(static_cast<std::uintptr_t>(begin), static_cast<std::size_t>(end - begin), [](void* context, std::uintptr_t first, std::uintptr_t last) { static_cast<std::vector<std::pair<std::uintptr_t, std::uintptr_t>>*>(context)->emplace_back(first, last); }, &zero);
auto cursor = begin;
for (const auto& [first, last] : zero) {
if (first > cursor) maybe.push_back({cursor, first, true});
cursor = std::max<std::uint64_t>(cursor, last);
}
if (cursor < end) maybe.push_back({cursor, end, true});
maybe.push_back({begin, end, true});
#else
for (const auto& mapping : mappings(begin, end)) {
int file = -1;
@@ -321,14 +314,7 @@ std::vector<Candidate> possiblyNonzero(std::uint64_t begin, std::uint64_t end) {
}
bool GuestBytesZero(std::uint64_t begin, std::uint64_t end) {
#ifdef _WIN32
for (const auto& candidate : possiblyNonzero(begin, end)) {
if (!allZero(candidate.begin, candidate.end)) return false;
}
return true;
#else
return allZero(begin, end);
#endif
}
GuestRuns GuestNonzeroBlocks(const GuestRuns& candidates) {
@@ -3120,7 +3120,6 @@ void StorageTexture::upload(const std::vector<bool>* layers) {
const auto elementBytes = BytesPerElement(descriptor.format);
const auto linearBytes = sliceLinearBytes * arrayLayers;
if (layers != nullptr && ((!blockUnits && trackedLayers != arrayLayers) || std::all_of(layers->begin(), layers->end(), [](bool selected) { return selected; }))) layers = nullptr;
GuestMemory::MaterializeKnownZero(descriptor.baseAddress, static_cast<std::size_t>(guestBytes));
auto now = GuestMemory::CollectWrites(descriptor.baseAddress, static_cast<std::size_t>(guestBytes));
const auto stampLayers = [&](bool selectedOnly) {
const bool whole = !selectedOnly || layers == nullptr;
@@ -4358,7 +4357,6 @@ std::uint64_t StorageTexture::OccasionalLabelStores() {
}
void StorageTexture::markLayersPending(std::uint32_t first, std::uint32_t count) {
if (count != 0) GuestMemory::ForgetKnownZero(layerBegin(first), static_cast<std::size_t>(layerBegin(first + count - 1) + layerBytes(first + count - 1) - layerBegin(first)));
{
auto& records = LayoutWrites();
std::lock_guard lock(records.mutex);
@@ -313,19 +313,6 @@ void StagingChunkTests(AgcDriver::VulkanDevice& device) {
}
}
void StorageImageZeroStoreTests(AgcDriver::VulkanDevice& device) {
GuestHeap heap(true);
GuestHeap written(true);
const OutputBlock output;
const ImportBudget budget(AgcDriver::Graphics::HostImportLiveBytes() + Block);
Dispatch(device, heap, written, output);
StoreImage(device, heap.Address() + UnreadBlock);
heap.Store(UnreadBlock, 0u);
std::uint32_t texel = 1u;
AgcDriver::GuestMemory::Read(heap.Address() + UnreadBlock, std::as_writable_bytes(std::span(&texel, 1)));
Require(texel == 0u, "resident heap mirror: a CPU store of zero after an image store into a never-written block read " + Hex(texel));
}
void StorageImageTests(AgcDriver::VulkanDevice& device) {
GuestHeap heap(true);
GuestHeap written(true);
@@ -618,7 +605,6 @@ int main() {
CopiedTests(*device);
BudgetEvictionTests(*device, sparse);
StorageImageTests(*device);
StorageImageZeroStoreTests(*device);
for (const bool copied : {false, true}) {
if (!copied && !sparse) continue;
for (const bool readFirst : {false, true}) PartialImportTests(*device, copied, readFirst);
@@ -73,8 +73,6 @@ void GuestArenaCompareWhenShared_nid_no_patch(std::uintptr_t address, std::size_
void GuestArenaCompareResidentWhenShared_nid_no_patch(std::uintptr_t address, std::size_t bytes);
void GuestArenaComparedPages_nid_no_patch(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count);
void GuestArenaTakeUnreportedPages_nid_no_patch(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count);
void GuestArenaKnownZero_nid_no_patch(std::uintptr_t address, std::size_t bytes, void (*zero)(void* context, std::uintptr_t begin, std::uintptr_t end), void* context);
void GuestArenaForgetKnownZero_nid_no_patch(std::uintptr_t address, std::size_t bytes);
void GuestArenaForgetSection_nid_postfix(void* section);
bool GuestArenaBeginHostImport_nid_no_patch(void* pointer, std::size_t bytes);
void GuestArenaEndHostImport_nid_no_patch(void* pointer, std::size_t bytes);
+7 -204
View File
@@ -16,7 +16,6 @@
#include <mutex>
#include <map>
#include <memory>
#include <unordered_map>
#include <vector>
#include <stdexcept>
#include <string>
@@ -56,7 +55,6 @@ public:
const auto replace = [&](std::uintptr_t at, std::size_t size) {
if (!allocate(GetCurrentProcess(), reinterpret_cast<void*>(at), size, flags, protection, nullptr, 0)) fail("replace guest placeholder with private memory");
privateAllocations.emplace(at, at + size);
if (watched) markZero(at, at + size);
if (!created.empty() && created.back().first + created.back().second == at) created.back().second += size;
else created.emplace_back(at, size);
};
@@ -175,10 +173,7 @@ public:
bool compared = false;
if (const auto found = physical.find(std::make_pair(key, offset + done + at)); found != physical.end()) {
const auto& slot = *found->second;
if (slot.saved) {
std::memcpy(reinterpret_cast<void*>(base + at), slot.saved->bytes + slot.savedOffset, pageBytes);
forgetZero(base + at, base + at + pageBytes);
}
if (slot.saved) std::memcpy(reinterpret_cast<void*>(base + at), slot.saved->bytes + slot.savedOffset, pageBytes);
compared = slot.comparedWhenShared;
physical.erase(found);
}
@@ -378,7 +373,6 @@ public:
if (!writable(it->second.protection)) return false;
}
disown(address, end);
forgetZero(address, end);
ownedHostAccess.emplace(address, end);
for (auto it = first; it != views.end() && it->first < end; ++it) {
auto& view = it->second;
@@ -540,7 +534,7 @@ public:
std::array<void*, ownedBlockBytes / 4096> written{};
ULONG_PTR reported = written.size();
DWORD granularity = 0;
if (watchWrites(WRITE_WATCH_FLAG_RESET, reinterpret_cast<void*>(block), ownedBlockBytes, written.data(), &reported, &granularity) != 0) return false;
if (GetWriteWatch(WRITE_WATCH_FLAG_RESET, reinterpret_cast<void*>(block), ownedBlockBytes, written.data(), &reported, &granularity) != 0) return false;
for (ULONG_PTR index = 0; index < reported; ++index) pending |= std::uint16_t{1} << ((reinterpret_cast<std::uintptr_t>(written[index]) - block) / 4096);
if (pending == 0) ownedPending.erase(block);
}
@@ -562,7 +556,7 @@ public:
std::array<void*, ownedBlockBytes / 4096> written{};
ULONG_PTR reported = written.size();
DWORD granularity = 0;
if (watchWrites(0, reinterpret_cast<void*>(block), ownedBlockBytes, written.data(), &reported, &granularity) != 0 || reported != 0) {
if (GetWriteWatch(0, reinterpret_cast<void*>(block), ownedBlockBytes, written.data(), &reported, &granularity) != 0 || reported != 0) {
ownedWords.erase(found);
return false;
}
@@ -570,66 +564,6 @@ public:
return true;
}
void ForgetKnownZero(std::uintptr_t address, std::size_t bytes) {
if (bytes == 0 || bytes > UINTPTR_MAX - address) return;
std::lock_guard lock(mutex);
forgetZero(address, address + bytes);
}
void KnownZero(std::uintptr_t address, std::size_t bytes, void (*zero)(void* context, std::uintptr_t begin, std::uintptr_t end), void* context) {
if (bytes == 0 || bytes > UINTPTR_MAX - address) return;
const auto end = address + bytes;
std::uintptr_t reportStart = 0;
std::uintptr_t reportEnd = 0;
std::vector<std::pair<std::uintptr_t, std::uintptr_t>> found;
const auto report = [&](std::uintptr_t from, std::uintptr_t to) {
from = std::max(from, address);
to = std::min(to, end);
if (from >= to) return;
if (!found.empty() && found.back().second == from) found.back().second = to;
else found.emplace_back(from, to);
};
const auto deliver = [&] {
for (const auto& [from, to] : found) {
if (reportEnd == from) {
reportEnd = to;
continue;
}
if (reportEnd > reportStart) zero(context, reportStart, reportEnd);
reportStart = from;
reportEnd = to;
}
found.clear();
};
for (auto cursor = address & ~std::uintptr_t{4095}; cursor < end;) {
const auto chunk = cursor & ~(chunkBytes - 1);
const auto chunkEnd = std::min(end, chunk + chunkBytes);
std::unique_lock lock(mutex);
if (zeroPages.empty()) break;
if (zeroPages.find(chunk) == zeroPages.end()) {
cursor = chunkEnd;
continue;
}
while (cursor < chunkEnd) {
const auto limit = zeroSpanEnd(cursor);
const auto stop = limit == 0 ? std::min(chunkEnd, (cursor & ~(pageBytes - 1)) + pageBytes) : std::min(chunkEnd, limit);
if (limit == 0) forgetZero(cursor, stop);
else harvestZero(cursor, stop);
if (const auto bits = zeroPages.find(chunk); bits != zeroPages.end()) {
for (auto page = cursor; page < stop; page += 4096) {
const auto bit = (page - chunk) >> 12;
if ((bits->second[bit >> 6] >> (bit & 63) & 1) != 0) report(page, page + 4096);
}
}
cursor = stop;
}
lock.unlock();
deliver();
}
deliver();
if (reportEnd > reportStart) zero(context, reportStart, reportEnd);
}
bool Collect(std::uintptr_t address, std::size_t bytes, void** pages, std::size_t* count, bool clear, Collection collection = Collection::All) {
std::lock_guard lock(mutex);
const auto capacity = *count;
@@ -841,7 +775,7 @@ private:
std::array<void*, ownedBlockBytes / 4096> written{};
ULONG_PTR reported = written.size();
DWORD granularity = 0;
return watchWrites(0, reinterpret_cast<void*>(block), ownedBlockBytes, written.data(), &reported, &granularity) == 0;
return GetWriteWatch(0, reinterpret_cast<void*>(block), ownedBlockBytes, written.data(), &reported, &granularity) == 0;
}
void forgetOwnedPending(std::uintptr_t first, std::uintptr_t end) {
@@ -855,135 +789,9 @@ private:
}
}
void markZero(std::uintptr_t start, std::uintptr_t end) {
start = (start + 4095) & ~std::uintptr_t{4095};
end &= ~std::uintptr_t{4095};
while (start < end) {
const auto chunk = start & ~(chunkBytes - 1);
const auto stop = std::min(end, chunk + chunkBytes);
auto& bits = zeroPages[chunk];
for (auto bit = (start - chunk) >> 12; bit < (stop - chunk) >> 12; ++bit) bits[bit >> 6] |= std::uint64_t{1} << (bit & 63);
start = stop;
}
}
static void clearZeroBits(auto& bits, std::uintptr_t chunk, std::uintptr_t from, std::uintptr_t to) {
for (auto bit = (from - chunk) >> 12; bit < (to - chunk) >> 12; ++bit) bits[bit >> 6] &= ~(std::uint64_t{1} << (bit & 63));
}
static bool noZeroBits(const auto& bits) {
return std::all_of(bits.begin(), bits.end(), [](std::uint64_t word) { return word == 0; });
}
void forgetZero(std::uintptr_t start, std::uintptr_t end) {
if (zeroPages.empty() || end <= start) return;
start &= ~std::uintptr_t{4095};
end = (end + 4095) & ~std::uintptr_t{4095};
if ((end - start) / chunkBytes > zeroPages.size()) {
for (auto it = zeroPages.begin(); it != zeroPages.end();) {
const auto chunk = it->first;
if (chunk + chunkBytes <= start || chunk >= end) {
++it;
continue;
}
clearZeroBits(it->second, chunk, std::max(start, chunk), std::min(end, chunk + chunkBytes));
if (noZeroBits(it->second)) it = zeroPages.erase(it);
else ++it;
}
return;
}
while (start < end) {
const auto chunk = start & ~(chunkBytes - 1);
const auto stop = std::min(end, chunk + chunkBytes);
if (const auto found = zeroPages.find(chunk); found != zeroPages.end()) {
clearZeroBits(found->second, chunk, start, stop);
if (noZeroBits(found->second)) zeroPages.erase(found);
}
start = stop;
}
}
bool anyZero(std::uintptr_t start, std::uintptr_t end) const {
if (zeroPages.empty()) return false;
start &= ~std::uintptr_t{4095};
end = (end + 4095) & ~std::uintptr_t{4095};
while (start < end) {
const auto chunk = start & ~(chunkBytes - 1);
const auto stop = std::min(end, chunk + chunkBytes);
if (const auto found = zeroPages.find(chunk); found != zeroPages.end()) {
for (auto bit = (start - chunk) >> 12; bit < (stop - chunk) >> 12; ++bit) {
if ((found->second[bit >> 6] >> (bit & 63) & 1) != 0) return true;
}
}
start = stop;
}
return false;
}
bool stillZero(std::uintptr_t page) const {
const auto found = zeroPages.find(page & ~(chunkBytes - 1));
if (found == zeroPages.end()) return false;
const auto bit = (page & (chunkBytes - 1)) >> 12;
if ((found->second[bit >> 6] >> (bit & 63) & 1) == 0) return false;
thread_local std::array<std::uint64_t, 512> bytes{};
SIZE_T read = 0;
if (!ReadProcessMemory(GetCurrentProcess(), reinterpret_cast<const void*>(page), bytes.data(), sizeof(bytes), &read) || read != sizeof(bytes)) return false;
return std::all_of(bytes.begin(), bytes.end(), [](std::uint64_t word) { return word == 0; });
}
UINT watchWrites(DWORD flags, void* address, std::size_t bytes, void** pages, ULONG_PTR* count, DWORD* granularity) {
const auto capacity = *count;
const auto result = GetWriteWatch(flags, address, bytes, pages, count, granularity);
if (result != 0 || zeroPages.empty()) return result;
const auto first = reinterpret_cast<std::uintptr_t>(address) & ~std::uintptr_t{4095};
const auto last = (reinterpret_cast<std::uintptr_t>(address) + bytes + 4095) & ~std::uintptr_t{4095};
const bool complete = *count < capacity || capacity >= (last - first) / 4096;
ULONG_PTR kept = 0;
for (ULONG_PTR index = 0; index < *count; ++index) {
const auto page = reinterpret_cast<std::uintptr_t>(pages[index]) & ~std::uintptr_t{4095};
if (complete && stillZero(page)) continue;
forgetZero(page, page + 4096);
pages[kept++] = pages[index];
}
*count = kept;
return result;
}
void harvestZero(std::uintptr_t start, std::uintptr_t end) {
start &= ~std::uintptr_t{4095};
end = (end + 4095) & ~std::uintptr_t{4095};
if (!anyZero(start, end)) return;
std::vector<void*> written((end - start) / 4096);
ULONG_PTR reported = written.size();
DWORD granularity = 0;
if (watchWrites(0, reinterpret_cast<void*>(start), end - start, written.data(), &reported, &granularity) != 0 || reported == written.size()) forgetZero(start, end);
}
bool resetWrites(std::uintptr_t start, std::uintptr_t end) {
if (!anyZero(start, end)) return ResetWriteWatch(reinterpret_cast<void*>(start), end - start) == 0;
std::vector<void*> written(((end + 4095) & ~std::uintptr_t{4095}) / 4096 - (start & ~std::uintptr_t{4095}) / 4096);
ULONG_PTR reported = written.size();
DWORD granularity = 0;
if (watchWrites(WRITE_WATCH_FLAG_RESET, reinterpret_cast<void*>(start), end - start, written.data(), &reported, &granularity) != 0) {
forgetZero(start, end);
return false;
}
return true;
}
std::uintptr_t zeroSpanEnd(std::uintptr_t at) {
const auto run = residentRuns.upper_bound(at);
if (run != residentRuns.begin() && std::prev(run)->second.end > at) {
const auto& resident = std::prev(run)->second;
return resident.watched && !resident.imported ? resident.end : 0;
}
if (views.find(at & ~(pageBytes - 1)) != views.end()) return 0;
return privateAllocationEnd(at);
}
UINT collectOwnedWrites(DWORD flags, void* address, std::size_t bytes, void** pages, ULONG_PTR* count, DWORD* granularity) {
const auto capacity = *count;
const auto result = watchWrites(flags, address, bytes, pages, count, granularity);
const auto result = GetWriteWatch(flags, address, bytes, pages, count, granularity);
if (result != 0 || ownedPending.empty()) return result;
std::sort(pages, pages + *count);
const auto reported = *count;
@@ -1162,7 +970,7 @@ private:
std::uintptr_t resetFrom = 0;
const auto flushReset = [&](std::uintptr_t to) {
if (resetFrom != 0) forgetOwnedPending(resetFrom, to);
if (resetFrom != 0 && !resetWrites(resetFrom, to)) fail("reset resident guest writes");
if (resetFrom != 0 && ResetWriteWatch(reinterpret_cast<void*>(resetFrom), to - resetFrom) != 0) fail("reset resident guest writes");
resetFrom = 0;
};
for (auto at = first; at < stop; at += 4096) {
@@ -1203,7 +1011,6 @@ private:
void commitResident(std::uintptr_t base, std::size_t bytes) {
const DWORD flags = MEM_RESERVE | MEM_COMMIT | MEM_REPLACE_PLACEHOLDER | (watchResident ? MEM_WRITE_WATCH : 0);
if (!allocate(GetCurrentProcess(), reinterpret_cast<void*>(base), bytes, flags, PAGE_READWRITE, nullptr, 0)) fail("replace guest placeholder with resident direct memory");
if (watchResident) markZero(base, base + bytes);
}
static std::size_t hostPages(std::size_t bytes) {
@@ -1319,7 +1126,6 @@ private:
const auto stop = run->second.end;
disown(start, stop);
forgetOwnedPending(start, stop);
forgetZero(start, stop);
const auto entry = indexEntry(run);
const bool indexed = entry != residentIndex.end();
std::vector<std::pair<std::shared_ptr<SharedPage>, std::shared_ptr<Physical>>> created;
@@ -1382,7 +1188,6 @@ private:
const auto stop = run->second.end;
disown(start, stop);
forgetOwnedPending(start, stop);
forgetZero(start, stop);
DWORD previous;
if (!VirtualProtect(reinterpret_cast<void*>(start), stop - start, PAGE_READONLY, &previous)) fail("open resident guest pages");
const auto entry = indexEntry(run);
@@ -1394,7 +1199,7 @@ private:
dirty.resize((stop - start) / subpageBytes);
ULONG_PTR reported = dirty.size();
DWORD granularity = 0;
if (watchWrites(0, reinterpret_cast<void*>(start), stop - start, dirty.data(), &reported, &granularity) == 0) {
if (GetWriteWatch(0, reinterpret_cast<void*>(start), stop - start, dirty.data(), &reported, &granularity) == 0) {
touched = TouchedPages(start, (stop - start) / pageBytes, dirty.data(), reported);
filtered = true;
}
@@ -1572,7 +1377,6 @@ private:
void reset(std::uintptr_t address, std::size_t bytes) {
disown(address, address + bytes);
forgetOwnedPending(address, address + bytes);
forgetZero(address, address + bytes);
collectableRanges.clear();
const auto end = address + bytes;
privateAllocations.erase(privateAllocations.lower_bound(address), privateAllocations.lower_bound(end));
@@ -1682,7 +1486,6 @@ private:
std::uint64_t collectPieces = 0;
#endif
Ranges freshRanges;
std::unordered_map<std::uintptr_t, std::array<std::uint64_t, chunkBytes / 4096 / 64>> zeroPages;
std::map<std::uintptr_t, View> views;
using Views = std::map<std::uintptr_t, View>;
-8
View File
@@ -799,14 +799,6 @@ void GuestArenaTakeUnreportedPages_nid_no_patch(std::uintptr_t address, std::siz
WindowsMappings::Get().ComparedPages(address, bytes, pages, count, true);
}
void GuestArenaKnownZero_nid_no_patch(std::uintptr_t address, std::size_t bytes, void (*zero)(void* context, std::uintptr_t begin, std::uintptr_t end), void* context) {
WindowsMappings::Get().KnownZero(address, bytes, zero, context);
}
void GuestArenaForgetKnownZero_nid_no_patch(std::uintptr_t address, std::size_t bytes) {
WindowsMappings::Get().ForgetKnownZero(address, bytes);
}
void GuestArenaForgetSection_nid_postfix(void* section) {
WindowsMappings::Get().ForgetSection(section);
}
-114
View File
@@ -921,116 +921,6 @@ void CheckCommitResetsEveryPageOfARunView() {
}
}
std::set<std::size_t> KnownZeroPages(const void* base, std::size_t bytes) {
struct Found {
std::set<std::size_t> pages;
std::uintptr_t base;
} found{{}, reinterpret_cast<std::uintptr_t>(base)};
Mappings::Get().KnownZero(found.base, bytes, [](void* context, std::uintptr_t begin, std::uintptr_t end) {
auto& result = *static_cast<Found*>(context);
for (auto at = begin; at < end; at += Small) result.pages.insert((at - result.base) / Small);
}, &found);
return found.pages;
}
std::set<std::size_t> PagesBelow(std::size_t count, std::set<std::size_t> without = {}) {
std::set<std::size_t> pages;
for (std::size_t page = 0; page < count; ++page) {
if (!without.contains(page)) pages.insert(page);
}
return pages;
}
void CheckKnownZeroPrivate() {
auto& mappings = Mappings::Get();
auto* memory = static_cast<unsigned char*>(mappings.Reserve(nullptr, Bytes));
Require(memory != nullptr, "known zero private reservation failed");
mappings.Commit(memory, Bytes, PAGE_READWRITE, Bytes, true);
const auto address = reinterpret_cast<std::uintptr_t>(memory);
const auto pages = Bytes / Small;
Require(KnownZeroPages(memory, Bytes) == PagesBelow(pages), "a fresh watched commit is not known zero");
Require(KnownZeroPages(memory + 3 * Small, 2 * Small) == std::set<std::size_t>{0, 1}, "a known zero query is not clipped to its range");
static_cast<volatile unsigned char*>(memory)[Small] = 1;
Require(KnownZeroPages(memory, Bytes) == PagesBelow(pages, {1}), "a written private page stayed known zero");
static_cast<volatile unsigned char*>(memory)[5 * Small] = 2;
std::array<void*, 32> written{};
std::size_t count = written.size();
Require(mappings.Collect(address, Bytes, written.data(), &count, true), "known zero private collect failed");
Require(KnownZeroPages(memory, Bytes) == PagesBelow(pages, {1, 5}), "a collect that reset the write watch lost a written page");
static_cast<volatile unsigned char*>(memory)[7 * Small] = 3;
count = written.size();
Require(mappings.Collect(address, Bytes, written.data(), &count, true, Collection::Fresh), "known zero fresh collect failed");
count = written.size();
Require(mappings.Collect(address, Bytes, written.data(), &count, true, Collection::Armed), "known zero armed collect failed");
Require(KnownZeroPages(memory, Bytes) == PagesBelow(pages, {1, 5, 7}), "a fresh or armed collect lost a written page");
Require(mappings.BeginHostWrite(address + 9 * Small, Small), "known zero host write failed");
mappings.EndHostWrite(address + 9 * Small, Small);
Require(KnownZeroPages(memory, Bytes) == PagesBelow(pages, {1, 5, 7, 9}), "a host write window stayed known zero");
mappings.Reset(memory, Bytes);
Require(KnownZeroPages(memory, Bytes).empty(), "a reset range stayed known zero");
VirtualFree(memory, 0, MEM_RELEASE);
auto* unwatched = static_cast<unsigned char*>(mappings.Reserve(nullptr, Bytes));
Require(unwatched != nullptr, "known zero unwatched reservation failed");
mappings.Commit(unwatched, Bytes, PAGE_READWRITE, Bytes, false);
Require(KnownZeroPages(unwatched, Bytes).empty(), "memory without a write watch is known zero");
mappings.Reset(unwatched, Bytes);
VirtualFree(unwatched, 0, MEM_RELEASE);
}
void CheckKnownZeroResident() {
constexpr std::size_t Large = 0x40000;
auto& mappings = Mappings::Get();
HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE | SEC_RESERVE, 0, Large, nullptr);
auto* first = static_cast<unsigned char*>(mappings.Reserve(nullptr, Large));
Require(section && first, "cannot allocate known zero resident test memory");
mappings.ForgetSection(section);
mappings.Map(first, Large, section, 0, PAGE_READWRITE, true, true);
const auto address = reinterpret_cast<std::uintptr_t>(first);
const auto pages = Large / Small;
Require(KnownZeroPages(first, Large) == PagesBelow(pages), "a fresh resident run is not known zero");
static_cast<volatile unsigned char*>(first)[2 * Small] = 7;
std::array<void*, 64> written{};
std::size_t count = written.size();
Require(mappings.Collect(address, Large, written.data(), &count, true), "known zero resident collect failed");
Require(KnownZeroPages(first, Large) == PagesBelow(pages, {2}), "a written resident page stayed known zero");
Require(mappings.BeginHostWrite(address + 8 * Small, 4 * Small, false), "known zero host import failed");
mappings.EndHostWrite(address + 8 * Small, 4 * Small, false);
for (const auto page : KnownZeroPages(first, Large)) Require(page < 8 || page >= 12, "a host import window stayed known zero");
mappings.Reset(first, Large);
Require(KnownZeroPages(first, Large).empty(), "an evicted resident run stayed known zero");
mappings.Map(first, Large, section, 0, PAGE_READWRITE, true, true);
Require(!KnownZeroPages(first, Large).contains(2), "a resident page restored with its saved bytes is known zero");
Require(static_cast<volatile unsigned char*>(first)[2 * Small] == 7, "a remapped resident run lost its saved bytes");
mappings.Reset(first, Large);
mappings.ForgetSection(section);
VirtualFree(first, 0, MEM_RELEASE);
CloseHandle(section);
}
void CheckKnownZeroFirstReads() {
auto& mappings = Mappings::Get();
auto* memory = static_cast<unsigned char*>(mappings.Reserve(nullptr, Bytes));
Require(memory != nullptr, "known zero first-read reservation failed");
mappings.Commit(memory, Bytes, PAGE_READWRITE, Bytes, true);
const auto address = reinterpret_cast<std::uintptr_t>(memory);
static_cast<void>(static_cast<volatile unsigned char*>(memory)[2 * Small]);
static_cast<volatile unsigned char*>(memory)[4 * Small] = 0;
static_cast<volatile unsigned char*>(memory)[6 * Small] = 7;
std::array<void*, 32> written{};
std::size_t count = 1;
Require(mappings.Collect(address + 2 * Small, Small, written.data(), &count, true) && count == 0, "the first read of a never-written page was collected as a write");
count = written.size();
Require(mappings.Collect(address, Bytes, written.data(), &count, true) && count == 1 && written[0] == memory + 6 * Small, "a zero store into a never-written page was collected as a write, or a real store was lost");
Require(KnownZeroPages(memory, Bytes) == PagesBelow(Bytes / Small, {6}), "a read or a zero store lost a page's known-zero state");
mappings.Reset(memory, Bytes);
VirtualFree(memory, 0, MEM_RELEASE);
}
void CheckKnownZeroShared() {
Region region;
Require(KnownZeroPages(region.first, Bytes).empty(), "shared memory is known zero");
}
int main(int argc, char** argv) {
auto* handler = AddVectoredExceptionHandler(1, HandleWrite);
try {
@@ -1066,10 +956,6 @@ int main(int argc, char** argv) {
CheckPrivateCommitEdgeLayouts();
CheckPrivateCommitTails();
CheckCommitResetsEveryPageOfARunView();
CheckKnownZeroPrivate();
CheckKnownZeroResident();
CheckKnownZeroShared();
CheckKnownZeroFirstReads();
std::cout << "Windows deferred write-watch tests passed\n";
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';