From cd3fc26190c4ef0897884449094a724022b4d2ae Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 22 Feb 2023 21:15:03 -0800 Subject: [PATCH] samplerjit: Prevent thread local stale cache read. If the generation count happens to match, would still get a stale pointer and crash. Let's just make the generation count static so it always increases. --- GPU/Software/DrawPixel.cpp | 2 ++ GPU/Software/DrawPixel.h | 2 +- GPU/Software/Sampler.cpp | 2 ++ GPU/Software/Sampler.h | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/GPU/Software/DrawPixel.cpp b/GPU/Software/DrawPixel.cpp index 72b6e2a91a..eda04b0b41 100644 --- a/GPU/Software/DrawPixel.cpp +++ b/GPU/Software/DrawPixel.cpp @@ -802,10 +802,12 @@ SingleFunc PixelJitCache::GenericSingle(const PixelFuncID &id) { } thread_local PixelJitCache::LastCache PixelJitCache::lastSingle_; +int PixelJitCache::clearGen_ = 0; // 256k should be plenty of space for plenty of variations. PixelJitCache::PixelJitCache() : CodeBlock(1024 * 64 * 4), cache_(64) { lastSingle_.gen = -1; + clearGen_++; } void PixelJitCache::Clear() { diff --git a/GPU/Software/DrawPixel.h b/GPU/Software/DrawPixel.h index d72c04a744..b0eae46ac6 100644 --- a/GPU/Software/DrawPixel.h +++ b/GPU/Software/DrawPixel.h @@ -128,7 +128,7 @@ private: DenseHashMap cache_; std::unordered_map addresses_; std::unordered_set compileQueue_; - int clearGen_ = 0; + static int clearGen_; static thread_local LastCache lastSingle_; const u8 *constBlendHalf_11_4s_ = nullptr; diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index dd022c5efd..eef759344f 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -109,12 +109,14 @@ FetchFunc GetFetchFunc(SamplerID id, BinManager *binner) { thread_local SamplerJitCache::LastCache SamplerJitCache::lastFetch_; thread_local SamplerJitCache::LastCache SamplerJitCache::lastNearest_; thread_local SamplerJitCache::LastCache SamplerJitCache::lastLinear_; +int SamplerJitCache::clearGen_ = 0; // 256k should be enough. SamplerJitCache::SamplerJitCache() : Rasterizer::CodeBlock(1024 * 64 * 4), cache_(64) { lastFetch_.gen = -1; lastNearest_.gen = -1; lastLinear_.gen = -1; + clearGen_++; } void SamplerJitCache::Clear() { diff --git a/GPU/Software/Sampler.h b/GPU/Software/Sampler.h index fbe606b110..ec8d4c2638 100644 --- a/GPU/Software/Sampler.h +++ b/GPU/Software/Sampler.h @@ -147,7 +147,7 @@ private: DenseHashMap cache_; std::unordered_map addresses_; std::unordered_set compileQueue_; - int clearGen_ = 0; + static int clearGen_; static thread_local LastCache lastFetch_; static thread_local LastCache lastNearest_; static thread_local LastCache lastLinear_;