Section 15.14

Interprocedural summary

Definition

An interprocedural summary is a description of what a function does that a static analysis §3.1 (a tool that reasons about every run of a program without running it) uses at every call instead of analyzing the function’s body again. The summary states the precondition, what must hold when the call starts. For each way the call can end, such as a normal return, an error return or an exception, it states what holds afterwards: the result, every memory location the function may have written, and every other effect.

The idea

A static analysis that reaches a call must know what the call can change and what it returns; otherwise every fact the caller has established becomes doubtful. One answer is to analyze the called function’s body again at every call, as if the body were pasted in at the call. That repeats the work for every call of every function, multiplies along chains of calls, and never ends for a recursive function. A summary answers the question once per function, and the analysis reuses the answer at every call. Such an analysis is called interprocedural because it carries facts across the boundaries between procedures, from caller to called function and back.

The datasheet of an electronic part is a fair picture. A designer uses the datasheet, not the circuit inside the part, to decide whether the part fits a design, so the datasheet must list every output the part can drive, including what it does on a fault. The picture stops being accurate in one respect. A datasheet may describe only typical behavior, but a summary used in a proof must admit every behavior that the real function can show when the precondition holds. A missing write or a missing error exit makes every proof that uses the summary unsound, even if every test passes.

How it works

A summary used for proofs lists the following, each under the assumption that the precondition holds.

A summary may also give a must-write set, the locations written on every run. The must-write set adds precision; soundness §3.7 needs the may-write set.

At a call, the analysis uses the summary in five steps.

  1. Substitute the actual arguments for the parameters, and check that the caller’s facts imply the precondition. If they do not, report the call.

  2. Make one successor state for each exit.

  3. In each successor state, delete every fact about a location in the may-write set of that exit, together with every fact derived from such a location.

  4. Add the postcondition of that exit.

  5. Keep every fact about locations outside the may-write set; the frame condition guarantees these facts.

Summaries are either written by hand, as specifications that a verifier checks against the function’s body, or computed by the analysis itself. A computing analysis works bottom-up: it summarizes called functions before their callers, and it repeats the analysis of mutually recursive functions until their summaries stop changing, which is a fixed point §3.22. Micha Sharir and Amir Pnueli described this functional approach to interprocedural analysis in 1981, together with the call-string approach, which instead keeps separate facts for each chain of calls that leads to a function. A summary that takes the caller’s facts as a parameter, instead of giving one fixed result, keeps callers apart; this is the summary form of context sensitivity §3.12. When a program inside a virtual machine §2.3 calls a function of the host system, the summary of that host function is its helper contract §2.21.

Worked example

A function fill(buf, n) copies nn bytes, one at a time, from a receive queue into the 8-byte array §14.5 buf. It fails if the queue runs empty. Its summary is as follows.

Before the call, the caller has proved that buf[0] and buf[6] are at most 7. It calls fill(buf, 3) and then reads table[buf[6]] and table[buf[0]] from an 8-entry array table, whose valid indices are 0 to 7.

  1. Precondition: n=3n = 3, and 0≤3≤80 \le 3 \le 8 holds.

  2. Success exit: the may-write set is the first 3 bytes, positions 0, 1 and 2; the highest position is n−1=3−1=2n - 1 = 3 - 1 = 2. The fact about buf[0] is deleted. The fact about buf[6] is kept, because position 6 lies outside the set. So the read of table[buf[6]] is proved safe, and the read of table[buf[0]] is not: the caller must check buf[0] again.

  3. Failure exit: kk is 0, 1 or 2, so buf[0] and buf[1] may have changed although the call failed. A caller that skips the check after a failure, reasoning that a failed call changed nothing, is wrong.

Now suppose the summary wrongly claimed that fill writes no memory. The analysis keeps the fact that buf[0] is at most 7 and proves the read of table[buf[0]] safe. A real run can copy the byte value 200 into buf[0], and the read then lands 200−7=193200 - 7 = 193 entries past table[7], the last valid entry. The proof failed because the summary was incomplete, not because the analysis made a mistake.

Stated precisely

