-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsmsb.html
3547 lines (3363 loc) · 189 KB
/
lsmsb.html
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>LSMSB</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" charset="utf-8">
</head>
<body>
<h1>LSMSB: A Linux Sandboxing Scheme</h1>
<p>Adam Langley (<tt>[email protected]</tt>)<br>
Version <tt>20101017</tt> - Daniel Borca (<tt>[email protected]</tt>)<br>
Version <tt>20101107</tt> - Daniel Borca (<tt>[email protected]</tt>)</p>
<ol class="toc"><li><a href="#section-1">Kernel code</a><ol class="toc"><li><a href="#section-6">Typechecking.</a></li><li><a href="#section-21">Evaluating filters</a></li><li><a href="#section-27">Installing the sandbox</a></li><li><a href="#section-32">Interfacing with LSM</a></li></ol></li><li><a href="#section-42">The assembler</a><ol class="toc"><li><a href="#section-50">Parsing</a></li></ol></li><li><a href="#section-76">Using a sandbox</a></li></ol>
<p>This is LSMSB, a sandboxing scheme for Linux based on the ideas of the <a
href="http://www.318.com/techjournal/security/a-brief-introduction-to-mac-os-x-sandbox-technology/">
OS X sandbox</a> (which, in turn, was inspired by TrustedBSD and FreeBSD).</p>
<p>Imagine that you're working on a university computer and you get a binary
which promises to do some fiendishly complex calculation, reading from a file
<tt>./input</tt> and writing to a file <tt>./output</tt>. It also talks to a
specific server to access a pre-computed lookup table. You want to run it, but
you don't want to have to trust that it won't do anything malicious (save
giving the wrong answer).</p>
<p>You current options are very limited. Without root access you cannot setup a
chroot jail, as troublesome as that is. If the system has SELinux or AppArmor
installed, the tools are there but those are MAC systems and only root can
define their policies.</p>
<p>Your best bet, currently, is either to use <tt>ptrace</tt> or to run a whole
virtual machine. The former is slow and difficult to get right in the face of
threads and the latter is a sledgehammer when we just want to crack a nut.</p>
<p>To address these concerns we need a sandboxing system which is:</p>
<ol>
<li>Available: The sandbox must be available to normal users. MAC systems
which are only configured by root are not the correct solution for this.</li>
<li>Flexible: <tt>seccomp</tt> is (currently) too rigid to really be useful. A
sandbox must be able to express the correct level of authority.</li>
<li>Reliable: The sandbox should not be open to races etc.</li>
<li>Deployable: One should be able to use a sandbox via a couple of call in
<tt>main</tt>(). If you have to implement an IPC system and pass file descriptors around
in order to achieve reliability then that's a significant demerit.</li>
<li>Composable: If I choose to impose a sandbox on a process before <tt>exec</tt>()
then that process should still be able to impose another sandbox on itself. The
resulting authority should be the intersection of the two sandboxes.</li>
<li>Affordable: It should be reasonable to sandbox many processes. If the
sandbox has a high performance impact, then that's a problem. If the sandbox
requires a tracing process for every sandboxed process, then that's a
problem.</li>
</ol>
<p>We present a sandboxing scheme using the LSM hooks in the Linux kernel. At
the moment this scheme is a prototype only and this code is based off of
2.6.30-rc<i>x</i>.</p>
<a name="section-1"></a>
<div class="section" id="section-1">
<p><span class="sectionnumber">1</span>
<b>Kernel code</b></p>
<div style="float:left; padding-right: 1em;"><object width="212" height="200" data="dia1.svg" type="image/svg+xml" class="img"></object></div>
<p>LSMSB uses the Linux Security Modules hooks to intercept security decisions in
the kernel. The policies are implemented by tables of rules which are uploaded
from user-space. Each process has a stack of zero or more sandboxes. Each
sandbox may define a rule table for a given operation. An action must be
accepted by all the sandboxes in the stack for it to be permitted. Child
processes inherit the sandbox stack of their parents and are free to push extra
sandboxes onto their stack. By construction, this can only reduce their
authority.</p>
<div style="clear: left;"></div>
<div class="codename">Sandbox structure</div>
<div class="code">
<div class="codeline"><span class="keyword">struct</span> lsmsb_sandbox {</div>
<div class="codeline"> atomic_t refcount;</div>
<div class="codeline"> <span class="keyword">struct</span> lsmsb_sandbox *parent;</div>
<div class="codeline"> <span class="keyword">struct</span> lsmsb_filter *filters[LSMSB_FILTER_CODE_MAX];</div>
<div class="codeline"> <span class="comment">// TODO: add support for more actions</span></div>
<div class="codeline">};</div>
</div>
<div class="sectionrefs">
<a class="fileref" href="#section-40">lsmsb.c</a>
</div>
<a name="section-2"></a>
<div class="section" id="section-2">
<p><span class="sectionnumber">2</span>
Rule tables</p>
<p>An LSMSB filter evaluates a table of rules to decide if a given action is
permitted or not. The table of rules is specific to a given filter as the
filter defines the context in which the table is evaluated. The context
determines which arguments are provided to the table, their order, syntax and
semantics. It also defines the meaning of the return code although, by
convention, zero always means reject and non-zero means accept.</p>
<p>The rule tables are very limited. Conditionals do exist, but a rule can only
jump forward, thus there are no loops. This also means that the rule tables are
not Turing complete and are guaranteed to terminate.</p>
<h5>The filter structure</h5>
<p>Each rule in the table is a single 32-bit unsigned integer. Because constant
values often don't fit in 32-bits, we need another way to deal with them. Thus
constants are kept in a side array, linked with each filter. The rules in the
table can reference them by their index.</p>
<p>For working storage, the rules in the table have an array of 16 registers which
can either store a 32-bit unsigned integer or a byte-string (which is a normal
string, but may contain NUL characters). If a table is exceedingly complex, it
may need more than 16-registers of storage to hold temporary values. For these
situations, we also allow the table to specify the number of spill slots
that it needs. Spill slots act just like registers except that they have to be
read and written explicitly.</p>
<p>From this, the structure for a filter is obvious:</p>
<div class="codename">Filter structure</div>
<div class="code">
<div class="codeline"><span class="keyword">struct</span> lsmsb_filter {</div>
<div class="codeline"> <span class="keyword">unsigned</span> num_operations;</div>
<div class="codeline"> <span class="keyword">unsigned</span> num_spill_slots;</div>
<div class="codeline"> <span class="keyword">unsigned</span> num_constants;</div>
<div class="codeline"> <span class="keyword">uint32_t</span> *operations;</div>
<div class="codeline"> <span class="keyword">struct</span> lsmsb_value constants[0];</div>
<div class="codeline">};</div>
<pre>
</pre>
<div class="codeline"><span class="preprocessor">#define LSMSB_NUM_REGISTERS 16</span></div>
<div class="codeline"><span class="comment">// This is the maximum number of operations in a filter. Note that the code</span></div>
<div class="codeline"><span class="comment">// assumes that this value fits in a uint16_t with a couple of values to spare.</span></div>
<div class="codeline"><span class="preprocessor">#define LSMSB_FILTER_OPS_MAX 32768</span></div>
<div class="codeline"><span class="preprocessor">#define LSMSB_SPILL_SLOTS_MAX 32</span></div>
<div class="codeline"><span class="preprocessor">#define LSMSB_CONSTANTS_MAX 256</span></div>
<div class="codeline"><span class="preprocessor">#define LSMSB_CONSTANT_LENGTH_MAX 512</span></div>
</div>
<div class="sectionrefs">
<a class="fileref" href="#section-40">lsmsb.c</a>
<a class="fileref" href="#section-75">lsmsb-as.rl</a>
</div>
<a name="section-3"></a>
<div class="section" id="section-3">
<p><span class="sectionnumber">3</span>
Filter values</p>
<p>
The contents of registers, spill slots and constants are all ‘values’. These
values are either a 32-bit unsigned int or a bytestring and represented by the
following structure.
</p>
<p>
For integer values, <tt>value</tt> is the integer and <tt>data</tt> is set to <tt>NULL</tt>. For
bytestrings, <tt>data</tt> is a pointer to the bytes (and thus not <tt>NULL</tt>) and <tt>value</tt>
is the number of bytes.
</p>
<div class="codename">Value structure</div>
<div class="code">
<div class="codeline"><span class="keyword">struct</span> lsmsb_value {</div>
<div class="codeline"> <span class="keyword">uint8_t</span> *data;</div>
<div class="codeline"> <span class="keyword">uint32_t</span> value;</div>
<div class="codeline">};</div>
</div>
<div class="sectionrefs">
<a class="fileref" href="#section-40">lsmsb.c</a>
<a class="fileref" href="#section-75">lsmsb-as.rl</a>
</div>
<a name="section-4"></a>
<div class="section" id="section-4">
<p><span class="sectionnumber">4</span>
Operations</p>
<p>
Each rule in the table consists of at least an operation, which is encoded in
the top 8-bits. Given the operation, there are often other arguments (register
numbers etc) encoded in the remaining 24-bits, the format of which is specific
to each operation.
</p>
<p>
The operations are numbered from zero by the following enum:
</p>
<table>
<tr><th>Name</th><th>Explanation</th><th>Effect</th></tr>
<tr><td><tt>MOV</tt></td> <td>Move</td> <td>reg<sub>1</sub> = reg<sub>2</sub></td></tr>
<tr><td><tt>LDI</tt></td> <td>Load immediate</td> <td>reg<sub>1</sub> = immediate value</td></tr>
<tr><td><tt>LDC</tt></td> <td>Load constant</td> <td>reg<sub>1</sub> = constant value</td></tr>
<tr><td><tt>RET</tt></td> <td>Return value in register</td> <td><i>terminates the filter</i></td></tr>
<tr><td><tt>JMP</tt></td> <td>Jump</td> <td>Skips the next <i>n</i> rules</td></tr>
<tr><td><tt>SPILL</tt></td> <td>Spill register</td> <td>spill<sub>1</sub> = reg<sub>1</sub></td></tr>
<tr><td><tt>UNSPILL</tt></td> <td>Unspill register</td> <td>reg<sub>1</sub> = spill<sub>1</sub></td></tr>
<tr><td><tt>JNZ</tt></td> <td>Jump conditionally</td> <td>If reg<sub>1</sub> != 0 then skip the next <i>n</i> rules</td></tr>
<tr><td><tt>JZ</tt></td> <td>Jump conditionally</td> <td>If reg<sub>1</sub> == 0 then skip the next <i>n</i> rules</td></tr>
<tr><td><tt>EQ</tt></td> <td>Equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> == reg<sub>3</sub></td></tr>
<tr><td><tt>NE</tt></td> <td>Not equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> != reg<sub>3</sub></td></tr>
<tr><td><tt>GT</tt></td> <td>Greater than?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> > reg<sub>3</sub></td></tr>
<tr><td><tt>LT</tt></td> <td>Less than?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> < reg<sub>3</sub></td></tr>
<tr><td><tt>GTE</tt></td> <td>Greater than or equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> >= reg<sub>3</sub></td></tr>
<tr><td><tt>LTE</tt></td> <td>Less than or equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> <= reg<sub>3</sub></td></tr>
<tr><td><tt>AND</tt></td> <td>Bitwise conjunction</td> <td>reg<sub>1</sub> = reg<sub>2</sub> & reg<sub>3</sub></td></tr>
<tr><td><tt>OR</tt></td> <td>Bitwise disjunction</td> <td>reg<sub>1</sub> = reg<sub>2</sub> | reg<sub>3</sub></td></tr>
<tr><td><tt>XOR</tt></td> <td>Bitwise exclusive-or</td> <td>reg<sub>1</sub> = reg<sub>2</sub> ^ reg<sub>3</sub></td></tr>
<tr><td><tt>ISPREFIXOF</tt></td> <td>Bytestring prefix</td> <td>reg<sub>1</sub> = reg<sub>2</sub> ⊆ reg<sub>3</sub></td></tr>
</table>
<div class="codename">List of operations</div>
<div class="code">
<div class="codeline"><span class="keyword">enum</span> lsmsb_opcode {</div>
<div class="codeline"> LSMSB_OPCODE_MOV = 0,</div>
<div class="codeline"> LSMSB_OPCODE_LDI,</div>
<div class="codeline"> LSMSB_OPCODE_LDC,</div>
<div class="codeline"> LSMSB_OPCODE_RET,</div>
<div class="codeline"> LSMSB_OPCODE_JMP,</div>
<div class="codeline"> LSMSB_OPCODE_SPILL,</div>
<div class="codeline"> LSMSB_OPCODE_UNSPILL,</div>
<div class="codeline"> LSMSB_OPCODE_JNZ,</div>
<div class="codeline"> LSMSB_OPCODE_JZ,</div>
<div class="codeline"> LSMSB_OPCODE_EQ,</div>
<div class="codeline"> LSMSB_OPCODE_NE,</div>
<div class="codeline"> LSMSB_OPCODE_GT,</div>
<div class="codeline"> LSMSB_OPCODE_LT,</div>
<div class="codeline"> LSMSB_OPCODE_GTE,</div>
<div class="codeline"> LSMSB_OPCODE_LTE,</div>
<div class="codeline"> LSMSB_OPCODE_AND,</div>
<div class="codeline"> LSMSB_OPCODE_OR,</div>
<div class="codeline"> LSMSB_OPCODE_XOR,</div>
<div class="codeline"> LSMSB_OPCODE_ISPREFIXOF,</div>
<div class="codeline">};</div>
</div>
<div class="sectionrefs">
<a class="fileref" href="#section-40">lsmsb.c</a>
<a class="fileref" href="#section-75">lsmsb-as.rl</a>
</div>
<a name="section-5"></a>
<div class="section" id="section-5">
<p><span class="sectionnumber">5</span>
Available filter types</p>
<p>The type of a filter signifies the operation which it intends to filter, as
well as the context that it runs in. (See <a href="#section-2">above</a>). Here are the currently defined filters.</p>
<table>
<tr><th>Name</th> <th>Explanation</th> <th>Arguments</th></tr>
<tr><td><tt>DENTRY_OPEN</tt></td> <td>File open</td> <td>Filename(<i>bs</i>) and mode(<i>i</i>)</td></tr>
<tr><td><tt>SOCKET_CREATE</tt></td> <td>Create socket</td> <td>Family(<i>i</i>), type(<i>i</i>), protocol(<i>i</i>) and kern(<i>i</i>)</td></tr>
<tr><td><tt>SOCKET_CONNECT</tt></td> <td>Connect socket</td> <td>Family(<i>i</i>), type(<i>i</i>), protocol(<i>i</i>), port(<i>i</i>), ipv4(<i>i</i>) and data(<i>bs</i>)</td></tr>
</table>
<div class="codename">Filter codes</div>
<div class="code">
<div class="codeline"><span class="keyword">enum</span> lsmsb_filter_code {</div>
<div class="codeline"> LSMSB_FILTER_CODE_DENTRY_OPEN = 0,</div>
<div class="codeline"> LSMSB_FILTER_CODE_SOCKET_CREATE,</div>
<div class="codeline"> LSMSB_FILTER_CODE_SOCKET_CONNECT,</div>
<div class="codeline"> LSMSB_FILTER_CODE_MAX, <span class="comment">// not a real filter code</span></div>
<div class="codeline">};</div>
</div>
<div class="sectionrefs">
<a class="fileref" href="#section-40">lsmsb.c</a>
<a class="fileref" href="#section-75">lsmsb-as.rl</a>
</div>
<a name="section-6"></a>
<div class="section" id="section-6">
<p><span class="sectionnumber">6</span>
<b>Typechecking.</b></p>
<p>
The rule tables are simple enough that they can be validated such that we can
know they they can never cause a run-time error. This is helpful as it means
that we can pay the verification cost once (when loading the table) and omit
run-time checks when evaluating.
</p>
<div class="codename">Typechecking</div>
<div class="code">
<div class="ref"><span class="reftext">Predecessor table utility functions</span> <a href="#section-22" class="refnum">22</a></div>
<div class="ref"><span class="reftext">Predecessor tables</span> <a href="#section-7" class="refnum">7</a></div>
<div class="ref"><span class="reftext">Type unification</span> <a href="#section-11" class="refnum">11</a></div>
<div class="ref"><span class="reftext">Typecheck function</span> <a href="#section-20" class="refnum">20</a></div>
</div>
<div class="sectionrefs">
<a class="fileref" href="#section-40">lsmsb.c</a>
<a class="fileref" href="#section-75">lsmsb-as.rl</a>
</div>
<a name="section-7"></a>
<div class="section" id="section-7">
<p><span class="sectionnumber">7</span>
Predecessor tables</p>
<p>
For each rule in the table there is a set of rules which can be the immediate
predecessor of that rule. In the simple case, the rule preceding will fall
though and be the single predecessor. In more complex cases, a rule might be
the target of several jumps and a fall though.
</p>
<p>
The predecessor table conceptually has <i>n</i> rows and <i>x</i> columns, where <i>n</i> is
the number of rules in the rule table and <i>x</i> is a constant value. If we didn't
have jumps each rule could only have a single predecessor, <i>x</i> could be one,
and we would have a simple array. However, in the presence of jumps <i>x</i> may
need to be greater than one.
</p>
<p>
In the pessimal case, every rule in the table except the last is a jump to the
very last one. Then <i>x</i> would need to be <i>n - 1</i>, even though most of
the slots in the table would be unused. Therefore we take a hybrid approach. We
define <i>x</i> to be a small number and, if a row overflows, we mark it with a
magic value. Most of the time this will work fine but, in exceptional cases,
we have to deal with the overflows.
</p>
<div style="text-align: center;"><object width="420" height="175" data="dia2.svg" type="image/svg+xml" class="img"></object></div>
<div class="codename">Predecessor tables</div>
<div class="code">
<div class="codeline">/* This is the magic unset value in the predecessor table. This value must be</div>
<div class="codeline"> * all ones because we clear the table with a memset. */</div>
<div class="codeline"><span class="preprocessor">#define LSMSB_PREDECESSOR_TABLE_INVAL 0xffff</span></div>
<div class="codeline">/* This is a magic value in the predecessor table which marks an overflow. */</div>
<div class="codeline"><span class="preprocessor">#define LSMSB_PREDECESSOR_TABLE_OVERFLOW (LSMSB_FILTER_OPS_MAX + 1)</span></div>
<div class="codeline">/* This is the number of predecessors that we record, per row, before</div>
<div class="codeline"> * overflowing */</div>
<div class="codeline"><span class="preprocessor">#define LSMSB_PREDECESSOR_TABLE_WIDTH 2</span></div>
<pre>
</pre>
<div class="ref"><span class="reftext">Appending to a predecessor table row</span> <a href="#section-8" class="refnum">8</a></div>
<div class="ref"><span class="reftext">Building the predecessor table</span> <a href="#section-9" class="refnum">9</a></div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-6">6</a>
</div>
<a name="section-8"></a>
<div class="section" id="section-8">
<p><span class="sectionnumber">8</span>
Appending to a predecessor table row</p>
<p>
Appending to a row simply involves scanning the entries in the row until a
free slot is found. If we don't find a free slot we mark the first entry in the
row with the magic overflow tag.
</p>
<div class="codename">Appending to a predecessor table row</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> void lsmsb_predecessor_table_append(<span class="keyword">uint16_t</span> *ptable, <span class="keyword">unsigned</span> target,</div>
<div class="codeline"> <span class="keyword">unsigned</span> source)</div>
<div class="codeline">{</div>
<div class="codeline"> <span class="keyword">uint16_t</span> *row = &ptable[target * LSMSB_PREDECESSOR_TABLE_WIDTH];</div>
<div class="codeline"> <span class="keyword">unsigned</span> i;</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">for</span> (i = 0; i < LSMSB_PREDECESSOR_TABLE_WIDTH; ++i) {</div>
<div class="codeline"> <span class="keyword">if</span> (row[i] == LSMSB_PREDECESSOR_TABLE_OVERFLOW)</div>
<div class="codeline"> return; <span class="comment">// this is already an overflow row</span></div>
<div class="codeline"> <span class="keyword">if</span> (row[i] == LSMSB_PREDECESSOR_TABLE_INVAL) {</div>
<div class="codeline"> row[i] = source;</div>
<div class="codeline"> return;</div>
<div class="codeline"> }</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> /* we reached the end of the table without a spare slot. We have to mark</div>
<div class="codeline"> the row as overflowed. */</div>
<div class="codeline"> row[0] = LSMSB_PREDECESSOR_TABLE_OVERFLOW;</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-7">7</a>
</div>
<a name="section-9"></a>
<div class="section" id="section-9">
<p><span class="sectionnumber">9</span>
Building the predecessor table</p>
<p>
We build the predecessor table by first clearing it then, for each rule, we
find its one or two successor instructions and mark it as a predecessor for
those instructions.
</p>
<div class="codename">Building the predecessor table</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> <span class="keyword">char</span> lsmsb_predecessor_table_fill(<span class="keyword">uint16_t</span> *ptable, <span class="keyword">const</span> <span class="keyword">uint32_t</span> *ops, <span class="keyword">unsigned</span> num_ops)</div>
<div class="codeline">{</div>
<div class="codeline"> <span class="keyword">unsigned</span> i;</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (num_ops == 0)</div>
<div class="codeline"> return 1;</div>
<pre>
</pre>
<div class="ref"> <span class="reftext">Clear the predecessor table</span> <a href="#section-10" class="refnum">10</a></div>
<pre>
</pre>
<div class="codeline"> /* First we deal with all the elements except the last. */</div>
<div class="codeline"> <span class="keyword">for</span> (i = 0; i < num_ops - 1; ++i) {</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint32_t</span> op = ops[i];</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">enum</span> lsmsb_opcode opcode = lsmsb_op_opcode_get(op);</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_opcode_falls_through(opcode))</div>
<div class="codeline"> lsmsb_predecessor_table_append(ptable, i + 1, i);</div>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_opcode_is_jump(opcode)) {</div>
<div class="codeline"> /* 0 <= i, jumplength <= 0xffff */</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> target = i + lsmsb_op_jump_length(op);</div>
<div class="codeline"> <span class="keyword">if</span> (target == i)</div>
<div class="codeline"> return 0; /* zero length jump */</div>
<div class="codeline"> <span class="keyword">if</span> (target >= num_ops)</div>
<div class="codeline"> return 0; /* tried to jump off the end */</div>
<div class="codeline"> lsmsb_predecessor_table_append(ptable, target, i);</div>
<div class="codeline"> }</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> /* Now deal with the last operation. */</div>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_op_opcode_get(ops[num_ops - 1]) != LSMSB_OPCODE_RET)</div>
<div class="codeline"> return 0;</div>
<pre>
</pre>
<div class="codeline"> return 1;</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-7">7</a>
</div>
<a name="section-10"></a>
<div class="section" id="section-10">
<p><span class="sectionnumber">10</span>
Clearing the predecessor table</p>
<p>
Since we chose our magic unused value to be <tt>0xffff</tt>, we can clear the table
using <tt>memset</tt>.
</p>
<div class="codename">Clear the predecessor table</div>
<div class="code">
<div class="codeline">memset(ptable, 0xff, sizeof(<span class="keyword">uint16_t</span>) * num_ops * LSMSB_PREDECESSOR_TABLE_WIDTH);</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-9">9</a>
</div>
<a name="section-11"></a>
<div class="section" id="section-11">
<p><span class="sectionnumber">11</span>
Type unification</p>
<p>
Type unification is the process of finding, for each rule, the set of possible
types for each register and spill slot. Since we only have two types (integers
and bytestrings), we can encode this using only a couple of bits.
</p>
<p>
If we then find a rule which is going to operate on a register which could
possibly hold the wrong type, we reject the rule table.
</p>
<div class="codename">Type unification</div>
<div class="code">
<div class="ref"><span class="reftext"><tt>lsmsb_type</tt></span> <a href="#section-12" class="refnum">12</a></div>
<div class="ref"><span class="reftext">Type vector utility functions</span> <a href="#section-19" class="refnum">19</a></div>
<div class="ref"><span class="reftext">Predecessor testing function</span> <a href="#section-18" class="refnum">18</a></div>
<div class="ref"><span class="reftext">Type vector unification</span> <a href="#section-13" class="refnum">13</a></div>
<div class="ref"><span class="reftext">Type vector updating</span> <a href="#section-14" class="refnum">14</a></div>
<div class="ref"><span class="reftext">Type vector arrays</span> <a href="#section-15" class="refnum">15</a></div>
<div class="ref"><span class="reftext">Filter contexts</span> <a href="#section-23" class="refnum">23</a></div>
<div class="ref"><span class="reftext">Getting a type vector for a filter</span> <a href="#section-24" class="refnum">24</a></div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-6">6</a>
</div>
<a name="section-12"></a>
<div class="section" id="section-12">
<p><span class="sectionnumber">12</span>
Type vectors</p>
<p>
A <i>type vector</i> is an array of 2-bit values, one for each register and
spill slot. Each 2-bit value is either:
</p>
<table>
<tr><td>Undefined:</td> <td>at the start of evaluation, every spill slot and register
which isn't defined by the context has an undefined type.</td></tr>
<tr><td>Integer:</td> <td>an integer type.</td></tr>
<tr><td>Bytestring:</td> <td>a byte-string type.</td></tr>
<tr><td>Conflicting:</td> <td>the type differs depending on the control flow.</td></tr>
</table>
<div class="codename"><tt>lsmsb_type</tt></div>
<div class="code">
<div class="codeline"><span class="keyword">enum</span> lsmsb_type {</div>
<div class="codeline"> LSMSB_TYPE_UNDEF = 0,</div>
<div class="codeline"> LSMSB_TYPE_U32,</div>
<div class="codeline"> LSMSB_TYPE_BYTESTRING,</div>
<div class="codeline"> LSMSB_TYPE_CONFLICTING,</div>
<div class="codeline">};</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">char</span> lsmsb_type_is_value(enum lsmsb_type type)</div>
<div class="codeline">{</div>
<div class="codeline"> return type == LSMSB_TYPE_U32 || type == LSMSB_TYPE_BYTESTRING;</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">enum</span> lsmsb_type lsmsb_constant_type_get(<span class="keyword">const</span> <span class="keyword">struct</span> lsmsb_value *v)</div>
<div class="codeline">{</div>
<div class="codeline"> return v->data ? LSMSB_TYPE_BYTESTRING : LSMSB_TYPE_U32;</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-11">11</a>
</div>
<a name="section-13"></a>
<div class="section" id="section-13">
<p><span class="sectionnumber">13</span>
Unifying type vectors</p>
<p>
Given two type vectors (say, the type vectors from the two predecessors of a
rule), we unify them by considering each element of each vector, pairwise.
Then, for each pair, if they match then we output that value, otherwise, we
record the value as conflicting.
</p>
<p>
To implement this quickly, we try to operate on many elements concurrently.
Most of the time a rule table will not use spill slots so the type vector will
be 2 × 16 = 32 bits long and we can do them all in one go.
</p>
<p>
To understand the code below, consider a single pair of 2-bit inputs. If we
exclusive-or them, we'll end up with a true bit in the result iff the inputs
differed. If we could map 01, 10, and 11 to 11 and then OR with the original
input, that would map equal inputs to themselves and differing inputs to
<tt>LSMSB_TYPE_CONFLICTING</tt>. (Remember that
<tt>LSMSB_TYPE_CONFLICTING</tt> <a href="#section-12">has a value of
11</a> in binary).
</p>
<p>
In order to transform the exclusive-or output we define <tt>left</tt> to be the left
bit and <tt>right</tt> to be the right bit. Then we OR together <tt>left</tt>,
<tt>right</tt>, <tt>left >> 1</tt> and <tt>right << 1</tt>. This
results in the desired mapping.
</p>
<p>
We work with 32-bit quantities where possible and fall back to 8-bit values
otherwise. Although the type vector might not be a multiple of 4 values long,
we still operate on the undefined bits at the end because it's harmless and
faster.
</p>
<div class="codename">Type vector unification</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> void lsmsb_type_vector_unify(<span class="keyword">uint8_t</span> *a, <span class="keyword">const</span> <span class="keyword">uint8_t</span> *b, <span class="keyword">unsigned</span> bytelen)</div>
<div class="codeline">{</div>
<div class="codeline"> <span class="keyword">unsigned</span> offset = 0;</div>
<div class="codeline"> <span class="keyword">while</span> (bytelen >= 4) {</div>
<div class="codeline"> <span class="keyword">uint32_t</span> u = *((<span class="keyword">uint32_t</span> *) (a + offset));</div>
<div class="codeline"> <span class="keyword">uint32_t</span> v = *((<span class="keyword">uint32_t</span> *) (b + offset));</div>
<div class="codeline"> <span class="keyword">uint32_t</span> x = u ^ v;</div>
<div class="codeline"> <span class="keyword">uint32_t</span> left = x & 0xaaaaaaaau, right = x & 0x55555555;</div>
<div class="codeline"> <span class="keyword">uint32_t</span> result = (left | (left >> 1)) | (right | (right << 1));</div>
<div class="codeline"> *((<span class="keyword">uint32_t</span> *) (a + offset)) |= result;</div>
<pre>
</pre>
<div class="codeline"> offset += 4;</div>
<div class="codeline"> bytelen -= 4;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">while</span> (bytelen) {</div>
<div class="codeline"> <span class="keyword">uint8_t</span> u = a[offset];</div>
<div class="codeline"> <span class="keyword">uint8_t</span> v = b[offset];</div>
<div class="codeline"> <span class="keyword">uint8_t</span> x = u ^ v;</div>
<div class="codeline"> <span class="keyword">uint32_t</span> left = x & 0xaa, right = x & 0x55;</div>
<div class="codeline"> <span class="keyword">uint32_t</span> result = (left | (left >> 1)) | (right | (right << 1));</div>
<div class="codeline"> a[offset] |= result;</div>
<pre>
</pre>
<div class="codeline"> offset++;</div>
<div class="codeline"> bytelen--;</div>
<div class="codeline"> }</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-11">11</a>
</div>
<a name="section-14"></a>
<div class="section" id="section-14">
<p><span class="sectionnumber">14</span>
Updating the type vectors for an operation</p>
<p>
Each operation may require certain types as inputs and may mutate the type of
outputs. We model this in a function which operates on type vectors.
</p>
<p>
At any point, if an operation would encounter a type error, we return 0.
</p>
<div class="codename">Type vector updating</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> <span class="keyword">char</span> lsmsb_op_type_vector_update(<span class="keyword">uint8_t</span> *tv, <span class="keyword">uint32_t</span> op,</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">struct</span> lsmsb_value *constants,</div>
<div class="codeline"> <span class="keyword">unsigned</span> num_constants,</div>
<div class="codeline"> <span class="keyword">unsigned</span> num_spills) {</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">enum</span> lsmsb_opcode opcode = lsmsb_op_opcode_get(op);</div>
<div class="codeline"> <span class="keyword">unsigned</span> reg1, reg2, reg3;</div>
<div class="codeline"> <span class="keyword">enum</span> lsmsb_type type1, type2, type3;</div>
<div class="codeline"> <span class="keyword">unsigned</span> c1, s1;</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">switch</span> (opcode) {</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_MOV:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> reg2 = lsmsb_op_reg2_get(op);</div>
<div class="codeline"> type2 = lsmsb_type_vector_reg_get(tv, reg2);</div>
<div class="codeline"> <span class="keyword">if</span> (!lsmsb_type_is_value(type2))</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> lsmsb_type_vector_reg_set(tv, reg1, type2);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_LDI:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> lsmsb_type_vector_reg_set(tv, reg1, LSMSB_TYPE_U32);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_LDC:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> c1 = lsmsb_op_constant1_get(op);</div>
<div class="codeline"> <span class="keyword">if</span> (c1 >= num_constants)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> type1 = lsmsb_constant_type_get(&constants[c1]);</div>
<div class="codeline"> lsmsb_type_vector_reg_set(tv, reg1, type1);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_RET:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_type_vector_reg_get(tv, reg1) != LSMSB_TYPE_U32)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_JMP:</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_SPILL:</div>
<div class="codeline"> s1 = lsmsb_op_spill1_get(op);</div>
<div class="codeline"> <span class="keyword">if</span> (s1 >= num_spills)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> reg1 = lsmsb_op_reg3_get(op);</div>
<div class="codeline"> type1 = lsmsb_type_vector_reg_get(tv, reg1);</div>
<div class="codeline"> <span class="keyword">if</span> (!lsmsb_type_is_value(type1))</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> lsmsb_type_vector_spill_set(tv, s1, type1);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_UNSPILL:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> s1 = lsmsb_op_spill2_get(op);</div>
<div class="codeline"> <span class="keyword">if</span> (s1 >= num_spills)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> type1 = lsmsb_type_vector_spill_get(tv, s1);</div>
<div class="codeline"> <span class="keyword">if</span> (!lsmsb_type_is_value(type1))</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> lsmsb_type_vector_reg_set(tv, reg1, type1);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_JNZ:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_JZ:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_type_vector_reg_get(tv, reg1) != LSMSB_TYPE_U32)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_EQ:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_NE:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_GT:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_LT:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_GTE:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_LTE:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_AND:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_OR:</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_XOR:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> reg2 = lsmsb_op_reg2_get(op);</div>
<div class="codeline"> reg3 = lsmsb_op_reg3_get(op);</div>
<div class="codeline"> type2 = lsmsb_type_vector_reg_get(tv, reg2);</div>
<div class="codeline"> type3 = lsmsb_type_vector_reg_get(tv, reg3);</div>
<div class="codeline"> <span class="keyword">if</span> (type2 != LSMSB_TYPE_U32 || type3 != LSMSB_TYPE_U32)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> lsmsb_type_vector_reg_set(tv, reg1, LSMSB_TYPE_U32);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">case</span> LSMSB_OPCODE_ISPREFIXOF:</div>
<div class="codeline"> reg1 = lsmsb_op_reg1_get(op);</div>
<div class="codeline"> reg2 = lsmsb_op_reg2_get(op);</div>
<div class="codeline"> reg3 = lsmsb_op_reg3_get(op);</div>
<div class="codeline"> type2 = lsmsb_type_vector_reg_get(tv, reg2);</div>
<div class="codeline"> type3 = lsmsb_type_vector_reg_get(tv, reg3);</div>
<div class="codeline"> <span class="keyword">if</span> (type2 != LSMSB_TYPE_BYTESTRING ||</div>
<div class="codeline"> type3 != LSMSB_TYPE_BYTESTRING)</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> lsmsb_type_vector_reg_set(tv, reg1, LSMSB_TYPE_U32);</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> <span class="keyword">default</span>:</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> }</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-11">11</a>
</div>
<a name="section-15"></a>
<div class="section" id="section-15">
<p><span class="sectionnumber">15</span>
Type vector arrays</p>
<p>
A <i>type vector array</i> is a simply an array of type vectors, one for each
operation in the rule table. We can build a type vector array by iterating over
the operations, unifying the type vectors for all predecessors and then
updating the type vector for that operation.
</p>
<p>
By the end we will have either found a type error or proved that evaluating the
rule table will not encounter any run-time errors.
</p>
<div class="codename">Type vector arrays</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> <span class="keyword">char</span> lsmsb_type_vector_array_fill(<span class="keyword">uint8_t</span> *tva,</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint16_t</span> *ptable,</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint8_t</span> *context_tv,</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">struct</span> lsmsb_filter *filter) {</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> tva_width = lsmsb_filter_tva_width(filter);</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint32_t</span> *ops = filter->operations;</div>
<div class="codeline"> <span class="keyword">unsigned</span> i, j;</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (filter->num_operations == 0)</div>
<div class="codeline"> return 1;</div>
<pre>
</pre>
<div class="codeline"> memset(tva, 0, tva_width); /* set the first row to LSMSB_TYPE_UNDEF */</div>
<div class="codeline"> memcpy(tva, context_tv, LSMSB_NUM_REGISTERS / 4);</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (!lsmsb_op_type_vector_update(tva, ops[0], filter->constants,</div>
<div class="codeline"> filter->num_constants,</div>
<div class="codeline"> filter->num_spill_slots)) {</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">for</span> (i = 1; i < filter->num_operations; ++i) {</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint16_t</span> *ptable_row =</div>
<div class="codeline"> &ptable[i * LSMSB_PREDECESSOR_TABLE_WIDTH];</div>
<div class="codeline"> <span class="keyword">uint8_t</span> *tva_row = tva + i * tva_width;</div>
<div class="codeline"> <span class="keyword">char</span> found_predecessor = 0;</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (ptable_row[0] == LSMSB_PREDECESSOR_TABLE_OVERFLOW) {</div>
<div class="ref"> <span class="reftext">handle overflowed row</span> <a href="#section-17" class="refnum">17</a></div>
<div class="codeline"> } else {</div>
<div class="ref"> <span class="reftext">handle normal row</span> <a href="#section-16" class="refnum">16</a></div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (!lsmsb_op_type_vector_update(tva_row, ops[i],</div>
<div class="codeline"> filter->constants,</div>
<div class="codeline"> filter->num_constants,</div>
<div class="codeline"> filter->num_spill_slots)) {</div>
<div class="codeline"> return 0;</div>
<div class="codeline"> }</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> return 1;</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-11">11</a>
</div>
<a name="section-16"></a>
<div class="section" id="section-16">
<p><span class="sectionnumber">16</span>
Unifying normal rows</p>
<p>
With a row where the predecessor table didn't overflow, we can easily find the
predecessor operations. For the first one, we need to copy its type vector. For
all subsequent predecessors, we unify the current type vector with their
type vector.
</p>
<p>
Here we also deal with the case of an operation with no predecessor. This can
only occur if no control flow path leads to it. In this case, we reject the
rule table. We assume that the tools used to generate the rule tables are
sufficiently smart to remove dead code and we don't want to waste kernel memory
keeping it around.
</p>
<div class="codename">handle normal row</div>
<div class="code">
<div class="codeline"><span class="keyword">for</span> (j = 0; j < LSMSB_PREDECESSOR_TABLE_WIDTH; ++j) {</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> p = ptable_row[j];</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint8_t</span> *tva_row_p = tva + p * tva_width;</div>
<div class="codeline"> <span class="keyword">if</span> (p == LSMSB_PREDECESSOR_TABLE_INVAL)</div>
<div class="codeline"> break;</div>
<div class="codeline"> <span class="keyword">if</span> (!found_predecessor) {</div>
<div class="codeline"> memcpy(tva_row, tva_row_p, tva_width);</div>
<div class="codeline"> found_predecessor = 1;</div>
<div class="codeline"> continue;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> lsmsb_type_vector_unify(tva_row, tva_row_p,</div>
<div class="codeline"> tva_width);</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">if</span> (!found_predecessor)</div>
<div class="codeline"> return 0; <span class="comment">// Dead code.</span></div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-15">15</a>
</div>
<a name="section-17"></a>
<div class="section" id="section-17">
<p><span class="sectionnumber">17</span>
Unifying overflow rows</p>
<p>
When the predecessor table is marked as overflowed, we have to find all the
predecessors ourselves. We can do this by checking all previous operations for
jumps to the current operation and checking the previous instruction for fall
though. (Recall that we only have forward jumps.)
</p>
<div class="codename">handle overflowed row</div>
<div class="code">
<div class="codeline"><span class="keyword">for</span> (j = 0; j < i; ++j) {</div>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_op_is_predecessor_of(ops, j, i)) {</div>
<div class="codeline"> <span class="keyword">if</span> (!found_predecessor) {</div>
<div class="codeline"> memcpy(tva_row,</div>
<div class="codeline"> tva + j * tva_width,</div>
<div class="codeline"> tva_width);</div>
<div class="codeline"> found_predecessor = 1;</div>
<div class="codeline"> continue;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> lsmsb_type_vector_unify(</div>
<div class="codeline"> tva_row,</div>
<div class="codeline"> tva + j * tva_width,</div>
<div class="codeline"> tva_width);</div>
<div class="codeline"> }</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">if</span> (!found_predecessor)</div>
<div class="codeline"> return 0; <span class="comment">// shouldn't ever happen</span></div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-15">15</a>
</div>
<a name="section-18"></a>
<div class="section" id="section-18">
<p><span class="sectionnumber">18</span>
Testing for predecessor rules</p>
<div class="codename">Predecessor testing function</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> <span class="keyword">char</span> lsmsb_op_is_predecessor_of(<span class="keyword">const</span> <span class="keyword">uint32_t</span> *ops, <span class="keyword">unsigned</span> i,</div>
<div class="codeline"> <span class="keyword">unsigned</span> target) {</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint32_t</span> op = ops[i];</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">enum</span> lsmsb_opcode opcode = lsmsb_op_opcode_get(op);</div>
<div class="codeline"> </div>
<div class="codeline"> <span class="keyword">if</span> (i == target - 1 &&</div>
<div class="codeline"> lsmsb_opcode_falls_through(opcode)) {</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (lsmsb_opcode_is_jump(opcode) &&</div>
<div class="codeline"> lsmsb_op_jump_length(op) + i == target) {</div>
<div class="codeline"> return 1;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> return 0;</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-11">11</a>
</div>
<a name="section-19"></a>
<div class="section" id="section-19">
<p><span class="sectionnumber">19</span>
Type vector utility functions</p>
<div class="codename">Type vector utility functions</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">unsigned</span> lsmsb_filter_tva_width(<span class="keyword">const</span> <span class="keyword">struct</span> lsmsb_filter *filter)</div>
<div class="codeline">{</div>
<div class="codeline"> return (LSMSB_NUM_REGISTERS + filter->num_spill_slots + 3) / 4;</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">enum</span> lsmsb_type lsmsb_type_vector_reg_get(<span class="keyword">uint8_t</span> *vector, <span class="keyword">unsigned</span> reg)</div>
<div class="codeline">{</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> byte = reg >> 2;</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> index = reg & 3;</div>
<pre>
</pre>
<div class="codeline"> return (<span class="keyword">enum</span> lsmsb_type) ((vector[byte] >> (6 - (index * 2))) & 3);</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">enum</span> lsmsb_type lsmsb_type_vector_spill_get(<span class="keyword">uint8_t</span> *vector, <span class="keyword">unsigned</span> slot)</div>
<div class="codeline">{</div>
<div class="codeline"> return lsmsb_type_vector_reg_get(vector, slot + LSMSB_NUM_REGISTERS);</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> void lsmsb_type_vector_reg_set(<span class="keyword">uint8_t</span> *vector, <span class="keyword">unsigned</span> reg,</div>
<div class="codeline"> <span class="keyword">enum</span> lsmsb_type newtype)</div>
<div class="codeline">{</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> byte = reg >> 2;</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> index = reg & 3;</div>
<div class="codeline"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">uint8_t</span> masks[4] = { 0x3f, 0xcf, 0xf3, 0xfc };</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint8_t</span> new_value = (vector[byte] & masks[index]) |</div>
<div class="codeline"> newtype << (6 - (index * 2));</div>
<div class="codeline"> vector[byte] = new_value;</div>
<div class="codeline">}</div>
<pre>
</pre>
<div class="codeline"><span class="keyword">static</span> <span class="keyword">inline</span> void lsmsb_type_vector_spill_set(<span class="keyword">uint8_t</span> *vector, <span class="keyword">unsigned</span> slot,</div>
<div class="codeline"> <span class="keyword">enum</span> lsmsb_type newtype)</div>
<div class="codeline">{</div>
<div class="codeline"> lsmsb_type_vector_reg_set(vector, slot + LSMSB_NUM_REGISTERS, newtype);</div>
<div class="codeline">}</div>
</div>
<div class="sectionrefs">
<a class="sectionref" href="#section-11">11</a>
</div>
<a name="section-20"></a>
<div class="section" id="section-20">
<p><span class="sectionnumber">20</span>
Bringing typechecking together</p>
<p>
Pulling together the above code is straight-forward. We allocate memory for the
predecessor table and type vector array and generate them both. If we manage to
generate them both and the rule table checks out, then we're done.
</p>
<div class="codename">Typecheck function</div>
<div class="code">
<div class="codeline"><span class="keyword">static</span> <span class="keyword">int</span> lsmsb_filter_typecheck(<span class="keyword">const</span> <span class="keyword">struct</span> lsmsb_filter *filter,</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">uint8_t</span> *context_type_vector)</div>
<div class="codeline">{</div>
<div class="codeline"> <span class="keyword">const</span> <span class="keyword">unsigned</span> tva_width = lsmsb_filter_tva_width(filter);</div>
<div class="codeline"> <span class="keyword">uint16_t</span> *predecessor_table;</div>
<div class="codeline"> <span class="keyword">uint8_t</span> *type_vector_array;</div>
<div class="codeline"> <span class="keyword">int</span> return_code = -EINVAL;</div>
<pre>
</pre>
<div class="codeline"> predecessor_table = (uint16_t*) kmalloc(</div>
<div class="codeline"> filter->num_operations *</div>
<div class="codeline"> LSMSB_PREDECESSOR_TABLE_WIDTH *</div>
<div class="codeline"> sizeof(<span class="keyword">uint16_t</span>), GFP_KERNEL);</div>
<div class="codeline"> <span class="keyword">if</span> (!predecessor_table)</div>
<div class="codeline"> return -ENOMEM;</div>
<div class="codeline"> type_vector_array = kmalloc(filter->num_operations *</div>
<div class="codeline"> tva_width, GFP_KERNEL);</div>
<div class="codeline"> <span class="keyword">if</span> (!type_vector_array) {</div>
<div class="codeline"> kfree(predecessor_table);</div>
<div class="codeline"> return -ENOMEM;</div>
<div class="codeline"> }</div>
<pre>
</pre>
<div class="codeline"> <span class="keyword">if</span> (!lsmsb_predecessor_table_fill(predecessor_table, filter->operations,</div>
<div class="codeline"> filter->num_operations)) {</div>
<div class="codeline"> goto exit;</div>
<div class="codeline"> }</div>
<pre>