diff --git a/asm/uncommonlisp b/asm/uncommonlisp index 367417e..dacd550 100755 Binary files a/asm/uncommonlisp and b/asm/uncommonlisp differ diff --git a/asm/uncommonlisp.o b/asm/uncommonlisp.o index 7b0d39e..bf826ba 100644 Binary files a/asm/uncommonlisp.o and b/asm/uncommonlisp.o differ diff --git a/asm/uncommonlisp.s b/asm/uncommonlisp.s index 8f372a6..c4c0a99 100644 --- a/asm/uncommonlisp.s +++ b/asm/uncommonlisp.s @@ -1626,7 +1626,9 @@ env_lookup_both: cmpq $TAG_PAIR, %rax je .ev_define_func - # Simple: (define var expr) + # Simple: (define var expr) — or (define var) with no expr → VAL_VOID + cmpq $VAL_NIL, %r12 + je .ev_define_no_expr movq %r12, %rax andq $-8, %rax movq (%rax), %rdi # expr @@ -1634,6 +1636,10 @@ env_lookup_both: movq %rbp, %rsi call eval popq %rbx # var symbol + jmp .ev_define_bind +.ev_define_no_expr: + movq $VAL_VOID, %rax +.ev_define_bind: movq %rbx, %rdi movq %rax, %rsi movq %r14, %rdx @@ -3927,13 +3933,15 @@ bi_portal_resume: js .pr_fail movq %rax, %rbx # fd - # Read header + # Read header — must get full PORTAL_HDR_SIZE bytes subq $PORTAL_HDR_SIZE, %rsp movq $SYS_READ, %rax movq %rbx, %rdi movq %rsp, %rsi movq $PORTAL_HDR_SIZE, %rdx syscall + cmpq $PORTAL_HDR_SIZE, %rax + jne .pr_bad_magic # short read → file is not a portal # Verify magic leaq portal_magic(%rip), %rdi @@ -3941,9 +3949,15 @@ bi_portal_resume: cmpq (%rdi), %rax jne .pr_bad_magic - # Read heap size, base, r14, r15 + # Sanity check header fields before committing movq 8(%rsp), %rcx # heap_size + testq %rcx, %rcx + jle .pr_bad_magic # zero or negative heap size → corrupt movq 16(%rsp), %rdx # heap_base (must match our mmap) + testq %rdx, %rdx + jz .pr_bad_magic # null heap base → corrupt + + # Header looks valid — commit to r14/r15 restore movq 24(%rsp), %r14 # restore global env movq 32(%rsp), %r15 # restore bump pointer diff --git a/tests/portal-benchmark.sh b/tests/portal-benchmark.sh new file mode 100755 index 0000000..a7fa816 --- /dev/null +++ b/tests/portal-benchmark.sh @@ -0,0 +1,179 @@ +#!/bin/bash +# portal-benchmark.sh — measure portal save/load across: +# 1. Same-process save + load per impl + format +# 2. Cross-process (proc A saves, proc B loads) same server, same binary +# 3. Mismatch cases (wrong format, corrupt file, truncated input) +# +# Usage: bash tests/portal-benchmark.sh +# +# Output: one table. Times are wall time averaged over N iterations. + +set -u +cd "$(dirname "$0")/.." + +PY="python3 uncommonlisp.py --fast" +C="./c/uncommonlisp" +ASM="./asm/uncommonlisp" +N=50 + +pad() { printf " %-42s " "$1"; } + +time_n() { + local n=$1 cmd=$2 t0 t1 + t0=$(date +%s.%N) + for _ in $(seq 1 "$n"); do eval "$cmd" >/dev/null 2>&1; done + t1=$(date +%s.%N) + python3 -c "print(f'{(float(\"$t1\")-float(\"$t0\"))*1000/$n:8.3f} ms/iter')" +} + +PROLOGUE='(define my-int 42) +(define my-list (list 1 2 3 4 5 6 7 8 9 10)) +(define my-str "hello world") +(define (fib n) (let loop ((a 0) (b 1) (i 0)) (if (= i n) a (loop b (+ a b) (+ i 1))))) +(define my-result (fib 30))' + +# Save programs that write the same state in sexp / binary / JSON +cat > /tmp/pb-sexp-save.lsp < /tmp/pb-bin-save.lsp < /tmp/pb-json-save.lsp < /tmp/pb-noop.lsp < /tmp/pb-sexp-load.lsp <<'EOF' +(load "/tmp/pb-state.sexp") +EOF +cat > /tmp/pb-bin-load.lsp <<'EOF' +(portal-resume "/tmp/pb-state.binary") +EOF + +# Pre-materialize files so "load" cases have something to load. +$PY /tmp/pb-sexp-save.lsp >/dev/null 2>&1 +$ASM < /tmp/pb-bin-save.lsp >/dev/null 2>&1 +$PY /tmp/pb-json-save.lsp >/dev/null 2>&1 + +echo "═══════════════════════════════════════════════════════════════" +echo "Portal benchmark — $N iterations per case" +echo " (each iter = one full process: startup + workload + task)" +echo "═══════════════════════════════════════════════════════════════" +echo + +echo "PART 1 — full lifetime: startup + build workload + SAVE" +pad "baseline (workload only, no save)" ; time_n $N "$PY /tmp/pb-noop.lsp" +pad "Python S-exp save (via ports)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp" +pad "C S-exp save (via ports)" ; time_n $N "$C /tmp/pb-sexp-save.lsp" +pad "Asm S-exp save (via ports)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp" +pad "Asm binary portal-save (heap dump)" ; time_n $N "$ASM < /tmp/pb-bin-save.lsp" +pad "Python JSON portal-save (graph-aware)" ; time_n $N "$PY /tmp/pb-json-save.lsp" +echo + +echo "PART 2 — full lifetime: startup + LOAD pre-written state" +pad "Python (load sexp)" ; time_n $N "$PY /tmp/pb-sexp-load.lsp" +pad "C (load sexp)" ; time_n $N "$C /tmp/pb-sexp-load.lsp" +pad "Asm (load sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-load.lsp" +pad "Asm (portal-resume binary)" ; time_n $N "$ASM < /tmp/pb-bin-load.lsp" +echo + +echo "PART 3 — cross-process: proc A saves → proc B loads" +echo " (wall-time for both procs end-to-end; pipes via file)" +pad "Python → Python (sexp)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp" +pad "C → C (sexp)" ; time_n $N "$C /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp" +pad "Asm → Asm (sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp" +pad "Asm → Asm (binary portal)" ; time_n $N "$ASM < /tmp/pb-bin-save.lsp && $ASM < /tmp/pb-bin-load.lsp" +pad "Python → C (sexp)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp" +pad "Python → Asm (sexp)" ; time_n $N "$PY /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp" +pad "C → Python (sexp)" ; time_n $N "$C /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp" +pad "C → Asm (sexp)" ; time_n $N "$C /tmp/pb-sexp-save.lsp && $ASM < /tmp/pb-sexp-load.lsp" +pad "Asm → Python (sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $PY /tmp/pb-sexp-load.lsp" +pad "Asm → C (sexp)" ; time_n $N "$ASM < /tmp/pb-sexp-save.lsp && $C /tmp/pb-sexp-load.lsp" +echo + +echo "PART 4 — mismatch cases (classify output, errors are expected)" + +# Helper: run, capture output, classify via keyword +run_out() { eval "$1" 2>&1 | head -4 | tr '\n' ' ' | cut -c1-180; } + +probe() { + local label="$1" cmd="$2" + pad "$label" + local out + out=$(run_out "$cmd") + if [ -z "$out" ]; then + echo "(no output / silent)" + else + echo "$out" + fi +} + +# Pre-materialize canonical files for the probes +$ASM < /tmp/pb-bin-save.lsp >/dev/null 2>&1 +$PY /tmp/pb-sexp-save.lsp >/dev/null 2>&1 + +# 4a. Load a binary portal as if it were sexp → reader errors +cat > /tmp/pb-load-binary-as-sexp.lsp <<'EOF' +(load "/tmp/pb-state.binary") (display "reached end") (newline) +EOF +probe "Python loads asm BINARY as sexp" "$PY /tmp/pb-load-binary-as-sexp.lsp" +probe "C loads asm BINARY as sexp" "$C /tmp/pb-load-binary-as-sexp.lsp" +probe "Asm loads asm BINARY as sexp" "$ASM < /tmp/pb-load-binary-as-sexp.lsp" + +# 4b. Portal-resume on a sexp file (wrong magic) → #f +cat > /tmp/pb-resume-sexp.lsp <<'EOF' +(display (portal-resume "/tmp/pb-state.sexp")) (newline) +EOF +probe "Asm portal-resume on SEXP file" "$ASM < /tmp/pb-resume-sexp.lsp" + +# 4c. Load an empty file +: > /tmp/pb-empty.lsp +cat > /tmp/pb-empty-test.lsp <<'EOF' +(load "/tmp/pb-empty.lsp") (display "continued") (newline) +EOF +probe "Python load EMPTY file" "$PY /tmp/pb-empty-test.lsp" +probe "C load EMPTY file" "$C /tmp/pb-empty-test.lsp" +probe "Asm load EMPTY file" "$ASM < /tmp/pb-empty-test.lsp" + +# 4d. Load a truncated sexp (unclosed paren, no value) +echo -n "(define truncated" > /tmp/pb-trunc.sexp +cat > /tmp/pb-trunc-test.lsp <<'EOF' +(load "/tmp/pb-trunc.sexp") (display "continued") (newline) +EOF +probe "Python load TRUNCATED sexp" "$PY /tmp/pb-trunc-test.lsp" +probe "C load TRUNCATED sexp" "$C /tmp/pb-trunc-test.lsp" +probe "Asm load TRUNCATED sexp" "$ASM < /tmp/pb-trunc-test.lsp" + +# 4e. Load a missing file +rm -f /tmp/pb-nope.xyz +cat > /tmp/pb-missing-test.lsp <<'EOF' +(display (load "/tmp/pb-nope.xyz")) (newline) +(display "continued") (newline) +EOF +probe "Python load MISSING file" "$PY /tmp/pb-missing-test.lsp" +probe "C load MISSING file" "$C /tmp/pb-missing-test.lsp" +probe "Asm load MISSING file" "$ASM < /tmp/pb-missing-test.lsp" + +# 4f. Asm portal-resume on truncated binary (corrupted heap dump) +head -c 40 /tmp/pb-state.binary > /tmp/pb-corrupt.binary +cat > /tmp/pb-corrupt-test.lsp <<'EOF' +(display (portal-resume "/tmp/pb-corrupt.binary")) (newline) +EOF +probe "Asm portal-resume on CORRUPT binary" "$ASM < /tmp/pb-corrupt-test.lsp" + +echo +echo "═══════════════════════════════════════════════════════════════" +echo "Done." + +rm -f /tmp/pb-*.lsp /tmp/pb-*.sexp /tmp/pb-*.binary /tmp/pb-*.json \ + /tmp/pb-state.* /tmp/pb-corrupt.binary /tmp/pb-nope.xyz 2>/dev/null diff --git a/uncommonlisp.py b/uncommonlisp.py index 508e8cf..b001f55 100644 --- a/uncommonlisp.py +++ b/uncommonlisp.py @@ -1084,8 +1084,11 @@ def _call(proc, args, env): def _load(path, env): - with open(path) as f: - src = f.read() + try: + with open(path) as f: + src = f.read() + except UnicodeDecodeError: + raise LispErr(f'{path}: not a text file (binary data encountered)') for expr in read_all(src, track_lines=True): leval(expr, env) ############################################################################### @@ -3663,8 +3666,9 @@ Features: R7RS core, bytecode compiler, full continuations, macros, _load(path, g) except LispErr as e: print(f'error: {e}', file=sys.stderr); sys.exit(1) - except FileNotFoundError: - print(f'file not found: {path}', file=sys.stderr); sys.exit(1) + except FileNotFoundError as e: + missing = e.filename if e.filename else path + print(f'file not found: {missing}', file=sys.stderr); sys.exit(1) return # REPL mode