Write a summary of a function ff as a precondition PP, a finite set KK of exit kinds, and, for each exit kind k∈Kk \in K, a postcondition QkQ_k and a may-write set MkM_k. A state ss gives a value s(ℓ)s(\ell) to every location ℓ\ell. The postcondition Qk(s,s′)Q_k(s, s') is a condition on the pair of the entry state ss and the exit state s′s', so it can relate the result to the arguments. The summary is sound for an implementation of ff when, for every entry state ss that satisfies PP, every execution of ff from ss either runs forever or leaves by some exit kind k∈Kk \in K in a state s′s' such that Qk(s,s′)Q_k(s, s') holds and s′(ℓ)=s(ℓ)s'(\ell) = s(\ell) for every location ℓ∉Mk\ell \notin M_k. If the summary also promises that ff always leaves, running forever is excluded.

For one exit kind, this is a Hoare triple §3.31 {P}f{Qk}\{P\}\ f\ \{Q_k\} together with a frame condition: if PP holds at the start and ff leaves by exit kk, then QkQ_k holds and nothing outside MkM_k has changed. At a call, the analysis may use the summary only after proving PP. The states after the call are then all states s′s' that, for some k∈Kk \in K, satisfy Qk(s,s′)Q_k(s, s') and agree with ss outside MkM_k.

Facts kept across a call rest on four assumptions.

  1. The summary matches the code that runs: the same version of the function, built with the same compiler flags §12.3 and configuration.

  2. No other writer: no other thread, interrupt handler §6.12 or device writes the locations of the caller during the call, or such writes are modeled separately.

  3. Called functions keep their own summaries: every function that ff calls through a pointer satisfies the summary that the summary of ff assumes for it.

  4. The ABI holds: at machine level, the registers that the ABI requires a called function to preserve are really restored.

Limits and common mistakes

A summary describes one implementation. A new version of the function, other compiler flags, or a different choice in conditional compilation §15.16 can change its writes and exits, and the summary must then be checked again.

The ABI and the summary answer different questions. The ABI fixes where arguments and results travel and which registers a called function may overwrite. The summary states what the function does with memory and how it can fail. An analysis of machine code needs both: the registers that a called function may overwrite belong to its may-write set.

An error exit is not a promise that nothing changed. Unless the function guarantees atomicity §8.3, meaning that it makes all of its writes or none, the summary must describe the partial writes made before each error exit.

A summary describes only the function’s own effects. Locations shared with another thread or an interrupt handler can change during the call for reasons that the summary does not cover.

A sound summary can be imprecise. A may-write set larger than needed deletes facts that were still true and causes false alarms §1.22, and one summary for all callers merges their facts. Bug-finding tools sometimes use summaries that list only some behaviors; such summaries can reveal real bugs but cannot support a proof.

A function that invokes a callback has effects that depend on the function called. Its summary must include the summary of every function that can be called there, or take that summary as a parameter. Likewise, the summary of a wrapper function §14.27 must include every effect of the function it wraps.

Questions answered

Which memory writes and exceptional exits must the summary include?

The summary must include every memory write and every exit that the function can produce, on any path, from any state that satisfies its precondition. An interprocedural summary is the description of a function that a static analysis uses at each call instead of analyzing the function’s body again, so the analysis assumes that whatever the summary leaves out cannot happen.

The writes to include are writes through pointer arguments, writes to global and static variables, writes to heap objects reachable from the arguments or from global variables, memory allocated or freed, and, for machine code, every register that the calling rules let the function overwrite. The exits to include are the normal return with each possible kind of result, every error return, every exception or long jump, and every path that does not return at all, such as a halt or a reset. For each exit, the summary must state the writes that may already have happened when control leaves by that exit, including partial writes made before an error, unless the function guarantees that it makes all of its writes or none. A write or exit left out lets the caller keep a fact that the call may have destroyed, and every proof built on that fact becomes unsound.

Related entries: Helper contract (§2.21), Guest-host ABI (§2.37), Context sensitivity (§3.12), Hoare triple (§3.31), Ownership (§4.2), Native application binary interface (ABI) (§12.47), Callback (§14.22), Atomicity (§8.3).