[PATCH] checking tcache mem size when allocating it
dudududumaxver@gmail.com
dudududumaxver@gmail.com
Wed Apr 2 10:41:14 GMT 2025
From: dbgbgtf <dudududuMaxVer@gmail.com>
plz ignore the earlier two email.my bad.
checking if `tcache mem size == request size` to avoid arbitrary mem
allocating from tcache bin
plus, since fastbin will be slash into tcachebin, i remove the check
when fastbin fall into tcachebin, they will be findout anyway(i am not
so sure about this change, so be free to ignore this)
finally, the bench, i think those data will be enough. is that cost
acceptable? i am really not an expert on this.
Signed-off-by: dbgbgtf <dudududuMaxVer@gmail.com>
$ cat now/bench-malloc-simple-4096.out
{
"timing_type": "hp_timing",
"functions": {
"malloc": {
"": {
"malloc_block_size": 4096,
"max_rss": 7492,
"main_arena_st_allocs_0025_time": 50.4246,
"main_arena_st_allocs_0100_time": 1788.27,
"main_arena_st_allocs_0400_time": 2345.23,
"main_arena_st_allocs_1600_time": 2600.93,
"main_arena_mt_allocs_0025_time": 102.663,
"main_arena_mt_allocs_0100_time": 1812.11,
"main_arena_mt_allocs_0400_time": 2394.82,
"main_arena_mt_allocs_1600_time": 2684.38,
"thread_arena__allocs_0025_time": 100.688,
"thread_arena__allocs_0100_time": 1744.47,
"thread_arena__allocs_0400_time": 2341.64,
"thread_arena__allocs_1600_time": 2633.91
}
}
}
}%
$ cat origin/bench-malloc-simple-4096.out
{
"timing_type": "hp_timing",
"functions": {
"malloc": {
"": {
"malloc_block_size": 4096,
"max_rss": 7660,
"main_arena_st_allocs_0025_time": 52.1138,
"main_arena_st_allocs_0100_time": 1743.2,
"main_arena_st_allocs_0400_time": 2336.48,
"main_arena_st_allocs_1600_time": 2584.16,
"main_arena_mt_allocs_0025_time": 98.8657,
"main_arena_mt_allocs_0100_time": 1784.57,
"main_arena_mt_allocs_0400_time": 2377.64,
"main_arena_mt_allocs_1600_time": 2623.67,
"thread_arena__allocs_0025_time": 97.7683,
"thread_arena__allocs_0100_time": 1732.62,
"thread_arena__allocs_0400_time": 2340.24,
"thread_arena__allocs_1600_time": 2608.58
}
}
}
}%
$ cat now/bench-malloc-thread-32.out
{
"timing_type": "hp_timing",
"functions": {
"malloc": {
"": {
"duration": 9.33754e+11,
"iterations": 9.06744e+09,
"time_per_iteration": 102.979,
"max_rss": 6772,
"threads": 32,
"min_size": 4,
"max_size": 32768,
"random_seed": 88
}
}
}
}%
$ cat origin/bench-malloc-thread-32.out
{
"timing_type": "hp_timing",
"functions": {
"malloc": {
"": {
"duration": 9.3371e+11,
"iterations": 1.08938e+10,
"time_per_iteration": 85.7105,
"max_rss": 7072,
"threads": 32,
"min_size": 4,
"max_size": 32768,
"random_seed": 88
}
}
}
}%
---
malloc/malloc.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/malloc/malloc.c b/malloc/malloc.c
index a0bc733482..c8ecebec2c 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3364,7 +3364,12 @@ tcache_try_malloc (size_t bytes, void **memptr)
MAYBE_INIT_TCACHE ();
if (tcache_available (tc_idx))
- *memptr = tcache_get (tc_idx);
+ {
+ *memptr = tcache_get (tc_idx);
+ if (__glibc_unlikely(chunksize(mem2chunk(memptr))) != tbytes)
+ malloc_printerr("calloc(): tcache mem size vs request size");
+ return memptr;
+ }
else
*memptr = NULL;
@@ -3429,10 +3434,16 @@ void *
__libc_malloc (size_t bytes)
{
#if USE_TCACHE
- size_t tc_idx = csize2tidx (checked_request2size (bytes));
+ size_t tbytes = checked_request2size(bytes);
+ size_t tc_idx = csize2tidx (tbytes);
if (tcache_available (tc_idx))
- return tag_new_usable (tcache_get (tc_idx));
+ {
+ void* memptr = tag_new_usable(tcache_get(tc_idx));
+ if (__glibc_unlikely(chunksize(mem2chunk(memptr))) != tbytes)
+ malloc_printerr("malloc(): tcache mem size vs request size");
+ return memptr;
+ }
#endif
return __libc_malloc2 (bytes);
@@ -4009,11 +4020,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
More information about the Libc-alpha
mailing list