-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathJavaLanguage.kt
255 lines (240 loc) · 9.88 KB
/
JavaLanguage.kt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package org.utbot.fuzzing
import mu.KotlinLogging
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.ExecutableId
import org.utbot.framework.plugin.api.Instruction
import org.utbot.framework.plugin.api.util.*
import org.utbot.fuzzer.*
import org.utbot.fuzzing.providers.*
import org.utbot.fuzzing.utils.Trie
import java.lang.reflect.*
import java.util.concurrent.CancellationException
import java.util.concurrent.TimeUnit
import kotlin.random.Random
private val logger = KotlinLogging.logger {}
typealias JavaValueProvider = ValueProvider<FuzzedType, FuzzedValue, FuzzedDescription>
class FuzzedDescription(
val description: FuzzedMethodDescription,
val tracer: Trie<Instruction, *>,
val typeCache: MutableMap<Type, FuzzedType>,
val random: Random,
val scope: Scope? = null
) : Description<FuzzedType>(
description.parameters.mapIndexed { index, classId ->
description.fuzzerType(index) ?: FuzzedType(classId)
}
) {
val constants: Sequence<FuzzedConcreteValue>
get() = description.concreteValues.asSequence()
override fun clone(scope: Scope): FuzzedDescription {
return FuzzedDescription(description, tracer, typeCache, random, scope)
}
}
fun defaultValueProviders(idGenerator: IdentityPreservingIdGenerator<Int>) = listOf(
BooleanValueProvider,
IntegerValueProvider,
FloatValueProvider,
StringValueProvider,
NumberValueProvider,
(ObjectValueProvider(idGenerator)
with ArrayValueProvider(idGenerator)
with EnumValueProvider(idGenerator)
with ListSetValueProvider(idGenerator)
with MapValueProvider(idGenerator)
with IteratorValueProvider(idGenerator)
with EmptyCollectionValueProvider(idGenerator)
with DateValueProvider(idGenerator))
.withFallback(
AbstractsObjectValueProvider(idGenerator)
with BuilderObjectValueProvider(idGenerator)
),
VoidValueProvider,
NullValueProvider,
)
suspend fun runJavaFuzzing(
idGenerator: IdentityPreservingIdGenerator<Int>,
methodUnderTest: ExecutableId,
constants: Collection<FuzzedConcreteValue>,
names: List<String>,
providers: List<JavaValueProvider> = defaultValueProviders(idGenerator),
exec: suspend (thisInstance: FuzzedValue?, description: FuzzedDescription, values: List<FuzzedValue>) -> BaseFeedback<Trie.Node<Instruction>, FuzzedType, FuzzedValue>
) {
val random = Random(0)
val classUnderTest = methodUnderTest.classId
val name = methodUnderTest.classId.simpleName + "." + methodUnderTest.name
val returnType = methodUnderTest.returnType
val parameters = methodUnderTest.parameters
// For a concrete fuzzing run we need to track types we create.
// Because of generics can be declared as recursive structures like `<T extends Iterable<T>>`,
// we should track them by reference and do not call `equals` and `hashCode` recursively.
val typeCache = hashMapOf<Type, FuzzedType>()
/**
* To fuzz this instance, the class of it is added into head of parameters list.
* Done for compatibility with old fuzzer logic and should be reworked more robust way.
*/
fun createFuzzedMethodDescription(self: ClassId?) = FuzzedMethodDescription(
name, returnType, listOfNotNull(self) + parameters, constants
).apply {
compilableName = if (!methodUnderTest.isConstructor) methodUnderTest.name else null
className = classUnderTest.simpleName
canonicalName = classUnderTest.canonicalName
isNested = classUnderTest.isNested
isStatic = methodUnderTest.isStatic
packageName = classUnderTest.packageName
parameterNameMap = { index ->
when {
self != null && index == 0 -> "this"
self != null -> names.getOrNull(index - 1)
else -> names.getOrNull(index)
}
}
fuzzerType = {
try {
when {
self != null && it == 0 -> toFuzzerType(methodUnderTest.executable.declaringClass, typeCache)
self != null -> toFuzzerType(methodUnderTest.executable.genericParameterTypes[it - 1], typeCache)
else -> toFuzzerType(methodUnderTest.executable.genericParameterTypes[it], typeCache)
}
} catch (_: Throwable) {
null
}
}
shouldMock = { false }
}
val thisInstance = with(methodUnderTest) {
if (!isStatic && !isConstructor) { classUnderTest } else { null }
}
val tracer = Trie(Instruction::id)
val descriptionWithOptionalThisInstance = FuzzedDescription(createFuzzedMethodDescription(thisInstance), tracer, typeCache, random)
val descriptionWithOnlyParameters = FuzzedDescription(createFuzzedMethodDescription(null), tracer, typeCache, random)
val start = System.nanoTime()
try {
logger.info { "Starting fuzzing for method: $methodUnderTest" }
logger.info { "\tuse thisInstance = ${thisInstance != null}" }
logger.info { "\tparameters = $parameters" }
var totalExecutionCalled = 0
runFuzzing(
provider = ValueProvider.of(providers),
description = descriptionWithOptionalThisInstance, random,
configuration = Configuration()
) { _, t ->
totalExecutionCalled++
if (thisInstance == null) {
exec(null, descriptionWithOnlyParameters, t)
} else {
exec(t.first(), descriptionWithOnlyParameters, t.drop(1))
}
}
val totalFuzzingTime = System.nanoTime() - start
logger.info { "Finishing fuzzing for method: $methodUnderTest in ${TimeUnit.NANOSECONDS.toMillis(totalFuzzingTime)} ms" }
logger.info { "\tTotal execution called: $totalExecutionCalled" }
} catch (ce: CancellationException) {
val totalFuzzingTime = System.nanoTime() - start
logger.info { "Fuzzing is stopped because of timeout. Total execution time: ${TimeUnit.NANOSECONDS.toMillis(totalFuzzingTime)} ms" }
logger.debug(ce) { "\tStacktrace:" }
} catch (t: Throwable) {
logger.info(t) { "Fuzzing is stopped because of an error" }
}
}
/**
* Traverse though type hierarchy of this fuzzed type.
* Ignores all set [FuzzedType.generics] of source type.
*
* todo Process types like `Fuzzed[Any, generics = T1, T2]` to match those T1 and T2 types with superclass and interfaces
*/
internal fun FuzzedType.traverseHierarchy(typeCache: MutableMap<Type, FuzzedType>): Sequence<FuzzedType> = sequence {
val typeQueue = mutableListOf(this@traverseHierarchy)
var index = 0
while (typeQueue.isNotEmpty()) {
val next = typeQueue.removeFirst()
if (index++ > 0) {
yield(next)
}
val jClass = next.classId.jClass
val superclass = jClass.genericSuperclass
if (superclass != null) {
typeQueue += toFuzzerType(superclass, typeCache)
}
typeQueue += jClass.genericInterfaces.asSequence().map { toFuzzerType(it, typeCache) }
}
}
/**
* Resolve a fuzzer type that has class info and some generics.
*
* @param type to be resolved
* @param cache is used to store same [FuzzedType] for same java types
*/
internal fun toFuzzerType(type: Type, cache: MutableMap<Type, FuzzedType>): FuzzedType {
return toFuzzerType(
type = type,
classId = { t -> toClassId(t, cache) },
generics = { t -> toGenerics(t) },
cache = cache,
)
}
/**
* Resolve a fuzzer type that has class info and some generics.
*
* Cache is used to stop recursive call in case of some recursive class definition like:
*
* ```
* public <T extends Iterable<T>> call(T type) { ... }
* ```
*
* @param type to be resolved into a fuzzed type.
* @param classId is a function that produces classId by general type.
* @param generics is a function that produced a list of generics for this concrete type.
* @param cache is used to store all generated types.
*/
private fun toFuzzerType(
type: Type,
classId: (type: Type) -> ClassId,
generics: (parent: Type) -> Array<out Type>,
cache: MutableMap<Type, FuzzedType>,
): FuzzedType {
val g = mutableListOf<FuzzedType>()
val t = type.replaceWithUpperBoundUntilNotTypeVariable()
var target = cache[t]
if (target == null) {
target = FuzzedType(classId(t), g)
cache[t] = target
g += generics(t).map {
toFuzzerType(it, classId, generics, cache)
}
}
return target
}
internal fun Type.replaceWithUpperBoundUntilNotTypeVariable() : Type {
var type: Type = this
while (type is TypeVariable<*>) {
type = type.bounds.firstOrNull() ?: java.lang.Object::class.java
}
return type
}
private fun toClassId(type: Type, cache: MutableMap<Type, FuzzedType>): ClassId {
return when (type) {
is WildcardType -> type.upperBounds.firstOrNull()?.let { toClassId(it, cache) } ?: objectClassId
is GenericArrayType -> {
val genericComponentType = type.genericComponentType
val classId = toFuzzerType(genericComponentType, cache).classId
if (genericComponentType !is GenericArrayType) {
ClassId("[L${classId.name};", classId)
} else {
ClassId("[" + classId.name, classId)
}
}
is ParameterizedType -> (type.rawType as Class<*>).id
is Class<*> -> type.id
is TypeVariable<*> -> type.bounds.firstOrNull()?.let { toClassId(it, cache) } ?: objectClassId
else -> error("unknown type: $type")
}
}
private fun toGenerics(t: Type) : Array<out Type> {
return when (t) {
is WildcardType -> t.upperBounds.firstOrNull()?.let { toGenerics(it) } ?: emptyArray()
is GenericArrayType -> arrayOf(t.genericComponentType)
is ParameterizedType -> t.actualTypeArguments
is Class<*> -> t.typeParameters
else -> emptyArray()
}
}