Skip to content

Commit e4ac429

Browse files
authored
initializeAllocatedSet in USetRegion (#316)
* tmp: USetMemoryRegion is now not internal * implemented initializedSet * initializeAllocatedSet * fix * import fix * detekt fix * Valentyn's fix in UComposer * PR fix
1 parent d496174 commit e4ac429

3 files changed

Lines changed: 72 additions & 4 deletions

File tree

usvm-core/src/main/kotlin/org/usvm/Composition.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ open class UComposer<Type, USizeSort : USort>(
3131
override fun <T : USort> transform(expr: UIteExpr<T>): UExpr<T> =
3232
transformExprAfterTransformed(expr, expr.condition) { condition ->
3333
when {
34-
condition.isTrue -> apply(expr.trueBranch)
35-
condition.isFalse -> apply(expr.falseBranch)
34+
condition.isTrue -> transformExprAfterTransformed(expr, expr.trueBranch) { it }
35+
condition.isFalse -> transformExprAfterTransformed(expr, expr.falseBranch) { it }
3636
else -> super.transform(expr)
3737
}
3838
}

usvm-core/src/main/kotlin/org/usvm/collection/set/primitive/USetRegion.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import org.usvm.UConcreteHeapAddress
66
import org.usvm.UExpr
77
import org.usvm.UHeapRef
88
import org.usvm.USort
9-
import org.usvm.collection.set.USymbolicSetEntries
109
import org.usvm.collection.set.USymbolicSetElement
1110
import org.usvm.collection.set.USymbolicSetElementsCollector
11+
import org.usvm.collection.set.USymbolicSetEntries
1212
import org.usvm.collections.immutable.getOrPut
1313
import org.usvm.collections.immutable.implementations.immutableMap.UPersistentHashMap
14-
import org.usvm.collections.immutable.persistentHashMapOf
1514
import org.usvm.collections.immutable.internal.MutabilityOwnership
15+
import org.usvm.collections.immutable.persistentHashMapOf
1616
import org.usvm.memory.ULValue
1717
import org.usvm.memory.UMemoryRegion
1818
import org.usvm.memory.UMemoryRegionId
@@ -81,6 +81,16 @@ interface USetRegion<SetType, ElementSort : USort, Reg : Region<Reg>> :
8181
operationGuard: UBoolExpr,
8282
ownership: MutabilityOwnership,
8383
): USetRegion<SetType, ElementSort, Reg>
84+
85+
fun initializeAllocatedSet(
86+
address: UConcreteHeapAddress,
87+
setType: SetType,
88+
sort: ElementSort,
89+
content: Set<UExpr<ElementSort>>,
90+
operationGuard: UBoolExpr,
91+
ownership: MutabilityOwnership,
92+
makeDisjointCheck: Boolean = true,
93+
): USetRegion<SetType, ElementSort, Reg>
8494
}
8595

8696
internal class USetMemoryRegion<SetType, ElementSort : USort, Reg : Region<Reg>>(
@@ -203,6 +213,26 @@ internal class USetMemoryRegion<SetType, ElementSort : USort, Reg : Region<Reg>>
203213
},
204214
)
205215

216+
override fun initializeAllocatedSet(
217+
address: UConcreteHeapAddress,
218+
setType: SetType,
219+
sort: ElementSort,
220+
content: Set<UExpr<ElementSort>>,
221+
operationGuard: UBoolExpr,
222+
ownership: MutabilityOwnership,
223+
makeDisjointCheck: Boolean,
224+
): USetRegion<SetType, ElementSort, Reg> {
225+
val setId = UAllocatedSetId(address, elementSort, setType, elementInfo)
226+
val newCollection = setId.initializedSet(content, operationGuard, makeDisjointCheck)
227+
return USetMemoryRegion(
228+
setType,
229+
elementSort,
230+
elementInfo,
231+
allocatedSets.put(setId, newCollection, ownership),
232+
inputSet
233+
)
234+
}
235+
206236
override fun setEntries(ref: UHeapRef): UPrimitiveSetEntries<SetType, ElementSort, Reg> =
207237
foldHeapRefWithStaticAsSymbolic(
208238
ref = ref,

usvm-core/src/main/kotlin/org/usvm/collection/set/primitive/USymbolicSetId.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.usvm.collection.set.primitive
22

33
import io.ksmt.cache.hash
44
import io.ksmt.utils.uncheckedCast
5+
import kotlinx.collections.immutable.toPersistentMap
56
import org.usvm.UBoolExpr
67
import org.usvm.UBoolSort
78
import org.usvm.UComposer
@@ -13,12 +14,15 @@ import org.usvm.collection.set.USymbolicSetElement
1314
import org.usvm.collection.set.USymbolicSetElementRegion
1415
import org.usvm.collection.set.USymbolicSetKeyInfo
1516
import org.usvm.memory.ULValue
17+
import org.usvm.memory.UPinpointUpdateNode
1618
import org.usvm.memory.USymbolicCollection
1719
import org.usvm.memory.USymbolicCollectionId
1820
import org.usvm.memory.USymbolicCollectionKeyInfo
1921
import org.usvm.memory.UTreeUpdates
22+
import org.usvm.memory.UUpdateNode
2023
import org.usvm.memory.UWritableMemory
2124
import org.usvm.regions.Region
25+
import org.usvm.regions.RegionTree
2226
import org.usvm.regions.emptyRegionTree
2327
import org.usvm.uctx
2428
import java.util.IdentityHashMap
@@ -115,6 +119,40 @@ class UAllocatedSetId<SetType, ElementSort : USort, Reg : Region<Reg>>(
115119
}
116120

117121
override fun hashCode(): Int = hash(setAddress, setType, elementSort)
122+
123+
fun initializedSet(
124+
content: Set<UExpr<ElementSort>>,
125+
guard: UBoolExpr,
126+
makeDisjointCheck: Boolean = false, // for debug purposes
127+
): USymbolicCollection<UAllocatedSetId<SetType, ElementSort, Reg>, UExpr<ElementSort>, UBoolSort> {
128+
val ctx = guard.ctx
129+
var unionRegion = elementInfo.bottomRegion()
130+
val entries = content.map { value ->
131+
val update = UPinpointUpdateNode(value, elementInfo, ctx.trueExpr, guard)
132+
133+
val region = elementInfo.keyToRegion(value)
134+
135+
if (makeDisjointCheck) {
136+
check(unionRegion.compare(region) == Region.ComparisonResult.DISJOINT) {
137+
"Cannot create initializedSet if regions of given elements intersect"
138+
}
139+
unionRegion = unionRegion.union(region)
140+
}
141+
142+
region to update
143+
}
144+
145+
val map = entries.associate {
146+
it.first to (it.second to emptyRegionTree<Reg, UUpdateNode<UExpr<ElementSort>, UBoolSort>>())
147+
}.toPersistentMap()
148+
149+
val updates = UTreeUpdates(
150+
updates = RegionTree(map),
151+
keyInfo()
152+
)
153+
154+
return USymbolicCollection(this, updates)
155+
}
118156
}
119157

120158
class UInputSetId<SetType, ElementSort : USort, Reg : Region<Reg>>(

0 commit comments

Comments
 (0)