Skip to content

Commit e041f84

Browse files
authored
Merge pull request #1032 from pnkfelix/ctfe-volatility-amendment
Update "Const Eval (Un)Safety Rules" post
2 parents 091d081 + 37b7452 commit e041f84

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

posts/2022-09-15-const-eval-safety-rule-revision.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,28 @@ here."
9797
But such reasoning would be based on an imprecise view of what the Rust compiler
9898
was doing here.
9999

100-
The const-eval machinery of the Rust compiler is built upon the MIR-interpreter
101-
[Miri][], which uses an *abstract model* of a hypothetical machine as the
100+
The const-eval machinery of the Rust compiler (also known as "the [CTFE][] engine")
101+
is built upon a [MIR][] interpreter which uses an *abstract model* of a hypothetical machine as the
102102
foundation for evaluating such expressions. This abstract model doesn't have to
103-
represent memory addresses as mere integers; in fact, to support Miri's
103+
represent memory addresses as mere integers; in fact, to support
104104
fine-grained checking for UB, it uses a much richer datatype for
105105
the values that are held in the abstract memory store.
106106

107+
(The aforementioned MIR interpreter is also the basis for [Miri][], a research
108+
tool that interprets *non-const* Rust code, with a focus on
109+
explicit detection of undefined behavior. The Miri developers are the primary
110+
contributors to the CTFE engine in the Rust compiler.)
111+
112+
[CTFE]: https://rustc-dev-guide.rust-lang.org/const-eval.html
113+
[MIR]: https://rustc-dev-guide.rust-lang.org/mir/index.html
107114
[Miri]: https://github.com/rust-lang/miri#readme
108115

109-
The details of Miri's value representation do not matter too much for our
116+
The details of the CTFE engine's value representation do not matter too much for our
110117
discussion here. We merely note that earlier versions of the compiler silently
111118
accepted expressions that *seemed to* transmute memory addresses into integers,
112119
copied them around, and then transmuted them back into addresses; but that was
113120
not what was acutally happening under the hood. Instead, what was happening was
114-
that the Miri values were passed around blindly (after all, the whole point of
121+
that the values were passed around blindly (after all, the whole point of
115122
transmute is that it does no transformation on its input value, so it is a no-op
116123
in terms of its operational semantics).
117124

@@ -153,12 +160,12 @@ In fact, *all* of the examples provided above are exhibiting *undefined
153160
behavior* according to the semantics of Rust's const-eval system.
154161

155162
The first example with `_copy` was accepted in Rust versions 1.46 through 1.63
156-
because of Miri implementation artifacts. Miri puts considerable effort into
163+
because of CTFE implementation artifacts. The CTFE engine puts considerable effort into
157164
detecting UB, but does not catch all instances of it. Furthermore, by default,
158-
Miri's detection can be delayed to a point far after where the actual
165+
such detection can be delayed to a point far after where the actual
159166
problematic expression is found.
160167

161-
But with nightly Rust, we can opt into extra checks for UB that Miri provides,
168+
But with nightly Rust, we can opt into extra checks for UB that the engine provides,
162169
by passing the unstable flag `-Z extra-const-ub-checks`. If we do that, then for
163170
*all* of the above examples we get the same result:
164171

@@ -207,7 +214,7 @@ though with less discussion than what you see in the stabilization report -->.)
207214
[doc for transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html
208215

209216
Thus, we can see that the classification of the above examples as UB during const evaluation
210-
is not a new thing at all. The only change here was that Miri had some internal
217+
is not a new thing at all. The only change here was that the CTFE engine had some internal
211218
changes that made it start detecting the UB rather than silently ignoring it.
212219

213220
This means the Rust compiler has a shifting notion of what UB it will
@@ -228,7 +235,7 @@ strived to mitigate such breakage *whenever feasible*, via things like
228235

229236
[future-incompat]: https://doc.rust-lang.org/rustc/lints/index.html#future-incompatible-lints
230237

231-
Today, with our current const-eval architecture layered atop Miri, it is not
238+
Today, with our current const-eval architecture, it is not
232239
feasible to ensure that changes such as the [one that injected][PR #97684] issue
233240
[#99923][] go through a future-incompat warning cycle.
234241
The compiler team plans to keep our eye on issues in this space. If we see
@@ -288,7 +295,7 @@ As you might imagine, a lot of us are pretty interested in questions such as
288295

289296
See for example Ralf Jung's excellent blog series on why pointers are
290297
complicated (parts [I][ralf1], [II][ralf2], [III][ralf3]), which contain some of
291-
the details elided above about Miri's representation, and spell out reasons why
298+
the details elided above about the representation of pointer values, and spell out reasons why
292299
you might want to be concerned about pointer-to-usize transmutes even *outside*
293300
of const-eval.
294301

@@ -322,6 +329,6 @@ that the 1.64 stable release will not encounter any other surprises related to
322329
the aforementioned change to the const-eval machinery.
323330

324331
But fluke or not, the issue provided excellent motivation to spend some time
325-
exploring facets of Rust's const-eval architecture, and the Miri interpreter
326-
that underlies it. We hope you enjoyed reading this as much as we did writing
327-
it.
332+
exploring facets of Rust's const-eval architecture and the interpreter
333+
that underlies it.
334+
We hope you enjoyed reading this as much as we did writing it.

0 commit comments

Comments
 (0)