Skip to content

Commit 64d40b4

Browse files
committed
Fix memory corruption and enable sanitizer for debug debugrelease
1 parent 6d84578 commit 64d40b4

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

interface/core/memory/memory.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ namespace hud
538538
return set_memory(buffer, buffer_size * sizeof(type_t), value);
539539
}
540540

541+
private:
541542
/**
542543
* Sets the first `size` bytes of the block of memory pointed to by `destination` to the specified `value`.
543544
* This safe version ensures that the operation is not removed by the compiler due to optimization.
@@ -561,6 +562,7 @@ namespace hud
561562
// return destination;
562563
}
563564

565+
public:
564566
/**
565567
* Sets the first `size` bytes of the block of memory pointed to by `destination` to the specified `value`.
566568
* This safe version ensures that the operation is not removed by the compiler due to optimization.
@@ -715,19 +717,6 @@ namespace hud
715717
return static_cast<type_t *>(set_memory_zero(buffer, buffer_size * sizeof(type_t)));
716718
}
717719

718-
/**
719-
* Sets the first `size` bytes of the block of memory pointed by `destination` to zero.
720-
* This safe version ensures that the operation is not removed by the compiler due to optimization.
721-
* `destination` is volatile to prevent memset to be remove by the compiler optimisation
722-
* @param destination Pointer to the buffer. Must not be null.
723-
* @param size Number of bytes to set to zero
724-
* @return Pointer to the buffer `destination`.
725-
*/
726-
static HD_FORCEINLINE void *set_memory_zero_safe(void *destination, const usize size) noexcept
727-
{
728-
return set_memory_safe(destination, size, 0);
729-
}
730-
731720
/**
732721
* Sets the first `size` bytes of the block of memory pointed by `destination` to zero.
733722
* This safe version ensures that the operation is not removed by the compiler due to optimization.
@@ -751,7 +740,7 @@ namespace hud
751740
* @return Pointer to the buffer `destination`.
752741
*/
753742
template<typename type_t>
754-
requires(!hud::is_integral_v<type_t> && !hud::is_pointer_v<type_t>)
743+
requires(!hud::is_integral_v<type_t> && !hud::is_pointer_v<type_t> && hud::is_trivially_copyable_v<type_t>)
755744
static HD_FORCEINLINE type_t *set_memory_zero_safe(type_t *destination, const usize size) noexcept
756745
{
757746
return set_memory_safe(destination, size, 0);
@@ -782,7 +771,7 @@ namespace hud
782771
// LCOV_EXCL_STOP
783772
else
784773
{
785-
return static_cast<type_t *>(set_memory_safe(static_cast<void *>(destination), size, 0));
774+
return static_cast<type_t *>(set_memory_safe(destination, size, 0));
786775
}
787776
}
788777

@@ -811,7 +800,7 @@ namespace hud
811800
// LCOV_EXCL_STOP
812801
else
813802
{
814-
return static_cast<type_t *>(set_memory_zero_safe(static_cast<void *>(destination), size));
803+
return static_cast<type_t *>(set_memory_safe(destination, size, 0));
815804
}
816805
}
817806

@@ -825,9 +814,10 @@ namespace hud
825814
* @return Pointer to the `buffer`.
826815
*/
827816
template<typename type_t, usize buffer_size>
817+
requires(hud::is_trivially_copyable_v<type_t>)
828818
static HD_FORCEINLINE constexpr type_t *set_memory_zero_safe(type_t (&buffer)[buffer_size]) noexcept
829819
{
830-
return static_cast<type_t *>(set_memory_zero_safe(buffer, buffer_size * sizeof(type_t)));
820+
return static_cast<type_t *>(set_memory_safe(buffer, buffer_size * sizeof(type_t), 0));
831821
}
832822

833823
/**

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ else()
441441
add_test(NAME ${lib_name} COMMAND ${test_project_name} --gtest_output=xml:${test_project_name}_report.xml --extra-verbose --gtest_break_on_failure)
442442
endif()
443443

444-
if(SANITIZER)
444+
if( CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR SANITIZER)
445445
include(../sanitizer.cmake)
446446
enable_sanitizer(${test_project_name} ${lib_name})
447447
endif()

test/memory/memory_set_zero.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,16 @@ GTEST_TEST(memory, set_memory_zero_safe_c_array)
109109

110110
GTEST_TEST(memory, set_memory_zero_safe_object_to_buffer)
111111
{
112-
hud_test::non_bitwise_type buf[3] = {2, 4, 8};
113-
const u8 zero_buffer[sizeof(hud_test::non_bitwise_type) * 3] = {0};
114-
hud::memory::set_memory_zero_safe(buf, 3 * sizeof(hud_test::non_bitwise_type));
115-
hud_assert_true(hud::memory::is_memory_compare_equal(buf, zero_buffer, sizeof(hud_test::non_bitwise_type) * 3));
112+
struct bitwise
113+
{
114+
u32 a;
115+
u8 b;
116+
f32 c;
117+
};
118+
119+
bitwise buf[3];
120+
121+
const u8 zero_buffer[sizeof(bitwise) * 3] = {0};
122+
hud::memory::set_memory_zero_safe(buf);
123+
hud_assert_true(hud::memory::is_memory_compare_equal(buf, zero_buffer, sizeof(bitwise) * 3));
116124
}

0 commit comments

Comments
 (0)