Skip to content

Commit ee6ce04

Browse files
committed
Preserve bounded UTF-16 backing and model global Array.isArray
1 parent 9f2ac2e commit ee6ce04

18 files changed

Lines changed: 576 additions & 24 deletions

‎usvm-ts/src/main/kotlin/org/usvm/machine/call/TsBuiltinGlobalType.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.usvm.machine.call
22

33
import org.jacodb.ets.model.EtsAnyType
4+
import org.jacodb.ets.model.EtsArrayType
45
import org.jacodb.ets.model.EtsClassSignature
56
import org.jacodb.ets.model.EtsFunctionType
67
import org.jacodb.ets.model.EtsLocal
@@ -41,3 +42,7 @@ private fun EtsValue.isLegacyBuiltinGlobal(
4142
name == expectedName &&
4243
type == EtsUnknownType &&
4344
callee.enclosingClass == EtsClassSignature.UNKNOWN.copy(name = expectedName)
45+
46+
internal fun EtsType.isBuiltinArrayGlobalType(): Boolean = this is EtsFunctionType &&
47+
signature.returnType is EtsArrayType &&
48+
signature.enclosingClass.file == EtsClassSignature.UNKNOWN.file
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.usvm.machine.call.intrinsic
2+
3+
import io.ksmt.utils.asExpr
4+
import org.jacodb.ets.model.EtsArrayType
5+
import org.jacodb.ets.model.EtsFunctionType
6+
import org.jacodb.ets.model.EtsLocal
7+
import org.jacodb.ets.model.EtsUnknownType
8+
import org.usvm.UBoolExpr
9+
import org.usvm.UExpr
10+
import org.usvm.UHeapRef
11+
import org.usvm.machine.call.TsUnknownCall
12+
import org.usvm.machine.call.TsUnknownCallModelCompletion
13+
import org.usvm.machine.call.TsUnknownCallModelExecution
14+
import org.usvm.machine.call.TsUnknownCallModelSuccessor
15+
import org.usvm.machine.call.TsUnknownCallTarget
16+
import org.usvm.machine.state.TsState
17+
18+
/** Exact `Array.isArray` semantics for the genuine global `Array` built-in. */
19+
internal object TsArrayIsArrayIntrinsicModel : TsBuiltInUnknownCallModel {
20+
const val MODEL_ID: String = "ts.array.isArray"
21+
22+
private val anyArrayType = EtsArrayType(elementType = EtsUnknownType, dimensions = 1)
23+
24+
override val id: String = MODEL_ID
25+
override val target = TsUnknownCallTarget(
26+
methodName = "isArray",
27+
)
28+
29+
override fun apply(state: TsState, call: TsUnknownCall): TsUnknownCallModelExecution? {
30+
if (!call.hasGlobalArrayOwner()) {
31+
return null
32+
}
33+
34+
val value = when {
35+
call.arguments.isEmpty() -> return state.normalExecution(state.ctx.falseExpr)
36+
else -> call.arguments.first().resolved ?: return null
37+
}
38+
val result = state.isArray(value)
39+
40+
return state.normalExecution(result)
41+
}
42+
43+
private fun TsUnknownCall.hasGlobalArrayOwner(): Boolean {
44+
val owner = receiver?.source as? EtsLocal ?: return false
45+
val ownerType = owner.type as? EtsFunctionType ?: return false
46+
val signatureFile = ownerType.signature.enclosingClass.file
47+
48+
return owner.name == "Array" &&
49+
ownerType.signature.returnType is EtsArrayType &&
50+
signatureFile.projectName == UNKNOWN_SIGNATURE_COMPONENT &&
51+
signatureFile.fileName == UNKNOWN_SIGNATURE_COMPONENT
52+
}
53+
54+
private fun TsState.isArray(value: UExpr<*>) = with(ctx) {
55+
when {
56+
value.isFakeObject() -> {
57+
val fakeType = value.getFakeType(memory)
58+
val reference = value.extractRef(memory)
59+
60+
mkAnd(
61+
fakeType.refTypeExpr,
62+
isNonNullArrayReference(reference),
63+
)
64+
}
65+
66+
value.sort == addressSort -> isNonNullArrayReference(value.asExpr(addressSort))
67+
else -> falseExpr
68+
}
69+
}
70+
71+
private fun TsState.isNonNullArrayReference(reference: UHeapRef): UBoolExpr = with(ctx) {
72+
val isDefined = mkNot(mkEq(reference, mkUndefinedValue()))
73+
val isNonNull = mkNot(mkEq(reference, mkTsNullValue()))
74+
75+
mkAnd(isDefined, isNonNull, memory.types.evalIsSubtype(reference, anyArrayType))
76+
}
77+
78+
private fun TsState.normalExecution(result: UExpr<*>): TsUnknownCallModelExecution = with(ctx) {
79+
val successor = TsUnknownCallModelSuccessor(
80+
guard = trueExpr,
81+
completion = TsUnknownCallModelCompletion.Normal { result },
82+
)
83+
84+
TsUnknownCallModelExecution(successors = listOf(successor))
85+
}
86+
87+
private const val UNKNOWN_SIGNATURE_COMPONENT: String = "%unk"
88+
}

‎usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private fun TsContext.materializeTypedStringField(
394394
val activeStringConstraints = mkImplies(activeGuard, constraints)
395395
scope.assert(activeStringConstraints) ?: return null
396396

397-
scope.doWithState { boundedStringBackingRefs += stringRef }
397+
scope.doWithState { boundedStringBackingRefs += stringRef to maxStringLength }
398398

399399
return stringRef
400400
}

‎usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadLength.kt‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import org.usvm.machine.interpreter.TsStepScope
1717
import org.usvm.sizeSort
1818
import org.usvm.util.arrayStorageType
1919
import org.usvm.util.mkArrayLengthLValue
20-
import org.usvm.util.mkStringBackingLValue
21-
import org.usvm.util.mkStringBackingLengthLValue
20+
import org.usvm.util.stringLength
2221

2322
// Handles reading the `length` property.
2423
fun TsContext.readLengthProperty(
@@ -29,6 +28,19 @@ fun TsContext.readLengthProperty(
2928
): UExpr<*>? {
3029
// Determine the array type.
3130
val storageType = scope.calcOnState { arrayStorageType(instance, instanceLocal.type) }
31+
if (storageType is EtsStringType) {
32+
val length = scope.calcOnState { stringLength(instance) }
33+
val lengthBound = scope.calcOnState { boundedStringBackingRefs[instance] ?: maxArraySize }
34+
ensureLengthBounds(scope, length, lengthBound) ?: return null
35+
36+
return mkBvToFpExpr(
37+
sort = fp64Sort,
38+
roundingMode = fpRoundingModeSortDefaultValue(),
39+
value = length.asExpr(sizeSort),
40+
signed = true,
41+
)
42+
}
43+
3244
val arrayType: EtsArrayType = when (val type = storageType) {
3345
is EtsArrayType -> type
3446

@@ -38,18 +50,6 @@ fun TsContext.readLengthProperty(
3850
EtsArrayType(EtsUnknownType, dimensions = 1)
3951
}
4052

41-
is EtsStringType -> {
42-
val charsRef = scope.calcOnState {
43-
memory.read(mkStringBackingLValue(instance))
44-
}
45-
46-
return readArrayLength(
47-
scope = scope,
48-
lengthLValue = mkStringBackingLengthLValue(charsRef),
49-
maxArraySize = maxArraySize,
50-
)
51-
}
52-
5353
else -> error("Expected EtsArrayType, EtsAnyType or EtsUnknownType, but got: $type")
5454
}
5555

‎usvm-ts/src/main/kotlin/org/usvm/machine/expr/TsCapturedInstanceCall.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.usvm.USort
1919
import org.usvm.api.allocateConcreteRef
2020
import org.usvm.isAllocatedConcreteHeapRef
2121
import org.usvm.isTrue
22+
import org.usvm.machine.call.isBuiltinArrayGlobalType
2223
import org.usvm.machine.call.isBuiltinGlobalType
2324
import org.usvm.machine.state.lastStmt
2425
import org.usvm.util.getAllFields
@@ -158,6 +159,10 @@ private fun isImplicitModeledGlobal(source: EtsLocal, callee: EtsMethodSignature
158159
return true
159160
}
160161

162+
if (source.type.isBuiltinArrayGlobalType()) {
163+
return true
164+
}
165+
161166
val modeled = when (source.name) {
162167
"Logger" -> true
163168
"Number" -> callee.name == "isNaN"

‎usvm-ts/src/main/kotlin/org/usvm/machine/expr/TsExprResolver.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ class TsExprResolver(
774774
guard = trueExpr,
775775
)
776776
memory.write(mkStringBackingLengthLValue(resultChars), resultLength, guard = trueExpr)
777-
boundedStringBackingRefs += result
777+
boundedStringBackingRefs += result to options.maxArraySize
778778

779779
memory.memcpy(
780780
srcRef = leftChars,

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.usvm.StepResult
4040
import org.usvm.StepScope
4141
import org.usvm.UExpr
4242
import org.usvm.UInterpreter
43+
import org.usvm.USort
4344
import org.usvm.api.evalTypeEquals
4445
import org.usvm.api.initializeArray
4546
import org.usvm.api.targets.TsTarget
@@ -775,7 +776,12 @@ class TsInterpreter(
775776
unknownCallDispatcher = unknownCallDispatcher,
776777
)
777778

778-
fun getInitialState(method: EtsMethod, targets: List<TsTarget>): TsState = with(ctx) {
779+
fun getInitialState(
780+
method: EtsMethod,
781+
targets: List<TsTarget>,
782+
configure: (TsState) -> Unit = {},
783+
parameterSortOverride: (Int) -> USort? = { null },
784+
): TsState = with(ctx) {
779785
val state = TsState(
780786
ctx = ctx,
781787
ownership = MutabilityOwnership(),
@@ -803,6 +809,11 @@ class TsInterpreter(
803809

804810
method.parameters.forEachIndexed { i, param ->
805811
val idx = i + 1 // +1 because 0 is reserved for `this`
812+
val overriddenSort = parameterSortOverride(idx)
813+
if (overriddenSort != null) {
814+
state.saveSortForLocal(idx, overriddenSort)
815+
return@forEachIndexed
816+
}
806817

807818
val ref by lazy {
808819
val lValue = mkRegisterStackLValue(addressSort, idx)
@@ -851,7 +862,7 @@ class TsInterpreter(
851862
state.pathConstraints += state.memory.types.evalTypeEquals(ref, EtsStringType)
852863

853864
state.pathConstraints += state.symbolicStringBackingConstraint(ref)
854-
state.boundedStringBackingRefs += ref
865+
state.boundedStringBackingRefs += ref to state.maxStringLength
855866
}
856867

857868
val parameterSort = typeToSort(parameterType)
@@ -869,6 +880,8 @@ class TsInterpreter(
869880
}
870881
}
871882

883+
configure(state)
884+
872885
val solver = solver<EtsType>()
873886
val model = solver.check(state.pathConstraints).ensureSat().model
874887
state.models = listOf(model)

‎usvm-ts/src/main/kotlin/org/usvm/machine/operator/TsBinaryOperator.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ private fun TsContext.stringValueEquals(
8686
rhsChars = rhsChars,
8787
lhsLength = lhsLength,
8888
rhsLength = rhsLength,
89-
maxLength = maxStringLength,
89+
maxLength = maxOf(
90+
boundedStringBackingRefs[lhs] ?: maxStringLength,
91+
boundedStringBackingRefs[rhs] ?: maxStringLength,
92+
),
9093
)
9194
}
9295

@@ -119,7 +122,7 @@ private fun TsContext.stringValueEquals(
119122
boundedLengths,
120123
)
121124
scope.fork(supported, blockOnFalseState = {
122-
terminateAsUnsupported(reason = "String equality requires symbolic string length in 0..$maxStringLength")
125+
terminateAsUnsupported(reason = "String equality requires symbolic string length in 0..${comparison.maxLength}")
123126
}) ?: return null
124127

125128
// Known literals supply a tighter comparison bound; all other string lengths are constrained above.

‎usvm-ts/src/main/kotlin/org/usvm/machine/state/TsState.kt‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.usvm.machine.state
22

3+
import org.jacodb.ets.model.EtsArrayType
34
import org.jacodb.ets.model.EtsBlockCfg
45
import org.jacodb.ets.model.EtsClass
56
import org.jacodb.ets.model.EtsFile
@@ -32,6 +33,7 @@ import org.usvm.machine.interpreter.PromiseState
3233
import org.usvm.machine.interpreter.TsFunction
3334
import org.usvm.memory.ULValue
3435
import org.usvm.memory.UMemory
36+
import org.usvm.memory.UReadOnlyMemoryRegion
3537
import org.usvm.model.UModelBase
3638
import org.usvm.sizeSort
3739
import org.usvm.targets.UTargetsSet
@@ -87,11 +89,11 @@ class TsState(
8789
*/
8890
var stringConstantAllocatedRefs: UPersistentHashMap<String, UConcreteHeapRef> = persistentHashMapOf(),
8991
/**
90-
* References whose string backing and length bound have been modeled.
92+
* Maps references with modeled string backing to their maximum length.
9193
* A new symbolic string producer must register its reference here after creating the backing model.
9294
* String literals are recognized separately through [TsContext.getStringConstantValue].
9395
*/
94-
var boundedStringBackingRefs: Set<UHeapRef> = emptySet(),
96+
var boundedStringBackingRefs: Map<UHeapRef, Int> = emptyMap(),
9597
/** Unresolved reference payloads that may acquire string backing after type refinement. */
9698
var symbolicStringCandidates: Set<UHeapRef> = emptySet(),
9799
/** Exception delivered to the current catch block, before its binding is initialized. */
@@ -100,6 +102,7 @@ class TsState(
100102
var trackedObjectProperties: Set<TrackedObjectProperty> = emptySet(),
101103
var writtenConcreteFields: Set<Pair<UHeapRef, String>> = emptySet(),
102104
var writtenObjectLiteralFieldSorts: UPersistentHashMap<Pair<UHeapRef, String>, USort> = persistentHashMapOf(),
105+
internal val denseInputArrays: MutableMap<UConcreteHeapRef, TsDenseInputArray> = mutableMapOf(),
103106
private val activeUnknownCallModels: MutableList<Pair<String, Int>> = mutableListOf(),
104107
) : UState<EtsType, EtsMethod, EtsStmt, TsContext, TsTarget, TsState>(
105108
ctx = ctx,
@@ -300,6 +303,7 @@ class TsState(
300303
ref
301304
}
302305
stringConstantAllocatedRefs = updated
306+
boundedStringBackingRefs += result to value.length
303307
result
304308
}
305309

@@ -347,10 +351,18 @@ class TsState(
347351
trackedObjectProperties = trackedObjectProperties,
348352
writtenConcreteFields = writtenConcreteFields,
349353
writtenObjectLiteralFieldSorts = writtenObjectLiteralFieldSorts,
354+
denseInputArrays = denseInputArrays.toMutableMap(),
350355
activeUnknownCallModels = activeUnknownCallModels.toMutableList(),
351356
)
352357
}
353358

354359
override val isExceptional: Boolean
355360
get() = methodResult is TsMethodResult.TsException
356361
}
362+
363+
/** Snapshot of the storage regions that establish dense input-array contents. */
364+
data class TsDenseInputArray(
365+
val type: EtsArrayType,
366+
val lengthRegion: UReadOnlyMemoryRegion<*, *>,
367+
val elementRegion: UReadOnlyMemoryRegion<*, *>,
368+
)

‎usvm-ts/src/main/kotlin/org/usvm/machine/state/TsStringBacking.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal fun TsStepScope.prepareRefinedStringBackings(): Unit? {
7070
val backingConstraint = calcOnState { symbolicStringBackingConstraint(ref) }
7171
assert(backingConstraint) ?: return null
7272
doWithState {
73-
boundedStringBackingRefs += ref
73+
boundedStringBackingRefs += ref to maxStringLength
7474
symbolicStringCandidates -= ref
7575
}
7676
}

0 commit comments

Comments
 (0)