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