From 4478d2b08392aab783f4b4bc742997deddae3129 Mon Sep 17 00:00:00 2001 From: Michael Meeks Date: Thu, 9 Jun 2022 13:29:02 +0100 Subject: Delta: add checks for bad memory usage across threads. Change-Id: I09f5ad5b0af44399f92ccc0b62056172f1a0b220 Signed-off-by: Michael Meeks --- kit/Delta.hpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'kit/Delta.hpp') diff --git a/kit/Delta.hpp b/kit/Delta.hpp index 9675bf1408..607d121c02 100644 --- a/kit/Delta.hpp +++ b/kit/Delta.hpp @@ -39,7 +39,10 @@ class DeltaGenerator { }; /// A bitmap tile with annotated rows and details on its location - struct DeltaData { + struct DeltaData final { + // no careless copying + DeltaData(const DeltaData&) = delete; + DeltaData& operator=(const DeltaData&) = delete; static inline uint64_t copyWithCrc(uint32_t *to, const uint32_t *from, unsigned int width) { @@ -69,6 +72,7 @@ class DeltaGenerator { _top(tileTop), _size(tileSize), _part(tilePart), + _inUse(false), _wid(wid), // in Pixels _width(width), @@ -150,6 +154,11 @@ class DeltaGenerator { { assert (_left == repl->_left && _top == repl->_top && _size == repl->_size && _part == repl->_part); + if (repl.get() == this) + { + assert("replacing with yourself should never happen"); + return; + } _wid = repl->_wid; _width = repl->_width; _height = repl->_height; @@ -162,11 +171,24 @@ class DeltaGenerator { repl.reset(); } + inline void use() + { + const bool wasInUse = _inUse.exchange(true); (void)wasInUse; + assert(!wasInUse && "Error: delta was already in use by another thread"); + } + + inline void unuse() + { + const bool wasInUse = _inUse.exchange(false); (void)wasInUse; + assert(wasInUse && "Error: delta was already un-used by another thread"); + } + int _left; int _top; int _size; int _part; private: + std::atomic _inUse; // thread debugging check. TileWireId _wid; int _width; int _height; @@ -434,6 +456,7 @@ class DeltaGenerator { _deltaEntries.push_back(update); return false; } + cacheEntry->use(); } // interestingly cacheEntry may no longer be in the cache by here. @@ -446,6 +469,8 @@ class DeltaGenerator { // no two threads can be working on the same DeltaData. cacheEntry->replaceAndFree(update); + + cacheEntry->unuse(); return delta; } -- cgit