## Wiki » History » Revision 1

Revision 1/27
| Next »

*Andrei Tatarnikov, 09/28/2012 06:10 PM *

# Constraint Solver API¶

The constraint solver subsystem is aimed to provide the possibility to automatically generate test cases based on specified constraints. A constraint is represented by a set of limitations for input values. Solvers calculate values of input variables which will violate the limitations if there are any such values.

The subsystem uses an openly distributed SMT solver as an engine (in the current version, we use the Z3 solver by Microsoft Research). In SMT solvers, a special functional language is used to specify constraints. The constraint solver subsystem generates constructions in the SMT language and runs the engine to process them and produce the results (find values of unknown input variables).

## Constraints and SMT¶

Constrains specified as an SMT model are represented by a set of assertions (formulas) that must be satisfied. An SMT solver checks the satisfiability of the model and suggests a solution (variable values) that would satisfy the model. In the example below, we specify a model that should help us create a test that will cause a MIPS processor to generate an exception. We want to find values of the rs and rt general purpose registers that will cause the ADD instruction to raise an integer overflow exception. It should be correct 32-bit signed integers that are not equal to each other. Here is an SMT script:

(define-sort Int_t () (_ BitVec 64)) (define-fun INT_ZERO () Int_t (_ bv0 64)) (define-fun INT_BASE_SIZE () Int_t (_ bv32 64)) (define-fun INT_SIGN_MASK () Int_t (bvshl (bvnot INT_ZERO) INT_BASE_SIZE)) (define-fun IsValidPos ((x!1 Int_t)) Bool (ite (= (bvand x!1 INT_SIGN_MASK) INT_ZERO) true false)) (define-fun IsValidNeg ((x!1 Int_t)) Bool (ite (= (bvand x!1 INT_SIGN_MASK) INT_SIGN_MASK) true false)) (define-fun IsValidSignedInt ((x!1 Int_t)) Bool (ite (or (IsValidPos x!1) (IsValidNeg x!1)) true false)) (declare-const rs Int_t) (declare-const rt Int_t) ; rt and rs must contain valid sign-extended 32-bit values (bits 63..31 equal) (assert (IsValidSignedInt rs)) (assert (IsValidSignedInt rt)) ; the condition for an overflow: the summation result is not a valid sign-extended 32-bit value (assert (not (IsValidSignedInt (bvadd rs rt)))) ; just in case: rs and rt are not equal (to make the results more interesting) (assert (not (= rs rt))) (check-sat) (echo "Values that lead to an overflow:") (get-value (rs rt))

In an ideal case, each run of an SMT solver should return random values from the set of possible solutions. This should improve test coverage. Unfortunately, the current implementation is limited to a single solution that is constant for all run. This should be improved in the final version.

## Tree Representation¶

In our system, we use context-independent syntax trees to represent constraints. These trees are then used to generate a representation that can be understood by a particular SMT solver. Generally, it is an SMT model that uses some limited set of solver features applicable to microprocessor verification. The syntax tree contains nodes of the following types:- Constraint. This is the root node of the tree. It holds the list of unknown variables and the list of assertions (formulas) for these variables.
- Formula. Represents an assertion expression. Can be combined with other formulas to build a more complex expression (by applying logic OR, AND or NOT to it). The underlying expression must be a logic expression that can be solved to true or false.
- Operation. Represents an unary or binary operation with some unknown variable, some value or some expression as parameters.
- Variable.Represents an input variable. It can have an assigned value and, in such a case, will be treated as a value. Otherwise, it is an unknown variable. A variable includes a type as an attribute.
- Value. Specifies some known value of the specified type which can be accessed as an attribute.

Note: Operation, Variables and Value are designed to be treated polymorphically. This allows combining them to build complex expressions.

## Constraint Solver Java Library¶

The Constraint Solver subsystem is implemented in Java. The source code files are located in the "microtesk++/constraint-solver" folder. The Java classes are organized in the following packages:- ru.ispras.microtesk.constraints - contains SMT model generation logic and solver implementations.
- ru.ispras.microtesk.constraints.syntax - contains classes implementing syntax tree nodes.
- ru.ispras.microtesk.constraints.syntax.types - contains code that specifies particular data types and operation types.
- ru.ispras.microtesk.constraints.tests - contains JUnit test cases.

### Core classes/interfaces¶

**Syntax Tree Implementation**

- Constraint. Parameterized by a collection of Variable objects and a collection of Formula objects.
- Formula. Parameterized by an Operation object.
- Operation. Implements SyntaxElement. Parameterized by operand objects implementing SyntaxElement and an operation type object implementing OperationType.
- Variable. Implements SyntaxElement. Parameterized by the variable name string, a data type object implemeting DataType and a BigInteger value object.
- Value. Implements SyntaxElement. Parameterized a data type object implemeting DataType and a BigInteger value object.

