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 / seed / trace / segment-test.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note seed/trace/segment-test.om: checks for segment.om (#608). Concatenate after segment.om and compile. |
| 3 | note Runs in the current directory under build/trace-test/. Each check prints "<label> <observed>" and, |
| 4 | note when it differs, " FAIL want=<expected>". The gate compares stdout with expected.txt byte for byte. |
| 5 | note Exit 0 when every check holds, 1 otherwise, 2 when stdout cannot be written. |
| 6 | global checks ulong 1 |
| 7 | global fails ulong 1 |
| 8 | global obuf uchar 256 |
| 9 | global on ulong 1 |
| 10 | global wbad ulong 1 |
| 11 | text labs "crc-123456789\ncrc-empty\nmissing-scan\nmissing-errno\ncreate\ncreate-again-refused\ncreate-again-errno\nappend-100-errors\nappend-last-seq\nscan\nscan-count\nscan-reason\nscan-total\nscan-lost\nappend-oversize-refused\ntruncate-last-record\ntorn-scan\ntorn-count\ntorn-reason\ntorn-lost\ntorn-append-refused\ntorn-size-unchanged\ntorn-recover\ntorn-recover-lost\nrecovered-scan\nrecovered-count\nrecovered-reason\nrecovered-total\nquarantine-size\nappend-after-recover\nappend-after-seq\nrecover-clean\nclean-count\ncrc-flip-poke\ncrc-scan\ncrc-count\ncrc-reason\ncrc-lost\ncrc-recover-bad-exists\ncrc-segment-unchanged\nunlink-bad\ncrc-recover\ncrc-recovered-scan\ncrc-recovered-count\ncrc-recovered-reason\nlen-raw-append\nlen-scan\nlen-count\nlen-reason\nlen-recover\nseq-raw-append\nseq-scan\nseq-count\nseq-reason\nseq-recover\nversion-poke\nversion-scan\nversion-append-refused\nversion-size-unchanged\nmagic-scan\ngeometry-scan\nreserved-scan\nrestored-scan\nrestored-count\nsymlink-create\nsymlink-scan\nsymlink-errno\nsymlink-append-refused\nsymlink-recover-refused\nfinal-scan\nfinal-count\n" |
| 12 | text paths "build/trace-test/segment\nbuild/trace-test/link\n" |
| 13 | text dbuild "build" |
| 14 | text dtest "build/trace-test" |
| 15 | text tgt "segment" |
| 16 | text sfail " FAIL want=" |
| 17 | text sumt "segment-test checks=" |
| 18 | text sumf " fails=" |
| 19 | fn oflush to void |
| 20 | w = write 1 obuf 0 get on 0 |
| 21 | if ne w long get on 0 |
| 22 | put wbad 0 1 |
| 23 | end |
| 24 | put on 0 0 |
| 25 | end |
| 26 | fn obyte b uchar to void |
| 27 | put obuf get on 0 b |
| 28 | put on 0 add get on 0 1 |
| 29 | end |
| 30 | fn onum v ulong to void |
| 31 | d = new uchar 20 0 |
| 32 | x = v |
| 33 | k = ulong 0 |
| 34 | go = true |
| 35 | while go bound 21 |
| 36 | put d k uchar add 48 rem x 10 |
| 37 | k = add k 1 |
| 38 | x = div x 10 |
| 39 | if eq x 0 |
| 40 | go = false |
| 41 | end |
| 42 | end |
| 43 | while gt k 0 bound 21 |
| 44 | k = sub k 1 |
| 45 | obyte get d k |
| 46 | end |
| 47 | end |
| 48 | note label k: emit the k-th newline-terminated line of labs (k from 1). |
| 49 | fn label k ulong to void |
| 50 | line = ulong 1 |
| 51 | for i ulong len labs |
| 52 | b = get labs i |
| 53 | if eq b 10 |
| 54 | line = add line 1 |
| 55 | else |
| 56 | if eq line k |
| 57 | obyte b |
| 58 | end |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | fn check got ulong want ulong to void |
| 63 | put checks 0 add get checks 0 1 |
| 64 | label get checks 0 |
| 65 | obyte 32 |
| 66 | onum got |
| 67 | if ne got want |
| 68 | put fails 0 add get fails 0 1 |
| 69 | for i ulong len sfail |
| 70 | obyte get sfail i |
| 71 | end |
| 72 | onum want |
| 73 | end |
| 74 | obyte 10 |
| 75 | oflush |
| 76 | end |
| 77 | note setp k: the k-th line of paths (k from 1) becomes the segment path. |
| 78 | fn setp k ulong to void |
| 79 | line = ulong 1 |
| 80 | n = ulong 0 |
| 81 | for i ulong len paths |
| 82 | b = get paths i |
| 83 | if eq b 10 |
| 84 | line = add line 1 |
| 85 | else |
| 86 | if eq line k |
| 87 | put tpath n b |
| 88 | n = add n 1 |
| 89 | end |
| 90 | end |
| 91 | end |
| 92 | put tlen 0 n |
| 93 | end |
| 94 | fn errno to ulong |
| 95 | ret ulong neg get serrno 0 |
| 96 | end |
| 97 | fn fsize to ulong |
| 98 | fd = fopen tpath 0 get tlen 0 32768 0 |
| 99 | if lt fd 0 |
| 100 | ret 0 |
| 101 | end |
| 102 | pos = fseek int fd 0 2 |
| 103 | shut fd |
| 104 | if lt pos 0 |
| 105 | ret 0 |
| 106 | end |
| 107 | ret ulong pos |
| 108 | end |
| 109 | fn fsizebad to ulong |
| 110 | sidepath 1 |
| 111 | fd = fopen opath 0 get olen 0 32768 0 |
| 112 | if lt fd 0 |
| 113 | ret 0 |
| 114 | end |
| 115 | pos = fseek int fd 0 2 |
| 116 | shut fd |
| 117 | if lt pos 0 |
| 118 | ret 0 |
| 119 | end |
| 120 | ret ulong pos |
| 121 | end |
| 122 | note poke off v: overwrite one byte of the segment in place (fault injection, not a library path). |
| 123 | fn poke off ulong v ulong to ulong |
| 124 | fd = fopen tpath 0 get tlen 0 32769 0 |
| 125 | if lt fd 0 |
| 126 | ret 1 |
| 127 | end |
| 128 | put trec 0 uchar v |
| 129 | w = pwrite int fd trec 0 1 long off |
| 130 | shut fd |
| 131 | if ne w 1 |
| 132 | ret 1 |
| 133 | end |
| 134 | ret 0 |
| 135 | end |
| 136 | note rawapp: append trec as-is, bypassing segappend (fault injection for crafted records). |
| 137 | fn rawapp to ulong |
| 138 | fd = fopen tpath 0 get tlen 0 33793 0 |
| 139 | if lt fd 0 |
| 140 | ret 1 |
| 141 | end |
| 142 | w = write int fd trec 0 64 |
| 143 | shut fd |
| 144 | if ne w 64 |
| 145 | ret 1 |
| 146 | end |
| 147 | ret 0 |
| 148 | end |
| 149 | fn unlbad to ulong |
| 150 | sidepath 1 |
| 151 | u = unlink opath 0 get olen 0 |
| 152 | if lt u 0 |
| 153 | ret 1 |
| 154 | end |
| 155 | ret 0 |
| 156 | end |
| 157 | fn mklink to ulong |
| 158 | setp 2 |
| 159 | put tn 0 0 |
| 160 | tput16 2 |
| 161 | tput16 len tgt |
| 162 | for i ulong len tgt |
| 163 | put tpkt add get tn 0 i get tgt i |
| 164 | end |
| 165 | put tn 0 add get tn 0 len tgt |
| 166 | pkttop |
| 167 | s = symlnk tpkt 0 get tn 0 |
| 168 | setp 1 |
| 169 | if lt s 0 |
| 170 | ret 1 |
| 171 | end |
| 172 | ret 0 |
| 173 | end |
| 174 | fn cleanup to void |
| 175 | setp 2 |
| 176 | u1 = unlink tpath 0 get tlen 0 |
| 177 | setp 1 |
| 178 | u2 = unlink tpath 0 get tlen 0 |
| 179 | sidepath 1 |
| 180 | u3 = unlink opath 0 get olen 0 |
| 181 | sidepath 2 |
| 182 | u4 = unlink opath 0 get olen 0 |
| 183 | end |
| 184 | fn crcvec to void |
| 185 | for i ulong 9 |
| 186 | put trec i uchar add 49 i |
| 187 | end |
| 188 | check segcrc 9 3421780262 |
| 189 | check segcrc 0 0 |
| 190 | end |
| 191 | fn fill100 to void |
| 192 | bad = ulong 0 |
| 193 | for i ulong 100 |
| 194 | put pay 0 uchar and i 255 |
| 195 | put pay 1 uchar and shr i 8 255 |
| 196 | put pay 2 0 |
| 197 | put pay 3 0 |
| 198 | if ne segappend 1 4 0 |
| 199 | bad = add bad 1 |
| 200 | end |
| 201 | end |
| 202 | check bad 0 |
| 203 | check get sseq 0 99 |
| 204 | end |
| 205 | fn scan5 c ulong r ulong t ulong l ulong to void |
| 206 | check segscan 0 |
| 207 | check get scount 0 c |
| 208 | check get sreason 0 r |
| 209 | check get stotal 0 t |
| 210 | check get slost 0 l |
| 211 | end |
| 212 | fn phase1 to void |
| 213 | check segscan 10 |
| 214 | check errno 2 |
| 215 | check segcreate 0 |
| 216 | check segcreate 10 |
| 217 | check errno 17 |
| 218 | fill100 |
| 219 | scan5 100 0 6416 0 |
| 220 | check segappend 1 53 20 |
| 221 | check ulong truncate tpath 0 get tlen 0 6382 0 |
| 222 | check segscan 0 |
| 223 | check get scount 0 99 |
| 224 | check get sreason 0 1 |
| 225 | check get slost 0 30 |
| 226 | check segappend 1 4 25 |
| 227 | check fsize 6382 |
| 228 | check segrecover 1 |
| 229 | check get slost 0 30 |
| 230 | check segscan 0 |
| 231 | check get scount 0 99 |
| 232 | check get sreason 0 0 |
| 233 | check get stotal 0 6352 |
| 234 | check fsizebad 6382 |
| 235 | check segappend 1 4 0 |
| 236 | check get sseq 0 99 |
| 237 | check segrecover 0 |
| 238 | e = segscan |
| 239 | end |
| 240 | fn phase2 to void |
| 241 | check get scount 0 100 |
| 242 | check poke 3224 51 0 |
| 243 | check segscan 0 |
| 244 | check get scount 0 50 |
| 245 | check get sreason 0 3 |
| 246 | check get slost 0 3200 |
| 247 | check segrecover 32 |
| 248 | check fsize 6416 |
| 249 | check unlbad 0 |
| 250 | check segrecover 1 |
| 251 | check segscan 0 |
| 252 | check get scount 0 50 |
| 253 | check get sreason 0 0 |
| 254 | end |
| 255 | fn phase3 to void |
| 256 | segpack 1 4 50 |
| 257 | put trec 2 60 |
| 258 | segseal |
| 259 | check rawapp 0 |
| 260 | check segscan 0 |
| 261 | check get scount 0 50 |
| 262 | check get sreason 0 2 |
| 263 | e = unlbad |
| 264 | check segrecover 1 |
| 265 | segpack 1 4 7 |
| 266 | check rawapp 0 |
| 267 | check segscan 0 |
| 268 | check get scount 0 50 |
| 269 | check get sreason 0 4 |
| 270 | e2 = unlbad |
| 271 | check segrecover 1 |
| 272 | end |
| 273 | fn phase4 to void |
| 274 | check poke 4 2 0 |
| 275 | check segscan 15 |
| 276 | check segappend 1 4 15 |
| 277 | check fsize 3216 |
| 278 | p1 = poke 4 1 |
| 279 | p2 = poke 0 0 |
| 280 | check segscan 11 |
| 281 | p3 = poke 0 65 |
| 282 | p4 = poke 6 32 |
| 283 | check segscan 16 |
| 284 | p5 = poke 6 64 |
| 285 | p6 = poke 12 1 |
| 286 | check segscan 16 |
| 287 | p7 = poke 12 0 |
| 288 | check segscan 0 |
| 289 | check get scount 0 50 |
| 290 | end |
| 291 | fn phase5 to void |
| 292 | check mklink 0 |
| 293 | setp 2 |
| 294 | check segscan 10 |
| 295 | check errno 40 |
| 296 | check segappend 1 4 10 |
| 297 | check segrecover 10 |
| 298 | setp 1 |
| 299 | check segscan 0 |
| 300 | check get scount 0 50 |
| 301 | end |
| 302 | fn summary to void |
| 303 | for i ulong len sumt |
| 304 | obyte get sumt i |
| 305 | end |
| 306 | onum get checks 0 |
| 307 | for i ulong len sumf |
| 308 | obyte get sumf i |
| 309 | end |
| 310 | onum get fails 0 |
| 311 | obyte 10 |
| 312 | oflush |
| 313 | end |
| 314 | fn main to int |
| 315 | m1 = makedir dbuild 0 len dbuild 448 |
| 316 | m2 = makedir dtest 0 len dtest 448 |
| 317 | cleanup |
| 318 | setp 1 |
| 319 | crcvec |
| 320 | phase1 |
| 321 | phase2 |
| 322 | phase3 |
| 323 | phase4 |
| 324 | phase5 |
| 325 | summary |
| 326 | if ne get wbad 0 0 |
| 327 | ret 2 |
| 328 | end |
| 329 | if ne get fails 0 0 |
| 330 | ret 1 |
| 331 | end |
| 332 | ret 0 |
| 333 | end |