> you will hit UB Thanks for the information, but: 1. As we have discussed in other emails, we do not use malloc_usage_size as such. 2. This is most likely a problem with gcc's corresponding checking mechanism, rather than using glibc's malloc_usable_size() in this way, see: https://gcc.godbolt.org/z/qhqheTqcz -- Best Regards BaiYang baiyang@gmail.com http://i.baiy.cn **** < END OF EMAIL > **** From: Joakim Sindholt Date: 2022-09-20 04:17 To: musl Subject: Re: [musl] The heap memory performance (malloc/free/realloc) is significantly degraded in musl 1.2 (compared to 1.1) On Tue, 20 Sep 2022 03:45:35 +0800, baiyang wrote: > > The only correct value malloc_usable_size can return is the value you passed to the allocator. > > I don't think so, see: > > Linux man page: https://man7.org/linux/man-pages/man3/malloc_usable_size.3.html - "The value returned by malloc_usable_size() may be **greater than** the requested size of the allocation". > > Mac OS X man page: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/malloc_size.3.html - "The memory block size is always at least as large as the allocation it backs, **and may be larger**." > > FreeBSD man page: https://www.freebsd.org/cgi/man.cgi?query=malloc_usable_size&apropos=0&sektion=0&manpath=FreeBSD+7.1-RELEASE&format=html - "The return value **may be larger** than the size that was requested during allocation". > > These official man pages clearly state that the return value of malloc_usable_size is the size of the memory block allocated internally, not the size submitted by the user. > > Instead, we didn't find any documentation saying that the return value of malloc_usable_size must be the size submitted by the user to be correct. Please correct me if you have the relevant documentation. It's not that malloc_usable_size must return the size originally submitted by the user but that if it doesn't and you take that as an invitation to exceed the original size allocated you will hit UB. Simple case: https://gcc.godbolt.org/z/5E65rr95W Real world example: https://github.com/systemd/systemd/issues/22801 And the reason why is pretty simple: http://port70.net/~nsz/c/c11/n1570.html#7.22.3.4p2 > The malloc function allocates space for an object whose size is > specified by size and whose value is indeterminate.