mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(agc): implement deferred faults for descriptors read through null SRT pointers
Capturing a shader evaluates every V#, T# and S# the program uses, including those behind a branch the wave does not take. When such a descriptor was loaded through a pointer read from memory that is null or unmapped, the capture threw "guest memory is not readable", while the console faults only if a wave executes that s_load. The descriptor now reads as zero words, and the flat SRT walk records the s_load's dwords as it does for flat reads, so their guarded ReadConst raises the BDA fault where the wave runs the s_load. A zero V# is an absent buffer and a zero T# binds a null descriptor, as for any zero word read from memory, so nothing new enters a key. A zeroed descriptor whose read no flat slot records still throws, as do branch conditions, uniform fills, table bases and user-data pointers.
This commit is contained in:
@@ -1933,10 +1933,74 @@ void verifyGuardedNullPointers() {
|
||||
require(chainResult->poisonedSrtReads == 3u && nullChainResult->poisonedSrtReads == 1u && mappedChainResult->poisonedSrtReads == 0u, "guarded pointer: a chain result does not count its poisoned reads");
|
||||
require(samePipeline(*chainResult, *mappedChainResult) && samePipeline(*nullChainResult, *mappedChainResult), "guarded pointer: a chain's poison changed the variant, the specialization or the module");
|
||||
|
||||
const auto poisonedWords = [](const ResourceCapture& capture, std::uint32_t pc, std::uint64_t base) {
|
||||
std::vector<std::uint64_t> addresses;
|
||||
for (const auto& entry : capture.snapshot.srtPoison) {
|
||||
if (entry.pc != pc || capture.snapshot.flattenedSrt.at(entry.slot) != 0u) return false;
|
||||
addresses.push_back(entry.address);
|
||||
}
|
||||
std::sort(addresses.begin(), addresses.end());
|
||||
return addresses == std::vector<std::uint64_t>{base, base + 4u, base + 8u, base + 12u} && GuardRecords(capture);
|
||||
};
|
||||
const auto zeroBuffer = [](const ResourceCapture& capture) {
|
||||
return capture.snapshot.buffers.size() == 1u && std::all_of(capture.snapshot.buffers[0].dwords.begin(), capture.snapshot.buffers[0].dwords.end(), [](std::uint32_t word) { return word == 0u; });
|
||||
};
|
||||
alignas(256) static std::array<std::uint32_t, 4> zeroTable{};
|
||||
NestedRequest descriptor(descriptorCode, root.data());
|
||||
point(0u);
|
||||
AgcDriver::ShaderMemory descriptorMemory({});
|
||||
expectFailure([&] { static_cast<void>(descriptorMemory.Capture(descriptor.request)); }, "null or misaligned address", "guarded pointer: a V# loaded through a null nested pointer was accepted");
|
||||
point(static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(zeroTable.data())));
|
||||
AgcDriver::ShaderMemory zeroMemory({});
|
||||
const auto zeroCapture = zeroMemory.Capture(descriptor.request);
|
||||
require(zeroCapture->snapshot.srtPoison.empty() && zeroBuffer(*zeroCapture) && GuardRecords(*zeroCapture), "guarded pointer: a zero V# read from mapped memory was poisoned or not read");
|
||||
descriptor.request.context.memory = zeroMemory.Regions();
|
||||
const auto zeroResult = Recompile(descriptor.request, *zeroCapture);
|
||||
for (const auto pointer : {std::uint64_t{0}, unmapped}) {
|
||||
point(pointer);
|
||||
descriptor.request.context.memory = {};
|
||||
AgcDriver::ShaderMemory memory({});
|
||||
const auto capture = memory.Capture(descriptor.request);
|
||||
require(poisonedWords(*capture, 0xcu, pointer), "guarded pointer: a V# loaded through a null or unmapped nested pointer did not poison its four words at its pc");
|
||||
require(zeroBuffer(*capture), "guarded pointer: a V# loaded through an inaccessible pointer is not bound as zero words");
|
||||
const auto regions = memory.Regions();
|
||||
descriptor.request.context.memory = regions;
|
||||
const auto result = Recompile(descriptor.request, *capture);
|
||||
require(result->poisonedSrtReads == 4u && faultBinding(*result) && result->bdaAbiVersion == BdaAbi::Version, "guarded pointer: a capture with a poisoned V# does not report through the fault buffer");
|
||||
require(samePipeline(*result, *zeroResult), "guarded pointer: a V# zeroed by poison and a V# read as zeros take different variants, specializations or modules");
|
||||
const auto replay = Recompile(descriptor.request);
|
||||
require(replay.poisonedSrtReads == 4u && samePipeline(replay, *result), "guarded pointer: a replay did not reproduce the poisoned V#");
|
||||
}
|
||||
|
||||
alignas(256) static std::array<std::uint32_t, 8> table{};
|
||||
alignas(256) static std::array<std::uint32_t, 4> vertexRoot{};
|
||||
table = {0u, 0u, 0u, 0u, static_cast<std::uint32_t>(payloadAddress), static_cast<std::uint32_t>((payloadAddress >> 32u) & 0xffffu), 4u, 0x30027facu};
|
||||
const std::vector<std::uint32_t> vertexDescriptorCode{0xf4000084u, 0xfa000000u, 0xf4040104u, 0xfa000008u, 0x7e020280u, 0xbf8cc07fu, 0xbf068002u, 0xbf850006u,
|
||||
0xf4080302u, 0xfa000010u, 0xbf8cc07fu, 0xe0300000u, 0x80030100u, 0xbf8c3f70u, 0xf80008cfu, 0x01010101u, 0xbf810000u};
|
||||
NestedRequest vertex(vertexDescriptorCode, vertexRoot.data());
|
||||
vertex.request.shader.stage = ShaderStage::Vertex;
|
||||
vertex.request.context.compute.reset();
|
||||
vertex.request.context.vertex = ShaderVertexStageInfo{};
|
||||
vertex.request.context.userDataBaseRegister = 8;
|
||||
const auto tableAddress = static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(table.data()));
|
||||
vertexRoot[2] = static_cast<std::uint32_t>(tableAddress);
|
||||
vertexRoot[3] = static_cast<std::uint32_t>(tableAddress >> 32u);
|
||||
AgcDriver::ShaderMemory mappedVertexMemory({});
|
||||
const auto mappedVertex = mappedVertexMemory.Capture(vertex.request);
|
||||
require(mappedVertex->snapshot.srtPoison.empty() && mappedVertex->snapshot.buffers.size() == 1u && mappedVertex->snapshot.buffers[0].dwords[0] == table[4], "guarded pointer: a vertex V# behind a mapped pointer was poisoned or not read");
|
||||
vertexRoot[2] = 0u;
|
||||
vertexRoot[3] = 0u;
|
||||
for (const auto guard : {0u, 1u}) {
|
||||
vertexRoot[0] = guard;
|
||||
vertex.request.context.memory = {};
|
||||
AgcDriver::ShaderMemory memory({});
|
||||
const auto capture = memory.Capture(vertex.request);
|
||||
require(poisonedWords(*capture, 0x20u, 0x10u), "guarded pointer: a vertex V# behind a branch and a null pointer did not poison its four words at its pc");
|
||||
const auto regions = memory.Regions();
|
||||
vertex.request.context.memory = regions;
|
||||
const auto result = Recompile(vertex.request, *capture);
|
||||
require(result->poisonedSrtReads == 4u && faultBinding(*result) && !result->spirv.empty(), "guarded pointer: a vertex capture with a poisoned V# has no fault buffer");
|
||||
#if ANYPS5_ENABLE_SPIRV_TOOLS
|
||||
static_cast<void>(ValidateAndOptimizeSpirv(result->spirv, vertex.request.target.vulkanVersion, vertex.request.target.spirvVersion));
|
||||
#endif
|
||||
}
|
||||
NestedRequest rootless(branchCode, nullptr);
|
||||
AgcDriver::ShaderMemory rootlessMemory({});
|
||||
expectFailure([&] { static_cast<void>(rootlessMemory.Capture(rootless.request)); }, "null or misaligned address", "guarded pointer: a null user-data pointer was accepted");
|
||||
|
||||
@@ -38,6 +38,7 @@ alignas(256) std::array<std::uint32_t, Lanes> Output{};
|
||||
alignas(256) std::array<std::uint32_t, 16> Root{};
|
||||
alignas(256) std::array<std::uint32_t, 4> Data{Payload, 0u, 0u, 0u};
|
||||
alignas(256) std::array<std::uint64_t, 1> Indirect{};
|
||||
alignas(256) std::array<std::uint32_t, 8> Table{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 19> BranchCode{
|
||||
0xf4080100u, 0xfa000000u, 0xf4000200u, 0xfa000010u, 0xf4040280u, 0xfa000018u, 0xbe8c03ffu, 0x11111111u,
|
||||
@@ -63,6 +64,12 @@ alignas(256) constexpr std::array<std::uint32_t, 22> ChainCode{
|
||||
0xbf8cc07fu, 0x7e02020cu, 0x34040082u, 0xe0701000u, 0x80010102u, 0xbf810000u,
|
||||
};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 21> DescriptorCode{
|
||||
0xf4080100u, 0xfa000000u, 0xf4000200u, 0xfa000010u, 0xf4040280u, 0xfa000018u, 0x7e0202ffu, 0x11111111u,
|
||||
0xbf8cc07fu, 0xbf068008u, 0xbf850006u, 0xf4080405u, 0xfa000010u, 0xbf8cc07fu, 0xe0300000u, 0x80040100u,
|
||||
0xbf8c3f70u, 0x34040082u, 0xe0701000u, 0x80010102u, 0xbf810000u,
|
||||
};
|
||||
|
||||
class StderrCapture {
|
||||
public:
|
||||
StderrCapture() {
|
||||
@@ -221,6 +228,23 @@ void RunTests(AgcDriver::VulkanDevice& device) {
|
||||
Require(!Faulted(chainMapped) && chainMapped.poisoned == 0u && chainMapped.cacheable && chainMapped.pipelineVariantId == chainTaken.pipelineVariantId && chainSkipped.pipelineVariantId == chainTaken.pipelineVariantId, "guarded pointer: a chain's poison changed its pipeline variant, or a mapped chain faulted:\n" + chainMapped.log);
|
||||
RequireWords(chainMapped, Lanes, Payload, "guarded pointer: a read through a mapped pointer chain");
|
||||
|
||||
Table = {0u, 0u, 0u, 0u, static_cast<std::uint32_t>(valid), static_cast<std::uint32_t>((valid >> 32u) & 0xffffu), static_cast<std::uint32_t>(sizeof(Data)), 0x30027facu};
|
||||
const auto table = static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(Table.data()));
|
||||
const auto descriptorTaken = Run(device, DescriptorCode, table, 1u);
|
||||
Require(!Faulted(descriptorTaken) && descriptorTaken.poisoned == 0u && descriptorTaken.cacheable, "guarded pointer: a V# behind a mapped pointer faulted, was poisoned or left the caches:\n" + descriptorTaken.log);
|
||||
RequireWords(descriptorTaken, Lanes, Payload, "guarded pointer: a buffer read through a V# behind a mapped pointer");
|
||||
const auto descriptorSkipped = Run(device, DescriptorCode, 0u, 0u);
|
||||
Require(!Faulted(descriptorSkipped) && descriptorSkipped.poisoned == 4u && !descriptorSkipped.cacheable, "guarded pointer: a V# behind a branch that skips its null pointer faulted or stayed cacheable:\n" + descriptorSkipped.log);
|
||||
RequireWords(descriptorSkipped, 0u, 0u, "guarded pointer: a skipped V# load through a null pointer");
|
||||
const auto descriptorNull = Run(device, DescriptorCode, 0u, 1u);
|
||||
Require(descriptorNull.pipelineVariantId == descriptorSkipped.pipelineVariantId && Faulted(descriptorNull) && descriptorNull.log.find(Fault(0x10u, 0x2cu)) != std::string::npos, "guarded pointer: a V# load through a null pointer that runs did not fault at its pc and address:\n" + descriptorNull.log);
|
||||
const auto descriptorUnmapped = Run(device, DescriptorCode, unmapped, 1u);
|
||||
Require(descriptorUnmapped.pipelineVariantId == descriptorNull.pipelineVariantId && Faulted(descriptorUnmapped) && descriptorUnmapped.log.find(Fault(unmapped + 0x10u, 0x2cu)) != std::string::npos, "guarded pointer: a V# load through an unmapped pointer that runs did not fault at its address:\n" + descriptorUnmapped.log);
|
||||
Table = {};
|
||||
const auto descriptorZero = Run(device, DescriptorCode, table, 1u);
|
||||
Require(!Faulted(descriptorZero) && descriptorZero.poisoned == 0u && descriptorZero.cacheable && descriptorZero.pipelineVariantId == descriptorNull.pipelineVariantId, "guarded pointer: a zero V# read from mapped memory faulted, or its pipeline variant differs from a V# zeroed by poison:\n" + descriptorZero.log);
|
||||
RequireWords(descriptorZero, Lanes, 0u, "guarded pointer: a buffer read through a zero V#");
|
||||
|
||||
#ifdef _WIN32
|
||||
VirtualFree(reserved, 0, MEM_RELEASE);
|
||||
#else
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace ShaderRecompiler::Detail {
|
||||
|
||||
@@ -92,6 +93,12 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const s
|
||||
}
|
||||
}
|
||||
}
|
||||
InaccessibleRead inaccessible;
|
||||
if (evaluateFlat && poison != nullptr) {
|
||||
evaluator.ReportInaccessibleReads(&inaccessible);
|
||||
cleanEvaluator.ReportInaccessibleReads(&inaccessible);
|
||||
}
|
||||
std::vector<std::pair<std::uint32_t, InaccessibleRead>> zeroedSources;
|
||||
std::vector<DescriptorValue> evaluated;
|
||||
evaluated.reserve(sources.size());
|
||||
for (const auto sourceIndex : sources) {
|
||||
@@ -103,7 +110,14 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const s
|
||||
value.dwordCount = source->dwordCount;
|
||||
if (!evaluateFlat || active[sourceIndex]) {
|
||||
for (std::uint32_t index = 0; index < source->dwordCount; index++) {
|
||||
inaccessible = {};
|
||||
if (!evaluator.Evaluate(source->dwords[index], value.dwords[index])) {
|
||||
if (inaccessible.read != nullptr) {
|
||||
if (debug) std::fprintf(stderr, "[srt] descriptor source %u reads inaccessible 0x%llx at pc 0x%x: zero words\n", sourceIndex, static_cast<unsigned long long>(inaccessible.address), inaccessible.read->Flags<MemoryFlags>().pc);
|
||||
value.dwords.fill(0u);
|
||||
zeroedSources.emplace_back(sourceIndex, inaccessible);
|
||||
break;
|
||||
}
|
||||
std::string detail = DescribeValue(source->dwords[index], 4);
|
||||
const IrValue* dword = source->dwords[index]->Resolve();
|
||||
if (dword->Opcode() == IrOpcode::ReadConst && dword->ArgumentCount() == 2 && dword->Argument(1)->Resolve()->HasImmediate()) {
|
||||
@@ -120,11 +134,6 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const s
|
||||
std::vector<SrtReadPoison> poisoned;
|
||||
if (evaluateFlat) {
|
||||
flattened.resize(program.srtReads.size());
|
||||
InaccessibleRead inaccessible;
|
||||
if (poison != nullptr) {
|
||||
evaluator.ReportInaccessibleReads(&inaccessible);
|
||||
cleanEvaluator.ReportInaccessibleReads(&inaccessible);
|
||||
}
|
||||
for (const auto& read : program.srtReads) {
|
||||
const bool clean = read.flatOffset < cleanFlatSlots.size() && cleanFlatSlots[read.flatOffset] != 0u;
|
||||
auto& selected = clean ? cleanEvaluator : evaluator;
|
||||
@@ -150,6 +159,14 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const s
|
||||
return Fail(std::string(clean ? "clean " : "") + "SRT read at flat offset " + std::to_string(read.flatOffset) + ": " + DescribeValue(read.value, 4));
|
||||
}
|
||||
}
|
||||
for (const auto& [sourceIndex, read] : zeroedSources) {
|
||||
const auto pc = read.read->Flags<MemoryFlags>().pc;
|
||||
if (std::none_of(poisoned.begin(), poisoned.end(), [&](const SrtReadPoison& entry) { return entry.pc == pc && entry.address == read.address; })) {
|
||||
char address[64];
|
||||
std::snprintf(address, sizeof(address), "0x%llx at pc 0x%x", static_cast<unsigned long long>(read.address), pc);
|
||||
return Fail("descriptor source " + std::to_string(sourceIndex) + " reads inaccessible " + address + ", which no flat SRT slot records");
|
||||
}
|
||||
}
|
||||
}
|
||||
results = std::move(evaluated);
|
||||
activeSources = std::move(active);
|
||||
|
||||
@@ -273,7 +273,8 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect, and `s_getreg_b32` reads of the MODE round fields report round to nearest even even after `s_setreg_b32` wrote another mode. The initial round mode from the shader's program registers is not read either.
|
||||
- A [flat SRT read](../../core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp) through a null or unmapped pointer loaded from memory raises a [BDA fault](../../core/libs/prx/libSceAgcDriver/Graphics/src/BdaResources.cpp) only if the program executes it, then stops or reads zero like a BDA read.
|
||||
- Repeated `s_load`s of one [flat SRT](../../core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp) dword through a null or unmapped pointer report one of their pcs, and an `s_load` whose inaccessible dwords are all unused, or only address repeated loads, reports no fault of its own.
|
||||
- A value read through a null or unmapped pointer still throws at record time if a [descriptor, branch condition, uniform fill or table base](../../core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp) needs it, or if the pointer comes from user data, even if the console skips the read.
|
||||
- A value read through a null or unmapped pointer still throws at record time if a [branch condition, uniform fill or table base](../../core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp) needs it, or if the pointer comes from user data, even if the console skips the read.
|
||||
- A [V#, T# or S#](../../core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp) loaded through a null or unmapped pointer read from memory is zero when the work is recorded, and the capture throws if no flat SRT slot records that read.
|
||||
- On a host subgroup narrower than 32 lanes, [`threadBit`](../../core/shader/recompiler/Translation/src/TranslationContext/ExecMask.cpp) reads an EXEC written as a value (`s_mov_b32`/`s_mov_b64`) at bit (subgroup invocation id) of EXEC_LO, so lanes at or above the subgroup size read a lower lane's bit.
|
||||
- [EXEC read as a value](../../core/shader/recompiler/Translation/src/TranslationContext/ExecMask.cpp) (`s_mov_b64 sN, exec`, the `s_*_saveexec` copy, ALU sources) holds only lanes with a host invocation; the console's also holds the wave's other lanes, e.g. 32-63 of a wave64 on 32 host lanes.
|
||||
- [`v_readlane_b32`](../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp) and fetch-inactive `v_permlane16_b32`/`v_permlanex16_b32` read undefined data from a lane that the host subgroup does not hold or whose host invocation is inactive; the console reads the lane's VGPR.
|
||||
|
||||
Reference in New Issue
Block a user