public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: dbgbgtf <dudududumaxver@gmail.com>
To: libc-alpha@sourceware.org
Subject: [PATCH] malloc: check tcache mem size in tcache_get_n to avoid arbitrary mem allocation
Date: Thu,  3 Apr 2025 19:16:59 +0800	[thread overview]
Message-ID: <20250403111659.7687-1-dudududuMaxVer@gmail.com> (raw)
In-Reply-To: <20250401030056.19142-1-dudududuMaxVer@gmail.com>

Sorry for the earlier emails, I didn't know the rules and open too many
email threads, I will reply to this email in the future.

Get the request size by tc_idx, and then compare request size with the
mem size.

Also remove the align and size check when fastbin stashing into
tcachebin, since the tcache has these two checks already.

In bench-malloc-thread, my patch will be 3% slower.
In bench-malloc-simple-4096, my patch will be 0.5% slower.
So i guess that is acceptable?

My patch result from bench-malloc-thread and bench-malloc-simple-4096

"duration": 3.19901e+11,
"iterations": 1.02021e+10,
"time_per_iteration": 31.3563,
"max_rss": 6476,
"threads": 32,

"thread_arena__allocs_1600_time": 892.826

The result from the lastest branch

"duration": 3.19966e+11,
"iterations": 1.05894e+10,
"time_per_iteration": 30.2158,
"max_rss": 6908,
"threads": 32,

"thread_arena__allocs_1600_time": 887.952

Signed-off-by: dbgbgtf <dudududuMaxVer@gmail.com>
---
 malloc/malloc.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index a0bc733482..3c2754d3d1 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -297,6 +297,9 @@
 /* Only used to pre-fill the tunables.  */
 # define tidx2usize(idx)	(((size_t) idx) * MALLOC_ALIGNMENT + MINSIZE - SIZE_SZ)
 
+/* tc_idx to chunksize */
+# define tidx2csize(idx)    (((size_t) idx) * MALLOC_ALIGNMENT + MINSIZE)
+
 /* When "x" is from chunksize().  */
 # define csize2tidx(x) (((x) - MINSIZE) / MALLOC_ALIGNMENT)
 /* When "x" is a user-provided size.  */
@@ -3186,6 +3189,8 @@ tcache_get_n (size_t tc_idx, tcache_entry **ep)
 
   if (__glibc_unlikely (!aligned_OK (e)))
     malloc_printerr ("malloc(): unaligned tcache chunk detected");
+  if (__glibc_unlikely (tidx2csize(tc_idx) != chunksize (mem2chunk(e))))
+    malloc_printerr ("malloc(): tcache mem size vs request size");
 
   if (ep == &(tcache->entries[tc_idx]))
       *ep = REVEAL_PTR (e->next);
@@ -4009,11 +4014,6 @@ _int_malloc (mstate av, size_t bytes)
 		  while (tcache->counts[tc_idx] < mp_.tcache_count
 			 && (tc_victim = *fb) != NULL)
 		    {
-		      if (__glibc_unlikely (misaligned_chunk (tc_victim)))
-			malloc_printerr ("malloc(): unaligned fastbin chunk detected 3");
-		      size_t victim_tc_idx = csize2tidx (chunksize (tc_victim));
-		      if (__glibc_unlikely (tc_idx != victim_tc_idx))
-			malloc_printerr ("malloc(): chunk size mismatch in fastbin");
 		      if (SINGLE_THREAD_P)
 			*fb = REVEAL_PTR (tc_victim->fd);
 		      else
-- 
2.49.0


  parent reply	other threads:[~2025-04-03 11:18 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-01  3:00 [PATCH] malloc: check tcachebin size when allocating dudududumaxver
2025-04-01  6:23 ` Florian Weimer
2025-04-03 11:47   ` dbgbgtf
2025-04-03 11:16 ` dbgbgtf [this message]
2025-04-03 17:39 [PATCH] malloc: check tcache mem size in tcache_get_n to avoid arbitrary mem allocation Wilco Dijkstra
2025-04-04  4:01 ` dbgbgtf
2025-04-07  9:20   ` Wilco Dijkstra
2025-04-07 14:43     ` dbgbgtf
2025-04-08 17:27       ` Wilco Dijkstra
2025-04-09  8:26         ` dbgbgtf
2025-04-30 15:33           ` Wilco Dijkstra
2025-04-18  8:28         ` dbgbgtf
2025-05-07 14:11 [PATCH] " Wilco Dijkstra
2025-05-07 16:08 ` dbgbgtf
2025-05-07 20:28   ` DJ Delorie
2025-05-08 13:41     ` dbgbgtf
2025-05-08 16:47       ` DJ Delorie
2025-05-10  3:37         ` dbgbgtf
2025-05-12 19:18           ` DJ Delorie
2025-06-15  6:11 ` dbgbgtf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250403111659.7687-1-dudududuMaxVer@gmail.com \
    --to=dudududumaxver@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).