Skip to content

Commit 04ee12c

Browse files
committed
Add assertSat
1 parent 7118a30 commit 04ee12c

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

usvm-core/src/main/kotlin/org/usvm/solver/Solver.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.usvm.isFalse
1111
import org.usvm.isTrue
1212
import org.usvm.model.UModelBase
1313
import org.usvm.model.UModelDecoder
14+
import org.usvm.utils.assertSat
1415
import kotlin.time.Duration
1516

1617
sealed interface USolverResult<out T>
@@ -149,7 +150,7 @@ open class USolverBase<Type>(
149150
}
150151

151152
fun emptyModel(): UModelBase<Type> =
152-
(check(UPathConstraints(ctx, ctx.defaultOwnership)) as USatResult<UModelBase<Type>>).model
153+
check(UPathConstraints(ctx, ctx.defaultOwnership)).assertSat().model
153154

154155
override fun close() {
155156
smtSolver.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.usvm.utils
2+
3+
import org.usvm.solver.USatResult
4+
import org.usvm.solver.USolverResult
5+
6+
fun <T> USolverResult<T>.assertSat(): USatResult<T> {
7+
check(this is USatResult) { "Expected SAT result, but got $this" }
8+
return this
9+
}

usvm-core/src/test/kotlin/org/usvm/solver/SoftConstraintsTest.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.usvm.memory.UReadOnlyMemory
2121
import org.usvm.model.ULazyModelDecoder
2222
import org.usvm.sizeSort
2323
import org.usvm.types.single.SingleTypeSystem
24+
import org.usvm.utils.assertSat
2425
import kotlin.test.assertSame
2526
import kotlin.time.Duration.Companion.INFINITE
2627

@@ -65,7 +66,7 @@ open class SoftConstraintsTest {
6566
pc += expr
6667

6768
val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
68-
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
69+
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
6970
val model = result.model
7071

7172
val fstRegisterValue = model.eval(fstRegister)
@@ -99,7 +100,7 @@ open class SoftConstraintsTest {
99100
USolverBase(ctx, KZ3Solver(ctx), typeSolver, translator, decoder, timeout = INFINITE)
100101

101102
val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
102-
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
103+
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
103104
val model = result.model
104105

105106
verify(exactly = 1) {
@@ -144,7 +145,7 @@ open class SoftConstraintsTest {
144145
pc += (inputRef eq nullRef).not()
145146

146147
val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
147-
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
148+
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
148149

149150
val model = result.model
150151
val value = model.eval(mkInputArrayLengthReading(region, inputRef))
@@ -164,7 +165,7 @@ open class SoftConstraintsTest {
164165
pc += (inputRef eq nullRef).not()
165166

166167
val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
167-
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
168+
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
168169

169170
val model = result.model
170171
val value = model.eval(mkInputArrayLengthReading(region, inputRef))
@@ -182,7 +183,7 @@ open class SoftConstraintsTest {
182183
pc += expression
183184

184185
val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
185-
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
186+
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
186187

187188
val model = result.model
188189
model.eval(expression)

usvm-core/src/test/kotlin/org/usvm/types/TypeSolverTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import org.usvm.types.system.interfaceBC1
5151
import org.usvm.types.system.interfaceBC2
5252
import org.usvm.types.system.testTypeSystem
5353
import org.usvm.types.system.top
54+
import org.usvm.utils.assertSat
5455
import kotlin.test.assertEquals
5556
import kotlin.test.assertIs
5657
import kotlin.test.assertNotEquals
@@ -92,7 +93,7 @@ class TypeSolverTest {
9293
val ref = mkRegisterReading(0, addressSort)
9394
pc += mkIsSubtypeExpr(ref, base1)
9495
pc += mkHeapRefEq(ref, nullRef).not()
95-
val model = (solver.check(pc) as USatResult<UModelBase<TestType>>).model
96+
val model = solver.check(pc).assertSat().model
9697
val concreteRef = assertIs<UConcreteHeapRef>(model.eval(ref))
9798
val types = model.typeStreamOf(concreteRef)
9899
types.take100AndAssertEqualsToSetOf(base1, derived1A, derived1B)
@@ -103,7 +104,7 @@ class TypeSolverTest {
103104
val ref = mkRegisterReading(0, addressSort)
104105
pc += mkIsSubtypeExpr(ref, interface1)
105106
pc += mkHeapRefEq(ref, nullRef).not()
106-
val model = (solver.check(pc) as USatResult<UModelBase<TestType>>).model
107+
val model = solver.check(pc).assertSat().model
107108
val concreteRef = assertIs<UConcreteHeapRef>(model.eval(ref))
108109
val types = model.typeStreamOf(concreteRef)
109110
types.take100AndAssertEqualsToSetOf(derived1A, derivedMulti, derivedMultiInterfaces)

usvm-jvm/src/main/kotlin/org/usvm/machine/interpreter/JcInterpreter.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ import org.usvm.machine.state.throwExceptionAndDropStackFrame
7474
import org.usvm.machine.state.throwExceptionWithoutStackFrameDrop
7575
import org.usvm.memory.ULValue
7676
import org.usvm.memory.URegisterStackLValue
77-
import org.usvm.solver.USatResult
7877
import org.usvm.targets.UTargetsSet
7978
import org.usvm.types.singleOrNull
8079
import org.usvm.util.name
8180
import org.usvm.util.outerClassInstanceField
8281
import org.usvm.util.write
82+
import org.usvm.utils.assertSat
8383
import org.usvm.utils.logAssertFailure
8484
import org.usvm.utils.onStateDeath
8585

@@ -140,7 +140,7 @@ class JcInterpreter(
140140

141141
val solver = ctx.solver<JcType>()
142142

143-
val model = (solver.check(state.pathConstraints) as USatResult).model
143+
val model = solver.check(state.pathConstraints).assertSat().model
144144
state.models = listOf(model)
145145

146146
val entrypointInst = JcMethodEntrypointInst(method, entrypointArguments)

usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TSInterpreter.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import org.usvm.machine.state.newStmt
3535
import org.usvm.machine.state.parametersWithThisCount
3636
import org.usvm.machine.state.returnValue
3737
import org.usvm.memory.URegisterStackLValue
38-
import org.usvm.solver.USatResult
3938
import org.usvm.targets.UTargetsSet
39+
import org.usvm.utils.assertSat
4040

4141
private val logger = KotlinLogging.logger {}
4242

@@ -199,7 +199,7 @@ class TSInterpreter(
199199
)
200200

201201
val solver = ctx.solver<EtsType>()
202-
val model = (solver.check(state.pathConstraints) as USatResult).model
202+
val model = solver.check(state.pathConstraints).assertSat().model
203203
state.models = listOf(model)
204204

205205
state.callStack.push(method, returnSite = null)

0 commit comments

Comments
 (0)