Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20200203235216.GB23985@port70.net>
Date: Tue, 4 Feb 2020 00:52:16 +0100
From: Szabolcs Nagy <nsz@...t70.net>
To: musl@...ts.openwall.com, Simon <simonhf@...il.com>
Subject: Re: Why does musl printf() use so much more stack than other
 implementations when printf()ing floating point numbers?

* Szabolcs Nagy <nsz@...t70.net> [2020-02-04 00:05:35 +0100]:
> glibc uses variable amount of stack and it can be big, so
> there is a check and then an alloca falls back to malloc.
> (so yes it can probably fail with oom and not as-safe).
> 
> the alloca threshold is 64k, i don't know if printf can
> actually hit that (there are multiple allocas in printf,
> some have smaller bounds).

ok i was curious, it seems glibc allocates a temp
buf of the size of the output assuming wchar_t,
i.e. unbounded based on user input, and this
allocations can fall back to malloc.

otherwise glibc should allocate around the same
stack as musl (i.e. 9K), so the glibc worst case
stack usage is about 64K+9K and it may do an
arbitrary large malloc instead of the large alloca.

tested with

 sprintf(s, "%.99999Lf\n", 0x1p-16445L);

on x86_64 glibc 2.29 with gdb, this does 3 mallocs
of size 100031, 400012, 100004, so about 600K, and
uses about 9K stack. (i dont know why there are 2
100k mallocs)

musl mallocs 0K and uses < 9K stack.

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.