interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / bridge / shlib / README.md

167 lines · 12475 bytes Raw Blame History Rendered
1# bridge/shlib: shared libraries from Omega alone
2
3`shlib.om` writes an AArch64 Linux shared object (ELF64 `ET_DYN`) from two inputs: the native
4object `seed/out/omc` emits for an OM-2 unit in object mode (`OMOBJ001`, `spec/omegese-modules.md`)
5and a table naming the soname, the libraries it needs, its states, its lock, its imports and its
6exports. No C compiler, assembler or linker takes part. The same program writes a probe executable
7that the system's dynamic loader runs against the library.
8
9```
10seed/out/omc < bridge/shlib/shlib.om > shlib
11{ printf 'object\nom2\n'; cat bridge/shlib/rmfake.om bridge/shlib/nvrmfake.om; } | seed/out/omc > fake.obj
12./shlib lib bridge/shlib/nvrmfake.tsv fake.obj libomega-nvrm.so.1
13N=gpu/nvidia/native/nvml
14{ printf 'object\nom2\n'; cat $N/nvml.om $N/query.om $N/step.om
15 sed '/^note/!s/\bat\b/at_/g' $N/errstr.om; cat bridge/shlib/nvml.om; } | seed/out/omc > nvml.obj
16./shlib lib bridge/shlib/nvml.tsv nvml.obj libnvidia-ml.so.1
17./shlib call bridge/shlib/nvml.tsv nvprobe && ./nvprobe; echo $?
18```
19
20The `sed` line is the OM-2 view of `errstr.om`: its parameter `at` is a reserved word in OM-2,
21so the whole-word token becomes `at_` outside note lines (the gate does the same in Omega). The
22table format, the refusals and the exit codes are at the top of `shlib.om`.
23
24## What the library holds
25
26- `.hash` (SysV), `.dynsym` (one global function per export row, then one undefined function per
27 import), `.dynstr` (soname first), `.text`, `.dynamic` (`DT_NEEDED` per needed row, `DT_RUNPATH`,
28 the hash, string and symbol tables, `DT_SONAME`, and `DT_RELA` when there are imports),
29 `.rela.dyn` (one `R_AARCH64_GLOB_DAT` per import), `.got` and `.bss` (the states).
30- One wrapper per export turns a C call (AAPCS64) into an omc function call. It saves x19..x30
31 and d8..d15. It builds the per-call context omc expects in x7, whose byte offset 16
32 (`[x7, #16]`) holds the trap entry,
33 and sign- or zero-extends narrow integer arguments, because omc assumes they are already
34 extended. A trap inside Omega code (for example a division by zero) returns the row's TRAP
35 value to the C caller with every saved register restored.
36- A frame export gets, as its first parameter, a frame holding the C argument registers x0..x7 as
37 they came, the address of every state and the address of the caller's stack arguments (C argument 9
38 on); each state it names in its row is passed as a further
39 sized parameter. States are zero-filled, 64-byte aligned, one copy per process. That is how
40 `nvml.om` keeps the NVML state between calls and reaches C pointers whose length is only known at
41 run time (through the `memcpy` import).
42- A lock row makes every frame export call LOCK with the lock state's address before its body, and
43 UNLOCK on both the return and the trap path. LOCK answering nonzero returns the TRAP value
44 without running the body.
45- An `extern fn` of the object is an import: omc's BL goes to a veneer that saves x7 and x30, makes
46 the C arguments from omc's parameter slots (an import row can reorder them and add byte offsets,
47 so one omc slot can feed two C arguments), calls through the GOT and extends a narrow result.
48- omc code runs on the caller's stack. The generator settles each function's stack need from the
49 object's call records, as the module linker does (`need(f) = max(local(f), cost(call) +
50 need(callee))`), and refuses a cycle (every call costs at least its save area, so a cycle keeps
51 growing and is still changing in the last pass) or a need larger than the 2^48-byte user address
52 space of AArch64 Linux. When an export's body needs stack, its wrapper first reads one word in
53 each 4 KiB page below its own lowest store (`[sp, #16]`), down past that need, top page first.
54 No two touches are more than 4096 bytes apart, so a caller whose stack is too small faults on a
55 4 KiB guard page before the body runs, instead of the body writing past the guard into other
56 memory. That assumes a guard of at least 4 KiB directly below the stack (a thread created with
57 guard size 0 has none) and a caller whose own stack pointer is inside mapped stack. The gate
58 checks the probe's words and, with a mutant generator that drops the call cost, fails. The fault
59 itself is shown outside the tree (a C caller on an 8 KiB thread stack); no receipt of that is
60 kept in this repository.
61- The object's code is copied unchanged, at a 16-byte boundary as in the object.
62- Before linking, the generator checks the object's metadata as spec/omegese-modules.md asks:
63 the export count against the header, each function's flags, address (inside the code,
64 4-aligned, increasing), result and parameter records and name padding, unique names, each
65 export's entry B slot pointing at its own function, and each call record (a nonzero cost, a BL
66 inside its source function that branches to the record's destination). Every BL in the code
67 must have exactly one call record. An import must have no address and no stack need.
68
69## Limits
70
71- The x7 check refuses every instruction of the load/store group whose base register is x7,
72 except the trap-entry load `LDR Xt, [X7, #16]`, because the wrapper's saved registers sit in the
73 frame x7 points at. It reads instruction words, so it is a pattern check, not a proof: the
74 lasting fix is for omc to record runtime-entry use in the object's metadata.
75- The wrappers do not save x18. Linux leaves x18 to the caller, but a caller built with a shadow
76 call stack keeps it in x18, and omc code may change it.
77- A C caller must hold up the side of each parameter's declaration: a sized `edit` or `view` is a
78 pointer to at least that many elements, which omc trusts without a check, and a `fresh`
79 parameter must not overlap another argument.
80- The wrappers have no frame record and the library has no `.eh_frame`, so debuggers and
81 `backtrace()` cannot unwind through a wrapper.
82- The probe compares the low 32 bits of each result (`any` skips that, for a result that is an
83 address). A call row passes up to ten arguments (`buf`, `buf+N`, `bufword` or a number), the
84 ninth and tenth on the stack. The buffer starts with every byte 0xA5 and reaches 4096 bytes past
85 the largest offset a row names; `bufset OFF VALUE` stores a u64 in it, `bufeq OFF VALUE` compares
86 the u64 it holds at OFF, and `reteq OFF VALUE` compares the u64 at OFF from the address the last
87 call returned. Its mismatch status is 1 + the row index, capped at 100 so it never collides with
88 the loader's 127 or the register check's 250.
89- The module linker refuses a need above 8 MiB (its program's own stack). A library runs on its
90 caller's stack, which may be larger, so the generator bounds the need only by the address space.
91- Each object refusal names the rule that fired (address, entry slot, call record, its
92 destination, BL coverage, runtime entry, x7 use); the remaining malformations share one message.
93- The code is position independent because omc refuses `text` and `global` in object mode and
94 states are reached with ADRP. If object mode ever allows them, the generator must refuse such
95 constants or relocate them.
96- The trap path returns without a matching BL and RET pair, and the library carries no GCS or
97 BTI property note. On a system that enforces Guarded Control Stack the trap path would fault.
98- A trap skips the rest of the Omega body: whatever the body would have done after it (a cleanup
99 write, a counter) does not happen. A body that needs such a step after a trap in a callee must do
100 it in the callee.
101
102## Gate
103
104`tests/gates/run.sh shlib` (manifest `tests/gates/omega-shlib.gate`, program `gate.om`, in Omega)
105runs these checks; `gate.om`'s header lists each with its exact status:
106
1071. It builds six objects: `test.om`, `dep.om`, `imp.om`, the test transport (`rmfake.om` then
108 `nvrmfake.om`), the NVML unit (four files of `gpu/nvidia/native/nvml` then `nvml.om`) and the Ampere
109 QMD unit (`gpu/nvidia/om/encoder/amp_runtime.om`, `amp_qmd.om`, then `ampqmd.om`).
1102. It builds each library and its probe.
1113. It runs each probe from one scratch directory, so `libnvidia-ml.so.1` finds its transport and
112 `libomshimp.so.1` its dependency through `$ORIGIN`. The `test.tsv` probe calls constants, int
113 arguments with dirty upper halves, a trapping division followed by another call, a pointer write
114 read back by a later call, a uint with a dirty upper half, two bodies that need 8 KiB and 16 KiB
115 of stack (one through a private call), a uchar, ushort, byte and short each under dirty upper
116 bits, a division inside a private callee that traps, and a body whose need sits one call cost
117 past a 4 KiB page. The `imp.tsv` probe calls across imports (live registers kept, a swapped and
118 an offset argument, the C library's `strlen`), keeps a counter in a state, and shows that a trap
119 releases the lock. The `nvml.tsv` probe drives the real NVML bodies against the RM fake: init,
120 count, handles, name, UUID, temperature, memory, utilization, the running processes, error
121 strings, refused arguments and shutdown, and the narrow arguments (error code, index, name
122 length, sensor) with their upper 32 bits set. It compares what each call writes, the word after
123 it and the words at each pointer a refused call was given, and each error string, with the C
124 library.
125 Each probe also sets x19..x27, x29 and d8..d15 and checks that they and sp come back unchanged.
1264. It runs `/usr/bin/readelf -W -S -d --dyn-syms` on each library. readelf reads the symbols
127 through the section headers, which the loader never looks at; it must exit 0, print no
128 warning, show the table's soname and every needed library and list every export of the table
129 (at least one) as a `FUNC GLOBAL DEFAULT` symbol line. readelf is a read-only verification
130 oracle, not part of the build.
1315. It reads the test library's wrappers (found through `.dynsym`): the stack probes, each with
132 its ADD, MOVZ and MOVKs and the wrapper's BL after it, covering the page counts `gate.om`'s
133 header gives; and in each wrapper the 10 STP words that save x19..x30 and d8..d15, with the
134 matching LDPs somewhere outside the wrappers (the restore controls check the shared return at
135 run time).
1366. It requires a second build from the same object to have identical bytes.
137
138Its controls change one thing each (the table, the object, the library or its copy, the composed
139source) and must be caught with their exact exit status and, where one is given, the refusal's own
140text. `gate.om`'s header lists the controls; `run.sh` pins the summary line.
141
142## NVML
143
144`libnvidia-ml.so.1` built from `nvml.tsv` exports the 11 functions of CUDApp's
145`compat/nvml/libnvidia-ml.map` with its `nvml.h` C signatures, with real bodies: `nvml.om` is
146CUDApp's `native/nvml/api.om` shell and the checks its generated C glue made, in Omega, calling
147the unchanged request protocol (`nvml_step`) and error text (`nvml_errstr`) directly. It needs
148`libomega-nvrm.so.1`, the transport (`omega2_nvrm_open_file`, `omega2_nvrm_close_file`,
149`omega2_nvrm_ioctl`), and runs every call under one pthread mutex. In the gate the transport is the
150Omega RM fake (`nvrmfake.om` over CUDApp's `tests/nvml/rmfake.om`, copied unchanged). No device is
151opened by the gate; a run against `/dev/nvidia*` needs the real transport and the GPU.
152
153Where it can differ from the C library, only after a bug (a trap): the call returns 999
154(`nvmlErrorString`: NULL) at once, and a trap inside the step does not reset the step's resume
155point as the C does after its own trap.
156
157## CUDApp C entry points
158
159`ampqmd.om` with `ampqmd.tsv` is CUDApp's `src/omega_ampere_qmd.c` (its default build) as
160`libomega-ampere-qmd.so.1`: `omega_ampere_policy` and `omega_ampere_build_qmd` over the unchanged
161`amp_qmd` unit. `omega_ampere_build_qmd` has ten parameters; the last two come on the caller's stack,
162which a frame export reaches through the frame word after its states. The gate's probe checks the three
163refusals; one accepted build whose last two arguments it passes on the stack, every narrow argument with
164its upper bits set; the same build as a policy request; the policy with no q; and the byte-reserve
165question CUDApp's `dma_alloc` asks (op 2) at four sizes around the reserve and the free bytes. It compares
166the 256 QMD bytes each writes, the word after them and the request words, with what the C writes for the
167same arguments; 200000 random builds are compared with the C outside this tree.