-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.sov
More file actions
690 lines (632 loc) · 17.3 KB
/
Copy pathruntime.sov
File metadata and controls
690 lines (632 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# runtime.sov — lists, strings, files. syscall is the only builtin.
# The compiler turns `syscall` into the one x86-64 `syscall` instruction.
# Everything else is Sovren.
heap_ptr: 0
heap_end: 0
# g_argc and g_argv are filled in by the start stub before main runs.
# They must not be set here: a top level line is emitted inside main
# and would wipe what the stub stored.
g_argc: g_argc
g_argv: g_argv
# set up the heap. this must not touch g_argc or g_argv: the start
# stub fills those in before main runs, and _sov_alloc can be called
# at any time, which would wipe them.
rt_heap:
# mmap 64MB anonymous RW: syscall 9
# addr=0 len=64M prot=3 flags=0x22 fd=-1 off=0
heap_ptr: syscall(9, 0, 67108864, 3, 34, 0 - 1, 0)
heap_end: heap_ptr + 67108864
return heap_ptr
rt_init argc argv:
g_argc: argc
g_argv: argv
return rt_heap()
# lock word for the bump. a spawned job may allocate, so the
# pointer move has to be one atomic step, not a load-add-store.
g_alloc_lk: 0
_sov_alloc size:
if heap_ptr is 0
rt_heap()
if g_alloc_lk is 0
g_alloc_lk: heap_ptr
heap_ptr: heap_ptr + 8
poke_i64(g_alloc_lk, 0, 0)
w: 1
as long as w is 1
old: atomic_swap(g_alloc_lk, 1)
if old is 0
w: 0
n: size
p: heap_ptr
heap_ptr: heap_ptr + n
atomic_store(g_alloc_lk, 0)
return p
_sov_strlen s:
# a byte value or any small integer is not a pointer: treat it
# as the empty string instead of dereferencing it
if s < 4096
return 0
n: 0
as long as _sov_byte_at(s, n) is not 0
n: n + 1
return n
_sov_print v:
# if it looks like a pointer into heap/strings, print as text, else as int
_sov_print_val(v, 1)
_sov_print_raw v:
_sov_print_val(v, 0)
_sov_print_val v nl:
if v is 0
syscall(1, 1, intern_zero(), 1)
if nl is 1
syscall(1, 1, intern_nl(), 1)
return 0
# try as c-string: if first bytes look printable || typical
b0: _sov_byte_at(v, 0)
if b0 > 0
if b0 < 128
n: _sov_strlen(v)
if n > 0
if n < 1000000
syscall(1, 1, v, n)
if nl is 1
syscall(1, 1, intern_nl(), 1)
return 0
_sov_print_int(v)
if nl is 1
syscall(1, 1, intern_nl(), 1)
return 0
intern_nl:
return "\n"
intern_zero:
return "0"
_sov_print_int n:
buf: _sov_alloc(32)
i: 0
neg: 0
x: n
if x is 0
syscall(1, 1, intern_zero(), 1)
return 0
if x < 0
neg: 1
# no "x: 0 - x" here. the most negative number has no positive
# twin - negating it gives back itself - so the old form left x
# negative and every digit came out as 48 minus its value. taking
# the sign off each digit instead works for every value, and
# division truncates toward zero so the loop still ends
as long as x is not 0
d: x % 10
if d < 0
d: 0 - d
dummy: _sov_set_byte(buf, i, d + 48)
i: i + 1
x: x / 10
if neg is 1
dummy: _sov_set_byte(buf, i, 45)
i: i + 1
as long as i > 0
i: i - 1
syscall(1, 1, buf + i, 1)
return 0
_sov_strcat a b:
if a is 0
a: blank
if b is 0
b: blank
la: _sov_strlen(a)
lb: _sov_strlen(b)
r: _sov_alloc(la + lb + 1)
i: 0
as long as i < la
dummy: _sov_set_byte(r, i, _sov_byte_at(a, i))
i: i + 1
j: 0
as long as j < lb
dummy: _sov_set_byte(r, i, _sov_byte_at(b, j))
i: i + 1
j: j + 1
dummy: _sov_set_byte(r, i, 0)
return r
_sov_substring s start endd:
# same guard: refuse to read through a non-pointer
if s < 4096
return blank
n: _sov_strlen(s)
if start < 0
start: 0
if endd > n
endd: n
if endd < start
endd: start
L: endd - start
r: _sov_alloc(L + 1)
i: 0
as long as i < L
dummy: _sov_set_byte(r, i, _sov_byte_at(s, start + i))
i: i + 1
dummy: _sov_set_byte(r, i, 0)
return r
_sov_char_at s i:
r: _sov_alloc(2)
if s is 0
dummy: _sov_set_byte(r, 0, 0)
return r
if i < 0
dummy: _sov_set_byte(r, 0, 0)
return r
if i >= _sov_strlen(s)
dummy: _sov_set_byte(r, 0, 0)
return r
dummy: _sov_set_byte(r, 0, _sov_byte_at(s, i))
dummy: _sov_set_byte(r, 1, 0)
return r
_sov_byte_at s i:
return peek_byte(s, i)
_sov_set_byte s i v:
poke_byte(s, i, v)
return s
_sov_str_eq a b:
if a is 0
if b is 0
return 1
return 0
if b is 0
return 0
i: 0
as long as 1
ca: _sov_byte_at(a, i)
cb: _sov_byte_at(b, i)
if ca is not cb
return 0
if ca is 0
return 1
i: i + 1
_sov_txt_eq a b:
# letters equal letters. a small number is one letter, so a
# letter code and a one-letter text compare true.
if a is 0
if b is 0
return 1
return 0
if b is 0
return 0
if a < 4194304
if _sov_strlen(b) is not 1
return 0
if _sov_byte_at(b, 0) is a
return 1
return 0
if b < 4194304
if _sov_strlen(a) is not 1
return 0
if _sov_byte_at(a, 0) is b
return 1
return 0
return _sov_str_eq(a, b)
_sov_txt_ne a b:
return 1 - _sov_txt_eq(a, b)
_sov_starts_with s p:
if s is 0
return 0
if p is 0
return 1
i: 0
as long as 1
cp: _sov_byte_at(p, i)
if cp is 0
return 1
if _sov_byte_at(s, i) is not cp
return 0
i: i + 1
_sov_find_text s n:
if s is 0
return 0 - 1
if n is 0
return 0
ls: _sov_strlen(s)
ln: _sov_strlen(n)
if ln is 0
return 0
if ln > ls
return 0 - 1
i: 0
as long as i + ln <= ls
j: 0
ok: 1
as long as j < ln
if ok is 1
if _sov_byte_at(s, i + j) is not _sov_byte_at(n, j)
ok: 0
j: j + 1
if ok is 1
return i
i: i + 1
return 0 - 1
_sov_trim s:
if s is 0
return blank
n: _sov_strlen(s)
a: 0
go: 1
as long as go is 1
if a >= n
go: 0
if not
c: _sov_byte_at(s, a)
if c is 32
a: a + 1
if not
if c is 9
a: a + 1
if not
if c is 10
a: a + 1
if not
if c is 13
a: a + 1
if not
go: 0
b: n
go: 1
as long as go is 1
if b <= a
go: 0
if not
c: _sov_byte_at(s, b - 1)
if c is 32
b: b - 1
if not
if c is 9
b: b - 1
if not
if c is 10
b: b - 1
if not
if c is 13
b: b - 1
if not
go: 0
return _sov_substring(s, a, b)
_sov_open path flags:
return syscall(257, 0 - 100, path, flags, 420, 0, 0)
_sov_read fd buf n:
return syscall(0, fd, buf, n, 0, 0, 0)
_sov_write fd buf n:
return syscall(1, fd, buf, n, 0, 0, 0)
_sov_close fd:
return syscall(3, fd, 0, 0, 0, 0, 0)
_sov_lseek fd off whence:
return syscall(8, fd, off, whence, 0, 0, 0)
_sov_chmod path mode:
return syscall(90, path, mode, 0, 0, 0, 0)
_sov_read_file path:
fd: _sov_open(path, 0)
if fd < 0
return 0
sz: _sov_lseek(fd, 0, 2)
_sov_lseek(fd, 0, 0)
if sz < 0
sz: 0
buf: _sov_alloc(sz + 1)
got: 0
as long as got < sz
n: _sov_read(fd, buf + got, sz - got)
if n < 1
sz: got
if not
got: got + n
dummy: _sov_set_byte(buf, got, 0)
_sov_close(fd)
return buf
_sov_write_file path data:
fd: syscall(257, 0 - 100, path, 577, 420, 0, 0)
if fd < 0
return 0
n: _sov_strlen(data)
off: 0
as long as off < n
w: _sov_write(fd, data + off, n - off)
if w < 1
_sov_close(fd)
return 0
off: off + w
_sov_close(fd)
return 1
_sov_read_n path n:
fd: _sov_open(path, 0)
if fd < 0
return 0
buf: _sov_alloc(n + 1)
got: 0
as long as got < n
r: _sov_read(fd, buf + got, n - got)
if r < 1
n: got
if not
got: got + r
dummy: _sov_set_byte(buf, got, 0)
_sov_close(fd)
return buf
_sov_write_n path data n:
fd: syscall(257, 0 - 100, path, 577, 420, 0, 0)
if fd < 0
return 0
off: 0
as long as off < n
w: _sov_write(fd, data + off, n - off)
if w < 1
_sov_close(fd)
return 0
off: off + w
_sov_close(fd)
return 1
_sov_argc:
return g_argc
_sov_argv_get i:
if i < 0
return blank
if i >= g_argc
return blank
return peek_ptr(g_argv, i)
# The environment sits on the stack right after the arguments: the
# list of arguments, then a nought, then the environment lines, then
# another nought. Reading it from there works everywhere, including
# where /proc is not readable.
_sov_env_at i:
if i < 0
return blank
base: g_argv + (g_argc + 1) * 8
p: peek_ptr(base, i)
if p is 0
return blank
return p
_sov_memcpy dst src n:
k: 0
as long as k < n
dummy: _sov_set_byte(dst, k, _sov_byte_at(src, k))
k: k + 1
return dst
_sov_atoi s:
if s is 0
return 0
n: 0
sign: 1
i: 0
if _sov_byte_at(s, 0) is 45
sign: 0 - 1
i: 1
go: 1
as long as go is 1
c: _sov_byte_at(s, i)
if c < 48
go: 0
if not
if c > 57
go: 0
if not
n: n * 10 + (c - 48)
i: i + 1
return n * sign
# list: [cap, len, items...] as 8-byte slots
_sov_list_new:
a: _sov_alloc(80)
poke_i64(a, 0, 8)
poke_i64(a, 8, 0)
return a
_sov_list_add lst item:
if lst is 0
lst: _sov_list_new()
cap: peek_i64(lst, 0)
len: peek_i64(lst, 8)
if len >= cap
nc: cap * 2
b: _sov_alloc(16 + nc * 8)
poke_i64(b, 0, nc)
poke_i64(b, 8, len)
i: 0
as long as i < len
poke_i64(b, 16 + i * 8, peek_i64(lst, 16 + i * 8))
i: i + 1
lst: b
poke_i64(lst, 16 + len * 8, item)
poke_i64(lst, 8, len + 1)
return lst
_sov_list_get lst i:
if lst is 0
return 0
len: peek_i64(lst, 8)
if i < 0
return 0
if i >= len
return 0
return peek_i64(lst, 16 + i * 8)
_sov_list_len lst:
if lst is 0
return 0
return peek_i64(lst, 8)
_sov_list_set lst i v:
if lst is 0
return 0
len: peek_i64(lst, 8)
if i < 0
return lst
if i >= len
return lst
poke_i64(lst, 16 + i * 8, v)
return lst
# byte buffer: [cap, len, bytes...]
_sov_buf_new:
p: _sov_alloc(80)
poke_i64(p, 0, 64)
poke_i64(p, 8, 0)
return p
_sov_buf_u8 buf v:
if buf is 0
buf: _sov_buf_new()
cap: peek_i64(buf, 0)
len: peek_i64(buf, 8)
if len + 1 > cap
nc: cap * 2
as long as nc < len + 1
nc: nc * 2
b: _sov_alloc(16 + nc)
poke_i64(b, 0, nc)
poke_i64(b, 8, len)
i: 0
as long as i < len
dummy: _sov_set_byte(b, 16 + i, _sov_byte_at(buf, 16 + i))
i: i + 1
buf: b
dummy: _sov_set_byte(buf, 16 + peek_i64(buf, 8), v)
poke_i64(buf, 8, peek_i64(buf, 8) + 1)
return buf
_sov_buf_u32 buf v:
buf: _sov_buf_u8(buf, v % 256)
buf: _sov_buf_u8(buf, (v / 256) % 256)
buf: _sov_buf_u8(buf, (v / 65536) % 256)
buf: _sov_buf_u8(buf, (v / 16777216) % 256)
return buf
_sov_buf_u64 buf v:
x: v
i: 0
as long as i < 8
buf: _sov_buf_u8(buf, x % 256)
x: x / 256
i: i + 1
return buf
_sov_buf_len buf:
if buf is 0
return 0
return peek_i64(buf, 8)
_sov_buf_at buf i:
if buf is 0
return 0
len: peek_i64(buf, 8)
if i < 0
return 0
if i >= len
return 0
return _sov_byte_at(buf, 16 + i)
_sov_buf_write path buf:
if buf is 0
return 0
return _sov_write_n(path, buf + 16, peek_i64(buf, 8))
# ---- threads: spawn a job on a new thread, join it -------------
#
# spawn work(slot, 5) start work on a new thread, gives a handle
# join h block until the thread has finished
# thread_slot i a shared result slot for the job to poke
#
# a spawned job takes at most six values. the first one should be a
# slot: the job pokes its result there and the parent reads it after
# the join. a spawned job must not allocate from the heap yet - the
# bump pointer is not thread-safe, and the race detector round is
# what fixes that. integer work and poking a slot are safe.
#
# the block and the slots live in a shared mapping: if the host
# refuses clone (the sandbox does), spawn falls back to fork and the
# child still sees the parent's slots, and the futex word the parent
# sleeps on. on a normal host clone wins and you get a real thread.
#
# one shared source, every os: the compiler's syscall bridge turns
# the numbers below (9, 56, 202, 57, 60) into whatever the os does -
# mmap/clone-or-fork/futex on linux and android, CreateThread and
# WaitForSingleObject on windows, vm_allocate, thread_create and a
# semaphore on macos. the bridge hands back the same control block
# either way, so a handle means the same thing everywhere: the
# block's status word, then the job, then its values.
#
# _sov_thread_exit is injected by each compiler: on linux a thread
# is a detached clone or a forked process, so ending it is the exit
# syscall; on windows it is ExitThread; on macos a thread return.
g_thread_cb: 0
t_pool_base: 0
t_pool_end: 0
thread_slot i:
# slots come from one shared 64K pool: eight thousand of them,
# visible to a forked child as well as a cloned thread
if t_pool_base is 0
b: syscall(9, 0, 65536, 3, 33, 0 - 1, 0)
if b < 0
return b
t_pool_base: b
t_pool_end: b + 65536
p: t_pool_base + i * 8
if p > t_pool_end
return 0 - 1
return p
_sov_thread_spawn fn a0 a1 a2 a3 a4 a5:
base: syscall(9, 0, 262272, 3, 33, 0 - 1, 0)
if base < 0
return base
top: base + 262272
poke_i64(base, 0, 0)
poke_i64(base, 8, fn)
poke_i64(base, 16, a0)
poke_i64(base, 24, a1)
poke_i64(base, 32, a2)
poke_i64(base, 40, a3)
poke_i64(base, 48, a4)
poke_i64(base, 56, a5)
# 12032 = CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_DETACHED
# clone(fn, stack, flags, parent_tid, child_tid, tls)
e: addr_of _sov_thread_entry_stack
r: syscall(56, e, top, 12032, 0, 0, 0)
if r < 0
# clone is refused (the sandbox does this): fork instead.
# the child finds the block through the global, runs the
# job, sets the shared status and wakes the shared futex.
g_thread_cb: base
p: syscall(57, 0, 0, 0, 0, 0, 0)
if p is 0
cb: g_thread_cb
f: peek_i64(cb, 8)
dummy: cfunc(f, peek_i64(cb, 16), peek_i64(cb, 24), peek_i64(cb, 32), peek_i64(cb, 40), peek_i64(cb, 48), peek_i64(cb, 56))
poke_i64(cb, 0, 1)
dummy: syscall(202, cb, 1, 1, 0, 0, 0)
dummy: _sov_thread_exit()
if p < 0
return p
# the handle is the control block's base on every os: join
# reads its status word. the clone model's pid stays internal.
return base
# where the stack is right now, as a task: the thread entry below
# calls it by name, and the compiler's sp() builtin answers directly
stack_pointer:
return sp()
# entry for the clone model: the child's stack pointer is the exact
# top that spawn passed to clone, so the control block sits a known
# distance below it
_sov_thread_entry_stack:
# clone model: the child's stack pointer is the exact top that
# spawn passed to clone, so the block sits a known distance down
s: stack_pointer()
cb: s - 262272
f: peek_i64(cb, 8)
dummy: cfunc(f, peek_i64(cb, 16), peek_i64(cb, 24), peek_i64(cb, 32), peek_i64(cb, 40), peek_i64(cb, 48), peek_i64(cb, 56))
poke_i64(cb, 0, 1)
dummy: syscall(202, cb, 1, 1, 0, 0, 0)
dummy: _sov_thread_exit()
return 0
# entry for the create model (windows, macos): the os calls this
# with the control block as its one argument, and returning from it
# is what ends the thread
_sov_thread_entry_param base:
f: peek_i64(base, 8)
dummy: cfunc(f, peek_i64(base, 16), peek_i64(base, 24), peek_i64(base, 32), peek_i64(base, 40), peek_i64(base, 48), peek_i64(base, 56))
poke_i64(base, 0, 1)
dummy: syscall(202, base, 1, 1, 0, 0, 0)
return 0
# wait for a thread to finish: read the status, and when it is still
# running, sleep until the thread's wake lands
_sov_thread_join h:
s: peek_i64(h, 0)
g: 1
as long as g is 1
if s is 1
g: 0
if not
dummy: syscall(202, h, 0, s, 0, 0, 0)
s: peek_i64(h, 0)
return 0