sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / gpu / cuda-crack / check / mutate.om
| 1 | om2 |
| 2 | note gpu/cuda-crack/check/mutate.om: the four planted bugs of check/check.sh, in pure seeded Omega. |
| 3 | note |
| 4 | note check.sh plants four bugs and requires each to be caught. It makes them with awk, od and |
| 5 | note printf. This program makes the same four inputs, byte for byte; build.sh compares its output |
| 6 | note with those one-liners on every build, on both targets. |
| 7 | note Build with the seed-born compiler: |
| 8 | note omc < check/mutate.om > mutate && chmod +x mutate |
| 9 | note { printf 'target x86linux\n'; cat check/mutate.om; } | omc-x86 > mutate (native x86-64) |
| 10 | note Run: |
| 11 | note mutate MODE < IN > OUT |
| 12 | note MODE 1 IN is isa/sm121-forms.txt. Let K be fields 5..10 of the F record whose field 3 is |
| 13 | note 255 (the NOP). In every F record whose fields 5..10 are K, field 5 (fix0) has its |
| 14 | note bit 0 flipped. If there is not exactly one such NOP record, nothing changes (as the |
| 15 | note awk key then never matches). |
| 16 | note MODE 2 IN is a kernel's machine code; bit 0 of its first byte is flipped. |
| 17 | note MODE 3 IN is a table; the first D record whose field 8 (width) is over 1 gets width - 1. |
| 18 | note MODE 4 IN is a table; in the first D record whose width is over 0, the mask word holding |
| 19 | note bit "start" (field 7) loses that bit: field 4 + start / 32 becomes itself minus |
| 20 | note 2 ^ (start mod 32), printed with a sign when it goes below zero. |
| 21 | note Text modes read lines split at "\n" and fields split at runs of spaces and tabs, as awk's |
| 22 | note default field splitting does. A changed record is written as its fields joined by single |
| 23 | note spaces; every other line is written as it was. Every record ends with "\n" on output, also |
| 24 | note a last line that had none. A field these rules must read as a number is decimal digits |
| 25 | note (1 to 19 of them): a field 3, 7 or 8 of an F or D record that is not, or a changed field |
| 26 | note that is not, is refused. |
| 27 | note Refusals: one stderr line "mutate: ...", exit 1, nothing on stdout. Usage errors exit 2. |
| 28 | note Input and output are each at most 4 MiB. No clock, no environment. |
| 29 | |
| 30 | note standard input and output |
| 31 | global inp uchar 4194304 |
| 32 | global inn ulong 1 |
| 33 | global ob uchar 4194304 |
| 34 | global obn ulong 1 |
| 35 | note the fields of the current line: offset in inp and length; nf is their count, 65 when over 64 |
| 36 | global fo ulong 64 |
| 37 | global fl ulong 64 |
| 38 | global nf ulong 1 |
| 39 | note mode 1: fields 5..10 of the NOP record (offset, length pairs) and how many NOP records there are |
| 40 | global ky ulong 12 |
| 41 | global nky ulong 1 |
| 42 | note the mode, and for modes 3 and 4 whether a D record was changed already |
| 43 | global md ulong 1 |
| 44 | global dn ulong 1 |
| 45 | |
| 46 | text tpre "mutate: " |
| 47 | text tnl "\n" |
| 48 | text tusage "usage: mutate 1|2|3|4 < IN > OUT (see the notes in check/mutate.om)\n" |
| 49 | text tbig "input over the 4 MiB buffer" |
| 50 | text tread "cannot read standard input" |
| 51 | text toverf "output is over the 4 MiB buffer" |
| 52 | text twrite "cannot write standard output" |
| 53 | text tempty "mode 2 needs at least one byte of input" |
| 54 | text tnum "a field that must be a decimal number is not, on the line: " |
| 55 | text tmany "a record to change has more than 64 fields: " |
| 56 | text tnof "the field to change does not exist, on the line: " |
| 57 | text tF "F" |
| 58 | text tD "D" |
| 59 | |
| 60 | note die: report m and up to 200 bytes of inp from o on stderr, then exit 1 |
| 61 | fn die m view uchar * o ulong n ulong to void |
| 62 | k = n |
| 63 | if gt k 200 |
| 64 | k = 200 |
| 65 | end |
| 66 | w = write 2 tpre 0 len tpre |
| 67 | w = write 2 m 0 len m |
| 68 | if gt k 0 |
| 69 | w = write 2 inp o k |
| 70 | end |
| 71 | w = write 2 tnl 0 len tnl |
| 72 | exit 1 |
| 73 | end |
| 74 | |
| 75 | note emb: append one byte to ob |
| 76 | fn emb c uchar to void |
| 77 | p = get obn 0 |
| 78 | if ge p 4194304 |
| 79 | die toverf 0 0 |
| 80 | end |
| 81 | put ob p c |
| 82 | put obn 0 add p 1 |
| 83 | end |
| 84 | |
| 85 | note emi: append n bytes of inp from o to ob |
| 86 | fn emi o ulong n ulong to void |
| 87 | p = get obn 0 |
| 88 | if gt n sub 4194304 p |
| 89 | die toverf 0 0 |
| 90 | end |
| 91 | for i ulong n bound 4194304 |
| 92 | put ob add p i get inp add o i |
| 93 | end |
| 94 | put obn 0 add p n |
| 95 | end |
| 96 | |
| 97 | note emu: append v in decimal to ob |
| 98 | fn emu v ulong to void |
| 99 | d = new uchar 20 0 |
| 100 | x = v |
| 101 | put d 0 uchar add rem x 10 48 |
| 102 | k = ulong 1 |
| 103 | x = div x 10 |
| 104 | while gt x 0 bound 20 |
| 105 | put d k uchar add rem x 10 48 |
| 106 | k = add k 1 |
| 107 | x = div x 10 |
| 108 | end |
| 109 | while gt k 0 bound 20 |
| 110 | k = sub k 1 |
| 111 | emb get d k |
| 112 | end |
| 113 | end |
| 114 | |
| 115 | note wout: write ob to standard output |
| 116 | fn wout to void |
| 117 | n = get obn 0 |
| 118 | p = ulong 0 |
| 119 | while lt p n bound 4194305 |
| 120 | k = write 1 ob p sub n p |
| 121 | if le k 0 |
| 122 | die twrite 0 0 |
| 123 | end |
| 124 | p = add p ulong k |
| 125 | end |
| 126 | end |
| 127 | |
| 128 | note loadin: read all of standard input into inp |
| 129 | fn loadin to void |
| 130 | t = new uchar 1 0 |
| 131 | p = ulong 0 |
| 132 | going = true |
| 133 | note every pass but the last reads at least one byte of a 4 MiB buffer |
| 134 | while going bound 4194306 |
| 135 | k = read 0 inp p sub 4194304 p |
| 136 | if lt k 0 |
| 137 | die tread 0 0 |
| 138 | end |
| 139 | if eq k 0 |
| 140 | going = false |
| 141 | else |
| 142 | p = add p ulong k |
| 143 | if eq p 4194304 |
| 144 | going = false |
| 145 | end |
| 146 | end |
| 147 | end |
| 148 | if eq p 4194304 |
| 149 | k = read 0 t 0 1 |
| 150 | if ne k 0 |
| 151 | die tbig 0 0 |
| 152 | end |
| 153 | end |
| 154 | put inn 0 p |
| 155 | end |
| 156 | |
| 157 | note eol: the end of the line that starts at p (its newline, or the end of the input) |
| 158 | fn eol p ulong to ulong |
| 159 | e = get inn 0 |
| 160 | q = p |
| 161 | while lt q e bound 4194305 |
| 162 | if eq get inp q 10 |
| 163 | ret q |
| 164 | end |
| 165 | q = add q 1 |
| 166 | end |
| 167 | ret e |
| 168 | end |
| 169 | |
| 170 | note isws: is c a field separator (space or tab) |
| 171 | fn isws c uchar to bool |
| 172 | ret or eq c 32 eq c 9 |
| 173 | end |
| 174 | |
| 175 | note split: the fields of the line inp[s, e) into fo, fl and nf |
| 176 | fn split s ulong e ulong to void |
| 177 | n = ulong 0 |
| 178 | p = s |
| 179 | while lt p e bound 4194305 |
| 180 | c = get inp p |
| 181 | if isws c |
| 182 | p = add p 1 |
| 183 | else |
| 184 | q = p |
| 185 | going = true |
| 186 | while going bound 4194305 |
| 187 | q = add q 1 |
| 188 | if ge q e |
| 189 | going = false |
| 190 | else |
| 191 | if isws get inp q |
| 192 | going = false |
| 193 | end |
| 194 | end |
| 195 | end |
| 196 | if lt n 64 |
| 197 | put fo n p |
| 198 | put fl n sub q p |
| 199 | end |
| 200 | n = add n 1 |
| 201 | p = q |
| 202 | end |
| 203 | end |
| 204 | if gt n 64 |
| 205 | n = 65 |
| 206 | end |
| 207 | put nf 0 n |
| 208 | end |
| 209 | |
| 210 | note fhas: does field i (1-based) exist on the current line |
| 211 | fn fhas i ulong to bool |
| 212 | n = get nf 0 |
| 213 | if eq i 0 |
| 214 | ret false |
| 215 | end |
| 216 | if gt i 64 |
| 217 | ret false |
| 218 | end |
| 219 | ret le i n |
| 220 | end |
| 221 | |
| 222 | note fis: is field i exactly the text t |
| 223 | fn fis i ulong t view uchar * to bool |
| 224 | if not fhas i |
| 225 | ret false |
| 226 | end |
| 227 | o = get fo sub i 1 |
| 228 | n = get fl sub i 1 |
| 229 | if ne n len t |
| 230 | ret false |
| 231 | end |
| 232 | for j ulong n bound 64 |
| 233 | if ne get inp add o j get t j |
| 234 | ret false |
| 235 | end |
| 236 | end |
| 237 | ret true |
| 238 | end |
| 239 | |
| 240 | note fdig: is field i 1 to 19 decimal digits |
| 241 | fn fdig i ulong to bool |
| 242 | if not fhas i |
| 243 | ret false |
| 244 | end |
| 245 | o = get fo sub i 1 |
| 246 | n = get fl sub i 1 |
| 247 | if eq n 0 |
| 248 | ret false |
| 249 | end |
| 250 | if gt n 19 |
| 251 | ret false |
| 252 | end |
| 253 | for j ulong n bound 19 |
| 254 | c = get inp add o j |
| 255 | if lt c 48 |
| 256 | ret false |
| 257 | end |
| 258 | if gt c 57 |
| 259 | ret false |
| 260 | end |
| 261 | end |
| 262 | ret true |
| 263 | end |
| 264 | |
| 265 | note fnum: field i as a number; s, e is the line, named in the refusal |
| 266 | fn fnum i ulong s ulong e ulong to ulong |
| 267 | if not fdig i |
| 268 | die tnum s sub e s |
| 269 | end |
| 270 | o = get fo sub i 1 |
| 271 | n = get fl sub i 1 |
| 272 | v = ulong 0 |
| 273 | for j ulong n bound 19 |
| 274 | v = add mul v 10 ulong sub get inp add o j 48 |
| 275 | end |
| 276 | ret v |
| 277 | end |
| 278 | |
| 279 | note fsame: is field i the same bytes as the saved key slice k (0..5) of ky |
| 280 | fn fsame i ulong k ulong to bool |
| 281 | ko = get ky mul k 2 |
| 282 | kn = get ky add mul k 2 1 |
| 283 | if not fhas i |
| 284 | ret eq kn 0 |
| 285 | end |
| 286 | o = get fo sub i 1 |
| 287 | n = get fl sub i 1 |
| 288 | if ne n kn |
| 289 | ret false |
| 290 | end |
| 291 | for j ulong n bound 4194304 |
| 292 | if ne get inp add o j get inp add ko j |
| 293 | ret false |
| 294 | end |
| 295 | end |
| 296 | ret true |
| 297 | end |
| 298 | |
| 299 | note emrec: write the current record's fields joined by single spaces, field ci replaced by the |
| 300 | note value neg/mag (a minus sign first when neg), then "\n"; s, e is the line, for refusals |
| 301 | fn emrec ci ulong ng bool mg ulong s ulong e ulong to void |
| 302 | n = get nf 0 |
| 303 | if gt n 64 |
| 304 | die tmany s sub e s |
| 305 | end |
| 306 | for i ulong n bound 64 |
| 307 | if gt i 0 |
| 308 | emb uchar 32 |
| 309 | end |
| 310 | if eq add i 1 ci |
| 311 | if ng |
| 312 | emb uchar 45 |
| 313 | end |
| 314 | emu mg |
| 315 | else |
| 316 | emi get fo i get fl i |
| 317 | end |
| 318 | end |
| 319 | emb uchar 10 |
| 320 | end |
| 321 | |
| 322 | note keep: write the line s, e as it was, then "\n" |
| 323 | fn keep s ulong e ulong to void |
| 324 | emi s sub e s |
| 325 | emb uchar 10 |
| 326 | end |
| 327 | |
| 328 | note nopscan: mode 1, first pass: count the F records whose field 3 is 255 and keep the first one's |
| 329 | note fields 5..10 |
| 330 | fn nopscan to void |
| 331 | e = get inn 0 |
| 332 | p = ulong 0 |
| 333 | while lt p e bound 4194305 |
| 334 | q = eol p |
| 335 | split p q |
| 336 | if fis 1 tF |
| 337 | if fdig 3 |
| 338 | if eq fnum 3 p q 255 |
| 339 | k = get nky 0 |
| 340 | if eq k 0 |
| 341 | savekey |
| 342 | end |
| 343 | put nky 0 add k 1 |
| 344 | end |
| 345 | end |
| 346 | end |
| 347 | p = add q 1 |
| 348 | end |
| 349 | end |
| 350 | |
| 351 | note savekey: keep fields 5..10 of the current line in ky (a missing field as length 0) |
| 352 | fn savekey to void |
| 353 | for k ulong 6 |
| 354 | i = add k 5 |
| 355 | if fhas i |
| 356 | put ky mul k 2 get fo sub i 1 |
| 357 | put ky add mul k 2 1 get fl sub i 1 |
| 358 | else |
| 359 | put ky mul k 2 0 |
| 360 | put ky add mul k 2 1 0 |
| 361 | end |
| 362 | end |
| 363 | end |
| 364 | |
| 365 | note iskey: are fields 5..10 of the current line the NOP's |
| 366 | fn iskey to bool |
| 367 | for k ulong 6 |
| 368 | if not fsame add k 5 k |
| 369 | ret false |
| 370 | end |
| 371 | end |
| 372 | ret true |
| 373 | end |
| 374 | |
| 375 | note line1: mode 1, one line |
| 376 | fn line1 s ulong e ulong to void |
| 377 | if fis 1 tF |
| 378 | if eq get nky 0 1 |
| 379 | if iskey |
| 380 | v = fnum 5 s e |
| 381 | emrec 5 false xor v 1 s e |
| 382 | ret |
| 383 | end |
| 384 | end |
| 385 | end |
| 386 | keep s e |
| 387 | end |
| 388 | |
| 389 | note line3: mode 3, one line |
| 390 | fn line3 s ulong e ulong to void |
| 391 | if eq get dn 0 0 |
| 392 | if fis 1 tD |
| 393 | if fhas 8 |
| 394 | w = fnum 8 s e |
| 395 | if gt w 1 |
| 396 | put dn 0 1 |
| 397 | emrec 8 false sub w 1 s e |
| 398 | ret |
| 399 | end |
| 400 | end |
| 401 | end |
| 402 | end |
| 403 | keep s e |
| 404 | end |
| 405 | |
| 406 | note line4: mode 4, one line |
| 407 | fn line4 s ulong e ulong to void |
| 408 | if eq get dn 0 0 |
| 409 | if fis 1 tD |
| 410 | if fhas 8 |
| 411 | w = fnum 8 s e |
| 412 | if gt w 0 |
| 413 | put dn 0 1 |
| 414 | st = fnum 7 s e |
| 415 | ci = add 4 div st 32 |
| 416 | if not fhas ci |
| 417 | die tnof s sub e s |
| 418 | end |
| 419 | v = fnum ci s e |
| 420 | b = shl ulong 1 rem st 32 |
| 421 | if ge v b |
| 422 | emrec ci false sub v b s e |
| 423 | else |
| 424 | emrec ci true sub b v s e |
| 425 | end |
| 426 | ret |
| 427 | end |
| 428 | end |
| 429 | end |
| 430 | end |
| 431 | keep s e |
| 432 | end |
| 433 | |
| 434 | note lines: run the mode's line rule over every line of the input |
| 435 | fn lines to void |
| 436 | e = get inn 0 |
| 437 | m = get md 0 |
| 438 | p = ulong 0 |
| 439 | while lt p e bound 4194305 |
| 440 | q = eol p |
| 441 | split p q |
| 442 | if eq m 1 |
| 443 | line1 p q |
| 444 | end |
| 445 | if eq m 3 |
| 446 | line3 p q |
| 447 | end |
| 448 | if eq m 4 |
| 449 | line4 p q |
| 450 | end |
| 451 | p = add q 1 |
| 452 | end |
| 453 | end |
| 454 | |
| 455 | note getmode: the mode from the one argument, 0 when it is not 1, 2, 3 or 4 |
| 456 | fn getmode to ulong |
| 457 | a = new uchar 8 0 |
| 458 | n = argread 1 a 0 8 |
| 459 | if ne n 1 |
| 460 | ret 0 |
| 461 | end |
| 462 | c = get a 0 |
| 463 | if lt c 49 |
| 464 | ret 0 |
| 465 | end |
| 466 | if gt c 52 |
| 467 | ret 0 |
| 468 | end |
| 469 | ret ulong sub c 48 |
| 470 | end |
| 471 | |
| 472 | fn main to int |
| 473 | if ne argcnt 2 |
| 474 | w = write 2 tusage 0 len tusage |
| 475 | ret 2 |
| 476 | end |
| 477 | m = getmode |
| 478 | if eq m 0 |
| 479 | w = write 2 tusage 0 len tusage |
| 480 | ret 2 |
| 481 | end |
| 482 | put md 0 m |
| 483 | loadin |
| 484 | if eq m 2 |
| 485 | n = get inn 0 |
| 486 | if eq n 0 |
| 487 | die tempty 0 0 |
| 488 | end |
| 489 | emi 0 n |
| 490 | put ob 0 xor get ob 0 1 |
| 491 | else |
| 492 | if eq m 1 |
| 493 | nopscan |
| 494 | end |
| 495 | lines |
| 496 | end |
| 497 | wout |
| 498 | ret 0 |
| 499 | end |