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

@ -0,0 +1,106 @@
/*-*- 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/assert.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "test/tool/build/lib/optest.h"
#include "tool/build/lib/alu.h"
#include "tool/build/lib/flags.h"
#define NATIVE_ALU2(MODE, INSTRUCTION) \
asm("pushf\n\t" \
"andl\t%3,(%%rsp)\n\t" \
"orl\t%4,(%%rsp)\n\t" \
"popf\n\t" INSTRUCTION "\t%" MODE "2,%" MODE "0\n\t" \
"pushf\n\t" \
"pop\t%q1" \
: "+r"(x), "=rm"(*f) \
: "r"(y), "i"(~FMASK), "r"(*f & FMASK) \
: "cc")
#define NATIVE_ALU2_ANYBITS(INSTRUCTION, MUTATING) \
switch (w) { \
case 0: \
NATIVE_ALU2("b", INSTRUCTION); \
if (MUTATING) x &= 0xff; \
return x; \
case 1: \
NATIVE_ALU2("w", INSTRUCTION); \
if (MUTATING) x &= 0xffff; \
return x; \
case 2: \
NATIVE_ALU2("k", INSTRUCTION); \
return x; \
case 3: \
NATIVE_ALU2("q", INSTRUCTION); \
return x; \
default: \
abort(); \
}
int64_t RunGolden(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
bool rw;
rw = !(h & ALU_TEST);
switch (h & 7) {
case ALU_OR:
NATIVE_ALU2_ANYBITS("or", rw);
case ALU_AND:
if (rw) {
NATIVE_ALU2_ANYBITS("and", rw);
} else {
NATIVE_ALU2_ANYBITS("test", rw);
}
case ALU_XOR:
NATIVE_ALU2_ANYBITS("xor", rw);
case ALU_SBB:
NATIVE_ALU2_ANYBITS("sbb", rw);
case ALU_CMP:
rw = false;
NATIVE_ALU2_ANYBITS("cmp", rw);
case ALU_SUB:
NATIVE_ALU2_ANYBITS("sub", rw);
case ALU_ADC:
NATIVE_ALU2_ANYBITS("adc", rw);
case ALU_ADD:
NATIVE_ALU2_ANYBITS("add", rw);
default:
abort();
}
}
const uint8_t kAluOps[] = {
ALU_ADD, ALU_OR, ALU_ADC, ALU_SBB, ALU_AND, ALU_SUB, ALU_XOR, ALU_CMP, ALU_AND | ALU_TEST,
};
const char *const kAluNames[] = {
[ALU_ADD] = "add", [ALU_OR] = "or", [ALU_ADC] = "adc",
[ALU_SBB] = "sbb", [ALU_AND] = "and", [ALU_SUB] = "sub",
[ALU_XOR] = "xor", [ALU_CMP] = "cmp", [ALU_AND | ALU_TEST] = "test",
};
int64_t RunOpTest(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
return Alu(w, h, x, y, f);
}
TEST(alu, test) {
RunOpTests(kAluOps, ARRAYLEN(kAluOps), kAluNames);
}

View file

@ -0,0 +1,112 @@
/*-*- 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/macros.h"
#include "libc/testlib/testlib.h"
#include "test/tool/build/lib/numbers.h"
#include "tool/build/lib/bitscan.h"
#include "tool/build/lib/flags.h"
#define OSZ 00000000040
#define REXW 00000004000
struct Machine m[1];
struct XedDecodedInst xedd[1];
void SetUp(void) {
m->xedd = xedd;
}
TEST(bsr64, test) {
bool zf;
uint64_t i, w, x, a, b;
m->xedd->op.rde = REXW;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsr(m, 0, x);
asm("bsrq\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsr32, test) {
bool zf;
uint32_t i, w, x, a, b;
m->xedd->op.rde = 0;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsr(m, 0, x);
asm("bsrl\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsr16, test) {
bool zf;
uint16_t i, w, x, a, b;
m->xedd->op.rde = OSZ;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsr(m, 0, x);
asm("bsrw\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsf64, test) {
bool zf;
uint64_t i, w, x, a, b;
m->xedd->op.rde = REXW;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsf(m, 0, x);
asm("bsfq\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsf32, test) {
bool zf;
uint32_t i, w, x, a, b;
m->xedd->op.rde = 0;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsf(m, 0, x);
asm("bsfl\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b);
}
}
TEST(bsf16, test) {
bool zf;
uint16_t i, w, x, a, b;
m->xedd->op.rde = OSZ;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
a = AluBsf(m, 0, x);
asm("bsfw\t%2,%0" : "=r"(b), "=@ccz"(zf) : "r"(x));
ASSERT_EQ(zf, GetFlag(m->flags, FLAGS_ZF));
if (!zf) ASSERT_EQ(a, b, "%#lx", x);
}
}

View file

@ -0,0 +1,225 @@
/*-*- 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/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "test/tool/build/lib/numbers.h"
#include "test/tool/build/lib/optest.h"
#include "tool/build/lib/alu.h"
#include "tool/build/lib/flags.h"
#include "tool/build/lib/machine.h"
#define NATIVE_ALU2(MODE, INSTRUCTION) \
asm("pushf\n\t" \
"andl\t%3,(%%rsp)\n\t" \
"orl\t%4,(%%rsp)\n\t" \
"popf\n\t" INSTRUCTION "\t%b2,%" MODE "0\n\t" \
"pushf\n\t" \
"pop\t%q1" \
: "+r"(x), "=r"(*f) \
: "c"(y), "i"(~FMASK), "r"(*f & FMASK) \
: "cc")
#define NATIVE_ALU2_ANYBITS(INSTRUCTION, MUTATING) \
switch (w) { \
case 0: \
NATIVE_ALU2("b", INSTRUCTION); \
if (MUTATING) x &= 0xff; \
return x; \
case 1: \
NATIVE_ALU2("w", INSTRUCTION); \
if (MUTATING) x &= 0xffff; \
return x; \
case 2: \
NATIVE_ALU2("k", INSTRUCTION); \
return x; \
case 3: \
NATIVE_ALU2("q", INSTRUCTION); \
return x; \
default: \
abort(); \
}
int64_t RunGolden(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
switch (h & 7) {
case BSU_ROR:
NATIVE_ALU2_ANYBITS("ror", true);
case BSU_SHL:
NATIVE_ALU2_ANYBITS("shl", true);
case BSU_SAL:
NATIVE_ALU2_ANYBITS("sal", true);
case BSU_RCR:
NATIVE_ALU2_ANYBITS("rcr", true);
case BSU_SAR:
NATIVE_ALU2_ANYBITS("sar", true);
case BSU_SHR:
NATIVE_ALU2_ANYBITS("shr", true);
case BSU_RCL:
NATIVE_ALU2_ANYBITS("rcl", true);
case BSU_ROL:
NATIVE_ALU2_ANYBITS("rol", true);
default:
abort();
}
}
const uint8_t kBsuOps[] = {
BSU_SHR, BSU_SHL, BSU_ROL, BSU_ROR, BSU_RCL,
BSU_RCR, BSU_SHL, BSU_SAL, BSU_SAR,
};
const char *const kBsuNames[] = {
[BSU_ROL] = "rol", [BSU_ROR] = "ror", [BSU_RCL] = "rcl", [BSU_RCR] = "rcr",
[BSU_SHL] = "shl", [BSU_SHR] = "shr", [BSU_SAL] = "sal", [BSU_SAR] = "sar",
};
int64_t RunOpTest(char w, int h, uint64_t x, uint64_t y, uint32_t *f) {
return Bsu(w, h, x, y, f);
}
void FixupUndefOpTestFlags(char w, int h, uint64_t x, uint64_t y,
uint32_t goldenflags, uint32_t *flags) {
y &= w == 3 ? 0x3F : 0x1F;
if (y != 0 && y != 1) {
*flags = SetFlag(*flags, FLAGS_OF, GetFlag(goldenflags, FLAGS_OF));
}
}
TEST(bsu, test) {
RunOpTests(kBsuOps, ARRAYLEN(kBsuOps), kBsuNames);
}
TEST(shrd32, smoke) {
int i, j, k;
uint8_t b, cf, of;
uint32_t x, y, z1, f, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
z1 = BsuDoubleShift(2, x, y, b, true, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shrd\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}
TEST(shld16, smoke) {
uint32_t f;
int i, j, k;
uint8_t b, cf, of;
uint16_t x, y, z1, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
if (b > 16) continue;
z1 = BsuDoubleShift(1, x, y, b, false, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shldw\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}
TEST(shld32, smoke) {
int i, j, k;
uint8_t b, cf, of;
uint32_t x, y, z1, f, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
if (b > 32) continue;
z1 = BsuDoubleShift(2, x, y, b, false, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shld\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}
TEST(shld64, smoke) {
uint32_t f;
int i, j, k;
uint8_t b, cf, of;
uint64_t x, y, z1, z2, unflag;
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
x = kNumbers[i];
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
y = kNumbers[j];
for (k = 0; k < ARRAYLEN(kNumbers); ++k) {
f = 0;
cf = 0;
of = 0;
b = kNumbers[k];
if (b > 64) continue;
z1 = BsuDoubleShift(3, x, y, b, false, &f);
z2 = x;
asm("xor\t%1,%1\n\t"
"shldq\t%5,%4,%0"
: "+rm"(z2), "=&r"(unflag), "=@ccc"(cf), "=@cco"(of)
: "r"(y), "c"(b)
: "cc");
ASSERT_EQ(z2, z1);
ASSERT_EQ(cf, GetFlag(f, FLAGS_CF));
if (b <= 1) ASSERT_EQ(of, GetFlag(f, FLAGS_OF));
}
}
}
}

View file

@ -0,0 +1,88 @@
/*-*- 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/dis.h"
char b1[64];
char b2[64];
struct Dis d[1];
struct DisBuilder b = {d, d->xedd, 0};
TEST(DisInst, testInt3) {
uint8_t op[] = {0xcc};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("int3 ", b1);
}
TEST(DisInst, testImmMem_needsSuffix) {
uint8_t op[] = {0x80, 0x3c, 0x07, 0x00};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("cmpb $0,(%rdi,%rax)", b1);
}
TEST(DisInst, testImmReg_doesntNeedSuffix) {
uint8_t op[] = {0xb8, 0x08, 0x70, 0x40, 0x00};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("mov $0x407008,%eax", b1);
}
TEST(DisInst, testPuttingOnTheRiz) {
static uint8_t ops[][15] = {
{0x8d, 0b00110100, 0b00100110},
{0x67, 0x8d, 0b00110100, 0b11100110},
{0x67, 0x8d, 0b10110100, 0b11100101, 0, 0, 0, 0},
{0x8d, 0b00110100, 0b11100101, 0x37, 0x13, 0x03, 0},
{0x8d, 0b10110100, 0b11100101, 0, 0, 0, 0},
};
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[0], sizeof(ops[0])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea (%rsi),%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[1], sizeof(ops[1])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea (%esi,%eiz,8),%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[2], sizeof(ops[2])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0(%ebp,%eiz,8),%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[3], sizeof(ops[3])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0x31337,%esi", b1);
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, ops[4], sizeof(ops[4])));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0(%rbp,%riz,8),%esi", b1);
}
TEST(DisInst, testSibIndexOnly) {
uint8_t op[] = {76, 141, 4, 141, 0, 0, 0, 0}; /* lea 0x0(,%rcx,4),%r8 */
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(d->xedd, op, sizeof(op)));
DisInst(b, b1, DisSpec(d->xedd, b2));
EXPECT_STREQ("lea 0(,%rcx,4),%r8", b1);
}

