Best Regards BaiYang baiyang@gmail.com http://i.baiy.cn |
From: Joakim SindholtDate: 2022-09-20 04:17To: muslSubject: 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 <baiyang@gmail.com> 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 originallysubmitted by the user but that if it doesn't and you take that as aninvitation to exceed the original size allocated you will hit UB.Simple case:https://gcc.godbolt.org/z/5E65rr95WReal world example:https://github.com/systemd/systemd/issues/22801And 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.