From 5d21612d64b0736373b208fc0d6ed345c2e9561b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 23:50:54 +0100 Subject: [PATCH] jed run: compile to a memfd and exec it with the rest of argv No temp file: the ELF goes to memfd_create's fd, written via /proc/self/fd/N, then execve replaces jed, so stdio and exit status are the program's. check.sh runs a static and a C program this way. --- diff --git a/README.md b/README.md index d449be9..9d2c4a7 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ ## Using it jed [-I dir]... [-o file] Main.jed + jed run [-I dir]... Main.jed [arg]... jed --version `jed Main.jed` compiles module `Main` and every module it imports, and @@ -73,6 +74,10 @@ `lib`. A module whose code depends on the system comes in one file per target, `Name.linux-x86_64.jed` before `Name.jed`; the compiler checks that all of them export the same things. + +`jed run Main.jed a b` compiles the same way, then runs the program +with the arguments `a b`, writing no file: its output and exit status +are the command's. A program that calls no C is a static executable of a few kilobytes, holding only the code it reaches. One that declares an `extern` diff --git a/doc/compiler.md b/doc/compiler.md index 9a5642a..4822447 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -15,7 +15,7 @@ | `src/parse.pas` | 1,816 | 1,685 | Declarations, statements, expressions, built-ins | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | -| `src/jed.pas` | 191 | 151 | The command line, the entry points | +| `src/jed.pas` | 214 | 169 | The command line, the entry points | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -335,6 +335,11 @@ one starts at a `_start` that calls `__libc_start_main` the way C's own start-up does, which sets C up and calls back a `main` that runs the module bodies and the program's `main`. + +`jed run` writes the executable to a memfd, a file only in memory, and +executes it through `/proc/self/fd` in jed's own place, handing it the +rest of jed's `argv`. Nothing is left on disk, and the program's +status is the command's. ## Where to start changing it diff --git a/src/jed.pas b/src/jed.pas index 46d63c2..7115843 100644 --- a/src/jed.pas +++ b/src/jed.pas @@ -1,11 +1,15 @@ { jed.pas: the command line. jed [-I dir]... [-o file] Main.jed + jed run [-I dir]... Main.jed [arg]... jed --version Compiles the main module and every module it imports, writes the entry point, links what it reaches, and writes the executable: by - default the main module's file without its .jed. + default the main module's file without its .jed. jed run writes it + to a memfd instead, a file that lives only in memory, and executes + it in jed's place with the arguments after Main.jed, so its status + is the program's; its argv[0] is Main.jed. The program starts by saving argc and argv, runs every module's body (those of the modules the program imports, imported modules first), @@ -21,11 +25,13 @@ {$mode objfpc}{$H-} -uses math, scan, sym, x64, gen, parse, link, elf; +uses math, BaseUnix, syscall, scan, sym, x64, gen, parse, link, elf; const Version = '0.3.0'; Report = '2026-10.0'; { the version of doc/jed.md it follows } + MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } + MfdCloexec = 1; { an ELF still executes: the kernel opens it first } { The modules the program uses: the main module, Rt, and every module they import. A module read only for the interface check is not. } @@ -82,10 +88,10 @@ var path, outPath, dir, s: shortstring; - i, entry, start: longint; + i, first, entry, start, fd: longint; m, rt: PModule; main: PObj; - hasResult, dynamic: boolean; + hasResult, dynamic, run: boolean; begin { constants fold as the machine computes: NaN and infinities, no traps } @@ -93,7 +99,8 @@ path := ''; outPath := ''; numInc := 0; - i := 1; + run := (ParamCount >= 1) and (ParamStr(1) = 'run'); + i := 1 + ord(run); while i <= ParamCount do begin s := ParamStr(i); if s = '-I' then begin @@ -109,10 +116,16 @@ Halt(0); end else if (s <> '') and (s[1] = '-') then Fatal('unknown option ' + s) - else path := s; + else begin + path := s; + first := i; + if run then break; { the rest are the program's } + end; inc(i); end; - if path = '' then Fatal('usage: jed [-I dir]... [-o file] Main.jed'); + if path = '' then + Fatal('usage: jed [-I dir]... [-o file] Main.jed, or jed run [-I dir]... Main.jed [arg]...'); + if run and (outPath <> '') then Fatal('jed run takes no -o'); dir := ParamStr(0); i := length(dir); while (i > 0) and (dir[i] <> '/') do dec(i); @@ -187,5 +200,15 @@ EndChunk(start); mainPath := path; + if run then begin + { argv from Main.jed on is the program's, ending in nil as it must } + fd := Do_SysCall(MemfdCreate, TSysParam(PChar('jed')), MfdCloexec); + if fd < 0 then Fatal('can''t create a memfd'); + Str(fd, s); + outPath := '/proc/self/fd/' + s + #0; + WriteElf(outPath, entry); + FpExecve(@outPath[1], @argv[first], envp); + Fatal('can''t run ' + path); + end; WriteElf(outPath, entry); end. diff --git a/test/check.sh b/test/check.sh index 98c9f7a..8b3404b 100755 --- a/test/check.sh +++ b/test/check.sh @@ -47,6 +47,18 @@ for f in examples/*.jed test/run/*.jed test/libc/*.jed; do run $(dirname $f) $(basename $f .jed) done + +# jed run: a static program and one over C, from memory, with their +# arguments, output and status passed through +runs() { # runs file status [arg]... + f=$1; want=$2; shift 2 + $jed run -I $(dirname $f)/mods $f "$@" > $out/run.out 2>/dev/null + status=$? + if cmp -s $out/run.out ${f%.jed}.expect && [ $status = $want ]; then pass=$((pass + 1)) + else echo "FAIL jed run $f: status $status, not $want, or output differs"; fail=$((fail + 1)); fi +} +runs test/run/Args.jed 4 one 'two words' é +runs test/libc/Through.jed 2 for f in test/err/*.jed; do name=$(basename $f .jed) if $jed -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then