Commit 77a2746
kernel: Avoid out-of-range shifts in CopyBits
CopyBits and its helpers shift by amounts derived from <frombit>,
<tobit> and the alignment difference between the two blocks. Several of
those can reach BIPEB, where `1 << BIPEB` and `x >> BIPEB` are undefined:
* After a partial first destination word is filled, <frombit> can be
left equal to BIPEB rather than wrapping to the next source word.
MaskForCopyBits then evaluates `(UInt)1 << BIPEB`.
* In the main loop <frombit> equal to 0 makes the second half of the
word assembly shift by `BIPEB - 0`.
* CopyInWord shifts by <shift>. The callers do keep it in range, but
only indirectly: in the `tobit + BIPEB - frombit` case it takes the
branch condition `frombit + tailbits > BIPEB` to show frombit > tobit.
Normalise <frombit> to the start of the next word, make MaskForCopyBits
total so an out-of-range bound yields an empty mask, special-case the
aligned main loop, and bound the shift in CopyInWord directly.
An exhaustive sweep over frombit, tobit in [0,BIPEB) and nbits in
[1,5*BIPEB] hits the undefined shift in 1953 of 1310720 cases, and gives
identical results before and after: in every one of those cases the mask
being computed is dead, because no whole words remain to copy. So this
changes no observable behaviour on current compilers.
The guards also do a second job. They are what lets a static analyzer
establish the bounds: without them clang's core.BitwiseShift reports
"Left shift overflows the capacity of 'UInt'" for paths reaching here
from blister.c and vecgf2.c. Please keep them even where a local reading
suggests the branch is dead.
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>1 parent 989cc35 commit 77a2746
1 file changed
Lines changed: 34 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
| |||
52 | 58 | | |
53 | 59 | | |
54 | 60 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
59 | 73 | | |
60 | 74 | | |
61 | 75 | | |
| |||
72 | 86 | | |
73 | 87 | | |
74 | 88 | | |
| 89 | + | |
| 90 | + | |
75 | 91 | | |
76 | 92 | | |
77 | 93 | | |
| |||
126 | 142 | | |
127 | 143 | | |
128 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
129 | 149 | | |
130 | 150 | | |
131 | 151 | | |
| 152 | + | |
| 153 | + | |
132 | 154 | | |
133 | 155 | | |
134 | | - | |
135 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
136 | 163 | | |
137 | 164 | | |
138 | 165 | | |
| |||
0 commit comments