Skip to content

Commit b618d6b

Browse files
committed
Improvements after review:
* Added GenericArrayType * Fixed the logic around number of generated values in recursive calls
1 parent 884fad0 commit b618d6b

File tree

10 files changed

+27
-117
lines changed

10 files changed

+27
-117
lines changed

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ import kotlinx.coroutines.yield
104104
import org.utbot.framework.plugin.api.UtExecutionSuccess
105105
import org.utbot.framework.plugin.api.UtLambdaModel
106106
import org.utbot.framework.plugin.api.util.executable
107-
import org.utbot.fuzzer.FuzzedType
108107
import org.utbot.fuzzer.toFuzzerType
109108

110109
val logger = KotlinLogging.logger {}
@@ -429,7 +428,7 @@ class UtBotSymbolicEngine(
429428
packageName = classUnderTest.packageName
430429
val names = graph.body.method.tags.filterIsInstance<ParamNamesTag>().firstOrNull()?.names
431430
parameterNameMap = { index -> names?.getOrNull(index) }
432-
fuzzerType = { toFuzzerType(methodUnderTest.executable.genericParameterTypes[it]) }
431+
fuzzerType = { try { toFuzzerType(methodUnderTest.executable.genericParameterTypes[it]) } catch (_: Throwable) { null } }
433432
}
434433
val coveredInstructionTracker = Trie(Instruction::id)
435434
val coveredInstructionValues = linkedMapOf<Trie.Node<Instruction>, List<FuzzedValue>>()

utbot-framework/src/main/kotlin/org/utbot/fuzzer/FuzzerFunctions.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.fuzzer
22

