angr's solver engine is called Claripy. Claripy exposes the following design:
- Claripy ASTs (the subclasses of claripy.ast.Base) provide a unified way to interact with concrete and symbolic expressions
Frontends provide different paradigms for evaluating these expressions. For example, the
FullFrontendsolves expressions using something like an SMT solver backend, while
LightFrontendhandles them by using an abstract (and approximating) data domain backend.
Frontendneeds to, at some point, do actual operation and evaluations on an AST. ASTs don't support this on their own. Instead,
Backends translate ASTs into backend objects (i.e., Python primitives for
BackendConcrete, Z3 expressions for
BackendZ3, strided intervals for
BackendVSA, etc) and handle any appropriate state-tracking objects (such as tracking the solver state in the case of
BackendZ3). Roughly speaking, frontends take ASTs as inputs and use backends to
backend.convert()those ASTs into backend objects that can be evaluated and otherwise reasoned about.
FrontendMixins customize the operation of
Frontends. For example,
ModelCacheMixincaches solutions from an SMT solver.
- The combination of a Frontend, a number of FrontendMixins, and a number of Backends comprise a claripy
Internally, Claripy seamlessly mediates the co-operation of multiple disparate backends -- concrete bitvectors, VSA constructs, and SAT solvers. It is pretty badass.
Most users of angr will not need to interact directly with Claripy (except for, maybe, claripy AST objects, which represent symbolic expressions) -- angr handles most interactions with Claripy internally. However, for dealing with expressions, an understanding of Claripy might be useful.
Claripy ASTs abstract away the differences between mathematical constructs that Claripy supports. They define a tree of operations (i.e.,
(a + b) / c)on any type of underlying data. Claripy handles the application of these operations on the underlying objects themselves by dispatching requests to the backends.
Currently, Claripy supports the following types of ASTs:
All of the above creation code returns claripy.AST objects, on which operations can then be carried out.
ASTs provide several useful operations.
>>> import claripy
>>> bv = claripy.BVV(0x41424344, 32)
# Size - you can get the size of an AST with .size()
>>> assert bv.size() == 32
# Reversing - .reversed is the reversed version of the BVV
>>> assert bv.reversed is claripy.BVV(0x44434241, 32)
>>> assert bv.reversed.reversed is bv
# Depth - you can get the depth of the AST
>>> assert bv.depth == 1
>>> x = claripy.BVS('x', 32)
>>> assert (x+bv).depth == 2
>>> assert ((x+bv)/10).depth == 3
Applying a condition (==, !=, etc) on ASTs will return an AST that represents the condition being carried out. For example:
>>> r = bv == x
>>> assert isinstance(r, claripy.ast.Bool)
>>> p = bv == bv
>>> assert isinstance(p, claripy.ast.Bool)
>>> assert p.is_true()
You can combine these conditions in different ways.
>>> q = claripy.And(claripy.Or(bv == x, bv * 2 == x, bv * 3 == x), x == 0)
>>> assert isinstance(p, claripy.ast.Bool)
The usefulness of this will become apparent when we discuss Claripy solvers.
In general, Claripy supports all of the normal Python operations (+, -, |, ==, etc), and provides additional ones via the Claripy instance object. Here's a list of available operations from the latter.
NOTE: The default Python
<=are unsigned in Claripy. This is different than their behavior in Z3, because it seems more natural in binary analysis.
The main point of interaction with Claripy are the Claripy Solvers. Solvers expose an API to interpret ASTs in different ways and return usable values. There are several different solvers.
Some examples of solver usage:
# create the solver and an expression
>>> s = claripy.Solver()
>>> x = claripy.BVS('x', 8)
# now let's add a constraint on x
>>> s.add(claripy.ULT(x, 5))
>>> assert sorted(s.eval(x, 10)) == [0, 1, 2, 3, 4]
>>> assert s.max(x) == 4
>>> assert s.min(x) == 0
# we can also get the values of complex expressions
>>> y = claripy.BVV(65, 8)
>>> z = claripy.If(x == 1, x, y)
>>> assert sorted(s.eval(z, 10)) == [1, 65]
# and, of course, we can add constraints on complex expressions
>>> s.add(z % 5 != 0)
>>> assert s.eval(z, 10) == (1,)
>>> assert s.eval(x, 10) == (1,) # interestingly enough, since z can't be y, x can only be 1!
Custom solvers can be built by combining a Claripy Frontend (the class that handles the actual interaction with SMT solver or the underlying data domain) and some combination of frontend mixins (that handle things like caching, filtering out duplicate constraints, doing opportunistic simplification, and so on).
Backends are Claripy's workhorses. Claripy exposes ASTs to the world, but when actual computation has to be done, it pushes those ASTs into objects that can be handled by the backends themselves. This provides a unified interface to the outside world while allowing Claripy to support different types of computation. For example, BackendConcrete provides computation support for concrete bitvectors and booleans, BackendVSA introduces VSA constructs such as StridedIntervals (and details what happens when operations are performed on them, and BackendZ3 provides support for symbolic variables and constraint solving.
There are a set of functions that a backend is expected to implement. For all of these functions, the "public" version is expected to be able to deal with claripy's AST objects, while the "private" version should only deal with objects specific to the backend itself. This is distinguished with Python idioms: a public function will be named func() while a private function will be _func(). All functions should return objects that are usable by the backend in its private methods. If this can't be done (i.e., some functionality is being attempted that the backend can't handle), the backend should raise a BackendError. In this case, Claripy will move on to the next backend in its list.
All backends must implement a
convert()function. This function receives a claripy AST and should return an object that the backend can handle in its private methods. Backends should also implement a
_convert()method, which will receive anything that is not a claripy AST object (i.e., an integer or an object from a different backend). If
_convert()receives something that the backend can't translate to a format that is usable internally, the backend should raise BackendError, and thus won't be used for that object. All backends must also implement any functions of the base
Backendabstract class that currently raise
Claripy's contract with its backends is as follows: backends should be able to handle, in their private functions, any object that they return from their private or public functions. Claripy will never pass an object to any backend private function that did not originate as a return value from a private or public function of that backend. One exception to this is
_convert(), as Claripy can try to stuff anything it feels like into _convert() to see if the backend can handle that type of object.
To perform actual, useful computation on ASTs, Claripy uses backend objects. A
BackendObjectis a result of the operation represented by the AST. Claripy expects these objects to be returned from their respective backends, and will pass such objects into that backend's other functions.