Re: Issues in manpage memalign

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2023-03-10 at 01:35 +0100, Alejandro Colomar via Libc-alpha wrote:
> Hi Paul,
> 
> On 3/7/23 23:24, Floyd, Paul wrote:
> > Hi
> > 
> > Quick bit of background. I'm a Valgrind maintainer and recently I've
> > been working on getting Valgrind to work more like the underlying OS / 
> > libc implementations of memalign, posix_memalign and aligned_alloc.
> > 
> > There are several issues with the manpage for memalign and aligned alloc.
> > 
> > quote:
> > 
> >         The  obsolete  function  memalign()  allocates size bytes and 
> > returns a
> >         pointer to the allocated memory.  The memory address will be a 
> > multiple
> >         of alignment, which must be a power of two.
> > 
> > endquote:
> > 
> > The power if two requirement is false for glibc which silently bumps up 
> > the alignment to the next power of two.
> > 
> > quote:
> > 
> >         The  function aligned_alloc() is the same as memalign(), except 
> > for the
> >         added restriction that size should be a multiple of alignment.
> > 
> > endquote:
> > 
> > This is also false for glibc. In the glibc implementation weak aliases 
> > are used so memalign and aligned_alloc call the same function.

aligned_alloc is in C99, it says (7.22.3.1p2):

"The value of alignment shall be a valid alignment supported by the
implementation and the value of size shall be an integral multiple of
alignment."

In the context of the C standard, "shall" means (section 4 p2):

"If a "shall" or "shall not" requirement that appears outside of a
constraint or runtime-constraint is violated, the behavior is
undefined."

"It seems working" is one of the legal consequences of an undefined
behavior.  "shall" does NOT mean "if you don't obey, the implementation
is required to give some errno to you".

> > quote:
> > 
> > ERRORS
> >         EINVAL The alignment argument was not a power of two, or was not 
> > a mul-
> >                tiple of sizeof(void *).
> > 
> > endquote:
> > 
> > Both of the above only apply to posix_memalign and not to either 
> > memalign or aligned_alloc.
> > 
> > There is a missing EINVAL description. If the alignment is so large that 
> > the allocation will not be possible to satisfy then the call will fail 
> > and set errno to EINVAL.

POSIX says it should be ENOMEM:

[ENOMEM]
There is insufficient memory available with the requested alignment.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html

And it seems also true with Glibc (at least Glibc-2.37):

$ cat t.c
#include <stdlib.h>
#include <stdio.h>

int main()
{
	void *p;
	if (posix_memalign(&p, sizeof(void *) << 55, 1) != 0)
		perror("posix_memalign");
}
$ cc t.c
$ ./a.out
posix_memalign: Cannot allocate memory

I have no idea about memalign() (without posix_) though.


-- 
Xi Ruoyao <xry111@xxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux