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 / aienux / kernel / core / runtime_test.om

426 lines · 9551 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/core/runtime_test.om: Pure-Omega behavioral self-test suite for Aienux Kernel
3note Runtime Slice 2 (First-fit Heap, AArch64 Trap/IRQ/Timer, RAM Klog, Lifecycle FSM, Byte Pipes).
4note Compose with: si/directives/om2.om aienux/kernel/mm/heap.om aienux/kernel/arch/trap_irq.om aienux/kernel/core/klog.om aienux/kernel/core/lifecycle.om aienux/kernel/core/pipe.om aienux/kernel/core/runtime_test.om
5
6text r_pass "PASS "
7text r_fail "FAIL "
8text r_nl "\n"
9text r_t01 "kheap_alloc_zero_coalesce_and_check"
10text r_t02 "kheap_double_free_forged_and_oom"
11text r_t03 "arch_exception_probe_fixup_and_fatal"
12text r_t04 "arch_irq_register_dispatch_and_spurious"
13text r_t05 "arch_timer_math_and_m1_window"
14text r_t06 "klog_head_tail_wrap_and_drop_marker"
15text r_t07 "lifecycle_decode_verbs_and_bounds"
16text r_t08 "lifecycle_transitions_lock_serve_quiesce"
17text r_t09 "pipe_create_rw_eof_broken_and_scrub"
18text r_t10 "pipe_dup_cancel_revoke_and_oshf_dispatch"
19text r_sum "KERNEL_RUNTIME_TEST_PASS total=10 failed=0\n"
20text r_fsum "KERNEL_RUNTIME_TEST_FAIL\n"
21
22text r_k1 "HEAD"
23text r_k2 "0123456789"
24text r_kexpect "HEAD\n[klog: 4 bytes dropped]\n456789"
25text r_lc_task " task build kernel "
26text r_lc_prompt "prompt hello"
27text r_lc_reply "world"
28text r_lc_bad "status extra"
29text r_lc_ok_status "status"
30text r_lc_unlock "unlock"
31text r_lc_approve "approve"
32text r_lc_shutdown "shutdown"
33text r_pipe_msg "hello-pipe"
34
35fn rwrite_all fd int buf view uchar * to bool
36total = len buf
37off = ulong 0
38while lt off total bound 4096
39n = write fd buf off sub total off
40if le n 0
41ret false
42end
43off = add off ulong n
44end
45ret true
46end
47
48fn rreport name view uchar * ok bool to uint
49if ok
50w1 = rwrite_all 1 r_pass
51w2 = rwrite_all 1 name
52w3 = rwrite_all 1 r_nl
53if and w1 and w2 w3
54ret 0
55end
56ret 1
57end
58w1 = rwrite_all 1 r_fail
59w2 = rwrite_all 1 name
60w3 = rwrite_all 1 r_nl
61if and w1 and w2 w3
62ret 1
63end
64ret 1
65end
66
67fn test_01_kheap_basic to bool
68h = new uchar 512 0xaa
69if ne kheap_init h 512 KHEAP_OK
70ret false
71end
72if ne kheap_check h 512 KHEAP_OK
73ret false
74end
75p1 = kheap_alloc h 512 24
76p2 = kheap_alloc h 512 40
77if or eq p1 0 eq p2 0
78ret false
79end
80note Payloads must be 16-byte aligned and zeroed
81if or ne and p1 15 0 ne get h p1 0
82ret false
83end
84if ne kheap_check h 512 KHEAP_OK
85ret false
86end
87note Free both -> coalesces back to single 496-byte free payload
88if ne kheap_free h 512 p1 KHEAP_OK
89ret false
90end
91if ne kheap_free h 512 p2 KHEAP_OK
92ret false
93end
94if ne kheap_free_bytes h 512 496
95ret false
96end
97if ne kheap_check h 512 KHEAP_OK
98ret false
99end
100ret true
101end
102
103fn test_02_kheap_negatives to bool
104h = new uchar 256 0
105if ne kheap_init h 256 KHEAP_OK
106ret false
107end
108p1 = kheap_alloc h 256 32
109if eq p1 0
110ret false
111end
112note Forged pointer refused
113if ne kheap_free h 256 add p1 16 KHEAP_ERR
114ret false
115end
116note Free once succeeds, double-free refused
117if ne kheap_free h 256 p1 KHEAP_OK
118ret false
119end
120if ne kheap_free h 256 p1 KHEAP_ERR
121ret false
122end
123note Over-capacity allocation returns 0
124if ne kheap_alloc h 256 300 0
125ret false
126end
127ret true
128end
129
130fn test_03_arch_exception to bool
131ps = new ProbeState 0 0 0 0 0
132exc_probe_arm ps 0x40001000 0x40001008
133esr_dabt = shl ESR_EC_DABT_CUR 26
134note Armed probe at exact EL1 vector 4 DABT_CUR insn triggers fixup and disarms
135if ne exc_classify 1 VEC_SYNC_EL_SPX esr_dabt 0xdead0000 0x40001000 ps EXC_DISP_PROBE_FIXUP
136ret false
137end
138if or ne get ps armed 0 ne get ps far 0xdead0000
139ret false
140end
141note Second hit while disarmed is fatal
142if ne exc_classify 1 VEC_SYNC_EL_SPX esr_dabt 0xdead0000 0x40001000 ps EXC_DISP_FATAL
143ret false
144end
145note EL0 lower64 sync routes to EXC_DISP_LOWER64
146if ne exc_classify 1 VEC_SYNC_LOWER64 shl ESR_EC_SVC64 26 0 0x1000 ps EXC_DISP_LOWER64
147ret false
148end
149ret true
150end
151
152fn test_04_arch_irq to bool
153irq_table_reset
154note INTID 30 (timer) and >= 1020 refused for registration
155if ne irq_register 30 1 0 IRQ_ERR_BAD
156ret false
157end
158if ne irq_register 1020 1 0 IRQ_ERR_BAD
159ret false
160end
161note Valid registration + duplicate refusal
162if ne irq_register 42 7 0x1234 IRQ_OK
163ret false
164end
165if ne irq_register 42 8 0 IRQ_ERR_DUP
166ret false
167end
168note Dispatch timer (30), registered (42), unhandled (43), spurious (1023)
169if ne irq_dispatch 30 1
170ret false
171end
172if ne irq_dispatch 42 0
173ret false
174end
175if ne irq_dispatch 43 0
176ret false
177end
178if ne irq_dispatch 1023 0
179ret false
180end
181if or ne irq_stat_get 0 1 or ne irq_stat_get 1 1 or ne irq_stat_get 2 1 ne irq_stat_get 3 1
182ret false
183end
184if or ne irq_stat_get 4 7 ne irq_stat_get 5 0x1234
185ret false
186end
187ret true
188end
189
190fn test_05_arch_timer to bool
191note 62.5 MHz counter: 625000 ticks = 10000 us = 10 ms
192if ne timer_counter_to_us 625000 62500000 10000
193ret false
194end
195if ne timer_us_to_ticks 10000 62500000 625000
196ret false
197end
198ts = new TimerState 0 0 0 0 0
199timer_state_init ts 1000000 62500000
200i = ulong 1
201while le i 5 bound 8
202dl = timer_tick_record ts add 1000000 mul i 625000
203if ne dl add 1000000 mul add i 1 625000
204ret false
205end
206i = add i 1
207end
208if not timer_window_ok ts
209ret false
210end
211note Short storm interval (100 ticks < half period 312500) fails M1 window check
212dl2 = timer_tick_record ts add get ts last 100
213if eq dl2 0
214ret false
215end
216if timer_window_ok ts
217ret false
218end
219ret true
220end
221
222fn test_06_klog_wrap to bool
223klog_clear_caps 4 6
224klog_put r_k1
225klog_put r_k2
226if or ne klog_total 14 ne klog_dropped 4
227ret false
228end
229out_b = new uchar 64 0
230nw = klog_read out_b
231if ne nw len r_kexpect
232ret false
233end
234i = ulong 0
235while lt i nw bound 64
236if ne get out_b i get r_kexpect i
237ret false
238end
239i = add i 1
240end
241ret true
242end
243
244fn test_07_lifecycle_decode to bool
245arg = new LcArg 0 0
246v = lc_decode r_lc_task arg
247if or ne v LC_V_TASK or ne get arg off 9 ne get arg alen 12
248ret false
249end
250note No-arg verb with trailing arg must be LC_V_INVALID
251if ne lc_decode r_lc_bad arg LC_V_INVALID
252ret false
253end
254if ne lc_decode r_lc_ok_status arg LC_V_STATUS
255ret false
256end
257ret true
258end
259
260fn test_08_lifecycle_fsm to bool
261lc_init
262lc_start false
263note LOCKED refuses task, accepts unlock
264if ne lc_apply LC_V_TASK r_lc_reply LC_R_REFUSED
265ret false
266end
267if ne lc_apply LC_V_UNLOCK r_lc_unlock LC_R_UNLOCK
268ret false
269end
270lc_unlock_done true
271if ne lc_get_cnt LC_C_STATE LC_READY
272ret false
273end
274note Prompt -> generate_done -> SERVE -> approve -> commit_done -> READY
275if ne lc_apply LC_V_PROMPT r_lc_prompt LC_R_GENERATE
276ret false
277end
278lc_generate_done LC_GEN_REPLY r_lc_reply
279if ne lc_get_cnt LC_C_STATE LC_SERVE
280ret false
281end
282if ne lc_apply LC_V_APPROVE r_lc_approve LC_R_COMMIT
283ret false
284end
285lc_commit_done 0
286if or ne lc_get_cnt LC_C_STATE LC_READY ne lc_get_cnt LC_C_APPROVED 1
287ret false
288end
289note Shutdown -> QUIESCE -> STOP refuses further verbs
290if ne lc_apply LC_V_SHUTDOWN r_lc_shutdown LC_R_STOP
291ret false
292end
293lc_stopped
294if ne lc_apply LC_V_STATUS r_lc_ok_status LC_R_REFUSED
295ret false
296end
297ret true
298end
299
300fn test_09_pipe_core to bool
301pipe_pool_init
302rd = new ulong 1 0
303wr = new ulong 1 0
304if ne pipe_create 0 rd wr OSH_E_OK
305ret false
306end
307n = new ulong 1 0
308note Empty read returns WOULD_BLOCK
309rb = new uchar 16 0
310if ne pipe_read 0 get rd 0 rb n OSH_E_WOULD_BLOCK
311ret false
312end
313note Write then read exact bytes
314if ne pipe_write 0 get wr 0 r_pipe_msg n OSH_E_OK
315ret false
316end
317if ne get n 0 10
318ret false
319end
320if ne pipe_read 0 get rd 0 rb n OSH_E_OK
321ret false
322end
323if ne get n 0 10
324ret false
325end
326for i ulong 10
327if ne get rb i get r_pipe_msg i
328ret false
329end
330end
331note Close writer -> read returns EOF (OK, n=0)
332if ne pipe_close 0 get wr 0 OSH_E_OK
333ret false
334end
335if ne pipe_read 0 get rd 0 rb n OSH_E_OK
336ret false
337end
338if ne get n 0 0
339ret false
340end
341note Close reader -> pool becomes empty
342if ne pipe_close 0 get rd 0 OSH_E_OK
343ret false
344end
345if not pipe_pool_empty
346ret false
347end
348ret true
349end
350
351fn test_10_pipe_dup_revoke_dispatch to bool
352pipe_pool_init
353rd = new ulong 1 0
354wr = new ulong 1 0
355rd2 = new ulong 1 0
356if ne pipe_create 0 rd wr OSH_E_OK
357ret false
358end
359note Dup read end to peer table 1 with attenuated READ-only right
360if ne pipe_dup 0 get rd 0 1 PIPE_R_READ rd2 OSH_E_OK
361ret false
362end
363note Cancel read end -> next read returns INTERRUPTED once
364if ne pipe_cancel 0 get rd 0 OSH_E_OK
365ret false
366end
367n = new ulong 1 0
368rb = new uchar 8 0
369if ne pipe_read 1 get rd2 0 rb n OSH_E_INTERRUPTED
370ret false
371end
372note Revoke pipe -> peer read returns REVOKED
373if ne pipe_revoke 0 get wr 0 OSH_E_OK
374ret false
375end
376if ne pipe_read 1 get rd2 0 rb n OSH_E_REVOKED
377ret false
378end
379note User-VA window bounds check (must reject kernel VA >= PIPE_USER_TOP and wrap)
380if not pipe_user_window_ok 0x1000 512 0x1000 0x4000
381ret false
382end
383if pipe_user_window_ok 0xffff000000000000 64 0x1000 0xffff000000001000
384ret false
385end
386note OSHF versioned frame validation
387f = new ulong 4 0
388put f 0 PIPE_FRAME_MAGIC
389put f 1 PIPE_ABI_VERSION
390put f 2 PIPE_OP_CREATE
391put f 3 0
392if ne pipe_dispatch_check f OSH_E_OK
393ret false
394end
395put f 1 99
396if ne pipe_dispatch_check f OSH_E_NOT_SUPPORTED
397ret false
398end
399ret true
400end
401
402fn main to int
403failed = uint 0
404failed = add failed rreport r_t01 test_01_kheap_basic
405failed = add failed rreport r_t02 test_02_kheap_negatives
406failed = add failed rreport r_t03 test_03_arch_exception
407failed = add failed rreport r_t04 test_04_arch_irq
408failed = add failed rreport r_t05 test_05_arch_timer
409failed = add failed rreport r_t06 test_06_klog_wrap
410failed = add failed rreport r_t07 test_07_lifecycle_decode
411failed = add failed rreport r_t08 test_08_lifecycle_fsm
412failed = add failed rreport r_t09 test_09_pipe_core
413failed = add failed rreport r_t10 test_10_pipe_dup_revoke_dispatch
414if eq failed 0
415w = rwrite_all 1 r_sum
416if w
417ret 0
418end
419ret 1
420end
421w = rwrite_all 1 r_fsum
422if w
423ret 1
424end
425ret 1
426end