| Summary: | Improve error codes from alloc functions under memcheck | ||
|---|---|---|---|
| Product: | [Developer tools] valgrind | Reporter: | Paul Floyd <pjfloyd> |
| Component: | memcheck | Assignee: | Julian Seward <jseward> |
| Status: | RESOLVED FIXED | ||
| Severity: | normal | CC: | tom |
| Priority: | NOR | ||
| Version First Reported In: | 3.19 GIT | ||
| Target Milestone: | --- | ||
| Platform: | Compiled Sources | ||
| OS: | FreeBSD | ||
| Latest Commit: | Version Fixed/Implemented In: | ||
| Sentry Crash Report: | |||
| Attachments: |
more errno for allocs
Updated patch |
||
|
Description
Paul Floyd
2021-12-09 21:15:59 UTC
On Linux aligned_alloc accepts an alignment of 0 and an alignment of 40 (i.e., not a power of 2). So the man page is wrong. In fact everything that the man page says about errors concerning alignment/size is wrong. Looking here https://elixir.bootlin.com/glibc/latest/source/malloc/malloc.c#L3416 If the alignment is less than MALLOC_ALIGNMENT (which is 16 for i386 and the max(2*sizeof(size_t), sizeof(long double)) on other platforms) then it just calls the malloc implementation, no error. Not in the man page but in the source there is a check that alignment is not over half the max value of size_t. That causes EINVAL. Lastly if the alignment is not a power of 2 it gets bumped up to the next power of 2, no error. There is no check that size is a multiple of alignment. What a nightmare. I think that's only true for memalign (which the manual page says is obsolete) and aligned_alloc (which appears to be an alias for memalign even the manual page says it has an extra restriction) though, and posix_memalign does enforce power of two and a multiple of sizeof(void *) as the manual page says: https://elixir.bootlin.com/glibc/latest/source/malloc/malloc.c#L5553 Yes, posix_memalign looks a lot better. For aligned_alloc I need to dig through the jemalloc code (used on FreeBSD). It's much more factorized which makes it fairly hard to follow. Created attachment 144458 [details]
Updated patch
Added as much as possible to the comment and added Solaris.
The big question is should I keep the conditon that size % align == 0 ? The man pages seem to say it is not allowed / UB but none complain or set EINVAL.
Also either we need to make the same checks in memalign as in aligned_alloc or else disable the aligned_alloc tests for Linux in the testcase I pushed the changes, but only for FreeBSD and Solaris. I'll leave the Linux decision to Mark. |