summaryrefslogtreecommitdiff
path: root/malloc
diff options
context:
space:
mode:
authorJustin King <jcking@google.com>2025-10-22 05:51:43 -0700
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2025-11-19 13:47:53 -0300
commit56549264d1e1723dc8ad9675141f316dc83626b3 (patch)
tree9e9b57ea964ca942bcc8dc4d3286dd81a7e87ef5 /malloc
parent4567204feb5dd94b87f3acb6b249acf9de90e573 (diff)
malloc: add free_sized and free_aligned_sized from C23
Signed-off-by: Justin King <jcking@google.com> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'malloc')
-rw-r--r--malloc/Makefile18
-rw-r--r--malloc/Versions8
-rw-r--r--malloc/malloc-debug.c17
-rw-r--r--malloc/malloc.c27
-rw-r--r--malloc/tst-free-aligned-sized-trace.c22
-rw-r--r--malloc/tst-free-aligned-sized.c37
-rw-r--r--malloc/tst-free-sized-trace.c22
-rw-r--r--malloc/tst-free-sized.c37
8 files changed, 188 insertions, 0 deletions
diff --git a/malloc/Makefile b/malloc/Makefile
index cc012e2921..faa3db2602 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -32,7 +32,11 @@ tests := \
tst-aligned-alloc-random-thread-cross \
tst-alloc_buffer \
tst-calloc \
+ tst-free-aligned-sized \
+ tst-free-aligned-sized-trace \
tst-free-errno \
+ tst-free-sized \
+ tst-free-sized-trace \
tst-interpose-nothread \
tst-interpose-thread \
tst-malloc \
@@ -351,6 +355,8 @@ ifneq ($(PERL),no)
tests-special += $(objpfx)tst-mtrace.out
tests-special += $(objpfx)tst-dynarray-mem.out
tests-special += $(objpfx)tst-dynarray-fail-mem.out
+tests-special += $(objpfx)tst-free-aligned-sized-mtrace.out
+tests-special += $(objpfx)tst-free-sized-mtrace.out
endif
endif
endif
@@ -459,3 +465,15 @@ tst-aligned-alloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
tst-aligned-alloc-random-thread-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
tst-aligned-alloc-random-thread-cross-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
tst-malloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
+
+tst-free-aligned-sized-trace-ENV = MALLOC_TRACE=$(objpfx)tst-free-aligned-sized-mem.mtrace \
+ LD_PRELOAD=$(objpfx)libc_malloc_debug.so
+$(objpfx)tst-free-aligned-sized-mtrace.out: $(objpfx)tst-free-aligned-sized-trace.out
+ $(common-objpfx)malloc/mtrace $(objpfx)tst-free-aligned-sized-mem.mtrace > $@; \
+ $(evaluate-test)
+
+tst-free-sized-trace-ENV = MALLOC_TRACE=$(objpfx)tst-free-sized-mem.mtrace \
+ LD_PRELOAD=$(objpfx)libc_malloc_debug.so
+$(objpfx)tst-free-sized-mtrace.out: $(objpfx)tst-free-sized-trace.out
+ $(common-objpfx)malloc/mtrace $(objpfx)tst-free-sized-mem.mtrace > $@; \
+ $(evaluate-test)
diff --git a/malloc/Versions b/malloc/Versions
index c763395c6d..a9ce4a035f 100644
--- a/malloc/Versions
+++ b/malloc/Versions
@@ -67,6 +67,10 @@ libc {
GLIBC_2.33 {
mallinfo2;
}
+ GLIBC_2.43 {
+ free_aligned_sized;
+ free_sized;
+ }
GLIBC_PRIVATE {
# Internal startup hook for libpthread.
__libc_malloc_pthread_startup;
@@ -141,4 +145,8 @@ libc_malloc_debug {
GLIBC_2.33 {
mallinfo2;
}
+ GLIBC_2.43 {
+ free_aligned_sized;
+ free_sized;
+ }
}
diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c
index 15867e23c5..bb420e4190 100644
--- a/malloc/malloc-debug.c
+++ b/malloc/malloc-debug.c
@@ -198,6 +198,21 @@ __debug_free (void *mem)
}
strong_alias (__debug_free, free)
+static void
+__debug_free_sized (void *mem, __attribute_maybe_unused__ size_t size)
+{
+ free (mem);
+}
+strong_alias (__debug_free_sized, free_sized)
+
+static void
+__debug_free_aligned_sized (void *mem, __attribute_maybe_unused__ size_t alignment,
+ __attribute_maybe_unused__ size_t size)
+{
+ free (mem);
+}
+strong_alias (__debug_free_aligned_sized, free_aligned_sized)
+
static void *
__debug_realloc (void *oldmem, size_t bytes)
{
@@ -497,6 +512,8 @@ compat_symbol (libc_malloc_debug, malloc_set_state, malloc_set_state,
compat_symbol (libc_malloc_debug, aligned_alloc, aligned_alloc, GLIBC_2_16);
compat_symbol (libc_malloc_debug, calloc, calloc, GLIBC_2_0);
compat_symbol (libc_malloc_debug, free, free, GLIBC_2_0);
+compat_symbol (libc_malloc_debug, free_aligned_sized, free_aligned_sized, GLIBC_2_43);
+compat_symbol (libc_malloc_debug, free_sized, free_sized, GLIBC_2_43);
compat_symbol (libc_malloc_debug, mallinfo2, mallinfo2, GLIBC_2_33);
compat_symbol (libc_malloc_debug, mallinfo, mallinfo, GLIBC_2_0);
compat_symbol (libc_malloc_debug, malloc_info, malloc_info, GLIBC_2_10);
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 0b21bdf1bd..975a005413 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3729,6 +3729,33 @@ aligned_alloc (size_t alignment, size_t bytes)
return _mid_memalign (alignment, bytes);
}
+/* For ISO C23. */
+void
+weak_function
+free_sized (void *ptr, __attribute_maybe_unused__ size_t size)
+{
+ /* We do not perform validation that size is the same as the original
+ requested size at this time. We leave that to the sanitizers. We
+ simply forward to `free`. This allows existing malloc replacements
+ to continue to work. */
+
+ free (ptr);
+}
+
+/* For ISO C23. */
+void
+weak_function
+free_aligned_sized (void *ptr, __attribute_maybe_unused__ size_t alignment,
+ __attribute_maybe_unused__ size_t size)
+{
+ /* We do not perform validation that size and alignment is the same as
+ the original requested size and alignment at this time. We leave that
+ to the sanitizers. We simply forward to `free`. This allows existing
+ malloc replacements to continue to work. */
+
+ free (ptr);
+}
+
static void *
_mid_memalign (size_t alignment, size_t bytes)
{
diff --git a/malloc/tst-free-aligned-sized-trace.c b/malloc/tst-free-aligned-sized-trace.c
new file mode 100644
index 0000000000..d28e156903
--- /dev/null
+++ b/malloc/tst-free-aligned-sized-trace.c
@@ -0,0 +1,22 @@
+/* Test that free_aligned_sized works.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <mcheck.h>
+
+#define MTRACE mtrace()
+#include "tst-free-aligned-sized.c"
diff --git a/malloc/tst-free-aligned-sized.c b/malloc/tst-free-aligned-sized.c
new file mode 100644
index 0000000000..da15860017
--- /dev/null
+++ b/malloc/tst-free-aligned-sized.c
@@ -0,0 +1,37 @@
+/* Test that free_aligned_sized works.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+static int
+do_test (void)
+{
+#ifdef MTRACE
+ MTRACE;
+#endif
+
+ free_aligned_sized (NULL, 0, 0);
+
+ void *p = aligned_alloc (128, 1024);
+ free_aligned_sized (p, 128, 1024);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/malloc/tst-free-sized-trace.c b/malloc/tst-free-sized-trace.c
new file mode 100644
index 0000000000..1164e1a306
--- /dev/null
+++ b/malloc/tst-free-sized-trace.c
@@ -0,0 +1,22 @@
+/* Test that free_aligned_sized works.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <mcheck.h>
+
+#define MTRACE mtrace()
+#include "tst-free-sized.c"
diff --git a/malloc/tst-free-sized.c b/malloc/tst-free-sized.c
new file mode 100644
index 0000000000..f2f5178918
--- /dev/null
+++ b/malloc/tst-free-sized.c
@@ -0,0 +1,37 @@
+/* Test that free_sized works.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <stddef.h>
+#include <stdlib.h>
+
+static int
+do_test (void)
+{
+#ifdef MTRACE
+ MTRACE;
+#endif
+
+ free_sized (NULL, 0);
+
+ void *p = malloc (128);
+ free_sized (p, 128);
+
+ return 0;
+}
+
+#include <support/test-driver.c>