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

@ -40,7 +40,22 @@
[("comdat"
":req"
":vararg"
["@" ("function"
["@" (;; calculates offset relative to tls block end
;; load: mov %fs:boop@tpoff,%eax
;; addr: mov %fs:0,%rax
;; lea x1@tpoff(%rax),%rax
;; addr: mov %fs:0,%rax
;; add $x1@tpoff,%rax
"tpoff"
;; allocates TLS_index structure
;; lea x@tlsgd(,%rbx,1),%rdi
;; call __tls_get_addr@plt
"tlsgd"
"tlsld"
"dtpmod"
"dtpoff"
"gottpoff"
"function"
"object"
"got"
"size"
@ -154,7 +169,7 @@
;; * mov $'c,%eax
;; * mov $'\n,%eax
;;
("[ \t]\\$\\(\\(?:'\\(?:'\\|\\s\"\\|\\s\\.\\|.\\)\\|\\(?:0x[[:xdigit:]]+\\|0b[01]+\\|[1-9][0-9]*\\|0[0-7]*\\)\\(?:[fb]\\|u?l?l?\\)\\|[-*/&^|()%<>~+]\\|[_.[:alpha:]][-_.[:alnum:]]*\\)+\\)"
("[ \t,]\\$\\(\\(?:'\\(?:'\\|\\s\"\\|\\s\\.\\|.\\)\\|\\(?:0x[[:xdigit:]]+\\|0b[01]+\\|[1-9][0-9]*\\|0[0-7]*\\)\\(?:[fb]\\|u?l?l?\\)\\|[-*/&^|()%<>~+]\\|[_.[:alpha:]][-_.[:alnum:]]*\\)+\\)"
1 font-lock-constant-face)
(cosmo-asm-doctag-keywords)
@ -188,8 +203,7 @@
1 font-lock-keyword-face)
;; it's complicated
(,asm-mode-gas-qualifiers-regexp
. font-lock-type-face))
(,asm-mode-gas-qualifiers-regexp . font-lock-type-face))
cpp-font-lock-keywords

View file

@ -4,48 +4,9 @@
(c11
'("static_assert"))
(clang-builtin-defines
'("__llvm__"))
(gcc-cpp-412-builtin-defines
'("__cplusplus"
"__OBJC__"
"__ASSEMBLER__"
"__STRICT_ANSI__"
"__ELF__"
"__VERSION__"
"__OPTIMIZE__"
"__OPTIMIZE_SIZE__"
"__NO_INLINE__"
"__CHAR_UNSIGNED__"
"__WCHAR_UNSIGNED__"
"__REGISTER_PREFIX__"
"__USER_LABEL_PREFIX__"
"__SIZE_TYPE__"
"__PTRDIFF_TYPE__"
"__WCHAR_TYPE__"
"__WINT_TYPE__"
"__INTMAX_TYPE__"
"__DEPRECATED"
"__EXCEPTIONS"
"__USING_SJLJ_EXCEPTIONS__"
"__GXX_WEAK__"
"__NEXT_RUNTIME__"
"__LP64__"
"__SSP__"
"__SSP_ALL__"
"__unix__"
"__mips__"
"__vax__"
"__ns16000__"))
(gcc-cpp-92-builtin-defines
'("__VA_OPT__"
"__COUNTER__"
"__GFORTRAN__"
"__GCC_ASM_FLAG_OUTPUTS__"
"__GNUC_GNU_INLINE__"
"__GNUC_STDC_INLINE__"
"__SIG_ATOMIC_TYPE__"
"__INT8_TYPE__"
"__INT16_TYPE__"
@ -83,25 +44,7 @@
"__UINT64_C"
"__INTMAX_C"
"__UINTMAX_C"
"__BYTE_ORDER__"
"__ORDER_LITTLE_ENDIAN__"
"__ORDER_BIG_ENDIAN__"
"__ORDER_PDP_ENDIAN__"
"__FLOAT_WORD_ORDER__"
"__GXX_RTTI"
"__GXX_EXPERIMENTAL_CXX0X__"
"__SSP_STRONG__"
"__SSP_EXPLICIT__"
"__SANITIZE_ADDRESS__"
"__SANITIZE_THREAD__"
"__TIMESTAMP__"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16"
"__HAVE_SPECULATION_SAFE_VALUE"
"__GCC_HAVE_DWARF2_CFI_ASM"
"__FP_FAST_FMA"
"__FP_FAST_FMAF"
"__FP_FAST_FMAL"
@ -111,13 +54,7 @@
"__FP_FAST_FMAF128"
"__FP_FAST_FMAF32X"
"__FP_FAST_FMAF64X"
"__FP_FAST_FMAF128X"
"__STDC_WANT_IEC_60559_TYPES_EXT__"
"__GCC_IEC_559"
"__SUPPORT_SNAN__"
"__GCC_IEC_559_COMPLEX"
"__NO_MATH_ERRNO__"
"__gnu__"))
"__FP_FAST_FMAF128X"))
(gcc-builtin-types
'("__builtin_va_list"))
@ -285,8 +222,7 @@
"__builtin_lrintl"
"__builtin_memcpy"
"__builtin_memset"
))
"__builtin_strlen"))
(gcc-builtin-functions-atomic
'("__atomic_load_n"
@ -1296,9 +1232,7 @@
"__is_same"))
(cosmopolitan-builtin-defines
'("__COSMOPOLITAN__"
"__LINKER__"
"COSMOPOLITAN_CXX_START_"
'("COSMOPOLITAN_CXX_START_"
"COSMOPOLITAN_CXX_END_"
"COSMOPOLITAN_CXX_USING_"
"COSMOPOLITAN_C_START_"
@ -1325,8 +1259,6 @@
)
(concat "\\_<"
(regexp-opt (append c11
clang-builtin-defines
gcc-cpp-412-builtin-defines
gcc-cpp-92-builtin-defines
gcc-builtin-types
gcc-builtin-functions

View file

@ -19,7 +19,8 @@
"__STDC_WANT_LIB_EXT1__"))
(defconst cosmo-c-constants-limits
'("SIZEOF_SHORT"
'("IMAGE_BASE_VIRTUAL"
"SIZEOF_SHORT"
"SIZEOF_INT"
"SIZEOF_LONG"
"SIZEOF_LONG_LONG"
@ -45,6 +46,7 @@
"INT16_MAX"
"INT32_MAX"
"INT64_MAX"
"INT128_MAX"
"WINT_MAX"
"WCHAR_MAX"
"INTPTR_MAX"
@ -61,6 +63,7 @@
"INT16_MIN"
"INT32_MIN"
"INT64_MIN"
"INT128_MIN"
"INTMAX_MIN"
"INTPTR_MIN"
"WINT_MIN"
@ -76,6 +79,7 @@
"UINT16_MAX"
"UINT32_MAX"
"UINT64_MAX"
"UINT128_MAX"
"USHRT_MIN"
"ULONG_MIN"
"ULLONG_MIN"
@ -84,6 +88,7 @@
"UINT16_MIN"
"UINT32_MIN"
"UINT64_MIN"
"UINT128_MIN"
"UINTMAX_MIN"
"UINTPTR_MIN"
"MB_CUR_MAX"

View file

@ -201,6 +201,7 @@
(gnu
'("__inline"
"__thread"
"__alignof"
"__typeof"
"__restrict"

View file

@ -5,26 +5,26 @@
"__DATE__"))
(defconst cosmo-cpp-constants-gcc-412
'("__BASE_FILE__"
"__CHAR_BIT__"
"__FUNCTION__"
"__GNUC_MINOR__"
"__GNUC_PATCHLEVEL__"
"__GNUC__"
"__GNUG__"
"__INCLUDE_LEVEL__"
"__INTMAX_MAX__"
"__INT_MAX__"
"__LONG_LONG_MAX__"
"__LONG_MAX__"
"__SCHAR_MAX__"
"__SHRT_MAX__"
"__DBL_MIN__"
"__DBL_MAX__"
"__FLT_MIN__"
"__FLT_MAX__"
"__WCHAR_MAX__"
"__WCHAR_UNSIGNED__"))
'("__BASE_FILE__"
"__CHAR_BIT__"
"__FUNCTION__"
"__GNUC_MINOR__"
"__GNUC_PATCHLEVEL__"
"__GNUC__"
"__GNUG__"
"__INCLUDE_LEVEL__"
"__INTMAX_MAX__"
"__INT_MAX__"
"__LONG_LONG_MAX__"
"__LONG_MAX__"
"__SCHAR_MAX__"
"__SHRT_MAX__"
"__DBL_MIN__"
"__DBL_MAX__"
"__FLT_MIN__"
"__FLT_MAX__"
"__WCHAR_MAX__"
"__WCHAR_UNSIGNED__"))
(defconst cosmo-cpp-constants-gcc-92
'("__x86_64__"

View file

@ -0,0 +1,84 @@
(defconst cosmo-platform-constants-regex
(let (
(gcc412
'("__cplusplus"
"__OBJC__"
"__STRICT_ANSI__"
"__ELF__"
"__VERSION__"
"__OPTIMIZE__"
"__OPTIMIZE_SIZE__"
"__NO_INLINE__"
"__CHAR_UNSIGNED__"
"__WCHAR_UNSIGNED__"
"__REGISTER_PREFIX__"
"__USER_LABEL_PREFIX__"
"__SIZE_TYPE__"
"__PTRDIFF_TYPE__"
"__WCHAR_TYPE__"
"__WINT_TYPE__"
"__INTMAX_TYPE__"
"__DEPRECATED"
"__EXCEPTIONS"
"__USING_SJLJ_EXCEPTIONS__"
"__GXX_WEAK__"
"__NEXT_RUNTIME__"
"__LP64__"
"__SSP__"
"__SSP_ALL__"
"__unix__"
"__mips__"
"__vax__"
"__ns16000__"
"__pic__"
"__pie__"
"__PIC__"
"__PIE__"
"__ASSEMBLER__"
"__code_model_large__"
"__code_model_medium__"))
(cpp92
'("__BYTE_ORDER__"
"__ORDER_LITTLE_ENDIAN__"
"__ORDER_BIG_ENDIAN__"
"__ORDER_PDP_ENDIAN__"
"__FLOAT_WORD_ORDER__"
"__GFORTRAN__"
"__GCC_ASM_FLAG_OUTPUTS__"
"__GNUC_GNU_INLINE__"
"__GNUC_STDC_INLINE__"
"__GXX_RTTI"
"__GXX_EXPERIMENTAL_CXX0X__"
"__SSP_STRONG__"
"__SSP_EXPLICIT__"
"__SANITIZE_ADDRESS__"
"__SANITIZE_THREAD__"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16"
"__HAVE_SPECULATION_SAFE_VALUE"
"__GCC_HAVE_DWARF2_CFI_ASM"
"__STDC_WANT_IEC_60559_TYPES_EXT__"
"__GCC_IEC_559"
"__SUPPORT_SNAN__"
"__GCC_IEC_559_COMPLEX"
"__NO_MATH_ERRNO__"
"__gnu__"))
(clang
'("__llvm__"))
(cosmo
'("__COSMOPOLITAN__"
"__LINKER__"))
)
(concat "\\_<"
(regexp-opt (append clang cpp92 gcc412 cosmo))
"\\_>")))
(provide 'cosmo-platform-constants)

View file

@ -19,6 +19,7 @@
(require 'cosmo-c-builtins)
(require 'cosmo-c-constants)
(require 'cosmo-cpp-constants)
(require 'cosmo-platform-constants)
(require 'dired)
(require 'javadown)
(require 'ld-script)
@ -175,9 +176,13 @@
((file-exists-p (format "%s" buddy))
(format (cosmo-join
" && "
'("m=%s; make -j8 -O o/$m/%s.o MODE=$m SILENT=0"
"objdump -wzCd o/$m/%s.o"
"make -j8 -O MODE=$m %s"))
'("m=%s; n=%s; make -j8 -O o/$m/%s.o MODE=$m SILENT=0"
"make -j8 -O MODE=$m %s"
;; "bloat o/$m/%s.o | head"
;; "nm -C --size o/$m/%s.o | sort -r"
"echo"
"size -A o/$m/$n.o | grep '^[.T]' | grep -v 'debug\\|command.line\\|stack' | sort -rnk2"
"objdump -wzCd o/$m/$n.o"))
mode name name buns))
((eq kind 'run)
(format
@ -196,6 +201,9 @@
" && "
`("m=%s; f=o/$m/%s.o"
,(concat "make -j8 -O $f MODE=$m SILENT=0")
;; "nm -C --size $f | sort -r"
"echo"
"size -A $f | grep '^[.T]' | grep -v 'debug\\|command.line\\|stack' | sort -rnk2"
"objdump -wzCd $f"))
mode name)))))
@ -205,6 +213,7 @@
(root (locate-dominating-file this "Makefile")))
(when root
(let* ((mode (cosmo--make-mode arg))
(compilation-scroll-output nil)
(default-directory root)
(compile-command (cosmo--compile-command this root nil mode)))
(compile compile-command)))))
@ -416,10 +425,11 @@
(defun cosmo-assembly (arg)
(interactive "P")
(setq arg (or arg 2))
;; -ffast-math -funsafe-math-optimizations -fsched2-use-superblocks -fjump-tables
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly (setq arg (logand (lognot 8)))
"SILENT=0 COPTS='-ffast-math -O3 -funsafe-math-optimizations -fsched2-use-superblocks'"))
(t (cosmo--assembly arg "SILENT=0 COPTS='-O3'"))))
"SILENT=0 COPTS='-Os'"))
(t (cosmo--assembly arg "SILENT=0 COPTS='-Os' TARGET_ARCH='-march=znver2 -mdispatch-scheduler' CPPFLAGS='-DSTACK_FRAME_UNLIMITED'"))))
(defun cosmo-assembly-native (arg)
(interactive "P")
@ -427,11 +437,11 @@
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly
(setq arg (logand (lognot 8)))
"SILENT=0 CCFLAGS=--verbose COPTS='$(IEEE_MATH)' TARGET_ARCH='-march=znver2'"))
"SILENT=0 CCFLAGS=--verbose COPTS='$(IEEE_MATH)' CPPFLAGS='-DSTACK_FRAME_UNLIMITED' TARGET_ARCH='-march=k8'")) ;; znver2
(t
(cosmo--assembly
arg
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' TARGET_ARCH='-march=znver2'"))))
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' CPPFLAGS='-DSTACK_FRAME_UNLIMITED' TARGET_ARCH='-march=k8'")))) ;; znver2
(defun cosmo-assembly-icelake (arg)
(interactive "P")
@ -439,15 +449,15 @@
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly
(setq arg (logand (lognot 8)))
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' TARGET_ARCH='-march=icelake-client'"))
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' CPPFLAGS='-DSTACK_FRAME_UNLIMITED' TARGET_ARCH='-march=icelake-client'"))
(t
(cosmo--assembly
arg
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' TARGET_ARCH='-march=icelake-client'"))))
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' CPPFLAGS='-DSTACK_FRAME_UNLIMITED' TARGET_ARCH='-march=icelake-client'"))))
(defun cosmo-assembly-balanced (arg)
(interactive "P")
(cosmo--assembly (or arg 5) "CFLAGS='-O2 -ftrapv' SILENT=0"))
(cosmo--assembly (or arg 5) "CFLAGS='-O2 -ftrapv' CPPFLAGS='-DSTACK_FRAME_UNLIMITED' SILENT=0"))
(defun cosmo-mca (arg)
(interactive "P")
@ -532,12 +542,17 @@
(defun cosmo-run (arg)
(interactive "P")
(let* ((this (or (buffer-file-name) dired-directory))
(root (or (locate-dominating-file this "Makefile") default-directory))
(proj (locate-dominating-file this "Makefile"))
(root (or proj default-directory))
(file (file-relative-name this root)))
(when root
(let ((default-directory root))
(save-buffer)
(cond ((memq major-mode '(c-mode c++-mode asm-mode fortran-mode))
(cond ((file-executable-p file)
(compile (if (cosmo-contains "/" file)
file
(format "./%s" file))))
((memq major-mode '(c-mode c++-mode asm-mode fortran-mode))
(let* ((mode (cosmo--make-mode arg))
(compile-command (cosmo--compile-command this root 'run mode)))
(compile compile-command)))
@ -694,13 +709,15 @@
(font-lock-add-keywords
nil `((,cosmo-c-keywords-regex . font-lock-keyword-face)
(,cosmo-c-builtins-regex . font-lock-builtin-face)
(,cosmo-platform-constants-regex . font-lock-builtin-face)
(,cosmo-cpp-constants-regex . font-lock-constant-face)
(,cosmo-c-constants-regex . font-lock-constant-face)
(,cosmo-c-types-regex . font-lock-type-face))))
(defun cosmo-asm-keywords-hook ()
(font-lock-add-keywords
nil `((,cosmo-cpp-constants-regex . font-lock-constant-face))))
nil `((,cosmo-cpp-constants-regex . font-lock-constant-face)
(,cosmo-platform-constants-regex . font-lock-constant-face))))
(add-hook 'c-mode-common-hook 'cosmo-c-keywords-hook)
(add-hook 'asm-mode-hook 'cosmo-asm-keywords-hook)