mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: "Jₑₙₛ Gustedt" <jens.gustedt@inria.fr>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [C23 new stdlib 1/4] C23: add the new interfaces free_sized and free_aligned_sized for stdlib.h
Date: Thu, 25 May 2023 08:57:51 -0400	[thread overview]
Message-ID: <20230525125751.GG4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <20230525113806.31744fae@inria.fr>

On Thu, May 25, 2023 at 11:38:06AM +0200, Jₑₙₛ Gustedt wrote:
> 
> on Wed, 24 May 2023 17:31:34 -0400 you (Rich Felker <dalias@libc.org>)
> wrote:
> 
> > These really should be in a separate file or files calling free() not
> > __libc_free, since if free has been replaced, they should call that,
> > not the libc-internal one. (Imagine a program linked or LD_PRELOADed
> > with an alternate malloc implementation that's not C23-aware.)
> 
> ok
> 
> > Optionally, they could also evaluate the predicate to determine if
> > malloc has been replaced, and if not, do the actual check. The
> > alignment check is trivial and malloc-agnostic. The size check would
> > require adding a libc-internal way to access malloc_usable_size. But
> > this can all be done later if desired.
> 
> I don't think these interfaces are about checking consistency. They
> don't provide any reasonable means to return an error. In the contrary
> they are meant to provide more efficient implementations for the
> feature if the correct size is used. Otherwise, the behavior is just
> undefined.

My understanding was that, by having it be UB to call them with wrong
size or alignment, they create an opportunity for the implementation
to harden by trapping on inconsistency. This is a lot more valuable
than whatever marginal performance advantage they might give (which is
none for musl's mallocng or oldmalloc, anyway).

> The first question to be would be if we want to offer that
> functionality for musl's allocator. (I don't feel that I know enough
> to do that.) The second question is, do we want to allow alternate
> malloc implementations to provide replacements for this? Then we
> should perhaps have these new interfaces as weak symbols?

Weak symbols aren't the way to achieve that. They might be ~a way~ in
some cases, but probably not a good one. What we use weak symbols for
in musl is to allow stub implementations for the pattern "B needs to
do something to interact with A if A was linked, but can skip that if
A was not linked" without having B pull in A as a dependency.

For static linking, just having the functions in separate TUs
automatically gives the property of being able to provide a
replacement (but there also has to be a contract to allow replacement;
the malloc subsystem is the only thing for which we have such a
contract, as an extension).

For dynamic linking, weak is irrelevant/ignored, and interposition (by
design) approximates the static linking behavior. Since redefining
standard functions is UB, we link with most symbol references inside
libc.so bound at libc.so link time, so that calls don't have to go
through GOT lookup or PLT. But for malloc, we suppress that via the
dynamic.list file.

Strictly speaking, any new functions added to malloc should be added
to dynamic.list so they're kept interposable. Since we're not planning
to call any of these from within libc, it doesn't make any difference,
but I'd still add them there for completeness and future-proofing.

Rich

  reply	other threads:[~2023-05-25 12:58 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-24 19:38 [musl] [C23 new stdlib 0/4] changes in stdlib.h Jens Gustedt
2023-05-24 19:38 ` [musl] [C23 new stdlib 1/4] C23: add the new interfaces free_sized and free_aligned_sized for stdlib.h Jens Gustedt
2023-05-24 21:31   ` Rich Felker
2023-05-25  9:38     ` Jₑₙₛ Gustedt
2023-05-25 12:57       ` Rich Felker [this message]
2023-05-24 19:38 ` [musl] [C23 new stdlib 2/4] C23: add the memalignment function Jens Gustedt
2023-05-24 21:28   ` Rich Felker
2023-05-24 22:12     ` Rich Felker
2023-05-25  9:17       ` Jₑₙₛ Gustedt
2023-05-25  9:16     ` Jₑₙₛ Gustedt
2023-05-25  9:42       ` NRK
2023-05-25 13:07       ` Rich Felker
2023-05-25 14:05         ` Jₑₙₛ Gustedt
2023-05-25 14:34           ` Rich Felker
2023-05-25 15:22             ` Jₑₙₛ Gustedt
2023-06-01 13:35       ` Alexander Monakov
2023-06-01 19:08         ` Rich Felker
2023-06-02  8:07           ` Jₑₙₛ Gustedt
2023-06-02  8:03         ` Jₑₙₛ Gustedt
2023-06-02  8:37           ` Alexander Monakov
2023-05-24 19:38 ` [musl] [C23 new stdlib 3/4] C23: implement the new strfrom[dfl] functions Jens Gustedt
2023-05-24 21:42   ` Rich Felker
2023-05-24 19:38 ` [musl] [C23 new stdlib 4/4] C23: add the new include guard to stdlib.h Jens Gustedt

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=20230525125751.GG4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=jens.gustedt@inria.fr \
    --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).