View file

@ -0,0 +1,606 @@
/*-*- 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/progn.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/log/check.h"
#include "libc/runtime/gc.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "tool/build/lib/divmul.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/flags.h"
#define CX 1
#define OSZ 00000000040
#define REXW 00000004000
#define RM(x) (0000000700 & ((x) << 006))
#define MOD(x) (0060000000 & ((x) << 026))
jmp_buf sigfpejmp;
struct Machine m[1];
struct sigaction oldsigfpe[1];
struct XedDecodedInst xedd[1];
void OnSigFpe(void) {
/* ProTip: gdb -ex 'handle SIGFPE nostop noprint pass' */
longjmp(sigfpejmp, 1);
}
void SetUp(void) {
m->xedd = xedd;
InitMachine(m);
CHECK_NE(-1, xsigaction(SIGFPE, OnSigFpe, SA_NODEFER, 0, oldsigfpe));
}
void TearDown(void) {
m->xedd = xedd;
CHECK_NE(-1, sigaction(SIGFPE, oldsigfpe, NULL));
}
TEST(imul8, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xBF};
int i, j;
int16_t ax;
bool cf, of;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write8(m->ax, A[i]);
Write8(m->cx, A[j]);
OpMulAxAlEbSigned(m);
asm volatile("imulb\t%3"
: "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "q"(A[j]), "0"(A[i])
: "cc");
EXPECT_EQ(ax, (int16_t)Read16(m->ax));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(imul16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF, 0x8001, 0x7FFE,
0xFFFF, 0xBeef, 0x00b5, 0x00b6, 0xb504, 0xb505};
int i, j;
bool cf, of;
uint16_t dx, ax;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write16(m->ax, A[i]);
Write16(m->cx, A[j]);
OpMulRdxRaxEvqpSigned(m);
asm("imulw\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((int32_t)((uint32_t)dx << 16 | ax),
(int32_t)((uint32_t)Read16(m->dx) << 16 | Read16(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(imul32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef,
0x000000b6, 0x0000b504, 0x0000b505, 0xb504f334};
int i, j;
bool cf, of;
uint32_t dx, ax;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write32(m->ax, A[i]);
Write32(m->cx, A[j]);
OpMulRdxRaxEvqpSigned(m);
asm("imull\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((int64_t)((uint64_t)dx << 32 | ax),
(int64_t)((uint64_t)Read32(m->dx) << 32 | Read32(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(imul64, test) {
static const uint64_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef,
0x000000b6, 0x0000b504, 0x0000b505, 0xb504f334};
int i, j;
bool cf, of;
uint64_t dx, ax;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write64(m->ax, A[i]);
Write64(m->cx, A[j]);
OpMulRdxRaxEvqpSigned(m);
asm("imulq\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((int128_t)((uint128_t)dx << 64 | ax),
(int128_t)((uint128_t)Read64(m->dx) << 64 | Read64(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul8, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xb6};
int i, j;
uint16_t ax;
bool cf, of;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write8(m->ax, A[i]);
Write8(m->cx, A[j]);
OpMulAxAlEbUnsigned(m);
asm volatile("mulb\t%3"
: "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "q"(A[j]), "0"(A[i])
: "cc");
EXPECT_EQ(ax, Read16(m->ax));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF,
0x8001, 0x7FFE, 0xFFFF, 0x00b6};
int i, j;
bool cf, of;
uint16_t dx, ax;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write16(m->ax, A[i]);
Write16(m->cx, A[j]);
OpMulRdxRaxEvqpUnsigned(m);
asm("mulw\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((uint32_t)((uint32_t)dx << 16 | ax),
(uint32_t)((uint32_t)Read16(m->dx) << 16 | Read16(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0x000000b5,
0x000000b6, 0x0000b504, 0x0000b505, 0xb504f334};
int i, j;
bool cf, of;
uint32_t dx, ax;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write32(m->ax, A[i]);
Write32(m->cx, A[j]);
OpMulRdxRaxEvqpUnsigned(m);
asm("mull\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((uint64_t)((uint64_t)dx << 32 | ax),
(uint64_t)((uint64_t)Read32(m->dx) << 32 | Read32(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(mul64, test) {
static const uint64_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0x000000b6,
0x0000b504, 0x0000b505, 0xb504f333, 0xb504f334};
int i, j;
bool cf, of;
uint64_t dx, ax;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
Write64(m->ax, A[i]);
Write64(m->cx, A[j]);
OpMulRdxRaxEvqpUnsigned(m);
asm("mulq\t%4"
: "=d"(dx), "=a"(ax), "=@ccc"(cf), "=@cco"(of)
: "r"(A[j]), "1"(A[i])
: "cc");
EXPECT_EQ((uint128_t)((uint128_t)dx << 64 | ax),
(uint128_t)((uint128_t)Read64(m->dx) << 64 | Read64(m->ax)));
EXPECT_EQ(cf, GetFlag(m->flags, FLAGS_CF));
EXPECT_EQ(of, GetFlag(m->flags, FLAGS_OF));
}
}
}
TEST(idiv8, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xBF};
uint16_t remquo;
bool gotthrow, gotsigfpe;
int8_t i, j, k, w, x, a, b;
int8_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
m->ax[1] = A[i];
m->ax[0] = A[j];
m->cx[0] = A[k];
gotthrow = false;
gotsigfpe = false;
if (!setjmp(m->onhalt)) {
OpDivAlAhAxEbSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
asm("idivb\t%1"
: "=a"(remquo)
: "q"(A[k]), "0"((int16_t)(A[i] << 8 | A[j]))
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
quotient = (int8_t)remquo;
remainder = (int8_t)(remquo >> 8);
EXPECT_EQ(quotient, (int8_t)m->ax[0]);
EXPECT_EQ(remainder, (int8_t)m->ax[1]);
}
}
}
}
}
TEST(idiv16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF,
0x8001, 0x7FFE, 0xFFFF, 0xBeef};
bool gotthrow, gotsigfpe;
int16_t i, j, k, w, x, a, b;
int16_t quotient, remainder;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 2);
memcpy(m->ax, &A[j], 2);
memcpy(m->cx, &A[k], 2);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("idivw\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (int16_t)Read16(m->ax));
EXPECT_EQ(remainder, (int16_t)Read16(m->dx));
}
}
}
}
}
TEST(idiv32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef};
bool gotthrow, gotsigfpe;
int32_t i, j, k, w, x, a, b;
int32_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 4);
memcpy(m->ax, &A[j], 4);
memcpy(m->cx, &A[k], 4);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("idivl\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (int32_t)Read32(m->ax));
EXPECT_EQ(remainder, (int32_t)Read32(m->dx));
}
}
}
}
}
TEST(idiv64, test) {
static const uint64_t A[] = {0x0000000000000000, 0x0000000000000001,
0x8000000000000000, 0x7FFFFFFFFFFFFFFF,
0x8000000000000001, 0x7FFFFFFFFFFFFFFE,
0xFFFFFFFFFFFFFFFF, 0x00DeadBeefCafe00};
bool gotthrow, gotsigfpe;
int64_t i, j, k, w, x, a, b;
int64_t quotient, remainder;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 8);
memcpy(m->ax, &A[j], 8);
memcpy(m->cx, &A[k], 8);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpSigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("idivq\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (int64_t)Read64(m->ax));
EXPECT_EQ(remainder, (int64_t)Read64(m->dx));
}
}
}
}
}
TEST(div, test) {
static const uint8_t A[] = {0x00, 0x01, 0x80, 0x7F, 0x81, 0x7E, 0xFF, 0xBF};
uint16_t remquo;
bool gotthrow, gotsigfpe;
uint8_t i, j, k, w, x, a, b;
uint8_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
m->ax[1] = A[i];
m->ax[0] = A[j];
m->cx[0] = A[k];
gotthrow = false;
gotsigfpe = false;
if (!setjmp(m->onhalt)) {
OpDivAlAhAxEbUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
asm("divb\t%1"
: "=a"(remquo)
: "q"(A[k]), "0"((uint16_t)(A[i] << 8 | A[j]))
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
quotient = (uint8_t)remquo;
remainder = (uint8_t)(remquo >> 8);
EXPECT_EQ(quotient, (uint8_t)m->ax[0]);
EXPECT_EQ(remainder, (uint8_t)m->ax[1]);
}
}
}
}
}
TEST(div16, test) {
static const uint16_t A[] = {0x0000, 0x0001, 0x8000, 0x7FFF,
0x8001, 0x7FFE, 0xFFFF, 0xBeef};
bool gotthrow, gotsigfpe;
uint16_t i, j, k, w, x, a, b;
uint16_t quotient, remainder;
m->xedd->op.rde = OSZ | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 2);
memcpy(m->ax, &A[j], 2);
memcpy(m->cx, &A[k], 2);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("divw\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (uint16_t)Read16(m->ax));
EXPECT_EQ(remainder, (uint16_t)Read16(m->dx));
}
}
}
}
}
TEST(div32, test) {
static const uint32_t A[] = {0x00000000, 0x00000001, 0x80000000, 0x7FFFFFFF,
0x80000001, 0x7FFFFFFE, 0xFFFFFFFF, 0xDeadBeef};
bool gotthrow, gotsigfpe;
uint32_t i, j, k, w, x, a, b;
uint32_t quotient, remainder;
m->xedd->op.rde = MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 4);
memcpy(m->ax, &A[j], 4);
memcpy(m->cx, &A[k], 4);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("divl\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (uint32_t)Read32(m->ax));
EXPECT_EQ(remainder, (uint32_t)Read32(m->dx));
}
}
}
}
}
TEST(div64, test) {
static const uint64_t A[] = {0x0000000000000000, 0x0000000000000001,
0x8000000000000000, 0x7FFFFFFFFFFFFFFF,
0x8000000000000001, 0x7FFFFFFFFFFFFFFE,
0xFFFFFFFFFFFFFFFF, 0x00DeadBeefCafe00};
bool gotthrow, gotsigfpe;
uint64_t i, j, k, w, x, a, b;
uint64_t quotient, remainder;
m->xedd->op.rde = REXW | MOD(3) | RM(CX);
for (i = 0; i < ARRAYLEN(A); ++i) {
for (j = 0; j < ARRAYLEN(A); ++j) {
for (k = 0; k < ARRAYLEN(A); ++k) {
memcpy(m->dx, &A[i], 8);
memcpy(m->ax, &A[j], 8);
memcpy(m->cx, &A[k], 8);
if (!setjmp(m->onhalt)) {
gotthrow = false;
OpDivRdxRaxEvqpUnsigned(m);
} else {
gotthrow = true;
}
if (!setjmp(sigfpejmp)) {
gotsigfpe = false;
asm("divq\t%2"
: "=d"(remainder), "=a"(quotient)
: "r"(A[k]), "0"(A[i]), "1"(A[j])
: "cc");
} else {
gotsigfpe = true;
}
EXPECT_EQ(gotsigfpe, gotthrow);
if (!gotsigfpe && !gotthrow) {
EXPECT_EQ(quotient, (uint64_t)Read64(m->ax));
EXPECT_EQ(remainder, (uint64_t)Read64(m->dx));
}
}
}
}
}
BENCH(imul, bench) {
volatile register int8_t x8, y8;
volatile register int16_t x16, y16;
volatile register int32_t x32, y32;
volatile register int64_t x64, y64;
EZBENCH2("imul8", PROGN(x8 = 7, y8 = 18), y8 *= x8);
EZBENCH2("imul16", PROGN(x16 = 123, y16 = 116), y16 *= x16);
EZBENCH2("imul32", PROGN(x32 = 0x238943, y32 = 0x238), y32 *= x32);
EZBENCH2("imul64", PROGN(x64 = 0x23894329838932, y64 = 0x238), y64 *= x64);
}
BENCH(idiv, bench) {
volatile register int8_t x8, y8;
volatile register int16_t x16, y16;
volatile register int32_t x32, y32;
volatile register int64_t x64, y64;
EZBENCH2("idiv8", PROGN(x8 = 7, y8 = 18), x8 /= y8);
EZBENCH2("idiv16", PROGN(x16 = 123, y16 = 116), x16 /= y16);
EZBENCH2("idiv32", PROGN(x32 = 0x238943298, y32 = 0x238), x32 /= y32);
EZBENCH2("idiv64", PROGN(x64 = 0x23894329838932, y64 = 0x238), x64 /= y64);
}
BENCH(mul, bench) {
volatile register uint8_t x8, y8;
volatile register uint16_t x16, y16;
volatile register uint32_t x32, y32;
volatile register uint64_t x64, y64;
EZBENCH2("mul8", PROGN(x8 = 7, y8 = 18), y8 *= x8);
EZBENCH2("mul16", PROGN(x16 = 123, y16 = 116), y16 *= x16);
EZBENCH2("mul32", PROGN(x32 = 0x238943, y32 = 0x238), y32 *= x32);
EZBENCH2("mul64", PROGN(x64 = 0x23894329838932, y64 = 0x238), y64 *= x64);
}
BENCH(div, bench) {
volatile register uint8_t x8, y8;
volatile register uint16_t x16, y16;
volatile register uint32_t x32, y32;
volatile register uint64_t x64, y64;
EZBENCH2("div8", PROGN(x8 = 7, y8 = 18), x8 /= y8);
EZBENCH2("div16", PROGN(x16 = 123, y16 = 116), x16 /= y16);
EZBENCH2("div32", PROGN(x32 = 0x238943298, y32 = 0x238), x32 /= y32);
EZBENCH2("div64", PROGN(x64 = 0x23894329838932, y64 = 0x238), x64 /= y64);
}

