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 / store / torn_slot.om

510 lines · 10815 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/store/torn_slot.om: Pure-Omega dual-slot torn-write-safe 4096-byte record.
3note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux
4note native/store/torn_slot.{h,c}.
5note Compose with: si/directives/om2.om aienux/store/torn_slot.om <test.om> om/tls/sha2.om
6
7const TS_RECORD_BYTES ulong 4096
8const TS_UNITS ulong 4
9const TS_HEADER_BYTES ulong 88
10const TS_MEDIA_UNITS ulong 8
11const TS_MEDIA_BYTES ulong 32768
12const TS_U64_MAX ulong 0xffffffffffffffff
13
14note Result codes
15const TS_OK int 0
16const TS_EMPTY int 1
17const TS_CORRUPT int -1
18const TS_CONFLICT int -2
19const TS_EIO int -3
20const TS_EGEOMETRY int -4
21const TS_ESTATE int -5
22
23note Per-slot diagnosis states
24const TS_SLOT_BLANK uint 0
25const TS_SLOT_VALID uint 1
26const TS_SLOT_INVALID uint 2
27
28note Header field offsets
29const TS_OFF_VERSION ulong 8
30const TS_OFF_SLOT ulong 10
31const TS_OFF_RESERVED ulong 11
32const TS_OFF_RECBYTES ulong 12
33const TS_OFF_SEQ ulong 16
34const TS_OFF_RECDIGEST ulong 24
35const TS_OFF_HDRDIGEST ulong 56
36
37note Region metadata indices (global ts_reg ulong 12)
38const TS_R_BLKSIZE ulong 0
39const TS_R_BLKCNT ulong 1
40const TS_R_BASE_LBA ulong 2
41const TS_R_BPU ulong 3
42const TS_R_KNOWN ulong 4
43const TS_R_NEWEST_SLOT ulong 5
44const TS_R_NEWEST_SEQ ulong 6
45const TS_R_HAS_NEWEST ulong 7
46const TS_R_FAULT_WRITE ulong 8
47const TS_R_FAULT_FLUSH ulong 9
48const TS_R_TEAR_AFTER ulong 10
49const TS_R_WRITE_COUNT ulong 11
50
51text ts_magic "AIENTWS1"
52text ts_rec_dom "AIENOS-TORN-SLOT-RECORD-V1"
53text ts_hdr_dom "AIENOS-TORN-SLOT-HEADER-V1"
54
55global ts_reg ulong 12
56global ts_media uchar 32768
57global ts_scratch_cu uchar 4096
58global ts_scratch_b0 uchar 4096
59global ts_scratch_b1 uchar 4096
60
61note ---- Little-endian helpers ----
62
63fn ts_put16 buf edit uchar * off ulong v ushort to void
64put buf off uchar and v 0xff
65put buf add off 1 uchar and shr v 8 0xff
66end
67
68fn ts_put32 buf edit uchar * off ulong v uint to void
69put buf off uchar and v 0xff
70put buf add off 1 uchar and shr v 8 0xff
71put buf add off 2 uchar and shr v 16 0xff
72put buf add off 3 uchar and shr v 24 0xff
73end
74
75fn ts_put64 buf edit uchar * off ulong v ulong to void
76for i ulong 8 unroll
77put buf add off i uchar and shr v mul i 8 0xff
78end
79end
80
81fn ts_get16 buf view uchar * off ulong to ushort
82b0 = ushort get buf off
83b1 = ushort get buf add off 1
84ret or b0 shl b1 8
85end
86
87fn ts_get32 buf view uchar * off ulong to uint
88b0 = uint get buf off
89b1 = uint get buf add off 1
90b2 = uint get buf add off 2
91b3 = uint get buf add off 3
92ret or or b0 shl b1 8 or shl b2 16 shl b3 24
93end
94
95fn ts_get64 buf view uchar * off ulong to ulong
96v = ulong 0
97for i ulong 8 unroll
98b = ulong get buf add off i
99v = or v shl b mul i 8
100end
101ret v
102end
103
104fn ts_all_zero buf view uchar * off ulong cnt ulong to bool
105acc = uchar 0
106i = ulong 0
107while lt i cnt bound 4096
108acc = or acc get buf add off i
109i = add i 1
110end
111ret eq acc 0
112end
113
114fn ts_digest_eq a view uchar * aoff ulong b view uchar * boff ulong to bool
115d = uchar 0
116for i ulong 32
117d = or d xor get a add aoff i get b add boff i
118end
119ret eq d 0
120end
121
122note ---- Domain-separated SHA-256 digests ----
123
124fn ts_record_digest slot uchar seq ulong rec view uchar 4096 dst edit uchar 32 to void
125ctx = new ulong 40 0
126z1 = new uchar 1 0
127sb = new uchar 9 0
128put sb 0 slot
129ts_put64 sb 1 seq
130sha256_init ctx
131sha256_update ctx ts_rec_dom
132sha256_update ctx z1
133sha256_update ctx sb
134sha256_update ctx rec
135sha256_final ctx dst
136end
137
138fn ts_header_digest hdr view uchar 4096 dst edit uchar 32 to void
139ctx = new ulong 40 0
140z1 = new uchar 1 0
141sha256_init ctx
142sha256_update ctx ts_hdr_dom
143sha256_update ctx z1
144sha256_update ctx part hdr 0 56
145sha256_final ctx dst
146end
147
148fn ts_encode_commit unit edit uchar 4096 slot uchar seq ulong rec view uchar 4096 to void
149for i ulong 4096
150put unit i 0
151end
152for i ulong 8
153put unit i get ts_magic i
154end
155ts_put16 unit TS_OFF_VERSION 1
156put unit TS_OFF_SLOT slot
157put unit TS_OFF_RESERVED 0
158ts_put32 unit TS_OFF_RECBYTES uint TS_RECORD_BYTES
159ts_put64 unit TS_OFF_SEQ seq
160rd = new uchar 32 0
161ts_record_digest slot seq rec rd
162for i ulong 32
163put unit add TS_OFF_RECDIGEST i get rd i
164end
165hd = new uchar 32 0
166ts_header_digest unit hd
167for i ulong 32
168put unit add TS_OFF_HDRDIGEST i get hd i
169end
170end
171
172note ---- Bounded media & fault-injection layer ----
173
174fn ts_media_zero to void
175for i ulong TS_MEDIA_BYTES
176put ts_media i 0
177end
178for i ulong 12
179put ts_reg i 0
180end
181end
182
183fn ts_set_faults fail_write ulong fail_flush ulong tear_after ulong to void
184put ts_reg TS_R_FAULT_WRITE fail_write
185put ts_reg TS_R_FAULT_FLUSH fail_flush
186put ts_reg TS_R_TEAR_AFTER tear_after
187put ts_reg TS_R_WRITE_COUNT 0
188end
189
190fn ts_media_read_unit unit ulong dst edit uchar 4096 to int
191bpu = get ts_reg TS_R_BPU
192bsize = get ts_reg TS_R_BLKSIZE
193lba = add get ts_reg TS_R_BASE_LBA mul unit bpu
194byte_off = mul lba bsize
195if gt add byte_off TS_RECORD_BYTES TS_MEDIA_BYTES
196ret TS_EIO
197end
198for i ulong 4096
199put dst i get ts_media add byte_off i
200end
201ret TS_OK
202end
203
204fn ts_media_write_unit unit ulong src view uchar 4096 to int
205bpu = get ts_reg TS_R_BPU
206bsize = get ts_reg TS_R_BLKSIZE
207lba = add get ts_reg TS_R_BASE_LBA mul unit bpu
208byte_off = mul lba bsize
209if gt add byte_off TS_RECORD_BYTES TS_MEDIA_BYTES
210ret TS_EIO
211end
212fw = get ts_reg TS_R_FAULT_WRITE
213wcnt = add get ts_reg TS_R_WRITE_COUNT 1
214put ts_reg TS_R_WRITE_COUNT wcnt
215if and ne fw 0 eq wcnt fw
216ret TS_EIO
217end
218tear = get ts_reg TS_R_TEAR_AFTER
219if and ne tear 0 le tear TS_RECORD_BYTES
220i = ulong 0
221while lt i tear bound 4096
222put ts_media add byte_off i get src i
223i = add i 1
224end
225ret TS_EIO
226end
227for i ulong 4096
228put ts_media add byte_off i get src i
229end
230ret TS_OK
231end
232
233fn ts_media_flush to int
234if ne get ts_reg TS_R_FAULT_FLUSH 0
235ret TS_EIO
236end
237ret TS_OK
238end
239
240fn ts_media_poke unit ulong off ulong val uchar to void
241bpu = get ts_reg TS_R_BPU
242bsize = get ts_reg TS_R_BLKSIZE
243lba = add get ts_reg TS_R_BASE_LBA mul unit bpu
244byte_off = add mul lba bsize off
245if lt byte_off TS_MEDIA_BYTES
246put ts_media byte_off val
247end
248end
249
250note ---- Public torn_slot API ----
251
252fn ts_open blk_size ulong blk_cnt ulong base_lba ulong to int
253put ts_reg TS_R_KNOWN 0
254put ts_reg TS_R_HAS_NEWEST 0
255put ts_reg TS_R_NEWEST_SLOT 0
256put ts_reg TS_R_NEWEST_SEQ 0
257put ts_reg TS_R_BPU 0
258if and ne blk_size 512 ne blk_size 4096
259ret TS_EGEOMETRY
260end
261bpu = div TS_RECORD_BYTES blk_size
262need = mul TS_UNITS bpu
263if or gt base_lba blk_cnt lt sub blk_cnt base_lba need
264ret TS_EGEOMETRY
265end
266put ts_reg TS_R_BLKSIZE blk_size
267put ts_reg TS_R_BLKCNT blk_cnt
268put ts_reg TS_R_BASE_LBA base_lba
269put ts_reg TS_R_BPU bpu
270ret TS_OK
271end
272
273fn ts_check_slot slot ulong body edit uchar 4096 seq_out edit ulong 1 st_out edit uint 1 to int
274cu = new uchar 4096 0
275d = new uchar 32 0
276e = ts_media_read_unit add mul 2 slot 1 cu
277if ne e TS_OK
278ret e
279end
280put st_out 0 TS_SLOT_INVALID
281if ts_all_zero cu 0 TS_RECORD_BYTES
282put st_out 0 TS_SLOT_BLANK
283ret TS_OK
284end
285for i ulong 8
286if ne get cu i get ts_magic i
287ret TS_OK
288end
289end
290if ne ts_get16 cu TS_OFF_VERSION 1
291ret TS_OK
292end
293if or ne get cu TS_OFF_SLOT uchar slot ne get cu TS_OFF_RESERVED 0
294ret TS_OK
295end
296if ne ts_get32 cu TS_OFF_RECBYTES uint TS_RECORD_BYTES
297ret TS_OK
298end
299if not ts_all_zero cu TS_HEADER_BYTES sub TS_RECORD_BYTES TS_HEADER_BYTES
300ret TS_OK
301end
302ts_header_digest cu d
303if not ts_digest_eq d 0 cu TS_OFF_HDRDIGEST
304ret TS_OK
305end
306s = ts_get64 cu TS_OFF_SEQ
307if eq s 0
308ret TS_OK
309end
310e = ts_media_read_unit mul 2 slot body
311if ne e TS_OK
312ret e
313end
314ts_record_digest uchar slot s body d
315if not ts_digest_eq d 0 cu TS_OFF_RECDIGEST
316ret TS_OK
317end
318put seq_out 0 s
319put st_out 0 TS_SLOT_VALID
320ret TS_OK
321end
322
323fn ts_unfinished_first_commit plaus edit uchar 1 to int
324put plaus 0 0
325cu = new uchar 4096 0
326e = ts_media_read_unit 1 cu
327if ne e TS_OK
328ret e
329end
330t = new uchar 24 0
331for i ulong 8
332put t i get ts_magic i
333end
334ts_put16 t TS_OFF_VERSION 1
335put t TS_OFF_SLOT 0
336put t TS_OFF_RESERVED 0
337ts_put32 t TS_OFF_RECBYTES uint TS_RECORD_BYTES
338ts_put64 t TS_OFF_SEQ 1
339for i ulong 24
340b = get cu i
341if and ne b 0 ne b get t i
342ret TS_OK
343end
344end
345if not ts_all_zero cu TS_HEADER_BYTES sub TS_RECORD_BYTES TS_HEADER_BYTES
346ret TS_OK
347end
348put plaus 0 1
349ret TS_OK
350end
351
352fn ts_check_lineage v0 uint v1 uint q0 ulong q1 ulong to int
353if eq v0 TS_SLOT_VALID
354if eq v1 TS_SLOT_VALID
355if eq q0 q1
356ret TS_CONFLICT
357end
358end
359if ne and q0 1 1
360ret TS_CORRUPT
361end
362end
363if eq v1 TS_SLOT_VALID
364if ne and q1 1 0
365ret TS_CORRUPT
366end
367if eq v0 TS_SLOT_BLANK
368ret TS_CORRUPT
369end
370end
371ret TS_OK
372end
373
374fn ts_pick_slot v0 uint v1 uint q0 ulong q1 ulong to int
375if and eq v0 TS_SLOT_VALID eq v1 TS_SLOT_VALID
376if gt q0 q1
377if ne sub q0 q1 1
378ret -2
379end
380ret 0
381end
382if ne sub q1 q0 1
383ret -2
384end
385ret 1
386end
387if eq v0 TS_SLOT_VALID
388ret 0
389end
390if eq v1 TS_SLOT_VALID
391ret 1
392end
393ret -1
394end
395
396fn ts_recover_empty v1 uint to int
397if ne v1 TS_SLOT_BLANK
398ret TS_CORRUPT
399end
400plaus = new uchar 1 0
401ef = ts_unfinished_first_commit plaus
402if ne ef TS_OK
403ret ef
404end
405if eq get plaus 0 0
406ret TS_CORRUPT
407end
408put ts_reg TS_R_KNOWN 1
409ret TS_EMPTY
410end
411
412fn ts_recover_finish dst edit uchar 4096 src view uchar 4096 slot ulong seq ulong seq_out edit ulong 1 to int
413for i ulong 4096
414put dst i get src i
415end
416put seq_out 0 seq
417put ts_reg TS_R_NEWEST_SLOT slot
418put ts_reg TS_R_NEWEST_SEQ seq
419put ts_reg TS_R_HAS_NEWEST 1
420put ts_reg TS_R_KNOWN 1
421ret TS_OK
422end
423
424fn ts_recover dst edit uchar 4096 seq_out edit ulong 1 slot_states edit uint 2 to int
425put ts_reg TS_R_KNOWN 0
426put ts_reg TS_R_HAS_NEWEST 0
427put ts_reg TS_R_NEWEST_SLOT 0
428put ts_reg TS_R_NEWEST_SEQ 0
429for i ulong 4096
430put dst i 0
431end
432put seq_out 0 0
433if eq get ts_reg TS_R_BPU 0
434ret TS_EGEOMETRY
435end
436b0 = new uchar 4096 0
437b1 = new uchar 4096 0
438sq = new ulong 1 0
439st = new uint 1 0
440e = ts_check_slot 0 b0 sq st
441if ne e TS_OK
442ret e
443end
444v0 = get st 0
445q0 = get sq 0
446e = ts_check_slot 1 b1 sq st
447if ne e TS_OK
448ret e
449end
450v1 = get st 0
451q1 = get sq 0
452put slot_states 0 v0
453put slot_states 1 v1
454
455e = ts_check_lineage v0 v1 q0 q1
456if ne e TS_OK
457ret e
458end
459pick = ts_pick_slot v0 v1 q0 q1
460if eq pick -2
461ret TS_CORRUPT
462end
463if lt pick 0
464ret ts_recover_empty v1
465end
466if eq pick 0
467if and eq v1 TS_SLOT_BLANK ne q0 1
468ret TS_CORRUPT
469end
470ret ts_recover_finish dst b0 0 q0 seq_out
471end
472ret ts_recover_finish dst b1 1 q1 seq_out
473end
474
475fn ts_commit rec view uchar 4096 to int
476if eq get ts_reg TS_R_KNOWN 0
477ret TS_ESTATE
478end
479cur_seq = get ts_reg TS_R_NEWEST_SEQ
480if eq cur_seq TS_U64_MAX
481ret TS_ESTATE
482end
483target = ulong 0
484if ne get ts_reg TS_R_HAS_NEWEST 0
485target = sub 1 get ts_reg TS_R_NEWEST_SLOT
486end
487nxt_seq = add cur_seq 1
488put ts_reg TS_R_KNOWN 0
489e = ts_media_write_unit mul 2 target rec
490if ne e TS_OK
491ret e
492end
493if ne ts_media_flush TS_OK
494ret TS_EIO
495end
496cu = new uchar 4096 0
497ts_encode_commit cu uchar target nxt_seq rec
498e = ts_media_write_unit add mul 2 target 1 cu
499if ne e TS_OK
500ret e
501end
502if ne ts_media_flush TS_OK
503ret TS_EIO
504end
505put ts_reg TS_R_HAS_NEWEST 1
506put ts_reg TS_R_NEWEST_SLOT target
507put ts_reg TS_R_NEWEST_SEQ nxt_seq
508put ts_reg TS_R_KNOWN 1
509ret TS_OK
510end