Debian Codegraph @

crt/s2n/tests/sidetrail/DEBUGGING.md

raw symbols SCIP
1 # Debugging SideTrail
2
3 This is a guide for what to do when SideTrail reports a potential timing violation / throws a red `x` in CI.
4
5 <!-- START doctoc generated TOC please keep comment here to allow auto update -->
6 <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7
8
9 - [How to locally rerun the tests](#how-to-locally-rerun-the-tests)
10 - [Updating the patch files](#updating-the-patch-files)
11 - [Undefined functions](#undefined-functions)
12 - [Loop related errors](#loop-related-errors)
13 - [Warning: No entrypoints found.](#warning-no-entrypoints-found)
14 - [SideTrail fails with a long error trace](#sidetrail-fails-with-a-long-error-trace)
15 - [What to do if the proof gets really slow](#what-to-do-if-the-proof-gets-really-slow)
16 - [Expected runtimes:](#expected-runtimes)
17
18 <!-- END doctoc generated TOC please keep comment here to allow auto update -->
19
20 ## How to locally rerun the tests
21
22 The first step to debugging is to locally rerun the failing proof(s).
23 Follow [the instructions here](README.md#how-to-execute-the-tests) to get SideTrail docker container and execute the tests.
24
25 ## Updating the patch files
26
27 The most common error you are likely to see is a failure to apply the necessary patches.
28 Sidetrail depends on a set of patch files, stored in the `s2n/tests/sidetrail/working/patches`.
29 These files patch away certain C constructs that SideTrail has trouble with.
30 They are applied by the `copy_as_needed.sh` script in each proof.
31 If the file they are patching is modified, `patch` may be unable to apply the needed patches, and fail with an error.
32
33 ```
34 patching file utils/s2n_safety.c
35 Hunk #1 FAILED at 57.
36 1 out of 2 hunks FAILED -- saving rejects to file utils/s2n_safety.c.rej
37 ```
38
39 To fix this, regenerate the patch file.
40
41 1. View the failing patch to see what it does.
42 It may be useful to apply it to a previous version of the file, so you can see what the patched file looks like.
43 Identify any failing chunks, and figure out why they are failing to apply.
44
45 2. In a clean branch of s2n, modify the file in need of patching to have the required changes.
46 Work directly on the file in the s2n src folders - we will revert it back before committing our changes.
47 You may find it useful to also (temporarily) modify the `copy_as_needed.sh` file [(example)](https://github.com/awslabs/s2n/blob/main/tests/sidetrail/working/s2n-cbc/copy_as_needed.sh) to just copy the file, and not patch it.
48
49 3. Iterate on the file in need of patching until the tests pass.
50
51 4. In the `s2n/tests/sidetrail/working/patches` folder, regenerate the patch using `git diff -u <path-to-changed-file> > <patch_filename>.patch`
52
53 5. Restore all other changed files using `git checkout`.
54
55 6. Some of the current patch files were not generated following this standard procedure, and hence are invoked using `-p5`, instead of the `-p1` this procedure will generate.
56 You may need to change the `./copy_as_needed.sh` files to use the correct `-p` level.
57
58 7. Rerun the proofs using the docker container to ensure they work.
59
60 8. Commit the new patch file, and attach it to your PR.
61
62 ## Undefined functions
63
64 Another common error comes if a new function is called, for which SideTrail does not have a definition.
65 In this case, you will see a warning like:
66
67 ```
68 warning: module contains undefined functions: malloc, __CONTRACT_invariant, nondet, foo
69 ```
70
71 The list above is the list of functions for which SMACK does not have a model.
72 In some cases, this is expected and benign --- you can find a list of `allowed_undefined` functions [here](https://github.com/awslabs/s2n/blob/main/tests/sidetrail/count_success.pl#L33).
73 If there are any functions in the warning that are not in that list, then one of three things needs to happen.
74
75 1. The new function does not need a timing stub, and should be added to the list of `allowed_undefined`.
76
77 1. The new function is in a file that SideTrail doesn't currently compile.
78 Update `Makefile` and `copy_as_needed.sh` for the proof, and rerun.
79
80 1. The new function needs a timing stub.
81 Take a look at existing timing stubs in the `working/stubs` folder to see what these look like, and how to write them.
82
83 ## Loop related errors
84
85 When Sidetrail analyzes a program, it needs to know how many times a loop will execute.
86 If Sidetrail encounters a loop, and can't tell how many times it will execute, it may give a spurious counterexample.
87 You can fix this using the `S2N_INVARIANT` macro.
88 A good example of this is in `s2n_constant_time_equals`.
89
90 ```C
91 uint8_t xor = 0;
92 for (uint32_t i = 0; i < len; i++) {
93 /* Invariants must hold for each execution of the loop
94 * and at loop exit, hence the <= */
95 S2N_INVARIANT(i <= len);
96 xor |= a[i] ^ b[i];
97 }
98 ```
99
100 ## Warning: No entrypoints found.
101
102 For unknown reasons, this warning sometimes appears in CI, although the same docker container does not display it when run locally.
103 This appears to be benign, although we are investigating and hope to close it out.
104
105 ## SideTrail fails with a long error trace
106
107 Your best bet here is to do delta-debugging to get a minimal reproducing test case.
108 - Take a look at the test-case.
109 - Did it introduce a timing violation?
110 - If so, fix it.
111
112 If you can't see an obvious timing issue in the minimal reproducing case, it may be worth taking a look at the trace.
113 The trace is not easy to read, and is super long.
114 I've found the best thing is to dump it to a file, and then to start annotating that file with the call-stack that led to the failure.
115 In particular, the trace will consist of two sequential traces: the initial, and the "shadow" trace.
116 Those should be equal - if they are not, that's where the timing violation comes in.
117 Look for the place where the two traces diverged.
118 It might be helpful to add/uncomment `printModel += true` in the `Makefile`.
119
120 An example failure trace on `s2n-cbc`:
121
122 ```
123 $ ./clean && ./run.sh
124 .
125 .
126 .
127 __ __ _ __
128 \ \ / /__ _ __(_)/ _|_ _
129 \ \ / / _ \ '__| | |_| | | |
130 \ V / __/ | | | _| |_| |
131 \_/ \___|_| |_|_| \__, |
132 |___/
133 [email protected]
134
135 + boogie /doModSetAnalysis [email protected]
136 Boogie program verifier version 2.3.0.61016, Copyright (c) 2003-2014, Microsoft.
137 [email protected](634,3): Error BP5001: This assertion might not hold.
138 Execution trace:
139 [email protected](630,3): anon0
140 [email protected](121,40): inline$simple_cbc_wrapper$0$Entry
141 .
142 .
143 .
144 [email protected](2999,1): inline$s2n_verify_cbc.shadow$0$$bb20
145 [email protected](691,1): inline$simple_cbc_wrapper.shadow$0$$bb0$13
146 [email protected](630,3): anon0$2
147
148 Boogie program verifier finished with 0 verified, 1 error
149
150 real 0m20.076s
151 user 0m20.322s
152 sys 0m1.000s
153 ```
154
155 The violated assertion (line 634 of [email protected]) is:
156
157 ```
158 assert ($l <= ($l.shadow + 68));
159 ```
160
161 and the bound `68` appears to be specified using the `__VERIFIER_ASSERT_MAX_LEAKAGE` constant:
162
163 ```
164 procedure {:entrypoint} {:cost_modeling} simple_cbc_wrapper.wrapper($i0: i32, $i0.shadow: i32, $i1: i32, $i1.shadow: i32, $p2: ref, $p2.shadow: ref, $p3: ref, $p3.shadow: ref) returns ($r: i32, $r.shadow: i32)
165 requires {:__VERIFIER_ASSERT_MAX_LEAKAGE 68} true;
166 requires {:public_in $i0} true;
167 requires {:public_in $i1} true;
168 requires ($i0 == $i0.shadow);
169 requires ($i1 == $i1.shadow);
170 {
171
172 call $r := simple_cbc_wrapper($i0, $i1, $p2, $p3);
173 call $r.shadow := simple_cbc_wrapper.shadow($i0.shadow, $i1.shadow, $p2.shadow, $p3.shadow);
174 assume ($l >= $l.shadow);
175 $__delta := ($l - $l.shadow);
176 assert ($l <= ($l.shadow + 68));
177 return;
178 }
179 ```
180
181 Now if we rerun the prover, we would get a counterexample model as well,
182 and we can inspect the difference between `$l` and `$l.shadow` in this model:
183
184 ```
185 $ ./clean && ./run.sh
186 .
187 .
188 .
189 __ __ _ __
190 \ \ / /__ _ __(_)/ _|_ _
191 \ \ / / _ \ '__| | |_| | | |
192 \ V / __/ | | | _| |_| |
193 \_/ \___|_| |_|_| \__, |
194 |___/
195 [email protected]
196
197 + boogie /printModel 4 /doModSetAnalysis [email protected]
198 Boogie program verifier version 2.3.0.61016, Copyright (c) 2003-2014, Microsoft.
199 *** MODEL
200 $__delta@0 -> 69
201 $0 -> 0
202 .
203 .
204 .
205 $l.shadow@92 -> 29
206 $l.shadow@93 -> 29
207 $l.shadow@94 -> 30
208 $l.shadow@95 -> 31
209 $l.shadow@96 -> 31
210 $l.shadow@97 -> 31
211 $l.shadow@98 -> 32
212 $l.shadow@99 -> 32
213 $l@0 -> 0
214 $l@1 -> 0
215 $l@10 -> 1
216 $l@100 -> 33
217 $l@101 -> 33
218 $l@102 -> 33
219 $l@103 -> 33
220 $l@104 -> 33
221 $l@105 -> 33
222 $l@106 -> 33
223 $l@107 -> 33
224 .
225 .
226 .
227 ```
228
229 We see several different values for `$l` and `$l.shadow` at various points.
230 To see why the assertion failed, we need to check the maximum difference between `$l` and `$l.shadow`,
231 and if it exceeds our `__VERIFIER_ASSERT_MAX_LEAKAGE` bound.
232 For this particular case, the maximum difference in the model was close to `100`,
233 so bumping `__VERIFIER_ASSERT_MAX_LEAKAGE` up to `100` resolved the issue.
234
235 ## What to do if the proof gets really slow
236
237 1. Delta-debugging is your friend here.
238 Find the last fast commit.
239 Then, apply parts of the diff until it suddenly gets slow.
240 That's your culprit.
241 1. Slowdown is often because alias analysis has failed to distinguish cases that can't actually alias.
242 Sidetrail exports information about how many alias sets it had as part of its output.
243 Did that change from the last fast version?
244 In this case, sometimes simple syntactic changes are enough to fix it; if that doesn't work, consult an AR expert.
245
246
247 ## Expected runtimes:
248
249 | Test name | Runtime |
250 | --------------------------------- | ------: |
251 | s2n-record-read-cbc-negative-test | 20s |
252 | s2n-cbc | 5m 45s |
253 | s2n-record-read-composite | 15s |
254 | s2n-record-read-aead | 4m 10s |
255 | s2n-record-read-stream | 25s |
256 | s2n-record-read-cbc | 20s |
257