The SyntaxElement interface provides the ability to combine different kinds of elements into expressions.

The current implementation supports operations with the following data types: (1) Bit vectors, (2) Booleans. They are implemented in the BitVector and LogicBoolean classes. The BitVectorOperation and LogicBooleanOperation classes specify supported operation with these types. For example, the LogicBooleanOperation class looks like this:

public final class LogicBooleanOperation extends OperationType { private LogicBooleanOperation() {} /** Operation: Logic - Equality */ public static final OperationType EQ = new LogicBooleanOperation(); /** Operation: Logic - AND */ public static final OperationType AND = new LogicBooleanOperation(); /** Operation: Logic - OR */ public static final OperationType OR = new LogicBooleanOperation(); /** Operation: Logic - NOT */ public static final OperationType NOT = new LogicBooleanOperation(); /** Operation: Logic - XOR */ public static final OperationType XOR = new LogicBooleanOperation(); /** Operation: Logic - Implication */ public static final OperationType IMPL= new LogicBooleanOperation(); }

The code below demonstrates how we can build a syntax tree representation for the integer overflow constraint:

class BitVectorIntegerOverflowTestCase implements SolverTestCase { private static final int BIT_VECTOR_LENGTH = 64; private static final DataType BIT_VECTOR_TYPE = DataType.getBitVector(BIT_VECTOR_LENGTH); private static final Value INT_ZERO = new Value(new BigInteger("0"), BIT_VECTOR_TYPE); private static final Value INT_BASE_SIZE = new Value(new BigInteger("32"), BIT_VECTOR_TYPE); private static final Operation INT_SIGN_MASK = new Operation(BitVectorOperation.BVSHL, new Operation(BitVectorOperation.BVNOT, INT_ZERO, null), INT_BASE_SIZE); private Operation IsValidPos(SyntaxElement arg) { return new Operation(LogicBooleanOperation.EQ, new Operation(BitVectorOperation.BVAND, arg, INT_SIGN_MASK), INT_ZERO); } private Operation IsValidNeg(SyntaxElement arg) { return new Operation(LogicBooleanOperation.EQ, new Operation(BitVectorOperation.BVAND, arg, INT_SIGN_MASK), INT_SIGN_MASK); } private Operation IsValidSignedInt(SyntaxElement arg) { return new Operation(LogicBooleanOperation.OR, IsValidPos(arg), IsValidNeg(arg)); } public Constraint getConstraint() { Constraint constraint = new Constraint(); Variable rs = new Variable("rs", BIT_VECTOR_TYPE, null); constraint.addVariable(rs); Variable rt = new Variable("rt", BIT_VECTOR_TYPE, null); constraint.addVariable(rt); constraint.addFormula( new Formula( IsValidSignedInt(rs) ) ); constraint.addFormula( new Formula( IsValidSignedInt(rt) ) ); constraint.addFormula( new Formula( new Operation( LogicBooleanOperation.NOT, IsValidSignedInt(new Operation(BitVectorOperation.BVADD, rs, rt)), null ) ) ); constraint.addFormula( new Formula( new Operation(LogicBooleanOperation.NOT, new Operation(LogicBooleanOperation.EQ, rs, rt), null) ) ); return constraint; } public Vector<Variable> getExpectedVariables() { Vector<Variable> result = new Vector<Variable>(); result.add(new Variable("rs", BIT_VECTOR_TYPE, new BigInteger("000000009b91b193", 16))); result.add(new Variable("rt", BIT_VECTOR_TYPE, new BigInteger("000000009b91b1b3", 16))); return result; } }

**Representation Translation**

The logic that translates a tree representation into an SMT representation is implemented in the following way: Methods of the Translator class traverse the constraint syntax tree and use methods of the RepresentationBuilder interface to translate information about its nodes into a representation that can be understood by a particular solver. The RepresentationBuilder interface looks like follows:

public interface RepresentationBuilder { public void addVariableDeclaration(Variable variable); public void beginConstraint(); public void endConstraint(); public void beginFormula(); public void endFormula(); public void beginExpression(); public void endExpression(); public void appendValue(Value value); public void appendVariable(Variable variable); public void appendOperation(OperationType type); }

**Solver Implementation**

Solvers use the Translator class and a specific implementation of the RepresentationBuilder interface to generate an SMT representation of a constraint. Then they run a solver engine to solve the constraint and produce the results. Solver implement a common interface called Solver that looks like this:

public interface Solver { public boolean solveConstraint(Constraint constraint); public boolean isSolved(); public boolean isSatisfiable(); public int getErrorCount(); public String getErrorText(int index); public int getVariableCount(); public Variable getVariable(int index); }

Updated by Andrei Tatarnikov about 12 years ago · 1 revisions