Add x86_64-linux-gnu emulator

I wanted a tiny scriptable meltdown proof way to run userspace programs
and visualize how program execution impacts memory. It helps to explain
how things like Actually Portable Executable works. It can show you how
the GCC generated code is going about manipulating matrices and more. I
didn't feel fully comfortable with Qemu and Bochs because I'm not smart
enough to understand them. I wanted something like gVisor but with much
stronger levels of assurances. I wanted a single binary that'll run, on
all major operating systems with an embedded GPL barrier ZIP filesystem
that is tiny enough to transpile to JavaScript and run in browsers too.

https://justine.storage.googleapis.com/emulator625.mp4
This commit is contained in:
Justine Tunney 2020-08-25 04:23:25 -07:00
parent 467504308a
commit f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions

View file

@ -17,8 +17,9 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/dce.h"
#include "libc/bits/segmentation.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/testlib/testlib.h"
TEST(arch_prctl, fs) {

View file

@ -1,105 +0,0 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/ucontext.h"
#include "libc/runtime/buffer.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "third_party/xed/x86.h"
char *p;
bool segfaulted_;
struct GuardedBuffer b_;
struct sigaction oldsegv_;
struct XedDecodedInst xedd_;
void RestrictPage(void *addr, unsigned flags) {
addr = (void *)rounddown((intptr_t)addr, PAGESIZE);
EXPECT_NE(-1, mprotect(addr, PAGESIZE, flags));
}
void OnSegLol(int sig, struct siginfo *si, struct ucontext *uc) {
size_t i;
uint8_t *rip;
segfaulted_ = true;
rip = (uint8_t *)uc->uc_mcontext.rip;
RestrictPage(rip, PROT_READ | PROT_WRITE | PROT_EXEC);
ASSERT_EQ(XED_ERROR_NONE,
xed_instruction_length_decode(xed_decoded_inst_zero_set_mode(
&xedd_, XED_MACHINE_MODE_LONG_64),
rip, XED_MAX_INSTRUCTION_BYTES));
for (i = 0; i < xedd_.decoded_length; ++i) rip[i] = 0x90; /* NOP */
RestrictPage(rip, PROT_READ | PROT_EXEC);
}
void SetUp(void) {
segfaulted_ = false;
memset(&b_, 0, sizeof(b_));
ASSERT_NE(-1, xsigaction(SIGSEGV, OnSegLol, SA_RESETHAND | SA_RESTART, 0,
&oldsegv_));
}
void TearDown(void) {
EXPECT_NE(-1, sigaction(SIGSEGV, &oldsegv_, NULL));
bfree(&b_);
EXPECT_EQ(NULL, b_.p);
}
TEST(balloc, createsGuardPage) {
ASSERT_NE(NULL, (p = balloc(&b_, 1, 1)));
EXPECT_EQ(p, b_.p);
p[0] = '.';
ASSERT_FALSE(segfaulted_);
/* TODO(jart): fix me!!! */
/* p[1 + __BIGGEST_ALIGNMENT__] = '!'; */
/* EXPECT_TRUE(segfaulted_); */
}
TEST(balloc, aligned_roundsUp) {
ASSERT_NE(NULL, (p = balloc(&b_, 128, 1)));
EXPECT_EQ(0, (intptr_t)b_.p & 127);
p[127] = '.';
ASSERT_FALSE(segfaulted_);
/* TODO(jart): fix me!!! */
/* p[128 + __BIGGEST_ALIGNMENT__] = '!'; */
/* EXPECT_TRUE(segfaulted_); */
}
TEST(balloc, multipleCalls_avoidsNeedlessSyscalls) {
size_t c;
c = g_syscount;
ASSERT_NE(NULL, (p = balloc(&b_, 1, 31337)));
EXPECT_GT(g_syscount, c);
c = g_syscount;
ASSERT_NE(NULL, (p = balloc(&b_, 1, 31337 / 2)));
EXPECT_EQ(g_syscount, c);
c = g_syscount;
ASSERT_NE(NULL, (p = balloc(&b_, 1, 31337 * 2)));
EXPECT_GT(g_syscount, c);
}

View file

@ -18,18 +18,58 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/bits/bits.h"
#include "libc/macros.h"
#include "libc/rand/rand.h"
#include "libc/runtime/carsort.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(heapsortcar, test) {
int32_t A[][2] = {{4, 'a'}, {65, 'b'}, {2, 'c'}, {-31, 'd'}, {0, 'e'},
{99, 'f'}, {2, 'g'}, {83, 'h'}, {782, 'i'}, {1, 'j'}};
const int32_t B[][2] = {{-31, 'd'}, {0, 'e'}, {1, 'j'}, {2, 'c'},
{2, 'g'}, {4, 'a'}, {65, 'b'}, {83, 'h'},
{99, 'f'}, {782, 'i'}};
unsigned n = ARRAYLEN(A);
heapsortcar(A, n);
ASSERT_EQ(0, memcmp(&A[0], &B[0], sizeof(A)));
const int32_t kUnsorted[][2] = {
{4, 'a'}, {65, 'b'}, {2, 'c'}, {-1, 'G'}, {-31, 'd'}, {0, 'e'},
{99, 'f'}, {2, 'g'}, {83, 'h'}, {782, 'i'}, {1, 'j'},
};
const int32_t kGolden[][2] = {
{-31, 'd'}, {-1, 'G'}, {0, 'e'}, {1, 'j'}, {2, 'c'}, {2, 'g'},
{4, 'a'}, {65, 'b'}, {83, 'h'}, {99, 'f'}, {782, 'i'},
};
int32_t A[ARRAYLEN(kUnsorted)][2];
int32_t B[2100][2];
TEST(carsort100, test) {
memcpy(A, kUnsorted, sizeof(A));
carsort100(ARRAYLEN(A), A);
ASSERT_EQ(0, memcmp(&A[0], &kGolden[0], sizeof(kUnsorted)));
}
TEST(carsort1000, test) {
memcpy(A, kUnsorted, sizeof(A));
carsort1000(ARRAYLEN(A), A);
ASSERT_EQ(0, memcmp(&A[0], &kGolden[0], sizeof(kUnsorted)));
}
TEST(qsort, test) {
memcpy(A, kUnsorted, sizeof(A));
qsort(A, ARRAYLEN(A), 8, cmpsl);
ASSERT_EQ(0, memcmp(&A[0], &kGolden[0], sizeof(kUnsorted)));
}
BENCH(carsort, benchMedium) {
EZBENCH2("medium carsort100", rngset(B, sizeof(B), rand64, -1),
carsort100(ARRAYLEN(B), B));
EZBENCH2("medium carsort1000", rngset(B, sizeof(B), rand64, -1),
carsort1000(ARRAYLEN(B), B));
EZBENCH2("medium qsort", rngset(B, sizeof(B), rand64, -1),
qsort(B, ARRAYLEN(B), 8, cmpsl));
}
BENCH(carsort, benchSmall) {
EZBENCH2("small carsort100", memcpy(A, kUnsorted, sizeof(A)),
carsort100(ARRAYLEN(A), A));
EZBENCH2("small carsort1000", memcpy(A, kUnsorted, sizeof(A)),
carsort1000(ARRAYLEN(A), A));
EZBENCH2("small qsort", memcpy(A, kUnsorted, sizeof(A)),
qsort(A, ARRAYLEN(A), 8, cmpsl));
}

View file

@ -21,7 +21,6 @@
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/runtime/mappings.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"

View file

@ -36,7 +36,7 @@
* @see __addvsi3, __mulvsi3, etc.
*/
bool overflowed_;
volatile bool overflowed_;
void __on_arithmetic_overflow(void) {
overflowed_ = true;
@ -174,6 +174,16 @@ TEST(__mulvdi3, standAndDeliver_aNegativeTimesANegativeEqualsAPositive) {
EXPECT_FALSE(overflowed_);
}
TEST(__mulvdi3, testOverflow) {
volatile int64_t x;
x = 3037000500;
x *= 3037000499;
EXPECT_FALSE(overflowed_);
x = 3037000500;
x *= 3037000500;
EXPECT_TRUE(overflowed_);
}
/* 32-BIT SIGNED ADDITION */
TEST(__addvsi3, testMin1) {
@ -246,3 +256,99 @@ TEST(__subvdi3, testMax1) {
EXPECT_EQ(LONG_MAX - 1, VEIL("r", LONG_MAX) - 1);
EXPECT_FALSE(overflowed_);
}
/* 128-BIT SIGNED ADDITION */
TEST(__addvti3, testMath) {
volatile int128_t x;
x = 2;
x += 2;
EXPECT_EQ(4, x);
x = -2;
x += -2;
EXPECT_EQ(-4, x);
x = UINT64_MAX;
x += 1;
EXPECT_EQ((int128_t)UINT64_MAX + 1, x);
EXPECT_FALSE(overflowed_);
}
TEST(__addvti3, testOverflow) {
volatile int128_t x;
x = INT128_MAX;
x += 1;
EXPECT_TRUE(overflowed_);
}
/* 128-BIT SIGNED SUBTRACTION */
TEST(__subvti3, testMath) {
volatile int128_t x;
x = -2;
x -= 2;
EXPECT_EQ(-4, x);
x = UINT64_MIN;
x -= 1;
EXPECT_EQ((int128_t)UINT64_MIN - 1, x);
EXPECT_FALSE(overflowed_);
}
TEST(__subvti3, testOverflow) {
volatile int128_t x;
x = INT128_MIN;
x -= 1;
EXPECT_TRUE(overflowed_);
}
/* 128-BIT SIGNED NEGATION */
TEST(__negvti3, testMath) {
volatile int128_t x;
x = -2;
x = -x;
EXPECT_EQ(2, x);
EXPECT_FALSE(overflowed_);
x = INT128_MAX;
x = -x;
EXPECT_EQ(INT128_MIN + 1, x);
EXPECT_FALSE(overflowed_);
x = (uint128_t)0x8000000000000000 << 64 | 0x8000000000000000;
x = -x;
EXPECT_EQ((uint128_t)0x7fffffffffffffff << 64 | 0x8000000000000000, x);
EXPECT_FALSE(overflowed_);
}
TEST(__negvti3, testOverflow) {
volatile int128_t x;
x = INT128_MIN;
x = -x;
EXPECT_TRUE(overflowed_);
}
/* 128-BIT SIGNED MULTIPLICATION */
TEST(__mulvti3, testMath) {
volatile int128_t x;
x = 7;
x *= 11;
EXPECT_EQ(77, x);
EXPECT_FALSE(overflowed_);
x = 0x1fffffffffffffff;
x *= 0x1fffffffffffffff;
EXPECT_EQ((uint128_t)0x3ffffffffffffff << 64 | 0xc000000000000001, x);
EXPECT_FALSE(overflowed_);
x = -0x1fffffffffffffff;
x *= 0x1fffffffffffffff;
EXPECT_EQ((uint128_t)0xfc00000000000000 << 64 | 0x3fffffffffffffff, x);
EXPECT_FALSE(overflowed_);
}
TEST(__mulvti3, testOverflow) {
volatile int128_t x;
x = 0xb504f333f9de5be0;
x *= 0xb504f333f9de6d28;
EXPECT_FALSE(overflowed_);
x = 0xb504f333f9de5be0;
x *= 0xb504f333f9de6d29;
EXPECT_TRUE(overflowed_);
}

View file

@ -1,58 +0,0 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/runtime/mappings.h"
#include "libc/testlib/testlib.h"
#define ADDR
struct MemoryCoord stack = ADDRSIZE_TO_COORD(0x7fffffff0000L, 0x00010000);
struct MemoryCoord heap3 = ADDRSIZE_TO_COORD(0x200000020000L, 0x00010000);
struct MemoryCoord heap2 = ADDRSIZE_TO_COORD(0x200000010000L, 0x00010000);
struct MemoryCoord heap1 = ADDRSIZE_TO_COORD(0x200000000000L, 0x00010000);
struct MemoryCoord heapa = ADDRSIZE_TO_COORD(0x200000000000L, 0x00030000);
struct MemoryCoord progg = ADDRSIZE_TO_COORD(0x000000400000L, 0x00010000);
struct MemoryCoord bane = ADDRSIZE_TO_COORD(0xffff800000080000L, 0x00010000);
TEST(isoverlapping, test) {
EXPECT_FALSE(ISOVERLAPPING(stack, heap3));
EXPECT_FALSE(ISOVERLAPPING(heap1, heap2));
EXPECT_FALSE(ISOVERLAPPING(heap2, heap3));
EXPECT_FALSE(ISOVERLAPPING(heap1, heap3));
EXPECT_TRUE(ISOVERLAPPING(heapa, heap1));
EXPECT_TRUE(ISOVERLAPPING(heapa, heap3));
}
TEST(findmapping, limits) {
ASSERT_EQ(INT_MAX, ADDR_TO_COORD(0x7fffffff0000L));
ASSERT_EQ(INT_MIN, ADDR_TO_COORD(-0x800000000000L));
}
TEST(findmapping, test) {
struct MemoryCoord c[] = {bane, progg, heap1, heap2, heap3, stack};
EXPECT_EQ(6, ARRAYLEN(c));
EXPECT_EQ(0, findmapping_(ADDR_TO_COORD(-0x800000000000UL), c, 6));
EXPECT_EQ(1, findmapping_(ADDR_TO_COORD(-42), c, 6));
EXPECT_EQ(1, findmapping_(ADDR_TO_COORD(0x000000300000L), c, 6));
EXPECT_EQ(2, findmapping_(ADDR_TO_COORD(0x000000400000L), c, 6));
EXPECT_EQ(6, findmapping_(ADDR_TO_COORD(0x7fffffffffffL), c, 6));
}

View file

@ -0,0 +1,289 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
static bool AreMemoryIntervalsEqual(const struct MemoryIntervals *mm1,
const struct MemoryIntervals *mm2) {
if (mm1->i != mm2->i) return false;
if (memcmp(mm1->p, mm2->p, mm1->i * sizeof(*mm2->p)) != 0) return false;
if (memcmp(mm1->h, mm2->h, mm1->i * sizeof(*mm2->h)) != 0) return false;
return true;
}
static void PrintMemoryInterval(const struct MemoryIntervals *mm) {
int i;
for (i = 0; i < mm->i; ++i) {
if (i) fprintf(stderr, ",");
fprintf(stderr, "{%d,%d}", mm->p[i].x, mm->p[i].y);
}
fprintf(stderr, "\n");
}
static void CheckMemoryIntervalsEqual(const struct MemoryIntervals *mm1,
const struct MemoryIntervals *mm2) {
if (!AreMemoryIntervalsEqual(mm1, mm2)) {
PrintMemoryInterval(mm1);
PrintMemoryInterval(mm2);
CHECK(!"memory intervals not equal");
exit(1);
}
}
static void CheckMemoryIntervalsAreOk(const struct MemoryIntervals *mm) {
if (!AreMemoryIntervalsOk(mm)) {
PrintMemoryInterval(mm);
CHECK(!"memory intervals not ok");
exit(1);
}
}
static void RunTrackMemoryIntervalTest(const struct MemoryIntervals t[2], int x,
int y, long h) {
struct MemoryIntervals *mm;
mm = memcpy(memalign(alignof(*t), sizeof(*t)), t, sizeof(*t));
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, TrackMemoryInterval(mm, x, y, h));
CheckMemoryIntervalsAreOk(mm);
CheckMemoryIntervalsEqual(mm, t + 1);
free(mm);
}
static void RunReleaseMemoryIntervalsTest(const struct MemoryIntervals t[2],
int x, int y) {
struct MemoryIntervals *mm;
mm = memcpy(memalign(alignof(*t), sizeof(*t)), t, sizeof(*t));
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, ReleaseMemoryIntervals(mm, x, y, NULL));
CheckMemoryIntervalsAreOk(mm);
CheckMemoryIntervalsEqual(t + 1, mm);
free(mm);
}
TEST(TrackMemoryInterval, TestEmpty) {
static const struct MemoryIntervals mm[2] = {
{0, {}, {}},
{1, {{2, 2}}, {0}},
};
RunTrackMemoryIntervalTest(mm, 2, 2, 0);
}
TEST(TrackMemoryInterval, TestFull) {
int i;
struct MemoryIntervals *mm;
mm = calloc(1, sizeof(struct MemoryIntervals));
for (i = 0; i < ARRAYLEN(mm->p); ++i) {
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, TrackMemoryInterval(mm, i, i, i));
CheckMemoryIntervalsAreOk(mm);
}
CHECK_EQ(-1, TrackMemoryInterval(mm, i, i, i));
CHECK_EQ(ENOMEM, errno);
CheckMemoryIntervalsAreOk(mm);
free(mm);
}
TEST(TrackMemoryInterval, TestAppend) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{1, {{2, 3}}, {0}},
};
RunTrackMemoryIntervalTest(mm, 3, 3, 0);
}
TEST(TrackMemoryInterval, TestPrepend) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{1, {{1, 2}}, {0}},
};
RunTrackMemoryIntervalTest(mm, 1, 1, 0);
}
TEST(TrackMemoryInterval, TestFillHole) {
static const struct MemoryIntervals mm[2] = {
{4, {{1, 1}, {3, 4}, {5, 5}, {6, 8}}, {0, 0, 1, 0}},
{3, {{1, 4}, {5, 5}, {6, 8}}, {0, 1, 0}},
};
RunTrackMemoryIntervalTest(mm, 2, 2, 0);
}
TEST(TrackMemoryInterval, TestAppend2) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{2, {{2, 2}, {3, 3}}, {0, 1}},
};
RunTrackMemoryIntervalTest(mm, 3, 3, 1);
}
TEST(TrackMemoryInterval, TestPrepend2) {
static const struct MemoryIntervals mm[2] = {
{1, {{2, 2}}, {0}},
{2, {{1, 1}, {2, 2}}, {1, 0}},
};
RunTrackMemoryIntervalTest(mm, 1, 1, 1);
}
TEST(TrackMemoryInterval, TestFillHole2) {
static const struct MemoryIntervals mm[2] = {
{4, {{1, 1}, {3, 4}, {5, 5}, {6, 8}}, {0, 0, 1, 0}},
{5, {{1, 1}, {2, 2}, {3, 4}, {5, 5}, {6, 8}}, {0, 1, 0, 1, 0}},
};
RunTrackMemoryIntervalTest(mm, 2, 2, 1);
}
TEST(FindMemoryInterval, Test) {
static const struct MemoryIntervals mm[1] = {
{4,
{
[0] = {1, 1},
[1] = {3, 4},
[2] = {5, 5},
[3] = {6, 8},
},
{0, 0, 1, 0}},
};
EXPECT_EQ(0, FindMemoryInterval(mm, 0));
EXPECT_EQ(0, FindMemoryInterval(mm, 1));
EXPECT_EQ(1, FindMemoryInterval(mm, 2));
EXPECT_EQ(1, FindMemoryInterval(mm, 3));
EXPECT_EQ(1, FindMemoryInterval(mm, 4));
EXPECT_EQ(2, FindMemoryInterval(mm, 5));
EXPECT_EQ(3, FindMemoryInterval(mm, 6));
EXPECT_EQ(3, FindMemoryInterval(mm, 7));
EXPECT_EQ(3, FindMemoryInterval(mm, 8));
EXPECT_EQ(4, FindMemoryInterval(mm, 9));
}
TEST(ReleaseMemoryIntervals, TestEmpty) {
static const struct MemoryIntervals mm[2] = {
{0, {}, {}},
{0, {}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 2, 2);
}
TEST(ReleaseMemoryIntervals, TestRemoveElement_UsesInclusiveRange) {
static const struct MemoryIntervals mm[2] = {
{3, {{0, 0}, {2, 2}, {4, 4}}, {}},
{2, {{0, 0}, {4, 4}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 2, 2);
}
TEST(ReleaseMemoryIntervals, TestPunchHole) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{2, {{0, 3}, {6, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 4, 5);
}
TEST(ReleaseMemoryIntervals, TestShortenLeft) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{0, 7}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 8, 9);
}
TEST(ReleaseMemoryIntervals, TestShortenRight) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 0, 2);
}
TEST(ReleaseMemoryIntervals, TestShortenLeft2) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{0, 7}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 8, 11);
}
TEST(ReleaseMemoryIntervals, TestShortenRight2) {
static const struct MemoryIntervals mm[2] = {
{1, {{0, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, -3, 2);
}
TEST(ReleaseMemoryIntervals, TestZeroZero) {
static const struct MemoryIntervals mm[2] = {
{1, {{3, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 0, 0);
}
TEST(ReleaseMemoryIntervals, TestNoopLeft) {
static const struct MemoryIntervals mm[2] = {
{1, {{3, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 1, 2);
}
TEST(ReleaseMemoryIntervals, TestNoopRight) {
static const struct MemoryIntervals mm[2] = {
{1, {{3, 9}}, {}},
{1, {{3, 9}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 10, 10);
}
TEST(ReleaseMemoryIntervals, TestBigFree) {
static const struct MemoryIntervals mm[2] = {
{2, {{0, 3}, {6, 9}}, {}},
{0, {}, {}},
};
RunReleaseMemoryIntervalsTest(mm, INT_MIN, INT_MAX);
}
TEST(ReleaseMemoryIntervals, TestWeirdGap) {
static const struct MemoryIntervals mm[2] = {
{3, {{10, 10}, {20, 20}, {30, 30}}, {}},
{2, {{10, 10}, {30, 30}}, {}},
};
RunReleaseMemoryIntervalsTest(mm, 15, 25);
}
TEST(ReleaseMemoryIntervals, TestOutOfMemory) {
int i;
struct MemoryIntervals *mm;
mm = calloc(1, sizeof(struct MemoryIntervals));
for (i = 0; i < ARRAYLEN(mm->p); ++i) {
CHECK_NE(-1, TrackMemoryInterval(mm, i * 10, i * 10 + 8, 0));
}
CheckMemoryIntervalsAreOk(mm);
CHECK_EQ(-1, ReleaseMemoryIntervals(mm, 4, 4, NULL));
CHECK_EQ(ENOMEM, errno);
CheckMemoryIntervalsAreOk(mm);
free(mm);
}

View file

@ -21,8 +21,9 @@
#include "libc/bits/xchg.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/mappings.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
@ -33,71 +34,17 @@
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
unsigned m1;
TEST(mmap, testMapUnmapAnonAnyAddr) {
void *p;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p, FRAMESIZE));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testMunmapUnmapsMultiple) {
void *p1, *p2;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p1 = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_NE(MAP_FAILED, (p2 = mmap(NULL, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
if ((intptr_t)p1 > (intptr_t)p2) xchg(&p1, &p2);
EXPECT_EQ(m1 + 2, _mm.i);
EXPECT_NE(-1, munmap(p1, (intptr_t)p2 + (intptr_t)FRAMESIZE - (intptr_t)p1));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testPartialUnmapRight) {
if (1) return; /* naaah */
char *p;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, FRAMESIZE * 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p + FRAMESIZE, FRAMESIZE));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p, FRAMESIZE));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testPartialUnmapLeft) {
if (1) return; /* naaah */
char *p;
m1 = _mm.i;
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, FRAMESIZE * 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p, FRAMESIZE));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_NE(-1, munmap(p + FRAMESIZE, FRAMESIZE));
EXPECT_EQ(m1 + 0, _mm.i);
}
TEST(mmap, testMapFile) {
int fd;
char *p;
char path[PATH_MAX];
sprintf(path, "%s%s.%d", kTmpPath, program_invocation_short_name, getpid());
m1 = _mm.i;
ASSERT_NE(-1, (fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644)));
EXPECT_EQ(5, write(fd, "hello", 5));
EXPECT_NE(-1, fdatasync(fd));
EXPECT_NE(MAP_FAILED, (p = mmap(NULL, 5, PROT_READ, MAP_PRIVATE, fd, 0)));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_STREQ("hello", p);
EXPECT_NE(-1, munmap(p, 5));
EXPECT_EQ(m1 + 0, _mm.i);
EXPECT_NE(-1, close(fd));
EXPECT_NE(-1, unlink(path));
}
@ -106,14 +53,12 @@ TEST(mmap, testMapFile_fdGetsClosed_makesNoDifference) {
int fd;
char *p, buf[16], path[PATH_MAX];
sprintf(path, "%s%s.%d", kTmpPath, program_invocation_short_name, getpid());
m1 = _mm.i;
ASSERT_NE(-1, (fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644)));
EXPECT_EQ(5, write(fd, "hello", 5));
EXPECT_NE(-1, fdatasync(fd));
EXPECT_NE(MAP_FAILED,
(p = mmap(NULL, 5, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)));
EXPECT_NE(-1, close(fd));
EXPECT_EQ(m1 + 1, _mm.i);
EXPECT_STREQ("hello", p);
p[1] = 'a';
EXPECT_NE(-1, msync(p, PAGESIZE, MS_SYNC));
@ -122,12 +67,11 @@ TEST(mmap, testMapFile_fdGetsClosed_makesNoDifference) {
EXPECT_STREQN("hallo", buf, 5);
EXPECT_NE(-1, close(fd));
EXPECT_NE(-1, munmap(p, 5));
EXPECT_EQ(m1 + 0, _mm.i);
EXPECT_NE(-1, unlink(path));
}
TEST(mmap, testMapFixed_destroysEverythingInItsPath) {
m1 = _mm.i;
unsigned m1 = _mmi.i;
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedMappingsStart + FRAMESIZE * 0),
FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
@ -137,10 +81,31 @@ TEST(mmap, testMapFixed_destroysEverythingInItsPath) {
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedMappingsStart + FRAMESIZE * 2),
FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
ASSERT_EQ(m1 + 3, _mm.i);
EXPECT_NE(MAP_FAILED, mmap((void *)(kFixedMappingsStart + FRAMESIZE * 0),
FRAMESIZE * 3, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
ASSERT_EQ(m1 + 1, _mm.i);
ASSERT_GT(_mmi.i, m1);
EXPECT_NE(-1, munmap((void *)kFixedMappingsStart, FRAMESIZE * 3));
ASSERT_EQ(m1, _mmi.i);
}
TEST(isheap, nullPtr) {
ASSERT_FALSE(isheap(NULL));
}
TEST(isheap, stackMemory) {
int boop;
ASSERT_FALSE(isheap(&boop));
}
TEST(isheap, malloc) {
ASSERT_TRUE(isheap(gc(malloc(1))));
}
TEST(isheap, emptyMalloc) {
ASSERT_TRUE(isheap(gc(malloc(0))));
}
TEST(isheap, mallocOffset) {
ASSERT_TRUE(isheap((char *)gc(malloc(131072)) + 100000));
}

View file

@ -5,12 +5,14 @@ PKGS += TEST_LIBC_RUNTIME
TEST_LIBC_RUNTIME_SRCS := $(wildcard test/libc/runtime/*.c)
TEST_LIBC_RUNTIME_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_RUNTIME_SRCS))
TEST_LIBC_RUNTIME_COMS = $(TEST_LIBC_RUNTIME_OBJS:%.o=%.com)
TEST_LIBC_RUNTIME_OBJS = \
$(TEST_LIBC_RUNTIME_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_LIBC_RUNTIME_SRCS:%.c=o/$(MODE)/%.o)
TEST_LIBC_RUNTIME_COMS = \
$(TEST_LIBC_RUNTIME_SRCS:%.c=o/$(MODE)/%.com)
TEST_LIBC_RUNTIME_BINS = \
$(TEST_LIBC_RUNTIME_COMS) \
$(TEST_LIBC_RUNTIME_COMS:%=%.dbg)
@ -22,13 +24,16 @@ TEST_LIBC_RUNTIME_CHECKS = \
$(TEST_LIBC_RUNTIME_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_LIBC_RUNTIME_DIRECTDEPS = \
LIBC_ALG \
LIBC_CALLS \
LIBC_CALLS_HEFTY \
LIBC_FMT \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_LOG \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
@ -60,6 +65,7 @@ o/$(MODE)/test/libc/runtime/getenv_test.com.runs: \
o/$(MODE)/test/libc/runtime/getenv_test.com
@HELLO=THERE build/runit $@ $<
o/$(MODE)/test/libc/runtime/fun_test.o \
o/$(MODE)/test/libc/runtime/itsatrap_test.o: \
OVERRIDE_CFLAGS += \
-fno-sanitize=all \