From 6373d130b59087650d78a73881f6f7d2824db8d8 Mon Sep 17 00:00:00 2001 From: Aleksei Menshutin Date: Fri, 4 Dec 2026 05:49:46 +0100 Subject: [PATCH 1/2] Resolve symbolic field length with runtime storage --- .../usvm/ts/calls/CallsClassStartSmokeTest.kt | 31 +++++++++++ .../kotlin/org/usvm/machine/expr/ReadField.kt | 55 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt b/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt index 5513447ae6..1cb6d4b3cb 100644 --- a/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt +++ b/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt @@ -384,6 +384,37 @@ class CallsClassStartSmokeTest { assertFalse(replay.nativeCoverageAvailable) } + @Test + fun `saved input replays array length read through a symbolic field`() { + val source = """ + export class Target { + private perm: number[]; + + constructor(perm: number[]) { + this.perm = perm; + } + + run(): number { + const n = this.perm.length; + return new Array(n * 0).length; + } + } + """.trimIndent() + + val result = runFixture(sourceText = source, exploratory = true, profile = CallsExperimentProfile.FROZEN_FRESH) + val candidates = Files.readAllLines(directory.resolve("output/input-candidates.jsonl")).map { + CallsExperimentJson.json.decodeFromString(it) + } + val returned = candidates.filter { + it.methodName == "run" && it.completion == CallsCoverageCompletion.RETURNED && it.environmentComplete + }.distinctBy { it.inputs } + + assertTrue(returned.isNotEmpty(), "Expected a replayable graph from the modeled length read") + for (candidate in returned) { + assertSavedInputReturned(result = result, candidate = candidate, expected = 0.0) + } + } + @Test fun `saved sparse array replay preserves missing slots and own undefined`() { val source = """ diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt index 55ce3f7550..0cb248699c 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt @@ -28,10 +28,12 @@ import org.usvm.USort import org.usvm.USymbolicHeapRef import org.usvm.api.evalTypeEquals import org.usvm.api.makeSymbolicRefUntyped +import org.usvm.api.typeStreamOf import org.usvm.isAllocatedConcreteHeapRef import org.usvm.isFalse import org.usvm.isTrue import org.usvm.machine.TsContext +import org.usvm.machine.TsRuntimeFeatureLimitationReason import org.usvm.machine.interpreter.TsStepScope import org.usvm.machine.interpreter.ensureStaticsInitialized import org.usvm.machine.state.recordInitialStaticBinding @@ -43,11 +45,13 @@ import org.usvm.machine.types.iteWriteIntoFakeObject import org.usvm.machine.types.mkFakeValue import org.usvm.machine.types.requiresConstructorWitness import org.usvm.machine.types.toAuxiliaryType +import org.usvm.types.firstOrNull import org.usvm.util.EtsHierarchy import org.usvm.util.InputPropertyDeclaration import org.usvm.util.TsResolutionResult import org.usvm.util.arrayStorageType import org.usvm.util.createFakeField +import org.usvm.util.mkArrayLengthLValue import org.usvm.util.mkFieldLValue import org.usvm.util.mkStringBackingElementLValue import org.usvm.util.mkStringBackingLValue @@ -106,6 +110,12 @@ internal fun TsExprResolver.handleInstanceFieldRef( val isObjectProperty = storageType is EtsClassType || storageType is EtsUnclearRefType || scope.calcOnState { trackedObjectProperties.any { it.instance == instance && it.name == "length" } } if (!isObjectProperty) { + val knownStorage = scope.calcOnState { arrayStorageType(instance, EtsUnknownType) } + val needsRuntimeType = storageType is EtsArrayType && knownStorage !is EtsArrayType + if (needsRuntimeType) { + return readLengthOfSymbolicArrayField(instanceLocal, instance, value.field) + } + return readLengthProperty( scope = scope, instanceLocal = instanceLocal, @@ -120,6 +130,51 @@ internal fun TsExprResolver.handleInstanceFieldRef( return resolveField(scope, instanceLocal, instance, value.field, hierarchy) } +private fun TsExprResolver.readLengthOfSymbolicArrayField( + instanceLocal: EtsLocal, + instance: UHeapRef, + field: EtsFieldSignature, +): UExpr<*>? = with(ctx) { + val runtimeType = scope.calcOnState { + val model = models.first() + model.typeStreamOf(model.eval(instance)).firstOrNull() + } + if (runtimeType == null) { + stopRuntimeFeatureLimitation( + reason = TsRuntimeFeatureLimitationReason.ARRAY_STORAGE_TYPE, + detail = "No runtime type was resolved for the length receiver", + ) + return null + } + + if (runtimeType is EtsArrayType) { + val sameRuntimeType = scope.calcOnState { memory.types.evalTypeEquals(instance, runtimeType) } + scope.fork(sameRuntimeType) ?: return null + return readArrayLength( + scope = scope, + lengthLValue = mkArrayLengthLValue(instance, runtimeType), + maxArraySize = options.maxArraySize, + ) + } + + val isString = scope.calcOnState { memory.types.evalTypeEquals(instance, EtsStringType) } + if (runtimeType == EtsStringType) { + scope.fork(isString) ?: return null + val string = materializeTypedStringField( + scope = scope, + value = instance, + maxStringLength = scope.calcOnState { maxStringLength }, + ) ?: return null + val stringLocal = EtsLocal(name = instanceLocal.name, type = EtsStringType) + return readLengthProperty(scope, stringLocal, string, options.maxArraySize, ::reportRuntimeFeatureLimitation) + } + + val anyArray = EtsArrayType(elementType = EtsUnknownType, dimensions = 1) + val isArray = scope.calcOnState { memory.types.evalIsSubtype(instance, anyArray) } + scope.fork(mkNot(mkOr(isArray, isString))) ?: return null + return resolveField(scope, instanceLocal = null, instance, field, hierarchy) +} + internal fun TsContext.resolveField( scope: TsStepScope, instanceLocal: EtsLocal?, From cbe12414a5e4369d9c3eeb5b772f3e39a64533fc Mon Sep 17 00:00:00 2001 From: Aleksei Menshutin Date: Fri, 4 Dec 2026 07:05:00 +0100 Subject: [PATCH 2/2] Limit array length dispatch to symbolic field reads --- .../org/usvm/ts/calls/CallsClassStartSmokeTest.kt | 3 ++- .../main/kotlin/org/usvm/machine/expr/ReadField.kt | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt b/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt index 1cb6d4b3cb..905908ffd5 100644 --- a/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt +++ b/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsClassStartSmokeTest.kt @@ -395,7 +395,8 @@ class CallsClassStartSmokeTest { } run(): number { - const n = this.perm.length; + const perm = this.perm; + const n = perm.length; return new Array(n * 0).length; } } diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt index 0cb248699c..b19a91153f 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt @@ -29,6 +29,7 @@ import org.usvm.USymbolicHeapRef import org.usvm.api.evalTypeEquals import org.usvm.api.makeSymbolicRefUntyped import org.usvm.api.typeStreamOf +import org.usvm.collection.field.UInputFieldReading import org.usvm.isAllocatedConcreteHeapRef import org.usvm.isFalse import org.usvm.isTrue @@ -110,10 +111,14 @@ internal fun TsExprResolver.handleInstanceFieldRef( val isObjectProperty = storageType is EtsClassType || storageType is EtsUnclearRefType || scope.calcOnState { trackedObjectProperties.any { it.instance == instance && it.name == "length" } } if (!isObjectProperty) { - val knownStorage = scope.calcOnState { arrayStorageType(instance, EtsUnknownType) } - val needsRuntimeType = storageType is EtsArrayType && knownStorage !is EtsArrayType - if (needsRuntimeType) { - return readLengthOfSymbolicArrayField(instanceLocal, instance, value.field) + // Entry arrays already have length storage selected from the parameter type. + // A symbolic field can hold a non-array even when its declaration names an array. + val readsSymbolicField = storageType is EtsArrayType && instance is UInputFieldReading<*, *> + if (readsSymbolicField) { + val knownStorage = scope.calcOnState { arrayStorageType(instance, EtsUnknownType) } + if (knownStorage !is EtsArrayType) { + return readLengthOfSymbolicArrayField(instanceLocal, instance, value.field) + } } return readLengthProperty(