mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com, Simon <simonhf@gmail.com>
Subject: Re: [musl] Why does musl printf() use so much more stack than other implementations when printf()ing floating point numbers?
Date: Tue, 4 Feb 2020 00:52:16 +0100	[thread overview]
Message-ID: <20200203235216.GB23985@port70.net> (raw)
In-Reply-To: <20200203230534.GA23985@port70.net>

* Szabolcs Nagy <nsz@port70.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.

      reply	other threads:[~2020-02-03 23:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-27  0:33 [musl] Bug report: Reproduction of seg fault caused by musl thread creation race condition Simon
2020-01-27  5:28 ` Markus Wichmann
2020-01-27 17:51 ` Rich Felker
2020-01-27 19:59   ` Simon
2020-01-27 20:37     ` Szabolcs Nagy
2020-01-27 20:46       ` Rich Felker
2020-02-03 21:14 ` [musl] Why does musl printf() use so much more stack than other implementations when printf()ing floating point numbers? Simon
2020-02-03 21:57   ` Rich Felker
2020-02-03 23:05     ` Szabolcs Nagy
2020-02-03 23:52       ` Szabolcs Nagy [this message]

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=20200203235216.GB23985@port70.net \
    --to=nsz@port70.net \
    --cc=musl@lists.openwall.com \
    --cc=simonhf@gmail.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).