Section 3.18

Alias analysis

Definition

Alias analysis is a static analysis §3.1, a way of reasoning about a program without running it, that decides whether two memory references can refer to the same memory location. A memory reference is an expression, such as *p or a[i], that reaches memory through a computed address; two references that reach the same location alias. For each pair the analysis answers must alias, may alias or no alias. Compilers and program checkers use the answer to decide whether a write through one reference can change what the other reads.

The idea

A C function receives two pointers p and q and runs *p = 1; *q = 2; return *p;, where *p is the integer at the address held in p. A compiler would like to replace return *p by return 1 and save a memory read. That is correct only if p and q never hold the same address. If a caller passes the same address in both, the second write replaces the 1 by 2, and the function must return 2. The two names look like two objects, but they may be one. A program checker that has established that *p holds 1 faces the same question: does the write through q destroy that fact?

In everyday language an alias is a second name for the same person. If “the treasurer” and “Sam” are the same person, a letter handed to the treasurer has been handed to Sam, though the two names look unrelated. Alias analysis decides, for each pair of names in a program, whether they can stand for the same memory. The picture stops being accurate in one respect: two memory references can overlap in part, sharing some bytes and not others, which two people cannot.

Because a program has many executions, the answer has three values. No alias and must alias are guarantees about every execution; may alias covers everything else, including “the analysis could not tell”.

How it works

A program can create a new object, a region of memory that holds one value or structure, every time a loop runs, so the analysis groups the objects of all executions into finitely many abstract objects. The usual choice is one abstract object per allocation site, the place in the code that creates memory: each variable declaration, and each call such as malloc that reserves memory at run time.

The analysis then works in four steps.

  1. Points-to sets. For each pointer p it computes the points-to set pts(𝚙)\mathrm{pts}(\texttt{p}): the abstract objects whose address p may hold. An object that itself holds a pointer has a points-to set of its own. Each kind of statement contributes one rule. The statement p = &x, where &x is the address of x, puts x into pts(𝚙)\mathrm{pts}(\texttt{p}). The copy p = q requires pts(𝚚)⊆pts(𝚙)\mathrm{pts}(\texttt{q}) \subseteq \mathrm{pts}(\texttt{p}). The load p = *q requires that pts(𝚙)\mathrm{pts}(\texttt{p}) contain the points-to set of every object in pts(𝚚)\mathrm{pts}(\texttt{q}). The store *p = q requires that the points-to set of every object in pts(𝚙)\mathrm{pts}(\texttt{p}) contain pts(𝚚)\mathrm{pts}(\texttt{q}).

  2. Solving. It applies the rules until no set grows. This inclusion-based style is named after Lars Ole Andersen (1994). The faster, less precise unification-based style of Bjarne Steensgaard (1996) [259] merges the two sets of an assignment into one.

  3. Answers. The references *p and *q get no alias when pts(𝚙)\mathrm{pts}(\texttt{p}) and pts(𝚚)\mathrm{pts}(\texttt{q}) share no abstract object. They get must alias when both sets consist of one and the same abstract object, that abstract object stands for exactly one location, such as a global variable, and both pointers are known to hold a valid address at that point. Every other pair gets may alias.

  4. Offsets and sizes. Inside one object the analysis compares byte ranges, not start addresses. An access of ss bytes at offset oo covers the half-open interval §14.12 [o,o+s)[o, o + s): the bytes from oo up to, but not including, o+so + s. Two accesses overlap when their ranges share at least one byte.

A flow-insensitive analysis computes one points-to set per pointer for the whole program, ignoring statement order; a flow-sensitive analysis computes one per program point, which is more precise and costs more. Context sensitivity §3.12 is the similar choice for function calls.

Language rules supply further no-alias facts. In C, an object may be accessed only through an expression of its own type or of a few related types, such as a character type, so a compiler may assume that an int access and a float access do not overlap; this is the strict aliasing rule §4.23. A pointer declared with the C qualifier restrict is the programmer’s promise that, within its block of code, any object that is both accessed through it and modified is accessed only through pointers derived from it. Two distinct objects alive at the same time never share a byte. The strict aliasing and restrict facts hold only for a program that obeys those rules.

A compiler or checker acts on the answers at every store. Take a store through q and a fact about *p. With no alias, the fact survives. With must alias, the stored value replaces the old value of *p, which is called a strong update. With may alias, the store may or may not have changed *p, so the possible values of *p become the old ones together with the stored one, which is called a weak update. A may-alias answer also forbids moving the load of *p across the store, and it adds a dependence from the store to the load in any analysis that tracks which statements influence which.

Worked example

Points-to sets. Two integer variables x and y are two abstract objects. A program runs p = &x and q = &y. Then it reads an input c and runs r = p when c is 1, and r = q when c is 0. The rules give pts(𝚙)={𝚡}\mathrm{pts}(\texttt{p}) = \{\texttt{x}\} and pts(𝚚)={𝚢}\mathrm{pts}(\texttt{q}) = \{\texttt{y}\}. Each of the two copies into r adds one set, so pts(𝚛)={𝚡}∪{𝚢}={𝚡,𝚢}\mathrm{pts}(\texttt{r}) = \{\texttt{x}\} \cup \{\texttt{y}\} = \{\texttt{x}, \texttt{y}\}.

