-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathProviders.kt
More file actions
223 lines (191 loc) · 7.81 KB
/
Providers.kt
File metadata and controls
223 lines (191 loc) · 7.81 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
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
package org.utbot.fuzzing
import mu.KotlinLogging
import kotlin.random.Random
private val logger by lazy { KotlinLogging.logger {} }
/**
* Entry point to run fuzzing.
*/
suspend fun <TYPE, RESULT, DESCRIPTION : Description<TYPE>, FEEDBACK : Feedback<TYPE, RESULT>> runFuzzing(
provider: ValueProvider<TYPE, RESULT, DESCRIPTION>,
description: DESCRIPTION,
random: Random = Random(0),
configuration: Configuration = Configuration(),
handle: suspend (description: DESCRIPTION, values: List<RESULT>) -> FEEDBACK
) {
BaseFuzzing(listOf(provider), handle).fuzz(description, random, configuration)
}
/**
* Implements base concepts that use providers to generate values for some types.
*
* @param providers is a list of "type to values" generator
* @param exec this function is called when fuzzer generates values of type R to run it with target program.
*/
class BaseFuzzing<T, R, D : Description<T>, F : Feedback<T, R>>(
val providers: List<ValueProvider<T, R, D>>,
val exec: suspend (description: D, values: List<R>) -> F
) : Fuzzing<T, R, D, F> {
constructor(vararg providers: ValueProvider<T, R, D>, exec: suspend (description: D, values: List<R>) -> F) : this(providers.toList(), exec)
override fun enrich(description: D, type: T, scope: Scope) {
providers.asSequence().forEach {
it.enrich(description, type, scope)
}
}
override fun generate(description: D, type: T): Sequence<Seed<T, R>> {
return providers.asSequence().flatMap { provider ->
try {
if (provider.accept(type)) {
provider.generate(description, type)
} else {
emptySequence()
}
} catch (t: Throwable) {
logger.error(t) { "Error occurs in value provider: $provider" }
emptySequence()
}
}
}
override suspend fun handle(description: D, values: List<R>): F {
return exec(description, values)
}
}
/**
* Value provider generates [Seed] and has other methods to combine providers.
*/
fun interface ValueProvider<T, R, D : Description<T>> {
fun enrich(description: D, type: T, scope: Scope) {}
/**
* Generate a sequence of [Seed] that is merged with values generated by other provider.
*/
fun generate(description: D, type: T): Sequence<Seed<T, R>>
/**
* Validates if this provider is applicable to some type.
*/
fun accept(type: T): Boolean = true
/**
* Combines this model provider with `anotherValueProviders` into one instance.
*
* This model provider is called before `anotherValueProviders`.
*/
infix fun with(anotherValueProvider: ValueProvider<T, R, D>): ValueProvider<T, R, D> {
fun toList(m: ValueProvider<T, R, D>) = if (m is Combined<T, R, D>) m.providers else listOf(m)
return Combined(toList(this) + toList(anotherValueProvider))
}
/**
* Removes `anotherValueProviders<T, R` from current one.
*/
fun except(anotherValueProvider: ValueProvider<T, R, D>): ValueProvider<T, R, D> {
return except { it == anotherValueProvider }
}
/**
* Removes providers matching [filter] from the current one.
*/
fun except(filter: (ValueProvider<T, R, D>) -> Boolean): ValueProvider<T, R, D> =
map { if (filter(it)) Combined(emptyList()) else it }
/**
* Applies [transform] for current provider
*/
fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(this)
/**
* Uses fallback value provider in case when 'this' one failed to generate any value.
*/
fun withFallback(fallback: ValueProvider<T, R, D>) : ValueProvider<T, R, D> {
return Fallback(this, fallback)
}
/**
* Creates a new value provider that creates default value if no values are generated by this provider.
*/
fun withFallback(fallbackSupplier: (T) -> Seed<T, R>) : ValueProvider<T, R, D> {
return withFallback { _, type ->
sequenceOf(fallbackSupplier(type))
}
}
fun letIf(flag: Boolean, block: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>) : ValueProvider<T, R, D> {
return if (flag) block(this) else this
}
/**
* Checks if current provider has fallback and return the initial provider.
*
* If the initial provider is also fallback, then it is unwrapped too.
*
* @return unwrapped provider or this if it is not fallback
*/
fun unwrapIfFallback(): ValueProvider<T, R, D> {
return if (this is Fallback<T, R, D>) { provider.unwrapIfFallback() } else { this }
}
private class Fallback<T, R, D : Description<T>>(
val provider: ValueProvider<T, R, D>,
val fallback: ValueProvider<T, R, D>,
): ValueProvider<T, R, D> {
override fun enrich(description: D, type: T, scope: Scope) {
provider.enrich(description, type, scope)
// Enriching scope by fallback value provider in this point is not quite right,
// but it doesn't look as a problem right now.
fallback.enrich(description, type, scope)
}
override fun generate(description: D, type: T): Sequence<Seed<T, R>> {
val default = if (provider.accept(type)) provider.generate(description, type) else emptySequence()
return if (default.iterator().hasNext()) {
default
} else if (fallback.accept(type)) {
fallback.generate(description, type)
} else {
emptySequence()
}
}
override fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(Fallback(provider.map(transform), fallback.map(transform)))
}
/**
* Wrapper class that delegates implementation to the [providers].
*/
private class Combined<T, R, D : Description<T>>(providers: List<ValueProvider<T, R, D>>): ValueProvider<T, R, D> {
val providers: List<ValueProvider<T, R, D>>
init {
// Flattening to avoid Combined inside Combined (for correct work of except, map, etc.)
this.providers = providers.flatMap {
if (it is Combined)
it.providers
else
listOf(it)
}
}
override fun enrich(description: D, type: T, scope: Scope) {
providers.forEach { it.enrich(description, type, scope) }
}
override fun accept(type: T): Boolean {
return providers.any { it.accept(type) }
}
override fun generate(description: D, type: T): Sequence<Seed<T, R>> = sequence {
providers.asSequence().filter { it.accept(type) }.forEach { provider ->
provider.generate(description, type).forEach {
yield(it)
}
}
}
override fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(Combined(providers.map { it.map(transform) }))
}
companion object {
fun <T, R, D : Description<T>> of(valueProviders: List<ValueProvider<T, R, D>>): ValueProvider<T, R, D> {
return Combined(valueProviders)
}
}
}
/**
* Simple value provider for a concrete type.
*
* @param type that is used as a filter to call this provider
* @param generate yields values for the type
*/
class TypeProvider<T, R, D : Description<T>>(
val type: T,
val generate: suspend SequenceScope<Seed<T, R>>.(description: D, type: T) -> Unit
) : ValueProvider<T, R, D> {
override fun accept(type: T) = this.type == type
override fun generate(description: D, type: T) = sequence {
if (accept(type)) {
this.generate(description, type)
}
}
}