Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,38 @@ 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 perm = this.perm;
const n = perm.length;
return new Array<number>(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<CallsClassInputCandidate>(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 = """
Expand Down
60 changes: 60 additions & 0 deletions usvm-ts/src/main/kotlin/org/usvm/machine/expr/ReadField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ 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.collection.field.UInputFieldReading
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
Expand All @@ -43,11 +46,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
Expand Down Expand Up @@ -106,6 +111,16 @@ internal fun TsExprResolver.handleInstanceFieldRef(
val isObjectProperty = storageType is EtsClassType || storageType is EtsUnclearRefType ||
scope.calcOnState { trackedObjectProperties.any { it.instance == instance && it.name == "length" } }
if (!isObjectProperty) {
// 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(
scope = scope,
instanceLocal = instanceLocal,
Expand All @@ -120,6 +135,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?,
Expand Down
Loading