rucc_opt/discharge.rs
1//! Taking out a safety check whose answer is already known.
2//!
3//! Design: `spec/safe-memory/07-check-elimination.md` section 7.3, which is the first half of the
4//! Tier E budget. `rucc-safety` puts a bounds check and a lifetime check in front of every access
5//! and does not try to be clever about it, on purpose: a walk that inserts everything is a walk
6//! anybody can read, and every check that is not needed is meant to be taken out here instead.
7//! This pass takes them out, and it does the case document 07 expects to be worth the most and to
8//! be the easiest to get right, which is a second access to bytes an earlier access already had
9//! checked. All three kinds `rucc-safety` emits are the pass's business, the bounds check and the
10//! lifetime check in front of an access and the derivation check after a walk, because they are
11//! emitted together and taking out one of three is a third of a saving.
12//!
13//! # The two halves
14//!
15//! Section 7.7 asks for the pass and the condition to be separate things, and they are. What is in
16//! this file is a walk: which check runs before which, which pointer was computed from which, and
17//! how far apart two addresses are. Nothing here decides whether that is enough. The condition
18//! under which a check may go is a rule in `rules/safety.rules`, a solver has to agree with it
19//! before this crate finishes building, and `crate::rules::safety` is the table it compiles into.
20//!
21//! The split is worth the trouble because the two halves fail differently. A walk that gets the
22//! context wrong is a bug of the ordinary kind, and section 14.3's differential check accounting,
23//! which runs the instrumented program with every check and again with the discharged ones gone,
24//! is what looks for it. A removal condition that is wrong is arithmetic that is off at the ends of
25//! the type. It gives the right answer on every test anybody writes and lets one access through in
26//! the one case nobody thought of, and nothing observes that until somebody exploits it.
27//!
28//! # What it establishes and what it asks
29//!
30//! Walking the dominator tree from the entry, the pass carries a set of facts. A `check_bounds`
31//! that stays is a fact, because a check that passes says the bytes it was about lie inside one
32//! storage instance, and a check that fails does not return. A fact is remembered as the pointer's
33//! base and the constant offset from it, which is what a chain of `ptr_add` over constants comes
34//! to, plus how many bytes the access covers.
35//!
36//! At the next `check_bounds`, the pointer is normalized the same way. When a fact shares its base,
37//! the distance between the two accesses is the difference of the two offsets, and that is a number
38//! this pass has rather than a claim it makes: both addresses are the same value plus a constant.
39//! The question of whether the later bytes are inside the earlier ones is then handed to the table,
40//! which answers it in sixty four bit arithmetic rather than in the offsets, and the check goes
41//! only if the answer is yes.
42//!
43//! A check whose extent is an operand is left out of all of this, in both directions. Section 7.4's
44//! hoisted check covers as many bytes as its loop runs times, and every range compared here is a
45//! pair of numbers, so such a check is neither read as a fact nor asked about. Reading its payload
46//! would be worse than skipping it, since the size there is one element of the walk rather than the
47//! range the check is about, and a fact recorded from it would be smaller than the truth in one
48//! direction and a question asked from it smaller in the other.
49//!
50//! The capability operand has to be the `cap_of` of the check's own pointer, which is the shape
51//! `rucc-safety` emits and the shape the argument needs. The check being removed asks whether its
52//! bytes are inside the instance that owns its own pointer, its pointer is inside the range the
53//! earlier check established, and that range is inside one instance, so the answer is yes. A check
54//! whose capability came from somewhere else is asking about a different instance and is left
55//! alone. Nothing is required of the earlier check's capability, because all that is used of it is
56//! that the check passed, and a check that passed put its bytes inside one instance whatever
57//! capability it named.
58//!
59//! # The conjunct that is not about bytes
60//!
61//! A `check_bounds` tests two things, because document 06 section 6.3 put the access alignment on
62//! it rather than in a check of its own: that the bytes are inside one instance, and that the
63//! address starts where an access of that alignment may start. Everything above is about the
64//! first. A check that goes takes the second away with it, so nothing goes until something has
65//! answered it, which is `aligned` below and which reads the object the address came from and the
66//! steps taken from it. An access that assumes nothing about where it starts has nothing to
67//! answer, and a member of a packed record is exactly that.
68//!
69//! A global is settled elsewhere and arrives as [`Flags::ALIGNED`], for the reason
70//! [`Flags::STATIC`] beside it exists: how aligned a global is lives on the module and a pass is
71//! given a function. Without that the gate would cost seventeen times what it costs, which is the
72//! measurement in the changelog and is what says the flag earns its bit.
73//!
74//! What is left is a pointer this function was handed, one it loaded out of memory, and one a call
75//! gave back. It is counted rather than argued about, the same as everything else here, so
76//! `-fopt-info-missed` says what the rest of the `!aligned` fact of section 6.2.4 would be worth.
77//!
78//! # The fact nobody had to check for
79//!
80//! Section 7.2 lists four sources of a discharge and puts the frontend first, because the majority
81//! of accesses in real C are to a local or a global at a constant offset and the bounds of either
82//! are not something anybody has to find out. An `alloca` of a fixed size makes one storage
83//! instance of that many bytes and says so in its payload, so the range from its address to that
84//! many further along is inside one instance for exactly the reason a passing `check_bounds` says
85//! its own range is. When the address a check is about normalizes to such an `alloca`, that range
86//! is the fact, and the question put to the table is the same question with the same rule
87//! answering it.
88//!
89//! Two things make it worth more than a fact a check established. It is there before anything has
90//! run, so the first access to a local is discharged rather than only the second. And no call takes
91//! it away: a callee cannot free a frame slot, whatever it does to whatever the slot points at, so
92//! this fact is asked separately rather than kept in the set the walk throws away at the first call
93//! it cannot see through.
94//!
95//! Only the fixed size form. A variable length array is an `alloca` with an operand and a payload
96//! whose size field reads zero, and reading it anyway would discharge every check in the array.
97//!
98//! A global is the same fact about the other half of section 7.2's sentence, and it arrives here
99//! differently for one reason: how big a global is lives on the module and this pass is given one
100//! function. So `crate::extents` works it out over the module before the pipeline starts, asks the
101//! same rule, and writes the answer onto the check as [`Flags::STATIC`], which is what
102//! `crate::nofree` does with what a call reaches and for the same reason. What is read here is what
103//! the IR says, the same way the pass reads an opcode.
104//!
105//! It answers a lifetime check as well as a bounds check, which a local does not. What a local
106//! gives is an extent, and how long it stays alive is the block it was declared in, which is a
107//! question this pass has nothing to say about. A global has static storage duration and is alive
108//! wherever the question is asked.
109//!
110//! # The walk that stops at a step it cannot read
111//!
112//! Everything above needs the address to be a base and a constant, and an array index is not a
113//! constant. The walk stops at the first `ptr_add` whose step is a value, and what comes out is a
114//! fact about a base whose size nobody knows, which answers nothing.
115//!
116//! Section 7.2's third source is what gets past it. Document 10's ranges know something about the
117//! step even though it is not a number: an index the program has already tested against a length,
118//! or one whose low bits are all that is used, is bounded. So the walk carries on, adding the low
119//! end of the step's range to the offset and the width of the range to the size, and what it ends
120//! up with is the range of addresses the access can land in.
121//!
122//! Whether an object holding all of that range holds the one address the access actually uses is
123//! its own rule, `reached.i64`, which leaves the distance opaque so that one answer covers every
124//! value the step could take. It is a rule of its own rather than the containment rule asked about
125//! the far end of the range, and the reason is section 7.7's: turning a range of addresses into one
126//! containment question is arithmetic on the thing being proved, and a pass doing that quietly is
127//! what the split between the walk and the rule exists to stop.
128//!
129//! What the range is asked of is the list above and not a shorter one: the local an `alloca`
130//! declares, the object an allocator made where the program has tested it, and the ranges checks
131//! that already ran established. The allocation was missing from that list until tamnd/rucc#880,
132//! which is what left a loop walking an index into its own `malloc` with every check it started
133//! with however plainly the call said how many bytes it made.
134//!
135//! The range is only ever asked with and never recorded. What a check proves when it runs is that
136//! the address the program used was inside the object, and nothing at all about the rest of a
137//! range this pass made up around it. So a check discharged this way records the narrow fact, the
138//! bytes the access really wanted, which is the thing that was proved and is what a second check
139//! of the same bytes is answered by.
140//!
141//! The ranges are built only for a function that has a walk by a value in it, because they cost a
142//! copy of the control flow graph and a function without one would never ask them anything.
143//!
144//! # The lifetime half, and what it borrows from the other one
145//!
146//! A `check_live` that stays is a fact too, and a smaller one than it looks: it says the storage
147//! instance holding its own address is alive, and it says nothing about the address four bytes
148//! along, because that address might be in a different instance. On its own that fact discharges
149//! only a second lifetime check of the very same address, and the shape `rucc-safety` emits is a
150//! lifetime check per field rather than per object, so on its own it would almost never fire.
151//!
152//! What makes it fire is the bounds fact sitting next to it. A `check_bounds` that passed put its
153//! whole range inside one instance, so if the lifetime check's address is in that range, the
154//! instance that was found alive is the instance the whole range is in, and the whole range is
155//! alive. So a lifetime fact is recorded as the widest checked range containing its address, and a
156//! later lifetime check is asked about as a single byte. The question of whether that byte is in
157//! that range is the same question the bounds half asks, put to the same rule.
158//!
159//! The order the two arrive in is what makes this work rather than a coincidence to be careful
160//! about: `rucc-safety` emits the bounds check first and the lifetime check second, so the range is
161//! established by the time there is a lifetime fact to widen. A lifetime check that arrives with no
162//! range around it keeps the narrow fact, which is correct and worth little.
163//!
164//! # The derivation half, which is one question rather than two
165//!
166//! `rucc-safety` puts a `check_deriv` after every `ptr_add` off a pointer, and what it asks is not
167//! about a range at all: it asks whether the pointer that came out is still in the storage instance
168//! the pointer that went in belongs to. The runtime has some slack in it for a pointer that walked
169//! exactly off either end, and none of that slack is used here, because the case this pass answers
170//! is the one where both ends are plainly inside something.
171//!
172//! What answers it is one fact holding both ends. A `check_bounds` that passed put its whole range
173//! inside one instance, so if the address that went in and the address that came out are both in
174//! that range, the second is in the instance the first belongs to, which is the question. It has to
175//! be one fact and not one for each end: two facts saying two addresses are each inside some
176//! instance say nothing about whether it is the same instance, and that is the only thing being
177//! asked. A local is a fact of exactly this shape and is asked the same way.
178//!
179//! Both ends are asked about as a single byte, the way a lifetime check is, and for the same reason.
180//! Nothing here is claiming anything about how many bytes are readable at either address.
181//!
182//! A `check_deriv` that stays leaves no fact behind. What it establishes is that two addresses share
183//! an instance, which is not a range of bytes and does not fit in what this walk carries, and the
184//! `covered.i64` rule has nothing to say about it. Recording it would mean a second kind of fact and
185//! a second rule, and the pointer it is about nearly always gets a `check_bounds` of its own a few
186//! instructions later that establishes the range properly.
187//!
188//! # Why a call throws the facts away, and which calls do not
189//!
190//! Section 7.3 says nothing kills a bounds fact except a redefinition of the capability, which in
191//! SSA is never, and this pass is stricter than that: a call, or anything else this pass cannot see
192//! through, drops every fact it is carrying.
193//!
194//! The case is a `free` and then an allocation of something smaller at the same address. The range
195//! established before the call is no longer inside one instance after it, and what document 07
196//! leaves that to is the lifetime judgement rather than this one. Today's lifetime check is about
197//! the address rather than about the version the capability was taken at, so it would not refuse
198//! the access either, and a rate this pass reports is worth less than a hole it opens. The strict
199//! version is what is written first.
200//!
201//! A `meta_end` and a `meta_transfer` drop the facts as well. Nothing emits either one yet, so
202//! this costs nothing today and is the difference between conservative and wrong on the day the
203//! instrumentation starts ending lifetimes. `crate::nofree` treats them the same way.
204//!
205//! The two facts nobody had to check for go across a call untouched, and neither is an exception to
206//! the paragraph above because neither is in the set being thrown away. A callee cannot free a
207//! frame slot and cannot free a global, so a check the declaration answers is answered on the far
208//! side of any call at all.
209//!
210//! A call that says it reaches nothing which can free is the exception, and it is not this pass
211//! being trusting. `crate::nofree` works the answer out over the whole module before the pipeline
212//! starts and writes it onto the call site as [`Flags::NOFREE`], because the fact belongs to the
213//! callee and a pass is given one function. Reading it here is reading what the IR says, the same
214//! way the pass reads an opcode. Nothing else about a call is believed: the facts still go across
215//! an unmarked call, a call through an address, and inline assembly.
216//!
217//! What the strictness still costs is measured rather than guessed. A check that a fact would have
218//! covered if a call had not intervened is counted, so `-fopt-info-missed` says per function what
219//! is left to win.
220
221use std::collections::{HashMap, HashSet};
222
223use rucc_ir::{Block, Def, Extra, Flags, Func, Inst, Opcode, Value};
224
225use crate::range::query::Ranges;
226use crate::rules::{Piece, Subject, Table, safety};
227use crate::{Analyses, Analysis, Cfg, Fuel, Pass, Preserved, Stats, heap};
228
229/// Recorded once for each bounds check taken out.
230const REMOVED: &str = "bounds check removed, a dominating check covers the same bytes";
231
232/// Recorded once for each bounds check taken out because it was inside a local.
233const REMOVED_LOCAL: &str = "bounds check removed, its bytes are inside a local this function \
234 declares";
235
236/// Recorded once for each bounds check taken out because it was inside a global.
237const REMOVED_STATIC: &str = "bounds check removed, its bytes are inside an object of static \
238 storage duration";
239
240/// Recorded once for each bounds check taken out because every caller hands in the object.
241const REMOVED_HANDED: &str = "bounds check removed, its bytes are inside an object every call to \
242 this function hands it";
243
244/// Recorded once for each bounds check taken out because an allocator made the object.
245const REMOVED_MADE: &str = "bounds check removed, its bytes are inside an object an allocator made \
246 and this function has tested";
247
248/// Recorded once for each bounds check taken out because a range answered the step it walked by.
249const REMOVED_RANGE: &str = "bounds check removed, every address the walk can reach is inside the \
250 object it started from";
251
252/// Recorded once for each lifetime check taken out.
253const REMOVED_LIVE: &str = "lifetime check removed, a dominating check covers the same storage";
254
255/// Recorded once for each lifetime check taken out because it was inside a global.
256const REMOVED_LIVE_STATIC: &str =
257 "lifetime check removed, its storage lives as long as the program does";
258
259/// Recorded once for each lifetime check taken out because every caller hands in the object.
260const REMOVED_LIVE_HANDED: &str = "lifetime check removed, its storage is an object every call to \
261 this function hands it";
262
263/// Recorded once for each lifetime check taken out because it was inside a frame slot.
264const REMOVED_LIVE_LOCAL: &str =
265 "lifetime check removed, its storage is a frame slot of this function";
266
267/// Recorded once for each lifetime check taken out because a range answered the step it walked by.
268const REMOVED_LIVE_RANGE: &str = "lifetime check removed, every address the walk can reach is in \
269 storage a check found alive";
270
271/// Recorded for a bounds check that would have gone if there had been fuel for it.
272const NO_FUEL: &str = "bounds check kept, the pass ran out of fuel";
273
274/// Recorded for a lifetime check that would have gone if there had been fuel for it.
275const NO_FUEL_LIVE: &str = "lifetime check kept, the pass ran out of fuel";
276
277/// Recorded once for each derivation check taken out because a range answered the step it walked by.
278const REMOVED_DERIV_RANGE: &str = "derivation check removed, every address either end can reach is \
279 inside one checked range";
280
281/// Recorded for a bounds check a call cost, which is the honest price of the paragraph above.
282///
283/// This one is worth reading rather than skipping. It is the number of checks that are still being
284/// paid for because `crate::nofree` could not vouch for a call, so it says per function what the
285/// rest of section 7.5's summary work would be worth before anybody writes it.
286const PAST_A_CALL: &str =
287 "bounds check kept, a call between it and the check that covers it might free";
288
289/// The same, for a lifetime check. Section 8.8 is about this number rather than the one above.
290const PAST_A_CALL_LIVE: &str =
291 "lifetime check kept, a call between it and the check that covers it might free";
292
293/// Recorded for a bounds check kept because nothing here says where the access starts.
294///
295/// The alignment conjunct of judgement J1 rides on `check_bounds`, so taking the check out takes
296/// the alignment test with it. Recorded only for a check a rule had already answered the bytes of,
297/// so the number is what the gate costs rather than how many checks have an alignment, which makes
298/// it what the `!aligned` fact of `spec/safe-memory/06-instrumentation.md` section 6.2.4 would be
299/// worth.
300const UNKNOWN_ALIGNMENT: &str =
301 "bounds check kept, nothing here says the address is aligned to what the access assumes";
302
303/// Recorded for a bounds check whose operands this pass cannot read.
304const UNKNOWN_SHAPE: &str = "bounds check left alone, its pointer is not a base and a constant";
305
306/// Recorded for a bounds check about a range the program worked out.
307const COMPUTED_EXTENT: &str =
308 "bounds check left alone, how many bytes it covers is a number only the program has";
309
310/// Recorded for a lifetime check whose operands this pass cannot read.
311const UNKNOWN_SHAPE_LIVE: &str =
312 "lifetime check left alone, its pointer is not a base and a constant";
313
314/// Recorded once for each derivation check taken out.
315const REMOVED_DERIV: &str =
316 "derivation check removed, one checked range holds both the pointer and where it walked to";
317
318/// Recorded once for each derivation check taken out because it walked inside a local.
319const REMOVED_DERIV_LOCAL: &str =
320 "derivation check removed, it walks inside a local this function declares";
321
322/// Recorded once for each derivation check taken out because it walked inside a global.
323const REMOVED_DERIV_STATIC: &str =
324 "derivation check removed, it walks inside an object of static storage duration";
325
326/// Recorded once for each derivation check taken out because every caller hands in the object.
327const REMOVED_DERIV_HANDED: &str = "derivation check removed, it walks inside an object every call \
328 to this function hands it";
329
330/// Recorded once for each derivation check taken out because an allocator made the object.
331const REMOVED_DERIV_MADE: &str = "derivation check removed, it walks inside an object an allocator \
332 made and this function has tested";
333
334/// Recorded for a derivation check that would have gone if there had been fuel for it.
335const NO_FUEL_DERIV: &str = "derivation check kept, the pass ran out of fuel";
336
337/// Recorded for a derivation check a call cost.
338const PAST_A_CALL_DERIV: &str =
339 "derivation check kept, a call between it and the range that holds both ends might free";
340
341/// Recorded for a derivation check naming a capability that is not the one it is about.
342const NOT_ITS_CAPABILITY_DERIV: &str = "derivation check left alone, the capability it names is not the one the pointer that went in \
343 carries";
344
345/// Recorded for a derivation check whose two ends are not off one value.
346const TWO_BASES_DERIV: &str =
347 "derivation check left alone, its two pointers are not built on one base";
348
349/// Recorded for a derivation check whose walk can reach past the end of the local it starts in.
350const OVER_THE_LOCAL_DERIV: &str =
351 "derivation check left alone, the walk can reach past the end of the local it starts in";
352
353/// Recorded for a derivation check on a pointer this function loaded out of memory.
354const NO_EXTENT_LOADED: &str = "derivation check left alone, nothing here says how big the object \
355 is and the pointer to it was loaded from memory";
356
357/// Recorded for a derivation check on a pointer this function was handed.
358const NO_EXTENT_HANDED: &str = "derivation check left alone, nothing here says how big the object \
359 is and the pointer to it was handed to this function";
360
361/// Recorded for a derivation check on a pointer into a global.
362const NO_EXTENT_GLOBAL: &str = "derivation check left alone, nothing here says how big the object \
363 is and the pointer to it is into a global";
364
365/// Recorded for a derivation check on a pointer a call handed back.
366const NO_EXTENT_RETURNED: &str = "derivation check left alone, nothing here says how big the \
367 object is and the pointer to it came back from a call";
368
369/// Recorded for a derivation check on a pointer none of the shapes above describes.
370const NO_EXTENT_OTHER: &str =
371 "derivation check left alone, nothing here says how big the object its pointers are in is";
372
373/// The pass. It holds nothing, because everything it works out is about one function.
374/// Which of the places a fact comes from a run of this pass may ask.
375///
376/// Everything is asked normally and there is one pass in the pipeline. The others are here for the
377/// measurement `spec/safe-memory/13-performance.md` section 13.5 asks for and
378/// `spec/safe-memory/17-open-questions.md` question 3 is: how much each source discharges on its
379/// own, and how much the same sources discharge together. A number for a source on its own cannot
380/// be read off the remarks of a full run, because the rules are asked in an order and whichever one
381/// answers first is the one the remark names, so the second source to be asked about a check two of
382/// them could answer looks like it answered nothing.
383///
384/// The four are document 07 section 7.2's four, with the caveat the measurement found: the ranges
385/// are not a fourth kind of fact but a way of asking the other three about a subscript instead of
386/// about an address written out in the program.
387#[derive(Clone, Copy, PartialEq, Eq, Debug)]
388pub struct Sources {
389 /// How big an object is, read off whatever made it. A global's extent comes from
390 /// `crate::extents`, a local's from its `alloca`, an allocation's from the call `crate::heap`
391 /// marked. Section 7.2's first source.
392 objects: bool,
393 /// What a check that has already run established, carried down the dominator tree. Section 7.3,
394 /// and the one the literature calls redundant check elimination.
395 dominance: bool,
396 /// What every caller of this function guarantees about what it was handed, from
397 /// `crate::params`. Section 7.5.
398 summaries: bool,
399 /// The value ranges and the recurrences, which widen the one address a check names into the
400 /// range of addresses a walk can reach so that the other three can be asked about a subscript.
401 /// Section 7.4, and the half of the PICO result this pass holds. The other half is
402 /// [`crate::hoist`] and [`crate::split`], which are passes of their own and have flags of their
403 /// own.
404 ranges: bool,
405}
406
407impl Sources {
408 /// Every one of them, which is what the pipeline runs.
409 pub const ALL: Self = Self { objects: true, dominance: true, summaries: true, ranges: true };
410 /// What an object says about itself and nothing else.
411 pub const OBJECTS: Self =
412 Self { objects: true, dominance: false, summaries: false, ranges: false };
413 /// What an earlier check established and nothing else.
414 pub const DOMINANCE: Self =
415 Self { objects: false, dominance: true, summaries: false, ranges: false };
416 /// What every caller guarantees and nothing else.
417 pub const SUMMARIES: Self =
418 Self { objects: false, dominance: false, summaries: true, ranges: false };
419 /// Every fact, asked only about addresses written out in the program.
420 pub const NARROW: Self =
421 Self { objects: true, dominance: true, summaries: true, ranges: false };
422}
423
424#[derive(Debug, Clone, Copy, PartialEq, Eq)]
425pub struct Discharge {
426 /// What `-f<name>` and `-fno-<name>` reach this run by.
427 name: &'static str,
428 /// Which places it may take a fact from. See [`Sources`].
429 sources: Sources,
430}
431
432/// The pass the pipeline runs, which asks everything.
433pub static DISCHARGE: Discharge = Discharge { name: "discharge", sources: Sources::ALL };
434
435/// The same pass asking an object how big it is and nothing else.
436pub static OBJECTS: Discharge = Discharge { name: "discharge-objects", sources: Sources::OBJECTS };
437
438/// The same pass asking what an earlier check established and nothing else.
439pub static DOMINANCE: Discharge =
440 Discharge { name: "discharge-dominance", sources: Sources::DOMINANCE };
441
442/// The same pass asking what every caller guarantees and nothing else.
443pub static SUMMARIES: Discharge =
444 Discharge { name: "discharge-summaries", sources: Sources::SUMMARIES };
445
446/// The same pass asking every fact, about addresses written out in the program only.
447pub static NARROW: Discharge = Discharge { name: "discharge-narrow", sources: Sources::NARROW };
448
449/// The same pass asking everything, under a name of its own.
450///
451/// [`DISCHARGE`] already asks everything, so this looks like a duplicate and is not. A pass the level
452/// did not choose goes on the end of the pipeline, so a run of `-fno-discharge -fdischarge-objects`
453/// asks its question in a different place from where the shipped pass asks it, and the two numbers
454/// are not comparable. This one is turned on the same way as the others and lands in the same place,
455/// so the sum of the parts and the whole are measured under one arrangement. What it costs against
456/// [`DISCHARGE`] is what the position is worth, which is a number the measurement wants anyway.
457pub static EVERY: Discharge = Discharge { name: "discharge-every", sources: Sources::ALL };
458
459impl Pass for Discharge {
460 fn name(&self) -> &'static str {
461 self.name
462 }
463
464 fn describe(&self) -> &'static str {
465 "a bounds, lifetime or derivation check whose answer is already known is removed"
466 }
467
468 fn preserves(&self) -> Preserved {
469 // Instructions go and blocks do not. A check is not a terminator and removing one leaves
470 // every edge where it was. What it does not leave where it was is the liveness, because
471 // the check was reading something and now nothing is.
472 Preserved::ALL.without(Analysis::Liveness)
473 }
474
475 fn run(&self, func: &mut Func, an: &mut Analyses, fuel: &mut Fuel) -> Stats {
476 let mut stats = Stats::new();
477 let Some(entry) = func.entry() else { return stats };
478 let dom = an.dominators(func);
479
480 // The graph is built for two reasons and neither is the common one, so a function with
481 // neither pays for no copy of it. The ranges want it when there is a walk the constant
482 // reader gives up on, and the allocation rule wants it to find where the program has tested
483 // what an allocator gave it.
484 let walks = self.sources.ranges && walks_by_a_value(func);
485 let cfg = (walks || (self.sources.objects && heap::allocates(func))).then(|| an.cfg(func));
486 let mut ranges = cfg.filter(|_| walks).map(|cfg| Ranges::new(&*func, cfg, dom));
487
488 // One answer per allocation rather than one per check, because a function that reads twenty
489 // fields of the same object asks the same question about the same pointer twenty times.
490 let mut checked: HashMap<Value, HashSet<Block>> = HashMap::new();
491
492 // Whether anything in here says a lifetime is over. Read once over the whole function
493 // rather than carried down the walk, because what the frame slot rule needs is that no
494 // `meta_end` runs before the check on any path, and a fact carried down the dominator
495 // tree only ever says something about the paths that go through one block.
496 let ends = ends_a_lifetime(func);
497
498 // The walk is a stack rather than recursion because the dominator tree of a long chain of
499 // blocks is as deep as the function is long, and a pass is not a place to find that out.
500 // Each block carries its own copy of what holds at its start, which is what makes a fact a
501 // call killed in one arm of a branch still hold in the other.
502 let mut going: Vec<(Inst, &'static str)> = Vec::new();
503 let mut work = vec![(entry, Scope::default())];
504 while let Some((block, mut scope)) = work.pop() {
505 for inst in func.insts(block).collect::<Vec<Inst>>() {
506 if opaque(func, inst) {
507 scope.forget();
508 continue;
509 }
510 match func[inst].opcode {
511 Opcode::CheckBounds => {
512 if func[func[inst].args].len() > 2 {
513 stats.missed(COMPUTED_EXTENT);
514 continue;
515 }
516 let Some(asked) = about(func, inst) else {
517 stats.missed(UNKNOWN_SHAPE);
518 continue;
519 };
520 // The four objects whose extent is known without anybody having checked
521 // it. A global was worked out over the module by `crate::extents` and an
522 // object every caller hands in by `crate::params`, both of which arrive as
523 // a flag; a local is read off its `alloca` here and an allocation off the
524 // call `crate::heap` marked. All four are asked of the same rule as every
525 // other fact. The reach of a walk the constant reader could not finish is
526 // asked last, because it is the only one that costs an analysis to answer.
527 let why = if self.sources.objects
528 && func[inst].flags.contains(Flags::STATIC)
529 {
530 Some(REMOVED_STATIC)
531 } else if self.sources.summaries && func[inst].flags.contains(Flags::HANDED)
532 {
533 Some(REMOVED_HANDED)
534 } else if self.sources.objects
535 && declared(func, asked.base)
536 .is_some_and(|local| covers(&local, &asked))
537 {
538 Some(REMOVED_LOCAL)
539 } else if self.sources.objects
540 && allocated(func, cfg, &mut checked, block, &[&asked])
541 {
542 Some(REMOVED_MADE)
543 } else if self.sources.dominance && scope.bounds.covers(&asked) {
544 Some(REMOVED)
545 } else {
546 // The same four sources in the same order, asked of the range of
547 // addresses the walk can reach rather than of the one address the
548 // constant reader could name. A flag has already been read above and
549 // reading it again would say the same thing, so what is left is the
550 // local, the allocation and what the walk carries.
551 reach(func, ranges.as_mut(), &asked, inst).and_then(|wide| {
552 if self.sources.objects
553 && declared(func, wide.base)
554 .is_some_and(|local| reaches(&local, &wide))
555 {
556 Some(REMOVED_RANGE)
557 } else if self.sources.objects
558 && allocated_around(func, cfg, &mut checked, block, &[&wide])
559 {
560 Some(REMOVED_MADE)
561 } else if self.sources.dominance && scope.bounds.reaches(&wide) {
562 Some(REMOVED_RANGE)
563 } else {
564 None
565 }
566 })
567 };
568 // Asked once a rule has answered the bounds rather than in front of them
569 // all, because a check that was staying anyway costs the gate nothing and
570 // the number somebody reads has to be what it actually costs. A check kept
571 // here still runs, so it still establishes what it was about.
572 let why = why.filter(|_| {
573 aligned(func, inst) || {
574 stats.missed(UNKNOWN_ALIGNMENT);
575 false
576 }
577 });
578 let Some(why) = why else {
579 if scope.bounds.covered_before(&asked) {
580 stats.missed(PAST_A_CALL);
581 }
582 // A check that stays is a check that runs, and a check that runs
583 // establishes what it was about. One that was removed establishes
584 // nothing new: whatever covered it covers everything it would have.
585 scope.bounds.held.push(asked);
586 continue;
587 };
588 if !fuel.take() {
589 stats.missed(NO_FUEL);
590 scope.bounds.held.push(asked);
591 continue;
592 }
593 // A check that goes normally establishes nothing new, because whatever
594 // answered it covers everything it would have. The range is the one
595 // exception: what answered it was a fact about a made up range around the
596 // address, and the next check on these bytes has to ask for that range
597 // again and may not get the same answer. So the narrow fact goes in, which
598 // is the thing that was actually proved.
599 if why == REMOVED_RANGE {
600 scope.bounds.held.push(asked);
601 }
602 going.push((inst, why));
603 }
604 Opcode::CheckLive => {
605 let Some(asked) = alive(func, inst) else {
606 stats.missed(UNKNOWN_SHAPE_LIVE);
607 continue;
608 };
609 // A global is alive as long as the program is, and a frame slot is alive
610 // until the function returns, so both objects whose extent is known
611 // without anybody having checked it answer this as well as a bounds
612 // check. `ends` is what makes the second one true: where a local stops
613 // being alive is written into the IR as `meta_end` and not read off the
614 // shape of the source, so a function with one in it is a function this
615 // does not claim anything about.
616 let why = if self.sources.objects
617 && func[inst].flags.contains(Flags::STATIC)
618 {
619 Some(REMOVED_LIVE_STATIC)
620 } else if self.sources.summaries && func[inst].flags.contains(Flags::HANDED)
621 {
622 Some(REMOVED_LIVE_HANDED)
623 } else if self.sources.objects
624 && !ends
625 && declared(func, asked.base)
626 .is_some_and(|local| covers(&local, &asked))
627 {
628 Some(REMOVED_LIVE_LOCAL)
629 } else if self.sources.dominance && scope.alive.covers(&asked) {
630 Some(REMOVED_LIVE)
631 } else {
632 // A lifetime fact and not a bounds one, because what is being asked
633 // is whether the storage is alive and a bounds check that passed says
634 // nothing about that. The widening argument is the bounds arm's: a
635 // range known alive that holds every address the walk can reach holds
636 // the one it actually uses.
637 reach(func, ranges.as_mut(), &asked, inst)
638 .filter(|wide| {
639 (self.sources.objects
640 && !ends
641 && declared(func, wide.base)
642 .is_some_and(|local| reaches(&local, wide)))
643 || (self.sources.dominance && scope.alive.reaches(wide))
644 })
645 .map(|_| REMOVED_LIVE_RANGE)
646 };
647 let Some(why) = why else {
648 if scope.alive.covered_before(&asked) {
649 stats.missed(PAST_A_CALL_LIVE);
650 }
651 scope.alive.held.push(widened(func, &scope.bounds, asked));
652 continue;
653 };
654 if !fuel.take() {
655 stats.missed(NO_FUEL_LIVE);
656 scope.alive.held.push(widened(func, &scope.bounds, asked));
657 continue;
658 }
659 // The bounds arm's exception, for its reason. A range answered a made up
660 // range around this address, so what was proved is about the address.
661 if why == REMOVED_LIVE_RANGE {
662 scope.alive.held.push(widened(func, &scope.bounds, asked));
663 }
664 going.push((inst, why));
665 }
666 Opcode::CheckDeriv => {
667 let narrow = derives(func, inst);
668 let why = narrow.and_then(|(from, to)| {
669 if self.sources.objects && func[inst].flags.contains(Flags::STATIC) {
670 Some(REMOVED_DERIV_STATIC)
671 } else if self.sources.summaries
672 && func[inst].flags.contains(Flags::HANDED)
673 {
674 Some(REMOVED_DERIV_HANDED)
675 } else if self.sources.objects
676 && declared(func, from.base).is_some_and(|local| {
677 covers(&local, &from) && covers(&local, &to)
678 })
679 {
680 Some(REMOVED_DERIV_LOCAL)
681 } else if self.sources.objects
682 && allocated(func, cfg, &mut checked, block, &[&from, &to])
683 {
684 Some(REMOVED_DERIV_MADE)
685 } else if self.sources.dominance && scope.bounds.holds_both(&from, &to)
686 {
687 Some(REMOVED_DERIV)
688 } else {
689 None
690 }
691 });
692 // Asked last, and asked off the check's own operands rather than off what
693 // `derives` worked out, because the case it is for is the one `derives`
694 // cannot read at all: past a step the constant reader gives up on the two
695 // ends are not one base and two constants. One thing has to hold both of
696 // the ranges, for the same reason one thing has to hold both of the
697 // addresses, which is that two things saying each end is inside something
698 // say nothing about it being the same something.
699 let why = why.or_else(|| {
700 spread(func, ranges.as_mut(), inst, inst).and_then(|(near, far)| {
701 if self.sources.objects
702 && declared(func, near.base).is_some_and(|local| {
703 reaches(&local, &near) && reaches(&local, &far)
704 })
705 {
706 Some(REMOVED_DERIV_RANGE)
707 } else if self.sources.objects
708 && allocated_around(
709 func,
710 cfg,
711 &mut checked,
712 block,
713 &[&near, &far],
714 )
715 {
716 Some(REMOVED_DERIV_MADE)
717 } else if self.sources.dominance
718 && scope.bounds.reaches_both(&near, &far)
719 {
720 Some(REMOVED_DERIV_RANGE)
721 } else {
722 None
723 }
724 })
725 });
726 let Some(why) = why else {
727 match narrow {
728 Some((from, to)) => {
729 if scope.bounds.held_both_before(&from, &to) {
730 stats.missed(PAST_A_CALL_DERIV);
731 }
732 }
733 None => {
734 stats.missed(unreadable(func, ranges.as_mut(), inst));
735 }
736 }
737 continue;
738 };
739 if !fuel.take() {
740 stats.missed(NO_FUEL_DERIV);
741 continue;
742 }
743 going.push((inst, why));
744 }
745 _ => continue,
746 }
747 }
748 for child in dom.children(block) {
749 work.push((child, scope.clone()));
750 }
751 }
752
753 for (inst, why) in going {
754 func.remove_inst(inst);
755 stats.optimized(why);
756 }
757 stats
758 }
759}
760
761/// A range of bytes some check has already been passed on, or is being asked about.
762///
763/// The address is kept as the value it was computed from and the constant distance from it, rather
764/// than as the pointer itself, because that is what makes two of these comparable: the whole of
765/// what this pass knows about two addresses is that they are one value plus two constants.
766#[derive(Debug, Clone, Copy, PartialEq, Eq)]
767pub(crate) struct Fact {
768 /// The value the address was computed from.
769 pub(crate) base: Value,
770 /// How far past it the access starts.
771 pub(crate) offset: i128,
772 /// How many bytes it covers.
773 size: i128,
774}
775
776impl Fact {
777 /// The whole of an object whose extent is known, starting at its own address.
778 ///
779 /// The two sources of one of these are an `alloca` of a fixed size and a global, and what they
780 /// have in common is that the size is said by something other than a check that passed.
781 pub(crate) fn whole(base: Value, size: i128) -> Self {
782 Self { base, offset: 0, size }
783 }
784
785 /// A range of bytes named by where it starts and how far it runs.
786 ///
787 /// The general form of [`Fact::whole`], for a caller that has both ends of a range in hand
788 /// rather than an object. `crate::dead_plane` is the one, and what it has is a plane write
789 /// rather than an access, which is a different thing to be about and the same thing to ask.
790 pub(crate) fn range(base: Value, offset: i128, size: i128) -> Self {
791 Self { base, offset, size }
792 }
793}
794
795/// A range of addresses an access can land in, and how many bytes it takes when it does.
796///
797/// What [`reach`] works out and the only thing it is used for. It is deliberately not a [`Fact`]:
798/// a fact is something that was established and may be recorded, and this is a question and may
799/// not. The address the program uses is `base` plus somewhere between `low` and `low` plus `width`
800/// further along, and what a check proves when it runs is about that one address rather than about
801/// the range this was made out of.
802#[derive(Debug, Clone, Copy)]
803struct Reach {
804 /// The value the address was computed from.
805 base: Value,
806 /// The nearest the access can start to it.
807 low: i128,
808 /// How much further than that it can start.
809 width: i128,
810 /// How many bytes it covers.
811 size: i128,
812}
813
814/// One kind of fact, and what has become of it.
815#[derive(Debug, Clone, Default)]
816struct Known {
817 /// The ranges a check has been passed on and nothing has cast doubt on since.
818 held: Vec<Fact>,
819 /// The ones a call threw away, kept only so that the cost of throwing them away is a number
820 /// somebody can read rather than a paragraph somebody has to believe.
821 lost: Vec<Fact>,
822}
823
824impl Known {
825 /// Whether something still standing answers this.
826 fn covers(&self, asked: &Fact) -> bool {
827 self.held.iter().any(|fact| covers(fact, asked))
828 }
829
830 /// Whether something still standing answers a range of addresses an access can land in.
831 fn reaches(&self, asked: &Reach) -> bool {
832 self.held.iter().any(|fact| reaches(fact, asked))
833 }
834
835 /// Whether one thing still standing answers both of these ranges.
836 ///
837 /// One rather than one each, for the reason [`Known::holds_both`] gives, and the reason does
838 /// not change when the ends are ranges instead of addresses.
839 fn reaches_both(&self, from: &Reach, to: &Reach) -> bool {
840 self.held.iter().any(|fact| reaches(fact, from) && reaches(fact, to))
841 }
842
843 /// Whether something would have answered it before a call came along.
844 fn covered_before(&self, asked: &Fact) -> bool {
845 self.lost.iter().any(|fact| covers(fact, asked))
846 }
847
848 /// Whether one thing still standing answers both of these.
849 ///
850 /// One rather than one each, which is the whole point of asking it this way. Two facts saying
851 /// two addresses are each inside some instance say nothing about whether it is the same
852 /// instance, and that is the only thing a derivation check wants to know.
853 fn holds_both(&self, from: &Fact, to: &Fact) -> bool {
854 self.held.iter().any(|fact| covers(fact, from) && covers(fact, to))
855 }
856
857 /// Whether one would have answered both before a call came along.
858 fn held_both_before(&self, from: &Fact, to: &Fact) -> bool {
859 self.lost.iter().any(|fact| covers(fact, from) && covers(fact, to))
860 }
861
862 /// Gives up everything, because something happened that this pass cannot see through.
863 fn forget(&mut self) {
864 self.lost.append(&mut self.held);
865 }
866}
867
868/// What holds where the walk has got to.
869///
870/// The two kinds are apart because they are killed together and answered separately: a range being
871/// inside one instance and that instance being alive are different claims, and reporting them as
872/// one number would hide which of the two a check is still being paid for.
873#[derive(Debug, Clone, Default)]
874struct Scope {
875 /// Ranges a `check_bounds` established are inside one storage instance.
876 bounds: Known,
877 /// Ranges a `check_live` established are in an instance that is alive.
878 alive: Known,
879}
880
881impl Scope {
882 /// Gives up every fact of either kind.
883 fn forget(&mut self) {
884 self.bounds.forget();
885 self.alive.forget();
886 }
887}
888
889/// Whether this instruction could do something to memory that this pass cannot account for.
890///
891/// A call is the whole of it, in every spelling, and inline assembly with it. A `tail_call` ends
892/// the block and there is nothing after it to protect, and it is here anyway so that the reason a
893/// fact survives is never that the walk did not think of something.
894///
895/// A call carrying [`Flags::NOFREE`] reaches nothing that ends a lifetime, so there is nothing for
896/// it to have done to the bytes an earlier check was passed on. `crate::nofree` is what put the
897/// flag there and what argues for it.
898///
899/// A `meta_end` and a `meta_transfer` end a lifetime by saying so, which is the plainest way for a
900/// fact to stop being true, and neither is emitted today.
901fn opaque(func: &Func, inst: Inst) -> bool {
902 match func[inst].opcode {
903 Opcode::Call | Opcode::CallIndirect | Opcode::TailCall => {
904 !func[inst].flags.contains(Flags::NOFREE)
905 }
906 Opcode::InlineAsm | Opcode::MetaEnd | Opcode::MetaTransfer => true,
907 _ => false,
908 }
909}
910
911/// What a `check_bounds` is about, when it is one this pass can read.
912pub(crate) fn about(func: &Func, check: Inst) -> Option<Fact> {
913 let (base, offset, whole) = addressed(func, check)?;
914 let Extra::Mem(info) = func[check].extra else { return None };
915 hull(base, offset, i128::from(func[info].size), whole)
916}
917
918/// What a `check_live` is about, when it is one this pass can read.
919///
920/// One byte, because that is the whole of what the check says: the instance holding this address
921/// is alive, and nothing about the address next door. The widening to a range that makes the fact
922/// useful is [`widened`], and it needs a bounds fact to do it.
923pub(crate) fn alive(func: &Func, check: Inst) -> Option<Fact> {
924 let (base, offset, whole) = addressed(func, check)?;
925 hull(base, offset, 1, whole)
926}
927
928/// The address a check is about, as a base and a constant, and whether the capability names the
929/// base rather than the address.
930///
931/// The capability has to be one this pass can tie to the address, which [`its_own`] is, so a check
932/// that does not have it is not a check this pass has anything to say about.
933fn addressed(func: &Func, check: Inst) -> Option<(Value, i128, bool)> {
934 let args = &func[func[check].args];
935 let &capability = args.first()?;
936 let &pointer = args.get(1)?;
937 let (base, offset) = normal(func, pointer);
938 let named = operand_of(func, capability, Opcode::CapOf, 0)?;
939 its_own(named, pointer, base).map(|whole| (base, offset, whole))
940}
941
942/// Whether a check's capability is about the address the check names or about the pointer that
943/// address was worked out from, and which of the two it is.
944///
945/// Both are shapes `rucc-safety` emits. The first is what it used to emit everywhere, a `cap_of` in
946/// front of each check naming the check's own pointer, and the second is what
947/// `rucc_safety::origin` emits now, one capability taken where the object came from and shared by
948/// every address walked off it. A check naming anything else is about some other instance and
949/// nothing here is entitled to read it.
950fn its_own(named: Value, pointer: Value, base: Value) -> Option<bool> {
951 if named == pointer {
952 return Some(false);
953 }
954 (named == base).then_some(true)
955}
956
957/// The bytes a check says belong to one instance.
958///
959/// Which is the access and nothing else when the capability was taken at the address, and the
960/// access together with everything between it and the base when the capability was taken at the
961/// base. The second is not a widening this pass made up. A capability names the instance its own
962/// pointer is in, so the base is in that instance by the meaning of the operand, the access is in
963/// it because that is what the check asks, and an instance is a run of bytes, so everything between
964/// the two is in it as well.
965///
966/// That is what makes reading the second shape sound, and it has to be the fact rather than a note
967/// on the side, because a fact is the thing both the asking and the recording go through. Asking
968/// with it means whatever answers holds the base too, so the instance the answer is about is the
969/// instance the capability names. Recording it after a check that stays is recording what the check
970/// proves, and it is more than the narrow one, which is the whole reason a capability taken at the
971/// base is worth having here.
972fn hull(base: Value, offset: i128, size: i128, whole: bool) -> Option<Fact> {
973 if !whole {
974 return Some(Fact { base, offset, size });
975 }
976 let low = offset.min(0);
977 let high = offset.checked_add(size)?.max(1);
978 Some(Fact { base, offset: low, size: high.checked_sub(low)? })
979}
980
981/// The two ends of a `check_deriv`, each as the single byte at it.
982///
983/// A derivation check asks whether the pointer that came out of a `ptr_add` is still in the storage
984/// instance the pointer that went in belongs to, so both ends have to be readable and both have to
985/// come out of the same value, which is what makes the two offsets comparable at all. One byte each
986/// because that is what is being asked about: not a range, but whether an address is in an instance.
987///
988/// The capability has to be about the pointer that went in, for the reason [`addressed`] gives. The
989/// instance the check is about is the one that pointer belongs to, and a check naming some other
990/// capability is about some other instance.
991///
992/// The width operand is not read. It matters to the runtime only for a pointer that walked off the
993/// near end, where the check passes on the byte a stride further along instead of on the address
994/// itself, and this pass never gets that far: it discharges nothing it has not put inside a range
995/// outright.
996pub(crate) fn derives(func: &Func, check: Inst) -> Option<(Fact, Fact)> {
997 let args = &func[func[check].args];
998 let &capability = args.first()?;
999 let &from = args.get(1)?;
1000 let &to = args.get(2)?;
1001 let (base, start) = normal(func, from);
1002 let named = operand_of(func, capability, Opcode::CapOf, 0)?;
1003 let whole = its_own(named, from, base)?;
1004 let (walked, end) = normal(func, to);
1005 if base != walked {
1006 return None;
1007 }
1008 // One fact has to hold both ends, so widening the near one to reach the base is what carries
1009 // the base into whatever answers, which is what [`hull`] is for. The far end is left as it is,
1010 // since the one fact that holds the pair holds it.
1011 Some((hull(base, start, 1, whole)?, Fact { base, offset: end, size: 1 }))
1012}
1013
1014/// The object a local is, when the address a check is about was computed from one.
1015///
1016/// This is the fact nobody had to check for, and section 7.2 puts it first of the four sources
1017/// because it is where most of the win is. An `alloca` of a fixed size is one storage instance of
1018/// that many bytes, said by the instruction that makes it rather than by a check that passed, so
1019/// the bytes from its address to that many further along are inside one instance for the same
1020/// reason a passing `check_bounds` says its own range is.
1021///
1022/// Only the fixed size form. The one that takes an operand is a variable length array, and how
1023/// many bytes it is is a value the program works out rather than a number in the payload, where
1024/// the field reads zero.
1025///
1026/// The fact holds everywhere in the function and no call takes it away, which is the other half of
1027/// what makes it worth having. A callee cannot free a frame slot: what it could free is whatever a
1028/// pointer stored in the slot points at, and that is a different instance and a different check.
1029/// So this is asked separately from the facts the walk carries rather than pushed into them, since
1030/// everything in there is thrown away at the first call this pass cannot see through.
1031fn declared(func: &Func, base: Value) -> Option<Fact> {
1032 let Def::Result { inst, .. } = func[base].def else { return None };
1033 if func[inst].opcode != Opcode::Alloca || !func[func[inst].args].is_empty() {
1034 return None;
1035 }
1036 let Extra::Mem(info) = func[inst].extra else { return None };
1037 Some(Fact::whole(base, i128::from(func[info].size)))
1038}
1039
1040/// The alignment an allocator promises, in bytes.
1041///
1042/// C says storage an allocator hands back is aligned for any object with a fundamental alignment,
1043/// which is sixteen bytes on the targets this compiles for. Eight is claimed rather than sixteen
1044/// because the claim has to hold wherever this pass runs and the pass is given a function rather
1045/// than a target. What it costs is an access that assumes more than eight bytes, which is a
1046/// `long double` or a vector, keeping a check it could have lost.
1047const ALLOCATED: u64 = 8;
1048
1049/// How far into an expression [`divides`] reads before it gives up.
1050///
1051/// A subscript is a multiply and a constant and the answer is two steps in. The bound is here
1052/// because the walk is over an expression the program wrote and nothing about an expression stops
1053/// it from being as deep as the source file is long.
1054const DEEP: u32 = 4;
1055
1056/// Whether the address a check is about starts where the access assumes it does.
1057///
1058/// The alignment conjunct of judgement J1 rides on `check_bounds`, which document 06 section 6.3
1059/// settled, so a check that goes takes the test of it with it and something here has to have
1060/// answered it first. An access that assumes nothing about where it starts has nothing to answer,
1061/// and that is what an alignment of one is and what a member of a packed record gets.
1062///
1063/// What answers it is the object the address was computed from and the steps taken from it, which
1064/// is the same ground the bounds question walks. An `alloca` says what it is aligned to and an
1065/// allocator promises [`ALLOCATED`], and each step from there leaves whatever the step itself
1066/// divides by. So `p[i]` on an `int *` out of `malloc` is answered by the four in the subscript's
1067/// own multiply, and `(int *)(p + 1)` is not answered at all, which is row S7 and the whole reason
1068/// this is here.
1069///
1070/// A global is not read here at all. It arrives as [`Flags::ALIGNED`] from `crate::extents`, which
1071/// is given the module this is not, and the flag is the whole of what this asks about one.
1072///
1073/// A pointer this cannot read the origin of is zero, which answers nothing and keeps the check.
1074/// That is a block parameter, a pointer loaded out of memory, and one handed in.
1075/// [`UNKNOWN_ALIGNMENT`] counts them.
1076///
1077/// The two answers given before [`settles`] is asked are not arithmetic and so are not a rule's.
1078/// An access of one byte assumes nothing about where it starts, so there is nothing to prove about
1079/// it, and the flag is a fact `crate::extents` established over the whole module and wrote down.
1080fn aligned(func: &Func, check: Inst) -> bool {
1081 let Extra::Mem(info) = func[check].extra else { return false };
1082 let claim = u64::from(func[info].align);
1083 if claim <= 1 || func[check].flags.contains(Flags::ALIGNED) {
1084 return true;
1085 }
1086 let Some(&pointer) = func[func[check].args].get(1) else { return false };
1087 settles(settled(func, pointer), claim)
1088}
1089
1090/// Whether an address known to be a multiple of one number meets an access's claim.
1091///
1092/// The companion to [`covers`] for the alignment conjunct, and it decides nothing either. The walk
1093/// in [`settled`] worked out a number the address divides by, and whether that answers the access
1094/// is the rule file's to say. The address is opaque in the question, because nothing here knows
1095/// what it is and the answer is about every address the walk's number holds of.
1096///
1097/// It is worth saying what the rule catches that the comparison it replaced did not. `known` being
1098/// the larger number is not `claim` dividing it, and the two agree only because both are powers of
1099/// two. Every number that gets here is one, for the reason the head in `safety.model` writes out,
1100/// and now that reason is written somewhere a solver reads rather than only somewhere a person
1101/// does.
1102fn settles(known: u64, claim: u64) -> bool {
1103 let mut question = Question::default();
1104 let at = question.opaque();
1105 let at = question.app("value.i64", &[at]);
1106 let known = question.number(i128::from(known));
1107 let known = question.app("iconst.i64", &[known]);
1108 let claim = question.number(i128::from(claim));
1109 let claim = question.app("iconst.i64", &[claim]);
1110 let term = question.app("aligned.i64", &[at, known, claim]);
1111 match safety::TABLE.find(&question, term) {
1112 Some(found) => yes(&safety::TABLE, found.rule),
1113 None => false,
1114 }
1115}
1116
1117/// What a pointer is known to be aligned to, in bytes, or zero when nothing here says.
1118///
1119/// Every number involved is a power of two, so the greatest common divisor of two of them is the
1120/// smaller, which is why the steps are gathered with a `min` and why they start at the largest
1121/// number there is instead of at zero. Zero is the answer and not a step, since an alignment of
1122/// zero is not something an access can assume and a claim is never met by one.
1123fn settled(func: &Func, pointer: Value) -> u64 {
1124 let mut steps = u64::MAX;
1125 let mut value = pointer;
1126 loop {
1127 let Def::Result { inst, .. } = func[value].def else { return 0 };
1128 match func[inst].opcode {
1129 Opcode::Alloca => {
1130 let Extra::Mem(info) = func[inst].extra else { return 0 };
1131 return steps.min(u64::from(func[info].align));
1132 }
1133 Opcode::Call if func[inst].flags.contains(Flags::HEAP) => {
1134 return steps.min(ALLOCATED);
1135 }
1136 Opcode::PtrAdd => {
1137 let args = &func[func[inst].args];
1138 let (Some(&from), Some(&by)) = (args.first(), args.get(1)) else { return 0 };
1139 steps = steps.min(divides(func, by, DEEP));
1140 value = from;
1141 }
1142 _ => return 0,
1143 }
1144 }
1145}
1146
1147/// The largest power of two that divides a step, or one when nothing here says.
1148///
1149/// One is the answer for anything unreadable and it is the right one: every number divides by one,
1150/// so a step nobody can read leaves a pointer aligned to a byte and no more. Zero divides by
1151/// everything, which is a walk that took no step and has to leave what it started with alone.
1152fn divides(func: &Func, step: Value, depth: u32) -> u64 {
1153 if let Some(number) = constant(func, step) {
1154 let Ok(size) = u64::try_from(number.unsigned_abs()) else { return 1 };
1155 return if size == 0 { u64::MAX } else { 1 << size.trailing_zeros() };
1156 }
1157 let Def::Result { inst, .. } = func[step].def else { return 1 };
1158 let args = &func[func[inst].args];
1159 let (Some(&left), Some(&right)) = (args.first(), args.get(1)) else { return 1 };
1160 if depth == 0 {
1161 return 1;
1162 }
1163 match func[inst].opcode {
1164 // A subscript, which is an index nobody knows anything about times the element size.
1165 Opcode::Mul => {
1166 divides(func, left, depth - 1).saturating_mul(divides(func, right, depth - 1))
1167 }
1168 Opcode::Shl => match constant(func, right) {
1169 Some(by) if (0..64).contains(&by) => {
1170 divides(func, left, depth - 1).checked_shl(by as u32).unwrap_or(u64::MAX)
1171 }
1172 _ => 1,
1173 },
1174 // Two numbers added divide by whatever they both divide by, which is a field offset added
1175 // to a subscript and is how a member of an array of records comes out.
1176 Opcode::Add | Opcode::Sub => {
1177 divides(func, left, depth - 1).min(divides(func, right, depth - 1))
1178 }
1179 _ => 1,
1180 }
1181}
1182
1183/// The object an allocator made, when the address a check is about was computed from one and this
1184/// function has already found out it is not null.
1185///
1186/// The same shape as [`declared`] one storey up, with a marked call saying the size instead of an
1187/// `alloca` and one more thing to establish. `crate::heap` has the argument for both halves: what a
1188/// call to `malloc` says is an extent and never a lifetime, and it only says it where the program
1189/// has looked, because a null pointer is inside no object and a check on one is a check that is
1190/// meant to fail.
1191///
1192/// Nothing is claimed when the graph was not built, which is a function this found no allocation in
1193/// and so a function where the answer would have been no anyway.
1194fn allocation(
1195 func: &Func,
1196 cfg: Option<&Cfg>,
1197 checked: &mut HashMap<Value, HashSet<Block>>,
1198 block: Block,
1199 base: Value,
1200) -> Option<Fact> {
1201 let whole = heap::made(func, base)?;
1202 let cfg = cfg?;
1203 checked
1204 .entry(whole.base)
1205 .or_insert_with(|| heap::tested(func, cfg, whole.base))
1206 .contains(&block)
1207 .then_some(whole)
1208}
1209
1210/// Whether all of those bytes are inside one object an allocator made.
1211///
1212/// Every part has to be inside, and inside the same object, which is what asking [`covers`] with one
1213/// fact and several does.
1214fn allocated(
1215 func: &Func,
1216 cfg: Option<&Cfg>,
1217 checked: &mut HashMap<Value, HashSet<Block>>,
1218 block: Block,
1219 parts: &[&Fact],
1220) -> bool {
1221 let Some(first) = parts.first() else { return false };
1222 let Some(whole) = allocation(func, cfg, checked, block, first.base) else { return false };
1223 parts.iter().all(|part| covers(&whole, part))
1224}
1225
1226/// Whether every address a walk can reach is inside one object an allocator made.
1227///
1228/// [`allocated`] for the question [`reach`] and [`spread`] ask. The object comes from the same place
1229/// and is believed for the same reason, and what is asked of it is [`reaches`] rather than
1230/// [`covers`], so a walk by a step the ranges put numbers on can be answered by a call that says how
1231/// many bytes it made.
1232///
1233/// The wide path used to ask a local and the facts the walk carries and nothing else, so a program
1234/// that walked into its own `malloc` by an index kept its checks however plainly the size was
1235/// written. That is the first half of tamnd/rucc#880.
1236fn allocated_around(
1237 func: &Func,
1238 cfg: Option<&Cfg>,
1239 checked: &mut HashMap<Value, HashSet<Block>>,
1240 block: Block,
1241 spans: &[&Reach],
1242) -> bool {
1243 let Some(first) = spans.first() else { return false };
1244 let Some(whole) = allocation(func, cfg, checked, block, first.base) else { return false };
1245 spans.iter().all(|span| reaches(&whole, span))
1246}
1247
1248/// A lifetime fact grown from one address to the checked range it sits in.
1249///
1250/// The argument is in the module comment: a `check_bounds` that passed put its whole range inside
1251/// one instance, so the instance this lifetime check found alive is the instance that range is in.
1252/// With no range around the address the fact stays as it came, which is correct and answers only a
1253/// repeat of the very same check.
1254///
1255/// A local is asked about first, because the object it is is the widest range there can be for an
1256/// address computed from it and a wider fact answers more later checks. What that gives is a
1257/// lifetime check anywhere in a local discharging every later one in the same local, up to the
1258/// first call, which is the shape a function that reads several fields of a local struct has.
1259fn widened(func: &Func, bounds: &Known, asked: Fact) -> Fact {
1260 if let Some(local) = declared(func, asked.base).filter(|local| covers(local, &asked)) {
1261 return local;
1262 }
1263 bounds.held.iter().find(|fact| covers(fact, &asked)).copied().unwrap_or(asked)
1264}
1265
1266/// The value an address was computed from, and how far past it the address is.
1267///
1268/// A `ptr_add` over a constant is walked through, and anything else is where the answer stops. The
1269/// arithmetic here is exact because it is done in `i128` over offsets that came out of the IR as
1270/// sixty four bit constants, and whether it is small enough to mean anything at sixty four bits is
1271/// the rule's question rather than this function's.
1272pub(crate) fn normal(func: &Func, value: Value) -> (Value, i128) {
1273 let mut base = value;
1274 let mut offset: i128 = 0;
1275 while let Some((from, step)) = walked(func, base) {
1276 let Some(sum) = offset.checked_add(step) else { break };
1277 base = from;
1278 offset = sum;
1279 }
1280 (base, offset)
1281}
1282
1283/// Every address a walk can reach, when a step it takes is a value rather than a constant.
1284///
1285/// This is the third of the four sources section 7.2 lists, and it is the one that needs an
1286/// analysis. [`normal`] stops at the first `ptr_add` whose step it cannot read, and what it hands
1287/// back is a fact about a base nobody knows the size of. Document 10's ranges do know something
1288/// about the step: an index the program has already tested, or one a loop counts, is bounded even
1289/// though it is not constant. So the walk carries on past the step, adding the low end of its
1290/// range to the offset and the width of the range to the size.
1291///
1292/// What comes out is a range of addresses the access can land in, and it is a [`Reach`] rather than
1293/// a [`Fact`] on purpose. Whether an object holding all of that range holds the one address the
1294/// access actually uses is [`reaches`], which asks a rule with the distance left opaque, so one
1295/// answer covers every value the step could take.
1296///
1297/// It is only ever asked with. What this returns must never be recorded as established, and the
1298/// one place it could be is the push in the `check_bounds` arm, which happens only where this
1299/// returned nothing or answered nothing. The reason is that the widened range is not what a check
1300/// proves. A check that runs and passes proves the address the program used was inside the object,
1301/// and says nothing at all about the rest of the range this function made up around it.
1302fn reach(func: &Func, ranges: Option<&mut Ranges<'_>>, asked: &Fact, at: Inst) -> Option<Reach> {
1303 let wide = spanned(func, ranges?, asked.base, asked.offset, asked.size, at)?;
1304 // Nothing was walked past, so this is the fact that came in and asking it again is work
1305 // somebody already did.
1306 (wide.base != asked.base).then_some(wide)
1307}
1308
1309/// Which of the reasons a derivation check this pass could not read is kept for.
1310///
1311/// The census and nothing else. Whether the check goes has already been decided by the time this
1312/// runs, and what it answers is the question somebody reading `-fopt-info-missed` is actually
1313/// asking, which is what would have to be built for this pile to move.
1314///
1315/// It walks the same ground [`spread`] walks rather than being folded into it, because the two want
1316/// different things. [`spread`] wants an answer or nothing, and stopping at the first step it cannot
1317/// read is the fastest way to nothing. This wants to get as far as it can and name where it stopped,
1318/// so it runs only on checks that are staying and it is allowed to be the slower of the two.
1319///
1320/// The five that begin `nothing here says how big` are one refusal counted five ways. What is missing
1321/// in every one of them is how many bytes belong to the object, and where the pointer came from is
1322/// what says which piece of work would supply it: `__counted_by` and the type plane for a pointer out
1323/// of memory, section 7.5's summaries for one that was handed over, `crate::extents` reaching further
1324/// for a global, and the allocation summaries for one a call returned.
1325fn unreadable(func: &Func, ranges: Option<&mut Ranges<'_>>, check: Inst) -> &'static str {
1326 let args = &func[func[check].args];
1327 let (Some(&capability), Some(&from), Some(&to)) = (args.first(), args.get(1), args.get(2))
1328 else {
1329 return NO_EXTENT_OTHER;
1330 };
1331 if operand_of(func, capability, Opcode::CapOf, 0) != Some(from) {
1332 return NOT_ITS_CAPABILITY_DERIV;
1333 }
1334 // No ranges is a function with no walk in it that steps by a value, so every step here was a
1335 // constant, so the reader that gives up on two bases gave up on two bases.
1336 let Some(ranges) = ranges else { return TWO_BASES_DERIV };
1337 let (base, offset) = normal(func, from);
1338 let Some(near) = spanned(func, ranges, base, offset, 1, check) else {
1339 return NO_EXTENT_OTHER;
1340 };
1341 let (base, offset) = normal(func, to);
1342 let Some(far) = spanned(func, ranges, base, offset, 1, check) else {
1343 return NO_EXTENT_OTHER;
1344 };
1345 if near.base != far.base {
1346 return TWO_BASES_DERIV;
1347 }
1348 if declared(func, near.base).is_some() {
1349 return OVER_THE_LOCAL_DERIV;
1350 }
1351 match func[near.base].def {
1352 Def::Param { .. } => NO_EXTENT_HANDED,
1353 Def::Result { inst, .. } => match func[inst].opcode {
1354 Opcode::Load => NO_EXTENT_LOADED,
1355 Opcode::GlobalAddr => NO_EXTENT_GLOBAL,
1356 Opcode::Call | Opcode::CallIndirect => NO_EXTENT_RETURNED,
1357 _ => NO_EXTENT_OTHER,
1358 },
1359 }
1360}
1361
1362/// The two ends of a derivation check, each as the range of addresses it can be at.
1363///
1364/// A derivation check asks whether the pointer that came out of a walk is still in the storage
1365/// instance the pointer that went in belongs to. [`derives`] answers that only when both ends
1366/// normalize to one base over constants, and past a step the constant reader gives up on they do
1367/// not, which is why this reads the check's operands again rather than taking what that worked
1368/// out. Each end becomes a range, and the two still have to be off one base or there is nothing
1369/// comparable to ask about.
1370///
1371/// One byte each, for the reason [`derives`] gives. Nothing here claims anything about how many
1372/// bytes are readable at either address.
1373///
1374/// The capability has to be the `cap_of` of the pointer that went in, for the reason [`addressed`]
1375/// gives. Only that one, where [`derives`] reads a capability taken at the base the address was
1376/// worked out from as well: a range this reached by walking past a step comes back off a base of
1377/// its own, which is not the base the capability names, so there is nothing to widen towards.
1378fn spread(
1379 func: &Func,
1380 ranges: Option<&mut Ranges<'_>>,
1381 check: Inst,
1382 at: Inst,
1383) -> Option<(Reach, Reach)> {
1384 let ranges = ranges?;
1385 let args = &func[func[check].args];
1386 let &capability = args.first()?;
1387 let &from = args.get(1)?;
1388 let &to = args.get(2)?;
1389 if operand_of(func, capability, Opcode::CapOf, 0) != Some(from) {
1390 return None;
1391 }
1392 let (base, offset) = normal(func, from);
1393 let near = spanned(func, ranges, base, offset, 1, at)?;
1394 let (base, offset) = normal(func, to);
1395 let far = spanned(func, ranges, base, offset, 1, at)?;
1396 (near.base == far.base).then_some((near, far))
1397}
1398
1399/// Every address a walk off `base` can reach, and how many bytes it takes when it gets there.
1400///
1401/// The loop is [`normal`]'s with one more thing to try. A `ptr_add` over a constant is walked
1402/// through the same way, and a `ptr_add` over a value is walked through when document 10's ranges
1403/// put numbers on that value: the low end of the range goes on the distance and the width of it on
1404/// the slack. Anything else is where the walk stops.
1405///
1406/// Nothing is returned when a step is a value the ranges say nothing useful about, rather than the
1407/// walk stopping there and handing back what it had. What it had would be a range off a `ptr_add`
1408/// nobody knows the size of, which answers nothing, so stopping would be a longer way of saying no.
1409fn spanned(
1410 func: &Func,
1411 ranges: &mut Ranges<'_>,
1412 base: Value,
1413 offset: i128,
1414 size: i128,
1415 at: Inst,
1416) -> Option<Reach> {
1417 let mut base = base;
1418 let mut low = offset;
1419 let mut width: i128 = 0;
1420 loop {
1421 // A constant step again, because past a step that needed a range there can be more of
1422 // them, and the frontend leaves a field offset as a constant under an array index.
1423 if let Some((from, step)) = walked(func, base) {
1424 low = low.checked_add(step)?;
1425 base = from;
1426 continue;
1427 }
1428 let Some(from) = operand_of(func, base, Opcode::PtrAdd, 0) else { break };
1429 let by = operand_of(func, base, Opcode::PtrAdd, 1)?;
1430 let (least, most) = ranges.at_inst(by, at).signed_bounds()?;
1431 low = low.checked_add(least)?;
1432 width = width.checked_add(most.checked_sub(least)?)?;
1433 base = from;
1434 }
1435 Some(Reach { base, low, width, size })
1436}
1437
1438/// Whether any walk in this function steps by a value rather than a constant.
1439///
1440/// The question the ranges are built for. A function without one of these would pay for a copy of
1441/// the control flow graph and never ask anything of it.
1442/// Whether anything in this function says a lifetime is over.
1443///
1444/// Nothing emits `meta_end` today, so this is false everywhere and the frame slot rule in
1445/// [`Discharge::run`] is on for every function. It is written anyway, and written over the whole
1446/// function rather than along the walk, because the day something does emit one the cheap reading
1447/// is the wrong one: a lifetime that ended in one arm of a branch has ended for a check after the
1448/// join, and a walk down the dominator tree would not have seen it. Turning the rule off for the
1449/// function is the reading that stays right when that day comes, and the finer one is a job for
1450/// whoever makes `meta_end` appear.
1451fn ends_a_lifetime(func: &Func) -> bool {
1452 func.blocks().any(|block| func.insts(block).any(|inst| func[inst].opcode == Opcode::MetaEnd))
1453}
1454
1455fn walks_by_a_value(func: &Func) -> bool {
1456 func.blocks().any(|block| {
1457 func.insts(block).any(|inst| {
1458 func[inst].opcode == Opcode::PtrAdd
1459 && func[func[inst].args].get(1).is_some_and(|&by| constant(func, by).is_none())
1460 })
1461 })
1462}
1463
1464/// The pointer one `ptr_add` over a constant was computed from, and by how much.
1465fn walked(func: &Func, value: Value) -> Option<(Value, i128)> {
1466 let from = operand_of(func, value, Opcode::PtrAdd, 0)?;
1467 let by = operand_of(func, value, Opcode::PtrAdd, 1)?;
1468 Some((from, constant(func, by)?))
1469}
1470
1471/// Operand `index` of the instruction that produced `value`, when that instruction is `opcode`.
1472pub(crate) fn operand_of(func: &Func, value: Value, opcode: Opcode, index: usize) -> Option<Value> {
1473 let Def::Result { inst, .. } = func[value].def else { return None };
1474 if func[inst].opcode != opcode {
1475 return None;
1476 }
1477 func[func[inst].args].get(index).copied()
1478}
1479
1480/// The value of an integer constant, read with its own sign.
1481pub(crate) fn constant(func: &Func, value: Value) -> Option<i128> {
1482 let Def::Result { inst, .. } = func[value].def else { return None };
1483 if func[inst].opcode != Opcode::IConst {
1484 return None;
1485 }
1486 let Extra::Imm(imm) = func[inst].extra else { return None };
1487 let ty = func[value].ty;
1488 ty.is_int().then(|| func[imm].signed(ty))
1489}
1490
1491/// Whether an established fact answers the check being asked about.
1492///
1493/// This function decides nothing. It puts the two together into the term the rule file is written
1494/// about and asks the table, which is the whole of section 7.7's split: the paragraph above worked
1495/// out that the two addresses are one value a constant apart, and whether that is enough is
1496/// somebody's proof rather than this file's opinion.
1497pub(crate) fn covers(fact: &Fact, asked: &Fact) -> bool {
1498 if fact.base != asked.base {
1499 return false;
1500 }
1501 let Some(delta) = asked.offset.checked_sub(fact.offset) else { return false };
1502 let mut question = Question::default();
1503 let at = question.opaque();
1504 let at = question.app("value.i64", &[at]);
1505 let span = question.number(fact.size);
1506 let span = question.app("iconst.i64", &[span]);
1507 let far = question.number(delta);
1508 let far = question.app("iconst.i64", &[far]);
1509 let reach = question.number(asked.size);
1510 let reach = question.app("iconst.i64", &[reach]);
1511 let term = question.app("covered.i64", &[at, span, far, reach]);
1512 match safety::TABLE.find(&question, term) {
1513 Some(found) => yes(&safety::TABLE, found.rule),
1514 None => false,
1515 }
1516}
1517
1518/// Whether an object holds every address a walk can land on.
1519///
1520/// The companion to [`covers`] for the question [`reach`] asks, and it decides nothing either. It
1521/// puts the object and the range of addresses into the term the rule file is written about and
1522/// asks the table. The distance the program actually walks is opaque in the question, which is
1523/// what makes one answer cover every value it could take.
1524fn reaches(fact: &Fact, asked: &Reach) -> bool {
1525 if fact.base != asked.base {
1526 return false;
1527 }
1528 let Some(delta) = asked.low.checked_sub(fact.offset) else { return false };
1529 let mut question = Question::default();
1530 let at = question.opaque();
1531 let at = question.app("value.i64", &[at]);
1532 let span = question.number(fact.size);
1533 let span = question.app("iconst.i64", &[span]);
1534 let delta = question.number(delta);
1535 let delta = question.app("iconst.i64", &[delta]);
1536 let width = question.number(asked.width);
1537 let width = question.app("iconst.i64", &[width]);
1538 let size = question.number(asked.size);
1539 let size = question.app("iconst.i64", &[size]);
1540 let step = question.opaque();
1541 let step = question.app("value.i64", &[step]);
1542 let term = question.app("reached.i64", &[at, span, delta, width, size, step]);
1543 match safety::TABLE.find(&question, term) {
1544 Some(found) => yes(&safety::TABLE, found.rule),
1545 None => false,
1546 }
1547}
1548
1549/// Whether the rule that fired answers yes.
1550///
1551/// A discharge rule replaces the question with a constant, and one is yes. Every rule in the file
1552/// answers that today, and reading it off the rule rather than assuming it is what keeps this
1553/// honest on the day one of them answers something else.
1554pub(crate) fn yes(table: &Table, rule: usize) -> bool {
1555 matches!(table.rules[rule].replacement, [Piece::App { .. }, Piece::Int(1)])
1556}
1557
1558/// A term built to be asked about, and nothing else.
1559///
1560/// The rules are matched against this rather than against the function, because what is being asked
1561/// about is not in the function: it is what the walk worked out about two of its instructions. So
1562/// the subject is a small arena of exactly the term being asked, built fresh for each question and
1563/// thrown away with the answer.
1564#[derive(Debug, Default)]
1565pub(crate) struct Question {
1566 held: Vec<Held>,
1567}
1568
1569/// One node of that term.
1570#[derive(Debug)]
1571enum Held {
1572 /// A number the pattern can read and a guard can be about.
1573 Int(i128),
1574 /// A head and its arguments.
1575 App(&'static str, Vec<usize>),
1576 /// Something with no structure, which is how an address the rule only names is written.
1577 Opaque,
1578}
1579
1580impl Question {
1581 /// Adds a constant and gives back where it went.
1582 ///
1583 /// Named for what it adds rather than for what it holds, because the arena also answers
1584 /// [`Subject::int`] and one name for the two would read as though building a term and asking
1585 /// about one were the same act.
1586 pub(crate) fn number(&mut self, value: i128) -> usize {
1587 self.held.push(Held::Int(value));
1588 self.held.len() - 1
1589 }
1590
1591 /// Adds an application of `head` to what is already in the arena.
1592 pub(crate) fn app(&mut self, head: &'static str, args: &[usize]) -> usize {
1593 self.held.push(Held::App(head, args.to_vec()));
1594 self.held.len() - 1
1595 }
1596
1597 /// Adds something the rule can bind and cannot look inside.
1598 pub(crate) fn opaque(&mut self) -> usize {
1599 self.held.push(Held::Opaque);
1600 self.held.len() - 1
1601 }
1602}
1603
1604impl Subject for Question {
1605 type Node = usize;
1606
1607 fn head(&self, node: usize) -> Option<(&str, usize)> {
1608 match &self.held[node] {
1609 Held::App(head, args) => Some((head, args.len())),
1610 Held::Int(_) | Held::Opaque => None,
1611 }
1612 }
1613
1614 fn arg(&self, node: usize, index: usize) -> usize {
1615 match &self.held[node] {
1616 Held::App(_, args) => args[index],
1617 // The walk only asks for an argument `head` said was there, so this is unreachable
1618 // rather than a case with an answer.
1619 Held::Int(_) | Held::Opaque => unreachable!("only an application has arguments"),
1620 }
1621 }
1622
1623 fn int(&self, node: usize) -> Option<i128> {
1624 match self.held[node] {
1625 Held::Int(value) => Some(value),
1626 Held::App(..) | Held::Opaque => None,
1627 }
1628 }
1629
1630 fn same(&self, a: usize, b: usize) -> bool {
1631 // Every node of a question is written once, so two places holding one thing are one place.
1632 a == b
1633 }
1634}
1635
1636#[cfg(test)]
1637mod tests {
1638 use rucc_base::Interner;
1639 use rucc_ir::{
1640 AsmInfo, Block, BlockCallList, Builder, Extra, Flags, Func, Inst, InstData, IntPred,
1641 MemInfo, MemOrder, Opcode, Restrict, Signature, Type, Value,
1642 };
1643
1644 use super::{DISCHARGE, Fact};
1645 use crate::stats::Kind;
1646 use crate::{Fuel, Pass, pass};
1647
1648 /// A function taking a pointer, with one block, ready to have accesses put in it.
1649 fn blank() -> (Interner, Func, Block, Value) {
1650 let mut names = Interner::new();
1651 let name = names.intern("f");
1652 let mut func = Func::new(name, Signature::new().with_params(&[Type::PTR]));
1653 let block = func.create_block();
1654 let pointer = func.append_param(block, Type::PTR);
1655 (names, func, block, pointer)
1656 }
1657
1658 /// Puts `cap_of` and a `check_bounds` over `size` bytes at `pointer` into a block.
1659 ///
1660 /// The same shape `rucc-safety` emits, written out here rather than reached for, because
1661 /// `rucc-opt` is rank 9 alongside `rucc-safety` and cannot depend on it.
1662 fn check(build: &mut Builder<'_>, pointer: Value, size: u64) {
1663 checking_at(build, pointer, pointer, size);
1664 }
1665
1666 /// The same, with the capability taken at `from` rather than at the address being checked.
1667 ///
1668 /// What `rucc_safety::origin` writes, once a capability belongs to a pointer rather than to an
1669 /// access: a field read off a struct is checked through the capability the struct's pointer
1670 /// got, and there is one of those for the whole function rather than one per field.
1671 fn checking_at(build: &mut Builder<'_>, from: Value, pointer: Value, size: u64) {
1672 let args = build.func().push_values(&[from]);
1673 let capability = build.value(InstData { args, ..InstData::new(Opcode::CapOf) }, Type::CAP);
1674 let info = MemInfo {
1675 size,
1676 align: 1,
1677 order: MemOrder::NotAtomic,
1678 tbaa: None,
1679 owns: 0,
1680 restrict: Restrict::NONE,
1681 };
1682 let args = build.func().push_values(&[capability, pointer]);
1683 let extra = Extra::Mem(build.func().add_mem(info));
1684 build.inst(InstData { args, extra, ..InstData::new(Opcode::CheckBounds) }, &[]);
1685 }
1686
1687 /// Puts `cap_of` and a `check_live` at `pointer` into a block.
1688 ///
1689 /// `rucc-safety` emits this straight after the bounds check for the same access and shares the
1690 /// one `cap_of` between the two. Sharing it is not what the pass reads, so the tests build a
1691 /// second one, which is the harder shape for it to accept.
1692 fn live(build: &mut Builder<'_>, pointer: Value) {
1693 let args = build.func().push_values(&[pointer]);
1694 let capability = build.value(InstData { args, ..InstData::new(Opcode::CapOf) }, Type::CAP);
1695 let args = build.func().push_values(&[capability, pointer]);
1696 build.inst(InstData { args, ..InstData::new(Opcode::CheckLive) }, &[]);
1697 }
1698
1699 /// Both checks in front of one access, in the order `rucc-safety` writes them.
1700 fn access(build: &mut Builder<'_>, pointer: Value, size: u64) {
1701 check(build, pointer, size);
1702 live(build, pointer);
1703 }
1704
1705 /// A pointer `bytes` past another one.
1706 fn past(build: &mut Builder<'_>, pointer: Value, bytes: i128) -> Value {
1707 let offset = build.iconst(Type::int(64), bytes);
1708 let args = build.func().push_values(&[pointer, offset]);
1709 build.value(InstData { args, ..InstData::new(Opcode::PtrAdd) }, Type::PTR)
1710 }
1711
1712 /// Puts the flag `crate::extents` writes onto every check in a function.
1713 ///
1714 /// The pass reads what the IR says, so what a test has to build is an IR that says it. Working
1715 /// out which checks deserve it is `crate::extents`, is about a module rather than a function,
1716 /// and has its own tests.
1717 fn marked(func: &mut Func) {
1718 flagged(func, Flags::STATIC);
1719 }
1720
1721 /// Puts that flag on every check in the function, the way an annotator before the pipeline
1722 /// would have.
1723 fn flagged(func: &mut Func, flag: Flags) {
1724 let insts: Vec<Inst> =
1725 func.blocks().flat_map(|block| func.insts(block).collect::<Vec<_>>()).collect();
1726 for inst in insts {
1727 let check = matches!(
1728 func[inst].opcode,
1729 Opcode::CheckBounds | Opcode::CheckLive | Opcode::CheckDeriv
1730 );
1731 if check {
1732 func[inst].flags |= flag;
1733 }
1734 }
1735 }
1736
1737 /// How many checks are left in a function.
1738 fn checks(func: &Func) -> usize {
1739 func.blocks()
1740 .flat_map(|block| func.insts(block).collect::<Vec<_>>())
1741 .filter(|&inst| func[inst].opcode == Opcode::CheckBounds)
1742 .count()
1743 }
1744
1745 /// How many lifetime checks are left in a function.
1746 fn lives(func: &Func) -> usize {
1747 func.blocks()
1748 .flat_map(|block| func.insts(block).collect::<Vec<_>>())
1749 .filter(|&inst| func[inst].opcode == Opcode::CheckLive)
1750 .count()
1751 }
1752
1753 fn run(func: &mut Func) -> crate::Stats {
1754 DISCHARGE.run(func, &mut crate::machine::fixtures::analyses(), &mut Fuel::unlimited())
1755 }
1756
1757 /// The same, with one of the measurement's variants rather than the pass the pipeline runs.
1758 fn run_with(pass: &super::Discharge, func: &mut Func) -> crate::Stats {
1759 pass.run(func, &mut crate::machine::fixtures::analyses(), &mut Fuel::unlimited())
1760 }
1761
1762 #[test]
1763 fn a_run_that_may_only_ask_an_object_leaves_what_dominance_would_have_taken() {
1764 // Two checks of the same bytes on a pointer that came from outside. Nothing here says how
1765 // big the object is, so the only thing that could answer the second one is the first one
1766 // having run, and a run that may not ask that has to keep both.
1767 let (_, mut func, block, pointer) = blank();
1768 let mut build = Builder::new(&mut func, block);
1769 check(&mut build, pointer, 4);
1770 check(&mut build, pointer, 4);
1771 build.ret(&[]);
1772 let stats = run_with(&super::OBJECTS, &mut func);
1773 assert_eq!(checks(&func), 2);
1774 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 0);
1775 }
1776
1777 #[test]
1778 fn a_run_that_may_only_ask_dominance_takes_the_second_check_of_the_same_bytes() {
1779 let (_, mut func, block, pointer) = blank();
1780 let mut build = Builder::new(&mut func, block);
1781 check(&mut build, pointer, 4);
1782 check(&mut build, pointer, 4);
1783 build.ret(&[]);
1784 let stats = run_with(&super::DOMINANCE, &mut func);
1785 assert_eq!(checks(&func), 1);
1786 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
1787 }
1788
1789 #[test]
1790 fn a_run_that_may_only_ask_dominance_leaves_a_check_inside_a_local() {
1791 // The other way round. One check, nothing in front of it, and the bytes are inside an
1792 // `alloca` whose size is written on it. Only the object can answer that one.
1793 let (_, mut func, block, _) = blank();
1794 let mut build = Builder::new(&mut func, block);
1795 let slot = local(&mut build, 16);
1796 check(&mut build, slot, 4);
1797 build.ret(&[]);
1798 let stats = run_with(&super::DOMINANCE, &mut func);
1799 assert_eq!(checks(&func), 1);
1800 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 0);
1801 assert_eq!(
1802 run_with(&super::OBJECTS, &mut func).count(Kind::Optimized, super::REMOVED_LOCAL),
1803 1
1804 );
1805 }
1806
1807 #[test]
1808 fn the_measurement_variants_answer_to_names_of_their_own() {
1809 // A run that cannot be reached by a flag is a run nobody can measure with.
1810 let names: Vec<&str> = [
1811 &DISCHARGE,
1812 &super::OBJECTS,
1813 &super::DOMINANCE,
1814 &super::SUMMARIES,
1815 &super::NARROW,
1816 &super::EVERY,
1817 ]
1818 .iter()
1819 .map(|pass| pass.name())
1820 .collect();
1821 assert_eq!(
1822 names,
1823 [
1824 "discharge",
1825 "discharge-objects",
1826 "discharge-dominance",
1827 "discharge-summaries",
1828 "discharge-narrow",
1829 "discharge-every"
1830 ]
1831 );
1832 for name in names {
1833 assert!(pass::find(name).is_some(), "`{name}` is not in the pass list");
1834 }
1835 }
1836
1837 #[test]
1838 fn a_second_check_of_the_same_bytes_goes() {
1839 let (_, mut func, block, pointer) = blank();
1840 let mut build = Builder::new(&mut func, block);
1841 check(&mut build, pointer, 4);
1842 check(&mut build, pointer, 4);
1843 build.ret(&[]);
1844 let stats = run(&mut func);
1845 assert_eq!(checks(&func), 1);
1846 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
1847 }
1848
1849 /// The same check over an access that assumes something about where it starts.
1850 ///
1851 /// [`check`] assumes nothing, which is the right default for the tests above it: what they are
1852 /// about is which bytes a check covers, and an access that assumes nothing has no alignment to
1853 /// answer and so reaches every rule. These are the ones about the alignment itself.
1854 fn assuming(build: &mut Builder<'_>, pointer: Value, size: u64, align: u32) {
1855 let args = build.func().push_values(&[pointer]);
1856 let capability = build.value(InstData { args, ..InstData::new(Opcode::CapOf) }, Type::CAP);
1857 let info = MemInfo {
1858 size,
1859 align,
1860 order: MemOrder::NotAtomic,
1861 tbaa: None,
1862 owns: 0,
1863 restrict: Restrict::NONE,
1864 };
1865 let args = build.func().push_values(&[capability, pointer]);
1866 let extra = Extra::Mem(build.func().add_mem(info));
1867 build.inst(InstData { args, extra, ..InstData::new(Opcode::CheckBounds) }, &[]);
1868 }
1869
1870 #[test]
1871 fn a_check_whose_alignment_nothing_here_settles_stays() {
1872 // A pointer from outside, so nothing says what it is aligned to, and a second check of the
1873 // same bytes that dominance would otherwise take. The bytes are covered and the alignment
1874 // is not, and the check tests both, so it stays. One remark and not two: the first check
1875 // was staying whatever anybody said about its alignment, and what the number is for is
1876 // what the gate costs.
1877 let (_, mut func, block, pointer) = blank();
1878 let mut build = Builder::new(&mut func, block);
1879 assuming(&mut build, pointer, 4, 4);
1880 assuming(&mut build, pointer, 4, 4);
1881 build.ret(&[]);
1882 let stats = run(&mut func);
1883 assert_eq!(checks(&func), 2);
1884 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 0);
1885 assert_eq!(stats.count(Kind::Missed, super::UNKNOWN_ALIGNMENT), 1);
1886 }
1887
1888 #[test]
1889 fn a_check_inside_a_local_at_an_offset_the_local_is_aligned_through_goes() {
1890 // An eight byte aligned slot read four bytes in, which is a member of a record and the
1891 // commonest access there is. The offset leaves four of the eight, the access assumes four,
1892 // and the check goes the way it did before any of this.
1893 let (_, mut func, block, _) = blank();
1894 let mut build = Builder::new(&mut func, block);
1895 let slot = local(&mut build, 16);
1896 let field = past(&mut build, slot, 4);
1897 assuming(&mut build, field, 4, 4);
1898 build.ret(&[]);
1899 let stats = run(&mut func);
1900 assert_eq!(checks(&func), 0);
1901 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 1);
1902 assert_eq!(stats.count(Kind::Missed, super::UNKNOWN_ALIGNMENT), 0);
1903 }
1904
1905 #[test]
1906 fn a_check_a_cast_moved_off_the_alignment_stays_however_well_its_bytes_are_covered() {
1907 // Row S7 written in IR. The bytes are inside the slot and the slot is aligned, but the
1908 // access starts one byte in and assumes four, and one byte in is where the alignment is
1909 // lost. This is the check the misaligned read needs and the one the accounting run found
1910 // going missing.
1911 let (_, mut func, block, _) = blank();
1912 let mut build = Builder::new(&mut func, block);
1913 let slot = local(&mut build, 16);
1914 let odd = past(&mut build, slot, 1);
1915 assuming(&mut build, odd, 4, 4);
1916 build.ret(&[]);
1917 let stats = run(&mut func);
1918 assert_eq!(checks(&func), 1);
1919 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 0);
1920 assert_eq!(stats.count(Kind::Missed, super::UNKNOWN_ALIGNMENT), 1);
1921 }
1922
1923 #[test]
1924 fn a_subscript_that_steps_by_the_width_it_reads_settles_its_own_alignment() {
1925 // `p[i]` on an `int *` an allocator made. Nobody knows what the index is, and nobody has
1926 // to: the step is the index times four, four divides it whatever the index turns out to
1927 // be, and the allocation it starts from is aligned to more than that.
1928 let (_, mut func, inside, _, pointer, index) = allocation(64);
1929 let mut build = Builder::new(&mut func, inside);
1930 let four = build.iconst(Type::int(64), 4);
1931 let step = build.binary(Opcode::Mul, index, four, Flags::NONE);
1932 let args = build.func().push_values(&[pointer, step]);
1933 let at = build.value(InstData { args, ..InstData::new(Opcode::PtrAdd) }, Type::PTR);
1934 assuming(&mut build, at, 4, 4);
1935 build.ret(&[]);
1936 let stats = run(&mut func);
1937 assert_eq!(stats.count(Kind::Missed, super::UNKNOWN_ALIGNMENT), 0);
1938 }
1939
1940 #[test]
1941 fn what_answers_an_alignment_claim_is_the_rule_and_not_a_comparison() {
1942 // The four cases the rule is asked about, and the fifth is the reason it is a rule. A
1943 // number larger than the claim and not a multiple of it answers nothing, and the guard is
1944 // written so that the question never gets asked with one, because `super::settled` only
1945 // ever gives back a power of two. The last one is the same point from the other end: the
1946 // largest number there is is larger than every claim and divides nothing, and what the
1947 // walk means by it is that it took no step rather than that it found an alignment.
1948 assert!(super::settles(8, 8));
1949 assert!(super::settles(16, 8));
1950 assert!(!super::settles(4, 8));
1951 assert!(!super::settles(0, 8));
1952 assert!(!super::settles(u64::MAX, 8));
1953 }
1954
1955 #[test]
1956 fn a_step_by_something_nobody_can_read_settles_nothing() {
1957 // A step the ranges do bound, so the bytes are answered and the check was on its way out,
1958 // and a step nothing says the low bits of, so where the access starts is not answered. A
1959 // mask of seven is nought to seven and three is one of those.
1960 let (_, mut func, block, _, index) = indexed();
1961 let mut build = Builder::new(&mut func, block);
1962 let slot = local(&mut build, 16);
1963 let step = low_bits(&mut build, index, 7);
1964 let args = build.func().push_values(&[slot, step]);
1965 let at = build.value(InstData { args, ..InstData::new(Opcode::PtrAdd) }, Type::PTR);
1966 assuming(&mut build, at, 4, 4);
1967 build.ret(&[]);
1968 let stats = run(&mut func);
1969 assert_eq!(checks(&func), 1);
1970 assert_eq!(stats.count(Kind::Missed, super::UNKNOWN_ALIGNMENT), 1);
1971 }
1972
1973 #[test]
1974 fn a_check_over_a_length_the_program_worked_out_is_not_this_pass_to_read() {
1975 // Section 7.4's hoisted check covers as many bytes as its loop runs times, which is a value
1976 // and not a number. Every range this pass compares is a pair of numbers, so it says so and
1977 // leaves the check alone rather than reading the payload, whose size is one element.
1978 let (_, mut func, block, pointer) = blank();
1979 let mut build = Builder::new(&mut func, block);
1980 check(&mut build, pointer, 4);
1981 let args = build.func().push_values(&[pointer]);
1982 let capability = build.value(InstData { args, ..InstData::new(Opcode::CapOf) }, Type::CAP);
1983 let bytes = build.iconst(Type::int(64), 4);
1984 let info = MemInfo {
1985 size: 4,
1986 align: 1,
1987 order: MemOrder::NotAtomic,
1988 tbaa: None,
1989 owns: 0,
1990 restrict: Restrict::NONE,
1991 };
1992 let extra = Extra::Mem(build.func().add_mem(info));
1993 let args = build.func().push_values(&[capability, pointer, bytes]);
1994 build.inst(InstData { args, extra, ..InstData::new(Opcode::CheckBounds) }, &[]);
1995 build.ret(&[]);
1996
1997 let stats = run(&mut func);
1998 assert_eq!(checks(&func), 2, "the second one stays");
1999 assert_eq!(stats.count(Kind::Missed, super::COMPUTED_EXTENT), 1);
2000 }
2001
2002 #[test]
2003 fn a_check_of_bytes_inside_a_checked_range_goes() {
2004 // Four bytes at offset four, inside sixteen bytes at offset zero. This is the shape the
2005 // whole pass is for: a struct whose fields are read one after another through one pointer.
2006 let (_, mut func, block, pointer) = blank();
2007 let mut build = Builder::new(&mut func, block);
2008 check(&mut build, pointer, 16);
2009 let field = past(&mut build, pointer, 4);
2010 check(&mut build, field, 4);
2011 build.ret(&[]);
2012 run(&mut func);
2013 assert_eq!(checks(&func), 1);
2014 }
2015
2016 #[test]
2017 fn a_check_of_bytes_past_the_end_of_a_checked_range_stays() {
2018 // Four bytes at offset fourteen is two bytes past the end of the sixteen that were
2019 // checked, and those two bytes are what the check is for.
2020 let (_, mut func, block, pointer) = blank();
2021 let mut build = Builder::new(&mut func, block);
2022 check(&mut build, pointer, 16);
2023 let over = past(&mut build, pointer, 14);
2024 check(&mut build, over, 4);
2025 build.ret(&[]);
2026 assert!(!run(&mut func).changed());
2027 assert_eq!(checks(&func), 2);
2028 }
2029
2030 #[test]
2031 fn a_check_of_bytes_before_a_checked_range_stays() {
2032 // The guard's `delta` is not negative, and this is why. A read four bytes below what was
2033 // checked is a read of somebody else's memory, and it is the bug the check exists for.
2034 let (_, mut func, block, pointer) = blank();
2035 let mut build = Builder::new(&mut func, block);
2036 check(&mut build, pointer, 16);
2037 let under = past(&mut build, pointer, -4);
2038 check(&mut build, under, 4);
2039 build.ret(&[]);
2040 assert!(!run(&mut func).changed());
2041 assert_eq!(checks(&func), 2);
2042 }
2043
2044 #[test]
2045 fn a_check_whose_capability_was_taken_where_the_pointer_came_from_covers_the_bytes_between() {
2046 // The shape a capability that belongs to a pointer produces. The check is on a field eight
2047 // bytes in and the capability was taken at the struct's pointer, so what it says is that
2048 // those four bytes and that pointer are in one instance. An instance is a run of bytes, so
2049 // everything from the pointer up to the end of the field is in it, and that is the fact.
2050 // The second check is inside it and goes.
2051 let (_, mut func, block, pointer) = blank();
2052 let mut build = Builder::new(&mut func, block);
2053 let field = past(&mut build, pointer, 8);
2054 checking_at(&mut build, pointer, field, 4);
2055 checking_at(&mut build, pointer, pointer, 4);
2056 build.ret(&[]);
2057 run(&mut func);
2058 assert_eq!(checks(&func), 1);
2059 }
2060
2061 #[test]
2062 fn a_check_whose_capability_was_taken_where_the_pointer_came_from_says_nothing_past_the_end() {
2063 // And the run stops where the access does. Four bytes at twelve are past the twelve the
2064 // check above established, and nothing here says the instance reaches that far.
2065 let (_, mut func, block, pointer) = blank();
2066 let mut build = Builder::new(&mut func, block);
2067 let field = past(&mut build, pointer, 8);
2068 checking_at(&mut build, pointer, field, 4);
2069 let over = past(&mut build, pointer, 12);
2070 checking_at(&mut build, pointer, over, 4);
2071 build.ret(&[]);
2072 assert!(!run(&mut func).changed());
2073 assert_eq!(checks(&func), 2);
2074 }
2075
2076 #[test]
2077 fn a_check_whose_capability_is_about_neither_end_of_the_walk_stays() {
2078 // Two rules and no third. A capability is about the address being checked or about the
2079 // pointer that address came off, and one about anything else is asking after an instance
2080 // this pass has nothing to say about.
2081 let mut names = Interner::new();
2082 let name = names.intern("two");
2083 let mut func = Func::new(name, Signature::new().with_params(&[Type::PTR, Type::PTR]));
2084 let block = func.create_block();
2085 let pointer = func.append_param(block, Type::PTR);
2086 let other = func.append_param(block, Type::PTR);
2087 let mut build = Builder::new(&mut func, block);
2088 check(&mut build, pointer, 16);
2089 let field = past(&mut build, pointer, 4);
2090 checking_at(&mut build, other, field, 4);
2091 build.ret(&[]);
2092 let stats = run(&mut func);
2093 assert_eq!(checks(&func), 2);
2094 assert_eq!(stats.count(Kind::Missed, super::UNKNOWN_SHAPE), 1);
2095 }
2096
2097 #[test]
2098 fn a_check_through_a_pointer_nothing_relates_to_the_first_stays() {
2099 let mut names = Interner::new();
2100 let name = names.intern("two");
2101 let mut func = Func::new(name, Signature::new().with_params(&[Type::PTR, Type::PTR]));
2102 let block = func.create_block();
2103 let one = func.append_param(block, Type::PTR);
2104 let other = func.append_param(block, Type::PTR);
2105 let mut build = Builder::new(&mut func, block);
2106 check(&mut build, one, 16);
2107 check(&mut build, other, 4);
2108 build.ret(&[]);
2109 assert!(!run(&mut func).changed());
2110 assert_eq!(checks(&func), 2);
2111 }
2112
2113 #[test]
2114 fn a_check_a_call_stands_between_stays_and_is_counted() {
2115 // The conservatism the module comment argues for, and the number that says what it costs.
2116 let (mut names, mut func, block, pointer) = blank();
2117 let mut build = Builder::new(&mut func, block);
2118 check(&mut build, pointer, 16);
2119 let callee = names.intern("might_free");
2120 let signature = build.func().add_signature(Signature::new());
2121 build.call(callee, signature, &[]);
2122 check(&mut build, pointer, 4);
2123 build.ret(&[]);
2124 let stats = run(&mut func);
2125 assert!(!stats.changed());
2126 assert_eq!(checks(&func), 2);
2127 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL), 1);
2128 }
2129
2130 #[test]
2131 fn a_check_a_call_that_cannot_free_stands_between_goes() {
2132 // The other side of the paragraph above. The summary said this call reaches nothing that
2133 // ends a lifetime, so the range the first check established is still one range.
2134 let (mut names, mut func, block, pointer) = blank();
2135 let mut build = Builder::new(&mut func, block);
2136 check(&mut build, pointer, 16);
2137 let callee = names.intern("counts_them");
2138 let signature = build.func().add_signature(Signature::new());
2139 let call = build.call(callee, signature, &[]);
2140 check(&mut build, pointer, 4);
2141 build.ret(&[]);
2142 func[call].flags |= Flags::NOFREE;
2143 let stats = run(&mut func);
2144 assert_eq!(checks(&func), 1);
2145 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
2146 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL), 0);
2147 }
2148
2149 #[test]
2150 fn inline_assembly_throws_the_facts_away_whatever_it_is_flagged() {
2151 // There is no flag that would make this safe. The template is text the compiler does not
2152 // read, so nothing worked anything out about what it reaches.
2153 let (mut names, mut func, block, pointer) = blank();
2154 let mut build = Builder::new(&mut func, block);
2155 check(&mut build, pointer, 16);
2156 build.inline_asm(
2157 AsmInfo {
2158 template: names.intern("nop"),
2159 constraints: names.intern(""),
2160 clobbers: names.intern(""),
2161 targets: BlockCallList::EMPTY,
2162 },
2163 &[],
2164 &[],
2165 Flags::NONE,
2166 );
2167 check(&mut build, pointer, 4);
2168 build.ret(&[]);
2169 let stats = run(&mut func);
2170 assert!(!stats.changed());
2171 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL), 1);
2172 }
2173
2174 #[test]
2175 fn a_check_that_only_one_path_covers_stays() {
2176 // The dominator tree is what makes this right. The check in the arm covers the one in the
2177 // join on one path and not on the other, and a check that goes has to be one that ran.
2178 let (_, mut func, block, pointer) = blank();
2179 let arm = func.create_block();
2180 let join = func.create_block();
2181 let mut build = Builder::new(&mut func, block);
2182 let condition = build.iconst(Type::int(32), 1);
2183 build.br_if(condition, arm, &[], join, &[]);
2184 let mut build = Builder::new(&mut func, arm);
2185 check(&mut build, pointer, 16);
2186 build.jump(join, &[]);
2187 let mut build = Builder::new(&mut func, join);
2188 check(&mut build, pointer, 4);
2189 build.ret(&[]);
2190 assert!(!run(&mut func).changed());
2191 assert_eq!(checks(&func), 2);
2192 }
2193
2194 #[test]
2195 fn a_check_a_dominating_block_covers_goes() {
2196 let (_, mut func, block, pointer) = blank();
2197 let after = func.create_block();
2198 let mut build = Builder::new(&mut func, block);
2199 check(&mut build, pointer, 16);
2200 build.jump(after, &[]);
2201 let mut build = Builder::new(&mut func, after);
2202 let field = past(&mut build, pointer, 8);
2203 check(&mut build, field, 8);
2204 build.ret(&[]);
2205 run(&mut func);
2206 assert_eq!(checks(&func), 1);
2207 }
2208
2209 #[test]
2210 fn fuel_stops_the_removing_and_not_the_looking() {
2211 let (_, mut func, block, pointer) = blank();
2212 let mut build = Builder::new(&mut func, block);
2213 check(&mut build, pointer, 4);
2214 check(&mut build, pointer, 4);
2215 check(&mut build, pointer, 4);
2216 build.ret(&[]);
2217 let mut fuel = Fuel::of(1);
2218 let stats = DISCHARGE.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut fuel);
2219 assert_eq!(checks(&func), 2);
2220 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
2221 assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 1);
2222 }
2223
2224 #[test]
2225 fn a_second_lifetime_check_of_the_same_address_goes() {
2226 // The narrow fact on its own, with no range around it to widen into.
2227 let (_, mut func, block, pointer) = blank();
2228 let mut build = Builder::new(&mut func, block);
2229 live(&mut build, pointer);
2230 live(&mut build, pointer);
2231 build.ret(&[]);
2232 let stats = run(&mut func);
2233 assert_eq!(lives(&func), 1);
2234 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE), 1);
2235 }
2236
2237 #[test]
2238 fn a_lifetime_check_inside_a_checked_range_goes() {
2239 // The shape the pass is for, with both halves of it. Sixteen bytes are checked and found
2240 // alive, then a field four bytes in is read, and neither check in front of it survives.
2241 let (_, mut func, block, pointer) = blank();
2242 let mut build = Builder::new(&mut func, block);
2243 access(&mut build, pointer, 16);
2244 let field = past(&mut build, pointer, 4);
2245 access(&mut build, field, 4);
2246 build.ret(&[]);
2247 let stats = run(&mut func);
2248 assert_eq!(checks(&func), 1);
2249 assert_eq!(lives(&func), 1);
2250 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
2251 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE), 1);
2252 }
2253
2254 #[test]
2255 fn a_lifetime_check_outside_every_checked_range_stays() {
2256 // Four bytes at offset twenty are past the sixteen that were checked, so nothing says the
2257 // address is in the instance that was found alive, and it might be in no instance at all.
2258 let (_, mut func, block, pointer) = blank();
2259 let mut build = Builder::new(&mut func, block);
2260 access(&mut build, pointer, 16);
2261 let over = past(&mut build, pointer, 20);
2262 live(&mut build, over);
2263 build.ret(&[]);
2264 assert!(!run(&mut func).changed());
2265 assert_eq!(lives(&func), 2);
2266 }
2267
2268 #[test]
2269 fn a_lifetime_check_with_no_range_around_it_does_not_widen() {
2270 // Without the bounds check the first lifetime check speaks only for its own address, so
2271 // the one four bytes along is a different question and stays.
2272 let (_, mut func, block, pointer) = blank();
2273 let mut build = Builder::new(&mut func, block);
2274 live(&mut build, pointer);
2275 let field = past(&mut build, pointer, 4);
2276 live(&mut build, field);
2277 build.ret(&[]);
2278 assert!(!run(&mut func).changed());
2279 assert_eq!(lives(&func), 2);
2280 }
2281
2282 #[test]
2283 fn a_lifetime_check_a_call_stands_between_stays_and_is_counted() {
2284 // Section 8.8's number. This is the one the summaries were written for.
2285 let (mut names, mut func, block, pointer) = blank();
2286 let mut build = Builder::new(&mut func, block);
2287 access(&mut build, pointer, 16);
2288 let callee = names.intern("might_free");
2289 let signature = build.func().add_signature(Signature::new());
2290 build.call(callee, signature, &[]);
2291 let field = past(&mut build, pointer, 4);
2292 live(&mut build, field);
2293 build.ret(&[]);
2294 let stats = run(&mut func);
2295 assert!(!stats.changed());
2296 assert_eq!(lives(&func), 2);
2297 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL_LIVE), 1);
2298 }
2299
2300 #[test]
2301 fn a_lifetime_check_a_call_that_cannot_free_stands_between_goes() {
2302 let (mut names, mut func, block, pointer) = blank();
2303 let mut build = Builder::new(&mut func, block);
2304 access(&mut build, pointer, 16);
2305 let callee = names.intern("counts_them");
2306 let signature = build.func().add_signature(Signature::new());
2307 let call = build.call(callee, signature, &[]);
2308 let field = past(&mut build, pointer, 4);
2309 live(&mut build, field);
2310 build.ret(&[]);
2311 func[call].flags |= Flags::NOFREE;
2312 let stats = run(&mut func);
2313 assert_eq!(lives(&func), 1);
2314 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE), 1);
2315 }
2316
2317 #[test]
2318 fn ending_a_lifetime_throws_the_facts_away() {
2319 // Nothing emits `meta_end` yet, so this is the test that says what will happen when
2320 // something does, rather than a test of anything the compiler does today.
2321 let (_, mut func, block, pointer) = blank();
2322 let mut build = Builder::new(&mut func, block);
2323 access(&mut build, pointer, 16);
2324 let size = build.iconst(Type::int(64), 16);
2325 let args = build.func().push_values(&[pointer, size]);
2326 build.inst(InstData { args, ..InstData::new(Opcode::MetaEnd) }, &[]);
2327 access(&mut build, pointer, 16);
2328 build.ret(&[]);
2329 let stats = run(&mut func);
2330 assert!(!stats.changed());
2331 assert_eq!(checks(&func), 2);
2332 assert_eq!(lives(&func), 2);
2333 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL), 1);
2334 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL_LIVE), 1);
2335 }
2336
2337 #[test]
2338 fn fuel_runs_out_over_both_kinds_of_check() {
2339 let (_, mut func, block, pointer) = blank();
2340 let mut build = Builder::new(&mut func, block);
2341 access(&mut build, pointer, 16);
2342 access(&mut build, pointer, 4);
2343 build.ret(&[]);
2344 let mut fuel = Fuel::of(1);
2345 let stats = DISCHARGE.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut fuel);
2346 assert_eq!(checks(&func), 1);
2347 assert_eq!(lives(&func), 2);
2348 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
2349 assert_eq!(stats.count(Kind::Missed, super::NO_FUEL_LIVE), 1);
2350 }
2351
2352 #[test]
2353 fn a_distance_too_large_to_be_a_real_access_is_not_discharged() {
2354 // The guard's bound. The two readings of the arithmetic agree while the numbers stay
2355 // small, so a rule proved at sixty four bits is not asked about anything else. Nothing
2356 // here is wrong, it simply is not proved, and a check that is not proved to be unnecessary
2357 // stays.
2358 let huge = i128::from(u64::MAX) * 4;
2359 let fact = Fact { base: Value::new(0), offset: 0, size: huge };
2360 let asked = Fact { base: Value::new(0), offset: huge / 2, size: 4 };
2361 assert!(!super::covers(&fact, &asked));
2362 }
2363
2364 #[test]
2365 fn a_range_of_addresses_wider_than_the_rule_allows_is_not_discharged() {
2366 // The guard on `reached.i64` bounds each of the three numbers at four gigabytes, for the
2367 // reason the rule file gives: past there the compiler's `i128` reading of the guard and the
2368 // solver's sixty four bit reading part company, and a rule proved under one and run under
2369 // the other is a rule proved about arithmetic that is not happening. A step whose range is
2370 // that wide is the usual case rather than a corner, since an index nothing has bounded says
2371 // nothing about where the access lands.
2372 let base = Value::new(0);
2373 let whole = Fact::whole(base, i128::from(u64::MAX) * 4);
2374 let asked = super::Reach { base, low: 0, width: i128::from(u64::MAX), size: 4 };
2375 assert!(!super::reaches(&whole, &asked));
2376 }
2377
2378 #[test]
2379 fn a_range_of_addresses_that_ends_where_the_object_does_is_discharged() {
2380 // Sixteen bytes, a step somewhere in nought to eleven, four bytes read. The last address
2381 // the walk can reach is the last one in the object, which is inside it.
2382 let base = Value::new(0);
2383 let whole = Fact::whole(base, 16);
2384 let asked = super::Reach { base, low: 0, width: 12, size: 4 };
2385 assert!(super::reaches(&whole, &asked));
2386 let over = super::Reach { base, low: 0, width: 13, size: 4 };
2387 assert!(!super::reaches(&whole, &over), "one byte further runs off the end");
2388 }
2389
2390 #[test]
2391 fn a_walk_by_a_bounded_step_off_a_local_takes_its_derivation_check_with_it() {
2392 // The shape `derives` cannot read at all: the pointer that went in is the slot and the one
2393 // that came out is a value past it, so the two are not one base and two constants. Both
2394 // ends widen to the slot, the slot holds both ranges, and one thing holding both is what a
2395 // derivation check asks about.
2396 let (_, mut func, block, _, index) = indexed();
2397 let mut build = Builder::new(&mut func, block);
2398 let slot = local(&mut build, 16);
2399 let step = low_bits(&mut build, index, 7);
2400 let at = walk(&mut build, slot, step);
2401 deriv(&mut build, slot, at, 4);
2402 build.ret(&[]);
2403 let stats = run(&mut func);
2404 assert_eq!(derivs(&func), 0);
2405 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_RANGE), 1);
2406 }
2407
2408 #[test]
2409 fn a_walk_that_can_leave_the_local_keeps_its_derivation_check() {
2410 // Nought to fifteen off a slot of eight. Every step is bounded and the answer is still no,
2411 // because the question is whether the slot holds every address the walk can reach.
2412 let (_, mut func, block, _, index) = indexed();
2413 let mut build = Builder::new(&mut func, block);
2414 let slot = local(&mut build, 8);
2415 let step = low_bits(&mut build, index, 15);
2416 let at = walk(&mut build, slot, step);
2417 deriv(&mut build, slot, at, 4);
2418 build.ret(&[]);
2419 let stats = run(&mut func);
2420 assert_eq!(derivs(&func), 1);
2421 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_RANGE), 0);
2422 assert_eq!(stats.count(Kind::Missed, super::OVER_THE_LOCAL_DERIV), 1);
2423 }
2424
2425 #[test]
2426 fn a_lifetime_check_a_bounded_walk_lands_inside_a_checked_range_goes() {
2427 // An access over thirty two bytes establishes the range, and the lifetime check beside it
2428 // makes that range one a check found alive. The lifetime check on the walk then goes,
2429 // because every address the walk can reach is in the range that was found alive.
2430 //
2431 // Written off a parameter rather than a slot because a slot answers the narrow question on
2432 // its own. What has to answer this one is a range a check was passed on.
2433 let (_, mut func, block, pointer, index) = indexed();
2434 let mut build = Builder::new(&mut func, block);
2435 access(&mut build, pointer, 32);
2436 let step = low_bits(&mut build, index, 7);
2437 let at = walk(&mut build, pointer, step);
2438 live(&mut build, at);
2439 build.ret(&[]);
2440 let stats = run(&mut func);
2441 assert_eq!(lives(&func), 1, "the one in front of the access stays");
2442 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_RANGE), 1);
2443 }
2444
2445 #[test]
2446 fn a_lifetime_check_a_bounded_walk_can_leave_the_checked_range_keeps_it() {
2447 // The same over eight bytes, under a walk that can go fifteen past the start. A range of
2448 // eight bytes does not hold an address fifteen along from where it begins.
2449 let (_, mut func, block, pointer, index) = indexed();
2450 let mut build = Builder::new(&mut func, block);
2451 access(&mut build, pointer, 8);
2452 let step = low_bits(&mut build, index, 15);
2453 let at = walk(&mut build, pointer, step);
2454 live(&mut build, at);
2455 build.ret(&[]);
2456 let stats = run(&mut func);
2457 assert_eq!(lives(&func), 2);
2458 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_RANGE), 0);
2459 }
2460
2461 /// A stack slot of `size` bytes, in the entry block where the verifier wants one.
2462 fn local(build: &mut Builder<'_>, size: u64) -> Value {
2463 let info = MemInfo {
2464 size,
2465 align: 8,
2466 order: MemOrder::NotAtomic,
2467 tbaa: None,
2468 owns: 0,
2469 restrict: Restrict::NONE,
2470 };
2471 let extra = Extra::Mem(build.func().add_mem(info));
2472 build.value(InstData { extra, ..InstData::new(Opcode::Alloca) }, Type::PTR)
2473 }
2474
2475 /// A function taking a pointer and an index, with one block.
2476 fn indexed() -> (Interner, Func, Block, Value, Value) {
2477 let mut names = Interner::new();
2478 let name = names.intern("f");
2479 let mut func = Func::new(name, Signature::new().with_params(&[Type::PTR, Type::int(64)]));
2480 let block = func.create_block();
2481 let pointer = func.append_param(block, Type::PTR);
2482 let index = func.append_param(block, Type::int(64));
2483 (names, func, block, pointer, index)
2484 }
2485
2486 /// A pointer a value past another one.
2487 fn walk(build: &mut Builder<'_>, pointer: Value, by: Value) -> Value {
2488 let args = build.func().push_values(&[pointer, by]);
2489 build.value(InstData { args, ..InstData::new(Opcode::PtrAdd) }, Type::PTR)
2490 }
2491
2492 /// The low bits of a value, which is a step the ranges can put a number on.
2493 fn low_bits(build: &mut Builder<'_>, value: Value, mask: i128) -> Value {
2494 let bits = build.iconst(Type::int(64), mask);
2495 build.binary(Opcode::And, value, bits, Flags::NONE)
2496 }
2497
2498 #[test]
2499 fn a_walk_by_a_step_the_ranges_bound_inside_a_local_goes() {
2500 // Section 7.2's third source. The step is not a constant, so the walk stops at the
2501 // `ptr_add` and the fact that comes out is about a base nobody knows the size of. What
2502 // the ranges say is that the step is somewhere in nought to seven, so the four bytes the
2503 // access wants are somewhere in nought to eleven, and all of that is inside the sixteen
2504 // the slot is.
2505 let (_, mut func, block, _, index) = indexed();
2506 let mut build = Builder::new(&mut func, block);
2507 let slot = local(&mut build, 16);
2508 let step = low_bits(&mut build, index, 7);
2509 let at = walk(&mut build, slot, step);
2510 check(&mut build, at, 4);
2511 build.ret(&[]);
2512 let stats = run(&mut func);
2513 assert_eq!(checks(&func), 0);
2514 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_RANGE), 1);
2515 }
2516
2517 #[test]
2518 fn a_walk_by_a_step_the_ranges_cannot_bound_is_left_alone() {
2519 // The same function with the mask taken off. A parameter can be anything, so the range of
2520 // addresses the walk reaches is the whole of memory and no slot covers it.
2521 let (_, mut func, block, _, index) = indexed();
2522 let mut build = Builder::new(&mut func, block);
2523 let slot = local(&mut build, 16);
2524 let at = walk(&mut build, slot, index);
2525 check(&mut build, at, 4);
2526 build.ret(&[]);
2527 let stats = run(&mut func);
2528 assert_eq!(checks(&func), 1);
2529 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_RANGE), 0);
2530 }
2531
2532 #[test]
2533 fn a_walk_a_bounded_step_can_take_off_the_end_of_a_local_is_left_alone() {
2534 // Nought to seven again, four bytes again, and a slot of eight this time. The step being
2535 // bounded is not the question. The question is whether every address it can reach is
2536 // inside the slot, and seven plus four is not.
2537 let (_, mut func, block, _, index) = indexed();
2538 let mut build = Builder::new(&mut func, block);
2539 let slot = local(&mut build, 8);
2540 let step = low_bits(&mut build, index, 7);
2541 let at = walk(&mut build, slot, step);
2542 check(&mut build, at, 4);
2543 build.ret(&[]);
2544 let stats = run(&mut func);
2545 assert_eq!(checks(&func), 1);
2546 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_RANGE), 0);
2547 }
2548
2549 #[test]
2550 fn a_constant_step_past_a_bounded_one_is_walked_too() {
2551 // A field of an element of an array of structs, which is the shape this is for. The array
2552 // index needs a range and the field offset does not, and the walk has to get through both.
2553 let (_, mut func, block, _, index) = indexed();
2554 let mut build = Builder::new(&mut func, block);
2555 let slot = local(&mut build, 32);
2556 let step = low_bits(&mut build, index, 15);
2557 let element = walk(&mut build, slot, step);
2558 let field = past(&mut build, element, 8);
2559 check(&mut build, field, 4);
2560 build.ret(&[]);
2561 let stats = run(&mut func);
2562 assert_eq!(checks(&func), 0);
2563 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_RANGE), 1);
2564 }
2565
2566 #[test]
2567 fn what_a_range_discharge_records_is_the_bytes_and_not_the_range() {
2568 // The second check is the same bytes as the first, and the first went because a made up
2569 // range around it was inside the slot. What the first one proved is that those bytes are
2570 // in the slot, so the second one goes on that rather than on the ranges being asked all
2571 // over again.
2572 let (_, mut func, block, _, index) = indexed();
2573 let mut build = Builder::new(&mut func, block);
2574 let slot = local(&mut build, 16);
2575 let step = low_bits(&mut build, index, 7);
2576 let at = walk(&mut build, slot, step);
2577 check(&mut build, at, 4);
2578 check(&mut build, at, 4);
2579 build.ret(&[]);
2580 let stats = run(&mut func);
2581 assert_eq!(checks(&func), 0);
2582 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_RANGE), 1);
2583 assert_eq!(stats.count(Kind::Optimized, super::REMOVED), 1);
2584 }
2585
2586 /// A stack slot whose size the program works out, which is what a variable length array is.
2587 fn growable(build: &mut Builder<'_>, size: Value) -> Value {
2588 let info = MemInfo {
2589 size: 0,
2590 align: 8,
2591 order: MemOrder::NotAtomic,
2592 tbaa: None,
2593 owns: 0,
2594 restrict: Restrict::NONE,
2595 };
2596 let extra = Extra::Mem(build.func().add_mem(info));
2597 let args = build.func().push_values(&[size]);
2598 build.value(InstData { args, extra, ..InstData::new(Opcode::Alloca) }, Type::PTR)
2599 }
2600
2601 #[test]
2602 fn a_check_of_bytes_inside_a_local_goes_with_nothing_in_front_of_it() {
2603 // Section 7.2's first source. No check established this and none had to: an `alloca` of
2604 // sixteen bytes is sixteen bytes of one storage instance because that is what it makes.
2605 let (_, mut func, block, _) = blank();
2606 let mut build = Builder::new(&mut func, block);
2607 let slot = local(&mut build, 16);
2608 let field = past(&mut build, slot, 8);
2609 check(&mut build, field, 4);
2610 build.ret(&[]);
2611 let stats = run(&mut func);
2612 assert_eq!(checks(&func), 0);
2613 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 1);
2614 }
2615
2616 #[test]
2617 fn a_check_past_the_end_of_a_local_stays() {
2618 // The slot is sixteen bytes and the access runs to twenty. Nothing about it being a local
2619 // says anything about the four bytes after it, which belong to whatever the frame puts
2620 // there next.
2621 let (_, mut func, block, _) = blank();
2622 let mut build = Builder::new(&mut func, block);
2623 let slot = local(&mut build, 16);
2624 let field = past(&mut build, slot, 16);
2625 check(&mut build, field, 4);
2626 build.ret(&[]);
2627 let stats = run(&mut func);
2628 assert_eq!(checks(&func), 1);
2629 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 0);
2630 }
2631
2632 #[test]
2633 fn a_check_of_bytes_inside_a_local_goes_across_a_call() {
2634 // The other half of what makes the fact worth having. A callee cannot free a frame slot,
2635 // so unlike everything the walk carries this one is not thrown away at a call.
2636 let (mut names, mut func, block, _) = blank();
2637 let mut build = Builder::new(&mut func, block);
2638 let slot = local(&mut build, 16);
2639 let callee = names.intern("might_free");
2640 let signature = build.func().add_signature(Signature::new());
2641 build.call(callee, signature, &[]);
2642 check(&mut build, slot, 4);
2643 build.ret(&[]);
2644 let stats = run(&mut func);
2645 assert_eq!(checks(&func), 0);
2646 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 1);
2647 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL), 0);
2648 }
2649
2650 #[test]
2651 fn a_check_inside_a_variable_length_array_stays() {
2652 // How many bytes it is is a value the program works out, and the payload's size field
2653 // reads zero. A pass that read it anyway would discharge every check in the array.
2654 let (_, mut func, block, _) = blank();
2655 let mut build = Builder::new(&mut func, block);
2656 let bytes = build.iconst(Type::int(64), 64);
2657 let slot = growable(&mut build, bytes);
2658 check(&mut build, slot, 4);
2659 build.ret(&[]);
2660 let stats = run(&mut func);
2661 assert_eq!(checks(&func), 1);
2662 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LOCAL), 0);
2663 }
2664
2665 #[test]
2666 fn a_lifetime_check_in_a_local_goes_with_nothing_in_front_of_it() {
2667 // The frame slot rule, and the point is that neither of these has a check in front of it.
2668 // A slot is alive until the function returns, so a lifetime check anywhere inside one is
2669 // asking a question the `alloca` already answered.
2670 let (_, mut func, block, _) = blank();
2671 let mut build = Builder::new(&mut func, block);
2672 let slot = local(&mut build, 16);
2673 live(&mut build, slot);
2674 let field = past(&mut build, slot, 12);
2675 live(&mut build, field);
2676 build.ret(&[]);
2677 let stats = run(&mut func);
2678 assert_eq!(lives(&func), 0);
2679 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_LOCAL), 2);
2680 }
2681
2682 #[test]
2683 fn a_lifetime_check_past_the_end_of_a_local_stays() {
2684 // The slot answers for its own bytes and no further, so an address outside it is a
2685 // different instance and a question nothing has answered.
2686 let (_, mut func, block, _) = blank();
2687 let mut build = Builder::new(&mut func, block);
2688 let slot = local(&mut build, 16);
2689 live(&mut build, slot);
2690 let field = past(&mut build, slot, 24);
2691 live(&mut build, field);
2692 build.ret(&[]);
2693 let stats = run(&mut func);
2694 assert_eq!(lives(&func), 1);
2695 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_LOCAL), 1);
2696 }
2697
2698 #[test]
2699 fn something_ending_a_lifetime_turns_the_frame_slot_rule_off() {
2700 // The gate, and with it the widening the frame slot rule usually hides. With a `meta_end`
2701 // anywhere in the function the slot answers nothing, so the first check stays and pays,
2702 // and what takes the second one out is the first one widened to the whole slot.
2703 let (_, mut func, block, pointer) = blank();
2704 let mut build = Builder::new(&mut func, block);
2705 let slot = local(&mut build, 16);
2706 live(&mut build, slot);
2707 let field = past(&mut build, slot, 12);
2708 live(&mut build, field);
2709 let size = build.iconst(Type::int(64), 16);
2710 let args = build.func().push_values(&[pointer, size]);
2711 build.inst(InstData { args, ..InstData::new(Opcode::MetaEnd) }, &[]);
2712 build.ret(&[]);
2713 let stats = run(&mut func);
2714 assert_eq!(lives(&func), 1);
2715 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_LOCAL), 0);
2716 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE), 1);
2717 }
2718
2719 /// Puts `cap_of` and a `check_deriv` for a walk from `from` to `to` into a block.
2720 ///
2721 /// The stride is the width of one element, which is what `rucc-safety` passes and what the
2722 /// runtime uses for a pointer that walked off the near end. This pass does not read it.
2723 fn deriv(build: &mut Builder<'_>, from: Value, to: Value, stride: i128) {
2724 deriving_at(build, from, from, to, stride);
2725 }
2726
2727 /// The same, with the capability taken at `held` rather than at the address the walk starts on.
2728 fn deriving_at(build: &mut Builder<'_>, held: Value, from: Value, to: Value, stride: i128) {
2729 let args = build.func().push_values(&[held]);
2730 let capability = build.value(InstData { args, ..InstData::new(Opcode::CapOf) }, Type::CAP);
2731 let width = build.iconst(Type::int(64), stride);
2732 let args = build.func().push_values(&[capability, from, to, width]);
2733 build.inst(InstData { args, ..InstData::new(Opcode::CheckDeriv) }, &[]);
2734 }
2735
2736 /// How many derivation checks are left in a function.
2737 fn derivs(func: &Func) -> usize {
2738 func.blocks()
2739 .flat_map(|block| func.insts(block).collect::<Vec<_>>())
2740 .filter(|&inst| func[inst].opcode == Opcode::CheckDeriv)
2741 .count()
2742 }
2743
2744 #[test]
2745 fn a_walk_inside_a_checked_range_goes() {
2746 // Sixteen bytes were checked, and the walk goes from the start of them to eight in. Both
2747 // ends are in one range, so the second address is in the instance the first belongs to.
2748 let (_, mut func, block, pointer) = blank();
2749 let mut build = Builder::new(&mut func, block);
2750 check(&mut build, pointer, 16);
2751 let field = past(&mut build, pointer, 8);
2752 deriv(&mut build, pointer, field, 4);
2753 build.ret(&[]);
2754 let stats = run(&mut func);
2755 assert_eq!(derivs(&func), 0);
2756 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV), 1);
2757 }
2758
2759 #[test]
2760 fn a_walk_that_leaves_the_checked_range_stays() {
2761 // Four bytes were checked and the walk goes eight past them. Nothing here says the two
2762 // addresses are in one instance, which is the whole of what the check is about.
2763 let (_, mut func, block, pointer) = blank();
2764 let mut build = Builder::new(&mut func, block);
2765 check(&mut build, pointer, 4);
2766 let field = past(&mut build, pointer, 8);
2767 deriv(&mut build, pointer, field, 4);
2768 build.ret(&[]);
2769 let stats = run(&mut func);
2770 assert_eq!(derivs(&func), 1);
2771 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV), 0);
2772 }
2773
2774 #[test]
2775 fn a_walk_whose_capability_was_taken_where_the_pointer_came_from_is_read_too() {
2776 // The same two rules on the near end of a walk. Sixteen bytes were checked, the walk runs
2777 // from eight in to twelve in, and the capability is the one the pointer those two came off
2778 // got. The near end has to reach back to that pointer for the answer to be about the
2779 // instance the capability names, which is what the fact it asks does.
2780 let (_, mut func, block, pointer) = blank();
2781 let mut build = Builder::new(&mut func, block);
2782 check(&mut build, pointer, 16);
2783 let field = past(&mut build, pointer, 8);
2784 let next = past(&mut build, pointer, 12);
2785 deriving_at(&mut build, pointer, field, next, 4);
2786 build.ret(&[]);
2787 let stats = run(&mut func);
2788 assert_eq!(derivs(&func), 0);
2789 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV), 1);
2790 }
2791
2792 #[test]
2793 fn two_ranges_holding_one_end_each_do_not_answer_a_walk() {
2794 // The case the one fact rule is written for. Both addresses have been checked, so both are
2795 // inside some instance, and nothing says it is the same one. The walk stays.
2796 let (_, mut func, block, pointer) = blank();
2797 let mut build = Builder::new(&mut func, block);
2798 check(&mut build, pointer, 4);
2799 let field = past(&mut build, pointer, 64);
2800 check(&mut build, field, 4);
2801 deriv(&mut build, pointer, field, 4);
2802 build.ret(&[]);
2803 let stats = run(&mut func);
2804 assert_eq!(derivs(&func), 1);
2805 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV), 0);
2806 }
2807
2808 #[test]
2809 fn a_walk_inside_a_local_goes_with_nothing_in_front_of_it() {
2810 // The shape almost every derivation check in real code has: a field of a local struct.
2811 // `rucc-safety` emits the walk before the bounds check on what it produced, so a fact from
2812 // an earlier check is usually the wrong size for it and the local is what answers.
2813 let (_, mut func, block, _) = blank();
2814 let mut build = Builder::new(&mut func, block);
2815 let slot = local(&mut build, 16);
2816 let field = past(&mut build, slot, 8);
2817 deriv(&mut build, slot, field, 4);
2818 build.ret(&[]);
2819 let stats = run(&mut func);
2820 assert_eq!(derivs(&func), 0);
2821 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_LOCAL), 1);
2822 }
2823
2824 #[test]
2825 fn a_walk_off_the_end_of_a_local_stays() {
2826 // Where the slot stops is where the fact stops. One past the end is the case the runtime
2827 // has slack for and this pass does not use any of it.
2828 let (_, mut func, block, _) = blank();
2829 let mut build = Builder::new(&mut func, block);
2830 let slot = local(&mut build, 16);
2831 let field = past(&mut build, slot, 16);
2832 deriv(&mut build, slot, field, 4);
2833 build.ret(&[]);
2834 let stats = run(&mut func);
2835 assert_eq!(derivs(&func), 1);
2836 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_LOCAL), 0);
2837 }
2838
2839 #[test]
2840 fn a_walk_a_call_stands_between_stays_and_is_counted() {
2841 // The same price the other two kinds pay, reported the same way, so the cost of not
2842 // trusting a call is a number per function rather than a paragraph.
2843 let (mut names, mut func, block, pointer) = blank();
2844 let mut build = Builder::new(&mut func, block);
2845 check(&mut build, pointer, 16);
2846 let callee = names.intern("might_free");
2847 let signature = build.func().add_signature(Signature::new());
2848 build.call(callee, signature, &[]);
2849 let field = past(&mut build, pointer, 8);
2850 deriv(&mut build, pointer, field, 4);
2851 build.ret(&[]);
2852 let stats = run(&mut func);
2853 assert_eq!(derivs(&func), 1);
2854 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL_DERIV), 1);
2855 }
2856
2857 #[test]
2858 fn a_check_the_module_says_is_inside_a_global_goes_with_nothing_in_front_of_it() {
2859 // The other half of section 7.2's first source. The size of a global lives on the module
2860 // and this pass is given one function, so the answer arrives as a flag `crate::extents`
2861 // wrote before the pipeline started, and all three kinds carry it.
2862 let (_, mut func, block, pointer) = blank();
2863 let mut build = Builder::new(&mut func, block);
2864 let field = past(&mut build, pointer, 8);
2865 deriv(&mut build, pointer, field, 1);
2866 access(&mut build, field, 4);
2867 build.ret(&[]);
2868 marked(&mut func);
2869 let stats = run(&mut func);
2870 assert_eq!(checks(&func), 0);
2871 assert_eq!(lives(&func), 0);
2872 assert_eq!(derivs(&func), 0);
2873 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_STATIC), 1);
2874 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_STATIC), 1);
2875 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_STATIC), 1);
2876 }
2877
2878 #[test]
2879 fn a_check_the_module_says_every_caller_hands_in_goes_with_nothing_in_front_of_it() {
2880 // Section 7.5's summaries, arriving the same way a global's extent does and for the same
2881 // reason: which object a caller passes is a fact about a different function. What the flag
2882 // says is an extent and a lifetime, because the objects `crate::params` believes are a
2883 // caller's frame slot and a global and both are alive for as long as the call runs.
2884 let (_, mut func, block, pointer) = blank();
2885 let mut build = Builder::new(&mut func, block);
2886 let field = past(&mut build, pointer, 8);
2887 deriv(&mut build, pointer, field, 1);
2888 access(&mut build, field, 4);
2889 build.ret(&[]);
2890 flagged(&mut func, Flags::HANDED);
2891 let stats = run(&mut func);
2892 assert_eq!(checks(&func), 0);
2893 assert_eq!(lives(&func), 0);
2894 assert_eq!(derivs(&func), 0);
2895 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_HANDED), 1);
2896 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_HANDED), 1);
2897 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_HANDED), 1);
2898 }
2899
2900 /// A function that takes an index, allocates `size` bytes and tests the answer against null.
2901 ///
2902 /// Gives back the block where the test has passed, the block where it has not, the pointer and
2903 /// the index. The flag is put on by hand, because which calls deserve it is a question about a
2904 /// module and `crate::heap` is what answers it.
2905 ///
2906 /// The index is there for the tests about a walk by a value. A parameter on its own is any
2907 /// number at all, so a test that wants a bounded one puts [`low_bits`] over it the same way the
2908 /// local tests do.
2909 fn allocation(size: i128) -> (Interner, Func, Block, Block, Value, Value) {
2910 let mut names = Interner::new();
2911 let name = names.intern("f");
2912 let mut func = Func::new(name, Signature::new().with_params(&[Type::int(64)]));
2913 let entry = func.create_block();
2914 let inside = func.create_block();
2915 let outside = func.create_block();
2916 let index = func.append_param(entry, Type::int(64));
2917 let mut build = Builder::new(&mut func, entry);
2918 let signature = build.func().add_signature(
2919 Signature::new().with_params(&[Type::int(64)]).with_returns(&[Type::PTR]),
2920 );
2921 let bytes = build.iconst(Type::int(64), size);
2922 let call = build.call(names.intern("malloc"), signature, &[bytes]);
2923 let at = build.func();
2924 at[call].flags |= Flags::HEAP;
2925 let pointer = at[call].results().next().expect("a call that gives back a pointer");
2926 let zero = build.iconst(Type::int(64), 0);
2927 let null = build.unary(Opcode::IntToPtr, zero, Type::PTR);
2928 let condition = build.icmp(IntPred::Ne, pointer, null);
2929 build.br_if(condition, inside, &[], outside, &[]);
2930 let mut build = Builder::new(&mut func, outside);
2931 build.ret(&[]);
2932 (names, func, inside, outside, pointer, index)
2933 }
2934
2935 #[test]
2936 fn a_check_inside_an_allocation_the_program_tested_goes() {
2937 // The third of the objects whose extent nobody had to check for. `malloc(16)` says how
2938 // many bytes it made in the call, and the branch on null is what makes it true here.
2939 let (_, mut func, inside, _, pointer, _) = allocation(16);
2940 let mut build = Builder::new(&mut func, inside);
2941 let field = past(&mut build, pointer, 8);
2942 deriv(&mut build, pointer, field, 1);
2943 access(&mut build, field, 4);
2944 build.ret(&[]);
2945 let stats = run(&mut func);
2946 assert_eq!(checks(&func), 0);
2947 assert_eq!(derivs(&func), 0);
2948 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 1);
2949 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_MADE), 1);
2950 // The lifetime check is the one an allocation says nothing about, because a `free` in this
2951 // same function can end it, and it is what reports a use after free.
2952 assert_eq!(lives(&func), 1);
2953 }
2954
2955 #[test]
2956 fn a_check_on_an_allocation_nobody_tested_stays() {
2957 // Down the other arm the pointer is null, a null pointer is inside no object at all, and
2958 // the check is one that is supposed to fail.
2959 let (_, mut func, _, outside, pointer, _) = allocation(16);
2960 let mut build = Builder::new(&mut func, outside);
2961 access(&mut build, pointer, 4);
2962 build.ret(&[]);
2963 let stats = run(&mut func);
2964 assert_eq!(checks(&func), 1);
2965 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 0);
2966 }
2967
2968 #[test]
2969 fn a_check_past_the_end_of_an_allocation_stays() {
2970 // Four bytes at offset fourteen is two bytes past the sixteen that were asked for, and
2971 // those two bytes are what the check is for.
2972 let (_, mut func, inside, _, pointer, _) = allocation(16);
2973 let mut build = Builder::new(&mut func, inside);
2974 let field = past(&mut build, pointer, 14);
2975 access(&mut build, field, 4);
2976 build.ret(&[]);
2977 let stats = run(&mut func);
2978 assert_eq!(checks(&func), 1);
2979 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 0);
2980 }
2981
2982 #[test]
2983 fn a_walk_that_leaves_an_allocation_stays() {
2984 // One end inside and the other past the end is a walk out of the object, which is what a
2985 // derivation check is there to catch, so both ends have to be inside before it goes.
2986 let (_, mut func, inside, _, pointer, _) = allocation(16);
2987 let mut build = Builder::new(&mut func, inside);
2988 let field = past(&mut build, pointer, 32);
2989 deriv(&mut build, pointer, field, 1);
2990 build.ret(&[]);
2991 let stats = run(&mut func);
2992 assert_eq!(derivs(&func), 1);
2993 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_MADE), 0);
2994 }
2995
2996 #[test]
2997 fn a_check_inside_an_allocation_goes_across_a_call() {
2998 // The other reason a fact read off the instruction is worth having. How many bytes an
2999 // allocator made is not something a callee can change, so unlike a fact from a check that
3000 // ran this one is still there on the far side of a call.
3001 let (mut names, mut func, inside, _, pointer, _) = allocation(16);
3002 let mut build = Builder::new(&mut func, inside);
3003 access(&mut build, pointer, 4);
3004 let signature = build.func().add_signature(Signature::new());
3005 build.call(names.intern("g"), signature, &[]);
3006 access(&mut build, pointer, 4);
3007 build.ret(&[]);
3008 let stats = run(&mut func);
3009 assert_eq!(checks(&func), 0);
3010 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 2);
3011 // Both lifetime checks stay, and the second one is the one a `free` inside `g` would make
3012 // report.
3013 assert_eq!(lives(&func), 2);
3014 }
3015
3016 /// A function that allocates `size` bytes and never looks at what it got back.
3017 ///
3018 /// The shape `bench/safety/a-strided-column-sum.c` has. The size on its own must not answer a
3019 /// check here, because reading through what `malloc` gave back without testing it is the bug
3020 /// this compiler is for.
3021 fn untested(size: i128) -> (Interner, Func, Block, Value, Value) {
3022 let mut names = Interner::new();
3023 let name = names.intern("f");
3024 let mut func = Func::new(name, Signature::new().with_params(&[Type::int(64)]));
3025 let block = func.create_block();
3026 let index = func.append_param(block, Type::int(64));
3027 let mut build = Builder::new(&mut func, block);
3028 let signature = build.func().add_signature(
3029 Signature::new().with_params(&[Type::int(64)]).with_returns(&[Type::PTR]),
3030 );
3031 let bytes = build.iconst(Type::int(64), size);
3032 let call = build.call(names.intern("malloc"), signature, &[bytes]);
3033 let at = build.func();
3034 at[call].flags |= Flags::HEAP;
3035 let pointer = at[call].results().next().expect("a call that gives back a pointer");
3036 (names, func, block, pointer, index)
3037 }
3038
3039 #[test]
3040 fn a_walk_by_a_step_the_ranges_bound_inside_an_allocation_goes() {
3041 // The first half of tamnd/rucc#880. The step is not a constant, so the walk stops at the
3042 // `ptr_add` and what answers the check has to be asked of the range of addresses it can
3043 // reach. That range is nought to seven plus the four bytes the access wants, all of it
3044 // inside the sixteen the call says it made, and the branch on null is what makes the
3045 // sixteen true here.
3046 let (_, mut func, inside, _, pointer, index) = allocation(16);
3047 let mut build = Builder::new(&mut func, inside);
3048 let step = low_bits(&mut build, index, 7);
3049 let at = walk(&mut build, pointer, step);
3050 check(&mut build, at, 4);
3051 build.ret(&[]);
3052 let stats = run(&mut func);
3053 assert_eq!(checks(&func), 0);
3054 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 1);
3055 }
3056
3057 #[test]
3058 fn a_walk_by_a_step_that_can_leave_an_allocation_stays() {
3059 // The same function with the mask widened. Nought to thirty one plus four bytes runs off
3060 // the end of sixteen, and the bytes past the end are what the check is for.
3061 let (_, mut func, inside, _, pointer, index) = allocation(16);
3062 let mut build = Builder::new(&mut func, inside);
3063 let step = low_bits(&mut build, index, 31);
3064 let at = walk(&mut build, pointer, step);
3065 check(&mut build, at, 4);
3066 build.ret(&[]);
3067 let stats = run(&mut func);
3068 assert_eq!(checks(&func), 1);
3069 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 0);
3070 }
3071
3072 #[test]
3073 fn a_derivation_by_a_step_the_ranges_bound_inside_an_allocation_goes() {
3074 // The same for the derivation check, which is the one the column sum is left with. Both
3075 // ends have to be inside and inside the same object: the near end is the pointer itself and
3076 // the far end is anywhere in nought to seven past it.
3077 let (_, mut func, inside, _, pointer, index) = allocation(16);
3078 let mut build = Builder::new(&mut func, inside);
3079 let step = low_bits(&mut build, index, 7);
3080 let at = walk(&mut build, pointer, step);
3081 deriv(&mut build, pointer, at, 1);
3082 build.ret(&[]);
3083 let stats = run(&mut func);
3084 assert_eq!(derivs(&func), 0);
3085 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_MADE), 1);
3086 }
3087
3088 #[test]
3089 fn a_walk_into_an_allocation_nobody_tested_stays() {
3090 // The other half of the rule, which this does not weaken. A program that walks into what
3091 // `malloc` gave back without ever looking at it is a program that reads through null when
3092 // the allocation fails, and the checks are what report it.
3093 let (_, mut func, block, pointer, index) = untested(16);
3094 let mut build = Builder::new(&mut func, block);
3095 let step = low_bits(&mut build, index, 7);
3096 let at = walk(&mut build, pointer, step);
3097 check(&mut build, at, 4);
3098 deriv(&mut build, pointer, at, 1);
3099 build.ret(&[]);
3100 let stats = run(&mut func);
3101 assert_eq!(checks(&func), 1);
3102 assert_eq!(derivs(&func), 1);
3103 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_MADE), 0);
3104 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_DERIV_MADE), 0);
3105 }
3106
3107 #[test]
3108 fn a_check_every_caller_hands_in_goes_across_a_call() {
3109 // The reason the flag is worth having at all. A frame slot of the caller is not something
3110 // the callee's own callees can free, so the fact does not die at a call the way a fact
3111 // from a check that ran does.
3112 let (mut names, mut func, block, pointer) = blank();
3113 let mut build = Builder::new(&mut func, block);
3114 access(&mut build, pointer, 4);
3115 let signature = build.func().add_signature(Signature::new());
3116 build.call(names.intern("g"), signature, &[]);
3117 access(&mut build, pointer, 4);
3118 build.ret(&[]);
3119 flagged(&mut func, Flags::HANDED);
3120 let stats = run(&mut func);
3121 assert_eq!(checks(&func), 0);
3122 assert_eq!(lives(&func), 0);
3123 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_HANDED), 2);
3124 assert_eq!(stats.count(Kind::Optimized, super::REMOVED_LIVE_HANDED), 2);
3125 }
3126
3127 #[test]
3128 fn a_check_inside_a_global_goes_across_a_call() {
3129 // A callee can free what a global points at and cannot free the global, which lives as
3130 // long as the program does. So this is the one fact besides a local that a call leaves
3131 // standing, and it is read off the instruction rather than out of the scope for that
3132 // reason.
3133 let (mut names, mut func, block, pointer) = blank();
3134 let mut build = Builder::new(&mut func, block);
3135 let callee = names.intern("might_free");
3136 let signature = build.func().add_signature(Signature::new());
3137 build.call(callee, signature, &[]);
3138 access(&mut build, pointer, 4);
3139 build.ret(&[]);
3140 marked(&mut func);
3141 let stats = run(&mut func);
3142 assert_eq!(checks(&func), 0);
3143 assert_eq!(lives(&func), 0);
3144 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL), 0);
3145 assert_eq!(stats.count(Kind::Missed, super::PAST_A_CALL_LIVE), 0);
3146 }
3147
3148 #[test]
3149 fn a_check_the_module_marked_costs_fuel_like_any_other() {
3150 // A discharge is a discharge whatever established the fact, so `-fpass-fuel` has to stop
3151 // this one too or a bisection would step over it.
3152 let (_, mut func, block, pointer) = blank();
3153 let mut build = Builder::new(&mut func, block);
3154 access(&mut build, pointer, 4);
3155 build.ret(&[]);
3156 marked(&mut func);
3157 let stats =
3158 DISCHARGE.run(&mut func, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(1));
3159 assert_eq!(checks(&func) + lives(&func), 1);
3160 assert_eq!(stats.count(Kind::Missed, super::NO_FUEL_LIVE), 1);
3161 }
3162
3163 #[test]
3164 fn a_walk_whose_two_ends_are_off_two_pointers_with_no_ranges_says_the_same() {
3165 // Nothing in this function steps by a value, so the ranges are never built and the answer
3166 // has to come out of the constant reader alone. That reader stopped for one reason, and it
3167 // is the same reason.
3168 let (_, mut func, block, pointer) = blank();
3169 let other = func.append_param(block, Type::PTR);
3170 let mut build = Builder::new(&mut func, block);
3171 let at = past(&mut build, other, 8);
3172 deriv(&mut build, pointer, at, 4);
3173 build.ret(&[]);
3174 let stats = run(&mut func);
3175 assert_eq!(derivs(&func), 1);
3176 assert_eq!(stats.count(Kind::Missed, super::TWO_BASES_DERIV), 1);
3177 }
3178
3179 #[test]
3180 fn a_walk_whose_two_ends_are_off_two_pointers_says_so() {
3181 // Nothing comparable to ask about. Both ends are readable and each is somewhere inside
3182 // something, and two facts of that shape say nothing at all about it being one something,
3183 // which is the only thing a derivation check wants to know.
3184 let (_, mut func, block, pointer, index) = indexed();
3185 let other = func.append_param(block, Type::PTR);
3186 let mut build = Builder::new(&mut func, block);
3187 let step = low_bits(&mut build, index, 7);
3188 let at = walk(&mut build, other, step);
3189 deriv(&mut build, pointer, at, 4);
3190 build.ret(&[]);
3191 let stats = run(&mut func);
3192 assert_eq!(derivs(&func), 1);
3193 assert_eq!(stats.count(Kind::Missed, super::TWO_BASES_DERIV), 1);
3194 }
3195
3196 #[test]
3197 fn a_walk_off_a_pointer_this_function_was_handed_says_so() {
3198 // The largest pile after a loaded pointer, 1321 checks on SQLite. Everything about the
3199 // shape is readable: one base, a step the ranges bound, both ends off that base. What is
3200 // missing is how many bytes belong to the object, and a pointer that arrived as a
3201 // parameter is one nothing in the function can say that about. Section 7.5's summaries are
3202 // what would.
3203 let (_, mut func, block, pointer, index) = indexed();
3204 let mut build = Builder::new(&mut func, block);
3205 let step = low_bits(&mut build, index, 7);
3206 let at = walk(&mut build, pointer, step);
3207 deriv(&mut build, pointer, at, 4);
3208 build.ret(&[]);
3209 let stats = run(&mut func);
3210 assert_eq!(derivs(&func), 1);
3211 assert_eq!(stats.count(Kind::Missed, super::NO_EXTENT_HANDED), 1);
3212 }
3213
3214 #[test]
3215 fn a_walk_off_a_pointer_this_function_loaded_says_so() {
3216 // The largest pile of the lot, 2155 checks on SQLite, and the shape is `p->field[i]`. The
3217 // extent of what a pointer in memory points at is not written down anywhere the compiler
3218 // can see today, which is what `__counted_by` and the type plane are for.
3219 let (_, mut func, block, pointer, index) = indexed();
3220 let mut build = Builder::new(&mut func, block);
3221 let info = MemInfo {
3222 size: 8,
3223 align: 8,
3224 order: MemOrder::NotAtomic,
3225 tbaa: None,
3226 owns: 0,
3227 restrict: Restrict::NONE,
3228 };
3229 let args = build.func().push_values(&[pointer]);
3230 let extra = Extra::Mem(build.func().add_mem(info));
3231 let held = build.value(InstData { args, extra, ..InstData::new(Opcode::Load) }, Type::PTR);
3232 let step = low_bits(&mut build, index, 7);
3233 let at = walk(&mut build, held, step);
3234 deriv(&mut build, held, at, 4);
3235 build.ret(&[]);
3236 let stats = run(&mut func);
3237 assert_eq!(derivs(&func), 1);
3238 assert_eq!(stats.count(Kind::Missed, super::NO_EXTENT_LOADED), 1);
3239 }
3240
3241 #[test]
3242 fn a_walk_off_a_global_says_so() {
3243 // 485 checks on SQLite, and the one pile of the four where somebody does know the answer.
3244 // A global's extent is on the module, `crate::extents` reads it and writes the fact onto
3245 // every check it can settle before the pipeline starts, and it cannot settle this one
3246 // because it runs before anything has put a number on the index. See tamnd/rucc#878.
3247 let (mut names, mut func, block, _, index) = indexed();
3248 let mut build = Builder::new(&mut func, block);
3249 let extra = Extra::Symbol(names.intern("g"));
3250 let base = build.value(InstData { extra, ..InstData::new(Opcode::GlobalAddr) }, Type::PTR);
3251 let step = low_bits(&mut build, index, 7);
3252 let at = walk(&mut build, base, step);
3253 deriv(&mut build, base, at, 4);
3254 build.ret(&[]);
3255 let stats = run(&mut func);
3256 assert_eq!(derivs(&func), 1);
3257 assert_eq!(stats.count(Kind::Missed, super::NO_EXTENT_GLOBAL), 1);
3258 }
3259
3260 #[test]
3261 fn a_walk_off_a_pointer_the_check_does_not_name_stays() {
3262 // The capability has to be the `cap_of` of the pointer that went in. One naming something
3263 // else is asking about a different instance and is not this pass's to answer.
3264 let (_, mut func, block, pointer) = blank();
3265 let mut build = Builder::new(&mut func, block);
3266 check(&mut build, pointer, 16);
3267 let field = past(&mut build, pointer, 8);
3268 let args = build.func().push_values(&[field]);
3269 let capability = build.value(InstData { args, ..InstData::new(Opcode::CapOf) }, Type::CAP);
3270 let width = build.iconst(Type::int(64), 4);
3271 let args = build.func().push_values(&[capability, pointer, field, width]);
3272 build.inst(InstData { args, ..InstData::new(Opcode::CheckDeriv) }, &[]);
3273 build.ret(&[]);
3274 let stats = run(&mut func);
3275 assert_eq!(derivs(&func), 1);
3276 assert_eq!(stats.count(Kind::Missed, super::NOT_ITS_CAPABILITY_DERIV), 1);
3277 }
3278}