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 / seed / ng / replay / tok_journal.om

659 lines · 12802 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/replay/tok_journal.om: Hash-chained tok-boundary effect journal ("OTKJ" v1).
3note Pure seeded OM-2. Requires seed/ng/sha256.om linked earlier (for sh_dat, sh_all, sh_dig).
4note
5note Wire format ("OTKJ" v1, little-endian, no padding):
6note Header (16 bytes):
7note [0..3] magic "OTKJ" (79 84 75 74)
8note [4] version = 1
9note [5] flags = 0 (reserved, must be 0)
10note [6..7] reserved = 0 (must be 0)
11note [8..15] entry_count u64
12note Entry (85 + payload_len bytes):
13note [0..7] seq u64 (strictly monotonic 0, 1, 2, ...)
14note [8] op_kind u8 (closed set 1..13)
15note [9..12] block_id u32
16note [13..16] op_idx u32
17note [17..20] payload_len u32
18note [21..] payload_bytes[payload_len]
19note [+0..31] prev_hash[32] (zeros for seq 0, else previous entry_hash)
20note [+32..63] entry_hash[32] = SHA-256("OTKJ1\0" || seq || op_kind || block_id || op_idx || payload_len || payload_bytes || prev_hash)
21note
22note Growable run-time pools (no arbitrary small hard cap) + direct disk streaming + alarm threshold.
23
24const TKJ_OK ulong 0
25const TKJ_ERR_MAGIC ulong 1
26const TKJ_ERR_VER ulong 2
27const TKJ_ERR_TRUNC ulong 3
28const TKJ_ERR_TRAILING ulong 4
29const TKJ_ERR_SEQ ulong 5
30const TKJ_ERR_KIND ulong 6
31const TKJ_ERR_CHAIN ulong 7
32const TKJ_ERR_HASH ulong 8
33const TKJ_ERR_IO ulong 9
34const TKJ_ERR_NOMEM ulong 10
35
36const TKJ_HDR_LEN ulong 16
37const TKJ_ENT_FIXED ulong 85
38
39note Closed set of tok-crossing event kinds (1..13)
40const TKJ_OP_CHECK ulong 1
41const TKJ_OP_STORE ulong 2
42const TKJ_OP_ATOM ulong 3
43const TKJ_OP_ALLOCA ulong 4
44const TKJ_OP_CALL ulong 5
45const TKJ_OP_HOST ulong 6
46const TKJ_OP_POOL ulong 7
47const TKJ_OP_TRAP ulong 8
48const TKJ_OP_EXIT ulong 9
49const TKJ_OP_POWER ulong 10
50const TKJ_OP_NET ulong 11
51const TKJ_OP_SENSOR ulong 12
52const TKJ_OP_CKPT_MARK ulong 13
53const TKJ_OP_MAX ulong 13
54
55text tj_magic "OTKJ"
56text tj_dom "OTKJ1"
57
58note Growable run-time pools (unbounded by any small fixed cap)
59global tj_buf uchar *
60global tj_eoff ulong *
61global tj_epoff ulong *
62global tj_eplen uint *
63global tj_eseq ulong *
64global tj_ekind uchar *
65global tj_eblk uint *
66global tj_eopi uint *
67
68note State: 0 buf_used, 1 entry_count, 2 alarm_bytes (0=off), 3 alarm_tripped,
69note 4 stream_fd_open, 5 stream_fd, 6 stream_bytes, 7 stream_entries, 8 verify_next_pos
70global tj_st ulong 12
71global tj_last_h uchar 32
72global tj_cur_h uchar 32
73global tj_tmp_p uchar 65536
74global tj_io uchar 65536
75global tj_path uchar 4096
76global tj_plen ulong 1
77
78fn tkj_ensure_buf need ulong to ulong
79cur = len tj_buf
80if le need cur
81ret TKJ_OK
82end
83cap = cur
84if eq cap 0
85cap = ulong 4096
86end
87while lt cap need bound 64
88cap = shl cap 1
89end
90rc = enlarge tj_buf cap
91if lt rc 0
92ret TKJ_ERR_NOMEM
93end
94ret TKJ_OK
95end
96
97fn tkj_ensure_idx need ulong to ulong
98cur = len tj_eoff
99if le need cur
100ret TKJ_OK
101end
102cap = cur
103if eq cap 0
104cap = ulong 64
105end
106while lt cap need bound 64
107cap = shl cap 1
108end
109r0 = enlarge tj_eoff cap
110if lt r0 0
111ret TKJ_ERR_NOMEM
112end
113r1 = enlarge tj_epoff cap
114if lt r1 0
115ret TKJ_ERR_NOMEM
116end
117r2 = enlarge tj_eplen cap
118if lt r2 0
119ret TKJ_ERR_NOMEM
120end
121r3 = enlarge tj_eseq cap
122if lt r3 0
123ret TKJ_ERR_NOMEM
124end
125r4 = enlarge tj_ekind cap
126if lt r4 0
127ret TKJ_ERR_NOMEM
128end
129r5 = enlarge tj_eblk cap
130if lt r5 0
131ret TKJ_ERR_NOMEM
132end
133r6 = enlarge tj_eopi cap
134if lt r6 0
135ret TKJ_ERR_NOMEM
136end
137ret TKJ_OK
138end
139
140fn tkj_put_u32 off ulong v ulong to void
141put tj_buf off uchar and v 255
142put tj_buf add off 1 uchar and shr v 8 255
143put tj_buf add off 2 uchar and shr v 16 255
144put tj_buf add off 3 uchar and shr v 24 255
145end
146
147fn tkj_put_u64 off ulong v ulong to void
148for j ulong 8
149b = uchar and shr v mul j 8 255
150put tj_buf add off j b
151end
152end
153
154fn tkj_get_u32 off ulong to ulong
155v = ulong 0
156for j ulong 4
157b = ulong get tj_buf add off j
158v = or v shl b mul j 8
159end
160ret v
161end
162
163fn tkj_get_u64 off ulong to ulong
164v = ulong 0
165for j ulong 8
166b = ulong get tj_buf add off j
167v = or v shl b mul j 8
168end
169ret v
170end
171
172fn tkj_reset to ulong
173put tj_st 0 0
174put tj_st 1 0
175put tj_st 3 0
176for j ulong 32
177put tj_last_h j 0
178put tj_cur_h j 0
179end
180rc = tkj_ensure_buf TKJ_HDR_LEN
181if ne rc TKJ_OK
182ret rc
183end
184for j ulong 4
185b = get tj_magic j
186put tj_buf j b
187end
188put tj_buf 4 1
189put tj_buf 5 0
190put tj_buf 6 0
191put tj_buf 7 0
192tkj_put_u64 8 0
193put tj_st 0 TKJ_HDR_LEN
194ret TKJ_OK
195end
196
197fn tkj_set_alarm_bytes limit ulong to void
198put tj_st 2 limit
199put tj_st 3 0
200end
201
202fn tkj_alarm_tripped to ulong
203ret get tj_st 3
204end
205
206fn tkj_count to ulong
207ret get tj_st 1
208end
209
210fn tkj_bytes to ulong
211ret get tj_st 0
212end
213
214note Compute entry_hash for serialized entry at buf_off with payload_len plen.
215fn tkj_hash_entry buf_off ulong plen ulong to ulong
216body_len = add add 21 plen 32
217tot = add 6 body_len
218if gt tot len sh_dat
219ret TKJ_ERR_NOMEM
220end
221for j ulong 5
222put sh_dat j get tj_dom j
223end
224put sh_dat 5 0
225for j ulong body_len bound 16777216
226b = get tj_buf add buf_off j
227put sh_dat add 6 j b
228end
229sh_all tot
230ret TKJ_OK
231end
232
233note Pack 4 u64 words into tj_tmp_p[0..32) (call tkj_append kind blk op_idx 32 next).
234fn tkj_pack_u64x4 w0 ulong w1 ulong w2 ulong w3 ulong to void
235for j ulong 8
236s = mul j 8
237put tj_tmp_p j uchar and shr w0 s 255
238put tj_tmp_p add 8 j uchar and shr w1 s 255
239put tj_tmp_p add 16 j uchar and shr w2 s 255
240put tj_tmp_p add 24 j uchar and shr w3 s 255
241end
242end
243
244fn tkj_append_tail seq ulong off ulong plen ulong to ulong
245p_off = add off 21
246for j ulong plen bound 65536
247b = get tj_tmp_p j
248put tj_buf add p_off j b
249end
250h_off = add p_off plen
251for j ulong 32
252b = get tj_last_h j
253put tj_buf add h_off j b
254end
255rc = tkj_hash_entry off plen
256if ne rc TKJ_OK
257ret rc
258end
259eh_off = add h_off 32
260for j ulong 32
261b = get sh_dig j
262put tj_buf add eh_off j b
263put tj_last_h j b
264end
265put tj_eoff seq off
266put tj_epoff seq p_off
267put tj_eplen seq uint plen
268put tj_eseq seq seq
269ret TKJ_OK
270end
271
272note Append one tok-crossing event using tj_tmp_p[0..plen) as payload.
273fn tkj_append kind ulong blk ulong op_idx ulong plen ulong to ulong
274if or eq kind 0 gt kind TKJ_OP_MAX
275ret TKJ_ERR_KIND
276end
277if gt plen len tj_tmp_p
278ret TKJ_ERR_NOMEM
279end
280seq = get tj_st 1
281rc = tkj_ensure_idx add seq 1
282if ne rc TKJ_OK
283ret rc
284end
285off = get tj_st 0
286rec_len = add TKJ_ENT_FIXED plen
287rc = tkj_ensure_buf add off rec_len
288if ne rc TKJ_OK
289ret rc
290end
291tkj_put_u64 off seq
292put tj_buf add off 8 uchar kind
293tkj_put_u32 add off 9 blk
294tkj_put_u32 add off 13 op_idx
295tkj_put_u32 add off 17 plen
296rc = tkj_append_tail seq off plen
297if ne rc TKJ_OK
298ret rc
299end
300put tj_ekind seq uchar kind
301put tj_eblk seq uint blk
302put tj_eopi seq uint op_idx
303new_cnt = add seq 1
304new_used = add off rec_len
305put tj_st 1 new_cnt
306put tj_st 0 new_used
307tkj_put_u64 8 new_cnt
308lim = get tj_st 2
309if and ne lim 0 ge new_used lim
310put tj_st 3 1
311end
312ret TKJ_OK
313end
314
315note Read u64 word widx from entry e's payload.
316fn tkj_entry_u64 e ulong widx ulong to ulong
317po = get tj_epoff e
318pl = ulong get tj_eplen e
319bo = mul widx 8
320if gt add bo 8 pl
321ret 0
322end
323ret tkj_get_u64 add po bo
324end
325
326note Verify one entry at pos; on success sets tj_st[8] to next pos and returns TKJ_OK.
327fn tkj_verify_hashes pos ulong plen ulong to ulong
328ph_off = add add pos 21 plen
329for j ulong 32
330b = get tj_buf add ph_off j
331if ne b get tj_cur_h j
332ret TKJ_ERR_CHAIN
333end
334end
335rc = tkj_hash_entry pos plen
336if ne rc TKJ_OK
337ret rc
338end
339eh_off = add ph_off 32
340for j ulong 32
341b = get tj_buf add eh_off j
342d = get sh_dig j
343if ne b d
344ret TKJ_ERR_HASH
345end
346put tj_cur_h j d
347end
348put tj_st 8 add eh_off 32
349ret TKJ_OK
350end
351
352fn tkj_verify_one i ulong pos ulong tot_bytes ulong to ulong
353if gt add pos TKJ_ENT_FIXED tot_bytes
354ret TKJ_ERR_TRUNC
355end
356seq = tkj_get_u64 pos
357if ne seq i
358ret TKJ_ERR_SEQ
359end
360kind = ulong get tj_buf add pos 8
361if or eq kind 0 gt kind TKJ_OP_MAX
362ret TKJ_ERR_KIND
363end
364blk = tkj_get_u32 add pos 9
365opi = tkj_get_u32 add pos 13
366plen = tkj_get_u32 add pos 17
367if gt plen sub tot_bytes add pos TKJ_ENT_FIXED
368ret TKJ_ERR_TRUNC
369end
370rc = tkj_verify_hashes pos plen
371if ne rc TKJ_OK
372ret rc
373end
374rc = tkj_ensure_idx add i 1
375if ne rc TKJ_OK
376ret rc
377end
378put tj_eoff i pos
379put tj_epoff i add pos 21
380put tj_eplen i uint plen
381put tj_eseq i seq
382put tj_ekind i uchar kind
383put tj_eblk i uint blk
384put tj_eopi i uint opi
385ret TKJ_OK
386end
387
388note Fail-closed verification and indexing of tj_buf[0..tot_bytes).
389fn tkj_verify_buf tot_bytes ulong to ulong
390if lt tot_bytes TKJ_HDR_LEN
391ret TKJ_ERR_TRUNC
392end
393for j ulong 4
394b = get tj_buf j
395if ne b get tj_magic j
396ret TKJ_ERR_MAGIC
397end
398end
399v4 = get tj_buf 4
400if ne v4 1
401ret TKJ_ERR_VER
402end
403v5 = get tj_buf 5
404v6 = get tj_buf 6
405v7 = get tj_buf 7
406if or ne v5 0 or ne v6 0 ne v7 0
407ret TKJ_ERR_VER
408end
409ecnt = tkj_get_u64 8
410for j ulong 32
411put tj_cur_h j 0
412end
413pos = ulong TKJ_HDR_LEN
414for i ulong ecnt bound 16777216
415rc = tkj_verify_one i pos tot_bytes
416if ne rc TKJ_OK
417ret rc
418end
419pos = get tj_st 8
420end
421if ne pos tot_bytes
422ret TKJ_ERR_TRAILING
423end
424put tj_st 0 tot_bytes
425put tj_st 1 ecnt
426for j ulong 32
427put tj_last_h j get tj_cur_h j
428end
429ret TKJ_OK
430end
431
432note Serialize tj_buf[0..tj_st[0]) cleanly to disk file at path.
433fn tkj_write_chunk fd int off ulong n ulong to ulong
434for j ulong n bound 65536
435b = get tj_buf add off j
436put tj_io j b
437end
438w = ulong 0
439while lt w n bound 65536
440k = write fd tj_io w sub n w
441if le k 0
442ret TKJ_ERR_IO
443end
444w = add w ulong k
445end
446ret TKJ_OK
447end
448
449fn tkj_write_fd fd int to ulong
450tot = get tj_st 0
451off = ulong 0
452while lt off tot bound 1048576
453n = sub tot off
454if gt n 65536
455n = ulong 65536
456end
457rc = tkj_write_chunk fd off n
458if ne rc TKJ_OK
459r = fclose fd
460ret rc
461end
462off = add off n
463end
464r = fclose fd
465if ne r 0
466ret TKJ_ERR_IO
467end
468ret TKJ_OK
469end
470
471fn tkj_write_file path view uchar * to ulong
472fd = fopen path 0 len path 577 420
473if lt fd 0
474ret TKJ_ERR_IO
475end
476ret tkj_write_fd int fd
477end
478
479fn tkj_write_path to ulong
480fd = fopen tj_path 0 get tj_plen 0 577 420
481if lt fd 0
482ret TKJ_ERR_IO
483end
484ret tkj_write_fd int fd
485end
486
487note Read disk file from open fd into growable tj_buf and fail-closed verify it.
488fn tkj_read_fd fd int to ulong
489off = ulong 0
490while true bound 1048576
491k = read fd tj_io 0 65536
492if lt k 0
493r = fclose fd
494ret TKJ_ERR_IO
495end
496if eq k 0
497break
498end
499n = ulong k
500rc = tkj_ensure_buf add off n
501if ne rc TKJ_OK
502r = fclose fd
503ret rc
504end
505for j ulong n bound 65536
506b = get tj_io j
507put tj_buf add off j b
508end
509off = add off n
510end
511r = fclose fd
512ret tkj_verify_buf off
513end
514
515fn tkj_read_file path view uchar * to ulong
516fd = fopen path 0 len path 0 0
517if lt fd 0
518ret TKJ_ERR_IO
519end
520ret tkj_read_fd int fd
521end
522
523fn tkj_read_path to ulong
524fd = fopen tj_path 0 get tj_plen 0 0 0
525if lt fd 0
526ret TKJ_ERR_IO
527end
528ret tkj_read_fd int fd
529end
530
531note Direct streaming writer for unbounded disk-backed journals.
532fn tkj_stream_init_fd fd int to ulong
533for j ulong 4
534put tj_io j get tj_magic j
535end
536put tj_io 4 1
537for j ulong 11
538put tj_io add 5 j 0
539end
540k = write fd tj_io 0 TKJ_HDR_LEN
541if ne k long TKJ_HDR_LEN
542r = fclose fd
543ret TKJ_ERR_IO
544end
545put tj_st 4 1
546put tj_st 5 ulong fd
547put tj_st 6 TKJ_HDR_LEN
548put tj_st 7 0
549put tj_st 3 0
550for j ulong 32
551put tj_last_h j 0
552end
553ret TKJ_OK
554end
555
556fn tkj_stream_open path view uchar * to ulong
557fd = fopen path 0 len path 577 420
558if lt fd 0
559ret TKJ_ERR_IO
560end
561ret tkj_stream_init_fd int fd
562end
563
564fn tkj_stream_open_path to ulong
565fd = fopen tj_path 0 get tj_plen 0 577 420
566if lt fd 0
567ret TKJ_ERR_IO
568end
569ret tkj_stream_init_fd int fd
570end
571
572fn tkj_stream_flush rec_len ulong to ulong
573fd = int get tj_st 5
574w = ulong 0
575while lt w rec_len bound 65536
576k = write fd tj_io w sub rec_len w
577if le k 0
578ret TKJ_ERR_IO
579end
580w = add w ulong k
581end
582new_bytes = add get tj_st 6 rec_len
583put tj_st 6 new_bytes
584put tj_st 7 add get tj_st 7 1
585lim = get tj_st 2
586if and ne lim 0 ge new_bytes lim
587put tj_st 3 1
588end
589ret TKJ_OK
590end
591
592fn tkj_stream_append kind ulong blk ulong op_idx ulong plen ulong to ulong
593if eq get tj_st 4 0
594ret TKJ_ERR_IO
595end
596if or eq kind 0 gt kind TKJ_OP_MAX
597ret TKJ_ERR_KIND
598end
599if gt plen 65000
600ret TKJ_ERR_NOMEM
601end
602seq = get tj_st 7
603for j ulong 8
604put tj_io j uchar and shr seq mul j 8 255
605end
606put tj_io 8 uchar kind
607for j ulong 4
608s = mul j 8
609put tj_io add 9 j uchar and shr blk s 255
610put tj_io add 13 j uchar and shr op_idx s 255
611put tj_io add 17 j uchar and shr plen s 255
612end
613for j ulong plen bound 65000
614put tj_io add 21 j get tj_tmp_p j
615end
616ph_off = add 21 plen
617for j ulong 32
618put tj_io add ph_off j get tj_last_h j
619end
620body_len = add ph_off 32
621for j ulong 5
622put sh_dat j get tj_dom j
623end
624put sh_dat 5 0
625for j ulong body_len bound 65536
626put sh_dat add 6 j get tj_io j
627end
628sh_all add 6 body_len
629for j ulong 32
630b = get sh_dig j
631put tj_io add body_len j b
632put tj_last_h j b
633end
634ret tkj_stream_flush add body_len 32
635end
636
637fn tkj_stream_close to ulong
638if eq get tj_st 4 0
639ret TKJ_ERR_IO
640end
641fd = int get tj_st 5
642cnt = get tj_st 7
643for j ulong 8
644put tj_io j uchar and shr cnt mul j 8 255
645end
646sk = fseek fd 8 0
647if lt sk 0
648r = fclose fd
649put tj_st 4 0
650ret TKJ_ERR_IO
651end
652k = write fd tj_io 0 8
653r = fclose fd
654put tj_st 4 0
655if ne k 8
656ret TKJ_ERR_IO
657end
658ret TKJ_OK
659end