From b0ed4788858157e271779a7726cccc1149a05407 Mon Sep 17 00:00:00 2001 From: Nathaniel Cesario Date: Tue, 26 Sep 2023 11:50:10 -0600 Subject: [PATCH] Fix race condition identified by TSAN Guard global variables 'CompileFailed' and 'LinkFailed' in the StandAlone application with atomics. --- StandAlone/StandAlone.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/StandAlone/StandAlone.cpp b/StandAlone/StandAlone.cpp index 8833e38e..b31a6449 100644 --- a/StandAlone/StandAlone.cpp +++ b/StandAlone/StandAlone.cpp @@ -51,15 +51,16 @@ #include "../SPIRV/doc.h" #include "../SPIRV/disassemble.h" -#include -#include +#include +#include #include #include -#include +#include +#include #include #include -#include #include +#include #include "../glslang/OSDependent/osinclude.h" @@ -144,8 +145,9 @@ void FreeFileData(char* data); void InfoLogMsg(const char* msg, const char* name, const int num); // Globally track if any compile or link failure. -bool CompileFailed = false; -bool LinkFailed = false; +std::atomic CompileFailed{0}; +std::atomic LinkFailed{0}; +std::atomic CompileOrLinkFailed{0}; // array of unique places to leave the shader names and infologs for the asynchronous compiles std::vector> WorkItems; @@ -1166,6 +1168,7 @@ void CompileShaders(glslang::TWorklist& worklist) if (Options & EOptionDebug) Error("cannot generate debug information unless linking to generate code"); + // NOTE: TWorkList::remove is thread-safe glslang::TWorkItem* workItem; if (Options & EOptionStdin) { if (worklist.remove(workItem)) { @@ -1442,7 +1445,7 @@ void CompileAndLinkShaderUnits(std::vector compUnits) if (shader->preprocess(GetResources(), defaultVersion, ENoProfile, false, false, messages, &str, includer)) { PutsIfNonEmpty(str.c_str()); } else { - CompileFailed = true; + CompileFailed = 1; } StderrIfNonEmpty(shader->getInfoLog()); StderrIfNonEmpty(shader->getInfoDebugLog()); @@ -1450,7 +1453,7 @@ void CompileAndLinkShaderUnits(std::vector compUnits) } if (! shader->parse(GetResources(), defaultVersion, false, messages, includer)) - CompileFailed = true; + CompileFailed = 1; if (!compileOnly) program.addShader(shader); @@ -1496,7 +1499,9 @@ void CompileAndLinkShaderUnits(std::vector compUnits) // Dump SPIR-V if (Options & EOptionSpv) { - if (CompileFailed || LinkFailed) + CompileOrLinkFailed.fetch_or(CompileFailed); + CompileOrLinkFailed.fetch_or(LinkFailed); + if (static_cast(CompileOrLinkFailed.load())) printf("SPIR-V is not generated for failed compile or link\n"); else { std::vector intermediates; @@ -1555,7 +1560,9 @@ void CompileAndLinkShaderUnits(std::vector compUnits) } } - if (depencyFileName && !(CompileFailed || LinkFailed)) { + CompileOrLinkFailed.fetch_or(CompileFailed); + CompileOrLinkFailed.fetch_or(LinkFailed); + if (depencyFileName && !static_cast(CompileOrLinkFailed.load())) { std::set includedFiles = includer.getIncludedFiles(); sources.insert(sources.end(), includedFiles.begin(), includedFiles.end()); @@ -1731,9 +1738,9 @@ int singleMain() ShFinalize(); } - if (CompileFailed) + if (CompileFailed.load()) return EFailCompile; - if (LinkFailed) + if (LinkFailed.load()) return EFailLink; return 0;