mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: "J. Hanne" <musl@jf.hanne.name>
Cc: musl@lists.openwall.com
Subject: Re: [musl] CMSG_LEN macro
Date: Fri, 3 Sep 2021 09:12:13 -0400	[thread overview]
Message-ID: <20210903131212.GE13220@brightrain.aerifal.cx> (raw)
In-Reply-To: <20210903101352.379FC2FC06B2@dd11108.kasserver.com>

On Fri, Sep 03, 2021 at 12:13:52PM +0200, J. Hanne wrote:
> Hi,
> 
> can somebody enlighten me on the purpose of "CMSG_ALIGN (sizeof
> (struct cmsghdr))" in
> 
> #define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
> 
> of https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h?
> 
> CMSG_ALIGN seems to round up to a multiple of sizeof(size_t) - e.g.
> to a multiple of 4 on x86/arm and to a multiple of 8 on
> x86_64/aarch64?
> 
> Given struct cmsghdr, which has a size of 16 bytes on all 4
> mentioned archs, I already wonder if this has an effect on any
> real-world architecture.

I believe you're correct that it's not actually needed to define
CMSG_LEN, and in some sense "couldn't be needed". It is documented
(very sparsely) by the Linux man page for cmsg, and so the purpose of
having it available to applications is just meeing that part of the
(extended) API. It would be interesting to look at what (potentially
wrong) things applications are using it for.

> But more importantly, I wonder *what* exactly is supposed to being
> aligned here:
> 
> - Shall it put some padding *before* struct cmsghdr? That doesn't
>   seem to make sense as the result of CMSG_LEN() goes into the
>   cmsg_len member of struct cmsg, so it would seem strange to me to
>   include bytes preceding the struct in its length field.

> - Shall it put some padding *after* struct cmsghdr? In this case,
>   CMSG_DATA would be wrong as it puts the data directly behind
>   struct cmsghdr without any padding between.

Conceptually any padding needed for alignment is after the cmsghdr. If
the padding were nonzero length then our definition of CMSG_DATA would
be wrong. But the application can't know this; that's why it has to
use the CMSG_* macros.

I think it may make sense to remove all internal use of CMSG_ALIGN
since it's nonstandard and a no-op where it's being used now. Then it
could be left just for applications to use to align their data sizes
or whatever.

Anyone else have thoughts on this?

Rich

  reply	other threads:[~2021-09-03 13:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-03 10:13 J. Hanne
2021-09-03 13:12 ` Rich Felker [this message]
2021-09-05 17:27   ` Markus Wichmann
2021-10-03  8:14     ` J. Hanne
2021-10-03 13:35       ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210903131212.GE13220@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@jf.hanne.name \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).