public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Arsen Arsenović" <arsen@gentoo.org>
To: Alejandro Colomar <alx@kernel.org>
Cc: Xi Ruoyao <xry111@xry111.site>,
	libc-alpha@sourceware.org, Andreas Schwab <schwab@linux-m68k.org>
Subject: Re: free(3) const void *
Date: Fri, 26 Jan 2024 21:04:53 +0100	[thread overview]
Message-ID: <87mssr271g.fsf@gentoo.org> (raw)
In-Reply-To: <ZbP1z1kP13yLcLL6@debian>

Hi Alex,

Alejandro Colomar <alx@kernel.org> writes:

> [[PGP Signed Part:No public key for 9E8C1AFBBEFFDB32 created at 2024-01-26T19:11:27+0100 using RSA]]
> Hi Xi,
>
> On Sat, Jan 27, 2024 at 01:55:48AM +0800, Xi Ruoyao wrote:
>> In C we (not sure about the people, but at least the compiler) cannot
>> rely on it at all.  It's perfectly legal to write something like
>>
>> void
>> stupid (const char *c)
>> {
>>   strcpy ((char *)c, "some bullshit");
>> }
>>
>> int
>> main (void)
>> {
>>   char buf[100];
>>   stupid (buf);
>>   puts (buf);
>> }
>>
>> Yes it's as stupid as the name of the function.  But it does *not*
>> invoke any undefined behavior, and so the compiler is not allowed to do
>> any optimization assuming "stupid" won't change the content in buf.
>>
>> That's why GCC has invented __attribute__ ((access (read_only, ...))).
>> The documentation of this attribute even says we cannot rely on the
>> const qualifier:
>>
>>    The read_only access mode specifies that the pointer to which it
>>    applies is used to read the referenced object but not write to it.
>>    Unless the argument specifying the size of the access denoted by
>>    size-index is zero, the referenced object must be initialized. The
>>    mode implies a stronger guarantee than the const qualifier which,
>>    when cast away from a pointer, does not prevent the pointed-to object
>>    from being modified. Examples of the use of the read_only access mode
>>    is the argument to the puts function, or the second and third
>>    arguments to the memcpy function.
>
> Thanks!
>
> This reminds me of access(none), which is the strongest of the access
> modes that [[gnu::access()]] has.  free(3) could perfectly use
> [[gnu::access(none)]], since it neither needs to read nor write to the
> pointee.  That is stronger than const, so slapping a const there should
> be straight-forward.
>
> Regarding Andreas's consideration that libc functions are a black box:
> free(3) as a black box means that the lifetime of the object is
> terminated at the function boundary, so for the abstract machine,
> free(3) cannot possibly access the object (since there's no object
> any more).  If it does, for implementation-defined reasons, that
> shouldn't be a problem of the caller, and libc should discard const
> internally.

Note that the library (and implementation more broadly) are exempt from
various bits of the standard, as they are expected to be magic.
Notably, the 'lifetime ends at free boundary' rule doesn't mean that
free can't access the object in whichever way it pleases (and, in fact,
free already fails to comply with various other lifetime-related
definitions, to my knowledge).

> Have a lovely night!  =)
> Alex


--
Arsen Arsenović

  reply	other threads:[~2024-01-26 20:07 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-26 13:21 Alejandro Colomar
2024-01-26 14:24 ` Arsen Arsenović
2024-01-26 15:35   ` Alejandro Colomar
2024-01-26 17:22     ` Arsen Arsenović
2024-01-26 17:55       ` Xi Ruoyao
2024-01-26 18:11         ` Alejandro Colomar
2024-01-26 20:04           ` Arsen Arsenović [this message]
2024-01-26 20:07         ` Arsen Arsenović
2024-01-26 17:40     ` Andreas Schwab
2024-01-26 19:45     ` Florian Weimer
2024-01-26 15:13 ` Andreas Schwab
2024-01-26 15:33   ` Alejandro Colomar
2024-01-26 18:09 ` Russ Allbery
2024-01-26 18:23   ` Alejandro Colomar
2024-01-26 18:36     ` Xi Ruoyao
2024-01-26 18:40       ` Alejandro Colomar
2024-01-26 18:49         ` Xi Ruoyao
2024-01-26 18:57           ` Alejandro Colomar
2024-01-26 18:40     ` Russ Allbery
2024-01-26 18:45       ` Alejandro Colomar
2024-01-26 19:41   ` Florian Weimer
2024-01-26 18:39 ` [PATCH] Use [[gnu::access(none)]] on free(3) Alejandro Colomar
2024-01-26 18:41   ` Alejandro Colomar
2024-01-26 21:23     ` Paul Eggert
2024-01-26 23:19       ` Alejandro Colomar
2024-01-27 13:21       ` Cristian Rodríguez
2024-02-13 15:19         ` Gabriel Ravier
2024-02-13 15:28           ` Alejandro Colomar
2024-01-26 21:11 ` free(3) const void * DJ Delorie
2024-01-26 21:30   ` Andreas Schwab
2024-01-26 21:47     ` DJ Delorie
2024-01-26 22:07       ` Andreas Schwab
2024-01-26 23:25       ` Alejandro Colomar

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=87mssr271g.fsf@gentoo.org \
    --to=arsen@gentoo.org \
    --cc=alx@kernel.org \
    --cc=libc-alpha@sourceware.org \
    --cc=schwab@linux-m68k.org \
    --cc=xry111@xry111.site \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).