Godot Engine – Multi-platform 2D and 3D game engine
Fix CallableCustomMethodPointerBase UB causing inconsistent hashing
Jason Stenftenagelc39d912134db
diff --git a/core/object/callable_mp.cpp b/core/object/callable_mp.cppindex 6506643f1ff..213d4c5d4fa 100644--- a/core/object/callable_mp.cpp+++ b/core/object/callable_mp.cpp@@ -30,6 +30,8 @@#include "callable_mp.h"+#include <cstring>+bool CallableCustomMethodPointerBase::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {const CallableCustomMethodPointerBase *a = static_cast<const CallableCustomMethodPointerBase *>(p_a);const CallableCustomMethodPointerBase *b = static_cast<const CallableCustomMethodPointerBase *>(p_b);@@ -41,7 +43,7 @@ bool CallableCustomMethodPointerBase::compare_equal(const CallableCustom *p_a, c// Avoid sorting by memory address proximity, which leads to unpredictable performance over time// due to the reuse of old addresses for newer objects. Use byte-wise comparison to leverage the// backwards encoding of little-endian systems as a way to decouple spatiality and time.- return memcmp(a->comp_ptr, b->comp_ptr, a->comp_size * 4) == 0;+ return memcmp(a->comp_ptr, b->comp_ptr, a->comp_size) == 0;}bool CallableCustomMethodPointerBase::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {@@ -53,7 +55,7 @@ bool CallableCustomMethodPointerBase::compare_less(const CallableCustom *p_a, co}// See note in compare_equal().- return memcmp(a->comp_ptr, b->comp_ptr, a->comp_size * 4) < 0;+ return memcmp(a->comp_ptr, b->comp_ptr, a->comp_size) < 0;}CallableCustom::CompareEqualFunc CallableCustomMethodPointerBase::get_compare_equal_func() const {@@ -68,16 +70,9 @@ uint32_t CallableCustomMethodPointerBase::hash() const {return h;}-void CallableCustomMethodPointerBase::_setup(uint32_t *p_base_ptr, uint32_t p_ptr_size) {+void CallableCustomMethodPointerBase::_setup(const char *p_base_ptr, uint32_t p_ptr_size) {comp_ptr = p_base_ptr;- comp_size = p_ptr_size / 4;+ comp_size = p_ptr_size;- // Precompute hash.- for (uint32_t i = 0; i < comp_size; i++) {- if (i == 0) {- h = hash_murmur3_one_32(comp_ptr[i]);- } else {- h = hash_murmur3_one_32(comp_ptr[i], h);- }- }+ h = hash_murmur3_buffer(p_base_ptr, p_ptr_size);}diff --git a/core/object/callable_mp.h b/core/object/callable_mp.hindex aad45a0048b..bb0bfd74644 100644--- a/core/object/callable_mp.h+++ b/core/object/callable_mp.h@@ -37,7 +37,7 @@#include <type_traits>class CallableCustomMethodPointerBase : public CallableCustom {- uint32_t *comp_ptr = nullptr;+ const char *comp_ptr = nullptr;uint32_t comp_size;uint32_t h;#ifdef DEBUG_ENABLED@@ -47,7 +47,7 @@ class CallableCustomMethodPointerBase : public CallableCustom {static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);protected:- void _setup(uint32_t *p_base_ptr, uint32_t p_ptr_size);+ void _setup(const char *p_base_ptr, uint32_t p_ptr_size);public:virtual StringName get_method() const {@@ -111,7 +111,7 @@ public:data.instance = p_instance;data.object_id = p_instance->get_instance_id();data.method = p_method;- _setup((uint32_t *)&data, sizeof(Data));+ _setup(reinterpret_cast<const char *>(&data), sizeof(Data));}};@@ -180,7 +180,7 @@ public:data.instance = p_instance;data.object_id = p_instance->get_instance_id();data.method = p_method;- _setup((uint32_t *)&data, sizeof(Data));+ _setup(reinterpret_cast<const char *>(&data), sizeof(Data));}};@@ -251,7 +251,7 @@ public:CallableCustomStaticMethodPointer(R (*p_method)(P...)) {memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.data.method = p_method;- _setup((uint32_t *)&data, sizeof(Data));+ _setup(reinterpret_cast<const char *>(&data), sizeof(Data));}};diff --git a/core/os/memory.cpp b/core/os/memory.cppindex 5418e9b36fc..b910424b182 100644--- a/core/os/memory.cpp+++ b/core/os/memory.cpp@@ -44,14 +44,6 @@ static SafeNumeric<uint64_t> _current_mem_usage;static SafeNumeric<uint64_t> _max_mem_usage;#endif-void *operator new(size_t p_size, DefaultAllocator p_allocator) {- return Memory::alloc_static(p_size);-}--void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) {- return p_allocfunc(p_size);-}-void *Memory::alloc_aligned_static(size_t p_bytes, size_t p_alignment) {DEV_ASSERT(Math::is_power_of_2(p_alignment));diff --git a/core/os/memory.h b/core/os/memory.hindex 75923e37e33..319a4d8887b 100644--- a/core/os/memory.h+++ b/core/os/memory.h@@ -104,13 +104,15 @@ public:_FORCE_INLINE_ static void free(void *p_ptr) { Memory::free_static(p_ptr, false); }};-// Overload of `new` operator to use the `Memory::alloc_static()` function.-// The `DefaultAllocator` parameter is just a tag to select this overload.-// NOTE: do not inline `new` operators due to GCC+LTO compiler bug (see GH-119752).-void *operator new(size_t p_size, DefaultAllocator p_allocator);--// Overload of `new` operator to use a custom allocation function.-void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size));+// Overload of new operator to use the Memory::alloc_static function.+// The DefaultAllocator parameter is just a tag to select this overload.+inline void *operator new(size_t p_size, DefaultAllocator p_allocator) {+ return Memory::alloc_static(p_size);+}+// Overload of new operator to use a custom allocation function.+inline void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) {+ return p_allocfunc(p_size);+}#if defined(_MSC_VER) && !defined(__clang__)// When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291).diff --git a/core/templates/hashfuncs.cpp b/core/templates/hashfuncs.cppindex e936cd5f087..6aa269447c3 100644--- a/core/templates/hashfuncs.cpp+++ b/core/templates/hashfuncs.cpp@@ -69,17 +69,17 @@ uint32_t hash_murmur3_one_double(double p_in, uint32_t p_seed) {uint32_t hash_murmur3_buffer(const void *p_key, int p_length, const uint32_t p_seed) {// Although not required, this is a random prime number.const uint8_t *data = (const uint8_t *)p_key;- const int nblocks = p_length / 4;+ const int nblocks = p_length / sizeof(uint32_t);uint32_t h1 = p_seed;const uint32_t c1 = 0xcc9e2d51;const uint32_t c2 = 0x1b873593;- const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);-- for (int i = -nblocks; i; i++) {- uint32_t k1 = blocks[i];+ for (int i = 0; i < nblocks; i++) {+ uint32_t k1;+ // Use memcpy to avoid UB with strict-aliasing.+ memcpy(&k1, data + i * sizeof(uint32_t), sizeof(uint32_t));k1 *= c1;k1 = hash_rotl32(k1, 15);@@ -90,7 +90,7 @@ uint32_t hash_murmur3_buffer(const void *p_key, int p_length, const uint32_t p_sh1 = h1 * 5 + 0xe6546b64;}- const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);+ const uint8_t *tail = data + nblocks * 4;uint32_t k1 = 0;