Skip to content
Draft
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
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TsTestValue.TsNumber>(
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")
Expand Down Expand Up @@ -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<TsTestValue.TsArray<*>>(test.before.parameters.single())
val result = assertIs<TsTestValue.TsClass>(test.returnValue)
val observedLength = assertIs<TsTestValue.TsNumber>(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');"
Expand Down
34 changes: 34 additions & 0 deletions usvm-ts/src/test/resources/samples/lang/SuperConstructorCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,3 +74,30 @@ class SymbolicConstructorChild extends SymbolicConstructorMiddle {
}

class SymbolicImplicitConstructorChild extends SymbolicConstructorMiddle {}

class RestConstructorParent<Args extends number[]> {
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<number[]> {}

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;
}
Loading