]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Bug 466762 - Add redirs for C23 free_sized() and free_aligned_sized()
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sat, 17 Feb 2024 10:56:32 +0000 (11:56 +0100)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sat, 17 Feb 2024 11:00:09 +0000 (12:00 +0100)
No testcase for the moment - I still need to link with a non-system
to be able to test

NEWS
configure.ac
coregrind/m_replacemalloc/vg_replace_malloc.c
include/pub_tool_replacemalloc.h
memcheck/mc_main.c

diff --git a/NEWS b/NEWS
index c7fe3df93662b0a84fb87f86cf0f53c733a2fb22..f0ec723c2869ca41ebd6e314a5ca58dc91afe1d7 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -32,6 +32,7 @@ are not entered into bugzilla tend to get forgotten about or ignored.
 437790  valgrind reports "Conditional jump or move depends on uninitialised
         value" in memchr of macOS 10.12-10.15
 460616  disInstr(arm64): unhandled instruction 0x4E819402 (dotprod/ASIMDDP)
+466762  Add redirs for C23 free_sized() and free_aligned_sized()
 466884  Missing writev uninit padding suppression for _XSend
 471036  disInstr_AMD64: disInstr miscalculated next %rip on RORX imm8, m32/64, r32/6
 475498  Add reallocarray wrapper
index 2aa11f29fdd67275cb6afc119550c37bbe42647d..858405ef67cd395de795992061acac477e2b5171 100755 (executable)
@@ -4968,7 +4968,8 @@ AC_CHECK_FUNCS([     \
         memrchr      \
         strndup      \
         close_range  \
-        wcsncpy
+        wcsncpy      \
+        free_aligned_sized
         ])
 
 # AC_CHECK_LIB adds any library found to the variable LIBS, and links these
@@ -5006,6 +5007,8 @@ AM_CONDITIONAL([HAVE_STRLCAT],
                [test x$ac_cv_func_strlcat = xyes])
 AM_CONDITIONAL([HAVE_STRLCPY],
                [test x$ac_cv_func_strlcpy = xyes])
+AM_CONDITIONAL([HAVE_FREE_ALIGNED_SIZED],
+               [test x$ac_cv_func_free_aligned_sized = xyes])
 
 if test x$VGCONF_PLATFORM_PRI_CAPS = xMIPS32_LINUX \
      -o x$VGCONF_PLATFORM_PRI_CAPS = xMIPS64_LINUX \
index 1e6764aee910184042d86a9b9e9fbea9cf2ddc4d..c8f93bc42df4c31dc77b4e7eb4b149f0beeb4aa5 100644 (file)
@@ -83,6 +83,8 @@
    10030 ALLOC_or_BOMB
    10040 ZONEFREE
    10050 FREE
+   10051 FREE_SIZED
+   10052 FREE_ALIGNED_SIZED
    10060 ZONECALLOC
    10070 CALLOC
    10080 ZONEREALLOC
@@ -1003,7 +1005,83 @@ extern int * __error(void) __attribute__((weak));
 
 #endif
 
+ /*------------------- free_sized -------------------*/
 
