summaryrefslogtreecommitdiff
path: root/core/templates
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-01-09 14:18:53 +0100
committerRémi Verschelde <rverschelde@gmail.com>2023-01-09 14:18:53 +0100
commitd46568205de9dcc5bb8367644c5b2f00986b0761 (patch)
tree2c292e22ed1679738908b98d16cd99583dc86723 /core/templates
parent4a504e3181c0b6d8ccc19a83452d1639c305b83a (diff)
parentb1c7665866717248a2d90ac8908acb9998da014a (diff)
Merge pull request #64795 from RandomShaper/fix_saferefcount
Prevent misuse of SafeRefCount
Diffstat (limited to 'core/templates')
-rw-r--r--core/templates/safe_refcount.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/core/templates/safe_refcount.h b/core/templates/safe_refcount.h
index 4d32e421cd..58ed019287 100644
--- a/core/templates/safe_refcount.h
+++ b/core/templates/safe_refcount.h
@@ -33,6 +33,10 @@
#include "core/typedefs.h"
+#ifdef DEV_ENABLED
+#include "core/error/error_macros.h"
+#endif
+
#include <atomic>
#include <type_traits>
@@ -163,6 +167,16 @@ public:
class SafeRefCount {
SafeNumeric<uint32_t> count;
+#ifdef DEV_ENABLED
+ _ALWAYS_INLINE_ void _check_unref_sanity() {
+ // This won't catch every misuse, but it's better than nothing.
+ CRASH_COND_MSG(count.get() == 0,
+ "Trying to unreference a SafeRefCount which is already zero is wrong and a symptom of it being misused.\n"
+ "Upon a SafeRefCount reaching zero any object whose lifetime is tied to it, as well as the ref count itself, must be destroyed.\n"
+ "Moreover, to guarantee that, no multiple threads should be racing to do the final unreferencing to zero.");
+ }
+#endif
+
public:
_ALWAYS_INLINE_ bool ref() { // true on success
return count.conditional_increment() != 0;
@@ -173,10 +187,16 @@ public:
}
_ALWAYS_INLINE_ bool unref() { // true if must be disposed of
+#ifdef DEV_ENABLED
+ _check_unref_sanity();
+#endif
return count.decrement() == 0;
}
_ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
+#ifdef DEV_ENABLED
+ _check_unref_sanity();
+#endif
return count.decrement();
}