Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fb0adca
Draft: model synthesis
Jun 20, 2022
11c47d9
m
AbdullinAM Jun 20, 2022
4aaa8e5
first prototype, needs debugging and tuning
AbdullinAM Jun 22, 2022
9c63ba0
prototype
AbdullinAM Jun 28, 2022
707fa6d
refactor
AbdullinAM Jun 29, 2022
10c4193
very early prototype
AbdullinAM Jul 8, 2022
4285a5c
UtConstraintModel implemented
AbdullinAM Jul 12, 2022
22e2266
partial UtConstraint to UtExpression convertion
AbdullinAM Jul 14, 2022
6b47a9a
working prototypes
AbdullinAM Jul 19, 2022
a75a840
bugfixes + expression support in constraints
AbdullinAM Jul 21, 2022
fb392f6
m
AbdullinAM Jul 26, 2022
5f35311
everything kind of works, but some constraints need to be added
AbdullinAM Jul 27, 2022
ba9bb62
first working protoype with arrays
AbdullinAM Aug 1, 2022
065924f
renaming
AbdullinAM Aug 1, 2022
8cc7515
first tests and fixes
AbdullinAM Aug 2, 2022
4a14606
more expressions supported
AbdullinAM Aug 3, 2022
bbf525b
some cleanup + more expressions supported
AbdullinAM Aug 8, 2022
850b530
cleanup
AbdullinAM Aug 8, 2022
8799691
support for multidimensional arrays + fix for array generation
AbdullinAM Aug 9, 2022
60df4a4
first support of lists
AbdullinAM Aug 10, 2022
8963a7e
support sets
AbdullinAM Aug 10, 2022
1578c96
small refactorings
AbdullinAM Aug 11, 2022
1dd4b25
maps
AbdullinAM Aug 12, 2022
313757d
some cleanup and parameters
AbdullinAM Aug 12, 2022
faa0047
test write fix
AbdullinAM Aug 12, 2022
94740c9
m
AbdullinAM Aug 22, 2022
9920283
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Aug 22, 2022
fd22077
merge with master
AbdullinAM Aug 24, 2022
4e97b5f
first prototype of constraint scoring selector
AbdullinAM Aug 26, 2022
2544b31
m
AbdullinAM Aug 30, 2022
e585371
fixes
AbdullinAM Aug 31, 2022
d2564e9
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Sep 7, 2022
bb1acbc
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Sep 7, 2022
c6fbeb0
Split constraint models into a set of non-intersecting subsets before…
AbdullinAM Sep 12, 2022
2553ef7
m
AbdullinAM Sep 12, 2022
e943a16
some cleanup
AbdullinAM Sep 13, 2022
cd5fff8
more cleanup
AbdullinAM Sep 13, 2022
c2a7d31
order models in the subsets
AbdullinAM Sep 13, 2022
d1b09c8
simple caching of synthesis unit contexts
AbdullinAM Sep 13, 2022
eedccb9
option to enable/disable caching of synthesis contexts
AbdullinAM Sep 14, 2022
11eea4d
m
AbdullinAM Sep 14, 2022
9712e70
tests
AbdullinAM Sep 14, 2022
1c6b679
unit tests fixed
AbdullinAM Sep 19, 2022
4e37763
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Sep 28, 2022
34ed6fe
merge with main
AbdullinAM Sep 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
very early prototype
  • Loading branch information
