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 / omc / tests / pools / mk.py
| 1 | #!/usr/bin/env python3 |
| 2 | # mk.py OUTDIR -- the test-data generator of seed/omc/tests/pools (run by seed/omc/gate-pools.sh, which |
| 3 | # checks that `python3 seed/omc/tests/pools/mk.py DIR` gives the committed .om and .want files byte for |
| 4 | # byte). Writes every hand-written case and its expected output, computed here; fuzz.py writes the rest. |
| 5 | import os, sys |
| 6 | |
| 7 | OUT = sys.argv[1] |
| 8 | os.makedirs(OUT, exist_ok=True) |
| 9 | |
| 10 | PNUM = """fn pnum v ulong to void |
| 11 | put ob 23 10 |
| 12 | i = ulong 23 |
| 13 | x = v |
| 14 | go = true |
| 15 | while go bound 21 |
| 16 | i = sub i 1 |
| 17 | put ob i uchar add rem x 10 48 |
| 18 | x = div x 10 |
| 19 | go = ne x 0 |
| 20 | end |
| 21 | k = write 1 ob i sub 24 i |
| 22 | end |
| 23 | fn plong v long to void |
| 24 | if lt v 0 |
| 25 | put ob 0 uchar 45 |
| 26 | k = write 1 ob 0 1 |
| 27 | pnum ulong neg v |
| 28 | else |
| 29 | pnum ulong v |
| 30 | end |
| 31 | end |
| 32 | """ |
| 33 | |
| 34 | RSS = """fn hit i ulong to bool |
| 35 | a = and eq get sb i 86 eq get sb add i 1 109 |
| 36 | b = and eq get sb add i 2 82 eq get sb add i 5 58 |
| 37 | ret and a b |
| 38 | end |
| 39 | fn rss to ulong |
| 40 | fd = fopen pth 0 len pth 0 0 |
| 41 | if lt fd 0 |
| 42 | ret 0 |
| 43 | end |
| 44 | n = read int fd sb 0 4096 |
| 45 | fd = fclose int fd |
| 46 | if le n 0 |
| 47 | ret 0 |
| 48 | end |
| 49 | lim = ulong n |
| 50 | i = ulong 0 |
| 51 | while lt add i 6 lim bound 4096 |
| 52 | if hit i |
| 53 | break |
| 54 | end |
| 55 | i = add i 1 |
| 56 | end |
| 57 | i = add i 6 |
| 58 | v = ulong 0 |
| 59 | while lt i lim bound 4096 |
| 60 | ch = get sb i |
| 61 | if and ge ch 48 le ch 57 |
| 62 | v = add mul v 10 ulong sub ch 48 |
| 63 | else |
| 64 | if and ne ch 32 ne ch 9 |
| 65 | break |
| 66 | end |
| 67 | end |
| 68 | i = add i 1 |
| 69 | end |
| 70 | ret v |
| 71 | end |
| 72 | """ |
| 73 | |
| 74 | |
| 75 | WROTE = [] |
| 76 | |
| 77 | |
| 78 | def put(name, src, want=None): |
| 79 | with open(os.path.join(OUT, name + ".om"), "w") as f: |
| 80 | f.write(src) |
| 81 | WROTE.append(name + ".om") |
| 82 | if want is not None: |
| 83 | with open(os.path.join(OUT, name + ".want"), "w") as f: |
| 84 | f.write(want) |
| 85 | WROTE.append(name + ".want") |
| 86 | |
| 87 | |
| 88 | def W(lines, status): |
| 89 | return "".join(str(x) + "\n" for x in lines) + "status %d\n" % status |
| 90 | |
| 91 | |
| 92 | # ---------------------------------------------------------------- gate 1: growth and contents |
| 93 | g1 = """note gate-pools 1: growth and contents. P grows 0 -> 1 -> 2 -> 512 -> 513 -> 4096 (then 10 and 4096, |
| 94 | note which change nothing) -> 2^20 -> 2^27 elements; Q (uchar) grows past 16777216 elements, then to 2^27. |
| 95 | note Old values are kept and new elements are 0 at every step. |
| 96 | global P ulong * |
| 97 | global Q uchar * |
| 98 | global ob uchar 24 |
| 99 | """ + PNUM + """fn psum a ulong b ulong to ulong |
| 100 | s = ulong 0 |
| 101 | i = a |
| 102 | while lt i b bound 200000000 |
| 103 | s = add s get P i |
| 104 | i = add i 1 |
| 105 | end |
| 106 | ret s |
| 107 | end |
| 108 | fn qsum a ulong b ulong to ulong |
| 109 | s = ulong 0 |
| 110 | i = a |
| 111 | while lt i b bound 200000000 |
| 112 | s = add s ulong get Q i |
| 113 | i = add i 1 |
| 114 | end |
| 115 | ret s |
| 116 | end |
| 117 | fn main to int |
| 118 | pnum len P |
| 119 | r = enlarge P ulong 1 |
| 120 | plong r |
| 121 | pnum len P |
| 122 | pnum get P 0 |
| 123 | put P 0 7 |
| 124 | r = enlarge P ulong 2 |
| 125 | plong r |
| 126 | pnum len P |
| 127 | pnum get P 0 |
| 128 | pnum get P 1 |
| 129 | put P 1 8 |
| 130 | r = enlarge P ulong 512 |
| 131 | plong r |
| 132 | pnum len P |
| 133 | pnum psum 2 512 |
| 134 | r = enlarge P ulong 513 |
| 135 | plong r |
| 136 | pnum len P |
| 137 | pnum get P 0 |
| 138 | pnum get P 1 |
| 139 | pnum get P 512 |
| 140 | r = enlarge P ulong 4096 |
| 141 | plong r |
| 142 | pnum len P |
| 143 | pnum psum 2 4096 |
| 144 | i = ulong 0 |
| 145 | while lt i 4096 |
| 146 | put P i add i 1 |
| 147 | i = add i 1 |
| 148 | end |
| 149 | r = enlarge P ulong 10 |
| 150 | plong r |
| 151 | pnum len P |
| 152 | pnum psum 0 4096 |
| 153 | r = enlarge P ulong 4096 |
| 154 | plong r |
| 155 | pnum len P |
| 156 | r = enlarge P ulong 1048576 |
| 157 | plong r |
| 158 | pnum len P |
| 159 | pnum psum 0 4096 |
| 160 | pnum psum 4096 1048576 |
| 161 | i = ulong 4096 |
| 162 | while lt i 1048576 bound 2000000 |
| 163 | put P i i |
| 164 | i = add i 1 |
| 165 | end |
| 166 | pnum psum 0 1048576 |
| 167 | r = enlarge P ulong 134217728 |
| 168 | plong r |
| 169 | pnum len P |
| 170 | pnum psum 0 1048576 |
| 171 | s = ulong 0 |
| 172 | i = ulong 1048576 |
| 173 | while lt i 134217728 bound 200000 |
| 174 | s = add s get P i |
| 175 | i = add i 4096 |
| 176 | end |
| 177 | pnum s |
| 178 | put P 134217727 5 |
| 179 | pnum get P 134217727 |
| 180 | pnum get P 1048575 |
| 181 | r = enlarge Q ulong 16777217 |
| 182 | plong r |
| 183 | pnum len Q |
| 184 | i = ulong 0 |
| 185 | while lt i 16777217 bound 20000000 |
| 186 | put Q i uchar and i 255 |
| 187 | i = add i 1 |
| 188 | end |
| 189 | pnum qsum 0 16777217 |
| 190 | pnum ulong get Q 16777216 |
| 191 | r = enlarge Q ulong 134217728 |
| 192 | plong r |
| 193 | pnum len Q |
| 194 | pnum qsum 0 16777217 |
| 195 | pnum qsum 16777217 134217728 |
| 196 | ret 0 |
| 197 | end |
| 198 | """ |
| 199 | o = [] |
| 200 | P = [] |
| 201 | o.append(0) |
| 202 | P = [0] * 1; o += [0, 1, 0]; P[0] = 7 |
| 203 | P += [0]; o += [0, 2, 7, 0]; P[1] = 8 |
| 204 | P += [0] * 510; o += [0, 512, sum(P[2:512])] |
| 205 | P += [0]; o += [0, 513, 7, 8, 0] |
| 206 | P += [0] * (4096 - 513); o += [0, 4096, sum(P[2:4096])] |
| 207 | for i in range(4096): |
| 208 | P[i] = i + 1 |
| 209 | o += [0, 4096, sum(P[0:4096])] |
| 210 | o += [0, 4096] |
| 211 | o += [0, 1048576, sum(P[0:4096]), 0] |
| 212 | M = 2**64 |
| 213 | fill = sum(range(4096, 1048576)) |
| 214 | tot = (sum(P[0:4096]) + fill) % M |
| 215 | o += [tot] |
| 216 | o += [0, 134217728, tot, 0, 5, 1048575] |
| 217 | qs = sum(i & 255 for i in range(16777217)) |
| 218 | o += [0, 16777217, qs, 16777216 & 255] |
| 219 | o += [0, 134217728, qs, 0] |
| 220 | put("g1", g1, W(o, 0)) |
| 221 | |
| 222 | gf = """note gate-pools 1: float pools. F (f64) and G (f32) grow by 4096 elements 40 times, a blocker pool B above |
| 223 | note them forcing moves; each value written is read back after every grow. Prints 2*F[k] and 4*G[k] (as |
| 224 | note ulong), their sums, both lengths, the last elements (0), then G at its length traps 3. |
| 225 | global B uchar * |
| 226 | global F f64 * |
| 227 | global G f32 * |
| 228 | global ob uchar 24 |
| 229 | """ + PNUM + """fn main to int |
| 230 | r = enlarge B ulong 1 |
| 231 | r = enlarge F ulong 1 |
| 232 | r = enlarge G ulong 1 |
| 233 | pnum ulong get F 0 |
| 234 | k = ulong 0 |
| 235 | while lt k 40 |
| 236 | r = enlarge B add len B 4096 |
| 237 | r = enlarge F add len F 4096 |
| 238 | r = enlarge G add len G 4096 |
| 239 | put F k add f64 k 0.5f64 |
| 240 | put G k add f32 k 0.25f32 |
| 241 | pnum ulong mul get F k 2.0f64 |
| 242 | pnum ulong mul get G k 4.0f32 |
| 243 | k = add k 1 |
| 244 | end |
| 245 | s = ulong 0 |
| 246 | t = ulong 0 |
| 247 | k = ulong 0 |
| 248 | while lt k 40 |
| 249 | s = add s ulong mul get F k 2.0f64 |
| 250 | t = add t ulong mul get G k 4.0f32 |
| 251 | k = add k 1 |
| 252 | end |
| 253 | pnum s |
| 254 | pnum t |
| 255 | pnum len F |
| 256 | pnum len G |
| 257 | pnum ulong get F sub len F 1 |
| 258 | pnum ulong get G 40 |
| 259 | n = len G |
| 260 | pnum ulong get G n |
| 261 | ret 0 |
| 262 | end |
| 263 | """ |
| 264 | o = [0] |
| 265 | for k in range(40): |
| 266 | o += [2 * k + 1, 4 * k + 1] |
| 267 | o += [sum(2 * k + 1 for k in range(40)), sum(4 * k + 1 for k in range(40)), 1 + 40 * 4096, 1 + 40 * 4096, 0, 0] |
| 268 | put("gf", gf, W(o, 103)) |
| 269 | |
| 270 | # ---------------------------------------------------------------- gate 2: range |
| 271 | g2a = """note gate-pools 2: an index equal to the length (a variable) traps 3 before any store; len + 1 is in range |
| 272 | note after a grow. With the compare removed the put lands and the next lines print 9. |
| 273 | global P ulong * |
| 274 | global ob uchar 24 |
| 275 | """ + PNUM + """fn main to int |
| 276 | r = enlarge P ulong 10 |
| 277 | put P 9 1 |
| 278 | pnum get P 9 |
| 279 | r = enlarge P ulong 11 |
| 280 | put P 10 5 |
| 281 | pnum get P 10 |
| 282 | i = len P |
| 283 | put P i 9 |
| 284 | r = enlarge P ulong 12 |
| 285 | pnum get P 11 |
| 286 | ret 0 |
| 287 | end |
| 288 | """ |
| 289 | put("g2a", g2a, W([1, 5], 103)) |
| 290 | g2b = """note gate-pools 2: a number index past the length compiles (no error 21) and traps 3 at run time (put). |
| 291 | global P ulong * |
| 292 | global ob uchar 24 |
| 293 | """ + PNUM + """fn main to int |
| 294 | r = enlarge P ulong 4 |
| 295 | put P 3 6 |
| 296 | pnum get P 3 |
| 297 | put P 100 1 |
| 298 | pnum get P 100 |
| 299 | ret 0 |
| 300 | end |
| 301 | """ |
| 302 | put("g2b", g2b, W([6], 103)) |
| 303 | g2c = """note gate-pools 2: a number index equal to the length traps 3 at run time (get), also when length 0. |
| 304 | global P ulong * |
| 305 | global Q ulong * |
| 306 | global ob uchar 24 |
| 307 | """ + PNUM + """fn main to int |
| 308 | r = enlarge P ulong 4 |
| 309 | pnum get P 3 |
| 310 | pnum len Q |
| 311 | pnum get P 4 |
| 312 | ret 0 |
| 313 | end |
| 314 | """ |
| 315 | put("g2c", g2c, W([0, 0], 103)) |
| 316 | g2d = """note gate-pools 2: a read or write slice past the length traps 3 (uchar pool, write path). |
| 317 | global U uchar * |
| 318 | global ob uchar 24 |
| 319 | """ + PNUM + """fn main to int |
| 320 | r = enlarge U ulong 3 |
| 321 | put U 0 79 |
| 322 | put U 1 75 |
| 323 | put U 2 10 |
| 324 | k = write 1 U 0 3 |
| 325 | n = len U |
| 326 | k = write 1 U 1 n |
| 327 | pnum 9 |
| 328 | ret 0 |
| 329 | end |
| 330 | """ |
| 331 | put("g2d", g2d, "OK\nstatus 103\n") |
| 332 | |
| 333 | # ---------------------------------------------------------------- gate 3: stale base |
| 334 | ROUNDS = 40 |
| 335 | STDIN = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN" |
| 336 | |
| 337 | |
| 338 | def g3src(inline): |
| 339 | il = " inline" if inline else "" |
| 340 | head = "om2\n" if inline else "" |
| 341 | return head + """note gate-pools 3: enlarge runs inside the value of put, the index of get, and the counts of read and |
| 342 | note write; every call grows the pool it serves by 4096 elements and a blocker pool beside it, so mremap |
| 343 | note moves the regions and later mappings reuse the old addresses (the gate counts both with strace). |
| 344 | note stdin: the 40 bytes abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN.%s |
| 345 | global P ulong * |
| 346 | global Q ulong * |
| 347 | global U uchar * |
| 348 | global V uchar * |
| 349 | global ob uchar 24 |
| 350 | """ % (" Every grower is an OM-2 inline body." if inline else "") + PNUM + """fn gv v ulong to ulong%s |
| 351 | r = enlarge P add len P 4096 |
| 352 | r = enlarge Q add len Q 4096 |
| 353 | ret v |
| 354 | end |
| 355 | fn gi j ulong to ulong%s |
| 356 | r = enlarge P add len P 4096 |
| 357 | r = enlarge Q add len Q 4096 |
| 358 | ret j |
| 359 | end |
| 360 | fn gu n ulong to ulong%s |
| 361 | r = enlarge U add len U 4096 |
| 362 | r = enlarge V add len V 4096 |
| 363 | ret n |
| 364 | end |
| 365 | fn main to int |
| 366 | r = enlarge Q ulong 1 |
| 367 | r = enlarge P ulong 1 |
| 368 | r = enlarge V ulong 1 |
| 369 | r = enlarge U ulong 64 |
| 370 | j = ulong 0 |
| 371 | while lt j %d |
| 372 | put P j gv add mul j 3 1 |
| 373 | x = get P gi j |
| 374 | pnum x |
| 375 | k = read 0 U j gu 1 |
| 376 | k = write 1 U j gu 1 |
| 377 | pnum ulong k |
| 378 | j = add j 1 |
| 379 | end |
| 380 | k = write 1 U 0 %d |
| 381 | s = ulong 0 |
| 382 | j = ulong 0 |
| 383 | while lt j %d |
| 384 | s = add s get P j |
| 385 | j = add j 1 |
| 386 | end |
| 387 | pnum s |
| 388 | pnum len P |
| 389 | pnum len U |
| 390 | ret 0 |
| 391 | end |
| 392 | """ % (il, il, il, ROUNDS, ROUNDS, ROUNDS) |
| 393 | |
| 394 | |
| 395 | o3 = [] |
| 396 | for j in range(ROUNDS): |
| 397 | o3.append(3 * j + 1) |
| 398 | o3.append(STDIN[j] + "1") |
| 399 | last = STDIN + str(sum(3 * j + 1 for j in range(ROUNDS))) |
| 400 | o3.append(last) |
| 401 | o3.append(1 + 2 * ROUNDS * 4096) |
| 402 | o3.append(64 + 2 * ROUNDS * 4096) |
| 403 | put("g3", g3src(False), W(o3, 0)) |
| 404 | put("g3i", g3src(True), W(o3, 0)) |
| 405 | |
| 406 | # ---------------------------------------------------------------- gate 4: pinning |
| 407 | g4 = """om2 |
| 408 | note gate-pools 4: the first global is a pool, sized globals follow. The pool takes no pinned register: the |
| 409 | note set of X18..X26 written by this unit equals that of g4base.om (the same unit without the pool). |
| 410 | global P ulong * |
| 411 | global g ulong 10 |
| 412 | global h ulong 5000 |
| 413 | global ob uchar 24 |
| 414 | """ + PNUM + """fn main to int |
| 415 | r = enlarge P ulong 1000 |
| 416 | put P 999 5 |
| 417 | put g 9 6 |
| 418 | put h 4999 7 |
| 419 | r = enlarge P ulong 1000000 |
| 420 | put P 999999 8 |
| 421 | pnum get P 999 |
| 422 | pnum get g 9 |
| 423 | pnum get h 4999 |
| 424 | pnum get P 999999 |
| 425 | pnum len P |
| 426 | ret 0 |
| 427 | end |
| 428 | """ |
| 429 | put("g4", g4, W([5, 6, 7, 8, 1000000], 0)) |
| 430 | g4base = """om2 |
| 431 | note gate-pools 4 baseline: g4.om without the pool (compiled only, for the pinned register set). |
| 432 | global g ulong 10 |
| 433 | global h ulong 5000 |
| 434 | global ob uchar 24 |
| 435 | """ + PNUM + """fn main to int |
| 436 | put g 9 6 |
| 437 | put h 4999 7 |
| 438 | pnum get g 9 |
| 439 | pnum get h 4999 |
| 440 | ret 0 |
| 441 | end |
| 442 | """ |
| 443 | put("g4base", g4base, W([6, 7], 0)) |
| 444 | |
| 445 | # ---------------------------------------------------------------- gate 5: contracts |
| 446 | g5a = """om2 diag |
| 447 | note gate-pools 5: requires eq len P 0 is kept (rule=runtime) and holds before the grow, fails after it. |
| 448 | global P ulong * |
| 449 | global ob uchar 24 |
| 450 | """ + PNUM + """fn f k ulong to ulong |
| 451 | requires eq len P 0 |
| 452 | ret k |
| 453 | end |
| 454 | fn main to int |
| 455 | x = f 1 |
| 456 | pnum x |
| 457 | r = enlarge P ulong 8 |
| 458 | x = f 2 |
| 459 | pnum x |
| 460 | ret 0 |
| 461 | end |
| 462 | """ |
| 463 | put("g5a", g5a, W([1], 109)) |
| 464 | g5b = """om2 diag |
| 465 | note gate-pools 5: invariant le i len P is kept (rule=runtime): true for i 0..8, false at 9. |
| 466 | global P ulong * |
| 467 | global ob uchar 24 |
| 468 | """ + PNUM + """fn main to int |
| 469 | r = enlarge P ulong 8 |
| 470 | i = ulong 0 |
| 471 | while lt i 12 |
| 472 | invariant le i len P |
| 473 | pnum i |
| 474 | i = add i 1 |
| 475 | end |
| 476 | ret 0 |
| 477 | end |
| 478 | """ |
| 479 | put("g5b", g5b, W(list(range(9)), 110)) |
| 480 | g5c = """om2 |
| 481 | note gate-pools 5: a constant index under unroll keeps its run-time check (length 8, copies 0..11). |
| 482 | global P ulong * |
| 483 | global ob uchar 24 |
| 484 | """ + PNUM + """fn main to int |
| 485 | r = enlarge P ulong 8 |
| 486 | for j ulong 12 unroll |
| 487 | put P j add j 1 |
| 488 | pnum get P j |
| 489 | end |
| 490 | ret 0 |
| 491 | end |
| 492 | """ |
| 493 | put("g5c", g5c, W(list(range(1, 9)), 103)) |
| 494 | g5d = """om2 |
| 495 | note gate-pools 5: a constant index in an inline body keeps its run-time check (7 in range, 9 not). |
| 496 | global P ulong * |
| 497 | global ob uchar 24 |
| 498 | """ + PNUM + """fn g7 to ulong inline |
| 499 | ret get P 7 |
| 500 | end |
| 501 | fn g9 to ulong inline |
| 502 | ret get P 9 |
| 503 | end |
| 504 | fn main to int |
| 505 | r = enlarge P ulong 8 |
| 506 | put P 7 4 |
| 507 | pnum g7 |
| 508 | pnum g9 |
| 509 | ret 0 |
| 510 | end |
| 511 | """ |
| 512 | put("g5d", g5d, W([4], 103)) |
| 513 | g5ctl = """om2 diag |
| 514 | note gate-pools 5 control: len of a sized global folds, so the same clause is erased rule=constant-true. |
| 515 | global G ulong 8 |
| 516 | fn f k ulong to ulong |
| 517 | requires eq len G 8 |
| 518 | ret k |
| 519 | end |
| 520 | fn main to int |
| 521 | x = f 1 |
| 522 | ret int x |
| 523 | end |
| 524 | """ |
| 525 | put("g5ctl", g5ctl, W([], 1)) |
| 526 | |
| 527 | # ---------------------------------------------------------------- gate 8: host refusal |
| 528 | g8a = """note gate-pools 8: run under prlimit --as=268435456: an 8 GiB enlarge returns -12, the pool is unchanged, |
| 529 | note the program goes on and ends with its own status 7. Without the limit the first 8 GiB enlarge gives 0. |
| 530 | global P ulong * |
| 531 | global Q ulong * |
| 532 | global ob uchar 24 |
| 533 | """ + PNUM + """fn main to int |
| 534 | r = enlarge P ulong 1000 |
| 535 | plong r |
| 536 | put P 5 42 |
| 537 | r = enlarge P ulong 1073741824 |
| 538 | plong r |
| 539 | pnum len P |
| 540 | pnum get P 5 |
| 541 | r = enlarge Q ulong 1073741824 |
| 542 | plong r |
| 543 | pnum len Q |
| 544 | r = enlarge P ulong 2000 |
| 545 | plong r |
| 546 | pnum len P |
| 547 | pnum get P 5 |
| 548 | ret 7 |
| 549 | end |
| 550 | """ |
| 551 | put("g8a", g8a, W([0, -12, 1000, 42, -12, 0, 0, 2000, 42], 7)) |
| 552 | g8b = """note gate-pools 8: n times the element size at or past 2^63 is -22 with nothing changed; just below it the |
| 553 | note host refuses (-12). |
| 554 | global P ulong * |
| 555 | global U uchar * |
| 556 | global ob uchar 24 |
| 557 | """ + PNUM + """fn main to int |
| 558 | r = enlarge P ulong 1152921504606846976 |
| 559 | plong r |
| 560 | pnum len P |
| 561 | r = enlarge P ulong 18446744073709551615 |
| 562 | plong r |
| 563 | r = enlarge P ulong 1152921504606846975 |
| 564 | plong r |
| 565 | pnum len P |
| 566 | r = enlarge U ulong 9223372036854775808 |
| 567 | plong r |
| 568 | r = enlarge U ulong 9223372036854775807 |
| 569 | plong r |
| 570 | pnum len U |
| 571 | r = enlarge P ulong 3 |
| 572 | plong r |
| 573 | put P 2 11 |
| 574 | r = enlarge P ulong 2305843009213693952 |
| 575 | plong r |
| 576 | pnum len P |
| 577 | pnum get P 2 |
| 578 | ret 5 |
| 579 | end |
| 580 | """ |
| 581 | put("g8b", g8b, W([-22, 0, -22, -12, 0, -22, -12, 0, 0, -22, 3, 11], 5)) |
| 582 | g8c = """note gate-pools 8 (recorded): touch 128 MiB of a pool, one byte per page. Under |
| 583 | note systemd-run --user --scope -p MemoryMax=64M the expected result is the kernel's OOM kill (137). |
| 584 | global U uchar * |
| 585 | fn main to int |
| 586 | r = enlarge U ulong 134217728 |
| 587 | if ne r 0 |
| 588 | ret 3 |
| 589 | end |
| 590 | i = ulong 0 |
| 591 | while lt i 134217728 bound 40000 |
| 592 | put U i 1 |
| 593 | i = add i 4096 |
| 594 | end |
| 595 | ret 0 |
| 596 | end |
| 597 | """ |
| 598 | put("g8c", g8c) |
| 599 | |
| 600 | # ---------------------------------------------------------------- gate 9: untouched pages |
| 601 | g9 = """note gate-pools 9: a 2 GiB pool with one page touched adds about one page of VmRSS. Prints the length, |
| 602 | note then VmRSS (kB) before the grow, after touching one page, and after touching 1024 more (the control). |
| 603 | global P uchar * |
| 604 | global sb uchar 4096 |
| 605 | global ob uchar 24 |
| 606 | text pth "/proc/self/status" |
| 607 | """ + PNUM + RSS + """fn main to int |
| 608 | a = rss |
| 609 | a = rss |
| 610 | r = enlarge P ulong 2147483648 |
| 611 | if ne r 0 |
| 612 | ret 3 |
| 613 | end |
| 614 | put P 12345678 1 |
| 615 | b = rss |
| 616 | i = ulong 0 |
| 617 | while lt i 1024 |
| 618 | put P add 1000000000 mul i 4096 1 |
| 619 | i = add i 1 |
| 620 | end |
| 621 | c = rss |
| 622 | pnum len P |
| 623 | pnum a |
| 624 | pnum b |
| 625 | pnum c |
| 626 | ret 0 |
| 627 | end |
| 628 | """ |
| 629 | put("g9", g9) |
| 630 | |
| 631 | # ---------------------------------------------------------------- section S: server |
| 632 | NREQ = 2000 |
| 633 | srv = """note gate-pools S (design section 11): 2000 requests of growing and shrinking sizes (a fixed 64-bit LCG, |
| 634 | note 1 byte .. 64 MiB; the first 8 ramp to the 64 MiB high-water mark) served from one pool that is enlarged |
| 635 | note only past its high-water mark and reused. Prints the high-water mark, enlarge calls, new marks, VmRSS (kB) |
| 636 | note after the high-water point (request 8) and at the end, and a checksum of the bytes served. |
| 637 | note Control: with the byte L on stdin, requests 8..23 are served from fresh memory past the old end (a |
| 638 | note server that enlarges per request); its VmRSS must rise past the high-water mark. |
| 639 | global P uchar * |
| 640 | global sb uchar 4096 |
| 641 | global mb uchar 1 |
| 642 | global ob uchar 24 |
| 643 | text pth "/proc/self/status" |
| 644 | """ + PNUM + RSS + """fn serve q ulong b ulong sz ulong to ulong |
| 645 | cs = ulong 0 |
| 646 | o = ulong 0 |
| 647 | while lt o sz bound 20000 |
| 648 | put P add b o uchar and add q o 255 |
| 649 | cs = add cs ulong get P add b o |
| 650 | o = add o 4096 |
| 651 | end |
| 652 | ret cs |
| 653 | end |
| 654 | fn main to int |
| 655 | leak = false |
| 656 | k = read 0 mb 0 1 |
| 657 | if gt k 0 |
| 658 | leak = eq get mb 0 76 |
| 659 | end |
| 660 | x = ulong 12345 |
| 661 | hw = ulong 0 |
| 662 | calls = ulong 0 |
| 663 | marks = ulong 0 |
| 664 | rhw = ulong 0 |
| 665 | cs = ulong 0 |
| 666 | q = ulong 0 |
| 667 | while lt q %d |
| 668 | x = add mul x 6364136223846793005 1442695040888963407 |
| 669 | sz = add rem shr x 33 67108864 1 |
| 670 | if lt q 8 |
| 671 | sz = mul add q 1 8388608 |
| 672 | end |
| 673 | b = ulong 0 |
| 674 | if and leak ge q 8 |
| 675 | if eq q 24 |
| 676 | break |
| 677 | end |
| 678 | sz = add rem sz 4194304 1 |
| 679 | b = len P |
| 680 | r = enlarge P add b sz |
| 681 | calls = add calls 1 |
| 682 | if ne r 0 |
| 683 | ret 4 |
| 684 | end |
| 685 | end |
| 686 | if gt sz hw |
| 687 | r = enlarge P sz |
| 688 | calls = add calls 1 |
| 689 | if ne r 0 |
| 690 | ret 3 |
| 691 | end |
| 692 | hw = sz |
| 693 | marks = add marks 1 |
| 694 | end |
| 695 | cs = add cs serve q b sz |
| 696 | if eq q 8 |
| 697 | rhw = rss |
| 698 | end |
| 699 | q = add q 1 |
| 700 | end |
| 701 | pnum hw |
| 702 | pnum calls |
| 703 | pnum marks |
| 704 | pnum rhw |
| 705 | pnum rss |
| 706 | pnum cs |
| 707 | ret 0 |
| 708 | end |
| 709 | """ % NREQ |
| 710 | put("srv", srv) |
| 711 | x = 12345 |
| 712 | hw = calls = marks = cs = 0 |
| 713 | for q in range(NREQ): |
| 714 | x = (x * 6364136223846793005 + 1442695040888963407) % M |
| 715 | sz = ((x >> 33) % 67108864) + 1 |
| 716 | if q < 8: |
| 717 | sz = (q + 1) * 8388608 |
| 718 | if sz > hw: |
| 719 | calls += 1; hw = sz; marks += 1 |
| 720 | cs += (q & 255) * ((sz + 4095) // 4096) |
| 721 | with open(os.path.join(OUT, "srv.want"), "w") as f: |
| 722 | f.write("hw %d\ncalls %d\nmarks %d\ncs %d\n" % (hw, calls, marks, cs % M)) |
| 723 | WROTE.append("srv.want") |
| 724 | |
| 725 | # ---------------------------------------------------------------- gate 10: refusals |
| 726 | PM = "global P ulong *\n" |
| 727 | errs = { |
| 728 | # name: (source, line or '*', code) |
| 729 | "err-fnmode": (PM + "fn f to long\nr = enlarge P ulong 4\nret r\nend\n", "*", 9), |
| 730 | "err-ir": ("ir\n" + PM + "fn main to int\nret 0\nend\n", 2, 9), |
| 731 | "err-x86efi": ("target x86efi\n" + PM + "fn main to int\nr = enlarge P ulong 4\nput P 0 1\nret int get P 0\nend\n", "*", 25), |
| 732 | "err-arg": ("om2\n" + PM + "fn f a view ulong * to ulong\nret len a\nend\nfn main to int\nr = enlarge P ulong 4\nx = f P\nret 0\nend\n", 8, 19), |
| 733 | "err-part": ("om2\n" + PM + "fn f a view ulong * to ulong\nret len a\nend\nfn main to int\nr = enlarge P ulong 4\nx = f part P 0 2\nret 0\nend\n", 8, 19), |
| 734 | "err-expr": (PM + "fn main to int\nif eq enlarge P ulong 4 0\nret 1\nend\nret 0\nend\n", 3, 9), |
| 735 | "err-sum": (PM + "fn main to int\nr = add enlarge P ulong 4 0\nret 0\nend\n", 3, 9), |
| 736 | "err-stmt": (PM + "fn main to int\nenlarge P ulong 4\nret 0\nend\n", 3, 16), |
| 737 | "err-sized": ("global G ulong 4\nfn main to int\nr = enlarge G ulong 8\nret 0\nend\n", 3, 7), |
| 738 | "err-local": ("fn main to int\na = new ulong 4 0\nr = enlarge a ulong 8\nret 0\nend\n", 3, 7), |
| 739 | "err-view": ("om2\nfn f a edit ulong * to long\nr = enlarge a ulong 8\nret r\nend\nfn main to int\nret 0\nend\n", 3, 7), |
| 740 | "err-bool": ("global P bool *\nfn main to int\nret 0\nend\n", 1, 9), |
| 741 | "err-nint": (PM + "fn main to int\nr = enlarge P int 4\nret 0\nend\n", 3, 7), |
| 742 | "err-nlong": (PM + "fn main to int\nn = long 4\nr = enlarge P n\nret 0\nend\n", 4, 7), |
| 743 | "err-rint": (PM + "fn main to int\nr = int 0\nr = enlarge P ulong 4\nret 0\nend\n", 4, 7), |
| 744 | "err-devctl": ("global P uchar *\nfn main to int\nr = devctl neg int 1 uint 21531 P 0 8\nret 0\nend\n", 3, 7), |
| 745 | "err-mapwrite": ("global P uchar *\nfn main to int\nh = long 0\nr = mapwrite h 0 P 0 1\nret 0\nend\n", 4, 7), |
| 746 | "err-struct": ("om2\nstruct Pr a ulong b ulong end\n" + PM + "fn f p view Pr to ulong\nret 0\nend\nfn main to int\nx = f P\nret 0\nend\n", 8, 7), |
| 747 | } |
| 748 | for n, (src, ln, code) in errs.items(): |
| 749 | put(n, src + "note gate-pools 10: refused with error %d\n" % code, |
| 750 | "omc: line %s: error %d\n" % (ln, code)) |
| 751 | |
| 752 | # ---------------------------------------------------------------- gate 3: read from a pipe across grows |
| 753 | gr = """note gate-pools 3: a uchar pool filled by read 0 P off n from a pipe, n given by a function that enlarges P |
| 754 | note (and a blocker B above it) by 65536 elements before every read; then the bytes are written back and their |
| 755 | note count printed. The gate feeds it `seq 1 200000` through a pipe; its expected output is built there. |
| 756 | global B uchar * |
| 757 | global P uchar * |
| 758 | global ob uchar 24 |
| 759 | """ + PNUM + """fn gn n ulong to ulong |
| 760 | r = enlarge B add len B 4096 |
| 761 | r = enlarge P add len P n |
| 762 | ret n |
| 763 | end |
| 764 | fn main to int |
| 765 | r = enlarge B ulong 1 |
| 766 | r = enlarge P ulong 1 |
| 767 | used = ulong 0 |
| 768 | go = true |
| 769 | while go bound 100000 |
| 770 | k = read 0 P used gn 65536 |
| 771 | if lt k 0 |
| 772 | ret 2 |
| 773 | end |
| 774 | if eq k 0 |
| 775 | go = false |
| 776 | else |
| 777 | used = add used ulong k |
| 778 | end |
| 779 | end |
| 780 | o = ulong 0 |
| 781 | while lt o used bound 100000 |
| 782 | k = write 1 P o sub used o |
| 783 | if le k 0 |
| 784 | ret 3 |
| 785 | end |
| 786 | o = add o ulong k |
| 787 | end |
| 788 | pnum used |
| 789 | ret 0 |
| 790 | end |
| 791 | """ |
| 792 | put("gr", gr) |
| 793 | print("wrote", len(WROTE), "files") |