Everything Everywhere All at Once
In this topic, we are going to perform our first static analysis. It is based on abstract interpretation.
In bounded static analysis, we want to talk about all the behaviors of the program up to some depth. There is no easy way to do this, because we have to handle a potentially infinite set of traces, which is hard enough on its own.
The idea behind bounded static analysis is straightforward: instead of selecting a single initial state and then applying the semantics to that until a trace has been made, we do something different. This is what we did with dynamic analysis. We start with the set of initial states and then apply the semantics to all states at once. Essentially, if dynamic analysis is depth-first search, static analysis is breadth-first. While we have the downside of having to work with infinite sets of traces, we can be smart about it and use abstractions to represent the set using a finite number of traces.
The goal of any static analysis is to soundly predict properties that must happen, e.g ., a property which is true for all traces. For bounded static analyses, we make this problem a little easier by only considering traces up to length .
We write that the property was found true in program for all traces smaller than , like so . A sound bounded static analysis will imply that any property found is also be true in the program :
In practice, we often solve a negated may problem. Instead of assuring that a property holds for all traces of size , we check if we can disprove that there exist a trace of size in which the property does not hold:
We use the notation to indicate that our may analysis should be complete:
We can check if a trace contains an assertion error by inspecting the last element:
In a static analysis, we would like to guarantee that an assertion error does not happen:
We can then use a not-may analysis for assertion errors down to depth . Essentially, if we cannot find a trace which emits an assertion error, we conclude that the program must not contain a trace which emits an assertion error.
Bounded Static Analysis on a program can be defined as the set of traces of depth . We can get this set using a multi-step function . It takes a set of traces and advances them by one step.
In the definition of , is the transition relation defined by the single step semantics, and means appending to . Since we are always talking about the same program , we'll sometimes omit it.
Now, we can define the set of traces of up to length , by applying to the initial state, times, and taking the union of the results.
Sadly, it is infeasible, and sometimes impossible, to do computations over all traces at once. Therefore, when designing an analysis, we either underestimate the set of traces or overestimate the set of traces.
In a may analysis we overestimate every step, and in the must analysis we underestimate every step:
Naturally, we'll see that
From this we get two complete analyses, a not-may analysis:
and a not-must analysis:
As we saw in the previous section, the best approach is to look for negated properties and use the not-may
analysis.
However, it is unclear why allowing us to approximate the result will allow us to run the analyses at all. We need abstractions!
Instead of working with sets of traces, we have to work with finite structures instead. We call these abstractions.
Before we go into the theory, let's start with a concrete example. Assume that we are working with all the possible integer values that a variable has at some point in the program. It would be much easier to simply keep track of whether the set contains positive, negative, or zero values.
We can represent this as a set that only contains three elements , exactly zero , larger than zero and smaller than zero . This set is of size , which is much easier to work with than the set of size . In the following sections, we are going to refer to this set as .
We call an abstraction, as we can convert the concrete domain of integers into the abstract domain of . We call this conversion the abstraction :
Implement the SignSet as an abstract set in the language of your choosing.
In Python, you can start with this:
from dataclasses import dataclass
from typing import TypeAlias, Literal
Sign : TypeAlias = Literal[1] | Literal[-1] | Literal[0]
@dataclass
class SignSet:
signs : frozenset[Sign]
...The solution is in solutions/static/src/abstractions.py.
However, before we continue with this set, we first have to ensure that a computation in an abstract domain does not invalidate our over-approximation.
In the following sections, we'll see that:
Our abstract domains can be ordered.
We can get the least over-approximation of two abstract sets, called a join, and the greatest under-approximation of two abstract sets, called a meet.
Then, we have a formal definition of a correct over-approximation or under-approximation called a Galois connection.
And finally, we'll cover monotone functions, which are functions that preserve order.
A partially ordered set or poset is a tuple set of elements and an ordering , that uphold:
Common partially ordered sets are the integers (also in the other direction ), the booleans , and the set of .
Think about if the rules apply to the posets above.
Make your abstraction into a partially ordered set by implementing (<=) in the language of your choice.
A lattice is a partially ordered set , with two extra operators and . is the least upper bound , meaning that
The dual is true for , which is the greatest lower bound
Furthermore, this implies that there exist a least bound and a greatest bound , from which we have the following identities: , and and .
A Hasse diagram over the poset . By I, KSmrq, CC BY-SA 3.0 (with edits)
The reason why they are called lattices is that they can be drawn using Hasse diagrams, which gives these nice structures that look like a wooden lattice.
Here we only draw the immediate next elements in the order. So while we do not draw that edge because and are in the way.
Create a Hasse diagram over your favorite animals. The order is . Where
While objectively , some of the other animals like and are no worse than each other.
Is your order a lattice: Does every pair of animals have a meet and a join?
In our Sign abstraction, we use set inclusion as our order and use the intersection and union of the underlying set as the meet () and join () of our lattice. and .
To make your abstraction into a lattice, you need to also implement the meet and join operators.
In Python, you can implement __and__ and __or__ to get the meet operator a & b, and the join operator a | b.