View file

@ -0,0 +1,298 @@
/*-*- 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/progn.h"
#include "libc/fmt/bing.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/fpu.h"
#include "tool/build/lib/machine.h"
#include "tool/build/lib/memory.h"
const uint8_t kPi80[] = {
0xd9, 0xe8, // fld1
0xb8, 0x0a, 0x00, 0x00, 0x00, // mov $0xa,%eax
0x31, 0xd2, // xor %edx,%edx
0xd9, 0xee, // fldz
0x48, 0x98, // cltq
0x48, 0x39, 0xc2, // cmp %rax,%rdx
0xd9, 0x05, 0x1a, 0x00, 0x00, 0x00, // flds 0x1a(%rip)
0x7d, 0x13, // jge 2b <pi80+0x2b>
0xde, 0xc1, // faddp
0x48, 0xff, 0xc2, // inc %rdx
0xd9, 0xfa, // fsqrt
0xd9, 0x05, 0x0f, 0x00, 0x00, 0x00, // flds 15(%rip)
0xd8, 0xc9, // fmul %st(1),%st
0xde, 0xca, // fmulp %st,%st(2)
0xeb, 0xe2, // jmp d <pi80+0xd>
0xdd, 0xd9, // fstp %st(1)
0xde, 0xf1, // fdivp
0xf4, // hlt
0x00, 0x00, 0x00, 0x40, // .float 2.0
0x00, 0x00, 0x00, 0x3f, // .float 0.5
};
const uint8_t kTenthprime[] = {
0x31, 0xd2, // xor %edx,%edx
0x45, 0x31, 0xc0, // xor %r8d,%r8d
0x31, 0xc9, // xor %ecx,%ecx
0xbe, 0x03, 0x00, 0x00, 0x00, // mov $0x3,%esi
0x41, 0xff, 0xc0, // inc %r8d
0x44, 0x89, 0xc0, // mov %r8d,%eax
0x83, 0xf9, 0x0a, // cmp $0xa,%ecx
0x74, 0x0b, // je 20
0x99, // cltd
0xf7, 0xfe, // idiv %esi
0x83, 0xfa, 0x01, // cmp $0x1,%edx
0x83, 0xd9, 0xff, // sbb $-1,%ecx
0xeb, 0xea, // jmp a
0xf4, // hlt
};
const uint8_t kTenthprime2[] = {
0xE8, 0x11, 0x00, 0x00, 0x00, //
0xF4, //
0x89, 0xF8, //
0xB9, 0x03, 0x00, 0x00, 0x00, //
0x99, //
0xF7, 0xF9, //
0x85, 0xD2, //
0x0F, 0x95, 0xC0, //
0xC3, //
0x55, //
0x48, 0x89, 0xE5, //
0x31, 0xF6, //
0x45, 0x31, 0xC0, //
0x44, 0x89, 0xC7, //
0xE8, 0xDF, 0xFF, 0xFF, 0xFF, //
0x0F, 0xB6, 0xC0, //
0x66, 0x83, 0xF8, 0x01, //
0x83, 0xDE, 0xFF, //
0x41, 0xFF, 0xC0, //
0x83, 0xFE, 0x0A, //
0x75, 0xE6, //
0x44, 0x89, 0xC0, //
0x5D, //
0xC3, //
};
int64_t base;
uint8_t *real;
size_t realsize;
struct Machine *m;
void SetUp(void) {
base = 0;
m = NewMachine();
realsize = 0x10000;
real = tmemalign(PAGESIZE, ROUNDUP(realsize, PAGESIZE));
RegisterMemory(m, base, real, realsize);
m->ip = base;
Write64(m->sp, m->ip + realsize);
}
void TearDown(void) {
ResetRam(m);
tfree(real);
free(m);
}
int ExecuteUntilHalt(struct Machine *m) {
int rc;
if (!(rc = setjmp(m->onhalt))) {
for (;;) {
LoadInstruction(m);
ExecuteInstruction(m);
}
} else {
return rc;
}
}
TEST(machine, test) {
memcpy(real, kTenthprime, sizeof(kTenthprime));
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
ASSERT_EQ(15, Read32(m->ax));
}
TEST(machine, testFpu) {
memcpy(real, kPi80, sizeof(kPi80));
ASSERT_EQ(kMachineHalt, ExecuteUntilHalt(m));
ASSERT_TRUE(fabs(3.14159 - FpuPop(m)) < 0.0001);
m->ip = base;
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));
ASSERT_EQ(15, Read32(m->ax));
memcpy(real, kTenthprime, sizeof(kTenthprime));
EZBENCH2("tenthprime", m->ip = base, 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)));
}
BENCH(machine, benchLoadExec2) {
uint8_t kMovCode[] = {0xbe, 0x03, 0x00, 0x00, 0x00};
memcpy(real, kMovCode, sizeof(kMovCode));
LoadInstruction(m);
EZBENCH2("mov", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec3) {
uint8_t kMovdCode[] = {0x66, 0x0f, 0x6e, 0xc0};
Write64(m->ax, 0);
memcpy(real, kMovdCode, sizeof(kMovdCode));
LoadInstruction(m);
EZBENCH2("movd", m->ip = base, 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));
LoadInstruction(m);
EZBENCH2("addps reg reg", m->ip = base, ExecuteInstruction(m));
memcpy(real, kAddpsMemregCode, sizeof(kAddpsMemregCode));
LoadInstruction(m);
EZBENCH2("addps mem reg", m->ip = base, 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));
LoadInstruction(m);
EZBENCH2("paddw", m->ip = base, ExecuteInstruction(m));
memcpy(real, kPaddwMemregCode, sizeof(kPaddwMemregCode));
LoadInstruction(m);
EZBENCH2("paddw mem", m->ip = base, 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));
LoadInstruction(m);
EZBENCH2("psubq", m->ip = base, ExecuteInstruction(m));
memcpy(real, kPsubqMemregCode, sizeof(kPsubqMemregCode));
LoadInstruction(m);
EZBENCH2("psubq mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddqMem) {
uint8_t kAddMemregCode[] = {0x48, 0x03, 0x08};
Write64(m->ax, 0);
memcpy(real, kAddMemregCode, sizeof(kAddMemregCode));
LoadInstruction(m);
EZBENCH2("addq mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddlMem) {
uint8_t kAddMemregCode[] = {0x03, 0x08};
Write64(m->ax, 0);
memcpy(real, kAddMemregCode, sizeof(kAddMemregCode));
LoadInstruction(m);
EZBENCH2("addl mem", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddq) {
uint8_t kAddqCode[] = {0x48, 0x01, 0xd8};
Write64(m->ax, 0);
memcpy(real, kAddqCode, sizeof(kAddqCode));
LoadInstruction(m);
EZBENCH2("addq", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchAddb) {
uint8_t kAddbCode[] = {0x00, 0xd8};
Write64(m->ax, 0);
memcpy(real, kAddbCode, sizeof(kAddbCode));
LoadInstruction(m);
EZBENCH2("addb", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchXorReg) {
memcpy(real, kTenthprime, sizeof(kTenthprime));
LoadInstruction(m);
EZBENCH2("xor", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchLoadExec8) {
uint8_t kFchsCode[] = {0xd9, 0xe0};
Write64(m->ax, 0);
OpFinit(m);
*FpuSt(m, 0) = M_PI;
FpuSetTag(m, 0, kFpuTagValid);
memcpy(real, kFchsCode, sizeof(kFchsCode));
LoadInstruction(m);
EZBENCH2("fchs", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchPushpop) {
uint8_t kPushpop[] = {0x50, 0x58};
Write64(m->ax, 0);
memcpy(real, kPushpop, sizeof(kPushpop));
EZBENCH2("pushpop", m->ip = base,
PROGN(LoadInstruction(m), ExecuteInstruction(m), LoadInstruction(m),
ExecuteInstruction(m)));
}
BENCH(machine, benchPause) {
uint8_t kPause[] = {0xf3, 0x90};
Write64(m->ax, 0);
memcpy(real, kPause, sizeof(kPause));
LoadInstruction(m);
EZBENCH2("pause", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchClc) {
uint8_t kClc[] = {0xf8};
Write64(m->ax, 0);
memcpy(real, kClc, sizeof(kClc));
LoadInstruction(m);
EZBENCH2("clc", m->ip = base, ExecuteInstruction(m));
}
BENCH(machine, benchNop) {
uint8_t kNop[] = {0x90};
Write64(m->ax, 0);
memcpy(real, kNop, sizeof(kNop));
LoadInstruction(m);
EZBENCH2("nop", m->ip = base, ExecuteInstruction(m));
}
TEST(machine, sizeIsReasonable) {
ASSERT_LE(sizeof(struct Machine), 65536);
}

View file

@ -0,0 +1,105 @@
/*-*- 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/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/testlib/testlib.h"
#include "third_party/xed/x86.h"
#include "tool/build/lib/endian.h"
#include "tool/build/lib/machine.h"
#include "tool/build/lib/modrm.h"
TEST(modrm, testAddressSizeOverride_isNotPresent_keepsWholeExpression) {
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {0x8d, 0x04, 0x03}; /* lea (%rbx,%rax,1),%eax */
m->xedd = xedd;
Write64(m->bx, 0x2);
Write64(m->ax, 0xffffffff);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x100000001, ComputeAddress(m));
}
TEST(modrm, testAddressSizeOverride_isPresent_modulesWholeExpression) {
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {0x67, 0x8d, 0x04, 0x03}; /* lea (%ebx,%eax,1),%eax */
m->xedd = xedd;
Write64(m->bx, 0x2);
Write64(m->ax, 0xffffffff);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x000000001, ComputeAddress(m));
}
TEST(modrm, testOverflow_doesntTriggerTooling) {
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {0x8d, 0x04, 0x03}; /* lea (%rbx,%rax,1),%eax */
m->xedd = xedd;
Write64(m->bx, 0x0000000000000001);
Write64(m->ax, 0x7fffffffffffffff);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x8000000000000000ull, (uint64_t)ComputeAddress(m));
}
TEST(modrm, testPuttingOnTheRiz) {
struct Machine *m = gc(NewMachine());
static uint8_t ops[][15] = {
{0x8d, 0b00110100, 0b00100110}, // lea (%rsi),%esi
{0x67, 0x8d, 0b00110100, 0b11100110}, // lea (%esi,%eiz,8),%esi
{103, 141, 180, 229, 55, 19, 3, 0}, // lea 0x31337(%ebp,%eiz,8),%esi
{141, 52, 229, 55, 19, 3, 0}, // lea 0x31337,%esi
};
m->xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
Write64(m->si, 0x100000001);
Write64(m->bp, 0x200000002);
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[0], sizeof(ops[0])));
EXPECT_EQ(0x100000001, ComputeAddress(m));
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[1], sizeof(ops[1])));
EXPECT_EQ(0x000000001, ComputeAddress(m));
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[2], sizeof(ops[2])));
EXPECT_EQ(0x31339, ComputeAddress(m));
xed_decoded_inst_zero_set_mode(m->xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(m->xedd, ops[3], sizeof(ops[3])));
EXPECT_EQ(0x31337, ComputeAddress(m));
}
TEST(modrm, testSibIndexOnly) {
// mod = 0b00 (0)
// reg = 0b000 (0)
// rm = 0b100 (4)
// scale = 0b10 (2)
// index = 0b001 (1)
// base = 0b101 (5)
struct Machine *m = gc(NewMachine());
struct XedDecodedInst *xedd = gc(calloc(1, sizeof(struct XedDecodedInst)));
uint8_t op[] = {76, 141, 4, 141, 0, 0, 0, 0}; /* lea 0x0(,%rcx,4),%r8 */
m->xedd = xedd;
Write64(m->bp, 0x123);
Write64(m->cx, 0x123);
xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64);
ASSERT_EQ(0, xed_instruction_length_decode(xedd, op, sizeof(op)));
EXPECT_EQ(0x123 * 4, (uint64_t)ComputeAddress(m));
}

