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

@ -18,6 +18,7 @@
*/
#include "libc/alg/alg.h"
#include "libc/alg/arraylist.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
@ -42,7 +43,9 @@ TEST(append, worksGreatForScalars) {
char c = 'a';
struct string s;
memset(&s, 0, sizeof(s));
for (size_t i = 0; i < 1024; ++i) ASSERT_EQ(i, append(&s, &c));
for (size_t i = 0; i < 1024; ++i) {
ASSERT_EQ(i, append(&s, &c));
}
ASSERT_EQ(1024, s.i);
for (size_t i = 0; i < s.i; ++i) ASSERT_EQ('a', s.p[i]);
free_s(&s.p);
@ -52,10 +55,14 @@ TEST(append, isGenericallyTyped) {
int c = 0x31337;
struct ArrayListInteger s;
memset(&s, 0, sizeof(s));
for (size_t i = 0; i < 1024; ++i) ASSERT_EQ(i, append(&s, &c));
for (size_t i = 0; i < 1024; ++i) {
ASSERT_EQ(i, append(&s, &c));
}
ASSERT_EQ(1024, s.i);
ASSERT_GT(malloc_usable_size(s.p), 1024 * sizeof(int));
for (size_t i = 0; i < s.i; ++i) ASSERT_EQ(0x31337, s.p[i]);
for (size_t i = 0; i < s.i; ++i) {
ASSERT_EQ(0x31337, s.p[i]);
}
free_s(&s.p);
}

View file

@ -18,22 +18,24 @@
*/
#include "libc/alg/alg.h"
#include "libc/errno.h"
#include "libc/runtime/gc.internal.h"
#include "libc/testlib/testlib.h"
TEST(replacestr, demo) {
EXPECT_STREQ("hello friends", replacestr("hello world", "world", "friends"));
EXPECT_STREQ("bbbbbbbb", replacestr("aaaa", "a", "bb"));
EXPECT_STREQ("hello friends",
gc(replacestr("hello world", "world", "friends")));
EXPECT_STREQ("bbbbbbbb", gc(replacestr("aaaa", "a", "bb")));
}
TEST(replacestr, emptyString) {
EXPECT_STREQ("", replacestr("", "x", "y"));
EXPECT_STREQ("", gc(replacestr("", "x", "y")));
}
TEST(replacestr, emptyNeedle) {
EXPECT_EQ(NULL, replacestr("a", "", "a"));
EXPECT_EQ(NULL, gc(replacestr("a", "", "a")));
EXPECT_EQ(EINVAL, errno);
}
TEST(replacestr, needleInReplacement_doesntExplode) {
EXPECT_STREQ("xxxxxxx", replacestr("x", "x", "xxxxxxx"));
EXPECT_STREQ("xxxxxxx", gc(replacestr("x", "x", "xxxxxxx")));
}

View file

@ -49,6 +49,7 @@ void SetUp(void) {
void TearDown(void) {
CHECK_NE(-1, setenv("PATH", oldpath, true));
free(oldpath);
}
TEST(commandv, testPathSearch) {

View file

@ -63,8 +63,8 @@ TEST(mkntcmdline, basicQuoting) {
TEST(mkntcmdline, testUnicode) {
char *argv1[] = {
strdup("(╯°□°)╯"),
strdup("要依法治国是赞美那些谁是公义的和惩罚恶人。 - 韩非"),
gc(strdup("(╯°□°)╯")),
gc(strdup("要依法治国是赞美那些谁是公义的和惩罚恶人。 - 韩非")),
NULL,
};
EXPECT_NE(-1, mkntcmdline(cmdline, argv1[0], argv1));

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sa.h"

View file

@ -77,3 +77,17 @@ TEST(readansi, testOperatingSystemCommand) {
ASSERT_TRUE(WIFEXITED(ws));
ASSERT_EQ(0, WEXITSTATUS(ws));
}
TEST(readansi, testSqueeze) {
int fds[2];
char b[32];
const char *s = "\e\\";
ASSERT_NE(-1, pipe(fds));
write(fds[1], s, strlen(s));
close(fds[1]);
EXPECT_EQ(strlen(s), readansi(fds[0], b, sizeof(b)));
EXPECT_STREQ(s, b);
EXPECT_EQ(0, readansi(fds[0], b, sizeof(b)));
EXPECT_STREQ("", b);
close(fds[0]);
}

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"

View file

@ -21,6 +21,7 @@
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/runtime/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/sysv/consts/auxv.h"
@ -47,9 +48,9 @@ TEST(writev, test) {
TEST(writev, big_fullCompletion) {
int fd;
char *ba = malloc(2 * 1024 * 1024);
char *bb = malloc(2 * 1024 * 1024);
char *bc = malloc(2 * 1024 * 1024);
char *ba = gc(malloc(2 * 1024 * 1024));
char *bb = gc(malloc(2 * 1024 * 1024));
char *bc = gc(malloc(2 * 1024 * 1024));
struct iovec iov[] = {
{"", 0}, //
{ba, 2 * 1024 * 1024}, //

View file

@ -25,22 +25,21 @@
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/dns/prototxt.h"
#include "libc/calls/calls.h"
#include "libc/dns/dns.h"
#include "libc/dns/ent.h"
#include "libc/dns/prototxt.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
char testlib_enable_tmp_setup_teardown;
void SetUp() {
int fd;
const char* sample = "\
const char *sample = "\
# skip comment string\n\
rspf 73 RSPF CPHB \n\
ggp 3 GGP ";
ASSERT_NE(-1, (fd = creat("protocols", 0755)));
ASSERT_NE(-1, write(fd, sample, strlen(sample)));
ASSERT_NE(-1, close(fd));
@ -49,15 +48,12 @@ ggp 3 GGP ";
TEST(LookupProtoByNumber, GetNameWhenNumberCorrect) {
char name[16]; /* sample has only names of length 3-4 */
strcpy(name, "");
ASSERT_EQ(-1, /*non-existent number */
LookupProtoByNumber(24, name, sizeof(name), "protocols"));
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
LookupProtoByNumber(73, name, 1, "protocols"));
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
ASSERT_EQ(0, /* works with valid number */
ASSERT_EQ(0, /* works with valid number */
LookupProtoByNumber(73, name, sizeof(name), "protocols"));
ASSERT_STREQ(name, "rspf"); /* official name written */
}
@ -65,18 +61,14 @@ TEST(LookupProtoByNumber, GetNameWhenNumberCorrect) {
TEST(LookupProtoByName, GetNumberWhenNameOrAlias) {
char name[16]; /* sample has only names of length 3-4 */
strcpy(name, "");
ASSERT_EQ(-1, /* non-existent name or alias */
LookupProtoByName("tcp", name, sizeof(name), "protocols"));
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
LookupProtoByName("ggp", name, 1, "protocols"));
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
ASSERT_EQ(3, /* works with valid name */
ASSERT_EQ(3, /* works with valid name */
LookupProtoByName("ggp", name, sizeof(name), "protocols"));
ASSERT_STREQ(name, "ggp");
ASSERT_EQ(73, /* works with valid alias */
LookupProtoByName("CPHB", name, sizeof(name), "protocols"));
ASSERT_STREQ(name, "rspf"); /* official name written */

View file

@ -25,6 +25,7 @@
TEST(atoi, test) {
EXPECT_EQ(0, atoi(""));
EXPECT_EQ(0, atoi("-b"));
EXPECT_EQ(0, atoi("0"));
EXPECT_EQ(1, atoi("1"));
EXPECT_EQ(9, atoi("9"));

View file

@ -18,6 +18,7 @@
*/
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/testlib/testlib.h"

View file

@ -23,7 +23,8 @@
TEST(fcvt, test) {
int decpt, sign;
ASSERT_STREQ("3.14159265358979", xasprintf("%.14f", 3.14159265358979323846));
ASSERT_STREQ("3.14159265358979",
gc(xasprintf("%.14f", 3.14159265358979323846)));
ASSERT_STREQ("3141592653589793",
fcvt(3.14159265358979323846, 15, &decpt, &sign));
ASSERT_EQ(1, decpt);

View file

@ -18,6 +18,7 @@
*/
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/math.h"
#include "libc/runtime/gc.internal.h"
#include "libc/str/str.h"

View file

@ -16,8 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/fmt/itoa.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"

View file

@ -0,0 +1,102 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/itoa.h"
#include "libc/limits.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(LengthUint64Thousands, test) {
int i;
char s[27];
static const uint64_t v[] = {
9,
10,
11,
99,
100,
101,
999,
1000,
1001,
999999,
1000000,
1000001,
UINT64_MIN,
UINT64_MAX,
UINT64_MIN + 1,
UINT64_MAX - 1,
0x8000000000000001ull,
0x8000000000000000ull,
};
for (i = 0; i < ARRAYLEN(v); ++i) {
FormatUint64Thousands(s, v[i]);
ASSERT_EQ(strlen(s), LengthUint64Thousands(v[i]), "%,lu", v[i]);
}
}
TEST(LengthInt64Thousands, test) {
int i;
char s[27];
static const int64_t v[] = {
-1000001,
-1000000,
-999999,
-1001,
-1000,
-999,
-101,
-100,
-99,
-11,
-10,
-9,
-1,
0,
1,
9,
10,
11,
99,
100,
101,
999,
1000,
1001,
999999,
1000000,
1000001,
INT64_MIN,
INT64_MAX,
INT64_MIN + 1,
INT64_MAX - 1,
};
for (i = 0; i < ARRAYLEN(v); ++i) {
FormatInt64Thousands(s, v[i]);
ASSERT_EQ(strlen(s), LengthInt64Thousands(v[i]), "%,ld\n\t%s\n\t%lx", v[i],
s, v[i]);
}
}
BENCH(LengthInt64, bench) {
EZBENCH2("LengthInt64", donothing, LengthInt64(INT64_MIN));
EZBENCH2("LengthUint64", donothing, LengthUint64(UINT64_MAX));
EZBENCH2("LengthInt64Thousands", donothing, LengthInt64Thousands(INT64_MIN));
EZBENCH2("LengthUint64Thousands", donothing,
LengthUint64Thousands(UINT64_MAX));
}

View file

@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,20 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/fmt/conv.h"
#include "libc/limits.h"
#include "libc/errno.h"
#include "libc/nt/errors.h"
#include "libc/sock/internal.h"
#include "libc/sock/sock.h"
#include "libc/testlib/testlib.h"
TEST(llog10, test) {
EXPECT_EQ(0, llog10(1));
EXPECT_EQ(0, llog10(2));
EXPECT_EQ(0, llog10(9));
EXPECT_EQ(1, llog10(10));
EXPECT_EQ(1, llog10(11));
EXPECT_EQ(1, llog10(99));
EXPECT_EQ(2, llog10(100));
EXPECT_EQ(2, llog10(101));
EXPECT_EQ(9, llog10(INT_MAX));
EXPECT_EQ(12, llog10(1000000000000));
TEST(__dos2errno, test) {
EXPECT_EQ(EACCES, __dos2errno(kNtErrorSectorNotFound));
EXPECT_EQ(EADDRNOTAVAIL, __dos2errno(kNtErrorInvalidNetname));
}

View file

@ -623,6 +623,12 @@ TEST(palandprintf, precisionStillRespectsNulTerminatorIfNotEscOrRepr) {
Format("%.20s - %d lines %s", "Makefile", 25, ""));
}
TEST(sprintf, commas) {
char buf[64];
sprintf(buf, "%,d", 123456789);
ASSERT_STREQ("123,456,789", buf);
}
BENCH(palandprintf, bench) {
EZBENCH2("ascii", donothing, Format(VEIL("r", "hiuhcreohucreo")));
EZBENCH2("ascii %s", donothing, Format("%s", VEIL("r", "hiuhcreohucreo")));
@ -632,11 +638,23 @@ BENCH(palandprintf, bench) {
EZBENCH2("snprintf %ls", donothing, Format("%ls", VEIL("r", L"hi (╯°□°)╯")));
EZBENCH2("23 %x", donothing, Format("%x", VEIL("r", 23)));
EZBENCH2("23 %d", donothing, Format("%d", VEIL("r", 23)));
EZBENCH2("%f M_PI", donothing, Format("%f", VEIL("x", M_PI)));
EZBENCH2("%g M_PI", donothing, Format("%g", VEIL("x", M_PI)));
EZBENCH2("%a M_PI", donothing, Format("%a", VEIL("x", M_PI)));
EZBENCH2("%e M_PI", donothing, Format("%e", VEIL("x", M_PI)));
EZBENCH2("INT_MIN %x", donothing, Format("%x", VEIL("r", INT_MIN)));
EZBENCH2("INT_MIN %d", donothing, Format("%d", VEIL("r", INT_MIN)));
EZBENCH2("LONG_MIN %x", donothing, Format("%lx", VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %d", donothing, Format("%ld", VEIL("r", LONG_MIN)));
EZBENCH2("23 int64toarray", donothing, int64toarray_radix10(23, buffer));
EZBENCH2("INT_MIN int64toarray", donothing,
EZBENCH2("INT_MIN %,d", donothing, Format("%,d", VEIL("r", INT_MIN)));
EZBENCH2("INT_MIN %ld", donothing, Format("%ld", (long)VEIL("r", INT_MIN)));
EZBENCH2("INT_MIN %jd", donothing,
Format("%jd", (intmax_t)VEIL("r", INT_MIN)));
EZBENCH2("LONG_MIN %lx", donothing, Format("%lx", VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %ld", donothing, Format("%ld", VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %jd", donothing,
Format("%jd", (intmax_t)VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %jx", donothing,
Format("%jx", (intmax_t)VEIL("r", LONG_MIN)));
EZBENCH2("int64toarray 23", donothing, int64toarray_radix10(23, buffer));
EZBENCH2("int64toarray min", donothing,
int64toarray_radix10(INT_MIN, buffer));
}

View file

@ -24,13 +24,14 @@ TEST_LIBC_FMT_DIRECTDEPS = \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TINYMATH \
LIBC_TESTLIB \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_X \
THIRD_PARTY_GDTOA

View file

@ -19,6 +19,7 @@
#include "libc/dce.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/asan.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
@ -83,8 +84,8 @@ BENCH(asan, bench) {
if (!IsAsan()) return;
m = 4 * 1024 * 1024;
p = gc(malloc(m));
EZBENCH_N("__asan_check", 0, __asan_check(p, 0));
EZBENCH_N("__asan_check", 0, EXPROPRIATE(__asan_check(p, 0).kind));
for (n = 2; n <= m; n *= 2) {
EZBENCH_N("__asan_check", n, __asan_check(p, n));
EZBENCH_N("__asan_check", n, EXPROPRIATE(__asan_check(p, n).kind));
}
}

View file

@ -27,7 +27,7 @@
static noasan void *golden(void *p, int c, size_t n) {
size_t i;
if (IsAsan()) __asan_check(p, n);
if (IsAsan()) __asan_verify(p, n);
for (i = 0; i < n; ++i) ((char *)p)[i] = c;
return p;
}
@ -35,8 +35,8 @@ static noasan void *golden(void *p, int c, size_t n) {
TEST(memset, hug) {
char *a, *b;
int i, j, c;
a = malloc(1025 * 2);
b = malloc(1025 * 2);
a = gc(malloc(1025 * 2));
b = gc(malloc(1025 * 2));
for (i = 0; i < 1025; ++i) {
for (j = 0; j < 1025 - i; ++j) {
c = vigna();
@ -52,8 +52,8 @@ TEST(memset, hug) {
TEST(bzero, hug) {
char *a, *b;
int i, j;
a = malloc(1025 * 2);
b = malloc(1025 * 2);
a = gc(malloc(1025 * 2));
b = gc(malloc(1025 * 2));
for (i = 0; i < 1025; ++i) {
for (j = 0; j < 1025 - i; ++j) {
rngset(a, i + j, 0, 0);

View file

@ -17,11 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/intrin/asan.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/stdio/append.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
@ -31,9 +36,12 @@
#include "libc/x/x.h"
#include "net/http/escape.h"
typedef char xmm_t __attribute__((__vector_size__(16)));
static bool OutputHasSymbol(const char *output, const char *s) {
return strstr(output, s) || (!FindDebugBinary() && strstr(output, "NULL"));
}
noinline void ThisIsAnFpeCrash(void) {
void FpuCrash(void) {
typedef char xmm_t __attribute__((__vector_size__(16)));
xmm_t v = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
volatile int x = 0;
@ -43,19 +51,249 @@ noinline void ThisIsAnFpeCrash(void) {
fputc(7 / x, stdout);
}
char bss[10];
void BssOverrunCrash(int n) {
int i;
for (i = 0; i < n; ++i) {
bss[i] = i;
}
}
char data[10] = "abcdeabcde";
void DataOverrunCrash(int n) {
int i;
for (i = 0; i < n; ++i) {
data[i] = i;
}
}
const char rodata[10] = "abcdeabcde";
int RodataOverrunCrash(int i) {
return rodata[i];
}
char *StackOverrunCrash(int n) {
int i;
char stack[10];
bzero(stack, sizeof(stack));
for (i = 0; i < n; ++i) {
stack[i] = i;
}
return strdup(stack);
}
char *MemoryLeakCrash(void) {
char *p = strdup("doge");
testlib_checkformemoryleaks();
return p;
}
int NpeCrash(char *p) {
return *p;
}
void (*pFpuCrash)(void) = FpuCrash;
void (*pBssOverrunCrash)(int) = BssOverrunCrash;
void (*pDataOverrunCrash)(int) = DataOverrunCrash;
int (*pRodataOverrunCrash)(int) = RodataOverrunCrash;
char *(*pStackOverrunCrash)(int) = StackOverrunCrash;
char *(*pMemoryLeakCrash)(void) = MemoryLeakCrash;
int (*pNpeCrash)(char *) = NpeCrash;
void SetUp(void) {
ShowCrashReports();
if (__argc == 2 && !strcmp(__argv[1], "1")) {
ThisIsAnFpeCrash();
if (__argc == 2) {
switch (atoi(__argv[1])) {
case 0:
break;
case 1:
pFpuCrash();
exit(0);
case 2:
pBssOverrunCrash(10 + 1);
exit(0);
case 3:
exit(pRodataOverrunCrash(10 + 1));
case 4:
pDataOverrunCrash(10 + 1);
exit(0);
case 5:
exit((intptr_t)pStackOverrunCrash(10 + 1));
case 6:
exit((intptr_t)pMemoryLeakCrash());
case 7:
exit(pNpeCrash(0));
default:
printf("preventing fork recursion: %s\n", __argv[1]);
exit(1);
}
}
}
// UNFREED MEMORY
// o/dbg/test/libc/log/backtrace_test.com
// max allocated space 655,360
// total allocated space 80
// total free space 327,600
// releasable space 0
// mmaped space 65,360
// non-mmapped space 327,680
//
// 100080040020 64 bytes 5 used
// 421871 strdup
// 416529 MemoryLeakCrash
// 41666d SetUp
// 45428c testlib_runtestcases
//
// 00007fff0000-000080010000 rw-pa-F 2x shadow of 000000000000
// 000080070000-0000800a0000 rw-pa-F 3x shadow of 0000003c0000
// 02008fff0000-020090020000 rw-pa-F 3x shadow of 10007ffc0000
// 020090060000-020090080000 rw-pa-F 2x shadow of 100080340000
// 0e007fff0000-0e0080010000 rw-pa-F 2x shadow of 6ffffffc0000
// 100006560000-100006580000 rw-pa-F 2x shadow of 7ffc32b40000
// 100080000000-100080050000 rw-pa-- 5x automap w/ 50 frame hole
// 100080370000-100080390000 rw-pa-- 2x automap w/ 1 frame hole
// 1000803a0000-1000803b0000 rw-pa-- 1x automap
// 6ffffffe0000-700000000000 rw-paSF 2x stack
// # 24 frames mapped w/ 51 frames gapped
TEST(ShowCrashReports, testMemoryLeakCrash) {
size_t got;
ssize_t rc;
int ws, pid, fds[2];
char *output, buf[512];
if (!IsAsan()) {
/* TODO(jart): How can we make this work without ASAN? */
return;
}
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
ASSERT_NE(-1, (pid = vfork()));
if (!pid) {
dup2(fds[1], 1);
dup2(fds[1], 2);
execv(program_executable_name,
(char *const[]){program_executable_name, "6", 0});
_exit(127);
}
close(fds[1]);
output = 0;
appends(&output, "");
for (;;) {
rc = read(fds[0], buf, sizeof(buf));
if (rc == -1) {
ASSERT_EQ(EINTR, errno);
continue;
}
if ((got = rc)) {
appendd(&output, buf, got);
} else {
break;
}
}
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(78, WEXITSTATUS(ws));
if (!strstr(output, "UNFREED MEMORY")) {
fprintf(stderr, "ERROR: crash report didn't report leak\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (IsAsan()) {
if (!OutputHasSymbol(output, "strdup") ||
!OutputHasSymbol(output, "MemoryLeakCrash")) {
fprintf(stderr, "ERROR: crash report didn't backtrace allocation\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
}
free(output);
}
// clang-format off
// asan error: stack overrun 1-byte store at 0x6fffffffff0a shadow 0x0e007fff7fe1
// x
// uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu..........oooooooooooooooooooooo..................
// |-15 |-15 |-15 |0 |2 |-13 |-13 |0 |0
// ╡A    ΦCE     └eA              ☺☻♥♦♣♠•◘○○0000003fffffffff◙CapAmb:○0000↑ÿ_╟ⁿ⌂  ÿ 
// 000000400000-000000462000 .text 000000462000-00000046a000 .data
// 00007fff0000-00008000ffff
// 000080070000-00008009ffff
// 02008fff0000-02009001ffff
// 020090060000-02009007ffff
// 0e007ffb0000-0e008000ffff ←shadow
// 100018eb0000-100018ecffff
// 100080000000-10008009ffff
// 100080360000-10008037ffff
// 100080390000-10008039ffff
// 6fffffe00000-6fffffffffff ←address
// 0x0000000000407c06: __die at libc/log/die.c:37
// 0x000000000040b1c1: __asan_report_store at libc/intrin/asan.c:1104
// 0x0000000000443302: __asan_report_store1 at libc/intrin/somanyasan.S:118
// 0x000000000041669a: StackOverrunCrash at test/libc/log/backtrace_test.c:76
// 0x00000000004167e7: SetUp at test/libc/log/backtrace_test.c:105
// 0x0000000000452d4b: testlib_runtestcases at libc/testlib/testrunner.c:98
// 0x000000000044c740: testlib_runalltests at libc/testlib/runner.c:37
// 0x00000000004026db: main at libc/testlib/testmain.c:155
// 0x000000000040324f: cosmo at libc/runtime/cosmo.S:64
// 0x000000000040219b: _start at libc/crt/crt.S:67
// clang-format on
TEST(ShowCrashReports, testStackOverrunCrash) {
if (!IsAsan()) return;
size_t got;
ssize_t rc;
int ws, pid, fds[2];
char *output, buf[512];
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
ASSERT_NE(-1, (pid = vfork()));
if (!pid) {
dup2(fds[1], 1);
dup2(fds[1], 2);
execv(program_executable_name,
(char *const[]){program_executable_name, "5", 0});
_exit(127);
}
close(fds[1]);
output = 0;
appends(&output, "");
for (;;) {
rc = read(fds[0], buf, sizeof(buf));
if (rc == -1) {
ASSERT_EQ(EINTR, errno);
continue;
}
if ((got = rc)) {
appendd(&output, buf, got);
} else {
break;
}
}
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(77, WEXITSTATUS(ws));
/* NULL is stopgap until we can copy symbol tablces into binary */
if (!OutputHasSymbol(output, "StackOverrunCrash")) {
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (!strstr(output, "☺☻♥♦♣♠•◘○")) {
fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (!strstr(output, "stack overrun")) {
fprintf(stderr, "ERROR: crash report misclassified stack overrun\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
free(output);
}
// error: Uncaught SIGFPE (FPE_INTDIV) on nightmare pid 11724
// /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721
// ENOTTY[25]
// Linux nightmare SMP Thu, 12 Aug 2021 06:16:45 UTC
//
// 0x0000000000414659: ThisIsAnFpeCrash at test/libc/log/backtrace_test.c:35
// 0x0000000000414659: FpuCrash at test/libc/log/backtrace_test.c:35
// 0x000000000045003b: testlib_runtestcases at libc/testlib/testrunner.c:98
// 0x000000000044b770: testlib_runalltests at libc/testlib/runner.c:37
// 0x000000000040278e: main at libc/testlib/testmain.c:86
@ -102,7 +340,6 @@ void SetUp(void) {
// 7ffe075ab000-7ffe075ac000 r-xp 00000000 00:00 0 [vdso]
//
// /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721 1
TEST(ShowCrashReports, testDivideByZero) {
size_t got;
ssize_t rc;
@ -135,43 +372,242 @@ TEST(ShowCrashReports, testDivideByZero) {
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(128 + SIGFPE, WEXITSTATUS(ws));
/* NULL is stopgap until we can copy symbol tablces into binary */
if (!strstr(output, "ThisIsAnFpeCrash") && !strstr(output, "NULL")) {
if (!OutputHasSymbol(output, "FpuCrash")) {
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
exit(1);
__die();
}
if (!strstr(output, gc(xasprintf("%d", pid)))) {
fprintf(stderr, "ERROR: crash report didn't have pid\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
exit(1);
__die();
}
if (!strstr(output, "SIGFPE")) {
fprintf(stderr, "ERROR: crash report didn't have signal name\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
exit(1);
__die();
}
if (!strstr(output, "3.141")) {
fprintf(stderr, "ERROR: crash report didn't have fpu register\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
exit(1);
__die();
}
if (!strstr(output, "0f0e0d0c0b0a09080706050403020100")) {
fprintf(stderr, "ERROR: crash report didn't have sse register\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
exit(1);
__die();
}
if (!strstr(output, "3133731337")) {
fprintf(stderr, "ERROR: crash report didn't have general register\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
exit(1);
__die();
}
free(output);
}
// clang-format off
// asan error: global redzone 1-byte store at 0x00000048cf2a shadow 0x0000800899e5
// x
// ........................................OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
// |0 |0 |0 |0 |2 |-6 |-6 |-6 |-6
//                                ☺☻♥♦♣♠•◘○                                        
// 000000400000-000000460000 .text
// 000000460000-000000468000 .data
// 00007fff0000-00008000ffff
// 000080070000-00008009ffff ←shadow
// 02008fff0000-02009001ffff
// 020090060000-02009007ffff
// 0e007ffb0000-0e008000ffff
// 1000286b0000-1000286cffff
// 100080000000-10008009ffff
// 100080350000-10008036ffff
// 100080380000-10008038ffff
// 6fffffe00000-6fffffffffff
// 0x0000000000407af6: __die at libc/log/die.c:36
// 0x0000000000444f13: __asan_die at libc/intrin/asan.c:318
// 0x0000000000445bc8: __asan_report at libc/intrin/asan.c:667
// 0x0000000000445e41: __asan_report_memory_fault at libc/intrin/asan.c:672
// 0x0000000000446312: __asan_report_store at libc/intrin/asan.c:1008
// 0x0000000000444442: __asan_report_store1 at libc/intrin/somanyasan.S:118
// 0x0000000000416216: BssOverrunCrash at test/libc/log/backtrace_test.c:52
// 0x000000000041642a: SetUp at test/libc/log/backtrace_test.c:73
// 0x00000000004513eb: testlib_runtestcases at libc/testlib/testrunner.c:98
// 0x000000000044bbe0: testlib_runalltests at libc/testlib/runner.c:37
// 0x00000000004026db: main at libc/testlib/testmain.c:155
// 0x000000000040323f: cosmo at libc/runtime/cosmo.S:64
// 0x000000000040219b: _start at libc/crt/crt.S:67
// clang-format on
TEST(ShowCrashReports, testBssOverrunCrash) {
if (!IsAsan()) return;
size_t got;
ssize_t rc;
int ws, pid, fds[2];
char *output, buf[512];
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
ASSERT_NE(-1, (pid = vfork()));
if (!pid) {
dup2(fds[1], 1);
dup2(fds[1], 2);
execv(program_executable_name,
(char *const[]){program_executable_name, "2", 0});
_exit(127);
}
close(fds[1]);
output = 0;
appends(&output, "");
for (;;) {
rc = read(fds[0], buf, sizeof(buf));
if (rc == -1) {
ASSERT_EQ(EINTR, errno);
continue;
}
if ((got = rc)) {
appendd(&output, buf, got);
} else {
break;
}
}
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(77, WEXITSTATUS(ws));
/* NULL is stopgap until we can copy symbol tablces into binary */
if (!OutputHasSymbol(output, "BssOverrunCrash")) {
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (!strstr(output, "☺☻♥♦♣♠•◘○") || !strstr(output, "global redzone")) {
fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
free(output);
}
// clang-format off
// asan error: null pointer dereference 1-byte load at 0x000000000000 shadow 0x00007fff8000
// x
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅
// |-17 |-17 |-17 |-17 |-17 |-1 |-1 |-1 |-1 |-1
// ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅
// 000000400000-000000464000 .text
// 000000464000-00000046d000 .data
// 00007fff0000-00008000ffff ←shadow
// 000080070000-00008009ffff
// 02008fff0000-02009001ffff
// 020090060000-02009007ffff
// 0e007fff0000-0e008000ffff
// 10000d3e0000-10000d3fffff
// 100080000000-10008009ffff
// 100080370000-10008038ffff
// 1000803a0000-1000803affff
// 6ffffffe0000-6fffffffffff
// 0x0000000000407c84: __die at libc/log/die.c:37
// 0x000000000040b1ee: __asan_report_load at libc/intrin/asan.c:1083
// 0x000000000041639e: NpeCrash at test/libc/log/backtrace_test.c:87
// 0x0000000000416733: SetUp at test/libc/log/backtrace_test.c:120
// 0x00000000004541fb: testlib_runtestcases at libc/testlib/testrunner.c:98
// 0x000000000044d000: testlib_runalltests at libc/testlib/runner.c:37
// 0x00000000004026db: main at libc/testlib/testmain.c:94
// 0x000000000040327f: cosmo at libc/runtime/cosmo.S:64
// 0x000000000040219b: _start at libc/crt/crt.S:67
// clang-format on
TEST(ShowCrashReports, testNpeCrash) {
if (!IsAsan()) return;
size_t got;
ssize_t rc;
int ws, pid, fds[2];
char *output, buf[512];
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
ASSERT_NE(-1, (pid = vfork()));
if (!pid) {
dup2(fds[1], 1);
dup2(fds[1], 2);
execv(program_executable_name,
(char *const[]){program_executable_name, "7", 0});
_exit(127);
}
close(fds[1]);
output = 0;
appends(&output, "");
for (;;) {
rc = read(fds[0], buf, sizeof(buf));
if (rc == -1) {
ASSERT_EQ(EINTR, errno);
continue;
}
if ((got = rc)) {
appendd(&output, buf, got);
} else {
break;
}
}
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(77, WEXITSTATUS(ws));
/* NULL is stopgap until we can copy symbol tablces into binary */
if (!strstr(output, "null pointer dereference")) {
fprintf(stderr, "ERROR: crash report didn't diagnose the problem\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (!OutputHasSymbol(output, "NpeCrash")) {
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (!strstr(output, "∅∅∅∅")) {
fprintf(stderr, "ERROR: crash report didn't have shadow diagram\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
free(output);
}
TEST(ShowCrashReports, testDataOverrunCrash) {
if (!IsAsan()) return;
size_t got;
ssize_t rc;
int ws, pid, fds[2];
char *output, buf[512];
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
ASSERT_NE(-1, (pid = vfork()));
if (!pid) {
dup2(fds[1], 1);
dup2(fds[1], 2);
execv(program_executable_name,
(char *const[]){program_executable_name, "4", 0});
_exit(127);
}
close(fds[1]);
output = 0;
appends(&output, "");
for (;;) {
rc = read(fds[0], buf, sizeof(buf));
if (rc == -1) {
ASSERT_EQ(EINTR, errno);
continue;
}
if ((got = rc)) {
appendd(&output, buf, got);
} else {
break;
}
}
ASSERT_NE(-1, wait(&ws));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_EQ(77, WEXITSTATUS(ws));
/* NULL is stopgap until we can copy symbol tablces into binary */
if (!OutputHasSymbol(output, "DataOverrunCrash")) {
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
if (!strstr(output, "☺☻♥♦♣♠•◘○") || !strstr(output, "global redzone")) {
fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n",
gc(IndentLines(output, -1, 0, 4)));
__die();
}
free(output);
}

View file

@ -29,6 +29,7 @@ TEST_LIBC_LOG_DIRECTDEPS = \
LIBC_STDIO \
LIBC_X \
LIBC_INTRIN \
LIBC_FMT \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_LOG \

View file

@ -16,9 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/log/libfatal.internal.h"
#include "libc/mem/arena.h"
#include "libc/mem/mem.h"
#include "libc/stdio/append.internal.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(arena, test) {
@ -37,29 +41,66 @@ TEST(arena, test) {
__arena_pop();
}
TEST(arena, testRealloc) {
char *b = 0;
size_t i, n = 0;
__arena_push();
for (i = 0; i < kHyperionSize; ++i) {
b = realloc(b, ++n * sizeof(*b));
b[n - 1] = kHyperion[i];
}
ASSERT_EQ(0, memcmp(b, kHyperion, kHyperionSize));
__arena_pop();
}
void *memalign_(size_t, size_t) asm("memalign");
void *calloc_(size_t, size_t) asm("calloc");
void A(size_t n) {
void Ca(size_t n) {
__arena_push();
for (int i = 0; i < n; ++i) {
calloc_(15, 1);
memalign_(1, 1);
}
__arena_pop();
}
void B(size_t n) {
void Cb(size_t n) {
void **P;
P = malloc(n * sizeof(void *));
for (int i = 0; i < n; ++i) {
P[i] = calloc_(15, 1);
P[i] = calloc_(1, 1);
}
bulk_free(P, n);
free(P);
}
BENCH(arena, bench) {
EZBENCH2("A 100", donothing, A(100));
EZBENCH2("B 100", donothing, B(100));
EZBENCH2("A 5000", donothing, A(5000));
EZBENCH2("B 5000", donothing, B(5000));
BENCH(arena, benchMalloc) {
EZBENCH2("arena calloc(1)", donothing, Ca(100));
EZBENCH2("dlmalloc calloc(1)", donothing, Cb(100));
}
void Ra(void) {
long *b = 0;
size_t i, n = 0;
__arena_push();
for (i = 0; i < kHyperionSize; ++i) {
b = realloc(b, ++n * sizeof(*b));
b[n - 1] = kHyperion[i];
}
__arena_pop();
}
void Rb(void) {
long *b = 0;
size_t i, n = 0;
for (i = 0; i < kHyperionSize; ++i) {
b = realloc(b, ++n * sizeof(*b));
b[n - 1] = kHyperion[i];
}
free(b);
}
BENCH(arena, benchRealloc) {
EZBENCH2("arena realloc", donothing, Ra());
EZBENCH2("dlmalloc realloc", donothing, Rb());
}

View file

@ -23,6 +23,7 @@
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/rand/rand.h"
#include "libc/runtime/cxaatexit.internal.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
@ -37,33 +38,49 @@
#define M 20
TEST(malloc, zeroMeansOne) {
ASSERT_GE(malloc_usable_size(malloc(0)), 1);
ASSERT_GE(malloc_usable_size(gc(malloc(0))), 1);
}
TEST(calloc, zerosMeansOne) {
ASSERT_GE(malloc_usable_size(calloc(0, 0)), 1);
ASSERT_GE(malloc_usable_size(gc(calloc(0, 0))), 1);
}
void AppendStuff(char **p, size_t *n) {
char buf[512];
ASSERT_NE(NULL, (*p = realloc(*p, (*n += 512))));
rngset(buf, sizeof(buf), 0, 0);
memcpy(*p + *n - sizeof(buf), buf, sizeof(buf));
}
TEST(malloc, test) {
char *big = 0;
size_t n, bigsize = 0;
static struct stat st;
static volatile int i, j, k, *A[4096], fds[M], *maps[M], mapsizes[M];
memset(fds, -1, sizeof(fds));
memset(maps, -1, sizeof(maps));
for (i = 0; i < N * M; ++i) {
/* AppendStuff(&big, &bigsize); */
j = rand() % ARRAYLEN(A);
if (A[j]) {
ASSERT_EQ(j, A[j][0]);
A[j] = realloc(A[j], max(sizeof(int), rand() % N));
n = rand() % N;
n = MAX(n, 1);
n = ROUNDUP(n, sizeof(int));
A[j] = realloc(A[j], n);
ASSERT_NE(NULL, A[j]);
ASSERT_EQ(j, A[j][0]);
free(A[j]);
A[j] = NULL;
} else {
A[j] = malloc(max(sizeof(int), rand() % N));
n = rand() % N;
n = MAX(n, 1);
n = ROUNDUP(n, sizeof(int));
A[j] = malloc(n);
ASSERT_NE(NULL, A[j]);
A[j][0] = j;
}
if (i % M == 0) {
if (!(i % M)) {
k = rand() % M;
if (fds[k] == -1) {
ASSERT_NE(-1, (fds[k] = open(program_invocation_name, O_RDONLY)));
@ -78,10 +95,10 @@ TEST(malloc, test) {
}
}
}
free(big);
for (i = 0; i < ARRAYLEN(A); ++i) free(A[i]);
for (i = 0; i < ARRAYLEN(maps); ++i) munmap(maps[i], mapsizes[i]);
for (i = 0; i < ARRAYLEN(fds); ++i) close(fds[i]);
malloc_trim(0);
}
void *bulk[1024];

View file

@ -25,6 +25,7 @@ TEST_LIBC_MEM_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RAND \

View file

@ -27,8 +27,7 @@
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#undef _gc
#define _gc(x) _defer(Free, x)
#define GC(x) _defer(Free, x)
char *x;
char *y;
@ -40,20 +39,20 @@ void Free(char *p) {
}
void C(void) {
x = _gc(strdup("abcd"));
if (0) PrintGarbage(stderr);
_gclongjmp(jb, 1);
x = GC(strdup("abcd"));
if (0) PrintGarbage();
gclongjmp(jb, 1);
abort();
}
void B(void C(void)) {
y = _gc(strdup("HIHI"));
y = GC(strdup("HIHI"));
C();
abort();
}
void A(void C(void), void B(void(void))) {
z = _gc(strdup("yoyo"));
z = GC(strdup("yoyo"));
B(C);
abort();
}
@ -63,10 +62,12 @@ void (*Bp)(void(void)) = B;
void (*Cp)(void) = C;
TEST(gclongjmp, test) {
PrintGarbage();
if (!setjmp(jb)) {
Ap(Cp, Bp);
abort();
}
if (0) PrintGarbage();
EXPECT_STREQ("FREE", x);
EXPECT_STREQ("FREE", y);
EXPECT_STREQ("FREE", z);
@ -75,12 +76,12 @@ TEST(gclongjmp, test) {
free(x);
}
void F1(void) {
noinline void F1(void) {
/* 3x slower than F2() but sooo worth it */
_gc(malloc(16));
gc(malloc(16));
}
void F2(void) {
noinline void F2(void) {
void *volatile p;
p = malloc(16);
free(p);

View file

@ -2,24 +2,26 @@
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
if CLANG=$(command -v clang); then
mkdir -p o/$MODE/test/libc/release
$CLANG \
-o o/$MODE/test/libc/release/smokeclang2.com.dbg \
-Os \
-Wall \
-Werror \
-static \
-fno-pie \
-nostdlib \
-nostdinc \
-fuse-ld=lld \
-mno-red-zone \
-Wl,-T,o/$MODE/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smoke.c \
o/$MODE/libc/crt/crt.o \
o/$MODE/ape/ape.o \
o/$MODE/cosmopolitan.a || exit
o/$MODE/test/libc/release/smokeclang2.com.dbg || exit
fi
# TODO: someone who uses clang please mantain this
# if CLANG=$(command -v clang); then
# mkdir -p o/$MODE/test/libc/release
# $CLANG \
# -o o/$MODE/test/libc/release/smokeclang2.com.dbg \
# -Os \
# -Wall \
# -Werror \
# -static \
# -fno-pie \
# -nostdlib \
# -nostdinc \
# -fuse-ld=lld \
# -mno-red-zone \
# -Wl,-T,o/$MODE/ape/ape.lds \
# -include o/cosmopolitan.h \
# test/libc/release/smoke.c \
# o/$MODE/libc/crt/crt.o \
# o/$MODE/ape/ape.o \
# o/$MODE/cosmopolitan.a || exit
# o/$MODE/test/libc/release/smokeclang2.com.dbg || exit
# fi

View file

@ -2,26 +2,26 @@
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
# TODO(jart): implement me
# TODO: someone who uses clang please mantain this
if CLANG=$(command -v clang); then
mkdir -p o/$MODE/test/libc/release
$CLANG \
-o o/$MODE/test/libc/release/smokeclang.com.dbg \
-Os \
-Wall \
-Werror \
-static \
-fno-pie \
-nostdlib \
-nostdinc \
-fuse-ld=lld \
-mno-red-zone \
-Wl,-T,o/$MODE/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smoke.c \
o/$MODE/libc/crt/crt.o \
o/$MODE/ape/ape.o \
o/$MODE/cosmopolitan.a || exit
o/$MODE/test/libc/release/smokeclang.com.dbg || exit
fi
# if CLANG=$(command -v clang); then
# mkdir -p o/$MODE/test/libc/release
# $CLANG \
# -o o/$MODE/test/libc/release/smokeclang.com.dbg \
# -Os \
# -Wall \
# -Werror \
# -static \
# -fno-pie \
# -nostdlib \
# -nostdinc \
# -fuse-ld=lld \
# -mno-red-zone \
# -Wl,-T,o/$MODE/ape/ape.lds \
# -include o/cosmopolitan.h \
# test/libc/release/smoke.c \
# o/$MODE/libc/crt/crt.o \
# o/$MODE/ape/ape.o \
# o/$MODE/cosmopolitan.a || exit
# o/$MODE/test/libc/release/smokeclang.com.dbg || exit
# fi

View file

@ -0,0 +1,33 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/testlib/testlib.h"
#include "third_party/dlmalloc/dlmalloc.internal.h"
TEST(mremap, testMalloc) {
int i;
char *a, *b, *c, *d;
ASSERT_NE(NULL, a = malloc(DEFAULT_MMAP_THRESHOLD));
ASSERT_NE(NULL, b = mapanon(FRAMESIZE));
ASSERT_NE(NULL, a = realloc(a, DEFAULT_MMAP_THRESHOLD * 2));
munmap(b, FRAMESIZE);
free(a);
}

View file

@ -27,6 +27,7 @@
#include "libc/x/x.h"
STATIC_YOINK("zip_uri_support");
STATIC_YOINK("usr/share/zoneinfo/New_York");
TEST(dirstream, test) {
DIR *dir;

View file

@ -26,7 +26,7 @@ TEST(DumpHexc, test) {
\\x68\\x65\\x6c\\x6c\\x6f\\xe2\\x86\\x92\\x0a\\x01\\x02\\x74\\x68\\x65\\x65\\x72\\\n\
\\x68\\x75\\x72\\x63\\x65\\x6f\\x61\\x68\\x72\\x63\\x75\\x6f\\x65\\x61\\x75\\x68\\\n\
\\x63\\x72\"",
DumpHexc("hello→\n\1\2theerhurceoahrcuoeauhcr", -1, 0));
gc(DumpHexc("hello→\n\1\2theerhurceoahrcuoeauhcr", -1, 0)));
}
BENCH(DumpHexc, bench) {

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
@ -24,7 +25,7 @@
TEST(fgets, test) {
FILE *f;
char buf[29];
f = fmemopen(strdup(kHyperion), kHyperionSize, "r+");
f = fmemopen(gc(strdup(kHyperion)), kHyperionSize, "r+");
ASSERT_STREQ("The fall of Hyperion - a Dre", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("am\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("John Keats\n", fgets(buf, sizeof(buf), f));

View file

@ -27,6 +27,7 @@ TEST(fmemopen, testWriteRewindRead) {
rewind(f);
EXPECT_EQ(1, fread(&c, 1, 1, f));
EXPECT_EQ('c', c);
fclose(f);
}
/* TEST(fmemopen, testWriteRead_readsNothingButNotEof) { */

View file

@ -18,6 +18,7 @@
*/
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/log/libfatal.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/stdio/stdio.h"
@ -95,7 +96,11 @@ void ReadHyperionLines(void) {
char *line = NULL;
size_t linesize = 0;
ASSERT_NE(NULL, (f = fopen("hyperion.txt", "r")));
while ((rc = getline(&line, &linesize, f)) != -1) {
int i = 0;
for (;;) {
__printf("i=%d\n", i++);
rc = getline(&line, &linesize, f);
if (rc == -1) break;
data = xrealloc(data, size + rc);
memcpy(data + size, line, rc);
size += rc;

View file

@ -152,7 +152,7 @@ TEST(appendd, testMemFail_doesntFreeExistingAllocation) {
char *b = 0;
ASSERT_NE(-1, appends(&b, "hello"));
EXPECT_STREQ("hello", b);
ASSERT_EQ(-1, appendd(&b, 0, -1ull >> 8));
ASSERT_EQ(-1, appendd(&b, 0, -1ull >> 7));
EXPECT_STREQ("hello", b);
free(b);
}

View file

@ -48,4 +48,6 @@ BENCH(longsort, bench) {
long *p2 = gc(malloc(n * sizeof(long)));
rngset(p1, n * sizeof(long), 0, 0);
EZBENCH2("longsort", memcpy(p2, p1, n * sizeof(long)), longsort(p2, n));
EZBENCH2("qsort", memcpy(p2, p1, n * sizeof(long)),
qsort(p2, n, sizeof(long), CompareLong));
}