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.
Precondition: the facts the caller must establish, such as “
bufpoints to at least writable bytes” and “”.Exits: every way control can leave the function. These include a normal return, each distinct error return, an exception, a long jump (the C function
longjmp, which transfers control back to an earlier point of the program without ordinary returns), and no return at all, as for a call that halts or resets the system.Postcondition of each exit: what holds when the function leaves by that exit, such as “the result equals ” for success or “the result is negative” for failure.
May-write set of each exit: every location the function may have changed before leaving by that exit. The set covers writes through pointer arguments, to global variables, to heap objects (memory blocks allocated while the program runs) reachable from the arguments and, for an analysis of machine code, to the registers that the ABI §12.47 (application binary interface, the machine-level rules for calls) allows a called function to overwrite. Every location outside the set keeps its value; this promise is called the frame condition.
Other effects: memory allocated or freed, ownership (the duty to free a piece of memory) passed between caller and called function, locks taken or released, callbacks §14.22 invoked, and input or output performed.
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.
Substitute the actual arguments for the parameters, and check that the caller’s facts imply the precondition. If they do not, report the call.
Make one successor state for each exit.
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.
Add the postcondition of that exit.
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 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.
Precondition:
bufpoints to 8 writable bytes, and .Success exit: returns ; the first bytes of
bufmay hold any values; the other bytes are unchanged.Failure exit: returns ; for some with , the first bytes of
bufmay hold any values; the other bytes are unchanged.No other exits, and no other location written.
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.
Precondition: , and holds.
Success exit: the may-write set is the first 3 bytes, positions 0, 1 and 2; the highest position is . The fact about
buf[0]is deleted. The fact aboutbuf[6]is kept, because position 6 lies outside the set. So the read oftable[buf[6]]is proved safe, and the read oftable[buf[0]]is not: the caller must checkbuf[0]again.Failure exit: is 0, 1 or 2, so
buf[0]andbuf[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 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 as a precondition , a finite set of exit kinds, and, for each exit kind , a postcondition and a may-write set . A state gives a value to every location . The postcondition is a condition on the pair of the entry state and the exit state , so it can relate the result to the arguments. The summary is sound for an implementation of when, for every entry state that satisfies , every execution of from either runs forever or leaves by some exit kind in a state such that holds and for every location . If the summary also promises that always leaves, running forever is excluded.
For one exit kind, this is a Hoare triple §3.31 together with a frame condition: if holds at the start and leaves by exit , then holds and nothing outside has changed. At a call, the analysis may use the summary only after proving . The states after the call are then all states that, for some , satisfy and agree with outside .
Facts kept across a call rest on four assumptions.
The summary matches the code that runs: the same version of the function, built with the same compiler flags §12.3 and configuration.
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.
Called functions keep their own summaries: every function that calls through a pointer satisfies the summary that the summary of assumes for it.
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).