View file

@ -0,0 +1,57 @@
/*-*- 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 "test/tool/build/lib/numbers.h"
const uint64_t kNumbers[102] = {
0x0000000000000000, 0x0000000080000000, 0x0000000000008000,
0x0000000000000001, 0x0000000000000002, 0x0000000000000003,
0x0000000000000004, 0x0000000000000005, 0x0000000000000006,
0x0000000000000007, 0x0000000000000008, 0x0000000000000009,
0x000000000000000a, 0x000000000000000b, 0x000000000000000c,
0x000000000000000d, 0x000000000000000e, 0x000000000000000f,
0x0000000000000010, 0x0000000000000011, 0x0000000000000012,
0x0000000000000013, 0x0000000000000014, 0x0000000000000015,
0x0000000000000016, 0x0000000000000017, 0x0000000000000018,
0x0000000000000019, 0x000000000000001a, 0x000000000000001b,
0x000000000000001c, 0x000000000000001d, 0x000000000000001e,
0x000000000000001f, 0x0000000000000020, 0x0000000000000040,
0x8000000000000000, 0x0000000000000080, 0x0000000000000002,
0x0000000000000001, 0x0000000000000004, 0x00000000C0000000,
0xC000000000000000, 0x000000000000C000, 0x00000000000000C0,
0x0000000000000003, 0x000000000000E000, 0x00000000E0000000,
0xE000000000000000, 0x00000000000000E0, 0x000000000000001F,
0x00000000000000FC, 0x000000000000003F, 0x000000000000007F,
0x00000000000000FB, 0x00000000000000FD, 0x00000000000000FE,
0x00000000000000FF, 0x000000000000FF1F, 0x0000000000001FFF,
0x000000000000FFFC, 0x0000000000003FFF, 0x000000000000FF3F,
0x000000000000FFFD, 0x000000000000FFFE, 0x000000000000FFFB,
0x000000000000FF7F, 0x0000000000007FFF, 0x000000000000FFFF,
0x00000000FFFF1FFF, 0x00000000FFFFFF1F, 0x000000001FFFFFFF,
0x00000000FFFFFF3F, 0x00000000FFFF3FFF, 0x00000000FFFFFFFC,
0x000000003FFFFFFF, 0x00000000FFFFFF7F, 0x00000000FFFFFFFD,
0x00000000FFFFFFFE, 0x00000000FFFFFFFB, 0x000000007FFFFFFF,
0x00000000FFFF7FFF, 0x00000000FFFFFFFF, 0xFFFFFFFF1FFFFFFF,
0x1FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFF1FFF, 0xFFFFFFFFFFFFFF1F,
0xFFFFFFFFFFFFFFFC, 0xFFFFFFFFFFFF3FFF, 0xFFFFFFFF3FFFFFFF,
0xFFFFFFFFFFFFFF3F, 0x3FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFD,
0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFB, 0xFFFFFFFF7FFFFFFF,
0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFF7F, 0xFFFFFFFFFFFF7FFF,
0xFFFFFFFFFFFFFFFF, 0x00DeadBeefCafe00, 0x0000031337000000,
};

View file

@ -0,0 +1,10 @@
#ifndef COSMOPOLITAN_TEST_TOOL_BUILD_LIB_NUMBERS_H_
#define COSMOPOLITAN_TEST_TOOL_BUILD_LIB_NUMBERS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern const uint64_t kNumbers[102];
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TEST_TOOL_BUILD_LIB_NUMBERS_H_ */

