Project

General

Profile

Wiki » History » Version 5

Andrei Tatarnikov, 10/01/2012 07:38 AM

1 1 Andrei Tatarnikov
h1. Constraint Solver API
2
3 2 Andrei Tatarnikov
h2. Basic Concepts
4 1 Andrei Tatarnikov
5 2 Andrei Tatarnikov
Constraint Solver Java API provides a Java API for generating pseudorandom values that satisfy certain constraints. This feature is important for test generators that aim at creating directed tests. At logical level, a constraint is represented by a set of expressions that specify limitations for input values (assertions that must be hold for those values). If there are values satisfying all of the specified assertions they will be used a solution for the constraint. If there is a multitude of values satisfying the constraint, specific values will be selected from the range of possible solutions on random basis.
6
7 4 Andrei Tatarnikov
From an implementational point of view, the API represents a wrapper around some kind of an openly distributed SMT solver engine (in the current version, we use the Z3 solver by Microsoft Research). It can be extended to support other solver engines and it provides a possibility to interact with different solver engines in a uniform way. Also, it facilitates creating task-specific custom solvers and extending functionality of existing solver engines by adding custom operations (macros based on built-in operations).
8 2 Andrei Tatarnikov
9 3 Andrei Tatarnikov
h2. SMT-LIB
10 2 Andrei Tatarnikov
11
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).
12
13
// TODO: more info
14 1 Andrei Tatarnikov
15
h2. Constraints and SMT
16
17
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:
18
19
<pre>
20
(define-sort        Int_t () (_ BitVec 64))
21
22
(define-fun      INT_ZERO () Int_t (_ bv0 64))
23
(define-fun INT_BASE_SIZE () Int_t (_ bv32 64))
24
(define-fun INT_SIGN_MASK () Int_t (bvshl (bvnot INT_ZERO) INT_BASE_SIZE))
25
26
(define-fun IsValidPos ((x!1 Int_t)) Bool (ite (= (bvand x!1 INT_SIGN_MASK) INT_ZERO) true false))
27
(define-fun IsValidNeg ((x!1 Int_t)) Bool (ite (= (bvand x!1 INT_SIGN_MASK) INT_SIGN_MASK) true false))
28
(define-fun IsValidSignedInt ((x!1 Int_t)) Bool (ite (or (IsValidPos x!1) (IsValidNeg x!1)) true false))
29
30
(declare-const rs Int_t)
31
(declare-const rt Int_t)
32
33
; rt and rs must contain valid sign-extended 32-bit values (bits 63..31 equal)
34
(assert (IsValidSignedInt rs))
35
(assert (IsValidSignedInt rt))
36
37
; the condition for an overflow: the summation result is not a valid sign-extended 32-bit value
38
(assert (not (IsValidSignedInt (bvadd rs rt))))
39
40
; just in case: rs and rt are not equal (to make the results more interesting)
41
(assert (not (= rs rt)))
42
43
(check-sat)
44
45
(echo "Values that lead to an overflow:")
46
(get-value (rs rt))
47
</pre>
48
49 5 Andrei Tatarnikov
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.
50
51
h3. SMT Liminations.
52
53
Recursion in not allowed in SMT-LIB. At least, this applies the Z3 implementation. In other words, code like provided below is not valid:
54
55
<pre>
56
(define-fun fact ((x Int)) Int (ite (= x 0) 1 (fact (- x 1))))
57
(simplify (fact 10))
58
</pre>
59 1 Andrei Tatarnikov
60
h2. Tree Representation
61
62
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:
63
# 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.
64
# 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.
65
# Operation. Represents an unary or binary operation with some unknown variable, some value or some expression as parameters.
66
# 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.
67
# Value. Specifies some known value of the specified type which can be accessed as an attribute.
68
69
Note: Operation, Variables and Value are designed to be treated polymorphically. This allows combining them to build complex expressions.
70
71
h2. Constraint Solver Java Library
72
73
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:
74
# ru.ispras.microtesk.constraints - contains SMT model generation logic and solver implementations.
75
# ru.ispras.microtesk.constraints.syntax - contains classes implementing syntax tree nodes.
76
# ru.ispras.microtesk.constraints.syntax.types - contains code that specifies particular data types and operation types.
77
# ru.ispras.microtesk.constraints.tests - contains JUnit test cases.
78
79
h3. Core classes/interfaces
80
81
*Syntax Tree Implementation*
82
83
The syntax tree nodes are implemented in the following classes:
84
* Constraint. Parameterized by a collection of Variable objects and a collection of Formula objects.
85
* Formula. Parameterized by an Operation object.
86
* Operation. Implements SyntaxElement. Parameterized by operand objects implementing SyntaxElement and an operation type object implementing OperationType.
87
* Variable. Implements SyntaxElement. Parameterized by the variable name string, a data type object implemeting DataType and a BigInteger value object.   
88
* Value. Implements SyntaxElement. Parameterized a data type object implemeting DataType and a BigInteger value object.
89
90
The SyntaxElement interface provides the ability to combine different kinds of elements into expressions.
91
92
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:
93
94
<pre>
95
public final class LogicBooleanOperation extends OperationType
96
{
97
	private LogicBooleanOperation() {}
98
	
99
	/** Operation: Logic - Equality */
100
	public static final OperationType EQ = new LogicBooleanOperation();
101
	/** Operation: Logic - AND */
102
	public static final OperationType AND = new LogicBooleanOperation();
103
	/** Operation: Logic - OR */
104
	public static final OperationType OR  = new LogicBooleanOperation();
105
	/** Operation: Logic - NOT */
106
	public static final OperationType NOT = new LogicBooleanOperation();
107
	/** Operation: Logic - XOR */
108
	public static final OperationType XOR = new LogicBooleanOperation();
109
	/** Operation: Logic - Implication */
110
	public static final OperationType IMPL= new LogicBooleanOperation();
111
} 
112
</pre>
113
114
The code below demonstrates how we can build a syntax tree representation for the integer overflow constraint:
115
116
<pre>
117
class BitVectorIntegerOverflowTestCase implements SolverTestCase
118
{
119
	private static final int      BIT_VECTOR_LENGTH = 64;
120
	private static final DataType   BIT_VECTOR_TYPE = DataType.getBitVector(BIT_VECTOR_LENGTH);
121
	private static final Value             INT_ZERO = new Value(new BigInteger("0"), BIT_VECTOR_TYPE);
122
	private static final Value        INT_BASE_SIZE = new Value(new BigInteger("32"), BIT_VECTOR_TYPE);
123
124
	private static final Operation    INT_SIGN_MASK =
125
		new Operation(BitVectorOperation.BVSHL, new Operation(BitVectorOperation.BVNOT, INT_ZERO, null), INT_BASE_SIZE);
126
	
127
	private Operation IsValidPos(SyntaxElement arg)
128
	{
129
		return new Operation(LogicBooleanOperation.EQ, new Operation(BitVectorOperation.BVAND, arg, INT_SIGN_MASK), INT_ZERO);
130
	}
131
	
132
	private Operation IsValidNeg(SyntaxElement arg)
133
	{
134
		return new Operation(LogicBooleanOperation.EQ, new Operation(BitVectorOperation.BVAND, arg, INT_SIGN_MASK), INT_SIGN_MASK);
135
	}
136
	
137
	private Operation IsValidSignedInt(SyntaxElement arg)
138
	{
139
		return new Operation(LogicBooleanOperation.OR, IsValidPos(arg), IsValidNeg(arg));
140
	}
141
	
142
	public Constraint getConstraint()
143
	{
144
		Constraint constraint = new Constraint();
145
		
146
		Variable rs = new Variable("rs", BIT_VECTOR_TYPE, null);
147
		constraint.addVariable(rs);
148
		
149
		Variable rt = new Variable("rt", BIT_VECTOR_TYPE, null);
150
		constraint.addVariable(rt);
151
		
152
		
153
		constraint.addFormula(
154
			new Formula(
155
				IsValidSignedInt(rs)
156
			)
157
		);
158
		
159
		constraint.addFormula(
160
			new Formula(
161
				IsValidSignedInt(rt)
162
			)
163
		);
164
165
		constraint.addFormula(
166
			new Formula(
167
				new Operation(
168
					LogicBooleanOperation.NOT,
169
					IsValidSignedInt(new Operation(BitVectorOperation.BVADD, rs, rt)),
170
					null
171
				) 
172
			)
173
		);
174
175
		constraint.addFormula(
176
			new Formula(
177
				new Operation(LogicBooleanOperation.NOT, new Operation(LogicBooleanOperation.EQ, rs, rt), null)
178
			)
179
		);
180
181
		return constraint;
182
	}
183
	
184
	public Vector<Variable> getExpectedVariables()	
185
	{
186
		Vector<Variable> result = new Vector<Variable>();
187
		
188
		result.add(new Variable("rs", BIT_VECTOR_TYPE, new BigInteger("000000009b91b193", 16)));
189
		result.add(new Variable("rt", BIT_VECTOR_TYPE, new BigInteger("000000009b91b1b3", 16)));
190
		
191
		return result;	
192
	}
193
}
194
</pre>
195
196
*Representation Translation*
197
198
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:
199
200
<pre>
201
public interface RepresentationBuilder
202
{	
203
	public void addVariableDeclaration(Variable variable);
204
205
	public void beginConstraint();
206
	public void endConstraint();
207
208
	public void beginFormula();
209
	public void endFormula();
210
211
	public void beginExpression();
212
	public void endExpression();
213
214
	public void appendValue(Value value);
215
	public void appendVariable(Variable variable);
216
	public void appendOperation(OperationType type);
217
}
218
</pre>
219
220
*Solver Implementation*
221
222
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:
223
224
<pre>
225
public interface Solver
226
{
227
	public boolean solveConstraint(Constraint constraint);
228
	
229
	public boolean isSolved();
230
	public boolean isSatisfiable();
231
	
232
	public int getErrorCount();
233
	public String getErrorText(int index);
234
	
235
	public int getVariableCount();
236
	public Variable getVariable(int index);
237
}
238
</pre>