public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Cc: 'GNU C Library' <libc-alpha@sourceware.org>,
	Siddhesh Poyarekar <siddhesh@gotplt.org>
Subject: Re: [RFC PATCH] malloc_usable_size.3: Warn about _FORTIFY_SOURCE interaction
Date: Wed, 5 Apr 2023 23:00:19 +0200	[thread overview]
Message-ID: <decb4625-25d1-14ae-cd5a-82b150bc020c@gmail.com> (raw)
In-Reply-To: <PAWPR08MB89825349457A98B9C4614A7983909@PAWPR08MB8982.eurprd08.prod.outlook.com>


[-- Attachment #1.1: Type: text/plain, Size: 2629 bytes --]

Hi Wilco,

On 4/5/23 15:55, Wilco Dijkstra wrote:
> Hi Alejandro,
> 
>> and the header where we define a wrapper macro, which contains several
>> comments about assumptions made about different libc implementations:
>>
>> <https://github.com/nginx/unit/blob/c54331fa3d9597ba6bc85e7b7242981f00ed25c2/src/nxt_malloc.h#L35>
>>
>> I hope that tells you something.  It doesn't tell me anything, but I'm
>> not used to fiddling with allocators.  :)
> 
> This looks rather worrying

Agree.  I've seen all kinds of UB crimes in this codebase.  I'm trying to fix
all that, but it's hard to convince maintainers that lived in a cave for the
last few decades and don't know about C99, strict aliasing rules, or overflow,
to change certain code "if it worked so far" for some definition of "worked".
Of course, it is compiled with -O which is probably what has avoided hell
breaking loose so far (I'm honestly surprised that the performance of nginx is
so high compiling with -O, I must admit).

> - it seems to deliberately use a malloc size that is too
> small in the hope that the particular malloc implementation allocates a bit more
> and then use that extra space.

Yes, that's what it looked to me.  :/

> So every malloc call now needs extra checks to
> adjust the size and another call to malloc_usable_size which then needs to be
> checked to be larger than the original requested size...
> 
> So basically they are trying to save 32 bytes in blocks larger than 128KB
> (a whopping 0.024%!!!) by adding ~64 bytes of extra code per malloc call plus
> lots of extra executed instructions...

I think it was more like avoiding an entire page just for those 32 bytes of
bookkeeping.  So if the bookkeeping would trigger the allocation of a new
page, that is avoided.  It seems to be saving (4K - 32) bytes for blocks
of >128K, so <3% of the size; still a low number, which I wouldn't trade for
extra code and CPU overhead.  Luckily, it seems these macros are not really
being called :)

> 
> This kind of stupidity convinces me even more that we need to obsolete
> malloc_usable_size - people clearly cannot use it properly or avoid
> hardcoding internal implementation details which could change at any time.

I would say that not all of us are like that.  There's still programmers
who respect nasal demons.  However, I have never had a reason to call such
a function, so I'm not going to be the one that asks you to not remove it.

> 
> Cheers,
> Wilco

Cheers,
Alex

-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-04-05 21:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-05 13:55 Wilco Dijkstra
2023-04-05 21:00 ` Alejandro Colomar [this message]
     [not found] <CAD66C+YQKWJQNv2i=8+BuL3Z5NzDQsG-1izhVxZ549xhMTTUjA@mail.gmail.com>
2023-04-04 11:42 ` Siddhesh Poyarekar
2023-04-05  0:51   ` Sam James
2023-04-05  2:35   ` Alejandro Colomar
2023-04-05 12:58     ` Siddhesh Poyarekar

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=decb4625-25d1-14ae-cd5a-82b150bc020c@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=libc-alpha@sourceware.org \
    --cc=siddhesh@gotplt.org \
    /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).