View file

@ -0,0 +1,94 @@
/*-*- 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/weaken.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "test/tool/build/lib/numbers.h"
#include "test/tool/build/lib/optest.h"
#include "tool/build/lib/flags.h"
const char kOpSuffix[] = {'b', 'w', 'l', 'q'};
void(RunOpTests)(const uint8_t *ops, size_t n, const char *const *opnames,
const char *file, int line, const char *func) {
uint64_t x, y;
uint64_t xn, xp;
uint32_t f0, f1, f2;
long failed, succeeded;
int w, h, i, j, c, z, s, o, p;
failed = 0;
succeeded = 0;
for (w = 0; w < 4; ++w) {
for (h = 0; h < n; ++h) {
for (z = 0; z < 2; ++z) {
for (o = 0; o < 2; ++o) {
for (s = 0; s < 2; ++s) {
for (i = 0; i < ARRAYLEN(kNumbers); ++i) {
for (j = 0; j < ARRAYLEN(kNumbers); ++j) {
for (c = 0; c < 2; ++c) {
x = kNumbers[i];
y = kNumbers[j];
f2 = f1 = f0 = c << FLAGS_CF | z << FLAGS_ZF | s << FLAGS_SF |
o << FLAGS_OF;
xn = RunGolden(w, ops[h], x, y, &f1);
xp = RunOpTest(w, ops[h], x, y, &f2);
if (weaken(FixupUndefOpTestFlags)) {
FixupUndefOpTestFlags(w, ops[h], x, y, f1, &f2);
}
if (xn == xp && (f1 & FMASK) == (f2 & FMASK)) {
succeeded++;
} else if (failed++ < 10) {
fprintf(stderr,
"%s:%d:%s: %s%c failed\n\t"
"𝑥 %016x\n\t"
"𝑦 %016x %c%c%c%c 0NPlODITSZKA1PVC\n\t"
"𝑥ₙ %016x %c%c%c%c %016b\n\t"
"𝑥ₚ %016x %c%c%c%c %016b\n",
file, line, func, opnames[ops[h]], kOpSuffix[w], x,
y, ".C"[c], ".Z"[z], ".S"[s], ".O"[o], xn,
".C"[!!(f1 & (1 << FLAGS_CF))],
".Z"[!!(f1 & (1 << FLAGS_ZF))],
".S"[!!(f1 & (1 << FLAGS_SF))],
".O"[!!(f1 & (1 << FLAGS_OF))], f1, xp,
".C"[!!(f2 & (1 << FLAGS_CF))],
".Z"[!!(f2 & (1 << FLAGS_ZF))],
".S"[!!(f2 & (1 << FLAGS_SF))],
".O"[!!(f2 & (1 << FLAGS_OF))], f2);
}
}
}
}
}
}
}
}
}
if (failed) {
fprintf(stderr,
"\n"
"passing: %d%%\n"
"succeeded: %,ld\n"
"failed: %,ld\n"
"\n",
(int)((1 - (double)failed / succeeded) * 100), succeeded, failed);
exit(1);
}
}

View file

@ -0,0 +1,21 @@
#ifndef COSMOPOLITAN_TEST_TOOL_BUILD_LIB_OPTEST_H_
#define COSMOPOLITAN_TEST_TOOL_BUILD_LIB_OPTEST_H_
#include "tool/build/lib/flags.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define FMASK (1 << FLAGS_CF | 1 << FLAGS_ZF | 1 << FLAGS_SF | 1 << FLAGS_OF)
void RunOpTests(const uint8_t *, size_t, const char *const *, const char *, int,
const char *);
int64_t RunGolden(char, int, uint64_t, uint64_t, uint32_t *);
int64_t RunOpTest(char, int, uint64_t, uint64_t, uint32_t *);
void FixupUndefOpTestFlags(char, int, uint64_t, uint64_t, uint32_t, uint32_t *);
#define RunOpTests(ops, n, names) \
RunOpTests(ops, n, names, __FILE__, __LINE__, __func__)
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TEST_TOOL_BUILD_LIB_OPTEST_H_ */

