-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathReadLength.kt
More file actions
65 lines (56 loc) · 1.96 KB
/
ReadLength.kt
File metadata and controls
65 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.usvm.machine.expr
import io.ksmt.sort.KFp64Sort
import io.ksmt.utils.asExpr
import org.jacodb.ets.model.EtsAnyType
import org.jacodb.ets.model.EtsArrayType
import org.jacodb.ets.model.EtsLocal
import org.jacodb.ets.model.EtsUnknownType
import org.usvm.UExpr
import org.usvm.UHeapRef
import org.usvm.machine.TsContext
import org.usvm.machine.interpreter.TsStepScope
import org.usvm.sizeSort
import org.usvm.util.mkArrayLengthLValue
// Handles reading the `length` property.
fun TsContext.readLengthProperty(
scope: TsStepScope,
instanceLocal: EtsLocal,
instance: UHeapRef,
maxArraySize: Int,
): UExpr<*>? {
// Determine the array type.
val arrayType: EtsArrayType = when (val type = instanceLocal.type) {
is EtsArrayType -> type
is EtsAnyType, is EtsUnknownType -> {
// If the type is not an array, and explicitly unknown,
// we represent it is an array with unknown element type.
EtsArrayType(EtsUnknownType, dimensions = 1)
}
else -> error("Expected EtsArrayType, EtsAnyType or EtsUnknownType, but got: $type")
}
// Read the length of the array.
return readArrayLength(scope, instance, arrayType, maxArraySize)
}
// Reads the length of the array and returns it as a fp64 expression.
fun TsContext.readArrayLength(
scope: TsStepScope,
array: UHeapRef,
arrayType: EtsArrayType,
maxArraySize: Int,
): UExpr<KFp64Sort>? {
checkNotFake(array)
// Read the length of the array.
val length = scope.calcOnState {
val lengthLValue = mkArrayLengthLValue(array, arrayType)
memory.read(lengthLValue)
}
// Check that the length is within the allowed bounds.
checkLengthBounds(scope, length, maxArraySize) ?: return null
// Convert the length to fp64.
return mkBvToFpExpr(
sort = fp64Sort,
roundingMode = fpRoundingModeSortDefaultValue(),
value = length.asExpr(sizeSort),
signed = true,
)
}