Skip to content

Commit

Permalink
Add assertSat
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipen committed Feb 5, 2025
1 parent 7118a30 commit 04ee12c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion usvm-core/src/main/kotlin/org/usvm/solver/Solver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.usvm.isFalse
import org.usvm.isTrue
import org.usvm.model.UModelBase
import org.usvm.model.UModelDecoder
import org.usvm.utils.assertSat
import kotlin.time.Duration

sealed interface USolverResult<out T>
Expand Down Expand Up @@ -149,7 +150,7 @@ open class USolverBase<Type>(
}

fun emptyModel(): UModelBase<Type> =
(check(UPathConstraints(ctx, ctx.defaultOwnership)) as USatResult<UModelBase<Type>>).model
check(UPathConstraints(ctx, ctx.defaultOwnership)).assertSat().model

override fun close() {
smtSolver.close()
Expand Down
9 changes: 9 additions & 0 deletions usvm-core/src/main/kotlin/org/usvm/utils/ResultUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.usvm.utils

import org.usvm.solver.USatResult
import org.usvm.solver.USolverResult

fun <T> USolverResult<T>.assertSat(): USatResult<T> {
check(this is USatResult) { "Expected SAT result, but got $this" }
return this
}
11 changes: 6 additions & 5 deletions usvm-core/src/test/kotlin/org/usvm/solver/SoftConstraintsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.usvm.memory.UReadOnlyMemory
import org.usvm.model.ULazyModelDecoder
import org.usvm.sizeSort
import org.usvm.types.single.SingleTypeSystem
import org.usvm.utils.assertSat
import kotlin.test.assertSame
import kotlin.time.Duration.Companion.INFINITE

Expand Down Expand Up @@ -65,7 +66,7 @@ open class SoftConstraintsTest {
pc += expr

val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
val model = result.model

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

val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()
val model = result.model

verify(exactly = 1) {
Expand Down Expand Up @@ -144,7 +145,7 @@ open class SoftConstraintsTest {
pc += (inputRef eq nullRef).not()

val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()

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

val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()

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

val softConstraints = softConstraintsProvider.makeSoftConstraints(pc)
val result = solver.checkWithSoftConstraints(pc, softConstraints) as USatResult
val result = solver.checkWithSoftConstraints(pc, softConstraints).assertSat()

val model = result.model
model.eval(expression)
Expand Down
5 changes: 3 additions & 2 deletions usvm-core/src/test/kotlin/org/usvm/types/TypeSolverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.usvm.types.system.interfaceBC1
import org.usvm.types.system.interfaceBC2
import org.usvm.types.system.testTypeSystem
import org.usvm.types.system.top
import org.usvm.utils.assertSat
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotEquals
Expand Down Expand Up @@ -92,7 +93,7 @@ class TypeSolverTest {
val ref = mkRegisterReading(0, addressSort)
pc += mkIsSubtypeExpr(ref, base1)
pc += mkHeapRefEq(ref, nullRef).not()
val model = (solver.check(pc) as USatResult<UModelBase<TestType>>).model
val model = solver.check(pc).assertSat().model
val concreteRef = assertIs<UConcreteHeapRef>(model.eval(ref))
val types = model.typeStreamOf(concreteRef)
types.take100AndAssertEqualsToSetOf(base1, derived1A, derived1B)
Expand All @@ -103,7 +104,7 @@ class TypeSolverTest {
val ref = mkRegisterReading(0, addressSort)
pc += mkIsSubtypeExpr(ref, interface1)
pc += mkHeapRefEq(ref, nullRef).not()
val model = (solver.check(pc) as USatResult<UModelBase<TestType>>).model
val model = solver.check(pc).assertSat().model
val concreteRef = assertIs<UConcreteHeapRef>(model.eval(ref))
val types = model.typeStreamOf(concreteRef)
types.take100AndAssertEqualsToSetOf(derived1A, derivedMulti, derivedMultiInterfaces)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ import org.usvm.machine.state.throwExceptionAndDropStackFrame
import org.usvm.machine.state.throwExceptionWithoutStackFrameDrop
import org.usvm.memory.ULValue
import org.usvm.memory.URegisterStackLValue
import org.usvm.solver.USatResult
import org.usvm.targets.UTargetsSet
import org.usvm.types.singleOrNull
import org.usvm.util.name
import org.usvm.util.outerClassInstanceField
import org.usvm.util.write
import org.usvm.utils.assertSat
import org.usvm.utils.logAssertFailure
import org.usvm.utils.onStateDeath

Expand Down Expand Up @@ -140,7 +140,7 @@ class JcInterpreter(

val solver = ctx.solver<JcType>()

val model = (solver.check(state.pathConstraints) as USatResult).model
val model = solver.check(state.pathConstraints).assertSat().model
state.models = listOf(model)

val entrypointInst = JcMethodEntrypointInst(method, entrypointArguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import org.usvm.machine.state.newStmt
import org.usvm.machine.state.parametersWithThisCount
import org.usvm.machine.state.returnValue
import org.usvm.memory.URegisterStackLValue
import org.usvm.solver.USatResult
import org.usvm.targets.UTargetsSet
import org.usvm.utils.assertSat

private val logger = KotlinLogging.logger {}

Expand Down Expand Up @@ -199,7 +199,7 @@ class TSInterpreter(
)

val solver = ctx.solver<EtsType>()
val model = (solver.check(state.pathConstraints) as USatResult).model
val model = solver.check(state.pathConstraints).assertSat().model
state.models = listOf(model)

state.callStack.push(method, returnSite = null)
Expand Down

0 comments on commit 04ee12c

Please sign in to comment.