fix(agc): snapshot misaligned draw inputs from the offset the shader reads

A guest buffer view off minStorageBufferOffsetAlignment is bound from the
aligned offset below it, and the shader adds the difference to every
access (3dec6ced). PrepareDrawBindings (9f1c680b) copied such a read-only
in-place view into a snapshot that starts at the view itself, so the
shader read the snapshot that many bytes past the view.

The snapshot now covers the view from the binding's start.
This commit is contained in:
Dean Galvin
2026-10-01 16:05:24 +13:00
parent a67b644ff6
commit f5355d917a
2 changed files with 89 additions and 4 deletions
@@ -2517,11 +2517,13 @@ std::shared_ptr<ShaderResources::DrawBindings> ShaderResources::PrepareDrawBindi
if (!item.guest || item.written || guestMemory.WritesOverlap(item.address, item.size)) continue;
const bool direct = std::any_of(reads.begin(), reads.end(), [&](const auto& range) { return item.address >= range.first && item.address < range.second && item.size <= range.second - item.address; });
if (!direct || recorder.PendingWriteOverlaps(item.address, item.size)) continue;
auto buffer = std::make_shared<Buffer>(context, item.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
std::memcpy(buffer->Bytes().data(), reinterpret_cast<const void*>(item.address), item.size);
const auto begin = item.address - item.adjustment;
const auto bytes = item.size + item.adjustment;
auto buffer = std::make_shared<Buffer>(context, bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
std::memcpy(buffer->Bytes().data(), reinterpret_cast<const void*>(begin), bytes);
selected.push_back(index);
result->snapshots.push_back({item.address, std::move(buffer)});
CaptureTrace::Log("draw-snapshot batch=%llu address=%llx bytes=%zu", static_cast<unsigned long long>(recorder.Submissions() + 1), static_cast<unsigned long long>(item.address), item.size);
result->snapshots.push_back({begin, std::move(buffer)});
CaptureTrace::Log("draw-snapshot batch=%llu address=%llx bytes=%zu", static_cast<unsigned long long>(recorder.Submissions() + 1), static_cast<unsigned long long>(begin), bytes);
}
if (selected.empty()) return {};
Require(context.descriptorCache != nullptr, "draw snapshots require a descriptor cache");
@@ -810,6 +810,88 @@ void resourceReadTests(const Device& device, Recorder& recorder) {
HostImportFor(context, address, bytes);
}
void misalignedSnapshotTests(const Device& device, Recorder& recorder) {
const auto& context = device.GetContext();
const auto alignment = context.limits.minStorageBufferOffsetAlignment;
if (context.hostImportAlignment == 0) {
std::cout << "host imports unavailable: misaligned draw snapshots not tested\n";
return;
}
if (alignment < 8 || alignment > 256) {
std::cout << "storage buffer offset alignment " << alignment << ": misaligned draw snapshots not tested\n";
return;
}
constexpr std::size_t bytes = 65536;
#ifdef _WIN32
void* block = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
void* block = std::aligned_alloc(65536, bytes);
#endif
Require(block != nullptr, "cannot allocate the misaligned snapshot block");
auto* guest = static_cast<std::uint8_t*>(block);
for (std::size_t at = 0; at < bytes; ++at) guest[at] = static_cast<std::uint8_t>(at * 7u + 3u);
const auto address = reinterpret_cast<std::uint64_t>(block);
{
GuestAllocations::Mutation mutation;
mutation.Add(block, bytes, true, true);
}
struct Unregister {
const Context& context;
void* block;
~Unregister() {
{
GuestAllocations::Mutation mutation;
mutation.Remove(block);
}
HostImportFor(context, reinterpret_cast<std::uint64_t>(block), bytes);
}
} unregister{context, block};
if (HostImportFor(context, address, bytes) == nullptr) {
std::cout << "host import of the test block refused: misaligned draw snapshots not tested\n";
return;
}
constexpr std::uint32_t outer = 4096;
constexpr std::uint32_t offset = outer + 4;
constexpr std::size_t outerBytes = 128;
constexpr std::size_t elementBytes = 64;
const auto words = [](std::uint64_t at, std::size_t size) { return std::array<std::uint32_t, 4>{static_cast<std::uint32_t>(at), static_cast<std::uint32_t>(at >> 32u) & 0xffffu, static_cast<std::uint32_t>(size), 0x31000000u}; };
ShaderRecompiler::RecompileResult program;
ShaderRecompiler::DescriptorBinding binding;
binding.kind = ShaderRecompiler::DescriptorKind::StorageBuffer;
binding.role = ShaderRecompiler::DescriptorRole::GuestBuffers;
binding.descriptorSet = 0;
binding.binding = 0;
binding.count = 2;
for (const auto word : words(address + outer, outerBytes)) binding.guestDescriptor.push_back(word);
for (const auto word : words(address + offset, elementBytes)) binding.guestDescriptor.push_back(word);
binding.bufferWritten = {false, false};
program.bindings.push_back(binding);
program.pushConstants.resize(16);
program.memoryOffsetDword = 0;
const CompiledShader compute{ShaderRecompiler::ShaderStage::Compute, &program, 0};
{
auto snapshotContext = context;
DescriptorCache cache(snapshotContext);
snapshotContext.descriptorCache = &cache;
Recorder snapshotRecorder(snapshotContext);
snapshotRecorder.Activate();
ShaderResources resources(snapshotContext, compute);
std::array<std::byte, PipelinePushConstantBytes> push{};
resources.PatchPushConstants(push);
const auto adjustment = std::to_integer<std::uint32_t>(push[1]);
Require(push[0] == std::byte{0} && adjustment == offset % alignment, "the inner view's shader offset is not its distance from the binding");
const auto bindings = resources.PrepareDrawBindings(snapshotRecorder);
Require(bindings != nullptr && bindings->snapshots.size() == 2, "read-only draw inputs were not snapshotted");
const auto outerContents = bindings->snapshots[0].buffer->Bytes();
Require(outerContents.size() >= outerBytes && std::memcmp(outerContents.data(), guest + outer, outerBytes) == 0, "an aligned draw snapshot misses its view's bytes");
const auto contents = bindings->snapshots[1].buffer->Bytes();
Require(contents.size() >= adjustment + elementBytes, "a draw snapshot ends before the bytes the shader reads");
Require(std::memcmp(contents.data() + adjustment, guest + offset, elementBytes) == 0, "the shader's offset into a draw snapshot misses the view's bytes");
snapshotRecorder.Sync();
}
recorder.Activate();
}
// Unit shadows (UnitShadow.hpp) over a host import of write-watched arena memory: a retile piece's
// slab destination and its seeds, freshness from the tracker (a publish never stamps, a CPU write
// makes the unit stale), the scopes, the slab boundary, and the retire publish. With
@@ -1571,6 +1653,7 @@ int main() {
closeRaceTests(device, recorder);
keyProofTests(device, recorder);
resourceReadTests(device, recorder);
misalignedSnapshotTests(device, recorder);
storeRunTests(device, recorder);
movedMetadataTests(device, recorder);
unitShadowTests(device, recorder);