diff --git a/usvm-ts-fast-check/DESIGN.md b/usvm-ts-fast-check/DESIGN.md index 533114e697..7acd3fe165 100644 --- a/usvm-ts-fast-check/DESIGN.md +++ b/usvm-ts-fast-check/DESIGN.md @@ -247,6 +247,21 @@ useful separation is preserved: declarative receiver/argument/result positions a values, and condition interpretation is distinct from position resolution. The TypeScript mapper expresses this with EtsIR-specific binding and mapping records and has no dependency on `usvm-jvm` or the taint-analysis module. +## USVM projection and property search + +The existing projection path configures `TsMachine`'s initial state from exact mapper bindings and declared Kotlin +domains. The existing search path prepends a mapped synchronous precondition to the predicate entry point. Guard +completion is explicit in `TsState`: false terminates a rejected path, while an exception or non-boolean result +terminates an error path. Neither path can reach the predicate target. + +Predicate false paths are re-solved on a cloned terminal state before target propagation, so the ordinary terminal +state is not rewritten. Predicate exceptions reach the same candidate target. Runtime non-boolean entry-point +results are property errors regardless of their TypeScript return annotation. A residual call that stops a path is +unsupported, while ordinary unsatisfiable path pruning is not an engine failure. Timeout, solver uncertainty, +interpreter failure, and candidate-input resolution failure retain separate search outcomes. Candidate extraction +reads the projected input state with the terminal model, so predicate-local array mutation does not rewrite the +reported input. + ## Process supervision `FastCheckProcessTransport` writes stdin and drains stdout and stderr concurrently. This is necessary because each @@ -286,7 +301,9 @@ classifier because `tsx` depends on a native esbuild package. - Backend integration tests execute real uncompiled TypeScript through the packaged adapter, including replay, shrinking, explicit examples, preconditions, async predicates, and timeouts. - Shared contract fixtures cover precondition admission, discard and errors; predicate violations and errors; - special values; aliases; mutation isolation; shrinking; and replay through observable outcomes. + special values; aliases; mutation isolation; shrinking; and replay through observable outcomes. One focused JVM + conformance test executes the same classification fixture through the real FastCheck and USVM paths, including + literal and never return annotations and replay of a pre-mutation USVM candidate. - Coverage golden tests assert literal TypeScript statement and branch outcomes for successful and falsified runs, cross-property isolation, scope and glob filtering, and source-map/report diagnostics. - Mapping golden tests load stable TypeScript fixtures through the native frontend and cover predicate, @@ -299,5 +316,5 @@ classifier because `tsx` depends on a native esbuild package. - Discovering properties by scanning TypeScript source roots. - Compiling user TypeScript as part of the PBT workflow. - Reimplementing generation, replay, skip accounting, or shrinking in Kotlin. -- Constructing symbolic inputs or executing mapped properties in USVM. +- General purity analysis, arbitrary mutable-object projection, and persistent state across property invocations. - Combining backend source coverage with future EtsIR replay coverage. diff --git a/usvm-ts-fast-check/build.gradle.kts b/usvm-ts-fast-check/build.gradle.kts index bd10be1065..ee25740b87 100644 --- a/usvm-ts-fast-check/build.gradle.kts +++ b/usvm-ts-fast-check/build.gradle.kts @@ -11,6 +11,9 @@ dependencies { implementation(Libs.clikt) implementation(Libs.kotlinx_serialization_json) + testImplementation(project(":usvm-core")) + testImplementation(project(":usvm-ts")) + testImplementation(Libs.jacodb_ets) testImplementation(Libs.logback) } diff --git a/usvm-ts-fast-check/src/test/kotlin/org/usvm/ts/pbt/usvm/PropertyExecutionConformanceTest.kt b/usvm-ts-fast-check/src/test/kotlin/org/usvm/ts/pbt/usvm/PropertyExecutionConformanceTest.kt new file mode 100644 index 0000000000..be529c822b --- /dev/null +++ b/usvm-ts-fast-check/src/test/kotlin/org/usvm/ts/pbt/usvm/PropertyExecutionConformanceTest.kt @@ -0,0 +1,250 @@ +package org.usvm.ts.pbt.usvm + +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.ProjectionLevel +import org.usvm.ts.pbt.backend.PropertyFailureKind +import org.usvm.ts.pbt.backend.PropertyRunConfiguration +import org.usvm.ts.pbt.backend.PropertyRunStatus +import org.usvm.ts.pbt.fastcheck.BackendErrorKind +import org.usvm.ts.pbt.fastcheck.FastCheckBackend +import org.usvm.ts.pbt.fastcheck.PbtBackendException +import org.usvm.ts.pbt.manifest.toManifest +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.PropertyDefinition +import org.usvm.ts.pbt.model.PropertyId +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.model.contains +import org.usvm.ts.pbt.testResourcePath +import org.usvm.ts.pbt.testResourcesRoot +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class PropertyExecutionConformanceTest { + @Test + fun `shared preconditions have the same concrete and search classification`() { + val cases = listOf( + ContractCase("truePrecondition", ContractOutcome.HOLDS), + ContractCase("falsePrecondition", ContractOutcome.PRECONDITION_REJECTED), + ContractCase( + exportName = "throwingPrecondition", + expected = ContractOutcome.PROPERTY_ERROR, + expectedBackendError = BackendErrorExpectation( + code = "entrypoint.precondition.threw", + path = "manifest.precondition", + ), + ), + ContractCase( + exportName = "nonBooleanPrecondition", + expected = ContractOutcome.PROPERTY_ERROR, + expectedBackendError = BackendErrorExpectation( + code = "entrypoint.result.invalid", + path = "manifest.precondition.result", + ), + ), + ) + + cases.forEach { case -> + val property = property( + predicateExport = "alwaysTrue", + preconditionExport = case.exportName, + ) + val manifest = property.toManifest() + val mapping = mapper.map(manifest) + + val concreteOutcome = concreteOutcome(property, case.expectedBackendError) + val searchOutcome = searchOutcome( + searcher.search(manifest, mapping, exactCapability), + ) + + assertEquals(case.expected, concreteOutcome, "${case.exportName}: concrete") + assertEquals(case.expected, searchOutcome, "${case.exportName}: search") + } + } + + @Test + fun `shared predicates have the same concrete and search classification`() { + val cases = listOf( + ContractCase("falsePredicate", ContractOutcome.PREDICATE_VIOLATION), + ContractCase("throwingPredicate", ContractOutcome.PREDICATE_VIOLATION), + ContractCase("literalFalsePredicate", ContractOutcome.PREDICATE_VIOLATION), + ContractCase("literalTruePredicate", ContractOutcome.HOLDS), + ContractCase("neverPredicate", ContractOutcome.PREDICATE_VIOLATION), + ContractCase( + exportName = "nonBooleanPredicate", + expected = ContractOutcome.PROPERTY_ERROR, + expectedBackendError = BackendErrorExpectation( + code = "entrypoint.result.invalid", + path = "manifest.predicate.result", + ), + ), + ) + + cases.forEach { case -> + val property = property(predicateExport = case.exportName) + val manifest = property.toManifest() + val mapping = mapper.map(manifest) + + val concreteOutcome = concreteOutcome(property, case.expectedBackendError) + val searchOutcome = searchOutcome( + searcher.search(manifest, mapping, exactCapability), + ) + + assertEquals(case.expected, concreteOutcome, "${case.exportName}: concrete") + assertEquals(case.expected, searchOutcome, "${case.exportName}: search") + } + } + + @Test + fun `symbolic candidates retain inputs from before predicate mutation`() { + val domain = ArrayDomain( + element = IntegerDomain(min = 1, max = 1), + minLength = 1, + maxLength = 1, + ) + val property = property( + predicateExport = "mutatesAndFails", + domain = domain, + ) + val manifest = property.toManifest() + val mapping = mapper.map(manifest) + + val concreteResult = backend.run(property, configuration) + val searchResult = searcher.search(manifest, mapping, exactCapability) + + val expectedInput = JsConcreteValue.Array( + elements = listOf(JsConcreteValue.number(1.0)), + ) + val symbolicInputs = assertNotNull(searchResult.inputs) + assertEquals(PropertyFailureKind.PROPERTY, concreteResult.failure?.kind) + assertEquals(listOf(expectedInput), concreteResult.counterexample) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, searchResult.status) + assertEquals(listOf(expectedInput), symbolicInputs) + assertTrue(symbolicInputs.single() in domain) + + val replay = backend.run( + property = property, + configuration = configuration.copy( + numRuns = 1, + examples = listOf(symbolicInputs), + ), + ) + + assertEquals(PropertyFailureKind.PROPERTY, replay.failure?.kind) + assertEquals(symbolicInputs, replay.counterexample) + } + + private fun concreteOutcome( + property: PropertyDefinition, + expectedBackendError: BackendErrorExpectation?, + ): ContractOutcome = try { + val result = backend.run(property, configuration) + + when (result.status) { + PropertyRunStatus.SUCCESS -> ContractOutcome.HOLDS + PropertyRunStatus.FAILURE -> when (result.failure?.kind) { + PropertyFailureKind.PROPERTY -> ContractOutcome.PREDICATE_VIOLATION + PropertyFailureKind.PRECONDITION_EXHAUSTED -> ContractOutcome.PRECONDITION_REJECTED + PropertyFailureKind.TIMEOUT, null -> error("Unexpected concrete result: $result") + } + } + } catch (failure: PbtBackendException) { + val expected = checkNotNull(expectedBackendError) { + "Unexpected concrete backend error: ${failure.kind}/${failure.code} at ${failure.path}" + } + + assertEquals(BackendErrorKind.ENTRY_POINT, failure.kind) + assertEquals(expected.code, failure.code) + assertEquals(expected.path, failure.path) + + ContractOutcome.PROPERTY_ERROR + } + + private fun searchOutcome(result: UsvmPropertySearchResult): ContractOutcome = when (result.status) { + UsvmPropertySearchStatus.VIOLATION_REACHED -> ContractOutcome.PREDICATE_VIOLATION + UsvmPropertySearchStatus.NO_VIOLATION_REACHED -> ContractOutcome.HOLDS + UsvmPropertySearchStatus.PRECONDITION_REJECTED -> ContractOutcome.PRECONDITION_REJECTED + UsvmPropertySearchStatus.PROPERTY_ERROR -> ContractOutcome.PROPERTY_ERROR + else -> error("Unexpected search result: $result") + } + + private fun property( + predicateExport: String, + preconditionExport: String? = null, + domain: org.usvm.ts.pbt.model.PropertyDomain = IntegerDomain(min = 0, max = 0), + ) = PropertyDefinition( + id = PropertyId("contract.$predicateExport.${preconditionExport ?: "none"}"), + inputs = listOf( + PropertyInput( + name = "value", + domain = domain, + ), + ), + predicate = TypeScriptEntryPoint( + module = MODULE, + exportName = predicateExport, + ), + precondition = preconditionExport?.let { exportName -> + TypeScriptEntryPoint( + module = MODULE, + exportName = exportName, + ) + }, + ) + + private data class ContractCase( + val exportName: String, + val expected: ContractOutcome, + val expectedBackendError: BackendErrorExpectation? = null, + ) + + private data class BackendErrorExpectation( + val code: String, + val path: String, + ) + + private enum class ContractOutcome { + HOLDS, + PRECONDITION_REJECTED, + PREDICATE_VIOLATION, + PROPERTY_ERROR, + } + + companion object { + private const val MODULE = "PropertyExecutionContract.ts" + + private val exactCapability = ProjectionCapability(level = ProjectionLevel.EXACT) + private val configuration = PropertyRunConfiguration( + seed = 42, + numRuns = 1, + timeoutMillis = 1_000, + ) + private val fixtureDirectory = testResourcesRoot().resolve("properties/contract") + private val backend = FastCheckBackend(sourceRoots = listOf(fixtureDirectory)) + private lateinit var mapper: PropertyEtsMapper + private lateinit var searcher: UsvmPropertySearcher + + @JvmStatic + @BeforeAll + fun loadFixture() { + val source = testResourcePath("/properties/contract/$MODULE") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + val scene = EtsScene(listOf(file)) + + mapper = PropertyEtsMapper( + scene = scene, + sourceRoots = listOf(fixtureDirectory), + ) + searcher = UsvmPropertySearcher(scene) + } + } +} diff --git a/usvm-ts-fast-check/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionConformanceTest.kt b/usvm-ts-fast-check/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionConformanceTest.kt new file mode 100644 index 0000000000..bca45b6dca --- /dev/null +++ b/usvm-ts-fast-check/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionConformanceTest.kt @@ -0,0 +1,48 @@ +package org.usvm.ts.pbt.usvm + +import org.junit.jupiter.api.Test +import org.usvm.ts.pbt.fastcheck.FastCheckProjectionClient +import org.usvm.ts.pbt.fastcheck.FastCheckProjectionRequest +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsNumber +import org.usvm.ts.pbt.model.NumberDomain +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.ts.pbt.model.contains +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class UsvmProjectionConformanceTest { + @Test + fun `fast-check samples satisfy the same domains used by USVM constraint tests`() { + val domains = listOf( + IntegerDomain(min = -5, max = 7), + NumberDomain( + min = JsNumber.finite(-1.5), + max = JsNumber.finite(2.5), + allowNaN = false, + ), + OptionalDomain(IntegerDomain(min = 1, max = 3)), + TupleDomain(listOf(IntegerDomain(min = 2, max = 4), BooleanDomain)), + ArrayDomain(IntegerDomain(min = -1, max = 1), minLength = 1, maxLength = 3), + ) + val response = FastCheckProjectionClient().sample( + FastCheckProjectionRequest( + seed = 351, + numSamples = 50, + domains = domains, + ), + ) + + assertEquals(50, response.samples.size) + response.samples.forEach { sample -> + assertEquals(domains.size, sample.size) + sample.zip(domains).forEach { (value, domain) -> + assertTrue(value in domain, "$value is outside $domain") + } + } + } +} diff --git a/usvm-ts-fast-check/src/test/resources/properties/contract/PropertyExecutionContract.ts b/usvm-ts-fast-check/src/test/resources/properties/contract/PropertyExecutionContract.ts index 9ee8c432ba..3ce5d3016d 100644 --- a/usvm-ts-fast-check/src/test/resources/properties/contract/PropertyExecutionContract.ts +++ b/usvm-ts-fast-check/src/test/resources/properties/contract/PropertyExecutionContract.ts @@ -112,6 +112,26 @@ export async function asyncNonBooleanWhenPositivePredicate(value: number): Promi return value > 0 ? 42 : false; } +export function literalFalsePredicate(_value: number): false { + return false; +} + +export function literalTruePredicate(_value: number): true { + return true; +} + +export function neverPredicate(_value: number): never { + throw 'never predicate exploded'; +} + +export function catchesExpectedException(_value: number): boolean { + try { + throw 'expected'; + } catch (error: unknown) { + return error === 'expected'; + } +} + export function recognizesSpecialValues( missing: undefined, empty: null, diff --git a/usvm-ts-pbt/PROPERTY_EXECUTION_CONTRACT.md b/usvm-ts-pbt/PROPERTY_EXECUTION_CONTRACT.md index e91f20c26b..23c19b4a86 100644 --- a/usvm-ts-pbt/PROPERTY_EXECUTION_CONTRACT.md +++ b/usvm-ts-pbt/PROPERTY_EXECUTION_CONTRACT.md @@ -82,13 +82,13 @@ was reached within that search. ## Implementation and regression points -- `fast-check-adapter/src/execute-property.ts` applies this contract to generation, explicit examples, replay, and +- `usvm-ts-fast-check/fast-check-adapter/src/execute-property.ts` applies this contract to generation, explicit examples, replay, and shrinking through the existing fast-check invocation. -- `fast-check-adapter/src/project-domain.ts` projects the declared Kotlin domains for concrete execution. -- Downstream USVM projection and search implementations consume the same manifest and mapping artifacts and must - link to this contract when their dependent changes are integrated. -- `src/test/resources/properties/contract/PropertyExecutionContract.ts` provides concrete regression coverage; - downstream symbolic integration extends the same fixture with symbolic assertions. +- `usvm-ts-fast-check/fast-check-adapter/src/project-domain.ts` projects the declared Kotlin domains for concrete execution. +- `UsvmPropertySearcher` applies this contract to USVM domain projection and search in one execution path. + `PropertyExecutionConformanceTest` runs the same TypeScript fixture through `FastCheckBackend` and USVM. +- `usvm-ts-fast-check/src/test/resources/properties/contract/PropertyExecutionContract.ts` is the shared observable fixture for + concrete and symbolic contract regressions. Replay remains ordinary concrete execution with the reported seed and path. It does not introduce a separate property runner or alternate callback semantics. diff --git a/usvm-ts-pbt/README.md b/usvm-ts-pbt/README.md index 04338b59cf..303b7ca080 100644 --- a/usvm-ts-pbt/README.md +++ b/usvm-ts-pbt/README.md @@ -205,6 +205,32 @@ Stable mapping diagnostics include `mapping.entry-point.unmapped`, `mapping.entr separately from mapping provenance and backend diagnostics are copied without reinterpretation. +## USVM projection and property search + +`UsvmProjectionCapabilityResolver` compares the declared domains with the exact EtsIR parameter bindings. +Booleans, bounded integers and numbers, supported primitive constants, optional domains, bounded tuples, and +nested bounded arrays are projected by `UsvmDomainProjector`. Strings contain symbolic UTF-16 code units, including +lone surrogates, and use private backing storage. Candidates are reconstructed from the initial input memory, +including nested arrays and dynamic payloads, so predicate mutations do not overwrite the input witness. +Incompatible EtsIR types and inputs above the configured string or collection bounds are rejected with stable +diagnostics before allocation. Search caps these bounds by the engine's array capacity. + +`UsvmPropertySearcher` evaluates the mapped precondition and predicate in one symbolic state. A false precondition +is `PRECONDITION_REJECTED` when it excludes the complete projected domain. A precondition exception or non-boolean +result is `PROPERTY_ERROR`. Predicate `false` and escaping predicate exceptions are `VIOLATION_REACHED`, while a +non-boolean predicate is `PROPERTY_ERROR`. Timeout, solver uncertainty, unsupported execution, engine failure, and +input-resolution failure retain distinct statuses and are never treated as proof. A reached target and any resolved +inputs remain attached when another explored path ends with unsupported execution or an engine failure. TypeScript +exception handlers use the interpreter's catch semantics; escaping exceptions remain predicate violations. + +The shared fixture in `usvm-ts-fast-check/src/test/resources/properties/contract/PropertyExecutionContract.ts` is +executed by both +`FastCheckBackend` and the single USVM search path. It covers precondition admission, rejection, exception and +non-boolean results, plus false, throwing, literal-boolean-typed, never-typed, and non-boolean predicates. A shared +mutation regression also verifies that a USVM candidate is reconstructed from the input before predicate mutation +and reproduces through fast-check. Special values, alias preservation, mutation isolation, shrinking, and replay +remain covered at the concrete invocation boundary. + ## Registries and CLI The CLI loads Kotlin property registries through `ServiceLoader`: diff --git a/usvm-ts-pbt/build.gradle.kts b/usvm-ts-pbt/build.gradle.kts index 1197ec28fa..17237eba23 100644 --- a/usvm-ts-pbt/build.gradle.kts +++ b/usvm-ts-pbt/build.gradle.kts @@ -4,6 +4,7 @@ plugins { } dependencies { + implementation(project(":usvm-core")) implementation(project(":usvm-ts")) implementation(Libs.jacodb_ets) implementation(Libs.kotlinx_serialization_json) diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/PbtDiagnosticCode.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/PbtDiagnosticCode.kt index 8971a44c4f..02df059163 100644 --- a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/PbtDiagnosticCode.kt +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/PbtDiagnosticCode.kt @@ -23,6 +23,28 @@ internal object PbtDiagnosticCode { const val MAPPING_STATEMENT_AMBIGUOUS = "mapping.statement.ambiguous" const val MAPPING_STATEMENT_UNMAPPED = "mapping.statement.unmapped" + const val USVM_DOMAIN_ARRAY_NESTED_UNSUPPORTED = "usvm.domain.array.nested.unsupported" + const val USVM_DOMAIN_COLLECTION_TOO_LARGE = "usvm.domain.collection.too-large" + const val USVM_DOMAIN_OPTIONAL_REFERENCE_UNSUPPORTED = "usvm.domain.optional-reference.unsupported" + const val USVM_DOMAIN_STRING_APPROXIMATE = "usvm.domain.string.approximate" + const val USVM_DOMAIN_TYPE_UNSUPPORTED = "usvm.domain.type.unsupported" + const val USVM_ENGINE_FAILURE = "usvm.engine.failure" + const val USVM_EXCEPTION_HANDLER_UNSUPPORTED = "usvm.exception-handler.unsupported" + const val USVM_EXECUTION_UNSUPPORTED = "usvm.execution.unsupported" + const val USVM_INPUT_BINDING_UNAVAILABLE = "usvm.input.binding.unavailable" + const val USVM_INPUT_RESOLUTION_FAILED = "usvm.input.resolution.failed" + const val USVM_MAPPING_PROPERTY_ID_MISMATCH = "usvm.mapping.property-id.mismatch" + const val USVM_PRECONDITION_ASYNC = "usvm.precondition.async" + const val USVM_PRECONDITION_BINDING_UNAVAILABLE = "usvm.precondition.binding.unavailable" + const val USVM_PRECONDITION_MAPPING_NON_EXACT = "usvm.precondition.mapping.non-exact" + const val USVM_PRECONDITION_MAPPING_UNAVAILABLE = "usvm.precondition.mapping.unavailable" + const val USVM_PRECONDITION_RESULT_NON_BOOLEAN = "usvm.precondition.result.non-boolean" + const val USVM_PRECONDITION_THREW = "usvm.precondition.threw" + const val USVM_PREDICATE_ASYNC = "usvm.predicate.async" + const val USVM_PREDICATE_MAPPING_NON_EXACT = "usvm.predicate.mapping.non-exact" + const val USVM_PREDICATE_RESULT_NON_BOOLEAN = "usvm.predicate.result.non-boolean" + const val USVM_SOLVER_UNKNOWN = "usvm.solver.unknown" + const val PROPERTY_ID_INVALID = "property.id.invalid" const val PROPERTY_INPUTS_EMPTY = "property.inputs.empty" const val INPUT_NAME_DUPLICATE = "input.name.duplicate" diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmCandidateInputResolver.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmCandidateInputResolver.kt new file mode 100644 index 0000000000..fb6d335cc8 --- /dev/null +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmCandidateInputResolver.kt @@ -0,0 +1,188 @@ +package org.usvm.ts.pbt.usvm + +import io.ksmt.utils.asExpr +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsTupleType +import org.jacodb.ets.model.EtsType +import org.jacodb.ets.model.EtsUnknownType +import org.usvm.UConcreteHeapRef +import org.usvm.UExpr +import org.usvm.USort +import org.usvm.isTrue +import org.usvm.machine.expr.TsUnresolvedSort +import org.usvm.machine.expr.extractDouble +import org.usvm.machine.expr.extractInt +import org.usvm.machine.expr.toConcreteBoolValue +import org.usvm.machine.state.TsState +import org.usvm.sizeSort +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.ConstantDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.NumberDomain +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.StringDomain +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.util.componentType +import org.usvm.util.mkArrayIndexLValue +import org.usvm.util.mkArrayLengthLValue +import org.usvm.util.resolveStringValue + +internal class UsvmCandidateInputResolver { + fun resolve( + state: TsState, + declaredInputs: List, + projection: UsvmDeclaredDomainProjection, + ): List { + require(declaredInputs.size == projection.inputs.size) + val initialState = projection.initialState.clone() + initialState.models = state.models + + return declaredInputs.zip(projection.inputs).map { (input, projected) -> + resolveValue( + state = initialState, + domain = input.domain, + etsType = projected.etsType, + value = projected.value, + ) + } + } + + private fun resolveValue( + state: TsState, + domain: PropertyDomain, + etsType: EtsType, + value: UExpr, + ): JsConcreteValue = with(state.ctx) { + if (value.sort == addressSort) { + val selectedReference = state.models.single().eval(value.asExpr(addressSort)) + if (selectedReference.isFakeObject()) { + return@with resolveFakeValue(state, domain, etsType, selectedReference) + } + } + + when (domain) { + BooleanDomain -> JsConcreteValue.Boolean( + state.models.single().eval(value.asExpr(boolSort)).toConcreteBoolValue(), + ) + + is IntegerDomain, is NumberDomain -> JsConcreteValue.number( + state.models.single().eval(value.asExpr(fp64Sort)).extractDouble(), + ) + + is StringDomain -> resolveString(state, value, maxLength = domain.maxLength) + is ConstantDomain -> domain.value + is OptionalDomain -> resolveOptional(state, domain, etsType, value) + is TupleDomain -> resolveTuple(state, domain, etsType, value) + is ArrayDomain -> resolveArray(state, domain, etsType as EtsArrayType, value) + } + } + + private fun resolveFakeValue( + state: TsState, + domain: PropertyDomain, + etsType: EtsType, + value: UConcreteHeapRef, + ): JsConcreteValue = with(state.ctx) { + val model = state.models.single() + val fakeType = value.getFakeType(state.memory) + val selected = when { + model.eval(fakeType.boolTypeExpr).isTrue -> value.extractBool(state.memory) + model.eval(fakeType.fpTypeExpr).isTrue -> value.extractFp(state.memory) + model.eval(fakeType.refTypeExpr).isTrue -> value.extractRef(state.memory) + else -> error("Cannot resolve the selected fake-object type") + } + + resolveValue(state, domain, etsType, selected) + } + + private fun resolveOptional( + state: TsState, + domain: OptionalDomain, + etsType: EtsType, + value: UExpr, + ): JsConcreteValue = with(state.ctx) { + if (value.sort == addressSort) { + val ref = value.asExpr(addressSort) + val nil = when (domain.nil) { + JsConcreteValue.Null -> mkTsNullValue() + JsConcreteValue.Undefined -> mkUndefinedValue() + else -> error("Optional nil must be null or undefined") + } + if (state.models.single().eval(mkHeapRefEq(ref, nil)).isTrue) { + return@with domain.nil + } + } + + val nestedType = (etsType as org.jacodb.ets.model.EtsUnionType).types.first { type -> + UsvmProjectionCapabilityResolver().domainCompatibilityForProjector(domain.value, type) + } + + resolveValue(state, domain.value, nestedType, value) + } + + private fun resolveString( + state: TsState, + value: UExpr, + maxLength: Int, + ): JsConcreteValue.String { + val contents = state.resolveStringValue( + string = value.asExpr(state.ctx.addressSort), + model = state.models.single(), + maxLength = maxLength, + ) + + return JsConcreteValue.String(contents) + } + + private fun resolveTuple( + state: TsState, + domain: TupleDomain, + etsType: EtsType, + value: UExpr, + ): JsConcreteValue.Array = with(state.ctx) { + val ref = state.models.single().eval(value.asExpr(addressSort)) as UConcreteHeapRef + val elementTypes = when (etsType) { + is EtsTupleType -> etsType.types + is EtsArrayType -> List(domain.elements.size) { etsType.componentType } + else -> error("Unsupported tuple EtsIR type $etsType") + } + val arrayType = EtsArrayType(EtsUnknownType, dimensions = 1) + val elements = domain.elements.zip(elementTypes).mapIndexed { index, (elementDomain, elementType) -> + val lValue = mkArrayIndexLValue(addressSort, ref, mkBv(index), arrayType) + val element = state.memory.read(lValue) + + resolveValue(state, elementDomain, elementType, element) + } + + JsConcreteValue.Array(elements) + } + + private fun resolveArray( + state: TsState, + domain: ArrayDomain, + etsType: EtsArrayType, + value: UExpr, + ): JsConcreteValue.Array = with(state.ctx) { + val ref = state.models.single().eval(value.asExpr(addressSort)) as UConcreteHeapRef + val lengthLValue = mkArrayLengthLValue(ref, etsType) + val length = state.models.single() + .eval(state.memory.read(lengthLValue).asExpr(sizeSort)) + .extractInt() + val elementSort = typeToSort(arrayDescriptorOf(etsType).elementType) + val elements = (0 until length).map { index -> + val element = if (elementSort is TsUnresolvedSort) { + state.memory.read(mkArrayIndexLValue(addressSort, ref, mkBv(index), etsType)) + } else { + state.memory.read(mkArrayIndexLValue(elementSort, ref, mkBv(index), etsType)) + } + + resolveValue(state, domain.element, etsType.componentType, element) + } + + JsConcreteValue.Array(elements) + } +} diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmDomainProjector.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmDomainProjector.kt new file mode 100644 index 0000000000..e18cacc92e --- /dev/null +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmDomainProjector.kt @@ -0,0 +1,360 @@ +package org.usvm.ts.pbt.usvm + +import io.ksmt.expr.KFpRoundingMode +import io.ksmt.sort.KBoolSort +import io.ksmt.sort.KFp64Sort +import io.ksmt.utils.asExpr +import io.ksmt.utils.cast +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsTupleType +import org.jacodb.ets.model.EtsType +import org.jacodb.ets.model.EtsUnionType +import org.jacodb.ets.model.EtsUnknownType +import org.usvm.UAddressSort +import org.usvm.UBoolExpr +import org.usvm.UConcreteHeapRef +import org.usvm.UExpr +import org.usvm.USort +import org.usvm.api.initializeArrayLength +import org.usvm.api.makeSymbolicPrimitive +import org.usvm.machine.expr.TsUnresolvedSort +import org.usvm.machine.state.TsState +import org.usvm.machine.types.mkFakeValue +import org.usvm.sizeSort +import org.usvm.ts.pbt.mapping.EtsInputBinding +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.ConstantDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.NumberDomain +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.StringDomain +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.util.componentType +import org.usvm.util.mkArrayIndexLValue +import org.usvm.util.mkRegisterStackLValue +import org.usvm.util.mkSymbolicString + +/** One symbolic input written to the mapped EtsIR stack slot. */ +data class UsvmProjectedInput( + val etsType: EtsType, + val value: UExpr, +) + +/** Constraints created exclusively from declared Kotlin property domains. */ +data class UsvmDeclaredDomainProjection( + val inputs: List, + val initialState: TsState, +) + +/** Materializes declared property domains in a real USVM TypeScript initial state. */ +class UsvmDomainProjector( + private val options: UsvmProjectionOptions = UsvmProjectionOptions(), +) { + fun configure( + state: TsState, + inputs: List, + bindings: List, + ): UsvmDeclaredDomainProjection { + require(inputs.size == bindings.size) { + "Property input count ${inputs.size} does not match EtsIR binding count ${bindings.size}" + } + + val pairedInputs = inputs.zip(bindings) + pairedInputs.forEachIndexed { index, (input, binding) -> + require(input.name == binding.propertyInputName) { + "Property input ${input.name} does not match EtsIR binding ${binding.propertyInputName}" + } + + val path = "inputs[$index].domain" + val capability = UsvmProjectionCapabilityResolver().domainCapabilityForProjector( + domain = input.domain, + etsType = binding.parameter.type, + path = path, + options = options, + ) + require(capability.level != org.usvm.ts.pbt.backend.ProjectionLevel.UNSUPPORTED) { + capability.diagnostics.joinToString { diagnostic -> diagnostic.message } + } + } + + val materializer = Materializer(state) + val projectedInputs = pairedInputs.map { (input, binding) -> + val value = materializer.materialize(input.domain, binding.parameter.type) + writeStackValue(state, binding.stackSlot, value) + + UsvmProjectedInput( + etsType = binding.parameter.type, + value = value, + ) + } + + return UsvmDeclaredDomainProjection( + inputs = projectedInputs, + initialState = state.clone(), + ) + } + + private inner class Materializer(private val state: TsState) { + fun materialize(domain: PropertyDomain, etsType: EtsType): UExpr = when (domain) { + BooleanDomain -> state.makeSymbolicPrimitive(state.ctx.boolSort) + is IntegerDomain -> materializeInteger(domain) + is NumberDomain -> materializeNumber(domain) + is StringDomain -> materializeString(domain) + is ConstantDomain -> materializeConstant(domain.value) + is OptionalDomain -> materializeOptional(domain, etsType) + is TupleDomain -> materializeTuple(domain, etsType) + is ArrayDomain -> materializeArray(domain, etsType as EtsArrayType) + } + + private fun materializeInteger(domain: IntegerDomain): UExpr = with(state.ctx) { + val value = state.makeSymbolicPrimitive(fp64Sort) + val rounded = mkFpRoundToIntegralExpr( + roundingMode = mkFpRoundingModeExpr(KFpRoundingMode.RoundTowardZero), + value = value, + ) + val negativeZero = mkAnd(mkFpIsZeroExpr(value), mkFpIsNegativeExpr(value)) + val minimum = mkFp(domain.min.toDouble(), fp64Sort) + val maximum = mkFp(domain.max.toDouble(), fp64Sort) + val isNumber = mkFpIsNaNExpr(value).not() + val isIntegral = mkFpEqualExpr(value, rounded) + val meetsMinimum = mkFpGreaterOrEqualExpr(value, minimum) + val meetsMaximum = mkFpLessOrEqualExpr(value, maximum) + + state.pathConstraints += mkAnd( + isNumber, + isIntegral, + negativeZero.not(), + meetsMinimum, + meetsMaximum, + ) + + value + } + + private fun materializeNumber(domain: NumberDomain): UExpr = with(state.ctx) { + val value = state.makeSymbolicPrimitive(fp64Sort) + val minimum = mkFp(domain.min.toDouble(), fp64Sort) + val maximum = mkFp(domain.max.toDouble(), fp64Sort) + val meetsMinimum = mkFpGreaterOrEqualExpr(value, minimum) + val meetsMaximum = mkFpLessOrEqualExpr(value, maximum) + val insideBounds = mkAnd(meetsMinimum, meetsMaximum) + val constraint = if (domain.allowNaN) { + mkOr(mkFpIsNaNExpr(value), insideBounds) + } else { + insideBounds + } + + state.pathConstraints += constraint + + value + } + + private fun materializeString(domain: StringDomain): UConcreteHeapRef = + state.mkSymbolicString(minLength = domain.minLength, maxLength = domain.maxLength) + + private fun materializeConstant(value: JsConcreteValue): UExpr = with(state.ctx) { + when (value) { + is JsConcreteValue.Boolean -> mkBool(value.value) + is JsConcreteValue.Number -> mkFp(value.toDouble(), fp64Sort) + is JsConcreteValue.String -> state.mkInitializedStringConstant(value.value) + JsConcreteValue.Null -> mkTsNullValue() + JsConcreteValue.Undefined -> mkUndefinedValue() + is JsConcreteValue.Array -> error("Array constants are not valid property-domain primitives") + } + } + + private fun materializeOptional( + domain: OptionalDomain, + etsType: EtsType, + ): UExpr = with(state.ctx) { + val unionType = etsType as EtsUnionType + val nestedType = unionType.types.first { type -> + UsvmProjectionCapabilityResolver().domainCompatibilityForProjector(domain.value, type) + } + val nestedValue = materialize(domain.value, nestedType) + val nilValue = materializeConstant(domain.nil) + val chooseValue = state.makeSymbolicPrimitive(boolSort) + + if (nestedValue.sort == nilValue.sort) { + return@with sameSortIte(chooseValue, nestedValue, nilValue) + } + + val fakeValue = state.mkFakeValue( + scope = null, + boolValue = nestedValue.asOptionalBool(), + fpValue = nestedValue.asOptionalFp(), + refValue = (nestedValue.asOptionalRef() ?: nilValue.asOptionalRef()), + ) + val fakeType = fakeValue.getFakeType(state.memory) + val nestedTypeExpr = when (nestedValue.sort) { + boolSort -> fakeType.boolTypeExpr + fp64Sort -> fakeType.fpTypeExpr + addressSort -> fakeType.refTypeExpr + else -> error("Unsupported optional value sort ${nestedValue.sort}") + } + val nilTypeExpr = when (nilValue.sort) { + boolSort -> fakeType.boolTypeExpr + fp64Sort -> fakeType.fpTypeExpr + addressSort -> fakeType.refTypeExpr + else -> error("Unsupported optional nil sort ${nilValue.sort}") + } + + state.pathConstraints += mkEq(nestedTypeExpr, chooseValue) + state.pathConstraints += mkEq(nilTypeExpr, chooseValue.not()) + + fakeValue + } + + private fun materializeTuple( + domain: TupleDomain, + etsType: EtsType, + ): UConcreteHeapRef = with(state.ctx) { + val elementTypes = when (etsType) { + is EtsTupleType -> etsType.types + is EtsArrayType -> List(domain.elements.size) { etsType.elementType } + else -> error("Unsupported tuple EtsIR type $etsType") + } + val arrayType = EtsArrayType(EtsUnknownType, dimensions = 1) + val descriptor = arrayDescriptorOf(arrayType) + val array = state.memory.allocConcrete(etsType) + + state.memory.initializeArrayLength(array, descriptor, sizeSort, mkBv(domain.elements.size)) + domain.elements.zip(elementTypes).forEachIndexed { index, (elementDomain, elementType) -> + val element = box(materialize(elementDomain, elementType)) + val lValue = mkArrayIndexLValue( + sort = addressSort, + ref = array, + index = mkBv(index), + type = arrayType, + ) + + state.memory.write(lValue, element, guard = trueExpr) + } + + array + } + + private fun materializeArray( + domain: ArrayDomain, + etsType: EtsArrayType, + ): UConcreteHeapRef = with(state.ctx) { + val array = state.memory.allocConcrete(etsType) + val length = state.makeSymbolicPrimitive(sizeSort) + val minimumLength = mkBv(domain.minLength) + val maximumLength = mkBv(domain.maxLength) + val meetsMinimum = mkBvSignedGreaterOrEqualExpr(length, minimumLength) + val meetsMaximum = mkBvSignedLessOrEqualExpr(length, maximumLength) + + state.memory.initializeArrayLength(array, arrayDescriptorOf(etsType), sizeSort, length) + state.pathConstraints += mkAnd(meetsMinimum, meetsMaximum) + + repeat(domain.maxLength) { index -> + val element = materialize(domain.element, etsType.componentType) + val guard = mkBvSignedLessExpr(mkBv(index), length) + + writeArrayElement(array, etsType, index, element, guard) + } + + array + } + + private fun writeArrayElement( + array: UConcreteHeapRef, + arrayType: EtsArrayType, + index: Int, + value: UExpr, + guard: UBoolExpr, + ) = with(state.ctx) { + val elementSort = typeToSort(arrayType.componentType) + if (elementSort is TsUnresolvedSort) { + val lValue = mkArrayIndexLValue( + sort = addressSort, + ref = array, + index = mkBv(index), + type = arrayType, + ) + + state.memory.write(lValue, box(value), guard) + } else { + writeArrayElementWithKnownSort(array, arrayType, index, value, guard, elementSort) + } + } + + private fun writeArrayElementWithKnownSort( + array: UConcreteHeapRef, + arrayType: EtsArrayType, + index: Int, + value: UExpr, + guard: UBoolExpr, + sort: USort, + ) = with(state.ctx) { + when (sort) { + boolSort -> { + val lValue = mkArrayIndexLValue(boolSort, array, mkBv(index), arrayType) + + state.memory.write(lValue, value.asExpr(boolSort), guard) + } + + fp64Sort -> { + val lValue = mkArrayIndexLValue(fp64Sort, array, mkBv(index), arrayType) + + state.memory.write(lValue, value.asExpr(fp64Sort), guard) + } + + addressSort -> { + val lValue = mkArrayIndexLValue(addressSort, array, mkBv(index), arrayType) + + state.memory.write(lValue, value.asExpr(addressSort), guard) + } + + else -> error("Unsupported projected array element sort $sort") + } + } + + private fun box(value: UExpr): UConcreteHeapRef = with(state.ctx) { + if (value is UConcreteHeapRef && value.isFakeObject()) return@with value + + state.mkFakeValue( + scope = null, + boolValue = value.asOptionalBool(), + fpValue = value.asOptionalFp(), + refValue = value.asOptionalRef(), + ) + } + + private fun UExpr.asOptionalBool(): UExpr? = + takeIf { sort == state.ctx.boolSort }?.asExpr(state.ctx.boolSort) + + private fun UExpr.asOptionalFp(): UExpr? = + takeIf { sort == state.ctx.fp64Sort }?.asExpr(state.ctx.fp64Sort) + + private fun UExpr.asOptionalRef(): UExpr? = + takeIf { sort == state.ctx.addressSort }?.asExpr(state.ctx.addressSort) + + @Suppress("UNCHECKED_CAST") + private fun sameSortIte( + condition: UBoolExpr, + trueValue: UExpr, + falseValue: UExpr, + ): UExpr = state.ctx.mkIte( + condition, + trueValue as UExpr, + falseValue as UExpr, + ) + } +} + +private fun writeStackValue(state: TsState, stackSlot: Int, value: UExpr): Unit = with(state.ctx) { + require(value.sort == boolSort || value.sort == fp64Sort || value.sort == addressSort) { + "Unsupported projected stack sort ${value.sort}" + } + + val lValue = mkRegisterStackLValue(value.sort, stackSlot) + + state.memory.write(lValue, value.cast(), guard = trueExpr) + state.saveSortForLocal(stackSlot, value.sort) +} diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionCapabilityResolver.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionCapabilityResolver.kt new file mode 100644 index 0000000000..b3ac7bba19 --- /dev/null +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionCapabilityResolver.kt @@ -0,0 +1,335 @@ +package org.usvm.ts.pbt.usvm + +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsBooleanLiteralType +import org.jacodb.ets.model.EtsBooleanType +import org.jacodb.ets.model.EtsNullType +import org.jacodb.ets.model.EtsNumberLiteralType +import org.jacodb.ets.model.EtsNumberType +import org.jacodb.ets.model.EtsStringLiteralType +import org.jacodb.ets.model.EtsStringType +import org.jacodb.ets.model.EtsTupleType +import org.jacodb.ets.model.EtsType +import org.jacodb.ets.model.EtsUndefinedType +import org.jacodb.ets.model.EtsUnionType +import org.usvm.ts.pbt.PbtDiagnosticCode +import org.usvm.ts.pbt.backend.CapabilityDiagnostic +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.ProjectionLevel +import org.usvm.ts.pbt.backend.aggregateProjectionCapabilities +import org.usvm.ts.pbt.backend.classifyPropertyCapability +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.EtsEntryPointTarget +import org.usvm.ts.pbt.mapping.EtsMappingResult +import org.usvm.ts.pbt.mapping.EtsMappingStatus +import org.usvm.ts.pbt.mapping.PropertyEtsMappingArtifact +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.ConstantDomain +import org.usvm.ts.pbt.model.ExecutionKind +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.NumberDomain +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.StringDomain +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.util.componentType + +/** Calculates USVM fidelity without constructing or mutating a symbolic state. */ +class UsvmProjectionCapabilityResolver { + fun resolve( + manifest: PropertyManifest, + mapping: PropertyEtsMappingArtifact, + concreteCapability: ProjectionCapability, + options: UsvmProjectionOptions = UsvmProjectionOptions(), + ): UsvmPropertyProjectionCapability { + val predicateCapability = entryPointCapability( + mapping = mapping.predicate, + path = "predicate", + nonExactCode = PbtDiagnosticCode.USVM_PREDICATE_MAPPING_NON_EXACT, + ) + val predicateTarget = mapping.predicate.exactTargetOrNull() + val predicateExecutionCapability = when { + manifest.predicate.executionKind == ExecutionKind.ASYNC -> unsupported( + code = PbtDiagnosticCode.USVM_PREDICATE_ASYNC, + message = "Asynchronous TypeScript predicates are not supported by USVM search", + path = "predicate", + ) + + else -> exact() + } + val inputCapabilities = manifest.inputs.mapIndexed { index, input -> + val path = "inputs[$index].domain" + val parameterType = predicateTarget + ?.bindings + ?.inputs + ?.getOrNull(index) + ?.parameter + ?.type + val capability = if (parameterType == null) { + unsupported( + code = PbtDiagnosticCode.USVM_INPUT_BINDING_UNAVAILABLE, + message = "An exact EtsIR input binding is required", + path = path, + ) + } else { + domainCapabilityForProjector(input.domain, parameterType, path, options) + } + + UsvmInputProjectionCapability( + inputName = input.name, + path = path, + capability = capability, + ) + } + val preconditionCapability = preconditionCapability(manifest, mapping, options) + val propertyIdCapability = if (mapping.propertyId.value == manifest.propertyId) { + exact() + } else { + unsupported( + code = PbtDiagnosticCode.USVM_MAPPING_PROPERTY_ID_MISMATCH, + message = "The property manifest and EtsIR mapping artifact have different IDs", + path = "propertyId", + ) + } + val symbolicComponents = buildList { + add(propertyIdCapability) + add(predicateCapability) + add(predicateExecutionCapability) + addAll(inputCapabilities.map { it.capability }) + add(preconditionCapability) + } + val symbolicCapability = aggregateProjectionCapabilities(symbolicComponents) + + return UsvmPropertyProjectionCapability( + inputs = inputCapabilities, + precondition = preconditionCapability, + symbolic = symbolicCapability, + property = classifyPropertyCapability(concreteCapability, symbolicCapability), + ) + } + + private fun preconditionCapability( + manifest: PropertyManifest, + mapping: PropertyEtsMappingArtifact, + options: UsvmProjectionOptions, + ): ProjectionCapability { + val declaredPrecondition = manifest.precondition ?: return exact() + if (declaredPrecondition.executionKind == ExecutionKind.ASYNC) { + return unsupported( + code = PbtDiagnosticCode.USVM_PRECONDITION_ASYNC, + message = "Asynchronous TypeScript preconditions are not supported by USVM projection", + path = "precondition", + ) + } + + val mappedPrecondition = mapping.precondition + ?: return unsupported( + code = PbtDiagnosticCode.USVM_PRECONDITION_MAPPING_UNAVAILABLE, + message = "The declared precondition has no EtsIR mapping", + path = "precondition", + ) + val mappingCapability = entryPointCapability( + mapping = mappedPrecondition, + path = "precondition", + nonExactCode = PbtDiagnosticCode.USVM_PRECONDITION_MAPPING_NON_EXACT, + ) + val target = mappedPrecondition.exactTargetOrNull() + ?: return mappingCapability + val inputCapabilities = manifest.inputs.mapIndexed { index, input -> + val parameterType = target.bindings.inputs + .getOrNull(index) + ?.parameter + ?.type + + if (parameterType == null) { + unsupported( + code = PbtDiagnosticCode.USVM_PRECONDITION_BINDING_UNAVAILABLE, + message = "An exact EtsIR precondition input binding is required", + path = "precondition", + ) + } else { + domainCapabilityForProjector(input.domain, parameterType, "inputs[$index].domain", options) + } + } + + return aggregateProjectionCapabilities( + listOf(mappingCapability) + inputCapabilities, + ) + } + + private fun entryPointCapability( + mapping: EtsMappingResult, + path: String, + nonExactCode: String, + ): ProjectionCapability = if (mapping.status == EtsMappingStatus.EXACT && mapping.targets.size == 1) { + exact() + } else { + unsupported( + code = nonExactCode, + message = "USVM projection requires exactly one EtsIR target, got ${mapping.status}", + path = path, + ) + } + + internal fun domainCapabilityForProjector( + domain: PropertyDomain, + etsType: EtsType, + path: String, + options: UsvmProjectionOptions, + ): ProjectionCapability { + if (!domainCompatibilityForProjector(domain, etsType)) { + return unsupported( + code = PbtDiagnosticCode.USVM_DOMAIN_TYPE_UNSUPPORTED, + message = "Domain ${domain::class.simpleName} cannot be projected into EtsIR type $etsType", + path = path, + ) + } + + return when (domain) { + BooleanDomain, is IntegerDomain, is NumberDomain -> exact() + is StringDomain -> if (domain.maxLength > options.maxSymbolicStringLength) { + unsupported( + code = PbtDiagnosticCode.USVM_DOMAIN_COLLECTION_TOO_LARGE, + message = "String length ${domain.maxLength} exceeds the configured symbolic string bound " + + options.maxSymbolicStringLength, + path = path, + ) + } else { + exact() + } + is ConstantDomain -> exact() + is OptionalDomain -> domainCapabilityForProjector( + domain = domain.value, + etsType = nestedOptionalType(domain, etsType), + path = "$path.value", + options = options, + ) + + is TupleDomain -> tupleCapability(domain, etsType, path, options) + is ArrayDomain -> arrayCapability(domain, etsType, path, options) + } + } + + private fun tupleCapability( + domain: TupleDomain, + etsType: EtsType, + path: String, + options: UsvmProjectionOptions, + ): ProjectionCapability { + if (domain.elements.size > options.maxSymbolicCollectionLength) { + return collectionTooLarge(domain.elements.size, options, path) + } + + val elementTypes = when (etsType) { + is EtsTupleType -> etsType.types + is EtsArrayType -> List(domain.elements.size) { etsType.componentType } + else -> return unsupported( + code = PbtDiagnosticCode.USVM_DOMAIN_TYPE_UNSUPPORTED, + message = "Tuple domain requires an EtsIR tuple or array type", + path = path, + ) + } + val elementCapabilities = domain.elements.mapIndexed { index, element -> + domainCapabilityForProjector(element, elementTypes[index], "$path.elements[$index]", options) + } + + return aggregateProjectionCapabilities(elementCapabilities) + } + + private fun arrayCapability( + domain: ArrayDomain, + etsType: EtsType, + path: String, + options: UsvmProjectionOptions, + ): ProjectionCapability { + if (domain.maxLength > options.maxSymbolicCollectionLength) { + return collectionTooLarge(domain.maxLength, options, path) + } + val elementType = (etsType as EtsArrayType).componentType + + return domainCapabilityForProjector(domain.element, elementType, "$path.element", options) + } + + private fun collectionTooLarge( + actualLength: Int, + options: UsvmProjectionOptions, + path: String, + ) = unsupported( + code = PbtDiagnosticCode.USVM_DOMAIN_COLLECTION_TOO_LARGE, + message = "Collection length $actualLength exceeds the symbolic cap " + + options.maxSymbolicCollectionLength, + path = path, + ) + + internal fun domainCompatibilityForProjector(domain: PropertyDomain, etsType: EtsType): Boolean = when (domain) { + BooleanDomain -> etsType == EtsBooleanType || etsType is EtsBooleanLiteralType + is IntegerDomain, is NumberDomain -> etsType == EtsNumberType || etsType is EtsNumberLiteralType + is StringDomain -> etsType == EtsStringType || etsType is EtsStringLiteralType + is ConstantDomain -> isConstantCompatible(domain.value, etsType) + is OptionalDomain -> isOptionalCompatible(domain, etsType) + is TupleDomain -> when (etsType) { + is EtsTupleType -> { + etsType.types.size == domain.elements.size && + domain.elements.zip(etsType.types).all { (element, elementType) -> + domainCompatibilityForProjector(element, elementType) + } + } + + is EtsArrayType -> { + domain.elements.all { domainCompatibilityForProjector(it, etsType.componentType) } + } + + else -> false + } + + is ArrayDomain -> { + etsType is EtsArrayType && + domainCompatibilityForProjector(domain.element, etsType.componentType) + } + } + + private fun isConstantCompatible(value: JsConcreteValue, etsType: EtsType): Boolean = when (value) { + is JsConcreteValue.Boolean -> etsType == EtsBooleanType || etsType is EtsBooleanLiteralType + is JsConcreteValue.Number -> etsType == EtsNumberType || etsType is EtsNumberLiteralType + is JsConcreteValue.String -> etsType == EtsStringType || etsType is EtsStringLiteralType + JsConcreteValue.Null -> etsType == EtsNullType + JsConcreteValue.Undefined -> etsType == EtsUndefinedType + is JsConcreteValue.Array -> false + } + + private fun isOptionalCompatible(domain: OptionalDomain, etsType: EtsType): Boolean { + val union = etsType as? EtsUnionType ?: return false + val nilType = nilType(domain.nil) ?: return false + + return union.types.any { it == nilType } && + union.types.any { domainCompatibilityForProjector(domain.value, it) } + } + + private fun nestedOptionalType(domain: OptionalDomain, etsType: EtsType): EtsType { + val union = etsType as EtsUnionType + + return union.types.first { domainCompatibilityForProjector(domain.value, it) } + } + + private fun nilType(nil: JsConcreteValue): EtsType? = when (nil) { + JsConcreteValue.Null -> EtsNullType + JsConcreteValue.Undefined -> EtsUndefinedType + else -> null + } + + private fun unsupported(code: String, message: String, path: String): ProjectionCapability { + val diagnostic = CapabilityDiagnostic(code = code, message = message, path = path) + + return ProjectionCapability( + level = ProjectionLevel.UNSUPPORTED, + diagnostics = listOf(diagnostic), + ) + } + + private fun exact() = ProjectionCapability(level = ProjectionLevel.EXACT) +} + +internal fun EtsMappingResult.exactTargetOrNull(): EtsEntryPointTarget? = + targets.singleOrNull()?.takeIf { status == EtsMappingStatus.EXACT } diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionModel.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionModel.kt new file mode 100644 index 0000000000..8b075df90a --- /dev/null +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionModel.kt @@ -0,0 +1,34 @@ +package org.usvm.ts.pbt.usvm + +import org.usvm.machine.TsOptions +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.PropertyCapabilityLevel + +/** Bounds resource usage while recursively materializing symbolic property inputs. */ +data class UsvmProjectionOptions( + val maxSymbolicCollectionLength: Int = 10, + val maxSymbolicStringLength: Int = TsOptions().maxArraySize, +) { + init { + require(maxSymbolicStringLength >= 0) { "Maximum symbolic string length must be non-negative" } + + require(maxSymbolicCollectionLength >= 0) { + "Maximum symbolic collection length must be non-negative" + } + } +} + +/** Capability of one ordered property input at its stable manifest path. */ +data class UsvmInputProjectionCapability( + val inputName: String, + val path: String, + val capability: ProjectionCapability, +) + +/** Combined concrete and symbolic execution capability for one property. */ +data class UsvmPropertyProjectionCapability( + val inputs: List, + val precondition: ProjectionCapability, + val symbolic: ProjectionCapability, + val property: PropertyCapabilityLevel, +) diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearchModel.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearchModel.kt new file mode 100644 index 0000000000..192f8ae6a5 --- /dev/null +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearchModel.kt @@ -0,0 +1,63 @@ +package org.usvm.ts.pbt.usvm + +import org.usvm.ts.pbt.backend.CapabilityDiagnostic +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.PropertyId + +/** Terminal outcome of one USVM property-violation search. */ +enum class UsvmPropertySearchStatus { + VIOLATION_REACHED, + NO_VIOLATION_REACHED, + PRECONDITION_REJECTED, + PROPERTY_ERROR, + TIMEOUT, + SOLVER_UNKNOWN, + UNSUPPORTED, + ENGINE_FAILURE, + FAILED_INPUT_RESOLUTION, +} + +/** Supported ways in which a mapped predicate can violate its property. */ +enum class UsvmPropertyViolationTarget { + PREDICATE_FALSE, + UNEXPECTED_EXCEPTION, + ASSERTION_FAILURE, +} + +/** Backend-neutral result of searching one mapped TypeScript property with USVM. */ +data class UsvmPropertySearchResult( + val propertyId: PropertyId, + val status: UsvmPropertySearchStatus, + val target: UsvmPropertyViolationTarget?, + val inputs: List?, + val capability: UsvmPropertyProjectionCapability, + val diagnostics: List, +) { + init { + when (status) { + UsvmPropertySearchStatus.VIOLATION_REACHED -> { + requireNotNull(target) { "A reached violation requires its target" } + requireNotNull(inputs) { "A resolved violation requires candidate inputs" } + } + + UsvmPropertySearchStatus.FAILED_INPUT_RESOLUTION -> { + requireNotNull(target) { "An input-resolution failure requires its reached target" } + require(inputs == null) { "An input-resolution failure cannot contain candidate inputs" } + } + + UsvmPropertySearchStatus.PROPERTY_ERROR, + UsvmPropertySearchStatus.UNSUPPORTED, + UsvmPropertySearchStatus.ENGINE_FAILURE, + -> { + require(inputs == null || target != null) { + "Resolved candidate inputs require their reached target" + } + } + + else -> { + require(target == null) { "A result without a violation cannot contain a target" } + require(inputs == null) { "A result without a violation cannot contain candidate inputs" } + } + } + } +} diff --git a/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearcher.kt b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearcher.kt new file mode 100644 index 0000000000..7aa34948aa --- /dev/null +++ b/usvm-ts-pbt/src/main/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearcher.kt @@ -0,0 +1,464 @@ +package org.usvm.ts.pbt.usvm + +import io.ksmt.utils.asExpr +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.model.EtsType +import org.usvm.StateCollectionStrategy +import org.usvm.UMachineOptions +import org.usvm.api.targets.TsTarget +import org.usvm.isAllocatedConcreteHeapRef +import org.usvm.machine.TsMachine +import org.usvm.machine.TsMachineAnalysisResult +import org.usvm.machine.TsOptions +import org.usvm.machine.state.TsEntryPointGuardOutcome +import org.usvm.machine.state.TsMethodResult +import org.usvm.machine.state.TsState +import org.usvm.machine.state.prependBooleanEntryPointGuard +import org.usvm.solver.USatResult +import org.usvm.solver.UUnknownResult +import org.usvm.solver.UUnsatResult +import org.usvm.statistics.UMachineObserver +import org.usvm.ts.pbt.PbtDiagnosticCode +import org.usvm.ts.pbt.backend.CapabilityDiagnostic +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.ProjectionLevel +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.EtsEntryPointTarget +import org.usvm.ts.pbt.mapping.PropertyEtsMappingArtifact +import org.usvm.ts.pbt.model.PropertyId + +/** Searches mapped TypeScript predicates for concrete property violations with USVM. */ +class UsvmPropertySearcher( + private val scene: EtsScene, + machineOptions: UMachineOptions = UMachineOptions(), + private val tsOptions: TsOptions = TsOptions(), + projectionOptions: UsvmProjectionOptions = UsvmProjectionOptions(), +) { + private val machineOptions = machineOptions.copy(stateCollectionStrategy = StateCollectionStrategy.ALL) + private val projectionOptions = projectionOptions.copy( + maxSymbolicCollectionLength = minOf(projectionOptions.maxSymbolicCollectionLength, tsOptions.maxArraySize), + maxSymbolicStringLength = minOf(projectionOptions.maxSymbolicStringLength, tsOptions.maxArraySize), + ) + private val capabilityResolver = UsvmProjectionCapabilityResolver() + private val domainProjector = UsvmDomainProjector(this.projectionOptions) + private val inputResolver = UsvmCandidateInputResolver() + + fun search( + manifest: PropertyManifest, + mapping: PropertyEtsMappingArtifact, + concreteCapability: ProjectionCapability, + ): UsvmPropertySearchResult { + val capability = capabilityResolver.resolve( + manifest = manifest, + mapping = mapping, + concreteCapability = concreteCapability, + options = projectionOptions, + ) + if (capability.symbolic.level == ProjectionLevel.UNSUPPORTED) { + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.UNSUPPORTED, + capability = capability, + ) + } + + val predicate = mapping.predicate.exactTargetOrNull() + ?: return result( + manifest = manifest, + status = UsvmPropertySearchStatus.ENGINE_FAILURE, + capability = capability, + additionalDiagnostic = diagnostic( + code = PbtDiagnosticCode.USVM_ENGINE_FAILURE, + message = "An exact predicate target was unavailable after capability validation", + path = "predicate", + ), + ) + val precondition = mapping.precondition?.exactTargetOrNull() + + return executeSearch( + manifest = manifest, + predicate = predicate, + precondition = precondition, + capability = capability, + ) + } + + private fun executeSearch( + manifest: PropertyManifest, + predicate: EtsEntryPointTarget, + precondition: EtsEntryPointTarget?, + capability: UsvmPropertyProjectionCapability, + ): UsvmPropertySearchResult { + lateinit var projection: UsvmDeclaredDomainProjection + val target = UsvmViolationTsTarget() + val observer = UsvmViolationObserver(target) + + return TsMachine( + scene = scene, + options = machineOptions, + tsOptions = tsOptions, + machineObserver = observer, + ).use { machine -> + val analysis = machine.analyzeWithMetadata( + methods = listOf(predicate.method), + targets = listOf(target), + configureInitialState = { method, state -> + check(method == predicate.method) + projection = domainProjector.configure( + state = state, + inputs = manifest.inputs, + bindings = predicate.bindings.inputs, + ) + if (precondition != null) { + state.prependBooleanEntryPointGuard( + guard = precondition.method, + arguments = projection.inputs.map(UsvmProjectedInput::value), + ) + } + }, + ) + val violationState = observer.violationStates.firstOrNull { state -> + state.methodResult is TsMethodResult.Success + } ?: observer.violationStates.firstOrNull() + val candidate = violationState?.let { state -> + violationResult( + manifest = manifest, + capability = capability, + violationState = state, + projection = projection, + ) + } + val terminalFailure = classifyTerminalFailure( + manifest = manifest, + capability = capability, + analysis = analysis, + ) + if (terminalFailure != null) { + return@use terminalFailure.withCandidate(candidate) + } + + candidate + ?: noViolationResult( + manifest = manifest, + capability = capability, + analysis = analysis, + observer = observer, + ) + } + } + + private fun classifyTerminalFailure( + manifest: PropertyManifest, + capability: UsvmPropertyProjectionCapability, + analysis: TsMachineAnalysisResult, + ): UsvmPropertySearchResult? { + val preconditionFailure = classifyPreconditionFailure(manifest, capability, analysis) + if (preconditionFailure != null) { + return preconditionFailure + } + + val predicateContractError = analysis.states.any { state -> + val methodResult = state.methodResult as? TsMethodResult.Success + state.entryPointGuardOutcome == TsEntryPointGuardOutcome.NONE && + methodResult != null && + methodResult.value.sort != state.ctx.boolSort + } + if (predicateContractError) { + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.PROPERTY_ERROR, + capability = capability, + additionalDiagnostic = diagnostic( + code = PbtDiagnosticCode.USVM_PREDICATE_RESULT_NON_BOOLEAN, + message = "The predicate returned a non-boolean symbolic value", + path = "predicate.result", + ), + ) + } + val missingPredicateResult = analysis.states.any { state -> + state.entryPointGuardOutcome == TsEntryPointGuardOutcome.NONE && + state.methodResult == TsMethodResult.NoCall + } + if (missingPredicateResult) { + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.ENGINE_FAILURE, + capability = capability, + additionalDiagnostic = diagnostic( + code = PbtDiagnosticCode.USVM_ENGINE_FAILURE, + message = "Predicate analysis terminated without a method result", + path = "predicate", + ), + ) + } + if (analysis.unsupportedPaths.isNotEmpty()) { + val unsupportedMessage = analysis.unsupportedPaths.joinToString() + + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.UNSUPPORTED, + capability = capability, + additionalDiagnostic = diagnostic( + code = PbtDiagnosticCode.USVM_EXECUTION_UNSUPPORTED, + message = unsupportedMessage, + path = "predicate", + ), + ) + } + if (analysis.unsupportedCall) { + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.UNSUPPORTED, + capability = capability, + additionalDiagnostic = diagnostic( + code = PbtDiagnosticCode.USVM_EXECUTION_UNSUPPORTED, + message = "The symbolic engine encountered an unsupported property call", + path = "predicate", + ), + ) + } + if (analysis.engineFailed) { + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.ENGINE_FAILURE, + capability = capability, + additionalDiagnostic = diagnostic( + code = PbtDiagnosticCode.USVM_ENGINE_FAILURE, + message = "The symbolic engine could not execute every reachable property path", + path = "predicate", + ), + ) + } + + return null + } + + private fun classifyPreconditionFailure( + manifest: PropertyManifest, + capability: UsvmPropertyProjectionCapability, + analysis: TsMachineAnalysisResult, + ): UsvmPropertySearchResult? { + val preconditionError = analysis.states.firstOrNull { state -> + state.entryPointGuardOutcome == TsEntryPointGuardOutcome.ERROR + } + ?: return null + val diagnostic = when (preconditionError.methodResult) { + is TsMethodResult.TsException -> diagnostic( + code = PbtDiagnosticCode.USVM_PRECONDITION_THREW, + message = "The precondition has a reachable escaping exception", + path = "precondition", + ) + + is TsMethodResult.Success -> diagnostic( + code = PbtDiagnosticCode.USVM_PRECONDITION_RESULT_NON_BOOLEAN, + message = "The precondition returned a non-boolean symbolic value", + path = "precondition.result", + ) + + TsMethodResult.NoCall -> diagnostic( + code = PbtDiagnosticCode.USVM_ENGINE_FAILURE, + message = "The precondition guard terminated without a method result", + path = "precondition", + ) + } + val status = if (preconditionError.methodResult == TsMethodResult.NoCall) { + UsvmPropertySearchStatus.ENGINE_FAILURE + } else { + UsvmPropertySearchStatus.PROPERTY_ERROR + } + + return result( + manifest = manifest, + status = status, + capability = capability, + additionalDiagnostic = diagnostic, + ) + } + + private fun noViolationResult( + manifest: PropertyManifest, + capability: UsvmPropertyProjectionCapability, + analysis: TsMachineAnalysisResult, + observer: UsvmViolationObserver, + ): UsvmPropertySearchResult { + val predicateCompleted = analysis.states.any { state -> + state.entryPointGuardOutcome == TsEntryPointGuardOutcome.NONE && + state.methodResult is TsMethodResult.Success + } + val preconditionRejected = analysis.states.any { state -> + state.entryPointGuardOutcome == TsEntryPointGuardOutcome.REJECTED + } + val status = when { + analysis.timedOut -> UsvmPropertySearchStatus.TIMEOUT + observer.solverUnknown -> UsvmPropertySearchStatus.SOLVER_UNKNOWN + predicateCompleted -> UsvmPropertySearchStatus.NO_VIOLATION_REACHED + preconditionRejected -> UsvmPropertySearchStatus.PRECONDITION_REJECTED + else -> UsvmPropertySearchStatus.ENGINE_FAILURE + } + val additionalDiagnostic = when (status) { + UsvmPropertySearchStatus.SOLVER_UNKNOWN -> diagnostic( + code = PbtDiagnosticCode.USVM_SOLVER_UNKNOWN, + message = "The solver could not classify a predicate result", + path = "predicate.result", + ) + + UsvmPropertySearchStatus.ENGINE_FAILURE -> diagnostic( + code = PbtDiagnosticCode.USVM_ENGINE_FAILURE, + message = "Property search produced no classified terminal state", + path = "predicate", + ) + + else -> null + } + + return result( + manifest = manifest, + status = status, + capability = capability, + additionalDiagnostic = additionalDiagnostic, + ) + } + + private fun violationResult( + manifest: PropertyManifest, + capability: UsvmPropertyProjectionCapability, + violationState: TsState, + projection: UsvmDeclaredDomainProjection, + ): UsvmPropertySearchResult { + val violationTarget = classifyViolation(violationState) + val inputs = runCatching { + inputResolver.resolve(violationState, manifest.inputs, projection) + }.getOrElse { failure -> + val diagnostic = CapabilityDiagnostic( + code = PbtDiagnosticCode.USVM_INPUT_RESOLUTION_FAILED, + message = failure.message ?: "Failed to resolve symbolic property inputs", + path = "inputs", + ) + + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.FAILED_INPUT_RESOLUTION, + target = violationTarget, + capability = capability, + additionalDiagnostic = diagnostic, + ) + } + + return result( + manifest = manifest, + status = UsvmPropertySearchStatus.VIOLATION_REACHED, + target = violationTarget, + inputs = inputs, + capability = capability, + ) + } + + private fun diagnostic(code: String, message: String, path: String) = CapabilityDiagnostic( + code = code, + message = message, + path = path, + ) + + private fun classifyViolation(state: TsState): UsvmPropertyViolationTarget { + return when (val result = state.methodResult) { + is TsMethodResult.Success -> { + UsvmPropertyViolationTarget.PREDICATE_FALSE + } + + is TsMethodResult.TsException -> { + val message = with(state.ctx) { + val ref = state.models.single().eval(result.value) as? org.usvm.UConcreteHeapRef + ref?.takeIf(::isAllocatedConcreteHeapRef)?.let(::getStringConstantValue) + } + if (message?.contains("AssertionError") == true) { + UsvmPropertyViolationTarget.ASSERTION_FAILURE + } else { + UsvmPropertyViolationTarget.UNEXPECTED_EXCEPTION + } + } + + TsMethodResult.NoCall -> { + error("Reached a violation target without a predicate result") + } + } + } + + private fun result( + manifest: PropertyManifest, + status: UsvmPropertySearchStatus, + capability: UsvmPropertyProjectionCapability, + target: UsvmPropertyViolationTarget? = null, + inputs: List? = null, + additionalDiagnostic: CapabilityDiagnostic? = null, + ) = UsvmPropertySearchResult( + propertyId = PropertyId(manifest.propertyId), + status = status, + target = target, + inputs = inputs, + capability = capability, + diagnostics = capability.symbolic.diagnostics + listOfNotNull(additionalDiagnostic), + ) +} + +private fun UsvmPropertySearchResult.withCandidate( + candidate: UsvmPropertySearchResult?, +): UsvmPropertySearchResult { + if (candidate == null) return this + + return copy( + target = candidate.target, + inputs = candidate.inputs, + diagnostics = (diagnostics + candidate.diagnostics).distinct(), + ) +} + +private class UsvmViolationTsTarget : TsTarget(location = null) + +private class UsvmViolationObserver( + private val target: UsvmViolationTsTarget, +) : UMachineObserver { + private val mutableViolationStates = mutableListOf() + + val violationStates: List + get() = mutableViolationStates + + var solverUnknown: Boolean = false + private set + + override fun onStateTerminated(state: TsState, stateReachable: Boolean) { + if ( + !stateReachable || + state.entryPointGuardActive || + state.entryPointGuardOutcome != TsEntryPointGuardOutcome.NONE + ) { + return + } + + when (val result = state.methodResult) { + is TsMethodResult.TsException -> recordViolation(state) + is TsMethodResult.Success -> with(state.ctx) { + val returnValue = result.value.takeIf { it.sort == boolSort }?.asExpr(boolSort) ?: return@with + val candidateState = state.clone() + candidateState.pathConstraints += returnValue.not() + val solverResult = solver().check(candidateState.pathConstraints) + + when (solverResult) { + is USatResult -> { + candidateState.models = listOf(solverResult.model) + recordViolation(candidateState) + } + + is UUnsatResult -> Unit + is UUnknownResult -> solverUnknown = true + } + } + + TsMethodResult.NoCall -> Unit + } + } + + private fun recordViolation(state: TsState) { + target.propagate(state) + mutableViolationStates += state + } +} diff --git a/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmCollectionDomainProjectorTest.kt b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmCollectionDomainProjectorTest.kt new file mode 100644 index 0000000000..b18601d748 --- /dev/null +++ b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmCollectionDomainProjectorTest.kt @@ -0,0 +1,175 @@ +package org.usvm.ts.pbt.usvm + +import io.ksmt.utils.asExpr +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsNumberType +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.model.EtsUnknownType +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.usvm.StateCollectionStrategy +import org.usvm.UConcreteHeapRef +import org.usvm.UMachineOptions +import org.usvm.machine.TsMachine +import org.usvm.machine.TsOptions +import org.usvm.machine.state.TsState +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.testResourcePath +import org.usvm.util.mkArrayIndexLValue +import org.usvm.util.mkArrayLengthLValue +import kotlin.test.assertFailsWith +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class UsvmCollectionDomainProjectorTest { + @Test + fun `array length and every active element satisfy their recursive domains`() { + val domain = ArrayDomain( + element = IntegerDomain(min = -1, max = 1), + minLength = 1, + maxLength = 2, + ) + + assertTrue(acceptsArray(domain, length = 1, elements = listOf(-1.0))) + assertTrue(acceptsArray(domain, length = 2, elements = listOf(-1.0, 1.0))) + assertFalse(acceptsArray(domain, length = 0, elements = emptyList())) + assertFalse(acceptsArray(domain, length = 3, elements = listOf(0.0, 0.0))) + assertFalse(acceptsArray(domain, length = 2, elements = listOf(0.0, 2.0))) + } + + @Test + fun `tuple has exact length and positional recursive domains`() { + val domain = TupleDomain(listOf(IntegerDomain(min = 2, max = 4), BooleanDomain)) + + assertTrue(acceptsTuple(domain, number = 3.0, boolean = true, length = 2)) + assertTrue(acceptsTuple(domain, number = 4.0, boolean = false, length = 2)) + assertFalse(acceptsTuple(domain, number = 1.0, boolean = true, length = 2)) + assertFalse(acceptsTuple(domain, number = 3.0, boolean = true, length = 1)) + } + + @Test + fun `oversized arrays are rejected before materialization`() { + val domain = ArrayDomain(IntegerDomain(), maxLength = 3) + val manifest = manifest(domain, exportName = "acceptsNumberArray") + val mapping = mapper.map(manifest) + val target = mapping.predicate.targets.single() + val projector = UsvmDomainProjector( + options = UsvmProjectionOptions(maxSymbolicCollectionLength = 2), + ) + + assertFailsWith { + analyze(target.method) { state -> + projector.configure(state, manifest.inputs, target.bindings.inputs) + } + } + } + + private fun acceptsArray(domain: ArrayDomain, length: Int, elements: List): Boolean { + val manifest = manifest(domain, exportName = "acceptsNumberArray") + val mapping = mapper.map(manifest) + val target = mapping.predicate.targets.single() + + return runCatchingAnalyze(target.method) { state -> + val projection = projector.configure(state, manifest.inputs, target.bindings.inputs) + + with(state.ctx) { + val array = projection.inputs.single().value.asExpr(addressSort) + val arrayType = EtsArrayType(EtsNumberType, dimensions = 1) + val projectedLength = state.memory.read(mkArrayLengthLValue(array, arrayType)) + state.pathConstraints += mkEq(projectedLength, mkBv(length)) + elements.forEachIndexed { index, element -> + val lValue = mkArrayIndexLValue(fp64Sort, array, mkBv(index), arrayType) + val projectedElement = state.memory.read(lValue) + + state.pathConstraints += mkEq(projectedElement, mkFp(element, fp64Sort)) + } + } + } + } + + private fun acceptsTuple( + domain: TupleDomain, + number: Double, + boolean: Boolean, + length: Int, + ): Boolean { + val manifest = manifest(domain, exportName = "acceptsNumberBooleanTuple") + val mapping = mapper.map(manifest) + val target = mapping.predicate.targets.single() + + return runCatchingAnalyze(target.method) { state -> + val projection = projector.configure(state, manifest.inputs, target.bindings.inputs) + + with(state.ctx) { + val tuple = projection.inputs.single().value.asExpr(addressSort) + val arrayType = EtsArrayType(EtsUnknownType, dimensions = 1) + val projectedLength = state.memory.read(mkArrayLengthLValue(tuple, arrayType)) + val numberBox = state.memory.read( + mkArrayIndexLValue(addressSort, tuple, mkBv(0), arrayType), + ) as UConcreteHeapRef + val booleanBox = state.memory.read( + mkArrayIndexLValue(addressSort, tuple, mkBv(1), arrayType), + ) as UConcreteHeapRef + + state.pathConstraints += mkEq(projectedLength, mkBv(length)) + state.pathConstraints += mkEq(numberBox.extractFp(state.memory), mkFp(number, fp64Sort)) + state.pathConstraints += mkEq(booleanBox.extractBool(state.memory), mkBool(boolean)) + } + } + } + + private fun manifest(domain: PropertyDomain, exportName: String) = PropertyManifest( + propertyId = "usvm.collection.$exportName", + inputs = listOf(PropertyInput(name = "value", domain = domain)), + predicate = TypeScriptEntryPoint( + module = "UsvmCapabilityFixture.ts", + exportName = exportName, + ), + ) + + private fun runCatchingAnalyze( + method: org.jacodb.ets.model.EtsMethod, + configure: (TsState) -> Unit, + ): Boolean = runCatching { analyze(method, configure) }.isSuccess + + private fun analyze( + method: org.jacodb.ets.model.EtsMethod, + configure: (TsState) -> Unit, + ) { + TsMachine( + scene = scene, + options = UMachineOptions(stateCollectionStrategy = StateCollectionStrategy.ALL), + tsOptions = TsOptions(), + ).use { machine -> + machine.analyze( + methods = listOf(method), + configureInitialState = { _, state -> configure(state) }, + ) + } + } + + companion object { + private lateinit var scene: EtsScene + private lateinit var mapper: PropertyEtsMapper + private val projector = UsvmDomainProjector() + + @JvmStatic + @BeforeAll + fun loadFixture() { + val source = testResourcePath("/usvm/UsvmCapabilityFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + scene = EtsScene(listOf(file)) + mapper = PropertyEtsMapper(scene = scene, sourceRoots = listOf(source.parent)) + } + } +} diff --git a/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmInitialStateConfigurationTest.kt b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmInitialStateConfigurationTest.kt new file mode 100644 index 0000000000..c9a988ef7b --- /dev/null +++ b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmInitialStateConfigurationTest.kt @@ -0,0 +1,110 @@ +package org.usvm.ts.pbt.usvm + +import io.ksmt.utils.asExpr +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.Test +import org.usvm.StateCollectionStrategy +import org.usvm.UMachineOptions +import org.usvm.machine.TsMachine +import org.usvm.machine.TsOptions +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.testResourcePath +import org.usvm.util.mkRegisterStackLValue +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class UsvmInitialStateConfigurationTest { + @Test + fun `initial state configuration participates in the first solver model`() { + val source = testResourcePath("/mapping/PropertyMappingFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + val scene = EtsScene(listOf(file)) + val method = scene.projectClasses + .flatMap { etsClass -> etsClass.methods } + .single { candidate -> candidate.name == "isPositive" } + val options = UMachineOptions(stateCollectionStrategy = StateCollectionStrategy.ALL) + + val states = TsMachine( + scene = scene, + options = options, + tsOptions = TsOptions(), + ).use { machine -> + machine.analyze( + methods = listOf(method), + configureInitialState = { configuredMethod, state -> + assertEquals(method, configuredMethod) + + with(state.ctx) { + val input = state.memory.read(mkRegisterStackLValue(fp64Sort, 1)).asExpr(fp64Sort) + state.pathConstraints += mkFpEqualExpr(input, mkFp(7.0, fp64Sort)) + } + }, + ) + } + + assertTrue(states.isNotEmpty()) + states.forEach { state -> + with(state.ctx) { + val input = state.memory.read(mkRegisterStackLValue(fp64Sort, 1)).asExpr(fp64Sort) + val evaluated = state.models.single().eval(input) + + assertEquals(mkFp(7.0, fp64Sort), evaluated) + } + } + } + + @Test + fun `ordinary constraint pruning is not reported as an engine failure`() { + val source = testResourcePath("/usvm/UsvmCapabilityFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + val scene = EtsScene(listOf(file)) + val manifest = PropertyManifest( + propertyId = "usvm.constraint-pruning", + inputs = listOf( + PropertyInput( + name = "value", + domain = ArrayDomain( + element = IntegerDomain(min = 0, max = 0), + minLength = 2, + maxLength = 2, + ), + ), + ), + predicate = TypeScriptEntryPoint( + module = "UsvmCapabilityFixture.ts", + exportName = "acceptsNumberArray", + ), + ) + val mapper = PropertyEtsMapper(scene = scene, sourceRoots = listOf(source.parent)) + val predicate = requireNotNull(mapper.map(manifest).predicate.exactTargetOrNull()) + val options = UMachineOptions(stateCollectionStrategy = StateCollectionStrategy.ALL) + + val analysis = TsMachine( + scene = scene, + options = options, + tsOptions = TsOptions(maxArraySize = 1), + ).use { machine -> + machine.analyzeWithMetadata( + methods = listOf(predicate.method), + configureInitialState = { _, state -> + UsvmDomainProjector().configure( + state = state, + inputs = manifest.inputs, + bindings = predicate.bindings.inputs, + ) + }, + ) + } + + assertEquals(emptyList(), analysis.states) + assertFalse(analysis.engineFailed) + } +} diff --git a/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmPreconditionSearchTest.kt b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmPreconditionSearchTest.kt new file mode 100644 index 0000000000..2025445eba --- /dev/null +++ b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmPreconditionSearchTest.kt @@ -0,0 +1,115 @@ +package org.usvm.ts.pbt.usvm + +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.usvm.machine.TsOptions +import org.usvm.machine.call.TsUnknownCallModelSelection +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.ProjectionLevel +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ExecutionKind +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.testResourcePath +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class UsvmPreconditionSearchTest { + @Test + fun `mapped precondition restricts the declared domain before predicate search`() { + val result = search(manifest(preconditionExport = "isPositive")) + + assertEquals(ProjectionLevel.EXACT, result.capability.symbolic.level) + assertEquals(UsvmPropertySearchStatus.NO_VIOLATION_REACHED, result.status) + } + + @Test + fun `precondition search distinguishes rejection modeled errors and disabled calls`() { + val errorPrecondition = manifest(preconditionExport = "acceptsNonPositiveOrThrows") + val searcherWithoutModels = UsvmPropertySearcher( + scene = scene, + tsOptions = TsOptions(unknownCallModelSelection = TsUnknownCallModelSelection.Only(emptySet())), + ) + + val rejected = search(manifest(preconditionExport = "alwaysFalse")) + val modeled = search(errorPrecondition) + val unsupported = search(manifest = errorPrecondition, propertySearcher = searcherWithoutModels) + + assertEquals(UsvmPropertySearchStatus.PRECONDITION_REJECTED, rejected.status) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, modeled.status) + assertEquals(UsvmPropertyViolationTarget.PREDICATE_FALSE, modeled.target) + assertEquals(listOf(JsConcreteValue.number(0.0)), modeled.inputs) + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, unsupported.status) + assertTrue(unsupported.diagnostics.any { it.code == "usvm.execution.unsupported" }) + } + + @Test + fun `async and non-boolean preconditions retain distinct classifications`() { + val async = search( + manifest( + preconditionExport = "asyncIsPositive", + executionKind = ExecutionKind.ASYNC, + ), + ) + val nonBoolean = search(manifest(preconditionExport = "returnsNumber")) + + assertEquals(ProjectionLevel.UNSUPPORTED, async.capability.precondition.level) + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, async.status) + assertEquals(UsvmPropertySearchStatus.PROPERTY_ERROR, nonBoolean.status) + assertTrue(nonBoolean.diagnostics.any { it.code == "usvm.precondition.result.non-boolean" }) + } + + private fun search( + manifest: PropertyManifest, + propertySearcher: UsvmPropertySearcher = searcher, + ): UsvmPropertySearchResult = propertySearcher.search( + manifest = manifest, + mapping = mapper.map(manifest), + concreteCapability = ProjectionCapability(level = ProjectionLevel.EXACT), + ) + + private fun manifest( + preconditionExport: String, + executionKind: ExecutionKind = ExecutionKind.SYNC, + ) = PropertyManifest( + propertyId = "usvm.precondition.$preconditionExport.${executionKind.name.lowercase()}", + inputs = listOf( + PropertyInput( + name = "value", + domain = IntegerDomain(min = -2, max = 3), + ), + ), + predicate = TypeScriptEntryPoint( + module = "UsvmPreconditionFixture.ts", + exportName = "predicate", + ), + precondition = TypeScriptEntryPoint( + module = "UsvmPreconditionFixture.ts", + exportName = preconditionExport, + executionKind = executionKind, + ), + ) + + companion object { + private lateinit var scene: EtsScene + private lateinit var mapper: PropertyEtsMapper + private lateinit var searcher: UsvmPropertySearcher + + @JvmStatic + @BeforeAll + fun loadFixture() { + val source = testResourcePath("/usvm/UsvmPreconditionFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + scene = EtsScene(listOf(file)) + + mapper = PropertyEtsMapper(scene = scene, sourceRoots = listOf(source.parent)) + searcher = UsvmPropertySearcher(scene) + } + } +} diff --git a/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionCapabilityTest.kt b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionCapabilityTest.kt new file mode 100644 index 0000000000..8e1bdd2900 --- /dev/null +++ b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmProjectionCapabilityTest.kt @@ -0,0 +1,244 @@ +package org.usvm.ts.pbt.usvm + +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.ProjectionLevel +import org.usvm.ts.pbt.backend.PropertyCapabilityLevel +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.EtsMappingDiagnostic +import org.usvm.ts.pbt.mapping.EtsMappingStatus +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.ConstantDomain +import org.usvm.ts.pbt.model.ExecutionKind +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.NumberDomain +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.StringDomain +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.testResourcePath +import kotlin.test.assertEquals + +class UsvmProjectionCapabilityTest { + @Test + fun `reports scalar fidelity and type mismatches`() { + val cases = listOf( + Case(BooleanDomain, "acceptsBoolean", ProjectionLevel.EXACT, emptyList()), + Case(IntegerDomain(min = -2, max = 3), "acceptsNumber", ProjectionLevel.EXACT, emptyList()), + Case(NumberDomain(allowNaN = false), "acceptsNumber", ProjectionLevel.EXACT, emptyList()), + Case(StringDomain(maxLength = 4), "acceptsString", ProjectionLevel.EXACT, emptyList()), + Case( + ConstantDomain(JsConcreteValue.Boolean(value = true)), + "acceptsBoolean", + ProjectionLevel.EXACT, + emptyList(), + ), + Case(IntegerDomain(), "acceptsString", ProjectionLevel.UNSUPPORTED, listOf("inputs[0].domain")), + ) + + cases.forEach { case -> + val capability = resolve(case.domain, case.exportName) + + assertEquals(case.level, capability.symbolic.level, case.exportName) + assertEquals(case.diagnosticPaths, capability.symbolic.diagnostics.map { it.path }, case.exportName) + } + } + + @Test + fun `recursive domains inherit the least capable nested projection`() { + val optional = resolve( + domain = OptionalDomain(IntegerDomain(min = 0, max = 5)), + exportName = "acceptsOptionalNumber", + ) + val tuple = resolve( + domain = TupleDomain(listOf(IntegerDomain(), StringDomain(maxLength = 3))), + exportName = "acceptsTuple", + ) + val array = resolve( + domain = ArrayDomain(IntegerDomain(), maxLength = 4), + exportName = "acceptsNumberArray", + ) + val oversizedArray = resolve( + domain = ArrayDomain(IntegerDomain(), maxLength = 11), + exportName = "acceptsNumberArray", + options = UsvmProjectionOptions(maxSymbolicCollectionLength = 10), + ) + + assertEquals(ProjectionLevel.EXACT, optional.symbolic.level) + assertEquals(ProjectionLevel.EXACT, tuple.symbolic.level) + assertEquals(emptyList(), tuple.symbolic.diagnostics) + assertEquals(ProjectionLevel.EXACT, array.symbolic.level) + assertEquals(ProjectionLevel.UNSUPPORTED, oversizedArray.symbolic.level) + assertEquals(listOf("inputs[0].domain"), oversizedArray.symbolic.diagnostics.map { it.path }) + } + + @Test + fun `oversized string domains are rejected before symbolic allocation`() { + val capability = resolve( + domain = StringDomain(maxLength = Int.MAX_VALUE), + exportName = "acceptsString", + ) + + assertEquals(ProjectionLevel.UNSUPPORTED, capability.symbolic.level) + assertEquals("usvm.domain.collection.too-large", capability.symbolic.diagnostics.single().code) + } + + @Test + fun `optional references and exception handlers are supported`() { + val optionalArray = resolve( + domain = OptionalDomain(ArrayDomain(IntegerDomain(), minLength = 1, maxLength = 1)), + exportName = "acceptsOptionalNumberArray", + ) + val caughtDirect = resolve(IntegerDomain(), "catchesDirectThrow") + val caughtHelper = resolve(IntegerDomain(), "catchesHelperThrow") + val preconditionManifest = manifest( + domain = IntegerDomain(), + predicateExport = "acceptsNumber", + preconditionExport = "catchesDirectThrow", + ) + val caughtPrecondition = resolver.resolve( + manifest = preconditionManifest, + mapping = mapper.map(preconditionManifest), + concreteCapability = exact(), + ) + + assertEquals(ProjectionLevel.EXACT, optionalArray.symbolic.level) + assertEquals(ProjectionLevel.EXACT, caughtDirect.symbolic.level) + assertEquals(ProjectionLevel.EXACT, caughtHelper.symbolic.level) + assertEquals(ProjectionLevel.EXACT, caughtPrecondition.precondition.level) + } + + @Test + fun `reports unsupported mapping and execution boundaries without classifying property errors`() { + val manifest = manifest(IntegerDomain(), predicateExport = "acceptsNumber") + val mapping = mapper.map(manifest) + val nonExactMapping = mapping.copy( + predicate = mapping.predicate.copy( + status = EtsMappingStatus.UNMAPPED, + targets = emptyList(), + diagnostics = listOf( + EtsMappingDiagnostic( + code = "test.mapping.unmapped", + message = "Synthetic unmapped predicate", + ), + ), + ), + ) + val asyncPreconditionManifest = manifest( + domain = IntegerDomain(), + predicateExport = "acceptsNumber", + preconditionExport = "acceptsNumber", + preconditionExecutionKind = ExecutionKind.ASYNC, + ) + val nonBooleanPreconditionManifest = manifest( + domain = IntegerDomain(), + predicateExport = "acceptsNumber", + preconditionExport = "returnsNumber", + ) + + val nonExact = resolver.resolve(manifest, nonExactMapping, exact()) + val async = resolver.resolve( + asyncPreconditionManifest, + mapper.map(asyncPreconditionManifest), + exact(), + ) + val nonBoolean = resolver.resolve( + nonBooleanPreconditionManifest, + mapper.map(nonBooleanPreconditionManifest), + exact(), + ) + + assertEquals(ProjectionLevel.UNSUPPORTED, nonExact.symbolic.level) + assertEquals( + "predicate", + nonExact.symbolic.diagnostics.single { it.code == "usvm.predicate.mapping.non-exact" }.path, + ) + assertEquals(ProjectionLevel.UNSUPPORTED, async.precondition.level) + assertEquals("precondition", async.precondition.diagnostics.single().path) + assertEquals(ProjectionLevel.EXACT, nonBoolean.precondition.level) + assertEquals(emptyList(), nonBoolean.precondition.diagnostics) + } + + @Test + fun `derives concrete only from a supported concrete projection`() { + val capability = resolve( + domain = ArrayDomain(IntegerDomain(), maxLength = 11), + exportName = "acceptsNumberArray", + concreteCapability = exact(), + options = UsvmProjectionOptions(maxSymbolicCollectionLength = 10), + ) + + assertEquals(PropertyCapabilityLevel.CONCRETE_ONLY, capability.property) + } + + private fun resolve( + domain: org.usvm.ts.pbt.model.PropertyDomain, + exportName: String, + concreteCapability: ProjectionCapability = exact(), + options: UsvmProjectionOptions = UsvmProjectionOptions(), + ): UsvmPropertyProjectionCapability { + val manifest = manifest(domain, predicateExport = exportName) + + return resolver.resolve( + manifest = manifest, + mapping = mapper.map(manifest), + concreteCapability = concreteCapability, + options = options, + ) + } + + private fun manifest( + domain: org.usvm.ts.pbt.model.PropertyDomain, + predicateExport: String, + preconditionExport: String? = null, + preconditionExecutionKind: ExecutionKind = ExecutionKind.SYNC, + ) = PropertyManifest( + propertyId = "usvm.capability.$predicateExport", + inputs = listOf(PropertyInput(name = "value", domain = domain)), + predicate = TypeScriptEntryPoint( + module = "UsvmCapabilityFixture.ts", + exportName = predicateExport, + ), + precondition = preconditionExport?.let { exportName -> + TypeScriptEntryPoint( + module = "UsvmCapabilityFixture.ts", + exportName = exportName, + executionKind = preconditionExecutionKind, + ) + }, + ) + + private fun exact() = ProjectionCapability(level = ProjectionLevel.EXACT) + + private data class Case( + val domain: org.usvm.ts.pbt.model.PropertyDomain, + val exportName: String, + val level: ProjectionLevel, + val diagnosticPaths: List, + ) + + companion object { + private lateinit var mapper: PropertyEtsMapper + private val resolver = UsvmProjectionCapabilityResolver() + + @JvmStatic + @BeforeAll + fun loadFixture() { + val source = testResourcePath("/usvm/UsvmCapabilityFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + + mapper = PropertyEtsMapper( + scene = EtsScene(listOf(file)), + sourceRoots = listOf(source.parent), + ) + } + } +} diff --git a/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearcherTest.kt b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearcherTest.kt new file mode 100644 index 0000000000..ffd0fe5a0c --- /dev/null +++ b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmPropertySearcherTest.kt @@ -0,0 +1,385 @@ +package org.usvm.ts.pbt.usvm + +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.usvm.UMachineOptions +import org.usvm.machine.TsOptions +import org.usvm.ts.pbt.backend.ProjectionCapability +import org.usvm.ts.pbt.backend.ProjectionLevel +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ArrayDomain +import org.usvm.ts.pbt.model.BooleanDomain +import org.usvm.ts.pbt.model.ConstantDomain +import org.usvm.ts.pbt.model.ExecutionKind +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.PropertyId +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.StringDomain +import org.usvm.ts.pbt.model.TupleDomain +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.model.contains +import org.usvm.ts.pbt.testResourcePath +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue +import kotlin.time.Duration + +class UsvmPropertySearcherTest { + @Test + fun `reports when no violation is reachable`() { + val manifest = manifest(predicateExport = "validProperty") + + val result = search(manifest) + + assertEquals(UsvmPropertySearchStatus.NO_VIOLATION_REACHED, result.status) + assertEquals(PropertyId(manifest.propertyId), result.propertyId) + assertNull(result.target) + assertNull(result.inputs) + } + + @Test + fun `finds false predicate and resolves a candidate input`() { + val manifest = manifest(predicateExport = "violatedProperty") + + val result = search(manifest) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, result.status) + assertEquals(UsvmPropertyViolationTarget.PREDICATE_FALSE, result.target) + assertEquals(listOf(JsConcreteValue.number(2.0)), result.inputs) + } + + @Test + fun `applies mapped precondition before searching the predicate`() { + val manifest = manifest( + predicateExport = "signedOneProperty", + preconditionExport = "positive", + ) + + val result = search(manifest) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, result.status) + assertEquals(listOf(JsConcreteValue.number(1.0)), result.inputs) + } + + @Test + fun `distinguishes rejected and exceptional preconditions from predicate violations`() { + val rejected = manifest( + predicateExport = "violatedProperty", + preconditionExport = "falsePrecondition", + ) + val exceptional = manifest( + predicateExport = "violatedProperty", + preconditionExport = "throwingPrecondition", + ) + + val rejectedResult = search(rejected) + val exceptionalResult = search(exceptional) + + assertEquals(UsvmPropertySearchStatus.PRECONDITION_REJECTED, rejectedResult.status) + assertEquals(UsvmPropertySearchStatus.PROPERTY_ERROR, exceptionalResult.status) + assertNull(rejectedResult.target) + assertNull(exceptionalResult.target) + assertTrue(exceptionalResult.diagnostics.any { it.code == "usvm.precondition.threw" }) + } + + @Test + fun `supports relational predicates with multiple calls`() { + val manifest = manifest(predicateExport = "relationalProperty") + + val result = search(manifest) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, result.status) + assertEquals(UsvmPropertyViolationTarget.PREDICATE_FALSE, result.target) + assertNotNull(result.inputs) + } + + @Test + fun `classifies unexpected exceptions and supported assertion failures`() { + val unexpected = search(manifest(predicateExport = "unexpectedException")) + val assertion = search(manifest(predicateExport = "assertionFailure")) + + assertEquals(UsvmPropertyViolationTarget.UNEXPECTED_EXCEPTION, unexpected.target) + assertEquals(UsvmPropertyViolationTarget.ASSERTION_FAILURE, assertion.target) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, unexpected.status) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, assertion.status) + } + + @Test + fun `resolves a symbolic string after reaching a violation`() { + val manifest = manifest( + predicateExport = "falseStringProperty", + domain = StringDomain(minLength = 0, maxLength = 3), + ) + + val result = search(manifest) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, result.status) + assertEquals(UsvmPropertyViolationTarget.PREDICATE_FALSE, result.target) + val inputs = assertNotNull(result.inputs) + assertTrue(inputs.single() in StringDomain(minLength = 0, maxLength = 3)) + } + + @Test + fun `resolves string contents and nested array inputs`() { + val stringDomain = StringDomain(minLength = 1, maxLength = 1) + val nestedDomain = ArrayDomain( + element = ArrayDomain( + element = ConstantDomain(JsConcreteValue.number(2.0)), + minLength = 1, + maxLength = 1, + ), + minLength = 1, + maxLength = 1, + ) + val innerArray = JsConcreteValue.Array(elements = listOf(JsConcreteValue.number(2.0))) + val expectedNested = listOf(JsConcreteValue.Array(elements = listOf(innerArray))) + + val string = search(manifest(predicateExport = "stringContentProperty", domain = stringDomain)) + val nested = search(manifest(predicateExport = "nestedArrayProperty", domain = nestedDomain)) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, string.status) + assertEquals(listOf(JsConcreteValue.String(value = "a")), string.inputs) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, nested.status) + assertEquals(expectedNested, nested.inputs) + } + + @Test + fun `preserves nested and optional array inputs through writes`() { + val nestedDomain = ArrayDomain( + element = ArrayDomain( + element = ConstantDomain(JsConcreteValue.number(2.0)), + minLength = 1, + maxLength = 1, + ), + minLength = 1, + maxLength = 1, + ) + val optionalDomain = ArrayDomain( + element = OptionalDomain(value = ConstantDomain(JsConcreteValue.number(2.0))), + minLength = 1, + maxLength = 1, + ) + val innerArray = JsConcreteValue.Array(elements = listOf(JsConcreteValue.number(2.0))) + val expectedNested = JsConcreteValue.Array(elements = listOf(innerArray)) + val expectedOptional = JsConcreteValue.Array(elements = listOf(JsConcreteValue.number(2.0))) + + val nested = search(manifest(predicateExport = "nestedArrayMutationProperty", domain = nestedDomain)) + val optional = search( + manifest(predicateExport = "optionalNumberArrayMutationProperty", domain = optionalDomain), + ) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, nested.status) + assertEquals(listOf(expectedNested), nested.inputs) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, optional.status) + assertEquals(listOf(expectedOptional), optional.inputs) + } + + @Test + fun `resolves string array snapshots and preserves UTF-16 code units`() { + val arrayDomain = ArrayDomain( + element = StringDomain(minLength = 1, maxLength = 1), + minLength = 1, + maxLength = 1, + ) + val loneSurrogate = manifest( + predicateExport = "loneSurrogateProperty", + domain = StringDomain(minLength = 1, maxLength = 1), + ) + val supplementary = manifest( + predicateExport = "supplementaryProperty", + domain = StringDomain(minLength = 2, maxLength = 2), + ) + + val array = search(manifest(predicateExport = "stringArrayMutationProperty", domain = arrayDomain)) + val lone = search(loneSurrogate) + val pair = search(supplementary) + + val expectedArray = JsConcreteValue.Array(elements = listOf(JsConcreteValue.String(value = "a"))) + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, array.status) + assertEquals(listOf(expectedArray), array.inputs) + assertEquals(listOf(JsConcreteValue.String(value = "\uD800")), lone.inputs) + assertEquals(listOf(JsConcreteValue.String(value = "\uD83D\uDE00")), pair.inputs) + } + + @Test + fun `projection respects an explicitly configured engine storage bound`() { + val limited = UsvmPropertySearcher(scene = scene, tsOptions = TsOptions(maxArraySize = 2)) + val manifest = manifest( + predicateExport = "falseStringProperty", + domain = StringDomain(minLength = 3, maxLength = 3), + ) + + val result = limited.search( + manifest = manifest, + mapping = mapper.map(manifest), + concreteCapability = ProjectionCapability(level = ProjectionLevel.EXACT), + ) + + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, result.status) + assertNull(result.inputs) + } + + @Test + fun `preserves engine limitation reasons and a reached candidate`() { + val unsupported = search(manifest(predicateExport = "unsupportedComputedProperty")) + val mixed = search( + manifest(predicateExport = "mixedComputedProperty", domain = IntegerDomain(min = 0, max = 1)), + ) + + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, unsupported.status) + assertTrue(unsupported.diagnostics.any { it.code == "usvm.execution.unsupported" }) + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, mixed.status) + assertEquals(UsvmPropertyViolationTarget.PREDICATE_FALSE, mixed.target) + assertEquals(listOf(JsConcreteValue.number(0.0)), mixed.inputs) + } + + @Test + fun `preserves a reached violation when another path is unsupported`() { + val result = search( + manifest( + predicateExport = "mixedUnsupportedProperty", + domain = IntegerDomain(min = 0, max = 1), + ), + ) + + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, result.status) + assertEquals(UsvmPropertyViolationTarget.PREDICATE_FALSE, result.target) + assertEquals(listOf(JsConcreteValue.number(0.0)), result.inputs) + assertTrue(result.diagnostics.any { it.code == "usvm.execution.unsupported" }) + } + + @Test + fun `executes exception handlers and optional collections`() { + val caughtDirect = search(manifest(predicateExport = "caughtDirectProperty")) + val caughtHelper = search(manifest(predicateExport = "caughtHelperProperty")) + val optionalArray = search( + manifest( + predicateExport = "optionalArrayProperty", + domain = OptionalDomain( + ArrayDomain( + element = ConstantDomain(JsConcreteValue.number(2.0)), + minLength = 1, + maxLength = 1, + ), + ), + ), + ) + + listOf(caughtDirect, caughtHelper, optionalArray).forEach { result -> + assertEquals(UsvmPropertySearchStatus.NO_VIOLATION_REACHED, result.status) + assertNull(result.target) + } + } + + @Test + fun `resolves candidates for common Kotlin domains`() { + val cases = listOf( + "falseBooleanProperty" to BooleanDomain, + "falseOptionalProperty" to OptionalDomain(IntegerDomain(min = -1, max = 1)), + "falseTupleProperty" to TupleDomain( + listOf(IntegerDomain(min = -1, max = 1), BooleanDomain), + ), + "falseNestedTupleProperty" to TupleDomain( + listOf(OptionalDomain(IntegerDomain(min = -1, max = 1)), BooleanDomain), + ), + "falseArrayProperty" to ArrayDomain( + element = IntegerDomain(min = -1, max = 1), + minLength = 1, + maxLength = 3, + ), + ) + + cases.forEach { (predicateExport, domain) -> + val result = search(manifest(predicateExport = predicateExport, domain = domain)) + + assertEquals(UsvmPropertySearchStatus.VIOLATION_REACHED, result.status, predicateExport) + assertTrue(assertNotNull(result.inputs).single() in domain, predicateExport) + } + } + + @Test + fun `reports async predicates as unsupported and non-boolean predicates as property errors`() { + val async = manifest( + predicateExport = "asyncValidProperty", + executionKind = ExecutionKind.ASYNC, + ) + val nonBoolean = manifest(predicateExport = "nonBooleanProperty") + + val asyncResult = search(async) + val nonBooleanResult = search(nonBoolean) + + assertEquals(UsvmPropertySearchStatus.UNSUPPORTED, asyncResult.status) + assertTrue(asyncResult.diagnostics.any { it.code == "usvm.predicate.async" }) + assertEquals(UsvmPropertySearchStatus.PROPERTY_ERROR, nonBooleanResult.status) + assertTrue(nonBooleanResult.diagnostics.any { it.code == "usvm.predicate.result.non-boolean" }) + } + + @Test + fun `distinguishes timeout from exhausted search`() { + val manifest = manifest(predicateExport = "validProperty") + val zeroTimeoutSearcher = UsvmPropertySearcher( + scene = scene, + machineOptions = UMachineOptions(timeout = Duration.ZERO), + ) + + val result = zeroTimeoutSearcher.search( + manifest = manifest, + mapping = mapper.map(manifest), + concreteCapability = ProjectionCapability(level = ProjectionLevel.EXACT), + ) + + assertEquals(UsvmPropertySearchStatus.TIMEOUT, result.status) + } + + private fun search(manifest: PropertyManifest): UsvmPropertySearchResult = searcher.search( + manifest = manifest, + mapping = mapper.map(manifest), + concreteCapability = ProjectionCapability(level = ProjectionLevel.EXACT), + ) + + private fun manifest( + predicateExport: String, + preconditionExport: String? = null, + executionKind: ExecutionKind = ExecutionKind.SYNC, + domain: PropertyDomain = IntegerDomain(min = -3, max = 3), + ) = PropertyManifest( + propertyId = "usvm.search.$predicateExport.${executionKind.name.lowercase()}", + inputs = listOf(PropertyInput(name = "value", domain = domain)), + predicate = TypeScriptEntryPoint( + module = "UsvmPropertySearchFixture.ts", + exportName = predicateExport, + executionKind = executionKind, + ), + precondition = preconditionExport?.let { exportName -> + TypeScriptEntryPoint( + module = "UsvmPropertySearchFixture.ts", + exportName = exportName, + ) + }, + ) + + companion object { + private lateinit var mapper: PropertyEtsMapper + private lateinit var searcher: UsvmPropertySearcher + private lateinit var scene: EtsScene + + @JvmStatic + @BeforeAll + fun loadFixture() { + val source = testResourcePath("/usvm/UsvmPropertySearchFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + scene = EtsScene(listOf(file)) + + mapper = PropertyEtsMapper(scene = scene, sourceRoots = listOf(source.parent)) + searcher = UsvmPropertySearcher(scene) + } + } +} diff --git a/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmScalarDomainProjectorTest.kt b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmScalarDomainProjectorTest.kt new file mode 100644 index 0000000000..65f8fc7e55 --- /dev/null +++ b/usvm-ts-pbt/src/test/kotlin/org/usvm/ts/pbt/usvm/UsvmScalarDomainProjectorTest.kt @@ -0,0 +1,242 @@ +package org.usvm.ts.pbt.usvm + +import io.ksmt.utils.asExpr +import org.jacodb.ets.model.EtsScene +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsFileAutoConvert +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.usvm.StateCollectionStrategy +import org.usvm.UBoolExpr +import org.usvm.UConcreteHeapRef +import org.usvm.UMachineOptions +import org.usvm.machine.TsMachine +import org.usvm.machine.TsOptions +import org.usvm.machine.state.TsState +import org.usvm.ts.pbt.manifest.PropertyManifest +import org.usvm.ts.pbt.mapping.PropertyEtsMapper +import org.usvm.ts.pbt.model.ConstantDomain +import org.usvm.ts.pbt.model.IntegerDomain +import org.usvm.ts.pbt.model.JsConcreteValue +import org.usvm.ts.pbt.model.JsNumber +import org.usvm.ts.pbt.model.NumberDomain +import org.usvm.ts.pbt.model.OptionalDomain +import org.usvm.ts.pbt.model.PropertyDomain +import org.usvm.ts.pbt.model.PropertyInput +import org.usvm.ts.pbt.model.StringDomain +import org.usvm.ts.pbt.model.TypeScriptEntryPoint +import org.usvm.ts.pbt.testResourcePath +import org.usvm.util.mkRegisterStackLValue +import org.usvm.util.readStringLength +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class UsvmScalarDomainProjectorTest { + @Test + fun `bounded integer accepts exactly integral non-negative-zero values inside inclusive bounds`() { + val domain = IntegerDomain(min = -2, max = 3) + + listOf(-2.0, 0.0, 3.0).forEach { value -> + assertTrue(acceptsNumber(domain, value), "Expected $value to be accepted") + } + listOf(-3.0, 4.0, 0.5, Double.NaN, Double.POSITIVE_INFINITY, -0.0).forEach { value -> + assertFalse(acceptsNumber(domain, value), "Expected $value to be rejected") + } + } + + @Test + fun `number bounds and NaN policy use shared JavaScript number semantics`() { + val bounded = NumberDomain( + min = JsNumber.finite(-1.5), + max = JsNumber.finite(2.5), + allowNaN = false, + ) + val unboundedWithNaN = NumberDomain(allowNaN = true) + + assertTrue(acceptsNumber(bounded, -1.5)) + assertTrue(acceptsNumber(bounded, 2.5)) + assertFalse(acceptsNumber(bounded, -1.6)) + assertFalse(acceptsNumber(bounded, 2.6)) + assertFalse(acceptsNumber(bounded, Double.NaN)) + assertTrue(acceptsNumber(unboundedWithNaN, Double.NaN)) + } + + @Test + fun `primitive constants admit only their declared JavaScript value`() { + val booleanDomain = ConstantDomain(JsConcreteValue.Boolean(value = true)) + val numberDomain = ConstantDomain(JsConcreteValue.number(7.25)) + + assertTrue(acceptsBoolean(booleanDomain, value = true)) + assertFalse(acceptsBoolean(booleanDomain, value = false)) + assertTrue(acceptsNumber(numberDomain, value = 7.25)) + assertFalse(acceptsNumber(numberDomain, value = 7.0)) + } + + @Test + fun `optional number admits its nested domain or exactly undefined`() { + val domain = OptionalDomain( + value = IntegerDomain(min = 1, max = 3), + nil = JsConcreteValue.Undefined, + ) + + assertTrue( + acceptsOptional(domain) { state, projection -> + with(state.ctx) { + val value = projection.inputs.single().value as UConcreteHeapRef + val type = value.getFakeType(state.memory) + + mkAnd(type.fpTypeExpr, mkFpEqualExpr(value.extractFp(state.memory), mkFp(2.0, fp64Sort))) + } + }, + ) + assertTrue( + acceptsOptional(domain) { state, projection -> + with(state.ctx) { + val value = projection.inputs.single().value as UConcreteHeapRef + val type = value.getFakeType(state.memory) + + mkAnd(type.refTypeExpr, mkHeapRefEq(value.extractRef(state.memory), mkUndefinedValue())) + } + }, + ) + assertFalse( + acceptsOptional(domain) { state, projection -> + with(state.ctx) { + val value = projection.inputs.single().value as UConcreteHeapRef + val type = value.getFakeType(state.memory) + + mkAnd(type.fpTypeExpr, mkFpEqualExpr(value.extractFp(state.memory), mkFp(4.0, fp64Sort))) + } + }, + ) + assertFalse( + acceptsOptional(domain) { state, projection -> + with(state.ctx) { + val value = projection.inputs.single().value as UConcreteHeapRef + val type = value.getFakeType(state.memory) + + mkAnd(type.refTypeExpr, mkHeapRefEq(value.extractRef(state.memory), mkTsNullValue())) + } + }, + ) + } + + @Test + fun `string projection constrains inclusive UTF-16 length bounds`() { + val domain = StringDomain(minLength = 1, maxLength = 3) + + assertTrue(acceptsStringLength(domain, length = 1)) + assertTrue(acceptsStringLength(domain, length = 3)) + assertFalse(acceptsStringLength(domain, length = 0)) + assertFalse(acceptsStringLength(domain, length = 4)) + } + + private fun acceptsNumber(domain: PropertyDomain, value: Double): Boolean = acceptsScalar( + domain = domain, + exportName = "acceptsNumber", + ) { state -> + with(state.ctx) { + val input = state.memory.read(mkRegisterStackLValue(fp64Sort, 1)).asExpr(fp64Sort) + + mkEq(input, mkFp(value, fp64Sort)) + } + } + + private fun acceptsBoolean(domain: PropertyDomain, value: Boolean): Boolean = acceptsScalar( + domain = domain, + exportName = "acceptsBoolean", + ) { state -> + with(state.ctx) { + val input = state.memory.read(mkRegisterStackLValue(boolSort, 1)).asExpr(boolSort) + + mkEq(input, mkBool(value)) + } + } + + private fun acceptsOptional( + domain: OptionalDomain, + constraint: (TsState, UsvmDeclaredDomainProjection) -> UBoolExpr, + ): Boolean { + val manifest = manifest(domain, exportName = "acceptsOptionalNumber") + val mapping = mapper.map(manifest) + val target = mapping.predicate.targets.single() + + return analyze(target.method) { state -> + val projection = projector.configure(state, manifest.inputs, target.bindings.inputs) + + state.pathConstraints += constraint(state, projection) + } + } + + private fun acceptsStringLength(domain: StringDomain, length: Int): Boolean { + val manifest = manifest(domain, exportName = "acceptsString") + val mapping = mapper.map(manifest) + val target = mapping.predicate.targets.single() + + return analyze(target.method) { state -> + val projection = projector.configure(state, manifest.inputs, target.bindings.inputs) + + with(state.ctx) { + val value = projection.inputs.single().value.asExpr(addressSort) + val projectedLength = state.readStringLength(value) + + state.pathConstraints += mkEq(projectedLength, mkBv(length)) + } + } + } + + private fun acceptsScalar( + domain: PropertyDomain, + exportName: String, + constraint: (TsState) -> UBoolExpr, + ): Boolean { + val manifest = manifest(domain, exportName) + val mapping = mapper.map(manifest) + val target = mapping.predicate.targets.single() + + return analyze(target.method) { state -> + projector.configure(state, manifest.inputs, target.bindings.inputs) + state.pathConstraints += constraint(state) + } + } + + private fun analyze( + method: org.jacodb.ets.model.EtsMethod, + configure: (TsState) -> Unit, + ): Boolean = runCatching { + TsMachine( + scene = scene, + options = UMachineOptions(stateCollectionStrategy = StateCollectionStrategy.ALL), + tsOptions = TsOptions(), + ).use { machine -> + machine.analyze( + methods = listOf(method), + configureInitialState = { _, state -> configure(state) }, + ) + } + }.isSuccess + + private fun manifest(domain: PropertyDomain, exportName: String) = PropertyManifest( + propertyId = "usvm.scalar.$exportName", + inputs = listOf(PropertyInput(name = "value", domain = domain)), + predicate = TypeScriptEntryPoint( + module = "UsvmCapabilityFixture.ts", + exportName = exportName, + ), + ) + + companion object { + private lateinit var scene: EtsScene + private lateinit var mapper: PropertyEtsMapper + private val projector = UsvmDomainProjector() + + @JvmStatic + @BeforeAll + fun loadFixture() { + val source = testResourcePath("/usvm/UsvmCapabilityFixture.ts") + val file = loadEtsFileAutoConvert(source, provider = EtsIrProvider.TS_FRONTEND) + scene = EtsScene(listOf(file)) + mapper = PropertyEtsMapper(scene = scene, sourceRoots = listOf(source.parent)) + } + } +} diff --git a/usvm-ts-pbt/src/test/resources/usvm/UsvmCapabilityFixture.ts b/usvm-ts-pbt/src/test/resources/usvm/UsvmCapabilityFixture.ts new file mode 100644 index 0000000000..b252f65a86 --- /dev/null +++ b/usvm-ts-pbt/src/test/resources/usvm/UsvmCapabilityFixture.ts @@ -0,0 +1,55 @@ +export function acceptsBoolean(value: boolean): boolean { + return value; +} + +export function acceptsNumber(value: number): boolean { + return value > 0; +} + +export function acceptsString(value: string): boolean { + return value.length > 0; +} + +export function acceptsOptionalNumber(value: number | undefined): boolean { + return value === undefined || value > 0; +} + +export function acceptsTuple(value: [number, string]): boolean { + return value.length === 2; +} + +export function acceptsNumberBooleanTuple(value: [number, boolean]): boolean { + return value !== undefined; +} + +export function acceptsNumberArray(value: number[]): boolean { + return value.length > 0; +} + +export function acceptsOptionalNumberArray(value: number[] | undefined): boolean { + return value === undefined || value.length > 0; +} + +export function catchesDirectThrow(_value: number): boolean { + try { + throw "expected"; + } catch { + return true; + } +} + +export function catchesHelperThrow(value: number): boolean { + try { + return throwingHelper(value); + } catch { + return true; + } +} + +function throwingHelper(_value: number): boolean { + throw "expected"; +} + +export function returnsNumber(value: number): number { + return value; +} diff --git a/usvm-ts-pbt/src/test/resources/usvm/UsvmPreconditionFixture.ts b/usvm-ts-pbt/src/test/resources/usvm/UsvmPreconditionFixture.ts new file mode 100644 index 0000000000..5d7e1aa6d7 --- /dev/null +++ b/usvm-ts-pbt/src/test/resources/usvm/UsvmPreconditionFixture.ts @@ -0,0 +1,27 @@ +export function predicate(value: number): boolean { + return value !== 0; +} + +export function isPositive(value: number): boolean { + return value > 0; +} + +export function alwaysFalse(_value: number): boolean { + return false; +} + +export function acceptsNonPositiveOrThrows(value: number): boolean { + if (value > 0) { + throw new Error("positive"); + } + + return true; +} + +export async function asyncIsPositive(value: number): Promise { + return value > 0; +} + +export function returnsNumber(value: number): number { + return value; +} diff --git a/usvm-ts-pbt/src/test/resources/usvm/UsvmPropertySearchFixture.ts b/usvm-ts-pbt/src/test/resources/usvm/UsvmPropertySearchFixture.ts new file mode 100644 index 0000000000..f196473103 --- /dev/null +++ b/usvm-ts-pbt/src/test/resources/usvm/UsvmPropertySearchFixture.ts @@ -0,0 +1,150 @@ +export function validProperty(value: number): boolean { + return value === value; +} + +export function violatedProperty(value: number): boolean { + return value !== 2; +} + +export function positive(value: number): boolean { + return value > 0; +} + +export function signedOneProperty(value: number): boolean { + return value !== 1 && value !== -1; +} + +export function falsePrecondition(_value: number): boolean { + return false; +} + +export function throwingPrecondition(_value: number): boolean { + throw "precondition"; +} + +function identity(value: number): number { + return value; +} + +export function relationalProperty(value: number): boolean { + return identity(value) === identity(value + 1); +} + +export function unexpectedException(_value: number): boolean { + throw "unexpected"; +} + +export function assertionFailure(_value: number): boolean { + throw "AssertionError: expected non-zero"; +} + +export function falseStringProperty(_value: string): boolean { + return false; +} + +export function falseBooleanProperty(_value: boolean): boolean { + return false; +} + +export function falseOptionalProperty(_value: number | undefined): boolean { + return false; +} + +export function falseTupleProperty(_value: [number, boolean]): boolean { + return false; +} + +export function falseNestedTupleProperty(_value: [number | undefined, boolean]): boolean { + return false; +} + +export function falseArrayProperty(_value: number[]): boolean { + return false; +} + +export function mixedUnsupportedProperty(value: number): boolean { + if (value === 0) { + return false; + } + + return Math.sin(value) > 0; +} + +export function caughtDirectProperty(_value: number): boolean { + try { + throw "expected"; + } catch { + return true; + } +} + +export function caughtHelperProperty(value: number): boolean { + try { + return throwingHelper(value); + } catch { + return true; + } +} + +function throwingHelper(_value: number): boolean { + throw "expected"; +} + +export function optionalArrayProperty(value: number[] | undefined): boolean { + return value === undefined || value[0] === 2; +} + +export function nonBooleanProperty(value: number): number { + return value; +} + +export async function asyncValidProperty(value: number): Promise { + return value === value; +} + +export function stringContentProperty(value: string): boolean { + return value !== "a"; +} + +export function nestedArrayProperty(value: number[][]): boolean { + return value[0][0] !== 2; +} + +export function unsupportedComputedProperty(value: number): boolean { + const values = { present: 1 }; + return values[value] === 1; +} + +export function mixedComputedProperty(value: number): boolean { + if (value === 0) { + return false; + } + + return unsupportedComputedProperty(value); +} + +export function stringArrayMutationProperty(values: string[]): boolean { + const old = values[0]; + values[0] = "b"; + return old !== "a"; +} + +export function loneSurrogateProperty(value: string): boolean { + return value !== "\uD800"; +} + +export function supplementaryProperty(value: string): boolean { + return value !== "\uD83D\uDE00"; +} + +export function nestedArrayMutationProperty(values: number[][]): boolean { + const old = values[0][0]; + values[0] = [3]; + return old !== 2 || values[0][0] !== 3; +} + +export function optionalNumberArrayMutationProperty(values: (number | undefined)[]): boolean { + const old = values[0]; + values[0] = undefined; + return old !== 2; +} diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/TsContext.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/TsContext.kt index 15fb6ec19d..054d583873 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/TsContext.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/TsContext.kt @@ -193,7 +193,7 @@ class TsContext( else -> TODO("${type::class.simpleName} is not yet supported: $type") } - fun arrayDescriptorOf(type: EtsArrayType): EtsType { + fun arrayDescriptorOf(type: EtsArrayType): EtsArrayType { val storageElement = type.componentType return when (storageElement) { is EtsBooleanType -> EtsArrayType(EtsBooleanType, dimensions = 1) diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/TsMachine.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/TsMachine.kt index e3c1ff64e8..eecba58aa9 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/TsMachine.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/TsMachine.kt @@ -11,7 +11,10 @@ import org.usvm.UMachineOptions import org.usvm.api.targets.TsTarget import org.usvm.machine.call.TsBuiltInUnknownCallModels import org.usvm.machine.call.TsModelUnknownCallDispatcher +import org.usvm.machine.call.TsResidualCallPolicy +import org.usvm.machine.call.TsUnknownCallDecision import org.usvm.machine.call.TsUnknownCallDispatcher +import org.usvm.machine.call.TsUnknownCallEvent import org.usvm.machine.call.TsUnknownCallModelCatalog import org.usvm.machine.call.deduplicateEtsFilesBySignature import org.usvm.machine.interpreter.TsInterpreter @@ -54,6 +57,16 @@ data class TsAnalysisResult( val unsupportedPaths: List, ) +/** Terminal states and failure metadata for one TypeScript analysis. */ +data class TsMachineAnalysisResult( + val states: List, + val stopReason: TsAnalysisStopReason, + val timedOut: Boolean, + val unsupportedCall: Boolean, + val engineFailed: Boolean, + val unsupportedPaths: List, +) + class TsMachine( scene: EtsScene, override val options: UMachineOptions, @@ -92,10 +105,11 @@ class TsMachine( applicationAndSdkClasses = scene.projectClasses + runtimeScene.sdkClasses, dateNowMilliseconds = tsOptions.dateNowMilliseconds, ) + private val failureTrackingUnknownCallObserver = FailureTrackingUnknownCallObserver(observer) private val resolvedUnknownCallDispatcher = unknownCallDispatcher ?: TsModelUnknownCallDispatcher( models = requireNotNull(resolvedUnknownCallModels), fallback = tsOptions.unknownCallFallback, - observer = observer, + observer = failureTrackingUnknownCallObserver, ) private val interpreter = TsInterpreter( ctx = ctx, @@ -111,17 +125,42 @@ class TsMachine( fun analyze( methods: List, targets: List = emptyList(), - ): List = analyzeWithOutcome(methods = methods, targets = targets).states + configureInitialState: (EtsMethod, TsState) -> Unit = { _, _ -> }, + ): List = analyzeWithMetadata( + methods = methods, + targets = targets, + configureInitialState = configureInitialState, + ).states fun analyzeWithOutcome( methods: List, targets: List = emptyList(), configureInitialState: (EtsMethod, TsState) -> Unit = { _, _ -> }, ): TsAnalysisResult { + val result = analyzeWithMetadata( + methods = methods, + targets = targets, + configureInitialState = configureInitialState, + ) + return TsAnalysisResult( + states = result.states, + stopReason = result.stopReason, + unsupportedPaths = result.unsupportedPaths, + ) + } + + fun analyzeWithMetadata( + methods: List, + targets: List = emptyList(), + configureInitialState: (EtsMethod, TsState) -> Unit = { _, _ -> }, + ): TsMachineAnalysisResult { + interpreter.resetStepFailure() + failureTrackingUnknownCallObserver.reset() + val (initialStates, unsupportedPaths) = createInitialStates(methods, targets, configureInitialState) if (initialStates.isEmpty()) { - return TsAnalysisResult(emptyList(), TsAnalysisStopReason.EXHAUSTED, unsupportedPaths.toList()) + return emptyAnalysisResult(unsupportedPaths) } val methodsToTrackCoverage = @@ -174,6 +213,7 @@ class TsMachine( } val stepsStatistics = StepsStatistics() + var timedOut = false val stopStrategy = object : StopStrategy { val strategy = createStopStrategy( options, @@ -185,7 +225,15 @@ class TsMachine( ) override fun shouldStop(): Boolean { + if (options.timeout <= kotlin.time.Duration.ZERO) { + timedOut = true + return true + } + val result = strategy.shouldStop() + if (result && timeStatistics.runningTime >= options.timeout) { + timedOut = true + } if (result) { logger.warn { "Stop strategy finished execution: ${strategy.stopReason()}" } @@ -213,19 +261,7 @@ class TsMachine( } val supportedObserver = CompositeUMachineObserver(observers) - val outcomeObserver = object : UMachineObserver by supportedObserver { - override fun onStateTerminated(state: TsState, stateReachable: Boolean) { - val unsupportedReason = state.unsupportedReason - if (unsupportedReason != null) { - if (stateReachable) { - unsupportedPaths += unsupportedReason - } - return - } - - supportedObserver.onStateTerminated(state, stateReachable) - } - } + val outcomeObserver = UnsupportedPathsObserver(supportedObserver, unsupportedPaths) run( interpreter, @@ -241,13 +277,25 @@ class TsMachine( TsAnalysisStopReason.STOPPED } - return TsAnalysisResult( + return TsMachineAnalysisResult( states = statesCollector.collectedStates, stopReason = stopReason, + timedOut = timedOut, + unsupportedCall = failureTrackingUnknownCallObserver.pathStopped, + engineFailed = interpreter.stepFailed, unsupportedPaths = unsupportedPaths.toList(), ) } + private fun emptyAnalysisResult(unsupportedPaths: Set) = TsMachineAnalysisResult( + states = emptyList(), + stopReason = TsAnalysisStopReason.EXHAUSTED, + timedOut = false, + unsupportedCall = false, + engineFailed = false, + unsupportedPaths = unsupportedPaths.toList(), + ) + private fun createInitialStates( methods: List, targets: List, @@ -275,3 +323,43 @@ class TsMachine( components.close() } } + +private class FailureTrackingUnknownCallObserver( + private val delegate: TsInterpreterObserver?, +) : TsInterpreterObserver { + var pathStopped: Boolean = false + private set + + override fun onUnknownCall(event: TsUnknownCallEvent) { + val decision = event.decision + val stopsPath = decision is TsUnknownCallDecision.ResidualFallback && + decision.policy == TsResidualCallPolicy.STOP_PATH + if (stopsPath) { + pathStopped = true + } + + delegate?.onUnknownCall(event) + } + + fun reset() { + pathStopped = false + } +} + +/** Keeps excluded satisfiable paths out of the ordinary witness collector. */ +private class UnsupportedPathsObserver( + private val delegate: UMachineObserver, + private val unsupportedPaths: MutableSet, +) : UMachineObserver by delegate { + override fun onStateTerminated(state: TsState, stateReachable: Boolean) { + val unsupportedReason = state.unsupportedReason + if (unsupportedReason != null) { + if (stateReachable) { + unsupportedPaths += unsupportedReason + } + return + } + + delegate.onStateTerminated(state, stateReachable) + } +} diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/TsMethodCall.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/TsMethodCall.kt index 75efa13ba3..6dba203aa2 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/TsMethodCall.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/TsMethodCall.kt @@ -64,3 +64,17 @@ class TsConcreteMethodCallStmt( return "concrete ${callee.signature.enclosingClass.name}::${callee.name}" } } + +/** Resumes the original entry point only when an auxiliary boolean guard returned true. */ +class TsEntryPointGuardResultStmt( + val entryPoint: EtsStmt, +) : EtsStmt { + override val location: EtsStmtLocation + get() = entryPoint.location + + override fun accept(visitor: EtsStmt.Visitor): R { + error("Auxiliary instruction") + } + + override fun toString(): String = "entry-point guard result" +} diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsEntryPointGuard.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsEntryPointGuard.kt new file mode 100644 index 0000000000..930f2bcf6f --- /dev/null +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsEntryPointGuard.kt @@ -0,0 +1,49 @@ +package org.usvm.machine.interpreter + +import io.ksmt.utils.asExpr +import org.usvm.machine.TsContext +import org.usvm.machine.TsEntryPointGuardResultStmt +import org.usvm.machine.state.TsEntryPointGuardOutcome +import org.usvm.machine.state.TsMethodResult +import org.usvm.machine.state.newStmt + +/** Completes the precondition before entering the ordinary source predicate. */ +internal fun TsContext.visitEntryPointGuardResult( + scope: TsStepScope, + stmt: TsEntryPointGuardResultStmt, +) = with(this) { + val result = scope.calcOnState { methodResult } + if (result !is TsMethodResult.Success) { + scope.doWithState { + entryPointGuardActive = false + entryPointGuardOutcome = TsEntryPointGuardOutcome.ERROR + callStack.pop() + } + return@with + } + + val guard = result.value.takeIf { it.sort == boolSort }?.asExpr(boolSort) + if (guard == null) { + scope.doWithState { + entryPointGuardActive = false + entryPointGuardOutcome = TsEntryPointGuardOutcome.ERROR + callStack.pop() + } + return@with + } + + scope.fork( + condition = guard, + blockOnTrueState = { + methodResult = TsMethodResult.NoCall + entryPointGuardActive = false + entryPointGuardOutcome = TsEntryPointGuardOutcome.NONE + newStmt(stmt.entryPoint) + }, + blockOnFalseState = { + entryPointGuardActive = false + entryPointGuardOutcome = TsEntryPointGuardOutcome.REJECTED + callStack.pop() + }, + ) +} diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInitialState.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInitialState.kt new file mode 100644 index 0000000000..debc5bd4e8 --- /dev/null +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInitialState.kt @@ -0,0 +1,170 @@ +package org.usvm.machine.interpreter + +import io.ksmt.utils.asExpr +import mu.KotlinLogging +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsGenericType +import org.jacodb.ets.model.EtsMethod +import org.jacodb.ets.model.EtsNullType +import org.jacodb.ets.model.EtsRefType +import org.jacodb.ets.model.EtsStringType +import org.jacodb.ets.model.EtsTupleType +import org.jacodb.ets.model.EtsType +import org.jacodb.ets.model.EtsUndefinedType +import org.jacodb.ets.model.EtsUnionType +import org.usvm.api.evalTypeEquals +import org.usvm.api.targets.TsTarget +import org.usvm.collections.immutable.internal.MutabilityOwnership +import org.usvm.machine.TsContext +import org.usvm.machine.TsGraph +import org.usvm.machine.TsOptions +import org.usvm.machine.expr.TsUnresolvedSort +import org.usvm.machine.expr.mkNotNullOrUndefined +import org.usvm.machine.state.TsState +import org.usvm.machine.state.localsCount +import org.usvm.machine.state.newStmt +import org.usvm.machine.state.parametersWithThisCount +import org.usvm.machine.state.symbolicStringBackingConstraint +import org.usvm.machine.types.initialInputType +import org.usvm.machine.types.mkFakeValue +import org.usvm.machine.types.toAuxiliaryType +import org.usvm.sizeSort +import org.usvm.solver.UUnknownResult +import org.usvm.targets.UTargetsSet +import org.usvm.util.mkArrayLengthLValue +import org.usvm.util.mkRegisterStackLValue +import org.usvm.util.type +import org.usvm.utils.ensureSat + +private val logger = KotlinLogging.logger {} + +internal fun createInitialState( + ctx: TsContext, + graph: TsGraph, + options: TsOptions, + method: EtsMethod, + targets: List, + configureState: TsState.() -> Unit = {}, +): TsState = with(ctx) { + val state = TsState( + ctx = ctx, + ownership = MutabilityOwnership(), + entrypoint = method, + maxStringLength = options.maxArraySize, + inputPropertyPresence = options.inputPropertyPresence, + targets = UTargetsSet.from(targets), + ) + + state.callStack.push(method, returnSite = null) + state.memory.stack.push(method.parametersWithThisCount, method.localsCount) + state.newStmt(method.cfg.instructions.first()) + + state.memory.types.allocate(mkTsNullValue().address, EtsNullType) + + // TODO check for statics + val thisIdx = 0 + val thisInstanceRef = mkRegisterStackLValue(addressSort, thisIdx) + val thisRef = state.memory.read(thisInstanceRef).asExpr(addressSort) + + state.pathConstraints += mkNotNullOrUndefined(thisRef) + + // TODO not equal but subtype for abstract/interfaces + val enclosingClass = requireNotNull(method.enclosingClass) { "Initial state requires a method owner" } + state.pathConstraints += state.memory.types.evalTypeEquals(thisRef, enclosingClass.type) + + val genericParameters = method.typeParameters.filterIsInstance().associateBy { it.typeName } + method.parameters.forEachIndexed { i, param -> + val idx = i + 1 // +1 because 0 is reserved for `this` + + val ref by lazy { + val lValue = mkRegisterStackLValue(addressSort, idx) + state.memory.read(lValue).asExpr(addressSort) + } + + val parameterType = param.type.initialInputType(genericParameters) + if (parameterType is EtsRefType) { + run { + state.pathConstraints += mkNotNullOrUndefined(ref) + + if (parameterType is EtsArrayType) { + state.pathConstraints += state.memory.types.evalIsSubtype(ref, parameterType) + + val lengthLValue = mkArrayLengthLValue(ref, parameterType) + val length = state.memory.read(lengthLValue).asExpr(sizeSort) + state.pathConstraints += mkBvSignedGreaterOrEqualExpr(length, mkBv(0)) + state.pathConstraints += mkBvSignedLessOrEqualExpr(length, mkBv(options.maxArraySize)) + + return@run + } + + // Tuple inputs are materialized as fixed-size arrays by domain-aware initial-state configurators. + if (parameterType is EtsTupleType) { + return@run + } + + val resolvedParameterType = graph.hierarchy.classesForType(parameterType) + + if (resolvedParameterType.isEmpty()) { + logger.error("Cannot resolve class for parameter type: $parameterType") + return@run // TODO should be an error + } + + // Because of structural equality in TS we cannot determine the exact type + // Therefore, we create information about the fields the type must consist + val types = resolvedParameterType.mapNotNull { it.type.toAuxiliaryType(graph.hierarchy) } + val auxiliaryType = EtsUnionType(types) // TODO error + state.pathConstraints += state.memory.types.evalIsSubtype(ref, auxiliaryType) + } + } + if (parameterType == EtsNullType) { + state.pathConstraints += mkHeapRefEq(ref, mkTsNullValue()) + } + if (parameterType == EtsUndefinedType) { + state.pathConstraints += mkHeapRefEq(ref, mkUndefinedValue()) + } + if (parameterType == EtsStringType) { + state.pathConstraints += mkNotNullOrUndefined(ref) + + state.pathConstraints += state.memory.types.evalTypeEquals(ref, EtsStringType) + + state.pathConstraints += state.symbolicStringBackingConstraint(ref) + state.boundedStringBackingRefs += ref to state.maxStringLength + } + + val parameterSort = typeToSort(parameterType) + if (parameterSort is TsUnresolvedSort) { + // If the parameter type is unresolved, we create a fake object for it + val bool = mkRegisterReading(idx, boolSort) + val fp = mkRegisterReading(idx, fp64Sort) + val referenceValue = mkRegisterReading(idx, addressSort) + val fakeObject = state.mkFakeValue( + scope = null, + boolValue = bool, + fpValue = fp, + refValue = referenceValue, + ) + val lValue = mkRegisterStackLValue(addressSort, idx) + state.memory.write(lValue, fakeObject.asExpr(addressSort), guard = trueExpr) + state.saveSortForLocal(idx, addressSort) + } else { + state.saveSortForLocal(idx, parameterSort) + } + } + + state.configureState() + + state.initializeModel(method) + + state +} + +private fun TsState.initializeModel(method: EtsMethod) { + val solver = ctx.solver() + val solverResult = solver.check(pathConstraints) + if (solverResult is UUnknownResult) { + throw TsInitialStateSolverUnknownException(method) + } + + val model = solverResult.ensureSat().model + models = listOf(model) +} 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 7716244634..a30bd0db0e 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 @@ -12,24 +12,19 @@ import org.jacodb.ets.model.EtsClosureFieldRef import org.jacodb.ets.model.EtsCopyDataPropertiesStmt import org.jacodb.ets.model.EtsDefineAccessorStmt import org.jacodb.ets.model.EtsDefineDataPropertyStmt -import org.jacodb.ets.model.EtsGenericType import org.jacodb.ets.model.EtsIfStmt import org.jacodb.ets.model.EtsInstanceFieldRef import org.jacodb.ets.model.EtsLValue import org.jacodb.ets.model.EtsLocal import org.jacodb.ets.model.EtsMethod import org.jacodb.ets.model.EtsNopStmt -import org.jacodb.ets.model.EtsNullType import org.jacodb.ets.model.EtsPropertyRef import org.jacodb.ets.model.EtsRefType import org.jacodb.ets.model.EtsReturnStmt import org.jacodb.ets.model.EtsStaticFieldRef import org.jacodb.ets.model.EtsStmt -import org.jacodb.ets.model.EtsStringType import org.jacodb.ets.model.EtsThrowStmt import org.jacodb.ets.model.EtsType -import org.jacodb.ets.model.EtsUndefinedType -import org.jacodb.ets.model.EtsUnionType import org.jacodb.ets.model.EtsUnknownType import org.jacodb.ets.utils.DEFAULT_ARK_CLASS_NAME import org.jacodb.ets.utils.DEFAULT_ARK_METHOD_NAME @@ -38,15 +33,14 @@ import org.usvm.StepResult import org.usvm.StepScope import org.usvm.UExpr import org.usvm.UInterpreter -import org.usvm.api.evalTypeEquals import org.usvm.api.initializeArray import org.usvm.api.targets.TsTarget import org.usvm.api.typeStreamOf -import org.usvm.collections.immutable.internal.MutabilityOwnership import org.usvm.forkblacklists.UForkBlackList import org.usvm.isAllocatedConcreteHeapRef import org.usvm.machine.TsConcreteMethodCallStmt import org.usvm.machine.TsContext +import org.usvm.machine.TsEntryPointGuardResultStmt import org.usvm.machine.TsGraph import org.usvm.machine.TsInterpreterObserver import org.usvm.machine.TsOptions @@ -56,7 +50,6 @@ import org.usvm.machine.call.TsUnknownCallFailureReason import org.usvm.machine.call.dispatch import org.usvm.machine.expr.TsExprApproximationResult import org.usvm.machine.expr.TsExprResolver -import org.usvm.machine.expr.TsUnresolvedSort import org.usvm.machine.expr.assignToInstanceField import org.usvm.machine.expr.checkUndefinedOrNullPropertyRead import org.usvm.machine.expr.ensureTruthinessSupported @@ -67,7 +60,6 @@ import org.usvm.machine.expr.handleAssignToLocal import org.usvm.machine.expr.handleAssignToProperty import org.usvm.machine.expr.handleAssignToStaticField import org.usvm.machine.expr.handleDefineDataProperty -import org.usvm.machine.expr.mkNotNullOrUndefined import org.usvm.machine.expr.mkTruthyExpr import org.usvm.machine.expr.normalizeThrownValue import org.usvm.machine.expr.readGlobal @@ -79,26 +71,16 @@ import org.usvm.machine.state.TsState import org.usvm.machine.state.lastStmt import org.usvm.machine.state.localsCount import org.usvm.machine.state.newStmt -import org.usvm.machine.state.parametersWithThisCount import org.usvm.machine.state.prepareRefinedStringBackings import org.usvm.machine.state.returnValue -import org.usvm.machine.state.symbolicStringBackingConstraint -import org.usvm.machine.types.initialInputType -import org.usvm.machine.types.mkFakeValue -import org.usvm.machine.types.toAuxiliaryType import org.usvm.sizeSort -import org.usvm.solver.UUnknownResult -import org.usvm.targets.UTargetsSet import org.usvm.types.TypesResult import org.usvm.types.single import org.usvm.util.executableOverloadImplementation -import org.usvm.util.mkArrayLengthLValue import org.usvm.util.mkFieldLValue -import org.usvm.util.mkRegisterStackLValue import org.usvm.util.resolveDeclaredConstructor import org.usvm.util.resolveEtsMethods import org.usvm.util.type -import org.usvm.utils.ensureSat private val logger = KotlinLogging.logger {} private typealias Reason = TsUnknownCallFailureReason @@ -116,13 +98,20 @@ class TsInterpreter( ) : UInterpreter() { private val forkBlackList: UForkBlackList = UForkBlackList.createDefault() + internal var stepFailed: Boolean = false + private set + + internal fun resetStepFailure() { + stepFailed = false + } override fun step(state: TsState): StepResult { val stmt = state.lastStmt val scope = StepScope(state, forkBlackList) val result = state.methodResult - if (result is TsMethodResult.TsException) { + val isEntryPointGuardResult = stmt is TsEntryPointGuardResultStmt + if (result is TsMethodResult.TsException && !isEntryPointGuardResult) { scope.doWithState { val catchers = stmt.location.method.cfg.catchers(stmt) val catcher = catchers.singleOrNull() @@ -160,6 +149,7 @@ class TsInterpreter( when (stmt) { is TsVirtualMethodCallStmt -> visitVirtualMethodCall(scope, stmt) is TsConcreteMethodCallStmt -> visitConcreteMethodCall(scope, stmt) + is TsEntryPointGuardResultStmt -> ctx.visitEntryPointGuardResult(scope, stmt) is EtsIfStmt -> visitIfStmt(scope, stmt) is EtsReturnStmt -> visitReturnStmt(scope, stmt) is EtsAssignStmt -> visitAssignStmt(scope, stmt) @@ -190,6 +180,7 @@ class TsInterpreter( originalStateAlive = true, ) } catch (e: Exception) { + stepFailed = true if (throwExceptionOnStepFailure) { throw e } @@ -759,118 +750,9 @@ class TsInterpreter( method: EtsMethod, targets: List, configureState: TsState.() -> Unit = {}, - ): TsState = with(ctx) { - val state = TsState( - ctx = ctx, - ownership = MutabilityOwnership(), - entrypoint = method, - maxStringLength = options.maxArraySize, - inputPropertyPresence = options.inputPropertyPresence, - targets = UTargetsSet.from(targets), - ) - - state.callStack.push(method, returnSite = null) - state.memory.stack.push(method.parametersWithThisCount, method.localsCount) - state.newStmt(method.cfg.instructions.first()) - - state.memory.types.allocate(mkTsNullValue().address, EtsNullType) - - // TODO check for statics - val thisIdx = 0 - val thisInstanceRef = mkRegisterStackLValue(addressSort, thisIdx) - val thisRef = state.memory.read(thisInstanceRef).asExpr(addressSort) - - state.pathConstraints += mkNotNullOrUndefined(thisRef) - - // TODO not equal but subtype for abstract/interfaces - state.pathConstraints += state.memory.types.evalTypeEquals(thisRef, method.enclosingClass!!.type) - - val genericParameters = method.typeParameters.filterIsInstance().associateBy { it.typeName } - method.parameters.forEachIndexed { i, param -> - val idx = i + 1 // +1 because 0 is reserved for `this` - val ref by lazy { - val lValue = mkRegisterStackLValue(addressSort, idx) - state.memory.read(lValue).asExpr(addressSort) - } - - val parameterType = param.type.initialInputType(genericParameters) - run { - if (parameterType !is EtsRefType) return@run - - state.pathConstraints += mkNotNullOrUndefined(ref) - - if (parameterType is EtsArrayType) { - state.pathConstraints += state.memory.types.evalIsSubtype(ref, parameterType) - - val lengthLValue = mkArrayLengthLValue(ref, parameterType) - val length = state.memory.read(lengthLValue).asExpr(sizeSort) - state.pathConstraints += mkBvSignedGreaterOrEqualExpr(length, mkBv(0)) - state.pathConstraints += mkBvSignedLessOrEqualExpr(length, mkBv(options.maxArraySize)) - - return@run - } - - val resolvedParameterType = graph.hierarchy.classesForType(parameterType) - - if (resolvedParameterType.isEmpty()) { - logger.error("Cannot resolve class for parameter type: $parameterType") - return@run // TODO should be an error - } - - // Because of structural equality in TS we cannot determine the exact type - // Therefore, we create information about the fields the type must consist - val types = resolvedParameterType.mapNotNull { it.type.toAuxiliaryType(graph.hierarchy) } - val auxiliaryType = EtsUnionType(types) // TODO error - state.pathConstraints += state.memory.types.evalIsSubtype(ref, auxiliaryType) - } - if (parameterType == EtsNullType) { - state.pathConstraints += mkHeapRefEq(ref, mkTsNullValue()) - } - if (parameterType == EtsUndefinedType) { - state.pathConstraints += mkHeapRefEq(ref, mkUndefinedValue()) - } - if (parameterType == EtsStringType) { - state.pathConstraints += mkNotNullOrUndefined(ref) - - state.pathConstraints += state.memory.types.evalTypeEquals(ref, EtsStringType) - - state.pathConstraints += state.symbolicStringBackingConstraint(ref) - state.boundedStringBackingRefs += ref to state.maxStringLength - } - - val parameterSort = typeToSort(parameterType) - if (parameterSort is TsUnresolvedSort) { - // If the parameter type is unresolved, we create a fake object for it - val bool = mkRegisterReading(idx, boolSort) - val fp = mkRegisterReading(idx, fp64Sort) - val ref = mkRegisterReading(idx, addressSort) - val fakeObject = state.mkFakeValue(null, bool, fp, ref) - val lValue = mkRegisterStackLValue(addressSort, idx) - state.memory.write(lValue, fakeObject.asExpr(addressSort), guard = trueExpr) - state.saveSortForLocal(idx, addressSort) - } else { - state.saveSortForLocal(idx, parameterSort) - } - } - - state.configureState() - state.initializeModel(method) - - state - } + ): TsState = createInitialState(ctx, graph, options, method, targets, configureState) // TODO: expand with interpreter implementation private val EtsStmt.nextStmt: EtsStmt? get() = graph.successors(this).firstOrNull() } - -private fun TsState.initializeModel(method: EtsMethod) { - val solver = ctx.solver() - val solverResult = solver.check(pathConstraints) - if (solverResult is UUnknownResult) { - throw TsInitialStateSolverUnknownException(method) - } - - val model = solverResult.ensureSat().model - models = listOf(model) -} diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsState.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsState.kt index 53d14765d2..39874af0cd 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsState.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsState.kt @@ -45,6 +45,13 @@ import org.usvm.targets.UTargetsSet import org.usvm.util.mkStringBackingLValue import org.usvm.util.type +/** Observable completion of an auxiliary entry-point guard. */ +enum class TsEntryPointGuardOutcome { + NONE, + REJECTED, + ERROR, +} + /** * [lValuesToAllocatedFakeObjects] contains records of l-values that were allocated with newly created fake objects. * It is important for result interpreters to be able to restore the order of fake objects allocation and @@ -92,6 +99,12 @@ class TsState( */ var dfltObjectFieldSorts: UPersistentHashMap, USort> = persistentHashMapOf(), + /** True while an auxiliary entry-point guard or one of its callees is executing. */ + var entryPointGuardActive: Boolean = false, + + /** Terminal guard outcome; [TsEntryPointGuardOutcome.NONE] also covers predicate execution. */ + var entryPointGuardOutcome: TsEntryPointGuardOutcome = TsEntryPointGuardOutcome.NONE, + /** * Maps string values to their corresponding heap references that were allocated for string constants. * This tracks which string constants have been initialized in this particular state to avoid @@ -361,6 +374,8 @@ class TsState( boundThis = boundThis, dfltObject = dfltObject, dfltObjectFieldSorts = dfltObjectFieldSorts, + entryPointGuardActive = entryPointGuardActive, + entryPointGuardOutcome = entryPointGuardOutcome, stringConstantAllocatedRefs = stringConstantAllocatedRefs, boundedStringBackingRefs = boundedStringBackingRefs, symbolicStringCandidates = symbolicStringCandidates, diff --git a/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsStateUtils.kt b/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsStateUtils.kt index eae28f6e14..898764e977 100644 --- a/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsStateUtils.kt +++ b/usvm-ts/src/main/kotlin/org/usvm/machine/state/TsStateUtils.kt @@ -4,6 +4,8 @@ import org.jacodb.ets.model.EtsMethod import org.jacodb.ets.model.EtsStmt import org.usvm.UExpr import org.usvm.USort +import org.usvm.machine.TsEntryPointGuardResultStmt +import org.usvm.util.type val TsState.lastStmt: EtsStmt get() = currentStatement @@ -28,6 +30,27 @@ fun TsState.returnValue(valueToReturn: UExpr) { } } +/** Executes [guard] over [arguments] before resuming this state's original entry point. */ +fun TsState.prependBooleanEntryPointGuard( + guard: EtsMethod, + arguments: List>, +) { + require(!entryPointGuardActive) { "An entry-point guard is already active" } + require(arguments.size == guard.parameters.size) { + "Expected ${guard.parameters.size} guard arguments, got ${arguments.size}" + } + + val originalEntryPoint = currentStatement + val receiver = memory.allocConcrete(requireNotNull(guard.enclosingClass).type) + val actualArguments = listOf(receiver) + arguments + + pushSortsForActualArguments(actualArguments) + callStack.push(guard, TsEntryPointGuardResultStmt(originalEntryPoint)) + memory.stack.push(actualArguments.toTypedArray(), guard.localsCount) + entryPointGuardActive = true + newStmt(guard.cfg.instructions.first()) +} + inline val EtsMethod.parametersWithThisCount: Int get() = parameters.size + 1 diff --git a/usvm-ts/src/main/kotlin/org/usvm/util/TsStringValue.kt b/usvm-ts/src/main/kotlin/org/usvm/util/TsStringValue.kt new file mode 100644 index 0000000000..468b8bf18f --- /dev/null +++ b/usvm-ts/src/main/kotlin/org/usvm/util/TsStringValue.kt @@ -0,0 +1,77 @@ +package org.usvm.util + +import io.ksmt.expr.KBitVec16Value +import org.jacodb.ets.model.EtsArrayType +import org.jacodb.ets.model.EtsNumberType +import org.jacodb.ets.model.EtsStringType +import org.jacodb.ets.model.EtsType +import org.usvm.UConcreteHeapRef +import org.usvm.UExpr +import org.usvm.UHeapRef +import org.usvm.api.initializeArray +import org.usvm.api.initializeArrayLength +import org.usvm.api.makeSymbolicPrimitive +import org.usvm.machine.TsSizeSort +import org.usvm.machine.expr.extractInt +import org.usvm.machine.state.TsState +import org.usvm.model.UModelBase +import org.usvm.sizeSort + +/** Creates a string with arbitrary UTF-16 contents within the declared inclusive length bounds. */ +fun TsState.mkSymbolicString(minLength: Int, maxLength: Int): UConcreteHeapRef = with(ctx) { + require(minLength >= 0 && maxLength >= minLength) { "Invalid symbolic string length bounds" } + + val string = memory.allocConcrete(EtsStringType) + val charactersType = EtsArrayType(EtsNumberType, dimensions = 1) + val characters = memory.allocConcrete(charactersType) + val length = makeSymbolicPrimitive(sizeSort) + val meetsMinimum = mkBvSignedGreaterOrEqualExpr(length, mkBv(minLength)) + val meetsMaximum = mkBvSignedLessOrEqualExpr(length, mkBv(maxLength)) + val contents = sequence { + repeat(maxLength) { + yield(makeSymbolicPrimitive(bv16Sort)) + } + } + + memory.initializeArray( + arrayHeapRef = characters, + type = stringBackingArrayDescriptor, + sort = bv16Sort, + sizeSort = sizeSort, + contents = contents, + ) + memory.initializeArrayLength(characters, stringBackingArrayDescriptor, sizeSort, length) + memory.write(mkStringBackingLValue(string), characters, guard = trueExpr) + pathConstraints += mkAnd(meetsMinimum, meetsMaximum) + boundedStringBackingRefs += string to maxLength + + string +} + +/** Reads the same private length region used by ordinary TypeScript string operations. */ +fun TsState.readStringLength(string: UHeapRef): UExpr { + val characters = memory.read(mkStringBackingLValue(string)) + return memory.read(mkStringBackingLengthLValue(characters)) +} + +/** Resolves a solver-selected witness from this state's memory without changing its input snapshot. */ +fun TsState.resolveStringValue(string: UHeapRef, model: UModelBase, maxLength: Int): String = with(ctx) { + val concrete = model.eval(string) as? UConcreteHeapRef + ?: error("String reference did not resolve to a concrete heap reference") + val constant = getStringConstantValue(concrete) + if (constant != null) { + return@with constant + } + + val characters = model.eval(memory.read(mkStringBackingLValue(string))) + val length = model.eval(memory.read(mkStringBackingLengthLValue(characters))).extractInt() + require(length in 0..maxLength) { "Resolved string length $length exceeds its declared domain" } + + buildString(length) { + repeat(length) { index -> + val lValue = mkStringBackingElementLValue(characters, mkBv(index)) + val character = model.eval(memory.read(lValue)) as KBitVec16Value + append(character.shortValue.toInt().toChar()) + } + } +}