Stack dimension is invisible in C and the effects on “portability”
September 27, 2021
Seriously lately I study Ariadne Conill’s concept thread
stack sizes and how alpine is diversified
(by the exhaust of),
which is in piece about how Alpine Linux
has a extraordinarily low default thread stack dimension, not like other issues, and
it will location off program crashes. As piece of this, Conill says:
On the whole, it is my concept that in case your program is crashing on
Alpine, it is on legend of your program relies on conduct that is
no longer guaranteed to essentially exist, meaning your program is no longer
if truth be told moveable. In phrases of this roughly dependency, the
traditional mission has to address the thread stack dimension restrict.
Conill also invent of calls out glibc-based mostly completely mostly Linux for having by a ways
the ideal default thread stack dimension at 8 MiB, and says:
[…] This results in crashes in code which assumes a beefy 8MiB is
on hand for every thread to make exhaust of.
The functional self-discipline with this glimpse is that stack dimension is invisible
in C, and especially it’s no longer piece of the moveable C API and on the whole
no longer piece of either the platform API or ABI. Unlike
can no no longer as a lot as officially fail, the stack is magic; your code can
neither guard against hitting its dimension restrict nor take a look at its limits
in any moveable map. Nor are you able to portably measure how valuable stack
dimension you are using or pick how valuable stack dimension it can maybe well also require
to name library capabilities (right here is piece of how the C library API
If a limitation exists but its actual parameters are invisible to
you, running into it (and crashing) does not construct your program “no longer
if truth be told moveable” in any pejorative sense, it makes it unhappy.
That your program does not flee in some restricted environments is perchance
no longer very ideal but it completely is no longer specifically your fault.
Furthermore, since there would possibly be no longer this form of thing as a (sensible) formulation to take a look at or mitigate stack
dimension points in C, all that every and every programmers and library implementers
can reasonably plot is operate by superstition and supposition. In
gentle of this, glibc’s resolution to make exhaust of a huge default thread stack
dimension is completely sensible; it’s comely valuable the safest various,
especially since glibc makes it such as the frequent default program
stack dimension. Attempting to restrict stack set utilization with out the instruments
to measure it can maybe well also very successfully be no longer as harmful as searching to optimize
your code with out doing performance making an try out, but it completely’s perchance no longer
going to yield surely appropriate results either.
Every other folks would love C programmers to be atmosphere friendly (ie restricted) in
their exhaust of stack set. Rather than the relaxation I would possibly perchance feel about
this, I would possibly converse that it is a must-have for folks so that you just can measure
and show screen the relaxation that you just if truth be told desire them to be atmosphere friendly with. If you happen to
desire me to diminish my code’s energy utilization but don’t present me with
instruments to measure that, you are no longer prone to internet valuable in educate (and
what I plot with out size would possibly perchance well also construct it worse).
(Technically talking it’s imaginable to evaluate and measure stack
dimension utilization of C code whenever you strive exhausting ample. To illustrate, it’s doubtless you’ll maybe well perchance
have a huge take a look at suite and conduct binary searches to pick out
at what thread stack dimension your code starts to rupture underneath take a look at.
Program diagnosis ideas would possibly perchance well also moreover be tempting, but take into accout that
your platform C library perchance does not have any scream stack