33
import mu.KotlinLogging
4+
import org.utbot.framework.plugin.api.ClassId
45
import org.utbot.framework.plugin.api.classId
56
import org.utbot.framework.plugin.api.util.booleanClassId
67
import org.utbot.framework.plugin.api.util.byteClassId
@@ -48,6 +49,7 @@ import soot.jimple.internal.JStaticInvokeExpr
4849
import soot.jimple.internal.JTableSwitchStmt
4950
import soot.jimple.internal.JVirtualInvokeExpr
5051
import soot.toolkits.graph.ExceptionalUnitGraph
52+
import java.lang.reflect.GenericArrayType
5153
import java.lang.reflect.ParameterizedType
5254
import java.lang.reflect.Type
5355
import java.lang.reflect.TypeVariable
@@ -304,6 +306,16 @@ fun toFuzzerType(type: Type): FuzzedType {
304306
is WildcardType -> type.upperBounds.firstOrNull()?.let(::toFuzzerType) ?: FuzzedType(objectClassId)
305307
is TypeVariable<*> -> type.bounds.firstOrNull()?.let(::toFuzzerType) ?: FuzzedType(objectClassId)
306308
is ParameterizedType -> FuzzedType((type.rawType as Class<*>).id, type.actualTypeArguments.map { toFuzzerType(it) })
309+
is GenericArrayType -> {
310+
val genericComponentType = type.genericComponentType
311+
val fuzzerType = toFuzzerType(genericComponentType)
312+
val classId = if (genericComponentType !is GenericArrayType) {
313+
ClassId("[L${fuzzerType.classId.name};", fuzzerType.classId)
314+
} else {
315+
ClassId("[" + fuzzerType.classId.name, fuzzerType.classId)
316+
}
317+
FuzzedType(classId)
318+
}
307319
is Class<*> -> FuzzedType(type.id)
308320
else -> error("Unknown type: $type")
309321
}

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/FuzzedMethodDescription.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ class FuzzedMethodDescription(
4949
*
5050
* Fuzzer doesn't care about interconnection between these types, therefore it waits
5151
* that function already has all necessary information to bound this values.
52-
*
53-
* If `K <: Number` and `V <: K` for map this method can return values like:
54-
* `K contains {Number, Integer} and V contains only {Integer}`.
5552
*/
5653
var fuzzerType: (Int) -> FuzzedType? = { null }
5754

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/FuzzedType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.utbot.framework.plugin.api.ClassId
77
*
88
* Currently, there's some limitation for generics that are supported:
99
* 1. Only concrete types and collections are supported
10-
* 2. No relative types like: `Map<T, V extends V>`
10+
* 2. No relative types like: `Map<T, V extends T>`
1111
*
1212
* Note, that this class can be replaced by API mechanism for collecting parametrized types,
1313
* but at the moment it doesn't fully support all necessary operations.

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/Fuzzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fun <T> Sequence<List<FuzzedValue>>.withMutations(statistics: FuzzerStatistics<T
164164
fun fuzzNumbers(concreteValues: Collection<FuzzedConcreteValue>, vararg defaultValues: Int, filter: (Number) -> Boolean = { true }): Sequence<Int> {
165165
val description = FuzzedMethodDescription("helper: number fuzzing", voidClassId, listOf(intClassId), concreteValues)
166166
val fuzzedValues = fuzz(description, ConstantsModelProvider)
167-
.mapNotNull { ((it[0].model as? UtPrimitiveModel)?.value as? Number) }
167+
.mapNotNull { ((it.single().model as? UtPrimitiveModel)?.value as? Number) }
168168
.filter(filter)
169169
.map { it.toInt() }
170170
return (defaultValues.asSequence() + fuzzedValues).distinct()

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/ArrayModelProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ArrayModelProvider(
2929
lengths.forEach { length ->
3030
yield(ModelConstructor(listOf(FuzzedType(classId.elementClassId!!)), repeat = length) { values ->
3131
createFuzzedArrayModel(classId, length, values.map { it.model } )
32+
}.apply {
33+
limit = totalLimit / branchingLimit
3234
})
3335
}
3436
}

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/CollectionModelProvider.kt

Lines changed: 0 additions & 104 deletions
This file was deleted.

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/CollectionWithModificationModelProvider.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class CollectionWithModificationModelProvider(
1919
private var defaultModificationCount: IntArray = intArrayOf(0, 1, 3)
2020
) : RecursiveModelProvider(idGenerator, recursionDepthLeft) {
2121

22+
init {
23+
totalLimit = 100_000
24+
}
25+
2226
// List of available implementations with modification method to insert values
2327
// Should be listed from more specific interface to more general,
2428
// because suitable info is searched by the list.

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/RecursiveModelProvider.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ data class ModelConstructor(
2424
val neededTypes: List<FuzzedType>,
2525
val repeat: Int = 1,
2626
val createModel: (subModels: List<FuzzedValue>) -> FuzzedValue,
27-
)
27+
) {
28+
var limit: Int = Int.MAX_VALUE
29+
}
2830

2931
/**
3032
* Abstraction for providers that may call other providers recursively inside them. [generate] will firstly get possible
@@ -80,7 +82,7 @@ abstract class RecursiveModelProvider(
8082
yieldAllValues(listOf(index), creator.recursiveCall(description))
8183
}
8284
}
83-
}
85+
}.take(totalLimit)
8486

8587
private fun ModelConstructor.recursiveCall(baseMethodDescription: FuzzedMethodDescription): Sequence<FuzzedValue> {
8688
// when no parameters are needed just call model creator once,
@@ -101,7 +103,7 @@ abstract class RecursiveModelProvider(
101103
}
102104
return fuzz(syntheticMethodDescription, nextModelProvider())
103105
.map { createModel(it) }
104-
.take(totalLimit)
106+
.take(limit)
105107
}
106108

107109
private fun nextModelProvider(): ModelProvider =

utbot-fuzzers/src/test/kotlin/org/utbot/framework/plugin/api/CollectionModelProviderTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ class CollectionModelProviderTest {
5050
CollectionWithModificationModelProvider(
5151
TestIdentityPreservingIdGenerator,
5252
defaultModificationCount = modifications
53-
).apply {
54-
totalLimit = 1
55-
},
53+
),
5654
parameters = listOf(Collection::class.id),
5755
) {
5856
fuzzerType = { FuzzedType(Collection::class.id, listOf(FuzzedType(objectClassId))) }

0 commit comments

Comments
 (0)