mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 10:48:29 +00:00
Make terminal ui binaries work well everywhere
Here's some screenshots of an emulator tui program that was compiled on Linux, then scp'd it to Windows, Mac, and FreeBSD. https://justine.storage.googleapis.com/blinkenlights-cmdexe.png https://justine.storage.googleapis.com/blinkenlights-imac.png https://justine.storage.googleapis.com/blinkenlights-freebsd.png https://justine.storage.googleapis.com/blinkenlights-lisp.png How is this even possible that we have a nontrivial ui binary that just works on Mac, Windows, Linux, and BSD? Surely a first ever achievement. Fixed many bugs. Bootstrapped John McCarthy's metacircular evaluator on bare metal in half the size of Altair BASIC (about 2.5kb) and ran it in emulator for fun and profit.
This commit is contained in:
parent
680daf1210
commit
9e3e985ae5
276 changed files with 7026 additions and 3790 deletions
|
@ -48,7 +48,6 @@ void OnSigFpe(void) {
|
|||
|
||||
void SetUp(void) {
|
||||
m->xedd = xedd;
|
||||
InitMachine(m);
|
||||
CHECK_NE(-1, xsigaction(SIGFPE, OnSigFpe, SA_NODEFER, 0, oldsigfpe));
|
||||
}
|
||||
|
||||
|
|
85
test/tool/build/lib/iovs_test.c
Normal file
85
test/tool/build/lib/iovs_test.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*-*- 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/testlib/testlib.h"
|
||||
#include "tool/build/lib/iovs.h"
|
||||
|
||||
TEST(iovs, testEmpty) {
|
||||
struct Iovs iv;
|
||||
InitIovs(&iv);
|
||||
EXPECT_EQ(0, iv.i);
|
||||
EXPECT_GE(iv.n, iv.i);
|
||||
FreeIovs(&iv);
|
||||
}
|
||||
|
||||
TEST(iovs, testAppendEmptyString_wontCreateEmptyEntries) {
|
||||
struct Iovs iv;
|
||||
InitIovs(&iv);
|
||||
EXPECT_NE(-1, AppendIovs(&iv, "", 0));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, NULL, 0));
|
||||
EXPECT_EQ(0, iv.i);
|
||||
EXPECT_GE(iv.n, iv.i);
|
||||
FreeIovs(&iv);
|
||||
}
|
||||
|
||||
TEST(iovs, testAppendContiguousVectors_coalescesAdjacentEntries) {
|
||||
struct Iovs iv;
|
||||
char buf[8], *b = buf;
|
||||
InitIovs(&iv);
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 0, 4));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 4, 4));
|
||||
EXPECT_EQ(1, iv.i);
|
||||
EXPECT_GE(iv.n, iv.i);
|
||||
EXPECT_EQ(8, iv.p[0].iov_len);
|
||||
EXPECT_EQ(b, iv.p[0].iov_base);
|
||||
FreeIovs(&iv);
|
||||
}
|
||||
|
||||
TEST(iovs, testAppendNoncontiguousVectors_growsMemoryAndPreservesOrdering) {
|
||||
struct Iovs iv;
|
||||
char buf[8], *b = buf;
|
||||
InitIovs(&iv);
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 0, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 2, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 4, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 6, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 1, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 3, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 5, 1));
|
||||
EXPECT_NE(-1, AppendIovs(&iv, b + 7, 1));
|
||||
EXPECT_EQ(8, iv.i);
|
||||
EXPECT_GE(iv.n, iv.i);
|
||||
EXPECT_EQ(b + 0, iv.p[0].iov_base);
|
||||
EXPECT_EQ(1, iv.p[0].iov_len);
|
||||
EXPECT_EQ(b + 2, iv.p[1].iov_base);
|
||||
EXPECT_EQ(1, iv.p[1].iov_len);
|
||||
EXPECT_EQ(b + 4, iv.p[2].iov_base);
|
||||
EXPECT_EQ(1, iv.p[2].iov_len);
|
||||
EXPECT_EQ(b + 6, iv.p[3].iov_base);
|
||||
EXPECT_EQ(1, iv.p[3].iov_len);
|
||||
EXPECT_EQ(b + 1, iv.p[4].iov_base);
|
||||
EXPECT_EQ(1, iv.p[4].iov_len);
|
||||
EXPECT_EQ(b + 3, iv.p[5].iov_base);
|
||||
EXPECT_EQ(1, iv.p[5].iov_len);
|
||||
EXPECT_EQ(b + 5, iv.p[6].iov_base);
|
||||
EXPECT_EQ(1, iv.p[6].iov_len);
|
||||
EXPECT_EQ(b + 7, iv.p[7].iov_base);
|
||||
EXPECT_EQ(1, iv.p[7].iov_len);
|
||||
FreeIovs(&iv);
|
||||
}
|
|
@ -98,27 +98,30 @@ const uint8_t kTenthprime2[] = {
|
|||
0xC3, //
|
||||
};
|
||||
|
||||
int64_t base;
|
||||
uint8_t *real;
|
||||
size_t realsize;
|
||||
struct Machine *m;
|
||||
|
||||
void SetUp(void) {
|
||||
base = 0;
|
||||
m = NewMachine();
|
||||
m->cr3 = MallocPage();
|
||||
realsize = 0x10000;
|
||||
real = tmemalign(PAGESIZE, ROUNDUP(realsize, PAGESIZE));
|
||||
RegisterMemory(m, base, real, realsize);
|
||||
m->ip = base;
|
||||
Write64(m->sp, m->ip + realsize);
|
||||
m->ip = 0;
|
||||
ReserveVirtual(m, 0, 4096);
|
||||
ASSERT_EQ(0x5000, m->real.i);
|
||||
ASSERT_EQ(0x1007, Read64(m->real.p + 0x0000)); // PML4T
|
||||
ASSERT_EQ(0x2007, Read64(m->real.p + 0x1000)); // PDPT
|
||||
ASSERT_EQ(0x3007, Read64(m->real.p + 0x2000)); // PDE
|
||||
ASSERT_EQ(0x4007, Read64(m->real.p + 0x3000)); // PT
|
||||
Write64(m->sp, 4096);
|
||||
}
|
||||
|
||||
void TearDown(void) {
|
||||
ResetRam(m);
|
||||
free(m->cr3);
|
||||
tfree(real);
|
||||
free(m);
|
||||
FreeVirtual(m, 0, 4096);
|
||||
ASSERT_EQ(0x5000, m->real.i);
|
||||
ASSERT_EQ(0x1007, Read64(m->real.p + 0x0000)); // PML4T
|
||||
ASSERT_EQ(0x2007, Read64(m->real.p + 0x1000)); // PDPT
|
||||
ASSERT_EQ(0x3007, Read64(m->real.p + 0x2000)); // PDE
|
||||
ASSERT_EQ(0x0000, Read64(m->real.p + 0x3000)); // PT
|
||||
ASSERT_EQ(0x4000, m->realfree->i);
|
||||
ASSERT_EQ(0x1000, m->realfree->n);
|
||||
FreeMachine(m);
|
||||
}
|
||||
|
||||
int ExecuteUntilHalt(struct Machine *m) {
|
||||
|
@ -134,121 +137,121 @@ int ExecuteUntilHalt(struct Machine *m) {
|
|||
}
|
||||
|
||||
TEST(machine, test) {
|
||||
memcpy(real, kTenthprime, sizeof(kTenthprime));
|
||||
VirtualRecv(m, 0, kTenthprime, sizeof(kTenthprime));
|
||||
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
|
||||
ASSERT_EQ(15, Read32(m->ax));
|
||||
}
|
||||
|
||||
TEST(machine, testFpu) {
|
||||
memcpy(real, kPi80, sizeof(kPi80));
|
||||
VirtualRecv(m, 0, kPi80, sizeof(kPi80));
|
||||
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
|
||||
ASSERT_TRUE(fabs(3.14159 - FpuPop(m)) < 0.0001);
|
||||
m->ip = base;
|
||||
m->ip = 0;
|
||||
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
|
||||
ASSERT_TRUE(fabs(3.14159 - FpuPop(m)) < 0.0001);
|
||||
}
|
||||
|
||||
BENCH(machine, benchPrimeNumberPrograms) {
|
||||
memcpy(real, kTenthprime2, sizeof(kTenthprime2));
|
||||
EZBENCH2("tenthprime2", m->ip = base, ExecuteUntilHalt(m));
|
||||
VirtualRecv(m, 0, kTenthprime2, sizeof(kTenthprime2));
|
||||
EZBENCH2("tenthprime2", m->ip = 0, ExecuteUntilHalt(m));
|
||||
ASSERT_EQ(15, Read32(m->ax));
|
||||
memcpy(real, kTenthprime, sizeof(kTenthprime));
|
||||
EZBENCH2("tenthprime", m->ip = base, ExecuteUntilHalt(m));
|
||||
VirtualRecv(m, 0, kTenthprime, sizeof(kTenthprime));
|
||||
EZBENCH2("tenthprime", m->ip = 0, ExecuteUntilHalt(m));
|
||||
ASSERT_EQ(15, Read32(m->ax));
|
||||
}
|
||||
|
||||
BENCH(machine, benchFpu) {
|
||||
memcpy(real, kPi80, sizeof(kPi80));
|
||||
EZBENCH2("pi80", m->ip = base, PROGN(ExecuteUntilHalt(m), FpuPop(m)));
|
||||
VirtualRecv(m, 0, kPi80, sizeof(kPi80));
|
||||
EZBENCH2("pi80", m->ip = 0, PROGN(ExecuteUntilHalt(m), FpuPop(m)));
|
||||
}
|
||||
|
||||
BENCH(machine, benchLoadExec2) {
|
||||
uint8_t kMovCode[] = {0xbe, 0x03, 0x00, 0x00, 0x00};
|
||||
memcpy(real, kMovCode, sizeof(kMovCode));
|
||||
VirtualRecv(m, 0, kMovCode, sizeof(kMovCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("mov", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("mov", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchLoadExec3) {
|
||||
uint8_t kMovdCode[] = {0x66, 0x0f, 0x6e, 0xc0};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kMovdCode, sizeof(kMovdCode));
|
||||
VirtualRecv(m, 0, kMovdCode, sizeof(kMovdCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("movd", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("movd", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchLoadExec4) {
|
||||
uint8_t kAddpsRegregCode[] = {0x0f, 0x58, 0xC0};
|
||||
uint8_t kAddpsMemregCode[] = {0x0f, 0x58, 0x00};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kAddpsRegregCode, sizeof(kAddpsRegregCode));
|
||||
VirtualRecv(m, 0, kAddpsRegregCode, sizeof(kAddpsRegregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("addps reg reg", m->ip = base, ExecuteInstruction(m));
|
||||
memcpy(real, kAddpsMemregCode, sizeof(kAddpsMemregCode));
|
||||
EZBENCH2("addps reg reg", m->ip = 0, ExecuteInstruction(m));
|
||||
VirtualRecv(m, 0, kAddpsMemregCode, sizeof(kAddpsMemregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("addps mem reg", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("addps mem reg", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchLoadExec5) {
|
||||
uint8_t kPaddwRegregCode[] = {0x66, 0x0F, 0xFD, 0xC0};
|
||||
uint8_t kPaddwMemregCode[] = {0x66, 0x0F, 0xFD, 0x00};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kPaddwRegregCode, sizeof(kPaddwRegregCode));
|
||||
VirtualRecv(m, 0, kPaddwRegregCode, sizeof(kPaddwRegregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("paddw", m->ip = base, ExecuteInstruction(m));
|
||||
memcpy(real, kPaddwMemregCode, sizeof(kPaddwMemregCode));
|
||||
EZBENCH2("paddw", m->ip = 0, ExecuteInstruction(m));
|
||||
VirtualRecv(m, 0, kPaddwMemregCode, sizeof(kPaddwMemregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("paddw mem", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("paddw mem", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchLoadExec6) {
|
||||
uint8_t kPsubqRegregCode[] = {0x66, 0x0F, 0xFB, 0xC0};
|
||||
uint8_t kPsubqMemregCode[] = {0x66, 0x0F, 0xFB, 0x00};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kPsubqRegregCode, sizeof(kPsubqRegregCode));
|
||||
VirtualRecv(m, 0, kPsubqRegregCode, sizeof(kPsubqRegregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("psubq", m->ip = base, ExecuteInstruction(m));
|
||||
memcpy(real, kPsubqMemregCode, sizeof(kPsubqMemregCode));
|
||||
EZBENCH2("psubq", m->ip = 0, ExecuteInstruction(m));
|
||||
VirtualRecv(m, 0, kPsubqMemregCode, sizeof(kPsubqMemregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("psubq mem", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("psubq mem", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchAddqMem) {
|
||||
uint8_t kAddMemregCode[] = {0x48, 0x03, 0x08};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kAddMemregCode, sizeof(kAddMemregCode));
|
||||
VirtualRecv(m, 0, kAddMemregCode, sizeof(kAddMemregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("addq mem", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("addq mem", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchAddlMem) {
|
||||
uint8_t kAddMemregCode[] = {0x03, 0x08};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kAddMemregCode, sizeof(kAddMemregCode));
|
||||
VirtualRecv(m, 0, kAddMemregCode, sizeof(kAddMemregCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("addl mem", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("addl mem", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchAddq) {
|
||||
uint8_t kAddqCode[] = {0x48, 0x01, 0xd8};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kAddqCode, sizeof(kAddqCode));
|
||||
VirtualRecv(m, 0, kAddqCode, sizeof(kAddqCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("addq", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("addq", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchAddb) {
|
||||
uint8_t kAddbCode[] = {0x00, 0xd8};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kAddbCode, sizeof(kAddbCode));
|
||||
VirtualRecv(m, 0, kAddbCode, sizeof(kAddbCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("addb", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("addb", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchXorReg) {
|
||||
memcpy(real, kTenthprime, sizeof(kTenthprime));
|
||||
VirtualRecv(m, 0, kTenthprime, sizeof(kTenthprime));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("xor", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("xor", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchLoadExec8) {
|
||||
|
@ -257,16 +260,16 @@ BENCH(machine, benchLoadExec8) {
|
|||
OpFinit(m);
|
||||
*FpuSt(m, 0) = M_PI;
|
||||
FpuSetTag(m, 0, kFpuTagValid);
|
||||
memcpy(real, kFchsCode, sizeof(kFchsCode));
|
||||
VirtualRecv(m, 0, kFchsCode, sizeof(kFchsCode));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("fchs", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("fchs", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchPushpop) {
|
||||
uint8_t kPushpop[] = {0x50, 0x58};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kPushpop, sizeof(kPushpop));
|
||||
EZBENCH2("pushpop", m->ip = base,
|
||||
VirtualRecv(m, 0, kPushpop, sizeof(kPushpop));
|
||||
EZBENCH2("pushpop", m->ip = 0,
|
||||
PROGN(LoadInstruction(m), ExecuteInstruction(m), LoadInstruction(m),
|
||||
ExecuteInstruction(m)));
|
||||
}
|
||||
|
@ -274,29 +277,27 @@ BENCH(machine, benchPushpop) {
|
|||
BENCH(machine, benchPause) {
|
||||
uint8_t kPause[] = {0xf3, 0x90};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kPause, sizeof(kPause));
|
||||
VirtualRecv(m, 0, kPause, sizeof(kPause));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("pause", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("pause", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchClc) {
|
||||
uint8_t kClc[] = {0xf8};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kClc, sizeof(kClc));
|
||||
VirtualRecv(m, 0, kClc, sizeof(kClc));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("clc", m->ip = base, ExecuteInstruction(m));
|
||||
EZBENCH2("clc", m->ip = 0, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
BENCH(machine, benchNop) {
|
||||
uint8_t kNop[] = {0x90};
|
||||
Write64(m->ax, 0);
|
||||
memcpy(real, kNop, sizeof(kNop));
|
||||
VirtualRecv(m, 0, kNop, sizeof(kNop));
|
||||
LoadInstruction(m);
|
||||
EZBENCH2("nop", m->ip = base, ExecuteInstruction(m));
|
||||
}
|
||||
|
||||
TEST(machine, sizeIsReasonable) {
|
||||
ASSERT_LE(sizeof(struct Machine), 65536 * 3);
|
||||
EZBENCH2("nop", m->ip = 0, ExecuteInstruction(m));
|
||||
EZBENCH2("nop w/ load", m->ip = 0,
|
||||
PROGN(LoadInstruction(m), ExecuteInstruction(m)));
|
||||
}
|
||||
|
||||
TEST(x87, fprem1) {
|
||||
|
@ -308,7 +309,7 @@ TEST(x87, fprem1) {
|
|||
0xf4, // hlt
|
||||
0x00, 0x00, 0xc0, 0xbf, // .float -1.5
|
||||
};
|
||||
memcpy(real, prog, sizeof(prog));
|
||||
VirtualRecv(m, 0, prog, sizeof(prog));
|
||||
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
|
||||
ASSERT_LDBL_EQ(1, FpuPop(m));
|
||||
}
|
||||
|
@ -323,7 +324,11 @@ TEST(x87, fprem2) {
|
|||
0x00, 0x60, 0x5e, 0x75, 0x64, 0xd9, 0x45, 0x43, //
|
||||
0x5b, 0x14, 0xea, 0x9d, 0x77, 0xb2, 0x0b, 0x3d, //
|
||||
};
|
||||
memcpy(real, prog, sizeof(prog));
|
||||
VirtualRecv(m, 0, prog, sizeof(prog));
|
||||
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
|
||||
ASSERT_LDBL_EQ(1.1766221079117338e-14, FpuPop(m));
|
||||
}
|
||||
|
||||
TEST(machine, sizeIsReasonable) {
|
||||
ASSERT_LE(sizeof(struct Machine), 65536 * 3);
|
||||
}
|
||||
|
|
|
@ -1,158 +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/log/check.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "tool/build/lib/memory.h"
|
||||
#include "tool/build/lib/pml4t.h"
|
||||
|
||||
struct Unmapped {
|
||||
size_t i;
|
||||
struct UnmappedEntry {
|
||||
void *addr;
|
||||
size_t size;
|
||||
} p[8];
|
||||
};
|
||||
|
||||
struct Unmapped unmapped;
|
||||
uint64_t *pt2, *pt3, *pt4;
|
||||
pml4t_t cr3;
|
||||
|
||||
void *NewPage(void) {
|
||||
void *p;
|
||||
p = tmemalign(4096, 4096);
|
||||
CHECK_ALIGNED(4096, p);
|
||||
memset(p, 0, 4096);
|
||||
return p;
|
||||
}
|
||||
|
||||
void FreePage(void *p) {
|
||||
tfree(p);
|
||||
}
|
||||
|
||||
int FakeMunmap(void *addr, size_t size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MockMunmap(void *addr, size_t size) {
|
||||
CHECK_LT(unmapped.i, ARRAYLEN(unmapped.p));
|
||||
unmapped.p[unmapped.i].addr = addr;
|
||||
unmapped.p[unmapped.i].size = size;
|
||||
unmapped.i++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SetUp(void) {
|
||||
memset(cr3, 0, sizeof(cr3));
|
||||
}
|
||||
|
||||
void TearDown(void) {
|
||||
unmapped.i = 0;
|
||||
FreePml4t(cr3, -0x800000000000, 0x800000000000, FreePage, FakeMunmap);
|
||||
}
|
||||
|
||||
TEST(pml4t, testHighestAddress) {
|
||||
ASSERT_NE(-1,
|
||||
RegisterPml4t(cr3, 0x7fffffffe000, 0x3133700000, 0x2000, NewPage));
|
||||
ASSERT_TRUE(IsValidPage(cr3[255]));
|
||||
pt2 = UnmaskPageAddr(cr3[255]);
|
||||
ASSERT_TRUE(IsValidPage(pt2[511]));
|
||||
pt3 = UnmaskPageAddr(pt2[511]);
|
||||
ASSERT_TRUE(IsValidPage(pt3[511]));
|
||||
pt4 = UnmaskPageAddr(pt3[511]);
|
||||
ASSERT_TRUE(IsValidPage(pt4[510]));
|
||||
ASSERT_TRUE(IsValidPage(pt4[511]));
|
||||
EXPECT_EQ(0x3133700000, UnmaskPageAddr(pt4[510]));
|
||||
EXPECT_EQ(0x3133701000, UnmaskPageAddr(pt4[511]));
|
||||
}
|
||||
|
||||
TEST(pml4t, testLowestAddress) {
|
||||
ASSERT_NE(-1,
|
||||
RegisterPml4t(cr3, -0x800000000000, 0x31337000, 0x2000, NewPage));
|
||||
ASSERT_TRUE(IsValidPage(cr3[256]));
|
||||
pt2 = UnmaskPageAddr(cr3[256]);
|
||||
ASSERT_TRUE(IsValidPage(pt2[0]));
|
||||
pt3 = UnmaskPageAddr(pt2[0]);
|
||||
ASSERT_TRUE(IsValidPage(pt3[0]));
|
||||
pt4 = UnmaskPageAddr(pt3[0]);
|
||||
ASSERT_TRUE(IsValidPage(pt4[0]));
|
||||
ASSERT_TRUE(IsValidPage(pt4[1]));
|
||||
}
|
||||
|
||||
TEST(pml4t, testOverlapsExistingRegistration_overwritesRegistration) {
|
||||
ASSERT_NE(-1,
|
||||
RegisterPml4t(cr3, 0x7fffffffe000, 0x31337000, 0x1000, NewPage));
|
||||
ASSERT_TRUE(IsValidPage(cr3[255]));
|
||||
pt2 = UnmaskPageAddr(cr3[255]);
|
||||
ASSERT_TRUE(IsValidPage(pt2[511]));
|
||||
pt3 = UnmaskPageAddr(pt2[511]);
|
||||
ASSERT_TRUE(IsValidPage(pt3[511]));
|
||||
pt4 = UnmaskPageAddr(pt3[511]);
|
||||
EXPECT_TRUE(IsValidPage(pt4[510]));
|
||||
EXPECT_EQ(0x31337000, UnmaskPageAddr(pt4[510]));
|
||||
ASSERT_NE(-1, RegisterPml4t(cr3, 0x7fffffffe000, 0x31337000 + 0x1000, 0x1000,
|
||||
NewPage));
|
||||
EXPECT_TRUE(IsValidPage(pt4[510]));
|
||||
EXPECT_EQ(0x31337000 + 0x1000, UnmaskPageAddr(pt4[510]));
|
||||
}
|
||||
|
||||
TEST(pml4t, testFindPml4t_holeTooSmall_skipsOver) {
|
||||
ASSERT_NE(-1, RegisterPml4t(cr3, 0x700000000, 0, 0x1000, NewPage));
|
||||
ASSERT_NE(-1, RegisterPml4t(cr3, 0x700005000, 0, 0x1000, NewPage));
|
||||
ASSERT_EQ(0x700001000, FindPml4t(cr3, 0x700000000, 0x01000));
|
||||
ASSERT_EQ(0x700006000, FindPml4t(cr3, 0x700000000, 0x10000));
|
||||
}
|
||||
|
||||
TEST(pml4t, testFindPml4t_bigAllocation) {
|
||||
ASSERT_EQ(0x00200000, FindPml4t(cr3, 0x00200000, 0x00400000));
|
||||
ASSERT_EQ(0x00201000, FindPml4t(cr3, 0x00201000, 0x00400000));
|
||||
ASSERT_EQ(0xff200000, FindPml4t(cr3, 0xff200000, 0x00400000));
|
||||
ASSERT_EQ(0xff201000, FindPml4t(cr3, 0xff201000, 0x00400000));
|
||||
}
|
||||
|
||||
TEST(pml4t, testFreePmlt) {
|
||||
ASSERT_NE(-1, RegisterPml4t(cr3, 0x000005000, 0x123000, 0x2000, NewPage));
|
||||
ASSERT_NE(-1, RegisterPml4t(cr3, 0x000000000, 0x321000, 0x1000, NewPage));
|
||||
ASSERT_NE(-1, FreePml4t(cr3, -0x800000000000, 0x800000000000, FreePage,
|
||||
MockMunmap));
|
||||
ASSERT_EQ(2, unmapped.i);
|
||||
EXPECT_EQ(0x321000, unmapped.p[0].addr);
|
||||
EXPECT_EQ(0x1000, unmapped.p[0].size);
|
||||
EXPECT_EQ(0x123000, unmapped.p[1].addr);
|
||||
EXPECT_EQ(0x2000, unmapped.p[1].size);
|
||||
}
|
||||
|
||||
BENCH(pml4t, bench) {
|
||||
EZBENCH2("RegisterPml4t 1mb fresh",
|
||||
FreePml4t(cr3, -0x800000000000, 0x800000000000, free, FakeMunmap),
|
||||
RegisterPml4t(cr3, 0x00100000, 0x31337000, 0x00100000, MallocPage));
|
||||
EZBENCH2("RegisterPml4t 1gb fresh",
|
||||
FreePml4t(cr3, -0x800000000000, 0x800000000000, free, FakeMunmap),
|
||||
RegisterPml4t(cr3, 0x40000000, 0x31337000, 0x40000000, MallocPage));
|
||||
EZBENCH2("RegisterPml4t 1mb overwrite", donothing,
|
||||
RegisterPml4t(cr3, 0x00100000, 0x31337000, 0x00100000, MallocPage));
|
||||
EZBENCH2("RegisterPml4t 1gb overwrite", donothing,
|
||||
RegisterPml4t(cr3, 0x40000000, 0x31337000, 0x40000000, MallocPage));
|
||||
FreePml4t(cr3, -0x800000000000, 0x800000000000, free, FakeMunmap);
|
||||
}
|
235
test/tool/build/lib/pty_test.c
Normal file
235
test/tool/build/lib/pty_test.c
Normal file
|
@ -0,0 +1,235 @@
|
|||
/*-*- 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/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/unicode/unicode.h"
|
||||
#include "tool/build/lib/pty.h"
|
||||
|
||||
char *render(struct MachinePty *pty) {
|
||||
static struct Buffer b;
|
||||
int y;
|
||||
b.i = 0;
|
||||
for (y = 0; y < pty->yn; ++y) {
|
||||
MachinePtyAppendLine(pty, &b, y);
|
||||
}
|
||||
b.p[b.i] = 0;
|
||||
return b.p;
|
||||
}
|
||||
|
||||
const char widelatin[] aligned(16) = "\
|
||||
A-BCDEFGHIJKLMNOPQRSTUVWXYZ\r\n\
|
||||
ab-cdefghijklmnopqrstuvwxyz\r\n\
|
||||
012-3456789\r\n\
|
||||
ABCD-EFGHIJKLMNOPQRSTUVWXYZ\r\n\
|
||||
abcde-fghijklmnopqrstuvwxyz\r\n\
|
||||
012345-6789\r\n\
|
||||
ABCDEFG-HIJKLMNOPQRSTUVWXYZ\r\n\
|
||||
abcdefgh-ijklmnopqrstuvwxyz\r\n\
|
||||
012345678-9";
|
||||
|
||||
static const char widelatin_golden[] = "\
|
||||
A-BCDEFGHIJKLMNOPQRSTUVWXYZ \
|
||||
ab-cdefghijklmnopqrstuvwxyz \
|
||||
012-3456789 \
|
||||
ABCD-EFGHIJKLMNOPQRSTUVWXYZ \
|
||||
abcde-fghijklmnopqrstuvwxyz \
|
||||
012345-6789 \
|
||||
ABCDEFG-HIJKLMNOPQRSTUVWXYZ \
|
||||
abcdefgh-ijklmnopqrstuvwxyz \
|
||||
012345678-9▂ \
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
";
|
||||
|
||||
TEST(pty, testFunWidth) {
|
||||
struct MachinePty *pty = MachinePtyNew();
|
||||
MachinePtyWrite(pty, widelatin, ARRAYLEN(widelatin) - 1);
|
||||
EXPECT_STREQ(widelatin_golden, render(pty));
|
||||
MachinePtyFree(pty);
|
||||
}
|
||||
|
||||
const char hyperion[] aligned(16) = "\
|
||||
Fanatics have their dreams, wherewith they weave \
|
||||
A paradise for a sect; the savage too \
|
||||
From forth the loftiest fashion of his sleep \
|
||||
Guesses at Heaven; pity these have not \
|
||||
Trac'd upon vellum or wild Indian leaf \
|
||||
The shadows of melodious utterance. \
|
||||
But bare of laurel they live, dream, and die; \
|
||||
For Poesy alone can tell her dreams, \
|
||||
With the fine spell of words alone can save \
|
||||
Imagination from the sable charm \
|
||||
And dumb enchantment. Who alive can say, \
|
||||
'Thou art no Poet may'st not tell thy dreams?' \
|
||||
Since every man whose soul is not a clod \
|
||||
Hath visions, and would speak, if he had loved \
|
||||
And been well nurtured in his mother tongue. \
|
||||
Whether the dream now purpos'd to rehearse \
|
||||
Be poet's or fanatic's will be known \
|
||||
When this warm scribe my hand is in the grave.";
|
||||
|
||||
static const wchar_t hyperion_golden[] = L"\
|
||||
Fanatics have their dreams, wherewith they weave A paradise for a sect; the sava\
|
||||
ge too From forth the loftiest fashion of his sleep Guesses at Heaven; pity thes\
|
||||
e have not Trac'd upon vellum or wild Indian leaf The shadows of melodious utter\
|
||||
ance. But bare of laurel they live, dream, and die; For Poesy alone can tell her\
|
||||
dreams, With the fine spell of words alone can save Imagination from the sable \
|
||||
charm And dumb enchantment. Who alive can say, 'Thou art no Poet may'st not tell\
|
||||
thy dreams?' Since every man whose soul is not a clod Hath visions, and would s\
|
||||
peak, if he had loved And been well nurtured in his mother tongue. Whether the d\
|
||||
ream now purpos'd to rehearse Be poet's or fanatic's will be known When this war\
|
||||
m scribe my hand is in the grave. \
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
\
|
||||
";
|
||||
|
||||
TEST(pty, testPureAscii_benefitsFromVectorization) {
|
||||
struct MachinePty *pty = MachinePtyNew();
|
||||
MachinePtyWrite(pty, hyperion, ARRAYLEN(hyperion) - 1);
|
||||
EXPECT_STREQN(pty->wcs, hyperion_golden, ARRAYLEN(hyperion_golden) - 1);
|
||||
MachinePtyFree(pty);
|
||||
}
|
||||
|
||||
static const char kKiloAnsi[] = "\
|
||||
\e[?25l\e[H\
|
||||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐\e[39m\e[0K\r\n\
|
||||
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi─────────\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# SYNOPSIS\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# Freestanding Hermetically-Sealed Monolithic Repository\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# REQUIREMENTS\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# You can run your programs on any operating system, but you have\e[39m\e[0K\r\n\
|
||||
# to build them on Linux 2.6+ (or WSL) using GNU Make. A modern C\e[39m\e[0K\r\n\
|
||||
# compiler that\'s statically-linked comes included as a courtesy.\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# EXAMPLES\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# # build and run everything\e[39m\e[0K\r\n\
|
||||
# make -j8 -O\e[39m\e[0K\r\n\
|
||||
# make -j8 -O MODE=dbg\e[39m\e[0K\r\n\
|
||||
# make -j8 -O MODE=opt\e[39m\e[0K\r\n\
|
||||
# make -j8 -O MODE=rel\e[39m\e[0K\r\n\
|
||||
# make -j8 -O MODE=tiny\e[39m\e[0K\r\n\
|
||||
#\e[39m\e[0K\r\n\
|
||||
# # build individual target\e[39m\e[0K\r\n\
|
||||
\e[0K\e[7mMakefile - 340 lines 1/340\e[0m\r\n\
|
||||
\e[0KHELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find\e[1;1H\e[?25h";
|
||||
|
||||
static const wchar_t kKiloGolden[] = L"\
|
||||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ \
|
||||
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────── \
|
||||
# \
|
||||
# SYNOPSIS \
|
||||
# \
|
||||
# Freestanding Hermetically-Sealed Monolithic Repository \
|
||||
# \
|
||||
# REQUIREMENTS \
|
||||
# \
|
||||
# You can run your programs on any operating system, but you have \
|
||||
# to build them on Linux 2.6+ (or WSL) using GNU Make. A modern C \
|
||||
# compiler that's statically-linked comes included as a courtesy. \
|
||||
# \
|
||||
# EXAMPLES \
|
||||
# \
|
||||
# # build and run everything \
|
||||
# make -j8 -O \
|
||||
# make -j8 -O MODE=dbg \
|
||||
# make -j8 -O MODE=opt \
|
||||
# make -j8 -O MODE=rel \
|
||||
# make -j8 -O MODE=tiny \
|
||||
# \
|
||||
# # build individual target \
|
||||
Makefile - 340 lines 1/340\
|
||||
HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find \
|
||||
";
|
||||
|
||||
TEST(pty, testLongestPossibleCharacter) {
|
||||
EXPECT_EQ(60, strlen("\e[21;22;27;24;25;29;38;2;255;255;255;48;2;255;255;"
|
||||
"255m\377\277\277\277\277\277"));
|
||||
struct Buffer b = {0};
|
||||
struct MachinePty *pty = MachinePtyNew();
|
||||
const char *s = "\e[1;2;3;4;5;6;7;9m"
|
||||
"h"
|
||||
"\e[0;"
|
||||
"38;2;255;255;255;"
|
||||
"48;2;255;255;255m"
|
||||
"\377\277\277\277\277\277"
|
||||
"\e[0m";
|
||||
MachinePtyWrite(pty, s, strlen(s));
|
||||
MachinePtyAppendLine(pty, &b, 0);
|
||||
AppendChar(&b, '\0');
|
||||
EXPECT_STREQ("\e[1;2;4;7;5;9m"
|
||||
"𝒉"
|
||||
"\e[22;24;27;25;29;38;2;255;255;255;48;2;255;255;255m"
|
||||
"\377\277\277\277\277\277"
|
||||
"\e[0m▂ "
|
||||
" ",
|
||||
b.p);
|
||||
MachinePtyFree(pty);
|
||||
free(b.p);
|
||||
}
|
||||
|
||||
TEST(pty, test) {
|
||||
struct MachinePty *pty = MachinePtyNew();
|
||||
MachinePtyWrite(pty, kKiloAnsi, strlen(kKiloAnsi));
|
||||
EXPECT_STREQN(kKiloGolden, pty->wcs, wcslen(kKiloGolden));
|
||||
MachinePtyFree(pty);
|
||||
}
|
||||
|
||||
BENCH(pty, bench) {
|
||||
struct MachinePty *pty = MachinePtyNew();
|
||||
EZBENCH2("pty write ascii", donothing,
|
||||
MachinePtyWrite(pty, hyperion, sizeof(hyperion) - 1));
|
||||
EZBENCH2("pty write kilo", donothing,
|
||||
MachinePtyWrite(pty, kKiloAnsi, sizeof(kKiloAnsi) - 1));
|
||||
EZBENCH2("pty render", donothing, render(pty));
|
||||
}
|
|
@ -36,6 +36,7 @@ TEST_TOOL_BUILD_LIB_DIRECTDEPS = \
|
|||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_FMT \
|
||||
LIBC_STR \
|
||||
LIBC_STDIO \
|
||||
LIBC_LOG \
|
||||
LIBC_SYSV \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue