Stack dimension is invisible in C and the outcomes on “portability”
September 27, 2021
A cramped unbiased now not too long ago I study Ariadne Conill’s working out thread
stack sizes and how alpine is varied
(by capacity of),
which is in section about how Alpine Linux
has an extraordinarily low default thread stack dimension, not like varied issues, and
this can sigh off program crashes. As section of this, Conill says:
In same outdated, it’s some distance my belief that in case your program is crashing on
Alpine, it’s some distance because your program is dependent on conduct that is
now not guaranteed to genuinely exist, which methodology your program is now not
genuinely portable. When it involves this manufacture of dependency, the
typical pickle has to take care of the thread stack dimension limit.
Conill additionally variety of calls out glibc-based mostly thoroughly Linux for having by some distance
the largest default thread stack dimension at 8 MiB, and says:
[…] This ends in crashes in code which assumes a pudgy 8MiB is
on the market for every thread to use.
The life like concern with this take into fable is that stack dimension is invisible
in C, and particularly or now not it’s now not section of the portable C API and customarily
now not section of both the platform API or ABI. No longer like malloc()
, which
can at the very least formally fail, the stack is magic; your code can
neither guard against hitting its dimension limit nor check its limits
in any portable manner. Nor are you able to portably measure how much stack
dimension you are utilizing or resolve how much stack dimension it could merely require
to name library gains (right here’s section of how the C library API
is below-specified).
If a limitation exists but its exact parameters are invisible to
you, running into it (and crashing) would not procure your program “now not
genuinely portable” in any pejorative sense, it makes it unhappy.
That your program would not bustle in some exiguous environments is probably going
now not very preferrred but it’s now not particularly your fault.
Furthermore, since there isn’t any (cheap) manner to take a look at or mitigate stack
dimension issues in C, all that both programmers and library implementers
can reasonably attain is feature by superstition and supposition. In
light of this, glibc’s resolution to use a huge default thread stack
dimension is thoroughly cheap; or now not it’s unbiased in regards to the most web various,
particularly since glibc makes it the same because the same outdated default program
stack dimension. Making an try to limit stack space usage without the instruments
to measure it’s some distance likely now not as abominable as attempting to optimize
your code without doing performance sorting out, but or now not it’s doubtlessly now not
going to yield genuinely factual outcomes both.
Any other folks would admire C programmers to be efficient (ie exiguous) in
their use of stack space. Moreover the rest I’d feel about
this, I’ll inform that or now not it could be primary for folks to be capable of measure
and video show the rest that you genuinely desire them to be efficient with. In case you
desire me to diminish my code’s vitality usage but manufacture now not present me with
instruments to measure that, you are now doubtlessly not to procure much in discover (and
what I attain without dimension could merely procure it worse).
(Technically speaking or now not it’s that you would imagine to assess and measure stack
dimension usage of C code if you happen to strive onerous enough. As an instance, you would
personal a mountainous take a look at suite and conduct binary searches to salvage out
at what thread stack dimension your code starts to wreck below take a look at.
Program prognosis tactics could merely additionally be tempting, but be aware that
your platform C library doubtlessly would not personal any allege stack
usage promises.)