View file

@ -0,0 +1,150 @@
/*-*- 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/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, NewPage));
ASSERT_EQ(0x700006000, FindPml4t(cr3, 0x700000000, 0x10000, NewPage));
}
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);
}

View file

@ -3,14 +3,21 @@
PKGS += TEST_TOOL_BUILD_LIB
TEST_TOOL_BUILD_LIB_SRCS := $(wildcard test/tool/build/lib/*.c)
TEST_TOOL_BUILD_LIB = $(TOOL_BUILD_LIB_A_DEPS) $(TOOL_BUILD_LIB_A)
TEST_TOOL_BUILD_LIB_A = o/$(MODE)/test/tool/build/lib/buildlib.a
TEST_TOOL_BUILD_LIB_FILES := $(wildcard test/tool/build/lib/*)
TEST_TOOL_BUILD_LIB_SRCS = $(filter %.c,$(TEST_TOOL_BUILD_LIB_FILES))
TEST_TOOL_BUILD_LIB_SRCS_TEST = $(filter %_test.c,$(TEST_TOOL_BUILD_LIB_SRCS))
TEST_TOOL_BUILD_LIB_HDRS = $(filter %.h,$(TEST_TOOL_BUILD_LIB_FILES))
TEST_TOOL_BUILD_LIB_COMS = $(TEST_TOOL_BUILD_LIB_OBJS:%.o=%.com)
TEST_TOOL_BUILD_LIB_OBJS = \
$(TEST_TOOL_BUILD_LIB_SRCS:%=o/$(MODE)/%.zip.o) \
$(TEST_TOOL_BUILD_LIB_SRCS:%.c=o/$(MODE)/%.o)
TEST_TOOL_BUILD_LIB_COMS = \
$(TEST_TOOL_BUILD_LIB_SRCS:%.c=o/$(MODE)/%.com)
TEST_TOOL_BUILD_LIB_BINS = \
$(TEST_TOOL_BUILD_LIB_COMS) \
$(TEST_TOOL_BUILD_LIB_COMS:%=%.dbg)
@ -19,34 +26,49 @@ TEST_TOOL_BUILD_LIB_TESTS = \
$(TEST_TOOL_BUILD_LIB_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
TEST_TOOL_BUILD_LIB_CHECKS = \
$(TEST_TOOL_BUILD_LIB_HDRS:%=o/$(MODE)/%.ok) \
$(TEST_TOOL_BUILD_LIB_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
TEST_TOOL_BUILD_LIB_DIRECTDEPS = \
LIBC_X \
LIBC_CALLS \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_FMT \
LIBC_STDIO \
LIBC_LOG \
LIBC_SYSV \
LIBC_STUBS \
LIBC_UNICODE \
LIBC_TESTLIB \
TOOL_BUILD_LIB
TOOL_BUILD_LIB \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_XED
TEST_TOOL_BUILD_LIB_DEPS := \
$(call uniq,$(foreach x,$(TEST_TOOL_BUILD_LIB_DIRECTDEPS),$($(x))))
o/$(MODE)/test/tool/build/lib/buildlib.pkg: \
$(TEST_TOOL_BUILD_LIB_A): \
test/tool/build/lib/ \
$(TEST_TOOL_BUILD_LIB_A).pkg \
$(TEST_TOOL_BUILD_LIB_OBJS)
$(TEST_TOOL_BUILD_LIB_A).pkg: \
$(TEST_TOOL_BUILD_LIB_OBJS) \
$(foreach x,$(TEST_TOOL_BUILD_LIB_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/test/tool/build/lib/%.com.dbg: \
$(TEST_TOOL_BUILD_LIB_DEPS) \
$(TEST_TOOL_BUILD_LIB_A) \
o/$(MODE)/test/tool/build/lib/%.o \
o/$(MODE)/test/tool/build/lib/buildlib.pkg \
$(TEST_TOOL_BUILD_LIB_A).pkg \
$(LIBC_TESTMAIN) \
$(CRT) \
$(APE)
@$(APELINK)
.PHONY: o/$(MODE)/test/tool/build/lib
.PHONY: o/$(MODE)/test/tool/build/lib
o/$(MODE)/test/tool/build/lib: \
$(TEST_TOOL_BUILD_LIB_BINS) \
$(TEST_TOOL_BUILD_LIB_CHECKS)

View file

@ -0,0 +1,39 @@
/*-*- 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/x87.h"
TEST(x87, fprem) {
ASSERT_LDBL_EQ(1, fprem(1, -1.5, NULL));
ASSERT_LDBL_EQ(1.1766221079117338e-14L,
fprem(12300000000000000.L, .0000000000000123L, NULL));
}
TEST(x87, fprem1) {
ASSERT_LDBL_EQ(-.5, fprem1(1, -1.5, NULL));
ASSERT_LDBL_EQ(-5.337789208826618e-16,
fprem1(12300000000000000.L, .0000000000000123L, NULL));
}
TEST(x87, fpremFlags) {
uint32_t sw = 0xffff;
ASSERT_LDBL_EQ(1, fprem(1, -1.5, &sw));
ASSERT_EQ(0b1011100011111111, sw);
}

View file

@ -0,0 +1,28 @@
/*-*- 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/testlib/testlib.h"
#include "tool/build/lib/xlaterrno.h"
TEST(xlaterrno, test) {
EXPECT_EQ(95, XlatErrno(EOPNOTSUPP));
EXPECT_EQ(90, XlatErrno(EMSGSIZE));
EXPECT_EQ(133, XlatErrno(EHWPOISON));
}