AbdullinAM committed Jul 8, 2022
commit 10c4193b1273581281860614956cc7c18e8d25ac
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ data class UtExecution(
var summary: List<DocStatement>? = null,
var testMethodName: String? = null,
var displayName: String? = null,
val hole: Any? = null
) : UtResult() {
/**
* By design the 'before' and 'after' states contain info about the same fields.
Expand Down
170 changes: 170 additions & 0 deletions utbot-framework/src/main/kotlin/org/utbot/engine/ConstraintResolver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package org.utbot.engine

import org.utbot.engine.MemoryState.CURRENT
import org.utbot.engine.MemoryState.INITIAL
import org.utbot.engine.MemoryState.STATIC_INITIAL
import org.utbot.engine.z3.value
import org.utbot.framework.plugin.api.FieldId
import org.utbot.engine.pc.*
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.classId
import soot.VoidType

data class ResolvedObject(
val classId: ClassId,
val value: SymbolicValue,
val concreteValue: Any,
val constraints: Set<UtBoolExpression>
)

data class ResolvedConstraints(val parameters: List<ResolvedObject>, val statics: Map<FieldId, ResolvedObject>)

data class ResolvedExecutionConstraints(
val modelsBefore: ResolvedConstraints,
val modelsAfter: ResolvedConstraints
)

/**
* Constructs path conditions using calculated model. Can construct them for initial and current memory states that reflect
* initial parameters or current values for concrete call.
*/
class ConstraintResolver(
private val memory: Memory,
val holder: UtSolverStatusSAT,
) {

lateinit var state: MemoryState
private val resolvedConstraints = mutableMapOf<Address, Set<UtBoolExpression>>()
private val validSymbols = mutableSetOf<UtExpression>()

/**
* Contains FieldId of the static field which is construction at the moment and null of there is no such field.
* It is used to find initial state for the fieldId in the [Memory.findArray] method.
*/
private var staticFieldUnderResolving: FieldId? = null

private fun clearState() {
resolvedConstraints.clear()
validSymbols.clear()
resolvedConstraints.clear()
}

private inline fun <T> withMemoryState(state: MemoryState, block: () -> T): T {
try {
this.state = state
clearState()
return block()
} finally {
clearState()
}
}

private inline fun <T> withStaticMemoryState(staticFieldUnderResolving: FieldId, block: () -> T): T {
return if (state == INITIAL) {
try {
this.staticFieldUnderResolving = staticFieldUnderResolving
this.state = STATIC_INITIAL
block()
} finally {
this.staticFieldUnderResolving = null
this.state = INITIAL
}
} else {
block()
}
}

internal fun resolveModels(parameters: List<SymbolicValue>): ResolvedExecutionConstraints {
validSymbols.addAll(parameters.map { it.asExpr })
val allAddresses = UtExprCollector { it is UtAddrExpression }.let {
holder.constraints.hard.forEach { constraint -> constraint.accept(it) }
holder.constraints.soft.forEach { constraint -> constraint.accept(it) }
it.results
}.map { it as UtAddrExpression }.associateWith { holder.concreteAddr(it) }
val staticsBefore = memory.staticFields().map { (fieldId, states) -> fieldId to states.stateBefore }
val staticsAfter = memory.staticFields().map { (fieldId, states) -> fieldId to states.stateAfter }

val modelsBefore = withMemoryState(INITIAL) {
internalResolveModel(parameters, staticsBefore, allAddresses)
}

val modelsAfter = withMemoryState(CURRENT) {
val resolvedModels = internalResolveModel(parameters, staticsAfter, allAddresses)
resolvedModels
}

return ResolvedExecutionConstraints(modelsBefore, modelsAfter)
}

private fun internalResolveModel(
parameters: List<SymbolicValue>,
statics: List<Pair<FieldId, SymbolicValue>>,
addrs: Map<UtAddrExpression, Address>
): ResolvedConstraints {
val parameterModels = parameters.map { resolveModel(it, addrs) }

val staticModels = statics.map { (fieldId, value) ->
withStaticMemoryState(fieldId) {
resolveModel(value, addrs)
}
}

val allStatics = staticModels.mapIndexed { i, model -> statics[i].first to model }.toMap()
return ResolvedConstraints(parameterModels, allStatics)
}

fun resolveModel(value: SymbolicValue, addrs: Map<UtAddrExpression, Address>): ResolvedObject = when (value) {
is PrimitiveValue -> resolvePrimitiveValue(value, addrs)
is ReferenceValue -> resolveReferenceValue(value, addrs)
}

private fun collectConstraintAtoms(predicate: (UtExpression) -> Boolean): Set<UtBoolExpression> = UtAtomCollector(predicate).run {
holder.constraints.hard.forEach {
it.accept(this)
}
holder.constraints.soft.forEach {
it.accept(this)
}
result
}.map {
val rhv = if (holder.eval(it).value() as Boolean) UtTrue else UtFalse
UtEqExpression(it, rhv)
}.toSet()

private fun collectAtoms(value: SymbolicValue, addrs: Map<UtAddrExpression, Address>): Set<UtBoolExpression> =
when (value) {
is PrimitiveValue -> collectConstraintAtoms { it == value.expr }
is ReferenceValue -> {
val concreteAddr = addrs[value.addr]!!
if (concreteAddr == NULL_ADDR) {
setOf(
UtEqExpression(UtEqExpression(value.addr, nullObjectAddr), UtTrue)
)
} else {
val valueExprs = addrs.filter { it.value == concreteAddr }.keys
validSymbols.addAll(valueExprs)
resolvedConstraints.getOrPut(concreteAddr) {
val set = collectConstraintAtoms { it in valueExprs }.toMutableSet()
set += UtEqExpression(UtEqExpression(value.addr, nullObjectAddr), UtFalse)
set
}
}
}
}

private fun resolvePrimitiveValue(value: PrimitiveValue, addrs: Map<UtAddrExpression, Address>): ResolvedObject =
if (value.type == VoidType.v()) {
ResolvedObject(value.type.classId, value, Unit, emptySet())
} else {
ResolvedObject(value.type.classId, value, holder.eval(value.expr), collectAtoms(value, addrs))
}

private fun resolveReferenceValue(value: ReferenceValue, addrs: Map<UtAddrExpression, Address>): ResolvedObject =
ResolvedObject(value.type.classId, value, holder.concreteAddr(value.addr), collectAtoms(value, addrs))

private val SymbolicValue.asExpr: UtExpression get() = when (this) {
is PrimitiveValue -> expr
is ReferenceValue -> addr
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ private data class ArrayExtractionDetails(
val oneDimensionalArray: UtArrayExpressionBase
)

private const val NULL_ADDR = 0
internal const val NULL_ADDR = 0
internal val nullObjectAddr = UtAddrExpression(mkInt(NULL_ADDR))


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3684,13 +3684,16 @@ class UtBotSymbolicEngine(
val stateAfter = modelsAfter.constructStateForMethod(state.executionStack.first().method)
require(stateBefore.parameters.size == stateAfter.parameters.size)

val resolvedConstraints = ConstraintResolver(updatedMemory, holder).run { resolveModels(resolvedParameters) }

val symbolicUtExecution = UtExecution(
stateBefore,
stateAfter,
symbolicExecutionResult,
instrumentation,
entryMethodPath(),
state.fullPath()
state.fullPath(),
hole = resolvedConstraints
)

globalGraph.traversed(state)
Expand Down
Loading