(A Galois Connection is a connection between two ordered sets, with a concretion and an abstraction function.)
A Galois connection is a relationship between two ordered and ordered sets and , where we can abstract information from the concrete domain into the abstract while preserving the order if going back . explains how to abstract a value, and explains what the abstraction means.
Because the abstraction might be lossy, it is not the case that if we abstract a value we get back the same concrete value . Instead, a Galois connection satisfies the following rule:
It states that if we have a concrete value , and abstract it , then any value larger
than this abstraction will concretize to a value that is larger
than the original value. At first this might not seem useful, but if we satisfy this rule we get the following laws for free:
The first two rules give us confidence that whatever abstraction we choose only loses information and never creates it. And the second two rules give us confidence that we don't keep losing that information.
Galois connections are actually a concrete example of a mathematical construct called adjunction. Adjunctions are one of the most useful and unappreciated mathematical structures in computer science. One example is a testing technique for data readers.
While in most cases we would expect anything we pretty-print to be parsable to the same value. That is not always the case, as line numbers might differ or some data is lost in the translation. It should, however, always hold that
# Most of the time
parse(pretty(a)) == a
# Always
pretty(parse(pretty(a))) == pretty(a)
# Bonus for all strings s:
parse(pretty(parse(s))) == parse(s)We can see that there exists a Galois connection between our concrete integer domain and our abstract sign domain , where the abstraction $ and concretion $ are defined like this:
We can also see that we satisfy the rules; let's pick a concrete value . If we convert that to an abstract value we get: .
Since , and since and , then we get that .
You need an abstract function, which takes a set and turns it into your abstract domain.
def to_sign(value: jvms.StackValue) -> Sign:
match value:
case jvms.StackInt(v):
return (v > 0) - (v < 0)
case a:
raise NotImplementedError(f"Unsupported value {value!r}")
class SignSet:
...
@classmethod
def abstract(cls, items : Iterable[jvms.StackValue]):
signs = set()
for value in values:
signs.add(to_sign(value))
return cls(frozenset(signs))Then because we are using possible infinite sets, instead of generating that set, we can just implement the contains method:
def __contains__(self, value : jvms.StackValue):
return to_sign(value) in self.signsYou can use the Galois rules to test your abstraction:
If you are using Python, you can fuzz test this property using hypothesis. A working example is provided in solutions/static/tests/test_abstractions.py. You can run the tests using uv run pytest.
Finally, we have reached the fun part. We can now define operations in the abstract domain that mimic the operations in the concrete domain.
Because our concrete domain is the set of integers, we can define , as the operation over the product:
Now we can also define the plus abstract domain:
And many more.
But, how do we know if we have implemented our abstract operation correctly? Since we want to over-approximate our operations, we know that computing in our abstract domain should never under-approximate:
The above equation requires us to create huge sets of values. Instead, we can use our Galois connection to rewrite the equation to see that we only have to show the following:
Essentially, abstracting the result of an operation should be smaller than abstracting and doing the operation in the abstract domain.
Implement arithmetic and comparators for your sign abstraction.
Check out the test_signset_arithmetic and test_signset_compare functions in, and expand the arithmetic and compare methods in the SignSet class.
In practice, Galois connections allow us to map our infinite domain of may or must analyses into a more manageable abstract domain, while giving us guarantees that we are still correctly over - or under-estimating.
Let's assume we are building a may analysis. Since we want to over-approximate our function, we therefore introduce an abstract domain that has an abstract stepping function . We assume the Galois connection . We can use this new abstract stepping function to define a bounded static analysis of depth . We define $ recursively:
We can prove by induction over that stepping in the abstract domain is in fact a may analysis , given that
We can also show that is a stronger statement, and is sufficient to prove the above, by seeing that From . And using that gamma preserves meets, we get
Finally, since the concrete domain is infinite and therefore difficult to discuss directly, we can use the Galois connection again to show that we only have to prove:
The first, and most useful abstraction, is the state abstraction. Here we throw away everything from the trace except the final state of the trace:
We can define the abstraction as the set of end states of the traces and the concretion as the set of traces that end in one of the states.
This is a useful abstraction for us, because we do not need to know where the trace came from to figure out if an assertion error exists in the program, only that a trace ends in an assertion error.
We can now define as only stepping the final state of each trace:
Now let . It is now clear by induction on that . So if there exists an assertion error within $ steps, then there exists a trace such that and .
From our Galois connection, we can see that
Which means that if , then . Which is exactly the guarantee we are looking for in a may analysis.
In our next abstraction, we want to take advantage of the fact that we execute the same instruction in states with the same program counter.
Let's focus on a JVM without a method stack, which means that every state is . We'll get back to discussing how to handle the method/call stack in later sections. We can abstract this by collecting the states per . Let be a mapping from program counters to sets of states. is a lattice with partial order where one mapping is less than another if all corresponding states are smaller and is the pointwise $ of states.
Now we can write a stepping function that can step all states with the same instruction at once , and then merge the result into the other states.
We are now faced with our first hard choice.
We would love to use the Sign abstraction, which we built in the previous section. One way of doing that is compressing the state, so that instead of having a set of states, we distribute the content of the locals and stack. So instead of having a set of states, we have a pair of abstract locals and abstract stack. The abstract locals is not a mapping from naturals to a powerset of possible values, and the abstract stack is a stack of possible values:
Since the stack and the locals are typed at each instruction, these are actually sets of integers, which we know how to abstract using the sign abstraction. Furthermore is also a partially ordered set, by pointwise comparing the set of variables, and a lattice by pointwise joining and meeting the sets of variables. We can also see that is a Galois connection.
The problem with this abstraction, and the reason this choice is hard, is that it is the first that severely over-approximates our problem. More about this next time.
Define the Abstract (per variable) Frame in your program analysis; remember to define the ordering as well as the meet and join operations. It might be useful to define three pseudo elements: Bot, which is an element that joins with all others; Err, which represents the error state; and Ok, which represents the Ok state.
In Python, we could define an abstract frame like so:
@dataclass
class State:
locals: tuple[SignSet, ...]
stack: tuple[SignSet, ...]Remember to define the abstract, __le__, __and__, and __or__ methods.
Finally, we are at a point where we can use our Sign Abstraction, by using it to abstract the sets of variables.
If we can see is an abstraction of , then we also get a Galois connection from to .
Specifically, we get:
You have been introduced to the Sign Abstraction, but next time we'll look at more different abstractions. If you have time, try to come up with your own.
One great thing about Galois connections is that they compose. This means we can create one long chain:
To get started with building your first static analysis, first complete the implementation of the SignSet analysis in the abstractions.py file, with the help of the test_abstractions.py tests.
Then go on to add steps to the manystep function in src/static.py using your interpreter as a guide.
After this, we can get creative by abstracting more of the state.
Until next time, write the best analysis you can and upload the results to Autolab, and ponder the following:
Why should we use abstractions when analyzing code?
What is the advantage of Galois connections?
Which cases can the Sign abstraction not catch?
Cousot, Patrick; Cousot, Radhia (1977). Abstract interpretation.
doi:10.1145/512950.512973 link
Cousot, Patrick; Cousot, Radhia (1979). Systematic design of program analysis frameworks.
doi:pdf/10.1145/567752.567778 link
Nielson, Hanne Riis; Nielson, Flemming (2007). Semantics with Applications.