diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 4e94f7b95a..b391ff5b20 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -6,7 +6,7 @@ object Versions { const val clikt = "5.0.0" const val detekt = "1.23.7" const val ini4j = "0.5.4" - const val jacodb = "0fac32c8c5" + const val jacodb = "5fb86717" const val juliet = "1.3.2" const val junit = "5.9.3" const val kotlin = "2.1.0" diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/ForwardedRestArguments.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/ForwardedRestArguments.kt new file mode 100644 index 0000000000..59c85f1405 --- /dev/null +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/ForwardedRestArguments.kt @@ -0,0 +1,45 @@ +package org.usvm.machine.interpreter + +import io.ksmt.utils.asExpr +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsMethod +import org.jacodb.ets.utils.CONSTRUCTOR_NAME +import org.usvm.machine.TsConcreteMethodCallStmt +import org.usvm.machine.TsContext +import org.usvm.machine.expr.mkNotNullOrUndefined +import org.usvm.util.arrayStorageType + +internal fun ensureForwardedRestIsPacked( + ctx: TsContext, + scope: TsStepScope, + stmt: TsConcreteMethodCallStmt, + callee: EtsMethod, +): Boolean = with(ctx) { + val restParameter = callee.parameters.lastOrNull() + val expectsPackedArguments = stmt.location.method.name == CONSTRUCTOR_NAME && + callee.name == CONSTRUCTOR_NAME && restParameter?.isRest == true && + stmt.args.size == callee.parameters.size + if (!expectsPackedArguments) { + throw UnsupportedOperationException("Invalid rest-argument forwarding to ${callee.signature}") + } + + val argument = stmt.args.last() + if (argument.sort != addressSort) { + throw UnsupportedOperationException("Forwarded rest arguments need array storage for ${callee.signature}") + } + + val reference = argument.asExpr(addressSort) + val arrayType = scope.calcOnState { + arrayStorageType(reference, requireNotNull(restParameter).type) + } as? EtsArrayType + val isPacked = if (arrayType != null) { + scope.calcOnState { + mkAnd(mkNotNullOrUndefined(reference), memory.types.evalIsSubtype(reference, arrayType)) + } + } else { + falseExpr + } + return scope.fork(isPacked, blockOnFalseState = { + terminateAsUnsupported(reason = "Forwarded rest arguments need array storage for ${callee.signature}") + }) != null +} diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInterpreter.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInterpreter.kt index 22865401ac..de3d59598b 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInterpreter.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInterpreter.kt @@ -14,6 +14,7 @@ import org.jacodb.ets.model.EtsCopyDataPropertiesStmt import org.jacodb.ets.model.EtsDefineAccessorStmt import org.jacodb.ets.model.EtsDefineDataPropertyStmt import org.jacodb.ets.model.EtsIfStmt +import org.jacodb.ets.model.EtsInstanceCallExpr import org.jacodb.ets.model.EtsInstanceFieldRef import org.jacodb.ets.model.EtsLValue import org.jacodb.ets.model.EtsLocal @@ -462,6 +463,9 @@ class TsInterpreter( return } + val forwardsRestArguments = (stmt.call as? EtsInstanceCallExpr)?.forwardRestArguments == true + if (forwardsRestArguments && !ensureForwardedRestIsPacked(ctx, scope, stmt, callee)) return + scope.doWithState { registerCallee(stmt.returnSite, callee.cfg) @@ -485,7 +489,11 @@ class TsInterpreter( // g() -> g(undefined, undefined) // g(1, 2, 3) -> g(1, 2) - if (callee.parameters.isNotEmpty() && callee.parameters.last().isRest) { + val hasRestParameter = callee.parameters.lastOrNull()?.isRest == true + if (forwardsRestArguments) { + // The synthetic caller cannot observe or expose its rest array. + args += stmt.args + } else if (hasRestParameter) { // vararg call // first n-1 args are normal diff --git a/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SuperConstructorCall.kt b/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SuperConstructorCall.kt index 382d10b0a8..ecc64c157f 100644 --- a/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SuperConstructorCall.kt +++ b/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SuperConstructorCall.kt @@ -49,6 +49,17 @@ class SuperConstructorCall : TsMethodTestRunner() { ) } + @Test + fun `implicit constructors forward empty and populated rest parameters through the superclass chain`() { + val method = getMethod(methodName = "implicitRestSuper") + + discoverProperties( + method = method, + { result -> result eq 326401 }, + invariants = arrayOf({ result -> result eq 326401 }), + ) + } + @Test fun `allocated receiver initializes every declared superclass in order`() { val method = getMethod(methodName = "nestedSuper") @@ -132,6 +143,22 @@ class SuperConstructorCall : TsMethodTestRunner() { assertEquals(expected = "constructor", actual = calls.single().callee.name) } + @Test + fun `default constructor forwards a symbolic packed array on direct entry`() { + val method = getMethod(methodName = "constructor", className = "SymbolicRestChild") + val constructorOptions = options.copy(timeout = 5.seconds, stepsFromLastCovered = 200L) + + val tests = runner(method, constructorOptions) + + assertTrue(tests.isNotEmpty()) + for (test in tests) { + val packed = assertIs>(test.before.parameters.single()) + val result = assertIs(test.returnValue) + val observedLength = assertIs(result.properties["observedLength"]) + assertEquals(packed.values.size.toDouble(), observedLength.number) + } + } + private fun replayConstructor(className: String, expectedValue: Int) { val source = getResourcePath("/samples/lang/SuperConstructorCall.ts").readText() val script = source + "\nif (new $className().value !== $expectedValue) throw Error('constructor replay');" diff --git a/usvm-ts/src/test/resources/samples/lang/SuperConstructorCall.ts b/usvm-ts/src/test/resources/samples/lang/SuperConstructorCall.ts index d37644eee5..dc6612490c 100644 --- a/usvm-ts/src/test/resources/samples/lang/SuperConstructorCall.ts +++ b/usvm-ts/src/test/resources/samples/lang/SuperConstructorCall.ts @@ -11,6 +11,13 @@ class SuperConstructorCall { return subject.value; } + static implicitRestSuper(): number { + const withRest = new RestConstructorLeaf(3, 2, 1); + const withoutRest = new RestConstructorLeaf(4); + + return 1000 * withRest.result + withoutRest.result; + } + static nestedSuper(): number { const subject = new SymbolicConstructorChild(); return subject.value; @@ -67,3 +74,30 @@ class SymbolicConstructorChild extends SymbolicConstructorMiddle { } class SymbolicImplicitConstructorChild extends SymbolicConstructorMiddle {} + +class RestConstructorParent { + total: number; + + constructor(prefix: number, ...values: Args) { + this.total = prefix * 100 + values.length * 10; + if (values.length > 0) { + this.total += 2 * values[0] + values[1]; + } + } +} + +class RestConstructorMiddle extends RestConstructorParent {} + +class RestConstructorLeaf extends RestConstructorMiddle { + result: number = this.total + 1; +} + +class SymbolicRestParent { + size: number; + + constructor(...values: number[]) { this.size = values.length; } +} + +class SymbolicRestChild extends SymbolicRestParent { + observedLength = this.size; +}