Next the program runs *p = 1; *r = 5; t = *p;. With c equal to 1, r holds the address of x, the second store overwrites the 1, and t becomes 5. With c equal to 0, the second store changes y, and t becomes 1. A compiler that replaced t = *p by t = 1 would be wrong whenever c is 1. The may-alias answer forbids that replacement, and the weak update records that x holds 1 or 5 after the second store.

Byte ranges. Overlap inside one object is decided byte by byte. An 8-byte buffer holds, at offsets 0 to 7, the hexadecimal byte values 11 22 33 44 55 66 77 88. A write stores 4 bytes at offset 0, covering [0,4)[0, 4), which is bytes 0, 1, 2 and 3. A read loads 4 bytes from offset 2, covering [2,6)[2, 6), which is bytes 2, 3, 4 and 5. The start offsets differ, 0 against 2, yet the ranges overlap: 0<60 < 6 and 2<42 < 4 both hold, and the shared bytes are [max(0,2),min(4,6))=[2,4)[\max(0, 2), \min(4, 6)) = [2, 4), which is bytes 2 and 3. If the write stores four zero bytes, the buffer becomes 00 00 00 00 55 66 77 88, and the read returns 00 00 55 66 instead of 33 44 55 66. A second read of 4 bytes from offset 4, covering [4,8)[4, 8), shares no byte with the write: 0<80 < 8 holds, but 4<44 < 4 does not.

Library contracts. The C function memcpy requires that its source and destination ranges not overlap; calling it on overlapping ranges is undefined behavior §4.12. Copying 4 bytes from offset 0 to offset 2 uses the ranges [0,4)[0, 4) and [2,6)[2, 6), which share bytes 2 and 3, so the copy must use memmove, which the C standard defines for overlapping ranges.

Stated precisely

Fix a program point and one execution that reaches it. Let loc(e)\mathrm{loc}(e) be the set of byte addresses that the reference ee accesses at that point in that execution. References e1e_1 and e2e_2 alias in that execution when loc(e1)∩loc(e2)≠∅\mathrm{loc}(e_1) \cap \mathrm{loc}(e_2) \neq \emptyset, that is, when they share at least one byte. An alias analysis is sound when every answer it gives is true over all the executions it models, with these meanings:

For two accesses to one object at the byte ranges [a,b)[a, b) and [c,d)[c, d), where a<ba < b, c<dc < d and no address computation wraps around past the largest address, the ranges overlap exactly when a<da < d and c<bc < b. The shared bytes are then [max(a,c),min(b,d))[\max(a, c), \min(b, d)).

Three assumptions carry these guarantees to the real program.

  1. Every writer is seen: the analysis covers every statement that can write a pointer or the memory it points to, or holds a correct summary of code it cannot see, such as library functions, assembly code and hardware that writes memory directly (direct memory access §4.36, DMA).

  2. No undefined behavior: pointers stay within their objects, and the program obeys the language rules the analysis relies on, such as strict aliasing and restrict.

  3. Every concurrent writer is modeled: every thread and interrupt handler §6.12 that can touch the memory is part of the analysis.

Exact alias analysis is undecidable §19.2.1 for a general programming language: whether two references alias at a point depends on whether a particular assignment ever runs, which is already undecidable, as the halting problem §19.2.2 shows. Every practical alias analysis therefore answers may alias for some pairs that never alias.

Limits and common mistakes

Not knowing is not no alias. When an analysis cannot resolve a pointer, for example one loaded from a table at an index taken from input, the sound answer is may alias with every object whose address could be in that table. Turning an unresolved case into no alias makes every optimization and proof built on it unsound. The opposite error is milder but costly: too many may-alias answers block optimizations and, in a checker, produce false alarms §1.22.

The same address at two different times is a question of object lifetime §4.1, not aliasing. After an object is freed, a new object can be placed at the same address. A stale pointer to the old object then holds the same number as a pointer to the new one, but using it is a use-after-free §4.6, an error in its own right.

Equal addresses do not always mean the same object. A pointer one past the end of one array §14.5 can equal a pointer to the start of the next object in memory, yet C does not allow the first to be used to access the second. The rules about which object a pointer may access, beyond its numeric value, are called pointer provenance §4.24.

Language-based no-alias facts are promises by the programmer. A program that reads a float object through an int pointer, or changes one object through two restrict pointers, breaks them. That is undefined behavior: the C standard places no requirement on what the program then does, and an optimizing compiler may produce results that differ from what the source text seems to say.

Memory that something outside the analyzed code can change needs its own model: a hardware register mapped into the address space, a buffer that a device fills by direct memory access, or a variable shared with an interrupt handler or another thread. The C volatile qualifier makes the compiler perform every access to such an object as written, but it does not make access from two threads safe.

Questions answered

What changes when two apparent objects may overlap?

When two references that look like separate objects may overlap, every write through one must be treated as a possible write to the other. Four things follow.

Overlap is judged on the bytes accessed, not the start addresses: a 4-byte write at offset 0 and a 4-byte read at offset 2 share bytes 2 and 3. A claim that the two do not overlap needs evidence, such as two distinct live objects, disjoint byte ranges, or a language rule the program provably obeys. Not knowing whether they overlap is not knowing that they do not, so an analysis that cannot decide must answer may alias.

Related entries: Strict aliasing and effective type (§4.23), Temporal memory safety (§2.29), Def-use chain (§3.17), Pointer provenance (§4.24), Object lifetime (§4.1), Half-open interval (§14.12), Compiler optimization (§3.39). Further reading: [259].