+ /* Generate a replacement for 'fnname' in object 'soname', which calls
+    'vg_replacement' to free previously allocated memory.
+ */
+
+#define FREE_SIZED(soname, fnname, vg_replacement, tag) \
+ \
+    void VG_REPLACE_FUNCTION_EZU(10051,soname,fnname) (void *p, SizeT size); \
+    void VG_REPLACE_FUNCTION_EZU(10051,soname,fnname) (void *p, SizeT size)  \
+ { \
+       struct AlignedAllocInfo aligned_alloc_info = { .size=size, .mem=p, .alloc_kind=AllocKind##tag }; \
+       \
+       DO_INIT; \
+       TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED((UWord)size); \
+       VERIFY_ALIGNMENT(&aligned_alloc_info); \
+       MALLOC_TRACE(#fnname "(%p)\n", p ); \
+       if (p == NULL)  \
+       return; \
+       (void)VALGRIND_NON_SIMD_CALL1( info.tl_##vg_replacement, p ); \
+ }
+
+
+#if defined(VGO_linux)
+ FREE_SIZED(VG_Z_LIBC_SONAME,       free_sized,                 free, FreeSized );
+ FREE_SIZED(SO_SYN_MALLOC,          free_sized,                 free, FreeSized );
+
+#elif defined(VGO_freebsd)
+ FREE_SIZED(VG_Z_LIBC_SONAME,       free_sized,                 free, FreeSized );
+ FREE_SIZED(SO_SYN_MALLOC,          free_sized,                 free, FreeSized );
+
+#elif defined(VGO_darwin)
+ FREE_SIZED(VG_Z_LIBC_SONAME,       free_sized,                 free, FreeSized );
+ FREE_SIZED(SO_SYN_MALLOC,          free_sized,                 free, FreeSized );
+
+#elif defined(VGO_solaris)
+ FREE_SIZED(VG_Z_LIBC_SONAME,       free_sized,                 free, FreeSized );
+ FREE_SIZED(SO_SYN_MALLOC,          free_sized,                 free, FreeSized );
+
+#endif
+
+
+ /*--------------- free_aligned_sized ---------------*/
+
+ /* Generate a replacement for 'fnname' in object 'soname', which calls
+    'vg_replacement' to free previously allocated memory.
+ */
+
+#define FREE_ALIGNED_SIZED(soname, fnname, vg_replacement, tag) \
+ \
+    void VG_REPLACE_FUNCTION_EZU(10052,soname,fnname) (void *p, SizeT alignment, SizeT size); \
+    void VG_REPLACE_FUNCTION_EZU(10052,soname,fnname) (void *p, SizeT alignment, SizeT size)  \
+ { \
+       struct AlignedAllocInfo aligned_alloc_info = { .orig_alignment=alignment, .size=size, .mem=p, .alloc_kind=AllocKind##tag }; \
+       \
+       DO_INIT; \
+       TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED((UWord)alignment); \
+       TRIGGER_MEMCHECK_ERROR_IF_UNDEFINED((UWord)size); \
+       VERIFY_ALIGNMENT(&aligned_alloc_info); \
+       MALLOC_TRACE(#fnname "(%p)\n", p ); \
+       if (p == NULL)  \
+       return; \
+       (void)VALGRIND_NON_SIMD_CALL1( info.tl_##vg_replacement, p ); \
+ }
+
+
+#if defined(VGO_linux)
+
+#elif defined(VGO_freebsd)
+ FREE_ALIGNED_SIZED(VG_Z_LIBC_SONAME,       free_aligned_sized,                 free, FreeAlignedSized );
+ FREE_ALIGNED_SIZED(SO_SYN_MALLOC,          free_aligned_sized,                 free, FreeAlignedSized );
+
+#elif defined(VGO_darwin)
+
+#elif defined(VGO_solaris)
+
+#endif
 /*---------------------- cfree ----------------------*/
 
 // cfree
index 4bddaa89433c97d669707f3f8a05850091fb507d..914f9a823c99d09f02da10711d297acdc3078126 100644 (file)
@@ -103,7 +103,9 @@ typedef enum {
    AllocKindDeleteAligned,
    AllocKindVecDeleteAligned,
    AllocKindDeleteSizedAligned,
-   AllocKindVecDeleteSizedAligned
+   AllocKindVecDeleteSizedAligned,
+   AllocKindFreeSized,
+   AllocKindFreeAlignedSized
 } AlignedAllocKind;
 
 struct AlignedAllocInfo {
index ea5637e561889d56ef3a219c960188298bd774a6..ba8ff34c523ce3243557de625131bb81c1850c58 100644 (file)
@@ -7272,6 +7272,32 @@ static Bool mc_handle_client_request ( ThreadId tid, UWord* arg, UWord* ret )
             MC_(record_size_mismatch_error) ( tid, mc, aligned_alloc_info->size, "new[][/delete[]" );
          }
          break;
+      case AllocKindFreeSized:
+         mc = VG_(HT_lookup) ( MC_(malloc_list), (UWord)aligned_alloc_info->mem );
+         if (mc && mc->szB != aligned_alloc_info->size) {
+            MC_(record_size_mismatch_error) ( tid, mc, aligned_alloc_info->size, "aligned_alloc/free_sized" );
+         }
+         break;
+      case AllocKindFreeAlignedSized:
+         // same alignment checks as aligned_alloc
+         if ((aligned_alloc_info->orig_alignment & (aligned_alloc_info->orig_alignment - 1)) != 0) {
+            MC_(record_bad_alignment) ( tid, aligned_alloc_info->orig_alignment , 0U, " (should be a power of 2)" );
+         }
+         if (aligned_alloc_info->orig_alignment &&
+             aligned_alloc_info->size % aligned_alloc_info->orig_alignment != 0U) {
+            MC_(record_bad_alignment) ( tid, aligned_alloc_info->orig_alignment , aligned_alloc_info->size, " (size should be a multiple of alignment)" );
+         }
+         if (aligned_alloc_info->size == 0) {
+            MC_(record_bad_size) ( tid, aligned_alloc_info->size, "free_aligned_sized()" );
+         }
+         mc = VG_(HT_lookup) ( MC_(malloc_list), (UWord)aligned_alloc_info->mem );
+         if (mc && aligned_alloc_info->orig_alignment != mc->alignB) {
+            MC_(record_align_mismatch_error) ( tid, mc, aligned_alloc_info->orig_alignment, False, "aligned_alloc/free_aligned_sized");
+         }
+         if (mc && mc->szB != aligned_alloc_info->size) {
+            MC_(record_size_mismatch_error) ( tid, mc, aligned_alloc_info->size, "aligned_alloc/free_aligned_sized" );
+         }
+         break;
       case AllocKindNewAligned:
          if (aligned_alloc_info->orig_alignment == 0 ||
              (aligned_alloc_info->orig_alignment & (aligned_alloc_info->orig_alignment - 1)) != 0) {