mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 19:28:29 +00:00
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:
parent
467504308a
commit
f4f4caab0e
1052 changed files with 65667 additions and 7825 deletions
|
@ -20,7 +20,7 @@
|
|||
#include "libc/math.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
#define EPSILON 0.0000000000001L
|
||||
#define EPSILON 0.00000001L
|
||||
|
||||
testonly bool testlib_almostequallongdouble(long double x, long double y) {
|
||||
/* TODO(jart): This algorithm has to be binary. */
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_BENCH_H_
|
||||
#define COSMOPOLITAN_LIBC_BENCH_H_
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/nexgen32e/bench.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
@ -9,19 +8,19 @@ COSMOPOLITAN_C_START_
|
|||
* @fileoverview Microbenchmarking tools.
|
||||
*/
|
||||
|
||||
#define BENCHLOOP(START, STOP, N, INIT, EXPR) \
|
||||
({ \
|
||||
int Iter, Count; \
|
||||
long double Average, Sample, Time1, Time2; \
|
||||
for (Average = 1.0, Iter = 1, Count = (N); Iter < Count; ++Iter) { \
|
||||
INIT; \
|
||||
Time1 = START(); \
|
||||
EXPR; \
|
||||
Time2 = STOP(); \
|
||||
Sample = Time2 - Time1; \
|
||||
Average += (Sample - Average) / Iter; \
|
||||
} \
|
||||
Average; \
|
||||
#define BENCHLOOP(START, STOP, N, INIT, EXPR) \
|
||||
({ \
|
||||
unsigned long Iter, Count; \
|
||||
double Average, Sample, Time1, Time2; \
|
||||
for (Average = 1, Iter = 1, Count = (N); Iter < Count; ++Iter) { \
|
||||
INIT; \
|
||||
Time1 = START(); \
|
||||
EXPR; \
|
||||
Time2 = STOP(); \
|
||||
Sample = Time2 - Time1; \
|
||||
Average += 1. / Iter * (Sample - Average); \
|
||||
} \
|
||||
Average; \
|
||||
})
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
31
libc/testlib/clearxmmregisters.c
Normal file
31
libc/testlib/clearxmmregisters.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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"
|
||||
|
||||
void testlib_clearxmmregisters(void) {
|
||||
asm("pxor\t%xmm0,%xmm0\n\t"
|
||||
"pxor\t%xmm1,%xmm1\n\t"
|
||||
"pxor\t%xmm2,%xmm2\n\t"
|
||||
"pxor\t%xmm3,%xmm3\n\t"
|
||||
"pxor\t%xmm4,%xmm4\n\t"
|
||||
"pxor\t%xmm5,%xmm5\n\t"
|
||||
"pxor\t%xmm6,%xmm6\n\t"
|
||||
"pxor\t%xmm7,%xmm7");
|
||||
}
|
|
@ -24,11 +24,12 @@
|
|||
|
||||
STATIC_YOINK("ntoa");
|
||||
STATIC_YOINK("stoa");
|
||||
STATIC_YOINK("strnwidth");
|
||||
|
||||
void __testlib_ezbenchreport(const char *form, uint64_t c1, uint64_t c2) {
|
||||
uint64_t ns1, ns2;
|
||||
ns1 = lrintl(converttickstonanos(c1));
|
||||
ns2 = lrintl(converttickstonanos(c2));
|
||||
ns1 = rintl(converttickstonanos(c1));
|
||||
ns2 = rintl(converttickstonanos(c2));
|
||||
(fprintf)(stderr,
|
||||
VEIL("r", "%-30s l: %,10lu𝑐 %,10lu𝑛𝑠 m: %,10lu𝑐 %,10lu𝑛𝑠\n"),
|
||||
form, c1, ns1, c2, ns2);
|
||||
|
|
|
@ -40,9 +40,9 @@ testonly void testlib_runfixtures(testfn_t *test_start, testfn_t *test_end,
|
|||
for (i = 0; i < count && !g_testlib_failed; ++i) {
|
||||
snprintf(g_fixturename, sizeof(g_fixturename), "%s_%s",
|
||||
fixture_start[i].group, fixture_start[i].name);
|
||||
__piro(PROT_READ | PROT_WRITE);
|
||||
_piro(PROT_READ | PROT_WRITE);
|
||||
fixture_start[i].fn();
|
||||
__piro(PROT_READ);
|
||||
_piro(PROT_READ);
|
||||
testlib_runtestcases(test_start, test_end, NULL);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ testonly void testlib_showerror(const char *file, int line, const char *func,
|
|||
"\t%s%s\n"
|
||||
"\t%s%s\n",
|
||||
RED2, "error", UNBOLD, BLUE1, file, line, RESET, method, "in", func,
|
||||
g_fixturename, code, "want", v1, symbol, " got", v2, SUBTLE,
|
||||
g_fixturename, code, "need", v1, symbol, " got", v2, SUBTLE,
|
||||
strerror(errno), program_invocation_name, RESET);
|
||||
free_s(&v1);
|
||||
free_s(&v2);
|
||||
|
|
|
@ -60,7 +60,7 @@ testonly void testlib_showerror_(int line, const char *wantcode,
|
|||
", %s)\n"
|
||||
"\t\t%s %s %s\n"
|
||||
"\t\t%s %s\n",
|
||||
gotcode, "want", FREED_want, testlib_showerror_symbol, " got",
|
||||
gotcode, "need", FREED_want, testlib_showerror_symbol, " got",
|
||||
FREED_got);
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_TESTLIB_H_
|
||||
#define COSMOPOLITAN_LIBC_TESTLIB_H_
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/testlib/ugly.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
@ -15,7 +15,7 @@ COSMOPOLITAN_C_START_
|
|||
* Test cases are guaranteed by the linker to be run in order, sorted by
|
||||
* the (SUITE, NAME) tuple passed here.
|
||||
*/
|
||||
#define TEST(SUITE, NAME) __TEST_PROTOTYPE(SUITE, NAME, __TEST_ARRAY)
|
||||
#define TEST(SUITE, NAME) __TEST_PROTOTYPE(SUITE, NAME, __TEST_ARRAY, )
|
||||
|
||||
/**
|
||||
* Declares function that globally modifies program state.
|
||||
|
@ -48,7 +48,7 @@ COSMOPOLITAN_C_START_
|
|||
*/
|
||||
#define BENCH(SUITE, NAME) \
|
||||
STATIC_YOINK("__bench_start"); \
|
||||
__TEST_PROTOTYPE(SUITE, NAME, __BENCH_ARRAY)
|
||||
__TEST_PROTOTYPE(SUITE, NAME, __BENCH_ARRAY, optimizespeed)
|
||||
|
||||
#define ASSERT_GE(C, X) _TEST2("ASSERT_GE", C, >=, (X), #C, " ≥ ", #X, 1)
|
||||
#define ASSERT_GT(C, X) _TEST2("ASSERT_GT", C, >, (X), #C, " > ", #X, 1)
|
||||
|
@ -82,9 +82,11 @@ COSMOPOLITAN_C_START_
|
|||
#define ASSERT_EQ(WANT, GOT, ...) \
|
||||
__TEST_EQ(assert, __FILE__, __LINE__, __FUNCTION__, #WANT, #GOT, WANT, GOT, \
|
||||
__VA_ARGS__)
|
||||
|
||||
#define EXPECT_EQ(WANT, GOT, ...) \
|
||||
__TEST_EQ(expect, __FILE__, __LINE__, __FUNCTION__, #WANT, #GOT, WANT, GOT, \
|
||||
__VA_ARGS__)
|
||||
|
||||
#define __TEST_EQ(KIND, FILE, LINE, FUNC, WANTCODE, GOTCODE, WANT, GOT, ...) \
|
||||
({ \
|
||||
autotype(GOT) Got = _I(GOT); \
|
||||
|
@ -272,6 +274,7 @@ void *tunbing(const char16_t *)
|
|||
void *tunbinga(size_t, const char16_t *)
|
||||
paramsnonnull() returnsnonnull returnspointerwithnoaliases nodiscard
|
||||
attributeallocalign((1));
|
||||
void testlib_clearxmmregisters(void);
|
||||
|
||||
#define tgc(TMEM) \
|
||||
({ \
|
||||
|
@ -533,8 +536,8 @@ forceinline void assertStartsWith(FILIFU_ARGS size_t cw, const char *prefix,
|
|||
if (testlib_startswith(cw, s, prefix)) return;
|
||||
if (g_testlib_shoulddebugbreak) DebugBreak();
|
||||
testlib_showerror(file, line, func, "assertStartsWith", "≠", gotcode,
|
||||
testlib_formatstr(1, s, -1),
|
||||
testlib_formatstr(1, prefix, -1));
|
||||
testlib_formatstr(1, prefix, -1),
|
||||
testlib_formatstr(1, s, -1));
|
||||
testlib_onfail2(isfatal);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ LIBC_TESTLIB_A_SRCS_C = \
|
|||
libc/testlib/almostequallongdouble.c \
|
||||
libc/testlib/hexequals.c \
|
||||
libc/testlib/binequals.c \
|
||||
libc/testlib/clearxmmregisters.c \
|
||||
libc/testlib/formatbool.c \
|
||||
libc/testlib/formatrange.c \
|
||||
libc/testlib/globals.c \
|
||||
|
|
|
@ -74,6 +74,7 @@ testonly int main(int argc, char *argv[]) {
|
|||
showcrashreports();
|
||||
g_testlib_shoulddebugbreak = IsDebuggerPresent(false);
|
||||
getpid$sysv(); /* make strace easier to read */
|
||||
testlib_clearxmmregisters();
|
||||
testlib_runalltests();
|
||||
if (!g_testlib_failed && runbenchmarks_ && weaken(testlib_runallbenchmarks)) {
|
||||
weaken(testlib_runallbenchmarks)();
|
||||
|
|
|
@ -94,7 +94,7 @@ static void testmem_fini(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static void testmem_init(void) {
|
||||
static textstartup void testmem_init(void) {
|
||||
atexit(testmem_fini);
|
||||
g_testmem.p = g_testmem_scratch[0];
|
||||
g_testmem.n = ARRAYLEN(g_testmem_scratch[0]);
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/nt/process.h"
|
||||
|
@ -70,6 +70,7 @@ testonly void testlib_runtestcases(testfn_t *start, testfn_t *end,
|
|||
SetLastError(0);
|
||||
getpid$sysv();
|
||||
if (warmup) warmup();
|
||||
testlib_clearxmmregisters();
|
||||
(*fn)();
|
||||
getpid$sysv();
|
||||
if (weaken(TearDown)) weaken(TearDown)();
|
||||
|
|
|
@ -14,10 +14,10 @@
|
|||
#define __BENCH_ARRAY(S) \
|
||||
_Section(".piro.relo.sort.bench.2." #S ",\"aw\",@init_array #")
|
||||
|
||||
#define __TEST_PROTOTYPE(S, N, A) \
|
||||
#define __TEST_PROTOTYPE(S, N, A, K) \
|
||||
testonly void S##_##N(void); \
|
||||
alignas(8) const void *const S##_##N##_ptr[] A(S) = {S##_##N}; \
|
||||
testonly void S##_##N(void)
|
||||
testonly K void S##_##N(void)
|
||||
|
||||
#define __TEST_SECTION(NAME, CONTENT) \
|
||||
".section " NAME "\n" CONTENT "\n\t.previous\n"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue