File tree Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -19,11 +19,6 @@ To prevent the latter possibility, when using the `nightly` feature
19
19
from the optimizer, by passing it through an inline assembly block. For more
20
20
information, see the _ About_ section below.
21
21
22
- When not using the ` nightly ` feature, there is no protection against b). This
23
- is unfortunate, but is at least no worse than C code, and has the advantange
24
- that if a suitable black box is stabilized, we will be able to transparently
25
- enable it with no changes to the external interface).
26
-
27
22
``` toml
28
23
[dependencies .subtle ]
29
24
version = " 2.1"
Original file line number Diff line number Diff line change @@ -155,10 +155,23 @@ fn black_box(mut input: u8) -> u8 {
155
155
#[ inline( never) ]
156
156
fn black_box ( input : u8 ) -> u8 {
157
157
debug_assert ! ( ( input == 0u8 ) | ( input == 1u8 ) ) ;
158
- // We don't have access to inline assembly or test::black_box or ...
158
+ // We don't have access to inline assembly or test::black_box, so we use the fact that
159
+ // volatile values will never be elided to register values.
159
160
//
160
- // Bailing out, hopefully the compiler doesn't use the fact that `input` is 0 or 1.
161
- input
161
+ // Note: Rust's notion of "volatile" is subject to change over time. While this code may break
162
+ // in a non-destructive way in the future, it is better than doing nothing.
163
+
164
+ unsafe {
165
+ // Optimization barrier
166
+ //
167
+ // Unsafe is ok, because:
168
+ // - &input is not NULL;
169
+ // - size of input is not zero;
170
+ // - u8 is neither Sync, nor Send;
171
+ // - u8 is Copy, so input is always live;
172
+ // - u8 type is always properly aligned.
173
+ core:: ptr:: read_volatile ( & input as * const u8 )
174
+ }
162
175
}
163
176
164
177
impl From < u8 > for Choice {
You can’t perform that action at this time.
0 commit comments