SAW Manual
The Software Analysis Workbench (SAW) is a tool for constructing mathematical models of the computational behavior of software, transforming these models, and proving properties about them.
SAW can currently construct models of a subset of programs written in Cryptol, LLVM (and therefore C), and JVM (and therefore Java). SAW also has experimental, incomplete support for MIR (and therefore Rust). The models take the form of typed functional programs, so in a sense SAW can be considered a translator from imperative programs to their functional equivalents. Various external proof tools, including a variety of SAT and SMT solvers, can be used to prove properties about the functional models. SAW can construct models from arbitrary Cryptol programs, and from C and Java programs that have fixed-size inputs and outputs and that terminate after a fixed number of iterations of any loop (or a fixed number of recursive calls). One common use case is to verify that an algorithm specification in Cryptol is equivalent to an algorithm implementation in C or Java.
The process of extracting models from programs, manipulating them, forming queries about them, and sending them to external provers is orchestrated using a special purpose language called SAWScript. SAWScript is a typed functional language with support for sequencing of imperative commands.
The rest of this document first describes how to use the SAW tool, saw
, and outlines the structure of the SAWScript language and its relationship to Cryptol. It then presents the SAWScript commands that transform functional models and prove properties about them. Finally, it describes the specific commands available for constructing models from imperative programs.
Invoking SAW
The primary mechanism for interacting with SAW is through the saw
executable included as part of the standard binary distribution. With no arguments, saw
starts a read-evaluate-print loop (REPL) that allows the user to interactively evaluate commands in the SAWScript language. With one file name argument, it executes the specified file as a SAWScript program.
In addition to a file name, the saw
executable accepts several command-line options:
-h, -?, --help
Print a help message.
-V, --version
Show the version of the SAWScript interpreter.
-c path, --classpath=path
Specify a colon-delimited list of paths to search for Java classes.
-i path, --import-path=path
Specify a colon-delimited list of paths to search for imports.
-t, --extra-type-checking
Perform extra type checking of intermediate values.
-I, --interactive
Run interactively (with a REPL). This is the default if no other arguments are specified.
-j path, --jars=path
Specify a colon-delimited list of paths to .jar
files to search for Java classes.
-b path, --java-bin-dirs
Specify a colon-delimited list of paths to search for a Java executable.
-d num, --sim-verbose=num
Set the verbosity level of the Java and LLVM simulators.
-v num, --verbose=num
Set the verbosity level of the SAWScript interpreter.
--clean-mismatched-versions-solver-cache[=path]
Run the clean_mismatched_versions_solver_cache
command on the solver cache at the given path, or if no path is given, the solver cache at the value of the SAW_SOLVER_CACHE_PATH
environment variable, then exit. See the section Caching Solver Results for a description of the clean_mismatched_versions_solver_cache
command and the solver caching feature in general.
SAW also uses several environment variables for configuration:
CRYPTOLPATH
Specify a colon-delimited list of directory paths to search for Cryptol imports (including the Cryptol prelude).
PATH
If the --java-bin-dirs
option is not set, then the PATH
will be searched to find a Java executable.
SAW_IMPORT_PATH
Specify a colon-delimited list of directory paths to search for imports.
SAW_JDK_JAR
Specify the path of the .jar
file containing the core Java libraries. Note that that is not necessary if the --java-bin-dirs
option or the PATH
environment variable is used, as SAW can use this information to determine the location of the core Java libraries’ .jar
file.
SAW_SOLVER_CACHE_PATH
Specify a path at which to keep a cache of solver results obtained during calls to certain tactics. A cache is not created at this path until it is needed. See the section Caching Solver Results for more detail about this feature.
On Windows, semicolon-delimited lists are used instead of colon-delimited lists.
Structure of SAWScript
A SAWScript program consists, at the top level, of a sequence of commands to be executed in order. Each command is terminated with a semicolon. For example, the print
command displays a textual representation of its argument. Suppose the following text is stored in the file print.saw
:
The command saw print.saw
will then yield output similar to the following:
The same code can be run from the interactive REPL:
At the REPL, terminating semicolons can be omitted:
To make common use cases simpler, bare values at the REPL are treated as if they were arguments to print
:
One SAWScript file can be included in another using the include
command, which takes the name of the file to be included as an argument. For example:
Typically, included files are used to import definitions, not perform side effects like printing. However, as you can see, if any commands with side effects occur at the top level of the imported file, those side effects will occur during import.
Syntax
The syntax of SAWScript is reminiscent of functional languages such as Cryptol, Haskell and ML. In particular, functions are applied by writing them next to their arguments rather than by using parentheses and commas. Rather than writing f(x, y)
, write f x y
.
Comments are written as in C, Java, and Rust (among many other languages). All text from //
until the end of a line is ignored. Additionally, all text between /*
and */
is ignored, regardless of whether the line ends.
Basic Types and Values
All values in SAWScript have types, and these types are determined and checked before a program runs (that is, SAWScript is statically typed). The basic types available are similar to those in many other languages.
The
Int
type represents unbounded mathematical integers. Integer constants can be written in decimal notation (e.g.,42
), hexadecimal notation (0x2a
), and binary (0b00101010
). However, unlike many languages, integers in SAWScript are used primarily as constants. Arithmetic is usually encoded in Cryptol, as discussed in the next section.The Boolean type,
Bool
, contains the valuestrue
andfalse
, like in many other languages. As with integers, computations on Boolean values usually occur in Cryptol.Values of any type can be aggregated into tuples. For example, the value
(true, 10)
has the type(Bool, Int)
.Values of any type can also be aggregated into records, which are exactly like tuples except that their components have names. For example, the value
{ b = true, n = 10 }
has the type{ b : Bool, n : Int }
.A sequence of values of the same type can be stored in a list. For example, the value
[true, false, true]
has the type[Bool]
.Strings of textual characters can be represented in the
String
type. For example, the value"example"
has typeString
.The “unit” type, written
()
, is essentially a placeholder, similar tovoid
in languages like C and Java. It has only one value, also written()
. Values of type()
convey no information. We will show in later sections several cases where this is useful.Functions are given types that indicate what type they consume and what type they produce. For example, the type
Int -> Bool
indicates a function that takes anInt
as input and produces aBool
as output. Functions with multiple arguments use multiple arrows. For example, the typeInt -> String -> Bool
indicates a function in which the first argument is anInt
, the second is aString
, and the result is aBool
. It is possible, but not necessary, to group arguments in tuples, as well, so the type(Int, String) -> Bool
describes a function that takes one argument, a pair of anInt
and aString
, and returns aBool
.
SAWScript also includes some more specialized types that do not have straightforward counterparts in most other languages. These will appear in later sections.
Basic Expression Forms
One of the key forms of top-level command in SAWScript is a binding, introduced with the let
keyword, which gives a name to a value. For example:
Bindings can have parameters, in which case they define functions. For instance, the following function takes one parameter and constructs a list containing that parameter as its single element.
Functions themselves are values and have types. The type of a function that takes an argument of type a
and returns a result of type b
is a -> b
.
Function types are typically inferred, as in the example f
above. In this case, because f
only creates a list with the given argument, and because it is possible to create a list of any element type, f
can be applied to an argument of any type. We say, therefore, that f
is polymorphic. Concretely, we write the type of f
as {a} a -> [a]
, meaning it takes a value of any type (denoted a
) and returns a list containing elements of that same type. This means we can also apply f
to 10
:
However, we may want to specify that a function has a more specific type. In this case, we could restrict f
to operate only on Int
parameters.
This will work identically to the original f
on an Int
parameter:
However, it will fail for a String
parameter:
Type annotations can be applied to any expression. The notation (e : t)
indicates that expression e
is expected to have type t
and that it is an error for e
to have a different type. Most types in SAWScript are inferred automatically, but specifying them explicitly can sometimes enhance readability.
Because functions are values, functions can return other functions. We make use of this feature when writing functions of multiple arguments. Consider the function g
, similar to f
but with two arguments:
Like f
, g
is polymorphic. Its type is {a} a -> a -> [a]
. This means it takes an argument of type a
and returns a function that takes an argument of the same type a
and returns a list of a
values. We can therefore apply g
to any two arguments of the same type:
But type checking will fail if we apply it to two values of different types:
So far we have used two related terms, function and command, and we take these to mean slightly different things. A function is any value with a function type (e.g., Int -> [Int]
). A command is any value with a special command type (e.g. TopLevel ()
, as shown below). These special types allow us to restrict command usage to specific contexts, and are also parameterized (like the list type). Most but not all commands are also functions.
The most important command type is the TopLevel
type, indicating a command that can run at the top level (directly at the REPL, or as one of the top level commands in a script file). The print
command has the type {a} a -> TopLevel ()
, where TopLevel ()
means that it is a command that runs in the TopLevel
context and returns a value of type ()
(that is, no useful information). In other words, it has a side effect (printing some text to the screen) but doesn’t produce any information to use in the rest of the SAWScript program. This is the primary usage of the ()
type.
It can sometimes be useful to bind a sequence of commands together. This can be accomplished with the do { ... }
construct. For example:
The bound value, print_two
, has type TopLevel ()
, since that is the type of its last command.
Note that in the previous example the printing doesn’t occur until print_two
directly appears at the REPL. The let
expression does not cause those commands to run. The construct that runs a command is written using the <-
operator. This operator works like let
except that it says to run the command listed on the right hand side and bind the result, rather than binding the variable to the command itself. Using <-
instead of let
in the previous example yields:
Here, the print
commands run first, and then print_two
gets the value returned by the second print
command, namely ()
. Any command run without using <-
at either the top level of a script or within a do
block discards its result. However, the REPL prints the result of any command run without using the <-
operator.
In some cases it can be useful to have more control over the value returned by a do
block. The return
command allows us to do this. For example, say we wanted to write a function that would print a message before and after running some arbitrary command and then return the result of that command. We could write:
If we put this script in run.saw
and run it with saw
, we get something like:
Note that it ran the first print
command, then the caller-specified command, then the second print
command. The result stored in x
at the end is the result of the return
command passed in as an argument.
Other Basic Functions
Aside from the functions we have listed so far, there are a number of other operations for working with basic data structures and interacting with the operating system.
The following functions work on lists:
concat : {a} [a] -> [a] -> [a]
takes two lists and returns the concatenation of the two.head : {a} [a] -> a
returns the first element of a list.tail : {a} [a] -> [a]
returns everything except the first element.length : {a} [a] -> Int
counts the number of elements in a list.null : {a} [a] -> Bool
indicates whether a list is empty (has zero elements).nth : {a} [a] -> Int -> a
returns the element at the given position, withnth l 0
being equivalent tohead l
.for : {m, a, b} [a] -> (a -> m b) -> m [b]
takes a list and a function that runs in some command context. The passed command will be called once for every element of the list, in order. Returns a list of all of the results produced by the command.
For interacting with the operating system, we have:
get_opt : Int -> String
returns the command-line argument tosaw
at the given index. Argument 0 is always the name of thesaw
executable itself, and higher indices represent later arguments.exec : String -> [String] -> String -> TopLevel String
runs an external program given, respectively, an executable name, a list of arguments, and a string to send to the standard input of the program. Theexec
command returns the standard output from the program it executes and prints standard error to the screen.exit : Int -> TopLevel ()
stops execution of the current script and returns the given exit code to the operating system.
Finally, there are a few miscellaneous functions and commands:
show : {a} a -> String
computes the textual representation of its argument in the same way asprint
, but instead of displaying the value it returns it as aString
value for later use in the program. This can be useful for constructing more detailed messages later.str_concat : String -> String -> String
concatenates twoString
values, and can also be useful withshow
.time : {a} TopLevel a -> TopLevel a
runs any otherTopLevel
command and prints out the time it took to execute.with_time : {a} TopLevel a -> TopLevel (Int, a)
returns both the original result of the timed command and the time taken to execute it (in milliseconds), without printing anything in the process.
The Term Type
Perhaps the most important type in SAWScript, and the one most unlike the built-in types of most other languages, is the Term
type. Essentially, a value of type Term
precisely describes all possible computations performed by some program. In particular, if two Term
values are equivalent, then the programs that they represent will always compute the same results given the same inputs. We will say more later about exactly what it means for two terms to be equivalent, and how to determine whether two terms are equivalent.
Before exploring the Term
type more deeply, it is important to understand the role of the Cryptol language in SAW.
Cryptol and its Role in SAW
Cryptol is a domain-specific language originally designed for the high-level specification of cryptographic algorithms. It is general enough, however, to describe a wide variety of programs, and is particularly applicable to describing computations that operate on streams of data of some fixed size.
In addition to being integrated into SAW, Cryptol is a standalone language with its own manual:
SAW includes deep support for Cryptol, and in fact requires the use of Cryptol for most non-trivial tasks. To fully understand the rest of this manual and to effectively use SAW, you will need to develop at least a rudimentary understanding of Cryptol.
The primary use of Cryptol within SAWScript is to construct values of type Term
. Although Term
values can be constructed from various sources, inline Cryptol expressions are the most direct and convenient way to create them.
Specifically, a Cryptol expression can be placed inside double curly braces ({{
and }}
), resulting in a value of type Term
. As a very simple example, there is no built-in integer addition operation in SAWScript. However, we can use Cryptol’s built-in integer addition operator within SAWScript as follows:
Although it printed out in the same way as an Int
, it is important to note that t
actually has type Term
. We can see how this term is represented internally, before being evaluated, with the print_term
function.
For the moment, it’s not important to understand what this output means. We show it only to clarify that Term
values have their own internal structure that goes beyond what exists in SAWScript. The internal representation of Term
values is in a language called SAWCore. The full semantics of SAWCore are beyond the scope of this manual.
The text constructed by print_term
can also be accessed programmatically (instead of printing to the screen) using the show_term
function, which returns a String
. The show_term
function is not a command, so it executes directly and does not need <-
to bind its result. Therefore, the following will have the same result as the print_term
command above:
Numbers are printed in decimal notation by default when printing terms, but the following two commands can change that behavior.
set_ascii : Bool -> TopLevel ()
, when passedtrue
, makes subsequentprint_term
orshow_term
commands print sequences of bytes as ASCII strings (and doesn’t affect printing of anything else).set_base : Int -> TopLevel ()
prints all bit vectors in the given base, which can be between 2 and 36 (inclusive).
A Term
that represents an integer (any bit vector, as affected by set_base
) can be translated into a SAWScript Int
using the eval_int : Term -> Int
function. This function returns an Int
if the Term
can be represented as one, and fails at runtime otherwise.
Similarly, values of type Bit
in Cryptol can be translated into values of type Bool
in SAWScript using the eval_bool : Term -> Bool
function:
Anything with sequence type in Cryptol can be translated into a list of Term
values in SAWScript using the eval_list : Term -> [Term]
function.
Finally, a list of Term
values in SAWScript can be collapsed into a single Term
with sequence type using the list_term : [Term] -> Term
function, which is the inverse of eval_list
.
In addition to being able to extract integer and Boolean values from Cryptol expressions, Term
values can be injected into Cryptol expressions. When SAWScript evaluates a Cryptol expression between {{
and }}
delimiters, it does so with several extra bindings in scope:
Any variable in scope that has SAWScript type
Bool
is visible in Cryptol expressions as a value of typeBit
.Any variable in scope that has SAWScript type
Int
is visible in Cryptol expressions as a type variable. Type variables can be demoted to numeric bit vector values using the backtick (`
) operator.Any variable in scope that has SAWScript type
Term
is visible in Cryptol expressions as a value with the Cryptol type corresponding to the internal type of the term. The power of this conversion is that theTerm
does not need to have originally been derived from a Cryptol expression.
In addition to these rules, bindings created at the Cryptol level, either from included files or inside Cryptol quoting brackets, are visible only to later Cryptol expressions, and not as SAWScript variables.
To make these rules more concrete, consider the following examples. If we bind a SAWScript Int
, we can use it as a Cryptol type variable. If we create a Term
variable that internally has function type, we can apply it to an argument within a Cryptol expression, but not at the SAWScript level:
If f
was a binding of a SAWScript variable to a Term
of function type, we would get a different error:
One subtlety of dealing with Term
s constructed from Cryptol is that because the Cryptol expressions themselves are type checked by the Cryptol type checker, and because they may make use of other Term
values already in scope, they are not type checked until the Cryptol brackets are evaluated. So type errors at the Cryptol level may occur at runtime from the SAWScript perspective (though they occur before the Cryptol expressions are run).
So far, we have talked about using Cryptol value expressions. However, SAWScript can also work with Cryptol types. The most direct way to refer to a Cryptol type is to use type brackets: {|
and |}
. Any Cryptol type written between these brackets becomes a Type
value in SAWScript. Some types in Cryptol are numeric (also known as size) types, and correspond to non-negative integers. These can be translated into SAWScript integers with the eval_size
function. For example:
For non-numeric types, eval_size
fails at runtime:
In addition to the use of brackets to write Cryptol expressions inline, several built-in functions can extract Term
values from Cryptol files in other ways. The import
command at the top level imports all top-level definitions from a Cryptol file and places them in scope within later bracketed expressions. This includes Cryptol foreign
declarations. If a Cryptol implementation of a foreign function is present, then it will be used as the definition when reasoning about the function. Otherwise, the function will be imported as an opaque constant with no definition.
The cryptol_load
command behaves similarly, but returns a CryptolModule
instead. If any CryptolModule
is in scope, its contents are available qualified with the name of the CryptolModule
variable. A specific definition can be explicitly extracted from a CryptolModule
using the cryptol_extract
command:
cryptol_extract : CryptolModule -> String -> TopLevel Term
Transforming Term Values
The three primary functions of SAW are extracting models (Term
values) from programs, transforming those models, and proving properties about models using external provers. So far we’ve shown how to construct Term
values from Cryptol programs; later sections will describe how to extract them from other programs. Now we show how to use the various term transformation features available in SAW.
Rewriting
Rewriting a Term
consists of applying one or more rewrite rules to it, resulting in a new Term
. A rewrite rule in SAW can be specified in multiple ways:
as the definition of a function that can be unfolded,
as a term of Boolean type (or a function returning a Boolean) that is an equality statement, and
as a term of equality type with a body that encodes a proof that the equality in the type is valid.
In each case the term logically consists of two sides and describes a way to transform the left side into the right side. Each side may contain variables (bound by enclosing lambda expressions) and is therefore a pattern which can match any term in which each variable represents an arbitrary sub-term. The left-hand pattern describes a term to match (which may be a sub-term of the full term being rewritten), and the right-hand pattern describes a term to replace it with. Any variable in the right-hand pattern must also appear in the left-hand pattern and will be instantiated with whatever sub-term matched that variable in the original term.
For example, say we have the following Cryptol function:
We might for some reason want to replace multiplication by a power of two with a shift. We can describe this replacement using an equality statement in Cryptol (a rule of form 2 above):
Interpreting this as a rewrite rule, it says that for any 8-bit vector (call it y
for now), we can replace y * 2
with y << 1
. Using this rule to rewrite the earlier expression would then yield:
The general philosophy of rewriting is that the left and right patterns, while syntactically different, should be semantically equivalent. Therefore, applying a set of rewrite rules should not change the fundamental meaning of the term being rewritten. SAW is particularly focused on the task of proving that some logical statement expressed as a Term
is always true. If that is in fact the case, then the entire term can be replaced by the term True
without changing its meaning. The rewriting process can in some cases, by repeatedly applying rules that themselves are known to be valid, reduce a complex term entirely to True
, which constitutes a proof of the original statement. In other cases, rewriting can simplify terms before sending them to external automated provers that can then finish the job. Sometimes this simplification can help the automated provers run more quickly, and sometimes it can help them prove things they would otherwise be unable to prove by applying reasoning steps (rewrite rules) that are not available to the automated provers.
In practical use, rewrite rules can be aggregated into Simpset
values in SAWScript. A few pre-defined Simpset
values exist:
empty_ss : Simpset
is the empty set of rules. Rewriting with it should have no effect, but it is useful as an argument to some of the functions that construct largerSimpset
values.basic_ss : Simpset
is a collection of rules that are useful in most proof scripts.cryptol_ss : () -> Simpset
includes a collection of Cryptol-specific rules. Some of these simplify away the abstractions introduced in the translation from Cryptol to SAWCore, which can be useful when proving equivalence between Cryptol and non-Cryptol code. Leaving these abstractions in place is appropriate when comparing only Cryptol code, however, socryptol_ss
is not included inbasic_ss
.
The next set of functions can extend or apply a Simpset
:
addsimp' : Term -> Simpset -> Simpset
adds a singleTerm
to an existing `Simpset.addsimps' : [Term] -> Simpset -> Simpset
adds a list ofTerm
s to an existingSimpset
.rewrite : Simpset -> Term -> Term
applies aSimpset
to an existingTerm
to produce a newTerm
.
To make this more concrete, we examine how the rewriting example sketched above, to convert multiplication into shift, can work in practice. We simplify everything with cryptol_ss
as we go along so that the Term
s don’t get too cluttered. First, we declare the term to be transformed:
Next, we declare the rewrite rule:
Finally, we apply the rule to the target term:
Note that addsimp'
and addsimps'
take a Term
or list of Term
s; these could in principle be anything, and are not necessarily terms representing logically valid equalities. They have '
suffixes because they are not intended to be the primary interface to rewriting. When using these functions, the soundness of the proof process depends on the correctness of these rules as a side condition.
The primary interface to rewriting uses the Theorem
type instead of the Term
type, as shown in the signatures for addsimp
and addsimps
.
addsimp : Theorem -> Simpset -> Simpset
adds a singleTheorem
to aSimpset
.addsimps : [Theorem] -> Simpset -> Simpset
adds severalTheorem
values to aSimpset
.
A Theorem
is essentially a Term
that is proven correct in some way. In general, a Theorem
can be any statement, and may not be useful as a rewrite rule. However, if it has an appropriate shape it can be used for rewriting. In the “Proofs about Terms” section, we’ll describe how to construct Theorem
values from Term
values.
In the absence of user-constructed Theorem
values, there are some additional built-in rules that are not included in either basic_ss
and cryptol_ss
because they are not always beneficial, but that can sometimes be helpful or essential. The cryptol_ss
simpset includes rewrite rules to unfold all definitions in the Cryptol
SAWCore module, but does not include any of the terms of equality type.
add_cryptol_defs :
[String] -> Simpset -> Simpsetadds unfolding rules for functions with the given names from the SAWCore
Cryptolmodule to the given
Simpset`.add_cryptol_eqs : [String] -> Simpset -> Simpset
adds the terms of equality type with the given names from the SAWCoreCryptol
module to the givenSimpset
.add_prelude_defs : [String] -> Simpset -> Simpset
adds unfolding rules from the SAWCorePrelude
module to aSimpset
.add_prelude_eqs : [String] -> Simpset -> Simpset
adds equality-typed terms from the SAWCorePrelude
module to aSimpset
.
Finally, it’s possible to construct a theorem from an arbitrary SAWCore expression (rather than a Cryptol expression), using the core_axiom
function.
core_axiom : String -> Theorem
creates aTheorem
from aString
in SAWCore syntax. AnyTheorem
introduced by this function is assumed to be correct, so use it with caution.
Folding and Unfolding
A SAWCore term can be given a name using the define
function, and is then by default printed as that name alone. A named subterm can be “unfolded” so that the original definition appears again.
define : String -> Term -> TopLevel Term
unfold_term : [String] -> Term -> Term
For example:
This process of folding and unfolding is useful both to make large terms easier for humans to work with and to make automated proofs more tractable. We’ll describe the latter in more detail when we discuss interacting with external provers.
In some cases, folding happens automatically when constructing Cryptol expressions. Consider the following example:
This illustrates that a bare expression in Cryptol braces gets translated directly to a SAWCore term. However, a Cryptol definition gets translated into a folded SAWCore term. In addition, because the second definition of t
occurs at the Cryptol level, rather than the SAWScript level, it is visible only inside Cryptol braces. Definitions imported from Cryptol source files are also initially folded and can be unfolded as needed.
Other Built-in Transformation and Inspection Functions
In addition to the Term
transformation functions described so far, a variety of others also exist.
beta_reduce_term : Term -> Term
takes any sub-expression of the form(\x -> t) v
in the givenTerm
and replaces it with a transformed version oft
in which all instances ofx
are replaced byv
.replace : Term -> Term -> Term -> TopLevel Term
replaces arbitrary subterms. A call toreplace x y t
replaces any instance ofx
insidet
withy
.
Assessing the size of a term can be particularly useful during benchmarking. SAWScript provides two mechanisms for this.
term_size : Term -> Int
calculates the number of nodes in the Directed Acyclic Graph (DAG) representation of aTerm
used internally by SAW. This is the most appropriate way of determining the resource use of a particular term.term_tree_size : Term -> Int
calculates how large aTerm
would be if it were represented by a tree instead of a DAG. This can, in general, be much, much larger than the number returned byterm_size
, and serves primarily as a way of assessing, for a specific term, how much benefit there is to the term sharing used by the DAG representation.
Finally, there are a few commands related to the internal SAWCore type of a Term
.
check_term : Term -> TopLevel ()
checks that the internal structure of aTerm
is well-formed and that it passes all of the rules of the SAWCore type checker.type : Term -> Type
returns the type of a particularTerm
, which can then be used to, for example, construct a new fresh variable withfresh_symbolic
.
Loading and Storing Terms
Most frequently, Term
values in SAWScript come from Cryptol, JVM, or LLVM programs, or some transformation thereof. However, it is also possible to obtain them from various other sources.
parse_core : String -> Term
parses aString
containing a term in SAWCore syntax, returning aTerm
.read_core : String -> TopLevel Term
is likeparse_core
, but obtains the text from the given file and expects it to be in the simpler SAWCore external representation format, rather than the human-readable syntax shown so far.read_aig : String -> TopLevel Term
returns aTerm
representation of an And-Inverter-Graph (AIG) file in AIGER format.read_bytes : String -> TopLevel Term
reads a constant sequence of bytes from a file and represents it as aTerm
. Its result will always have Cryptol type[n][8]
for somen
.
It is also possible to write Term
values into files in various formats, including: AIGER (write_aig
), CNF (write_cnf
), SAWCore external representation (write_core
), and SMT-Lib version 2 (write_smtlib2
).
write_aig : String -> Term -> TopLevel ()
write_cnf : String -> Term -> TopLevel ()
write_core : String -> Term -> TopLevel ()
write_smtlib2 : String -> Term -> TopLevel ()
Proofs about Terms
The goal of SAW is to facilitate proofs about the behavior of programs. It may be useful to prove some small fact to use as a rewrite rule in later proofs, but ultimately these rewrite rules come together into a proof of some higher-level property about a software system.
Whether proving small lemmas (in the form of rewrite rules) or a top-level theorem, the process builds on the idea of a proof script that is run by one of the top level proof commands.
prove_print : ProofScript () -> Term -> TopLevel Theorem
takes a proof script (which we’ll describe next) and aTerm
. TheTerm
should be of function type with a return value ofBool
(Bit
at the Cryptol level). It will then use the proof script to attempt to show that theTerm
returnsTrue
for all possible inputs. If it is successful, it will printValid
and return aTheorem
. If not, it will abort.sat_print : ProofScript () -> Term -> TopLevel ()
is similar except that it looks for a single value for which theTerm
evaluates toTrue
and prints out that value, returning nothing.prove_core : ProofScript () -> String -> TopLevel Theorem
proves and returns aTheorem
from a string in SAWCore syntax.
Automated Tactics
The simplest proof scripts just specify the automated prover to use. The ProofScript
values abc
and z3
select the ABC and Z3 theorem provers, respectively, and are typically good choices.
For example, combining prove_print
with abc
:
Similarly, sat_print
will show that the function returns True
for one specific input (which it should, since we already know it returns True
for all inputs):
In addition to these, the boolector
, cvc4
, cvc5
, mathsat
, and yices
provers are available. The internal decision procedure rme
, short for Reed-Muller Expansion, is an automated prover that works particularly well on the Galois field operations that show up, for example, in AES.
In more complex cases, some pre-processing can be helpful or necessary before handing the problem off to an automated prover. The pre-processing can involve rewriting, beta reduction, unfolding, the use of provers that require slightly more configuration, or the use of provers that do very little real work.
Proof Script Diagnostics
During development of a proof, it can be useful to print various information about the current goal. The following tactics are useful in that context.
print_goal : ProofScript ()
prints the entire goal in SAWCore syntax.print_goal_consts : ProofScript ()
prints a list of unfoldable constants in the current goal.print_goal_depth : Int -> ProofScript ()
takes an integer argument,n
, and prints the goal up to depthn
. Any elided subterms are printed with a...
notation.print_goal_size : ProofScript ()
prints the number of nodes in the DAG representation of the goal.
Rewriting in Proof Scripts
One of the key techniques available for completing proofs in SAWScript is the use of rewriting or transformation. The following commands support this approach.
simplify : Simpset -> ProofScript ()
works just likerewrite
, except that it works in aProofScript
context and implicitly transforms the current (unnamed) goal rather than taking aTerm
as a parameter.goal_eval : ProofScript ()
will evaluate the current proof goal to a first-order combination of primitives.goal_eval_unint : [String] -> ProofScript ()
works likegoal_eval
but avoids expanding or simplifying the given names.
Other Transformations
Some useful transformations are not easily specified using equality statements, and instead have special tactics.
beta_reduce_goal : ProofScript ()
works likebeta_reduce_term
but on the current goal. It takes any sub-expression of the form(\x -> t) v
and replaces it with a transformed version oft
in which all instances ofx
are replaced byv
.unfolding : [String] -> ProofScript ()
works likeunfold_term
but on the current goal.
Using unfolding
is mostly valuable for proofs based entirely on rewriting, since the default behavior for automated provers is to unfold everything before sending a goal to a prover. However, with some provers it is possible to indicate that specific named subterms should be represented as uninterpreted functions.
unint_cvc4 : [String] -> ProofScript ()
unint_cvc5 : [String] -> ProofScript ()
unint_yices : [String] -> ProofScript ()
unint_z3 : [String] -> ProofScript ()
The list of String
arguments in these cases indicates the names of the subterms to leave folded, and therefore present as uninterpreted functions to the prover. To determine which folded constants appear in a goal, use the print_goal_consts
function described above.
Ultimately, we plan to implement a more generic tactic that leaves certain constants uninterpreted in whatever prover is ultimately used (provided that uninterpreted functions are expressible in the prover).
Note that each of the unint_*
tactics have variants that are prefixed with sbv_
and w4_
. The sbv_
-prefixed tactics make use of the SBV library to represent and solve SMT queries:
sbv_unint_cvc4 : [String] -> ProofScript ()
sbv_unint_cvc5 : [String] -> ProofScript ()
sbv_unint_yices : [String] -> ProofScript ()
sbv_unint_z3 : [String] -> ProofScript ()
The w4_
-prefixed tactics make use of the What4 library instead of SBV:
w4_unint_cvc4 : [String] -> ProofScript ()
w4_unint_cvc5 : [String] -> ProofScript ()
w4_unint_yices : [String] -> ProofScript ()
w4_unint_z3 : [String] -> ProofScript ()
In most specifications, the choice of SBV versus What4 is not important, as both libraries are broadly compatible in terms of functionality. There are some situations where one library may outpeform the other, however, due to differences in how each library represents certain SMT queries. There are also some experimental features that are only supported with What4 at the moment, such as enable_lax_loads_and_stores
.
Caching Solver Results
SAW has the capability to cache the results of tactics which call out to automated provers. This can save a considerable amount of time in cases such as proof development and CI, where the same proof scripts are often run repeatedly without changes.
This caching is available for all tactics which call out to automated provers at runtime: abc
, boolector
, cvc4
, cvc5
, mathsat
, yices
, z3
, rme
, and the family of unint
tactics described in the previous section.
When solver caching is enabled and one of the tactics mentioned above is encountered, if there is already an entry in the cache corresponding to the call then the cached result is used, otherwise the appropriate solver is queried, and the result saved to the cache. Entries are indexed by a SHA256 hash of the exact query to the solver (ignoring variable names), any options passed to the solver, and the names and full version strings of all the solver backends involved (e.g. ABC and SBV for the abc
tactic). This ensures cached results are only used when they would be identical to the result of actually running the tactic.
The simplest way to enable solver caching is to set the environment variable SAW_SOLVER_CACHE_PATH
. With this environment variable set, saw
and saw-remote-api
will automatically keep an LMDB database at the given path containing the solver result cache. Setting this environment variable globally therefore creates a global, concurrency-safe solver result cache used by all newly created saw
or saw-remote-api
processes. Note that when this environment variable is set, SAW does not create a cache at the specified path until it is actually needed.
There are also a number of SAW commands related to solver caching.
set_solver_cache_path
is like settingSAW_SOLVER_CACHE_PATH
for the remainder of the current session, but opens an LMDB database at the specified path immediately. If a cache is already in use in the current session (i.e. through a prior call toset_solver_cache_path
or throughSAW_SOLVER_CACHE_PATH
being set and the cache being used at least once) then all entries in the cache already in use will be copied to the new cache being opened.clean_mismatched_versions_solver_cache
will remove all entries in the solver result cache which were created using solver backend versions which do not match the versions in the current environment. This can be run after an update to clear out any old, unusable entries from the solver cache. This command can also be run directly from the command line through the--clean-mismatched-versions-solver-cache
command-line option.print_solver_cache
prints to the console all entries in the cache whose SHA256 hash keys start with the given hex string. Providing an empty string results in all entries in the cache being printed.print_solver_cache_stats
prints to the console statistics including the size of the solver cache, where on disk it is stored, and some counts of how often it has been used during the current session.
For performing more complicated database operations on the set of cached results, the file solver_cache.py
is provided with the Python bindings of the SAW Remote API. This file implements a general-purpose Python interface for interacting with the LMDB databases kept by SAW for solver caching.
Below is an example of using solver caching with saw -v Debug
. Only the relevant output is shown, the rest abbreviated with “…”.
Other External Provers
In addition to the built-in automated provers already discussed, SAW supports more generic interfaces to other arbitrary theorem provers supporting specific interfaces.
external_aig_solver : String -> [String] -> ProofScript ()
supports theorem provers that can take input as a single-output AIGER file. The first argument is the name of the executable to run. The second argument is the list of command-line parameters to pass to that executable. Any element of this list equal to"%f"
will be replaced with the name of the temporary AIGER file generated for the proof goal. The output from the solver is expected to be in DIMACS solution format.external_cnf_solver : String -> [String] -> ProofScript ()
works similarly but for SAT solvers that take input in DIMACS CNF format and produce output in DIMACS solution format.
Offline Provers
For provers that must be invoked in more complex ways, or to defer proof until a later time, there are functions to write the current goal to a file in various formats, and then assume that the goal is valid through the rest of the script.
offline_aig : String -> ProofScript ()
offline_cnf : String -> ProofScript ()
offline_extcore : String -> ProofScript ()
offline_smtlib2 : String -> ProofScript ()
offline_unint_smtlib2 : [String] -> String -> ProofScript ()
These support the AIGER, DIMACS CNF, shared SAWCore, and SMT-Lib v2 formats, respectively. The shared representation for SAWCore is described in the saw-script
repository. The offline_unint_smtlib2
command represents the folded subterms listed in its first argument as uninterpreted functions.
Finishing Proofs without External Solvers
Some proofs can be completed using unsound placeholders, or using techniques that do not require significant computation.
assume_unsat : ProofScript ()
indicates that the current goal should be assumed to be unsatisfiable. This is an alias forassume_valid
. Users should prefer to useadmit
instead.assume_valid : ProofScript ()
indicates that the current goal should be assumed to be valid. Users should prefer to useadmit
insteadadmit : String -> ProofScript ()
indicates that the current goal should be assumed to be valid without proof. The given string should be used to record why the user has decided to assume this proof goal.quickcheck : Int -> ProofScript ()
runs the goal on the given number of random inputs, and succeeds if the result of evaluation is alwaysTrue
. This is unsound, but can be helpful during proof development, or as a way to provide some evidence for the validity of a specification believed to be true but difficult or infeasible to prove.trivial : ProofScript ()
states that the current goal should be trivially true. This tactic recognizes instances of equality that can be demonstrated by conversion alone. In particular it is able to proveEqTrue x
goals wherex
reduces to the constant valueTrue
. It fails if this is not the case.
Multiple Goals
The proof scripts shown so far all have a single implicit goal. As in many other interactive provers, however, SAWScript proofs can have multiple goals. The following commands can introduce or work with multiple goals. These are experimental and can be used only after enable_experimental
has been called.
goal_apply : Theorem -> ProofScript ()
will apply a given introduction rule to the current goal. This will result in zero or more new subgoals.goal_assume : ProofScript Theorem
will convert the first hypothesis in the current proof goal into a localTheorem
goal_insert : Theorem -> ProofScript ()
will insert a givenTheorem
as a new hypothesis in the current proof goal.goal_intro : String -> ProofScript Term
will introduce a quantified variable in the current proof goal, returning the variable as aTerm
.goal_when : String -> ProofScript () -> ProofScript ()
will run the given proof script only when the goal name contains the given string.goal_exact : Term -> ProofScript ()
will attempt to use the given term as an exact proof for the current goal. This tactic will succeed whever the type of the given term exactly matches the current goal, and will fail otherwise.split_goal : ProofScript ()
will split a goal of the formPrelude.and prop1 prop2
into two separate goalsprop1
andprop2
.
Proof Failure and Satisfying Assignments
The prove_print
and sat_print
commands print out their essential results (potentially returning a Theorem
in the case of prove_print
). In some cases, though, one may want to act programmatically on the result of a proof rather than displaying it.
The prove
and sat
commands allow this sort of programmatic analysis of proof results. To allow this, they use two types we haven’t mentioned yet: ProofResult
and SatResult
. These are different from the other types in SAWScript because they encode the possibility of two outcomes. In the case of ProofResult
, a statement may be valid or there may be a counter-example. In the case of SatResult
, there may be a satisfying assignment or the statement may be unsatisfiable.
prove : ProofScript SatResult -> Term -> TopLevel ProofResult
sat : ProofScript SatResult -> Term -> TopLevel SatResult
To operate on these new types, SAWScript includes a pair of functions:
caseProofResult : {b} ProofResult -> b -> (Term -> b) -> b
takes aProofResult
, a value to return in the case that the statement is valid, and a function to run on the counter-example, if there is one.caseSatResult : {b} SatResult -> b -> (Term -> b) -> b
has the same shape: it returns its first argument if the result represents an unsatisfiable statement, or its second argument applied to a satisfying assignment if it finds one.
AIG Values and Proofs
Most SAWScript programs operate on Term
values, and in most cases this is the appropriate representation. It is possible, however, to represent the same function that a Term
may represent using a different data structure: an And-Inverter-Graph (AIG). An AIG is a representation of a Boolean function as a circuit composed entirely of AND gates and inverters. Hardware synthesis and verification tools, including the ABC tool that SAW has built in, can do efficient verification and particularly equivalence checking on AIGs.
To take advantage of this capability, a handful of built-in commands can operate on AIGs.
bitblast : Term -> TopLevel AIG
represents aTerm
as anAIG
by “blasting” all of its primitive operations (things like bit-vector addition) down to the level of individual bits.load_aig : String -> TopLevel AIG
loads anAIG
from an external AIGER file.save_aig : String -> AIG -> TopLevel ()
saves anAIG
to an external AIGER file.save_aig_as_cnf : String -> AIG -> TopLevel ()
writes anAIG
out in CNF format for input into a standard SAT solver.
Symbolic Execution
Analysis of Java and LLVM within SAWScript relies heavily on symbolic execution, so some background on how this process works can help with understanding the behavior of the available built-in functions.
At the most abstract level, symbolic execution works like normal program execution except that the values of all variables within the program can be arbitrary expressions, potentially containing free variables, rather than concrete values. Therefore, each symbolic execution corresponds to some set of possible concrete executions.
As a concrete example, consider the following C program that returns the maximum of two values:
If you call this function with two concrete inputs, like this:
then it will assign the value 5
to r
. However, we can also consider what it will do for arbitrary inputs. Consider the following example:
where a
and b
are variables with unknown values. It is still possible to describe the result of the max
function in terms of a
and b
. The following expression describes the value of r
:
where ite
is the “if-then-else” mathematical function, which based on the value of the first argument returns either the second or third. One subtlety of constructing this expression, however, is the treatment of conditionals in the original program. For any concrete values of a
and b
, only one branch of the if
statement will execute. During symbolic execution, on the other hand, it is necessary to execute both branches, track two different program states (each composed of symbolic values), and then merge those states after executing the if
statement. This merging process takes into account the original branch condition and introduces the ite
expression.
A symbolic execution system, then, is very similar to an interpreter that has a different notion of what constitutes a value and executes all paths through the program instead of just one. Therefore, the execution process is similar to that of a normal interpreter, and the process of generating a model for a piece of code is similar to building a test harness for that same code.
More specifically, the setup process for a test harness typically takes the following form:
Initialize or allocate any resources needed by the code. For Java and LLVM code, this typically means allocating memory and setting the initial values of variables.
Execute the code.
Check the desired properties of the system state after the code completes.
Accordingly, three pieces of information are particularly relevant to the symbolic execution process, and are therefore needed as input to the symbolic execution system:
The initial (potentially symbolic) state of the system.
The code to execute.
The final state of the system, and which parts of it are relevant to the properties being tested.
In the following sections, we describe how the Java and LLVM analysis primitives work in the context of these key concepts. We start with the simplest situation, in which the structure of the initial and final states can be directly inferred, and move on to more complex cases that require more information from the user.
Symbolic Termination
Above we described the process of executing multiple branches and merging the results when encountering a conditional statement in the program. When a program contains loops, the branch that chooses to continue or terminate a loop could go either way. Therefore, without a bit more information, the most obvious implementation of symbolic execution would never terminate when executing programs that contain loops.
The solution to this problem is to analyze the branch condition whenever considering multiple branches. If the condition for one branch can never be true in the context of the current symbolic state, there is no reason to execute that branch, and skipping it can make it possible for symbolic execution to terminate.
Directly comparing the branch condition to a constant can sometimes be enough to ensure termination. For example, in simple, bounded loops like the following, comparison with a constant is sufficient.
In this case, the value of i
is always concrete, and will eventually reach the value 10
, at which point the branch corresponding to continuing the loop will be infeasible.
As a more complex example, consider the following function:
The loop in this function can only be determined to symbolically terminate if the analysis takes into account algebraic rules about common multiples. Similarly, it can be difficult to prove that a base case is eventually reached for all inputs to a recursive program.
In this particular case, however, the code is guaranteed to terminate after a fixed number of iterations (where the number of possible iterations is a function of the number of bits in the integers being used). To show that the last iteration is in fact the last possible one, it’s necessary to do more than just compare the branch condition with a constant. Instead, we can use the same proof tools that we use to ultimately analyze the generated models to, early in the process, prove that certain branch conditions can never be true (i.e., are unsatisfiable).
Normally, most of the Java and LLVM analysis commands simply compare branch conditions to the constant True
or False
to determine whether a branch may be feasible. However, each form of analysis allows branch satisfiability checking to be turned on if needed, in which case functions like f
above will terminate.
Next, we examine the details of the specific commands available to analyze JVM and LLVM programs.
Loading Code
The first step in analyzing any code is to load it into the system.
Loading LLVM
To load LLVM code, simply provide the location of a valid bitcode file to the llvm_load_module
function.
llvm_load_module : String -> TopLevel LLVMModule
The resulting LLVMModule
can be passed into the various functions described below to perform analysis of specific LLVM functions.
The LLVM bitcode parser should generally work with LLVM versions between 3.5 and 16.0, though it may be incomplete for some versions. Debug metadata has changed somewhat throughout that version range, so is the most likely case of incompleteness. We aim to support every version after 3.5, however, so report any parsing failures as on GitHub.
Loading Java
Loading Java code is slightly more complex, because of the more structured nature of Java packages. First, when running saw
, three flags control where to look for classes:
The
-b
flag takes the path where thejava
executable lives, which is used to locate the Java standard library classes and add them to the class database. Alternatively, one can put the directory wherejava
lives on thePATH
, which SAW will search if-b
is not set.The
-j
flag takes the name of a JAR file as an argument and adds the contents of that file to the class database.The
-c
flag takes the name of a directory as an argument and adds all class files found in that directory (and its subdirectories) to the class database. By default, the current directory is included in the class path.
Most Java programs will only require setting the -b
flag (or the PATH
), as that is enough to bring in the standard Java libraries. Note that when searching the PATH
, SAW makes assumptions about where the standard library classes live. These assumptions are likely to hold on JDK 7 or later, but they may not hold on older JDKs on certain operating systems. If you are using an old version of the JDK and SAW is unable to find a standard Java class, you may need to specify the location of the standard classes’ JAR file with the -j
flag (or, alternatively, with the SAW_JDK_JAR
environment variable).
Once the class path is configured, you can pass the name of a class to the java_load_class
function.
java_load_class : String -> TopLevel JavaClass
The resulting JavaClass
can be passed into the various functions described below to perform analysis of specific Java methods.
Java class files from any JDK newer than version 6 should work. However, support for JDK 9 and later is experimental. Verifying code that only uses primitive data types is known to work well, but there are some as-of-yet unresolved issues in verifying code involving classes such as String
. For more information on these issues, refer to this GitHub issue.
Loading MIR
To load a piece of Rust code, first compile it to a MIR JSON file, as described in this section, and then provide the location of the JSON file to the mir_load_module
function:
mir_load_module : String -> TopLevel MIRModule
SAW currently supports Rust code that can be built with a January 23, 2023 Rust nightly. If you encounter a Rust feature that SAW does not support, please report it on GitHub.
Notes on Compiling Code for SAW
SAW will generally be able to load arbitrary LLVM bitcode, JVM bytecode, and MIR JSON files, but several guidelines can help make verification easier or more likely to succeed.
Compiling LLVM
For generating LLVM with clang
, it can be helpful to:
Turn on debugging symbols with
-g
so that SAW can find source locations of functions, names of variables, etc.Optimize with
-O1
so that the generated bitcode more closely matches the C/C++ source, making the results more comprehensible.Use
-fno-threadsafe-statics
to preventclang
from emitting unnecessary pthread code.Link all relevant bitcode with
llvm-link
(including, e.g., the C++ standard library when analyzing C++ code).
All SAW proofs include side conditions to rule out undefined behavior, and proofs will only succeed if all of these side conditions have been discharged. However the default SAW notion of undefined behavior is with respect to the semantics of LLVM, rather than C or C++. If you want to rule out undefined behavior according to the C or C++ standards, consider compiling your code with -fsanitize=undefined
or one of the related flags1 to clang
.
Generally, you’ll also want to use -fsanitize-trap=undefined
, or one of the related flags, to cause the compiled code to use llvm.trap
to indicate the presence of undefined behavior. Otherwise, the compiled code will call a separate function, such as __ubsan_handle_shift_out_of_bounds
, for each type of undefined behavior, and SAW currently does not have built in support for these functions (though you could manually create overrides for them in a verification script).
Compiling Java
For Java, the only compilation flag that tends to be valuable is -g
to retain information about the names of function arguments and local variables.
Compiling MIR
In order to verify Rust code, SAW analyzes Rust’s MIR (mid-level intermediate representation) language. In particular, SAW analyzes a particular form of MIR that the mir-json
tool produces. You will need to intall mir-json
and run it on Rust code in order to produce MIR JSON files that SAW can load (see this section).
For cargo
-based projects, mir-json
provides a cargo
subcommand called cargo saw-build
that builds a JSON file suitable for use with SAW. cargo saw-build
integrates directly with cargo
, so you can pass flags to it like any other cargo
subcommand. For example:
Note that:
The full output of
cargo saw-build
here is omitted. The important part is the.linked-mir.json
file that appears afterlinking X mir files into
, as that is the JSON file that must be loaded with SAW.SAW_RUST_LIBRARY_PATH
should point to the the MIR JSON files for the Rust standard library.
mir-json
also supports compiling individual .rs
files through mir-json
’s saw-rustc
command. As the name suggests, it accepts all of the flags that rustc
accepts. For example:
Notes on C++ Analysis
The distance between C++ code and LLVM is greater than between C and LLVM, so some additional considerations come into play when analyzing C++ code with SAW.
The first key issue is that the C++ standard library is large and complex, and tends to be widely used by C++ applications. To analyze most C++ code, it will be necessary to link your code with a version of the libc++
library2 compiled to LLVM bitcode. The wllvm
program can3 be useful for this.
The C++ standard library includes a number of key global variables, and any code that touches them will require that they be initialized using llvm_alloc_global
.
Many C++ names are slightly awkward to deal with in SAW. They may be mangled relative to the text that appears in the C++ source code. SAW currently only understands the mangled names. The llvm-nm
program can be used to show the list of symbols in an LLVM bitcode file, and the c++filt
program can be used to demangle them, which can help in identifying the symbol you want to refer to. In addition, C++ names from namespaces can sometimes include quote marks in their LLVM encoding. For example:
This can be mentioned in SAW by saying:
Finally, there is no support for calling constructors in specifications, so you will need to construct objects piece-by-piece using, e.g., llvm_alloc
and llvm_points_to
.
Direct Extraction
In the case of the max
function described earlier, the relevant inputs and outputs are immediately apparent. The function takes two integer arguments, always uses both of them, and returns a single integer value, making no other changes to the program state.
In cases like this, a direct translation is possible, given only an identification of which code to execute. Two functions exist to handle such simple code. The first, for LLVM is the more stable of the two:
llvm_extract : LLVMModule -> String -> TopLevel Term
A similar function exists for Java, but is more experimental.
jvm_extract : JavaClass -> String -> TopLevel Term
Because of its lack of maturity, it (and later Java-related commands) must be enabled by running the enable_experimental
command beforehand.
enable_experimental : TopLevel ()
The structure of these two extraction functions is essentially identical. The first argument describes where to look for code (in either a Java class or an LLVM module, loaded as described in the previous section). The second argument is the name of the method or function to extract.
When the extraction functions complete, they return a Term
corresponding to the value returned by the function or method as a function of its arguments.
These functions currently work only for code that takes some fixed number of integral parameters, returns an integral result, and does not access any dynamically-allocated memory (although temporary memory allocated during execution is allowed).
Creating Symbolic Variables
The direct extraction process just discussed automatically introduces symbolic variables and then abstracts over them, yielding a SAWScript Term
that reflects the semantics of the original Java, LLVM, or MIR code. For simple functions, this is often the most convenient interface. For more complex code, however, it can be necessary (or more natural) to specifically introduce fresh variables and indicate what portions of the program state they correspond to.
fresh_symbolic : String -> Type -> TopLevel Term
is responsible for creating new variables in this context. The first argument is a name used for pretty-printing of terms and counter-examples. In many cases it makes sense for this to be the same as the name used within SAWScript, as in the following:
However, using the same name is not required.
The second argument to fresh_symbolic
is the type of the fresh variable. Ultimately, this will be a SAWCore type; however, it is usually convenient to specify it using Cryptol syntax with the type quoting brackets {|
and |}
. For example, creating a 32-bit integer, as might be used to represent a Java int
or an LLVM i32
, can be done as follows:
Although symbolic execution works best on symbolic variables, which are “unbound” or “free”, most of the proof infrastructure within SAW uses variables that are bound by an enclosing lambda expression. Given a Term
with free symbolic variables, we can construct a lambda term that binds them in several ways.
abstract_symbolic : Term -> Term
finds all symbolic variables in theTerm
and constructs a lambda expression binding each one, in some order. The result is a function of some number of arguments, one for each symbolic variable. It is the simplest but least flexible way to bind symbolic variables.
If there are multiple symbolic variables in the Term
passed to abstract_symbolic
, the ordering of parameters can be hard to predict. In some cases (such as when a proof is the immediate next step, and it’s expected to succeed) the order isn’t important. In others, it’s nice to have more control over the order.
lambda : Term -> Term -> Term
is the building block for controlled binding. It takes two terms: the one to transform, and the portion of the term to abstract over. Generally, the firstTerm
is one obtained fromfresh_symbolic
and the second is aTerm
that would be passed toabstract_symbolic
.
lambdas : [Term] -> Term -> Term
allows you to list the order in which symbolic variables should be bound. Consider, for example, aTerm
which adds two symbolic variables:
We can turn t
into a function that takes x1
followed by x2
:
Or we can turn t
into a function that takes x2
followed by x1
:
Specification-Based Verification
The built-in functions described so far work by extracting models of code that can then be used for a variety of purposes, including proofs about the properties of the code.
When the goal is to prove equivalence between some LLVM, Java, or MIR code and a specification, however, a more declarative approach is sometimes convenient. The following sections describe an approach that combines model extraction and verification with respect to a specification. A verified specification can then be used as input to future verifications, allowing the proof process to be decomposed.
Running a Verification
Verification of LLVM is controlled by the llvm_verify
command.
The first two arguments specify the module and function name to verify, as with llvm_verify
. The third argument specifies the list of already-verified specifications to use for compositional verification (described later; use []
for now). The fourth argument specifies whether to do path satisfiability checking, and the fifth gives the specification of the function to be verified. Finally, the last argument gives the proof script to use for verification. The result is a proved specification that can be used to simplify verification of functions that call this one.
Similar commands are available for JVM programs:
And for MIR programs:
Running a MIR-based verification
(Note: API functions involving MIR verification require enable_experimental
in order to be used. As such, some parts of this API may change before being finalized.)
The String
supplied as an argument to mir_verify
is expected to be a function identifier. An identifier is expected adhere to one of the following conventions:
<crate name>/<disambiguator>::<function path>
<crate name>::<function path>
Where:
<crate name>
is the name of the crate in which the function is defined. (If you produced your MIR JSON file by compiling a single.rs
file withsaw-rustc
, then the crate name is the same as the name of the file, but without the.rs
file extension.)<disambiguator>
is a hash of the crate and its dependencies. In extreme cases, it is possible for two different crates to have identical crate names, in which case the disambiguator must be used to distinguish between the two crates. In the common case, however, most crate names will correspond to exactly one disambiguator, and you are allowed to leave out the/<disambiguator>
part of theString
in this case. If you supply an identifier with an ambiguous crate name and omit the disambiguator, then SAW will raise an error.<function path>
is the path to the function within the crate. Sometimes, this is as simple as the function name itself. In other cases, a function path may involve multiple segments, depending on the module hierarchy for the program being verified. For instance, aread
function located incore/src/ptr/mod.rs
will have the identifier:Where
core
is the crate name andptr::read
is the function path, which has two segmentsptr
andread
. There are also some special forms of segments that appear for functions defined in certain language constructs. For instance, if a function is defined in animpl
block, then it will have{impl}
as one of its segments, e.g.,If you are in doubt about what the full identifier for a given function is, consult the MIR JSON file for your program.
Now we describe how to construct a value of type LLVMSetup ()
, JVMSetup ()
, or MIRSetup ()
.
Structure of a Specification
A specifications for Crucible consists of three logical components:
A specification of the initial state before execution of the function.
A description of how to call the function within that state.
A specification of the expected final value of the program state.
These three portions of the specification are written in sequence within a do
block of type {LLVM,JVM,MIR}Setup
. The command {llvm,jvm,mir}_execute_func
separates the specification of the initial state from the specification of the final state, and specifies the arguments to the function in terms of the initial state. Most of the commands available for state description will work either before or after {llvm,jvm,mir}_execute_func
, though with slightly different meaning, as described below.
Creating Fresh Variables
In any case where you want to prove a property of a function for an entire class of inputs (perhaps all inputs) rather than concrete values, the initial values of at least some elements of the program state must contain fresh variables. These are created in a specification with the {llvm,jvm,mir}_fresh_var
commands rather than fresh_symbolic
.
llvm_fresh_var : String -> LLVMType -> LLVMSetup Term
jvm_fresh_var : String -> JavaType -> JVMSetup Term
mir_fresh_var : String -> MIRType -> MIRSetup Term
The first parameter to both functions is a name, used only for presentation. It’s possible (though not recommended) to create multiple variables with the same name, but SAW will distinguish between them internally. The second parameter is the LLVM, Java, or MIR type of the variable. The resulting Term
can be used in various subsequent commands.
Note that the second parameter to {llvm,jvm,mir}_fresh_var
must be a type that has a counterpart in Cryptol. (For more information on this, refer to the “Cryptol type correspondence” section.) If the type does not have a Cryptol counterpart, the function will raise an error. If you do need to create a fresh value of a type that cannot be represented in Cryptol, consider using a function such as llvm_fresh_expanded_val
(for LLVM verification) or mir_fresh_expanded_value
(for MIR verification).
LLVM types are built with this set of functions:
llvm_int : Int -> LLVMType
llvm_alias : String -> LLVMType
llvm_array : Int -> LLVMType -> LLVMType
llvm_float : LLVMType
llvm_double : LLVMType
llvm_packed_struct : [LLVMType] -> LLVMType
llvm_struct : [LLVMType] -> LLVMType
Java types are built up using the following functions:
java_bool : JavaType
java_byte : JavaType
java_char : JavaType
java_short : JavaType
java_int : JavaType
java_long : JavaType
java_float : JavaType
java_double : JavaType
java_class : String -> JavaType
java_array : Int -> JavaType -> JavaType
MIR types are built up using the following functions:
mir_adt : MIRAdt -> MIRType
mir_array : Int -> MIRType -> MIRType
mir_bool : MIRType
mir_char : MIRType
mir_i8 : MIRType
mir_i6 : MIRType
mir_i32 : MIRType
mir_i64 : MIRType
mir_i128 : MIRType
mir_isize : MIRType
mir_f32 : MIRType
mir_f64 : MIRType
mir_lifetime : MIRType
mir_ref : MIRType -> MIRType
mir_ref_mut : MIRType -> MIRType
mir_slice : MIRType -> MIRType
mir_str : MIRType
mir_tuple : [MIRType] -> MIRType
mir_u8 : MIRType
mir_u6 : MIRType
mir_u32 : MIRType
mir_u64 : MIRType
mir_u128 : MIRType
mir_usize : MIRType
Most of these types are straightforward mappings to the standard LLVM and Java types. The one key difference is that arrays must have a fixed, concrete size. Therefore, all analysis results are valid only under the assumption that any arrays have the specific size indicated, and may not hold for other sizes.
The llvm_int
function takes an Int
parameter indicating the variable’s bit width. For example, the C uint16_t
and int16_t
types correspond to llvm_int 16
. The C bool
type is slightly trickier. A bare bool
type typically corresponds to llvm_int 1
, but if a bool
is a member of a composite type such as a pointer, array, or struct, then it corresponds to llvm_int 8
. This is due to a peculiarity in the way Clang compiles bool
down to LLVM. When in doubt about how a bool
is represented, check the LLVM bitcode by compiling your code with clang -S -emit-llvm
.
LLVM types can also be specified in LLVM syntax directly by using the llvm_type
function.
llvm_type : String -> LLVMType
For example, llvm_type "i32"
yields the same result as llvm_int 32
.
The most common use for creating fresh variables is to state that a particular function should have the specified behaviour for arbitrary initial values of the variables in question. Sometimes, however, it can be useful to specify that a function returns (or stores, more about this later) an arbitrary value, without specifying what that value should be. To express such a pattern, you can also run llvm_fresh_var
from the post state (i.e., after llvm_execute_func
).
The SetupValue and JVMValue Types
Many specifications require reasoning about both pure values and about the configuration of the heap. The SetupValue
type corresponds to values that can occur during symbolic execution, which includes both Term
values, pointers, and composite types consisting of either of these (both structures and arrays).
The llvm_term
, jvm_term
, and mir_term
functions create a SetupValue
, JVMValue
, or MIRValue
, respectively, from a Term
:
llvm_term : Term -> SetupValue
jvm_term : Term -> JVMValue
mir_term : Term -> MIRValue
The value that these functions return will have an LLVM, JVM, or MIR type corresponding to the Cryptol type of the Term
argument. (For more information on this, refer to the “Cryptol type correspondence” section.) If the type does not have a Cryptol counterpart, the function will raise an error.
Cryptol type correspondence
The {llvm,jvm,mir}_fresh_var
functions take an LLVM, JVM, or MIR type as an argument and produces a Term
variable of the corresponding Cryptol type as output. Similarly, the {llvm,jvm,mir}_term
functions take a Cryptol Term
as input and produce a value of the corresponding LLVM, JVM, or MIR type as output. This section describes precisely which types can be converted to Cryptol types (and vice versa) in this way.
LLVM verification
The following LLVM types correspond to Cryptol types:
llvm_alias <name>
: Corresponds to the same Cryptol type as the type used in the definition of<name>
.llvm_array <n> <ty>
: Corresponds to the Cryptol sequence[<n>][<cty>]
, where<cty>
is the Cryptol type corresponding to<ty>
.llvm_int <n>
: Corresponds to the Cryptol word[<n>]
.llvm_struct [<ty_1>, ..., <ty_n>]
andllvm_packed_struct [<ty_1>, ..., <ty_n>]
: Corresponds to the Cryptol tuple(<cty_1>, ..., <cty_n>)
, where<cty_i>
is the Cryptol type corresponding to<ty_i>
for eachi
ranging from1
ton
.
The following LLVM types do not correspond to Cryptol types:
llvm_double
llvm_float
llvm_pointer
JVM verification
The following Java types correspond to Cryptol types:
java_array <n> <ty>
: Corresponds to the Cryptol sequence[<n>][<cty>]
, where<cty>
is the Cryptol type corresponding to<ty>
.java_bool
: Corresponds to the CryptolBit
type.java_byte
: Corresponds to the Cryptol[8]
type.java_char
: Corresponds to the Cryptol[16]
type.java_int
: Corresponds to the Cryptol[32]
type.java_long
: Corresponds to the Cryptol[64]
type.java_short
: Corresponds to the Cryptol[16]
type.
The following Java types do not correspond to Cryptol types:
java_class
java_double
java_float
MIR verification
The following MIR types correspond to Cryptol types:
mir_array <n> <ty>
: Corresponds to the Cryptol sequence[<n>][<cty>]
, where<cty>
is the Cryptol type corresponding to<ty>
.mir_bool
: Corresponds to the CryptolBit
type.mir_char
: Corresponds to the Cryptol[32]
type.mir_i8
andmir_u8
: Corresponds to the Cryptol[8]
type.mir_i16
andmir_u16
: Corresponds to the Cryptol[16]
type.mir_i32
andmir_u32
: Corresponds to the Cryptol[32]
type.mir_i64
andmir_u64
: Corresponds to the Cryptol[64]
type.mir_i128
andmir_u128
: Corresponds to the Cryptol[128]
type.mir_isize
andmir_usize
: Corresponds to the Cryptol[32]
type.mir_tuple [<ty_1>, ..., <ty_n>]
: Corresponds to the Cryptol tuple(<cty_1>, ..., <cty_n>)
, where<cty_i>
is the Cryptol type corresponding to<ty_i>
for eachi
ranging from1
ton
.
The following MIR types do not correspond to Cryptol types:
mir_adt
mir_f32
mir_f64
mir_ref
andmir_ref_mut
mir_slice
mir_str
Executing
Once the initial state has been configured, the {llvm,jvm,mir}_execute_func
command specifies the parameters of the function being analyzed in terms of the state elements already configured.
llvm_execute_func : [SetupValue] -> LLVMSetup ()
jvm_execute_func : [JVMValue] -> JVMSetup ()
mir_execute_func : [MIRValue] -> MIRSetup ()
Return Values
To specify the value that should be returned by the function being verified use the {llvm,jvm,mir}_return
command.
llvm_return : SetupValue -> LLVMSetup ()
jvm_return : JVMValue -> JVMSetup ()
mir_return : MIRValue -> MIRSetup ()
A First Simple Example
The commands introuduced so far are sufficient to verify simple programs that do not use pointers (or that use them only internally). Consider, for instance the C program that adds its two arguments together:
We can specify this function’s expected behavior as follows:
We can then compile the C file add.c
into the bitcode file add.bc
and verify it with ABC:
Compositional Verification
The primary advantage of the specification-based approach to verification is that it allows for compositional reasoning. That is, when proving properties of a given method or function, we can make use of properties we have already proved about its callees rather than analyzing them anew. This enables us to reason about much larger and more complex systems than otherwise possible.
The llvm_verify
, jvm_verify
, and mir_verify
functions return values of type CrucibleMethodSpec
, JVMMethodSpec
, and MIRMethodSpec
, respectively. These values are opaque objects that internally contain both the information provided in the associated LLVMSetup
, JVMSetup
, or MIRSetup
blocks, respectively, and the results of the verification process.
Any of these MethodSpec
objects can be passed in via the third argument of the ..._verify
functions. For any function or method specified by one of these parameters, the simulator will not follow calls to the associated target. Instead, it will perform the following steps:
Check that all
llvm_points_to
andllvm_precond
statements (or the corresponding JVM or MIR statements) in the specification are satisfied.Update the simulator state and optionally construct a return value as described in the specification.
More concretely, building on the previous example, say we have a doubling function written in terms of add
:
It has a similar specification to add
:
And we can verify it using what we’ve already proved about add
:
In this case, doing the verification compositionally doesn’t save computational effort, since the functions are so simple, but it illustrates the approach.
Compositional Verification and Mutable Allocations
A common pitfall when using compositional verification is to reuse a specification that underspecifies the value of a mutable allocation. In general, doing so can lead to unsound verification, so SAW goes through great lengths to check for this.
Here is an example of this pitfall in an LLVM verification. Given this C code:
And the following SAW specifications:
Should SAW be able to verify the foo
function against foo_spec
using compositional verification? That is, should the following be expected to work?
A literal reading of side_effect_spec
would suggest that the side_effect
function allocates a_ptr
but then does nothing with it, implying that foo
returns its argument unchanged. This is incorrect, however, as the side_effect
function actually changes its argument to point to 0
, so the foo
function ought to return 0
as a result. SAW should not verify foo
against foo_spec
, and indeed it does not.
The problem is that side_effect_spec
underspecifies the value of a_ptr
in its postconditions, which can lead to the potential unsoundness seen above when side_effect_spec
is used in compositional verification. To prevent this source of unsoundness, SAW will invalidate the underlying memory of any mutable pointers (i.e., those declared with llvm_alloc
, not llvm_alloc_global
) allocated in the preconditions of compositional override that do not have a corresponding llvm_points_to
statement in the postconditions. Attempting to read from invalidated memory constitutes an error, as can be seen in this portion of the error message when attempting to verify foo
against foo_spec
:
To fix this particular issue, add an llvm_points_to
statement to side_effect_spec
:
After making this change, SAW will reject foo_spec
for a different reason, as it claims that foo
returns its argument unchanged when it actually returns 0
.
Note that invalidating memory itself does not constitute an error, so if the foo
function never read the value of b
after calling side_effect(&b)
, then there would be no issue. It is only when a function attempts to read from invalidated memory that an error is thrown. In general, it can be difficult to predict when a function will or will not read from invalidated memory, however. For this reason, it is recommended to always specify the values of mutable allocations in the postconditions of your specs, as it can avoid pitfalls like the one above.
The same pitfalls apply to compositional MIR verification, with a couple of key differences. In MIR verification, mutable references are allocated using mir_alloc_mut
. Here is a Rust version of the pitfall program above:
Just like above, if you attempted to prove foo
against foo_spec
using compositional verification:
Then SAW would throw an error, as side_effect_spec
underspecifies the value of a_ref
in its postconditions. side_effect_spec
can similarly be repaired by adding a mir_points_to
statement involving a_ref
in side_effect_spec
’s postconditions.
MIR verification differs slightly from LLVM verification in how it catches underspecified mutable allocations when using compositional overrides. The LLVM memory model achieves this by invalidating the underlying memory in underspecified allocations. The MIR memory model, on the other hand, does not have a direct counterpart to memory invalidation. As a result, any MIR overrides must specify the values of all mutable allocations in their postconditions, even if the function that calls the override never uses the allocations.
To illustrate this point more finely, suppose that the foo
function had instead been defined like this:
Here, it does not particularly matter what effects the side_effect
function has on its argument, as foo
will now return 42
regardless. Still, if you attempt to prove foo
by using side_effect
as a compositional override, then it is strictly required that you specify the value of side_effect
’s argument in its postconditions, even though the answer that foo
returns is unaffected by this. This is in contrast with LLVM verification, where one could get away without specifying side_effect
’s argument in this example, as the invalidated memory in b
would never be read.
Compositional Verification and Mutable Global Variables
Just like with local mutable allocations (see the previous section), specifications used in compositional overrides must specify the values of mutable global variables in their postconditions. To illustrate this using LLVM verification, here is a variant of the C program from the previous example that uses a mutable global variable a
:
If we attempted to verify foo
against this foo_spec
specification using compositional verification:
Then SAW would reject it, as side_effect_spec
does not specify what a
’s value should be in its postconditions. Just as with local mutable allocations, SAW will invalidate the underlying memory in a
, and subsequently reading from a
in the foo
function will throw an error. The solution is to add an llvm_points_to
statement in the postconditions that declares that a
’s value is set to 0
.
The same concerns apply to MIR verification, where mutable global variables are referred to as static mut
items. (See the MIR static items section for more information). Here is a Rust version of the program above:
Just as above, we can repair this by adding a mir_points_to
statement in side_effect_spec
’s postconditions that specifies that A
is set to 0
.
Recall from the previous section that MIR verification is stricter than LLVM verification when it comes to specifying mutable allocations in the postconditions of compositional overrides. This is especially true for mutable static items. In MIR verification, any compositional overrides must specify the values of all mutable static items in the entire program in their postconditions, even if the function that calls the override never uses the static items. For example, if the foo
function were instead defined like this:
Then it is still required for side_effect_spec
to specify what A
’s value will be in its postconditions, despite the fact that this has no effect on the value that foo
will return.
Specifying Heap Layout
Most functions that operate on pointers expect that certain pointers point to allocated memory before they are called. The llvm_alloc
command allows you to specify that a function expects a particular pointer to refer to an allocated region appropriate for a specific type.
llvm_alloc : LLV