mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-17 15:03:25 +09:00
[Fix] (Magma): decline programs whose final SPIR-V fails validation instead of handing the module to the driver
This commit is contained in:
@@ -391,6 +391,15 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
|
||||
VkPipeline PipelineFactory::CreatePipeline(const PipelineCreatePayload& payload) const {
|
||||
MOBILEGL_ASSERT(payload.stages != nullptr && !payload.stages->empty(), "PipelineFactory: stages are empty");
|
||||
// The assert compiles out of shipping builds, and a program declined for an invalid
|
||||
// module (VkProgramObject::invalidSpirvDeclined) has no stages at all: a caller that
|
||||
// forgot to refuse it must get a skipped draw, not a stageCount=0 pipeline.
|
||||
if (payload.stages == nullptr || payload.stages->empty()) {
|
||||
MGLOG_E_ONCE("PipelineFactory::CreatePipeline: refusing a pipeline with no shader stages "
|
||||
"(programHash=0x%llx); its draws are skipped",
|
||||
static_cast<unsigned long long>(payload.programHash));
|
||||
return VK_NULL_HANDLE;
|
||||
}
|
||||
MOBILEGL_ASSERT(payload.vertexInputState != nullptr, "PipelineFactory: vertexInputState is null");
|
||||
MOBILEGL_ASSERT(payload.pipelineLayout != VK_NULL_HANDLE, "PipelineFactory: pipelineLayout is null");
|
||||
MOBILEGL_ASSERT(payload.renderPass != VK_NULL_HANDLE, "PipelineFactory: renderPass is null");
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
#include "MG_Util/ShaderTranspiler/ShaderCompiler.h"
|
||||
#include "MG_Util/ShaderTranspiler/SpvcSession.h"
|
||||
#include "MG_Util/ShaderTranspiler/Types.h"
|
||||
#include <Config.h>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <bit>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
@@ -2273,8 +2275,84 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ScreenModuleForDriver's memory. Keyed by content, never by program: the same bytes get
|
||||
// the same answer whichever program, variant or session carries them.
|
||||
struct DriverModuleVerdictCache {
|
||||
struct Entry {
|
||||
Uint64 wordCount = 0;
|
||||
Bool valid = true;
|
||||
String reason;
|
||||
};
|
||||
// Generous for any one application's distinct stage modules; past it the whole table
|
||||
// is dropped rather than tracked per entry - a re-validation is the only cost.
|
||||
static constexpr SizeT kMaxEntries = 8192;
|
||||
std::mutex mutex;
|
||||
UnorderedMap<Uint64, Entry> entries;
|
||||
std::atomic<Uint64> validatorRuns{0};
|
||||
};
|
||||
|
||||
DriverModuleVerdictCache& GetDriverModuleVerdictCache() {
|
||||
// Leaked on purpose: screened from session threads that may outlive static destruction.
|
||||
static auto* cache = new DriverModuleVerdictCache();
|
||||
return *cache;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ProgramFactory::DriverModuleVerdict ProgramFactory::ScreenModuleForDriver(const Vector<Uint>& spirv,
|
||||
Bool allowInvalid, String* outReason) {
|
||||
if (outReason != nullptr) outReason->clear();
|
||||
if (spirv.empty()) return DriverModuleVerdict::Valid;
|
||||
|
||||
auto& cache = GetDriverModuleVerdictCache();
|
||||
const Uint64 key = XXH64(spirv.data(), spirv.size() * sizeof(Uint), 0);
|
||||
Bool valid = true;
|
||||
String reason;
|
||||
Bool known = false;
|
||||
{
|
||||
const std::lock_guard<std::mutex> lock(cache.mutex);
|
||||
const auto it = cache.entries.find(key);
|
||||
if (it != cache.entries.end() && it->second.wordCount == spirv.size()) {
|
||||
known = true;
|
||||
valid = it->second.valid;
|
||||
reason = it->second.reason;
|
||||
}
|
||||
}
|
||||
if (!known) {
|
||||
// Outside the lock: two threads screening the same new module both validate, and
|
||||
// agree; nothing waits on another session's spirv-val.
|
||||
MG_Util::ShaderTranspiler::ShaderCompiler::PrepareSpirvValidation();
|
||||
cache.validatorRuns.fetch_add(1, std::memory_order_relaxed);
|
||||
spv_context context = spvContextCreate(GetSpirvTargetEnv(spirv));
|
||||
spv_validator_options options = spvValidatorOptionsCreate();
|
||||
spvValidatorOptionsSetFriendlyNames(options, true);
|
||||
spv_const_binary_t binary = {spirv.data(), spirv.size()};
|
||||
spv_diagnostic diagnostic = nullptr;
|
||||
const spv_result_t result = spvValidateWithOptions(context, options, &binary, &diagnostic);
|
||||
valid = result == SPV_SUCCESS;
|
||||
if (!valid) {
|
||||
reason = diagnostic != nullptr && diagnostic->error != nullptr ? diagnostic->error : "<no diagnostic>";
|
||||
// The process-wide counter the validating test lanes assert on.
|
||||
MG_Util::ShaderTranspiler::ShaderCompiler::NoteSpirvValidationFailure();
|
||||
}
|
||||
spvDiagnosticDestroy(diagnostic);
|
||||
spvValidatorOptionsDestroy(options);
|
||||
spvContextDestroy(context);
|
||||
|
||||
const std::lock_guard<std::mutex> lock(cache.mutex);
|
||||
if (cache.entries.size() >= DriverModuleVerdictCache::kMaxEntries) cache.entries.clear();
|
||||
cache.entries[key] = DriverModuleVerdictCache::Entry{spirv.size(), valid, reason};
|
||||
}
|
||||
|
||||
if (valid) return DriverModuleVerdict::Valid;
|
||||
if (outReason != nullptr) *outReason = Move(reason);
|
||||
return allowInvalid ? DriverModuleVerdict::AdmittedInvalid : DriverModuleVerdict::DeclinedInvalid;
|
||||
}
|
||||
|
||||
Uint64 ProgramFactory::DriverModuleValidatorRuns() {
|
||||
return GetDriverModuleVerdictCache().validatorRuns.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// A shader that assigns gl_FragDepth (SPIR-V DepthReplacing) supplies depth itself
|
||||
// instead of taking the pipeline's interpolated Z, so a driver that varies the vertex
|
||||
// position math between pipelines cannot desynchronize it; the blended depth-write
|
||||
@@ -3814,27 +3892,42 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
const Bool remapOk = RemapDescriptorBindingsForVulkan(moduleSpirvs, m_maxBindings, moduleSpirvs);
|
||||
MOBILEGL_ASSERT(remapOk, "ProgramFactory::GetOrCreateProgram: descriptor binding remap failed");
|
||||
|
||||
// Last look at the exact bytes the driver receives, in EVERY build rather than only in
|
||||
// DEBUG or with MOBILEGL_ENABLE_SPIRV_VALIDATION armed, and for every stage BEFORE any
|
||||
// module is created. By here the descriptor bindings have been remapped and the layout
|
||||
// about to be reflected describes the remapped module, so there is no module left that
|
||||
// is both valid and consistent with it to fall back to (the recovery that exists lives
|
||||
// one step earlier, at the clip/XFB fixups). What is left is to keep the bytes away from
|
||||
// the driver: a driver is not a validating entry point, and an invalid module it accepts
|
||||
// can fault the GPU into device loss (a frontend translation once put a sampler inside
|
||||
// the default uniform block, and Adreno page-faulted reading its "descriptor" out of
|
||||
// uniform-buffer bytes). One invalid stage declines the whole program: no shader module
|
||||
// of it is created, and every draw or dispatch of it is refused by name
|
||||
// (VkProgramObject::invalidSpirvDeclined). MOBILEGL_MAGMA_ALLOW_INVALID_SPIRV hands the
|
||||
// bytes over anyway, for diagnosing a driver that copes.
|
||||
for (SizeT i = 0; i < moduleCount; ++i) {
|
||||
String reason;
|
||||
const DriverModuleVerdict verdict = ScreenModuleForDriver(
|
||||
moduleSpirvs[i], MG_Config::Features.MagmaAllowInvalidSpirv, &reason);
|
||||
if (verdict == DriverModuleVerdict::DeclinedInvalid) {
|
||||
MGLOG_E_ONCE("ProgramFactory: declining program %u - its stage %d module fails SPIR-V validation (%s); "
|
||||
"no shader module is created and its draws are refused. "
|
||||
"MOBILEGL_MAGMA_ALLOW_INVALID_SPIRV=1 hands such modules to the driver anyway.",
|
||||
program.GetExternalIndex(), static_cast<Int>(stages[i]), reason.c_str());
|
||||
entry.invalidSpirvDeclined = true;
|
||||
break;
|
||||
}
|
||||
if (verdict == DriverModuleVerdict::AdmittedInvalid) {
|
||||
MGLOG_E_ONCE("ProgramFactory: handing vkCreateShaderModule an INVALID module for program %u stage %d "
|
||||
"because MOBILEGL_MAGMA_ALLOW_INVALID_SPIRV is set (%s)",
|
||||
program.GetExternalIndex(), static_cast<Int>(stages[i]), reason.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
for (SizeT i = 0; i < moduleCount && !entry.invalidSpirvDeclined; ++i) {
|
||||
auto& moduleSpv = moduleSpirvs[i];
|
||||
if (moduleSpv.empty()) continue;
|
||||
|
||||
// Last look at the exact bytes the driver receives, in EVERY build rather than only
|
||||
// in DEBUG or with MOBILEGL_ENABLE_SPIRV_VALIDATION armed. This one only reports:
|
||||
// by here the descriptor bindings have been remapped and the layout about to be
|
||||
// reflected describes the remapped module, so there is no module left that is both
|
||||
// valid and consistent with it to fall back to. The recovery lives one step earlier,
|
||||
// at the clip/XFB fixups (see the revert there) - which is where a transform can
|
||||
// introduce a reference to a delisted interface variable, the failure this whole
|
||||
// guard exists for. Anything that reaches this line names itself in the log of a
|
||||
// shipping build instead of dying anonymously inside the driver.
|
||||
SpirvValidationFailure finalFailure{};
|
||||
if (!ValidateTransformedSpirv(moduleSpv, stages[i], program.GetExternalIndex(), &finalFailure)) {
|
||||
MGLOG_E_ONCE("ProgramFactory: handing vkCreateShaderModule an INVALID module for program %u stage %d - "
|
||||
"a backend transform after the clip/XFB fixups broke it (%s)",
|
||||
program.GetExternalIndex(), static_cast<Int>(stages[i]),
|
||||
finalFailure.message.c_str());
|
||||
}
|
||||
|
||||
// Does the stage the driver will treat as the last pre-rasterization one actually
|
||||
// carry Xfb? Asked of the FINAL bytes, so it answers for whatever the whole transform
|
||||
// chain produced - a rewound clip/XFB backstop, a capture pass that resolved no
|
||||
|
||||
@@ -229,6 +229,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
// the cache entry, so every later draw of the same program variant reads the same
|
||||
// answer instead of re-deciding it.
|
||||
Bool pointSizeCapabilityUnsupported = false;
|
||||
// One of the program's final modules fails SPIR-V validation, so none of its modules
|
||||
// was created and every draw or dispatch of it is refused by name (the ProgramInvalidSpirv /
|
||||
// ComputeProgramInvalidSpirv wire declines) - see ProgramFactory::ScreenModuleForDriver.
|
||||
// Sticky like the flag above: it lives on the cache entry, so the one log line is honest.
|
||||
Bool invalidSpirvDeclined = false;
|
||||
Bool needsPassthroughTessControl = false;
|
||||
// ...and the pass-through this renderer can synthesize carries gl_Position and
|
||||
// nothing else, so it is only correct when the evaluation stage's inputs are
|
||||
@@ -289,6 +294,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
storageBlockIndexByBinding = std::move(other.storageBlockIndexByBinding);
|
||||
hasStorageImages = other.hasStorageImages;
|
||||
declinedDescriptors = other.declinedDescriptors;
|
||||
// The three refusal flags travel like every other field: a robin-hood insert swaps
|
||||
// entries, and a flag left behind would refuse (or admit) the wrong program.
|
||||
invalidSpirvDeclined = other.invalidSpirvDeclined;
|
||||
xfbCaptureDeclined = other.xfbCaptureDeclined;
|
||||
pointSizeCapabilityUnsupported = other.pointSizeCapabilityUnsupported;
|
||||
globalUboBinding = other.globalUboBinding;
|
||||
activeVertexInputLocationMask = other.activeVertexInputLocationMask;
|
||||
vertexInputTypes = other.vertexInputTypes;
|
||||
@@ -310,6 +320,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
other.pipelineLayout = VK_NULL_HANDLE;
|
||||
other.hasStorageImages = false;
|
||||
other.declinedDescriptors = false;
|
||||
other.invalidSpirvDeclined = false;
|
||||
other.xfbCaptureDeclined = false;
|
||||
other.pointSizeCapabilityUnsupported = false;
|
||||
other.globalUboBinding = -1;
|
||||
other.activeVertexInputLocationMask = 0;
|
||||
other.activeFragmentOutputLocationMask = 0;
|
||||
@@ -353,6 +366,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
storageBlockIndexByBinding = std::move(other.storageBlockIndexByBinding);
|
||||
hasStorageImages = other.hasStorageImages;
|
||||
declinedDescriptors = other.declinedDescriptors;
|
||||
// The three refusal flags travel like every other field: a robin-hood insert swaps
|
||||
// entries, and a flag left behind would refuse (or admit) the wrong program.
|
||||
invalidSpirvDeclined = other.invalidSpirvDeclined;
|
||||
xfbCaptureDeclined = other.xfbCaptureDeclined;
|
||||
pointSizeCapabilityUnsupported = other.pointSizeCapabilityUnsupported;
|
||||
globalUboBinding = other.globalUboBinding;
|
||||
activeVertexInputLocationMask = other.activeVertexInputLocationMask;
|
||||
vertexInputTypes = other.vertexInputTypes;
|
||||
@@ -374,6 +392,9 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
other.pipelineLayout = VK_NULL_HANDLE;
|
||||
other.hasStorageImages = false;
|
||||
other.declinedDescriptors = false;
|
||||
other.invalidSpirvDeclined = false;
|
||||
other.xfbCaptureDeclined = false;
|
||||
other.pointSizeCapabilityUnsupported = false;
|
||||
other.globalUboBinding = -1;
|
||||
other.activeVertexInputLocationMask = 0;
|
||||
other.activeFragmentOutputLocationMask = 0;
|
||||
@@ -504,6 +525,29 @@ namespace MobileGL::MG_Backend::DirectVulkan {
|
||||
// VkRenderPassManager::OnPresent's sweep.
|
||||
void OnFrameBoundary();
|
||||
|
||||
// What the last look at a module's final bytes - the exact words vkCreateShaderModule
|
||||
// would receive - decided.
|
||||
enum class DriverModuleVerdict : Uint8 {
|
||||
Valid, // validates: handed to the driver
|
||||
DeclinedInvalid, // fails validation: never handed over, the program's work is declined
|
||||
AdmittedInvalid, // fails validation, handed over anyway (MOBILEGL_MAGMA_ALLOW_INVALID_SPIRV)
|
||||
};
|
||||
// Validates `spirv` for the Vulkan environment its version word names and turns the
|
||||
// result into a verdict. A driver is not a validating entry point: Adreno page-faulted
|
||||
// into device loss on a module that carried a sampler inside a uniform block, Mali
|
||||
// SIGSEGVs inside pipeline creation. So an invalid module is declined unless
|
||||
// `allowInvalid` (the escape hatch) says to hand it over regardless.
|
||||
//
|
||||
// The validator's answer is remembered per module content (64-bit hash plus word count),
|
||||
// process-wide: a program variant, an evicted-then-rebuilt entry and two programs that
|
||||
// share a stage all re-screen the same bytes, and only the first pays for spirv-val.
|
||||
// `outReason` receives the validator's message for an invalid module.
|
||||
static DriverModuleVerdict ScreenModuleForDriver(const Vector<Uint>& spirv, Bool allowInvalid,
|
||||
String* outReason = nullptr);
|
||||
// How many times ScreenModuleForDriver actually ran the validator (cache misses), for
|
||||
// the tests that pin the cache.
|
||||
static Uint64 DriverModuleValidatorRuns();
|
||||
|
||||
static VkShaderStageFlagBits ToVkStage(ShaderStage stage);
|
||||
static VkFormat ConvertSpirvImageFormatToVkFormat(SpvImageFormat format);
|
||||
static SamplerNumericDomain UniformTypeToSamplerNumericDomain(GLenum glType);
|
||||
|
||||
@@ -6835,7 +6835,7 @@ void main() {
|
||||
// A refused program cannot reach here today - the full path refuses before it ever
|
||||
// records a snapshot - but declining the fast path costs one compare and means the
|
||||
// refusal does not depend on that ordering staying true.
|
||||
if (programObj.pointSizeCapabilityUnsupported) {
|
||||
if (programObj.pointSizeCapabilityUnsupported || programObj.invalidSpirvDeclined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -7318,7 +7318,7 @@ void main() {
|
||||
// The build already said why, once, naming the program and the stage. Refusing here -
|
||||
// before any pipeline is built from it - is what makes that message a decline rather
|
||||
// than a note attached to invalid usage the driver still receives.
|
||||
if (programObj.pointSizeCapabilityUnsupported) {
|
||||
if (programObj.pointSizeCapabilityUnsupported || programObj.invalidSpirvDeclined) {
|
||||
return false;
|
||||
}
|
||||
// For the snapshot's memoised entry pointer: if anything below inserts into the
|
||||
@@ -7778,6 +7778,8 @@ void main() {
|
||||
}
|
||||
ProgramFactory::CompileOptionFlags transformFlags = 0;
|
||||
const auto& programObj = m_programFactory->GetOrCreateProgram(program, transformFlags);
|
||||
// No shader module of a program that failed validation exists; the build said why, once.
|
||||
if (programObj.invalidSpirvDeclined) return;
|
||||
|
||||
if (!frame.isCommandRecording) {
|
||||
m_frameContext.BeginCommandRecording();
|
||||
@@ -7840,6 +7842,8 @@ void main() {
|
||||
}
|
||||
ProgramFactory::CompileOptionFlags transformFlags = 0;
|
||||
const auto& programObj = m_programFactory->GetOrCreateProgram(program, transformFlags);
|
||||
// No shader module of a program that failed validation exists; the build said why, once.
|
||||
if (programObj.invalidSpirvDeclined) return;
|
||||
|
||||
if (!frame.isCommandRecording) {
|
||||
m_frameContext.BeginCommandRecording();
|
||||
|
||||
@@ -126,3 +126,11 @@ MGL_WIRE_DECLINE(MipmapDepthStencilAspect)
|
||||
// is missing, or the three group-count words lie outside it or are misaligned.
|
||||
MGL_WIRE_DECLINE(DispatchIndirectBufferUnbound)
|
||||
MGL_WIRE_DECLINE(DispatchIndirectRange)
|
||||
|
||||
// ---- ProgramFactory::ScreenModuleForDriver ------------------------------------------------
|
||||
// One of the bound program's final modules fails SPIR-V validation, so no shader module of it was
|
||||
// created (VkProgramObject::invalidSpirvDeclined): SetupWireDraw refuses the draw, and the two
|
||||
// dispatch arms the dispatch. A driver handed the module could fault the GPU into device loss.
|
||||
// MOBILEGL_MAGMA_ALLOW_INVALID_SPIRV=1 builds the module anyway, and then neither row moves.
|
||||
MGL_WIRE_DECLINE(ProgramInvalidSpirv)
|
||||
MGL_WIRE_DECLINE(ComputeProgramInvalidSpirv)
|
||||
|
||||
@@ -230,6 +230,10 @@
|
||||
flags |= ProgramFactory::CompileOptionBit::ZeroBaseVertex;
|
||||
programObject = &m_programFactory->GetOrCreateProgram(program, flags);
|
||||
}
|
||||
if (programObject->invalidSpirvDeclined) {
|
||||
MGL_WIRE_DECLINE_AT(ProgramInvalidSpirv, "a module of the program fails SPIR-V validation and was never built");
|
||||
return false;
|
||||
}
|
||||
if (programObject->pointSizeCapabilityUnsupported) {
|
||||
MGL_WIRE_DECLINE_AT(PointSizeCapability, "the program writes gl_PointSize and the device cannot express it");
|
||||
return false;
|
||||
@@ -775,6 +779,10 @@
|
||||
return;
|
||||
}
|
||||
const auto& programObj = m_programFactory->GetOrCreateProgram(program, {});
|
||||
if (programObj.invalidSpirvDeclined) {
|
||||
MGL_WIRE_DECLINE_AT(ComputeProgramInvalidSpirv, "a module of the compute program fails SPIR-V validation and was never built");
|
||||
return;
|
||||
}
|
||||
if (!m_uniformManager->PrepareWireTextureResources(program, programObj)) {
|
||||
MGL_WIRE_DECLINE_AT(ComputeTextureResourcesUnprepared, "a sampled/storage image could not be made ready for the dispatch");
|
||||
return;
|
||||
@@ -847,6 +855,10 @@
|
||||
return;
|
||||
}
|
||||
const auto& programObj = m_programFactory->GetOrCreateProgram(program, {});
|
||||
if (programObj.invalidSpirvDeclined) {
|
||||
MGL_WIRE_DECLINE_AT(ComputeProgramInvalidSpirv, "a module of the compute program fails SPIR-V validation and was never built");
|
||||
return;
|
||||
}
|
||||
if (!m_uniformManager->PrepareWireTextureResources(program, programObj)) {
|
||||
MGL_WIRE_DECLINE_AT(ComputeTextureResourcesUnprepared, "a sampled/storage image could not be made ready for the dispatch");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user