raz / commit 5d21612d

commit
5d21612d64b0736373b208fc0d6ed345c2e9561b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
5f20fec6
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.

patch · log

4 files changed, +53 -8

README.md (history) modified +5 -0

@@ -65,6 +65,7 @@
6565 ## Using it
6666
6767 jed [-I dir]... [-o file] Main.jed
68+ jed run [-I dir]... Main.jed [arg]...
6869 jed --version
6970
7071 `jed Main.jed` compiles module `Main` and every module it imports, and
@@ -73,6 +74,10 @@
7374 `lib`. A module whose code depends on the system comes in one file per
7475 target, `Name.linux-x86_64.jed` before `Name.jed`; the compiler checks
7576 that all of them export the same things.
77+
78+`jed run Main.jed a b` compiles the same way, then runs the program
79+with the arguments `a b`, writing no file: its output and exit status
80+are the command's.
7681
7782 A program that calls no C is a static executable of a few kilobytes,
7883 holding only the code it reaches. One that declares an `extern`

doc/compiler.md (history) modified +6 -1

@@ -15,7 +15,7 @@
1515 | `src/parse.pas` | 1,816 | 1,685 | Declarations, statements, expressions, built-ins |
1616 | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching |
1717 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
18-| `src/jed.pas` | 191 | 151 | The command line, the entry points |
18+| `src/jed.pas` | 214 | 169 | The command line, the entry points |
1919
2020 Every file opens with a comment that says what it is, what it depends
2121 on and what it leaves out. Read those first; they are the map.
@@ -335,6 +335,11 @@
335335 one starts at a `_start` that calls `__libc_start_main` the way C's own
336336 start-up does, which sets C up and calls back a `main` that runs the
337337 module bodies and the program's `main`.
338+
339+`jed run` writes the executable to a memfd, a file only in memory, and
340+executes it through `/proc/self/fd` in jed's own place, handing it the
341+rest of jed's `argv`. Nothing is left on disk, and the program's
342+status is the command's.
338343
339344 ## Where to start changing it
340345

src/jed.pas modified +30 -7

@@ -1,11 +1,15 @@
11 { jed.pas: the command line.
22
33 jed [-I dir]... [-o file] Main.jed
4+ jed run [-I dir]... Main.jed [arg]...
45 jed --version
56
67 Compiles the main module and every module it imports, writes the
78 entry point, links what it reaches, and writes the executable: by
8- default the main module's file without its .jed.
9+ default the main module's file without its .jed. jed run writes it
10+ to a memfd instead, a file that lives only in memory, and executes
11+ it in jed's place with the arguments after Main.jed, so its status
12+ is the program's; its argv[0] is Main.jed.
913
1014 The program starts by saving argc and argv, runs every module's body
1115 (those of the modules the program imports, imported modules first),
@@ -21,11 +25,13 @@
2125
2226 {$mode objfpc}{$H-}
2327
24-uses math, scan, sym, x64, gen, parse, link, elf;
28+uses math, BaseUnix, syscall, scan, sym, x64, gen, parse, link, elf;
2529
2630 const
2731 Version = '0.3.0';
2832 Report = '2026-10.0'; { the version of doc/jed.md it follows }
33+ MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it }
34+ MfdCloexec = 1; { an ELF still executes: the kernel opens it first }
2935
3036 { The modules the program uses: the main module, Rt, and every module
3137 they import. A module read only for the interface check is not. }
@@ -82,10 +88,10 @@
8288
8389 var
8490 path, outPath, dir, s: shortstring;
85- i, entry, start: longint;
91+ i, first, entry, start, fd: longint;
8692 m, rt: PModule;
8793 main: PObj;
88- hasResult, dynamic: boolean;
94+ hasResult, dynamic, run: boolean;
8995
9096 begin
9197 { constants fold as the machine computes: NaN and infinities, no traps }
@@ -93,7 +99,8 @@
9399 path := '';
94100 outPath := '';
95101 numInc := 0;
96- i := 1;
102+ run := (ParamCount >= 1) and (ParamStr(1) = 'run');
103+ i := 1 + ord(run);
97104 while i <= ParamCount do begin
98105 s := ParamStr(i);
99106 if s = '-I' then begin
@@ -109,10 +116,16 @@
109116 Halt(0);
110117 end else if (s <> '') and (s[1] = '-') then
111118 Fatal('unknown option ' + s)
112- else path := s;
119+ else begin
120+ path := s;
121+ first := i;
122+ if run then break; { the rest are the program's }
123+ end;
113124 inc(i);
114125 end;
115- if path = '' then Fatal('usage: jed [-I dir]... [-o file] Main.jed');
126+ if path = '' then
127+ Fatal('usage: jed [-I dir]... [-o file] Main.jed, or jed run [-I dir]... Main.jed [arg]...');
128+ if run and (outPath <> '') then Fatal('jed run takes no -o');
116129 dir := ParamStr(0);
117130 i := length(dir);
118131 while (i > 0) and (dir[i] <> '/') do dec(i);
@@ -187,5 +200,15 @@
187200 EndChunk(start);
188201
189202 mainPath := path;
203+ if run then begin
204+ { argv from Main.jed on is the program's, ending in nil as it must }
205+ fd := Do_SysCall(MemfdCreate, TSysParam(PChar('jed')), MfdCloexec);
206+ if fd < 0 then Fatal('can''t create a memfd');
207+ Str(fd, s);
208+ outPath := '/proc/self/fd/' + s + #0;
209+ WriteElf(outPath, entry);
210+ FpExecve(@outPath[1], @argv[first], envp);
211+ Fatal('can''t run ' + path);
212+ end;
190213 WriteElf(outPath, entry);
191214 end.

test/check.sh (history) modified +12 -0

@@ -47,6 +47,18 @@
4747 for f in examples/*.jed test/run/*.jed test/libc/*.jed; do
4848 run $(dirname $f) $(basename $f .jed)
4949 done
50+
51+# jed run: a static program and one over C, from memory, with their
52+# arguments, output and status passed through
53+runs() { # runs file status [arg]...
54+ f=$1; want=$2; shift 2
55+ $jed run -I $(dirname $f)/mods $f "$@" > $out/run.out 2>/dev/null
56+ status=$?
57+ if cmp -s $out/run.out ${f%.jed}.expect && [ $status = $want ]; then pass=$((pass + 1))
58+ else echo "FAIL jed run $f: status $status, not $want, or output differs"; fail=$((fail + 1)); fi
59+}
60+runs test/run/Args.jed 4 one 'two words' é
61+runs test/libc/Through.jed 2
5062 for f in test/err/*.jed; do
5163 name=$(basename $f .jed)
5264 if $jed -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then