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

@ -1,7 +1,7 @@
#include "third_party/dlmalloc/dlmalloc.h"
/* Check properties of any chunk, whether free, inuse, mmapped etc */
static void do_check_any_chunk(mstate m, mchunkptr p) {
forceinline void do_check_any_chunk(mstate m, mchunkptr p) {
assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD));
assert(ok_address(m, p));
}

View file

@ -1,3 +1,4 @@
#include "libc/bits/initializer.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/sysinfo.h"
@ -16,6 +17,8 @@
#include "libc/sysv/errfuns.h"
#include "third_party/dlmalloc/dlmalloc.h"
STATIC_YOINK("_init_dlmalloc");
#define OOM_WARNING "warning: running out of physical memory\n"
#define is_global(M) ((M) == &_gm_)
@ -32,7 +35,7 @@ struct malloc_state _gm_;
* Note that contiguous allocations are what Doug Lea recommends.
*/
static void *dlmalloc_requires_more_vespene_gas(size_t size) {
if (!IsTrustworthy()) {
if (0 && !IsTrustworthy()) {
size_t need = mallinfo().arena + size;
if (need > 8 * 1024 * 1024) {
struct sysinfo info;
@ -249,7 +252,7 @@ static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb, int flags) {
/**
* Gets memory from system.
*/
static void *sys_alloc(mstate m, size_t nb) {
void *sys_alloc(mstate m, size_t nb) {
char *tbase = CMFAIL;
size_t tsize = 0;
flag_t mmap_flag = 0;
@ -379,8 +382,9 @@ static size_t dlmalloc_release_unused_segments(mstate m) {
}
}
}
if (NO_SEGMENT_TRAVERSAL) /* scan only first segment */
if (NO_SEGMENT_TRAVERSAL) { /* scan only first segment */
break;
}
pred = sp;
sp = next;
}
@ -445,56 +449,6 @@ static void post_fork_child(void) {
}
#endif /* LOCK_AT_FORK */
static noinline void dlmalloc_init(void) {
#ifdef NEED_GLOBAL_LOCK_INIT
if (malloc_global_mutex_status <= 0) init_malloc_global_mutex();
#endif
ACQUIRE_MALLOC_GLOBAL_LOCK();
if (mparams.magic == 0) {
size_t magic;
size_t psize = PAGESIZE;
size_t gsize = max(g_ntsysteminfo.dwAllocationGranularity, 64 * 1024);
/* Sanity-check configuration:
size_t must be unsigned and as wide as pointer type.
ints must be at least 4 bytes.
alignment must be at least 8.
Alignment, min chunk size, and page size must all be powers of 2.
*/
if ((sizeof(size_t) != sizeof(char *)) || (SIZE_MAX < MIN_CHUNK_SIZE) ||
(sizeof(int) < 4) || (MALLOC_ALIGNMENT < (size_t)8U) ||
((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT - SIZE_T_ONE)) != 0) ||
((MCHUNK_SIZE & (MCHUNK_SIZE - SIZE_T_ONE)) != 0) ||
((gsize & (gsize - SIZE_T_ONE)) != 0) ||
((psize & (psize - SIZE_T_ONE)) != 0))
MALLOC_ABORT;
mparams.granularity = gsize;
mparams.page_size = psize;
mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
mparams.default_mflags =
USE_LOCK_BIT | USE_MMAP_BIT | USE_NONCONTIGUOUS_BIT;
/* Set up lock for main malloc area */
gm->mflags = mparams.default_mflags;
(void)INITIAL_LOCK(&gm->mutex);
#if LOCK_AT_FORK
pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child);
#endif
magic = kStartTsc;
magic |= (size_t)8U; /* ensure nonzero */
magic &= ~(size_t)7U; /* improve chances of fault for bad values */
/* Until memory modes commonly available, use volatile-write */
(*(volatile size_t *)(&(mparams.magic))) = magic;
}
RELEASE_MALLOC_GLOBAL_LOCK();
}
INITIALIZER(800, _init_dlmalloc, { dlmalloc_init(); })
/* ───────────────────────────── statistics ────────────────────────────── */
/* Consolidate and bin a chunk. Differs from exported versions
@ -1029,3 +983,46 @@ void *dlrealloc(void *oldmem, size_t bytes) {
}
return mem;
}
textstartup void dlmalloc_init(void) {
#ifdef NEED_GLOBAL_LOCK_INIT
if (malloc_global_mutex_status <= 0) init_malloc_global_mutex();
#endif
ACQUIRE_MALLOC_GLOBAL_LOCK();
if (mparams.magic == 0) {
size_t magic;
size_t psize = PAGESIZE;
size_t gsize = MAX(g_ntsysteminfo.dwAllocationGranularity, 64 * 1024);
/* Sanity-check configuration:
size_t must be unsigned and as wide as pointer type.
ints must be at least 4 bytes.
alignment must be at least 8.
Alignment, min chunk size, and page size must all be powers of 2.
*/
if ((sizeof(size_t) != sizeof(char *)) || (SIZE_MAX < MIN_CHUNK_SIZE) ||
(sizeof(int) < 4) || (MALLOC_ALIGNMENT < (size_t)8U) ||
((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT - SIZE_T_ONE)) != 0) ||
((MCHUNK_SIZE & (MCHUNK_SIZE - SIZE_T_ONE)) != 0) ||
((gsize & (gsize - SIZE_T_ONE)) != 0) ||
((psize & (psize - SIZE_T_ONE)) != 0))
MALLOC_ABORT;
mparams.granularity = gsize;
mparams.page_size = psize;
mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD;
mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD;
mparams.default_mflags =
USE_LOCK_BIT | USE_MMAP_BIT | USE_NONCONTIGUOUS_BIT;
/* Set up lock for main malloc area */
gm->mflags = mparams.default_mflags;
(void)INITIAL_LOCK(&gm->mutex);
#if LOCK_AT_FORK
pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child);
#endif
magic = kStartTsc;
magic |= (size_t)8U; /* ensure nonzero */
magic &= ~(size_t)7U; /* improve chances of fault for bad values */
/* Until memory modes commonly available, use volatile-write */
(*(volatile size_t *)(&(mparams.magic))) = magic;
}
RELEASE_MALLOC_GLOBAL_LOCK();
}

View file

@ -1,5 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_MEM_DLMALLOC_H_
#define COSMOPOLITAN_LIBC_MEM_DLMALLOC_H_
#ifndef __STRICT_ANSI__
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
@ -1255,4 +1256,5 @@ mchunkptr dlmalloc_try_realloc_chunk(mstate, mchunkptr, size_t, int) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_MEM_DLMALLOC_H_ */

30
third_party/dlmalloc/initdlmalloc.S vendored Normal file
View file

@ -0,0 +1,30 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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"
.source __FILE__
/ Sneak ahead ctor list b/c runtime weakly links malloc.
.init.start 800,_init_dlmalloc
push %rdi
push %rsi
call dlmalloc_init
pop %rsi
pop %rdi
.init.end 800,_init_dlmalloc,globl,hidden