Improve memory safety

This commit makes numerous refinements to cosmopolitan memory handling.

The default stack size has been reduced from 2mb to 128kb. A new macro
is now provided so you can easily reconfigure the stack size to be any
value you want. Work around the breaking change by adding to your main:

    STATIC_STACK_SIZE(0x00200000);  // 2mb stack

If you're not sure how much stack you need, then you can use:

    STATIC_YOINK("stack_usage_logging");

After which you can `sort -nr o/$MODE/stack.log`. Based on the unit test
suite, nothing in the Cosmopolitan repository (except for Python) needs
a stack size greater than 30kb. There are also new macros for detecting
the size and address of the stack at runtime, e.g. GetStackAddr(). We
also now support sigaltstack() so if you want to see nice looking crash
reports whenever a stack overflow happens, you can put this in main():

    ShowCrashReports();

Under `make MODE=dbg` and `make MODE=asan` the unit testing framework
will now automatically print backtraces of memory allocations when
things like memory leaks happen. Bugs are now fixed in ASAN global
variable overrun detection. The memtrack and asan runtimes also handle
edge cases now. The new tools helped to identify a few memory leaks,
which are fixed by this change.

This change should fix an issue reported in #288 with ARG_MAX limits.
Fixing this doubled the performance of MKDEPS.COM and AR.COM yet again.
This commit is contained in:
Justine Tunney 2021-10-13 17:27:13 -07:00
parent a0b39f886c
commit 226aaf3547
317 changed files with 6474 additions and 3993 deletions

View file

@ -41,12 +41,12 @@
* Modifications:
* 1. Rather than iterating, we use a simple numeric overestimate
* to determine k = floor(log10(d)). We scale relevant
* quantities using O(log2(k)) rather than O(k) multiplications.
* quantities using O(log2(k)) rather than O(k) __gdtoa_multiplications.
* 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
* try to generate digits strictly left to right. Instead, we
* compute with fewer bits and propagate the carry if necessary
* when rounding the final digit up. This is often faster.
* 3. Under the assumption that input will be rounded nearest,
* 3. Under the as__gdtoa_sumption that input will be rounded nearest,
* mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
* That is, we allow equality in stopping tests when the
* round-nearest rule will give the same floating-point value
@ -56,10 +56,10 @@
* quantities.
* 5. When converting floating-point integers less than 1e16,
* we use floating-point arithmetic rather than resorting
* to multiple-precision integers.
* to __gdtoa_multiple-precision integers.
* 6. When asked to produce fewer than 15 digits, we first try
* to get by with floating-point arithmetic; we resort to
* multiple-precision integer arithmetic only if we cannot
* __gdtoa_multiple-precision integer arithmetic only if we cannot
* guarantee that the floating-point calculation has given
* the correctly rounded result. For k requested digits and
* "uniformly" distributed input, the probability is
@ -128,12 +128,12 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
/* Infinity or NaN */
*decpt = 9999;
if (!word1(&d) && !(word0(&d) & 0xfffff))
return nrv_alloc("Infinity", rve, 8);
return nrv_alloc("NaN", rve, 3);
return __gdtoa_nrv_alloc("Infinity", rve, 8);
return __gdtoa_nrv_alloc("NaN", rve, 3);
}
if (!dval(&d)) {
*decpt = 1;
return nrv_alloc("0", rve, 1);
return __gdtoa_nrv_alloc("0", rve, 1);
}
if (Rounding >= 2) {
if (*sign)
@ -142,7 +142,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
if (Rounding != 2)
Rounding = 0;
}
b = d2b(dval(&d), &be, &bbits);
b = __gdtoa_d2b(dval(&d), &be, &bbits);
if (( i = (int)(word0(&d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)) )!=0) {
dval(&d2) = dval(&d);
word0(&d2) &= Frac_mask1;
@ -160,7 +160,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
* We want k to be too large rather than too small.
* The error in the first-order Taylor series approximation
* is in our favor, so we just round up the constant enough
* to compensate for any error in the multiplication of
* to compensate for any error in the __gdtoa_multiplication of
* (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
* and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
* adding 1e-13 to the constant term more than suffices.
@ -187,7 +187,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
k--; /* want k = floor(ds) */
k_check = 1;
if (k >= 0 && k <= Ten_pmax) {
if (dval(&d) < tens[k])
if (dval(&d) < __gdtoa_tens[k])
k--;
k_check = 0;
}
@ -245,7 +245,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
if (i <= 0)
i = 1;
}
s = s0 = rv_alloc(i);
s = s0 = __gdtoa_rv_alloc(i);
if (mode > 1 && Rounding != 1)
leftright = 0;
if (ilim >= 0 && ilim <= Quick_max && try_quick) {
@ -257,27 +257,27 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
ilim0 = ilim;
ieps = 2; /* conservative */
if (k > 0) {
ds = tens[k&0xf];
ds = __gdtoa_tens[k&0xf];
j = k >> 4;
if (j & Bletch) {
/* prevent overflows */
j &= Bletch - 1;
dval(&d) /= bigtens[n_bigtens-1];
dval(&d) /= __gdtoa_bigtens[n___gdtoa_bigtens-1];
ieps++;
}
for(; j; j >>= 1, i++)
if (j & 1) {
ieps++;
ds *= bigtens[i];
ds *= __gdtoa_bigtens[i];
}
dval(&d) /= ds;
}
else if (( j1 = -k )!=0) {
dval(&d) *= tens[j1 & 0xf];
dval(&d) *= __gdtoa_tens[j1 & 0xf];
for(j = j1 >> 4; j; j >>= 1, i++)
if (j & 1) {
ieps++;
dval(&d) *= bigtens[i];
dval(&d) *= __gdtoa_bigtens[i];
}
}
if (k_check && dval(&d) < 1. && ilim > 0) {
@ -303,14 +303,14 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
/* Use Steele & White method of only
* generating digits needed.
*/
dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
dval(&eps) = 0.5/__gdtoa_tens[ilim-1] - dval(&eps);
if (k0 < 0 && j1 >= 307) {
eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
word0(&eps1) -= Exp_msk1 * (Bias+P-1);
dval(&eps1) *= tens[j1 & 0xf];
dval(&eps1) *= __gdtoa_tens[j1 & 0xf];
for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
if (j & 1)
dval(&eps1) *= bigtens[i];
dval(&eps1) *= __gdtoa_bigtens[i];
if (eps.d < eps1.d)
eps.d = eps1.d;
if (10. - d.d < 10.*eps.d && eps.d < 1.) {
@ -336,7 +336,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
}
else {
/* Generate ilim digits, then fix them up. */
dval(&eps) *= tens[ilim-1];
dval(&eps) *= __gdtoa_tens[ilim-1];
for(i = 1;; i++, dval(&d) *= 10.) {
L = (Long)(dval(&d));
if (!(dval(&d) -= L))
@ -361,7 +361,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
/* Do we have a "small" integer? */
if (be >= 0 && k <= Int_max) {
/* Yes. */
ds = tens[k];
ds = __gdtoa_tens[k];
if (ndigits < 0 && ilim <= 0) {
S = mhi = 0;
if (ilim < 0 || dval(&d) <= 5*ds)
@ -409,7 +409,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits;
b2 += i;
s2 += i;
mhi = i2b(1);
mhi = __gdtoa_i2b(1);
}
if (m2 > 0 && s2 > 0) {
i = m2 < s2 ? m2 : s2;
@ -420,20 +420,20 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
if (b5 > 0) {
if (leftright) {
if (m5 > 0) {
mhi = pow5mult(mhi, m5);
b1 = mult(mhi, b);
Bfree(b);
mhi = __gdtoa_pow5mult(mhi, m5);
b1 = __gdtoa_mult(mhi, b);
__gdtoa_Bfree(b);
b = b1;
}
if (( j = b5 - m5 )!=0)
b = pow5mult(b, j);
b = __gdtoa_pow5mult(b, j);
}
else
b = pow5mult(b, b5);
b = __gdtoa_pow5mult(b, b5);
}
S = i2b(1);
S = __gdtoa_i2b(1);
if (s5 > 0)
S = pow5mult(S, s5);
S = __gdtoa_pow5mult(S, s5);
/* Check for special case that d is a normalized power of 2. */
spec_case = 0;
@ -451,7 +451,7 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
* shift left if necessary so divisor has 4 leading 0 bits.
*
* Perhaps we should just compute leading 28 bits of S once
* and for all and pass them and a shift to quorem, so it
* and for all and pass them and a shift to __gdtoa_quorem, so it
* can do shifts and ors to compute the numerator for q.
*/
if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f )!=0)
@ -469,20 +469,20 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
s2 += i;
}
if (b2 > 0)
b = lshift(b, b2);
b = __gdtoa_lshift(b, b2);
if (s2 > 0)
S = lshift(S, s2);
S = __gdtoa_lshift(S, s2);
if (k_check) {
if (cmp(b,S) < 0) {
if (__gdtoa_cmp(b,S) < 0) {
k--;
b = multadd(b, 10, 0); /* we botched the k estimate */
b = __gdtoa_multadd(b, 10, 0); /* we botched the k estimate */
if (leftright)
mhi = multadd(mhi, 10, 0);
mhi = __gdtoa_multadd(mhi, 10, 0);
ilim = ilim1;
}
}
if (ilim <= 0 && (mode == 3 || mode == 5)) {
if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
if (ilim < 0 || __gdtoa_cmp(b,S = __gdtoa_multadd(S,5,0)) <= 0) {
/* no digits, fcvt style */
no_digits:
k = -1 - ndigits;
@ -495,25 +495,25 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
}
if (leftright) {
if (m2 > 0)
mhi = lshift(mhi, m2);
mhi = __gdtoa_lshift(mhi, m2);
/* Compute mlo -- check for special case
* that d is a normalized power of 2.
*/
mlo = mhi;
if (spec_case) {
mhi = Balloc(mhi->k);
mhi = __gdtoa_Balloc(mhi->k);
Bcopy(mhi, mlo);
mhi = lshift(mhi, Log2P);
mhi = __gdtoa_lshift(mhi, Log2P);
}
for(i = 1;;i++) {
dig = quorem(b,S) + '0';
dig = __gdtoa_quorem(b,S) + '0';
/* Do we yet have the shortest decimal string
* that will round to d?
*/
j = cmp(b, mlo);
delta = diff(S, mhi);
j1 = delta->sign ? 1 : cmp(b, delta);
Bfree(delta);
j = __gdtoa_cmp(b, mlo);
delta = __gdtoa_diff(S, mhi);
j1 = delta->sign ? 1 : __gdtoa_cmp(b, delta);
__gdtoa_Bfree(delta);
if (j1 == 0 && mode != 1 && !(word1(&d) & 1) && Rounding >= 1) {
if (dig == '9')
goto round_9_up;
@ -533,8 +533,8 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
case 2: goto keep_dig;
}
if (j1 > 0) {
b = lshift(b, 1);
j1 = cmp(b, S);
b = __gdtoa_lshift(b, 1);
j1 = __gdtoa_cmp(b, S);
if ((j1 > 0 || (j1 == 0 && dig & 1))
&& dig++ == '9')
goto round_9_up;
@ -558,24 +558,24 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
*s++ = dig;
if (i == ilim)
break;
b = multadd(b, 10, 0);
b = __gdtoa_multadd(b, 10, 0);
if (mlo == mhi)
mlo = mhi = multadd(mhi, 10, 0);
mlo = mhi = __gdtoa_multadd(mhi, 10, 0);
else {
mlo = multadd(mlo, 10, 0);
mhi = multadd(mhi, 10, 0);
mlo = __gdtoa_multadd(mlo, 10, 0);
mhi = __gdtoa_multadd(mhi, 10, 0);
}
}
}
else {
for(i = 1;; i++) {
*s++ = dig = quorem(b,S) + '0';
*s++ = dig = __gdtoa_quorem(b,S) + '0';
if (!b->x[0] && b->wds <= 1) {
goto ret;
}
if (i >= ilim)
break;
b = multadd(b, 10, 0);
b = __gdtoa_multadd(b, 10, 0);
}
}
@ -584,8 +584,8 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
case 0: goto trimzeros;
case 2: goto roundoff;
}
b = lshift(b, 1);
j = cmp(b, S);
b = __gdtoa_lshift(b, 1);
j = __gdtoa_cmp(b, S);
if (j > 0 || (j == 0 && dig & 1))
{
roundoff:
@ -603,17 +603,17 @@ dtoa(double d0, int mode, int ndigits, int *decpt, int *sign, char **rve)
s++;
}
ret:
Bfree(S);
__gdtoa_Bfree(S);
if (mhi) {
if (mlo && mlo != mhi)
Bfree(mlo);
Bfree(mhi);
__gdtoa_Bfree(mlo);
__gdtoa_Bfree(mhi);
}
retc:
while(s > s0 && s[-1] == '0')
--s;
ret1:
Bfree(b);
__gdtoa_Bfree(b);
*s = 0;
*decpt = k + 1;
if (rve)