Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.usvm.machine.call.intrinsic

import io.ksmt.expr.KFp64Value
import io.ksmt.utils.asExpr
import io.ksmt.utils.cast
import org.jacodb.ets.model.EtsAnyType
Expand Down Expand Up @@ -38,12 +39,13 @@ import org.usvm.util.initializeArrayKind
import org.usvm.util.isUnmodifiedDenseInputArray
import org.usvm.util.mkArrayLengthLValue

private const val MAX_SOURCE_ARRAY_LENGTH = 16

/** Built-in Array algorithms implemented by ordinary TypeScript bodies. */
internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
private const val CLASS_NAME = "ArrayModels"
private const val PRIMITIVES_CLASS_NAME = "ArrayModelPrimitives"
private const val RESOURCE_NAME = "/org/usvm/machine/call/models/ArrayModels.ts"
private const val MAX_SOURCE_ARRAY_LENGTH = 16
private const val MAX_VARIADIC_ARGUMENTS = 3
private const val MAX_FILL_ARGUMENTS = 3

Expand Down Expand Up @@ -329,7 +331,7 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
id = "ts.array.map",
methodName = "map",
inputAdapter = TsDenseArrayModelSupport.callbackAdapter,
domainGuard = TsArraySlotPrimitives.reverseDomain,
domainGuard = TsArraySlotPrimitives.mapDomain,
),
sourceModel(
id = "ts.array.forEach",
Expand Down Expand Up @@ -408,6 +410,18 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
arity = 1,
implementation = TsArraySlotPrimitives::prepareOwnSlots,
),
primitiveModel(
methodName = "prepareMapOwnSlots",
arity = 1,
implementation = TsArraySlotPrimitives::prepareMapOwnSlots,
unsupportedCallPolicy = TsResidualCallPolicy.STOP_PATH,
),
primitiveModel(
methodName = "allocateMapOutput",
arity = 1,
implementation = { state, inputs -> allocateArray(state, inputs, allowLargerMap = true) },
unsupportedCallPolicy = TsResidualCallPolicy.STOP_PATH,
),
primitiveModel(
methodName = "hasOwnSlot",
arity = 2,
Expand All @@ -421,7 +435,7 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
primitiveModel(
methodName = "allocate",
arity = 1,
implementation = ::allocateArray,
implementation = { state, inputs -> allocateArray(state, inputs) },
),
primitiveModel(
methodName = "allocateLike",
Expand Down Expand Up @@ -451,7 +465,8 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
inputAdapter = inputAdapter,
requiredModelIds = buildSet {
if (methodName in setOf("reverse", "unshift", "map", "forEach")) {
add("ts.array.primitive.prepareOwnSlots")
val prepareSlots = if (methodName == "map") "prepareMapOwnSlots" else "prepareOwnSlots"
add("ts.array.primitive." + prepareSlots)
add("ts.array.primitive.hasOwnSlot")
}
if (methodName == "join") {
Expand All @@ -470,7 +485,7 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
add(PRIMITIVE_GROW_ID)
}
if (methodName == "map") {
add(PRIMITIVE_ALLOCATE_ID)
add("ts.array.primitive.allocateMapOutput")
}
if (methodName in setOf("slice", "concat")) {
add(PRIMITIVE_ALLOCATE_LIKE_ID)
Expand Down Expand Up @@ -555,62 +570,6 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
)
}

private fun allocateArray(
state: TsState,
inputs: List<UExpr<*>>,
): TsUnknownCallModelExecution? = with(state.ctx) {
val fpLength = inputs.singleOrNull()
?.takeIf { value -> value.sort == fp64Sort }
?.asExpr(fp64Sort)
?: return null
val validLength = mkValidArrayLength(fpLength)
val maximumLength = mkFp64(MAX_SOURCE_ARRAY_LENGTH.toDouble())
val withinModelCapacity = mkFpLessOrEqualExpr(
fpLength,
maximumLength,
)
val supportedLength = mkAnd(validLength, withinModelCapacity)
val length = mkFpToUint32AfterValidation(fpLength, validLength).asExpr(sizeSort)
val arrayType = EtsArrayType(EtsUnknownType, dimensions = 1)

// The model covers only valid, bounded one-number lengths. All other inputs remain residual.
val validSuccessor = TsUnknownCallModelSuccessor(
guard = supportedLength,
completion = TsUnknownCallModelCompletion.Normal {
val descriptor = arrayDescriptorOf(arrayType)
val result = memory.allocConcrete(arrayType)
val undefinedSlots = List(MAX_SOURCE_ARRAY_LENGTH) { mkUndefinedValue() }
memory.initializeArray(
arrayHeapRef = result,
type = descriptor,
sort = addressSort,
sizeSort = sizeSort,
contents = undefinedSlots.asSequence(),
)
TsUnresolvedArrayKind.entries.forEach { kind ->
initializeArrayKind(
array = result,
kind = kind,
values = List(MAX_SOURCE_ARRAY_LENGTH) { falseExpr },
)
}
memory.initializeArrayLength(
arrayHeapRef = result,
type = descriptor,
sizeSort = sizeSort,
count = length,
)
// Fresh allocations have known holes until the source algorithm writes each own slot.
knownArrayPropertyPresence += result
result
},
)
TsUnknownCallModelExecution(
successors = listOf(validSuccessor),
residualGuard = mkNot(supportedLength),
)
}

private fun allocateArrayLike(
state: TsState,
inputs: List<UExpr<*>>,
Expand Down Expand Up @@ -684,3 +643,63 @@ internal object TsArrayEtsIrModelFamily : TsBuiltInUnknownCallModelFamily {
private const val PRIMITIVE_ALLOCATE_LIKE_ID = "ts.array.primitive.allocateLike"
private const val UNKNOWN_SIGNATURE_COMPONENT = "%unk"
}

private fun allocateArray(
state: TsState,
inputs: List<UExpr<*>>,
allowLargerMap: Boolean = false,
): TsUnknownCallModelExecution? = with(state.ctx) {
val fpLength = inputs.singleOrNull()
?.takeIf { value -> value.sort == fp64Sort }
?.asExpr(fp64Sort)
?: return null
val concreteLength = (fpLength as? KFp64Value)?.value
val largerMap = allowLargerMap && concreteLength != null && concreteLength > MAX_SOURCE_ARRAY_LENGTH
val maximumSize = if (largerMap) MAX_CONCRETE_MAP_LENGTH else MAX_SOURCE_ARRAY_LENGTH
val validLength = mkValidArrayLength(fpLength)
val maximumLength = mkFp64(maximumSize.toDouble())
val withinModelCapacity = mkFpLessOrEqualExpr(
fpLength,
maximumLength,
)
val supportedLength = mkAnd(validLength, withinModelCapacity)
val length = mkFpToUint32AfterValidation(fpLength, validLength).asExpr(sizeSort)
val arrayType = EtsArrayType(EtsUnknownType, dimensions = 1)

// The model covers only valid, bounded one-number lengths. All other inputs remain residual.
val validSuccessor = TsUnknownCallModelSuccessor(
guard = supportedLength,
completion = TsUnknownCallModelCompletion.Normal {
val descriptor = arrayDescriptorOf(arrayType)
val result = memory.allocConcrete(arrayType)
val undefinedSlots = List(maximumSize) { mkUndefinedValue() }
memory.initializeArray(
arrayHeapRef = result,
type = descriptor,
sort = addressSort,
sizeSort = sizeSort,
contents = undefinedSlots.asSequence(),
)
TsUnresolvedArrayKind.entries.forEach { kind ->
initializeArrayKind(
array = result,
kind = kind,
values = List(maximumSize) { falseExpr },
)
}
memory.initializeArrayLength(
arrayHeapRef = result,
type = descriptor,
sizeSort = sizeSort,
count = length,
)
// Fresh allocations have known holes until the source algorithm writes each own slot.
knownArrayPropertyPresence += result
result
},
)
TsUnknownCallModelExecution(
successors = listOf(validSuccessor),
residualGuard = mkNot(supportedLength),
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.usvm.machine.call.intrinsic

import io.ksmt.expr.KBitVec32Value
import io.ksmt.utils.asExpr
import org.jacodb.ets.model.EtsArrayType
import org.jacodb.ets.model.EtsUnknownType
Expand All @@ -21,12 +22,18 @@ import org.usvm.util.knownArrayIndexPresence
import org.usvm.util.markDenseArrayOwnSlots
import org.usvm.util.mkArrayLengthLValue

internal const val MAX_CONCRETE_MAP_LENGTH = 64

/** Own-slot metadata operations used by the source reverse and unshift algorithms. */
internal object TsArraySlotPrimitives {
val reverseDomain = TsEtsIrUnknownCallModelDomainGuard { state, call, inputs ->
knownSlotGuard(state, call, inputs, additionalSlots = 0)
}

val mapDomain = TsEtsIrUnknownCallModelDomainGuard { state, call, inputs ->
knownSlotGuard(state, call, inputs, additionalSlots = 0, allowLargerMap = true)
}

val unshiftDomain = TsEtsIrUnknownCallModelDomainGuard { state, call, inputs ->
knownSlotGuard(state, call, inputs, additionalSlots = call.arguments.size)
}
Expand All @@ -36,23 +43,36 @@ internal object TsArraySlotPrimitives {
call: TsUnknownCall,
inputs: List<UExpr<*>>,
additionalSlots: Int,
allowLargerMap: Boolean = false,
): UBoolExpr {
val (array, type) = state.concreteArray(call, inputs) ?: return state.ctx.falseExpr
val hasSlotProof = array in state.knownArrayPropertyPresence || state.hasDenseArrayShape(array, type)
if (!hasSlotProof) return state.ctx.falseExpr

return state.boundedArrayGuard(array, type, maximumLength = SOURCE_ARRAY_MODEL_CAPACITY - additionalSlots)
val capacity = ownSlotCapacity(state, array, type, allowLargerMap)
return state.boundedArrayGuard(array, type, maximumLength = capacity - additionalSlots)
}

fun prepareOwnSlots(state: TsState, inputs: List<UExpr<*>>): TsUnknownCallModelExecution? {
fun prepareOwnSlots(state: TsState, inputs: List<UExpr<*>>): TsUnknownCallModelExecution? =
prepareOwnSlots(state, inputs, allowLargerMap = false)

fun prepareMapOwnSlots(state: TsState, inputs: List<UExpr<*>>): TsUnknownCallModelExecution? =
prepareOwnSlots(state, inputs, allowLargerMap = true)

private fun prepareOwnSlots(
state: TsState,
inputs: List<UExpr<*>>,
allowLargerMap: Boolean,
): TsUnknownCallModelExecution? {
val array = inputs.single() as? UConcreteHeapRef ?: return null
val type = state.arrayStorageType(array, EtsArrayType(EtsUnknownType, dimensions = 1)) as? EtsArrayType
?: return null
val hasKnownSlots = array in state.knownArrayPropertyPresence
val hasSlotProof = hasKnownSlots || state.hasDenseArrayShape(array, type)
if (!hasSlotProof) return null

val guard = state.boundedArrayGuard(array, type, maximumLength = SOURCE_ARRAY_MODEL_CAPACITY)
val capacity = ownSlotCapacity(state, array, type, allowLargerMap)
val guard = state.boundedArrayGuard(array, type, maximumLength = capacity)
val successor = TsUnknownCallModelSuccessor(
guard = guard,
completion = TsUnknownCallModelCompletion.Normal {
Expand All @@ -71,6 +91,18 @@ internal object TsArraySlotPrimitives {
)
}

private fun ownSlotCapacity(
state: TsState,
array: UConcreteHeapRef,
type: EtsArrayType,
allowLargerMap: Boolean,
): Int {
if (!allowLargerMap || array !in state.knownArrayPropertyPresence) return SOURCE_ARRAY_MODEL_CAPACITY

val length = state.memory.read(mkArrayLengthLValue(array, type))
return if (length is KBitVec32Value) MAX_CONCRETE_MAP_LENGTH else SOURCE_ARRAY_MODEL_CAPACITY
}

fun hasOwnSlot(state: TsState, inputs: List<UExpr<*>>): TsUnknownCallModelExecution? = with(state.ctx) {
val array = inputs[0] as? UConcreteHeapRef ?: return null
val number = inputs[1].takeIf { it.sort == fp64Sort }?.asExpr(fp64Sort) ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ declare class ArrayModelPrimitives {
static elementString(value: any): string;
static requireDense(receiver: any[]): void;
static prepareOwnSlots(receiver: any[]): void;
static prepareMapOwnSlots(receiver: any[]): void;
static allocateMapOutput(length: number): any[];
static hasOwnSlot(receiver: any[], index: number): boolean;
}

Expand Down Expand Up @@ -71,9 +73,9 @@ export class ArrayModels {
}

static map(receiver: any[], callback: (value: any, index: number, array: any[]) => any): any[] {
ArrayModelPrimitives.prepareOwnSlots(receiver);
ArrayModelPrimitives.prepareMapOwnSlots(receiver);
const length = receiver.length;
const result = ArrayModelPrimitives.allocate(length);
const result = ArrayModelPrimitives.allocateMapOutput(length);
let index = 0;
while (index < length) {
if (ArrayModelPrimitives.hasOwnSlot(receiver, index)) {
Expand Down
Loading
Loading