Repository navigation
Commit 718b019
authored
bugc: revert on out-of-bounds indexing (#344)
* bugc: add an assert instruction that reverts with Panic
An `assert` instruction takes a condition and a panic code. Code
generation jumps over a revert when the condition holds; otherwise the
code reverts with Solidity's `Panic(uint256)` error data, and the
REVERT carries a `revert` context with the panic code and a pointer
to that data. Dead code elimination keeps asserts, and constant
folding removes an assert whose condition is a true constant.
* bugc: give an array literal a memory reference type
An array literal's value had a scalar type, so code generation took its
length as the scalar's size: [1, 2, 3].length was 32.
* bugc: require an unsigned integer index
As in Solidity, an array or bytes index and a slice bound must be an
unsigned integer. Before, a signed index compiled, and since #339 its
comparisons are signed, so -1 would pass a bounds check.
* bugc: revert on out-of-bounds indexing
Indexing an array or bytes in memory, or a fixed-size array in
storage, now reverts with Panic(0x32) when the index is not less than
the length, as Solidity does. This holds for reads and writes, at
every level of a nested array. A slice x[a:b] reverts unless
a <= b <= x.length.
The check compares as unsigned words whatever the index's type, so a
negative value cast to an index is a huge index and reverts.
Before, an index past the end read or wrote whatever memory followed:
writing m[0][1] on [[1], [2]] overwrote m[1]'s length word.
The locals soundness test that sliced msg.data[0:4] runs with no
calldata, so it slices msg.data[0:0] now.
* bugc: document bounds checks1 parent be3ee87 commit 718b019
24 files changed
Lines changed: 855 additions & 26 deletions
File tree
- packages
- bugc
- src
- evmgen
- analysis
- debug
- generation
- instructions
- irgen/generate
- expressions
- ir
- analysis
- spec
- utils
- optimizer/steps
- typechecker
- web/docs/implementation-guides/compiler
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
53 | 65 | | |
54 | 66 | | |
55 | 67 | | |
| |||
187 | 199 | | |
188 | 200 | | |
189 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
190 | 205 | | |
191 | 206 | | |
192 | 207 | | |
| |||
240 | 255 | | |
241 | 256 | | |
242 | 257 | | |
| 258 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
287 | 290 | | |
288 | 291 | | |
289 | 292 | | |
| |||
338 | 341 | | |
339 | 342 | | |
340 | 343 | | |
| 344 | + | |
341 | 345 | | |
342 | 346 | | |
343 | 347 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| 268 | + | |
268 | 269 | | |
269 | 270 | | |
270 | 271 | | |
| |||
356 | 357 | | |
357 | 358 | | |
358 | 359 | | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
359 | 363 | | |
360 | 364 | | |
361 | 365 | | |
| |||
412 | 416 | | |
413 | 417 | | |
414 | 418 | | |
| 419 | + | |
415 | 420 | | |
416 | 421 | | |
417 | 422 | | |
| |||
0 commit comments