Commit c31cab2
committed
fix(utxo): a corrupt record could panic the amount decoder
`decompress_amount` ended in a `while` loop multiplying by ten up to nine
times, with no bound on its input. `read_varint` hands it whatever a record
contains, and `validate_encoded` runs it over every output of every record
loaded from a snapshot, so a file on disk could reach it with an arbitrary
`u64`. `decompress_amount(u64::MAX)` is 2.05e22 — a panic in a debug build, a
silent wrap in a release one.
Reproduced before fixing:
`an_absurd_compressed_amount_is_rejected_rather_than_overflowing` failed with
`attempt to multiply with overflow` from `compress.rs:193`.
The function now returns `Option` and requires the decompressed value back
inside the compressible domain. That also closes a canonicality hole that was
open until now: the compact form may encode only amounts the escape refuses,
and the escape refuses exactly the amounts the compact form covers, so every
amount has one spelling and no other. `decompress_accepts_exactly_the_encoder_image`
states it as a property over every `u64` — whatever the decoder accepts must
round-trip back to the same compressed value through the encoder, which pins
the accepted set to the encoder's image rather than merely checking that
nothing panics.
Found while investigating the fixed ~12 ns `find_output` cost, which is what
the same loop was suspected of. Replacing it with a power-of-ten lookup does
help, but only 12.5 ns -> 11.3 ns (1.1x): the remaining fixed cost is the
directory read and building the returned view, not the arithmetic. Quoted that
way in the doc rather than as a speed win, and the crossover table is
re-measured.
The `miss` arms are unchanged at every record size, which is the control this
wants: that path never decompresses an amount, so a change to the amount
transform must not move it.1 parent 7d3c948 commit c31cab2
4 files changed
Lines changed: 131 additions & 25 deletions
File tree
- crates/utxo
- proptest-regressions
- src
- docs/benchmarks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
167 | | - | |
| 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 | + | |
168 | 198 | | |
169 | | - | |
| 199 | + | |
170 | 200 | | |
171 | 201 | | |
172 | | - | |
173 | | - | |
174 | 202 | | |
175 | | - | |
176 | | - | |
177 | 203 | | |
178 | | - | |
| 204 | + | |
179 | 205 | | |
180 | 206 | | |
181 | 207 | | |
182 | 208 | | |
183 | 209 | | |
184 | 210 | | |
185 | 211 | | |
186 | | - | |
| 212 | + | |
187 | 213 | | |
188 | | - | |
| 214 | + | |
189 | 215 | | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
197 | 220 | | |
198 | 221 | | |
199 | 222 | | |
| |||
324 | 347 | | |
325 | 348 | | |
326 | 349 | | |
327 | | - | |
| 350 | + | |
328 | 351 | | |
329 | 352 | | |
330 | 353 | | |
| |||
362 | 385 | | |
363 | 386 | | |
364 | 387 | | |
365 | | - | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
366 | 405 | | |
367 | 406 | | |
368 | 407 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1472 | 1472 | | |
1473 | 1473 | | |
1474 | 1474 | | |
1475 | | - | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
1476 | 1479 | | |
1477 | 1480 | | |
1478 | 1481 | | |
| |||
2013 | 2016 | | |
2014 | 2017 | | |
2015 | 2018 | | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
| 2030 | + | |
| 2031 | + | |
| 2032 | + | |
| 2033 | + | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
| 2039 | + | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
| 2043 | + | |
2016 | 2044 | | |
2017 | 2045 | | |
2018 | 2046 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
159 | 159 | | |
160 | | - | |
| 160 | + | |
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
166 | 172 | | |
167 | 173 | | |
168 | 174 | | |
| |||
233 | 239 | | |
234 | 240 | | |
235 | 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 | + | |
236 | 267 | | |
237 | 268 | | |
238 | 269 | | |
| |||
0 commit comments