Debian Codegraph @

doc/internal/bytecode.html

raw symbols SCIP
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Cyrus IMAP Server: Sieve Bytecode</title>
6 </head>
7 <body>
8 <h1>Cyrus IMAP Server: Sieve Bytecode</h1>
9
10 <h2>Motivation</h2>
11
12 <p>The motivation behind moving to Sieve Bytecode is severalfold:</p>
13
14 <ul>
15 <li>Parsing a script at each execution is expensive computationally</li>
16
17 <li>Lex/Yacc are costly in terms of memory usage and executable size, whereas
18 a bytecode parser is much lighter weight.</li>
19
20 <li>Using bytecode can simplify the code for the execution phase,
21 which is far more frequently occurring than the upload/compile phase.</li>
22
23 <li>Rewriting a significant part of the sieve execution framework forces
24 a decent amount of refactoring on what has traditionally been a problematic
25 part of the Cyrus code base. There is still work to do in this area.</li>
26
27 </ul>
28
29 <h2>Overall Bytecode Format</h2>
30
31 <p>In the final bytecode, each opcode/parameter is aligned on a 4-byte boundary.
32 Strings are NUL-terminated (and padded to a 4-byte boundary as needed).</p>
33
34 <p>Ideally, we'd have all integers in network byte order, so as to make the
35 scripts portable, but version 1 does not have this feature.</p>
36
37 <p>At the beginning of the file, there is a magic header to identify it as
38 a bytecode file, and a 4 byte version number. Immediately following the
39 version number are the opcodes that relate to the script.</p>
40
41 <h2>Generation</h2>
42
43 <p>A Sieve Bytecode file is generated in three "passes":</p>
44
45 <ul>
46
47 <li>Generate a parse tree using lex/yacc from the sieve script. (addr.y,
48 addr-lex.l, sieve.y, sieve-lex.l).</li>
49
50 <li>Serialize the parse tree into an intermediate form, where strings
51 are held separate from the rest of the representation. (bc_generate.c)</li>
52
53 <li>Serialize the intermediate form into the final bytecode. (bc_emit.c)</li>
54
55 </ul>
56
57 <p>The intermediate form is an array of bytecode_t unions, with strings
58 located elsewhere in memory. The entry point is bc_generate: sieve_generate_bytecode()
59 / bc_action_generate().</p>
60
61 <p>bc_action_generate traverses the commandlist_t tree and emits opcodes in
62 sequence.</p>
63
64 <p>Simple actions (STOP, DISCARD, KEEP, MARK, UNMARK) have no arguments,
65 and processing proceeds directly.</p>
66
67 <p>More complicated options have a sequence of arguments that are emitted
68 following the initial opcode.</p>
69
70 <p>For example, single argument commands such as REJECT, FILEINTO, REDIRECT
71 are followed by a bytecode_t for a string's length, and then a bytecode_t
72 which contains a pointer to a string.</p>
73
74 <p>Commands such as ADDFLAG, SETFLAG, REMOVEFLAG, which take a stringlist,
75 format the stringlist as (using bc_stringlist_generate):</p>
76
77 <pre>
78 {Number of Strings}{String 1 Length}{String 1 Ptr}{String 2 Length}....
79 </pre>
80
81 <p>So their resulting final output would appear as:</p>
82
83 <pre>
84 {Opcode}{...stringlist from above...}
85 </pre>
86
87 <p>Even more complicated action opcodes (vacation, notify) etc,
88 may take a sequence of integer values (flags), stringlists, or individual
89 strings. These are more specifically documented in the code.</p>
90
91 <p>This leaves us with the IF keyword (and tests).
92 In the pass 1 form, IF appears as the following bytecode_t structures:</p>
93
94 <pre>
95 {IF opcode}
96 {Beginning of the then block}
97 {End of the then block / beginning of the else block}
98 {End of the else block / -1 for no else block}
99 {....test opcodes....}
100 {....'then' action opcodes....}
101 {....'else' action opcodes.... [optional]}
102 </pre>
103
104 <p>Test opcodes are generated by the bc_test_generate function, which
105 is very similar to bc_action_generate (tests without arguments are
106 just opcodes, tests with arguments have them serialized into place
107 directly following the original opcode). Test lists are represented
108 as {number of tests}{address of the end of the list}{test 1}{test 2}.</p>
109
110 <p>In the third pass, strings are serialized into place, and if statement
111 jumps are resolved to actual addresses within the file This is done
112 in bc_emit: sieve_emit_bytecode / bc_action_emit.</p>
113
114 <p>This results in a totally serialized representation, using byte offsets
115 within the file instead of indexes into the array of bytecode_t's. In
116 addition to the manipulations that are necessary to do this, there are several
117 other changes in format.</p>
118
119 <p>Two new opcodes exist: NULL and JUMP (which performs an unconditional
120 jump).</p>
121
122 <p>Stringlists and testslists now include a precomputed byte length of
123 the entire list, so it can be skipped over as needed.</p>
124
125 <p>So as to be executable without a stack, the IF statements are designed as
126 follows:</p>
127
128 <pre>
129 {IF opcode}
130 {....test block....}
131 {JUMP (location of false condition) }
132 {....then block....}
133 {(if there is an else) JUMP (end of else block)}
134 {(if there is an else) .... else block ....}
135 </pre>
136
137 <p>The idea being that if the test is true, the instruction pointer should move
138 to the then block, otherwise the else block will be hit automatically
139 (due to the unconditional jump).</p>
140
141 <h2>Evaluation</h2>
142
143 <p>The evaluation routines are in bc_eval.c, the basic idea is that we can
144 simply mmap the bytecode, run straight through it, and complete the processing
145 without maintaining a stack.</p>
146
147 <p>The processing is done by overlaying a bytecode_input_t array over the mmap.
148 This allows addressing elements within the file to be simple. There is
149 an instruction pointer which is incremented as each action is performed,
150 or as actions/tests are skipped.</p>
151
152 <p>Of special note in bc_eval.c is the unwrap_string function which
153 will pull a string out of the bytecode, and return the instruction
154 pointer at the end of the string.</p>
155
156
157 <h2>Other things to consider</h2>
158
159 <p>The Bytecode can be extended to contain other extensions. This could require
160 regeneration of older scripts. In many cases this can be avoided by putting
161 the new commands at the end of the proper enum in bytecode.h</p>
162
163 </body>
164 </html>