@@ -23,9 +23,8 @@ class GreyBoxFuzzer(
23
23
) {
24
24
25
25
private val seeds = SeedCollector ()
26
- private val explorationStageIterations = 100
26
+ private val explorationStageIterations = 50
27
27
private val exploitationStageIterations = 100
28
- private var thisInstance: UtModel ? = null
29
28
30
29
// TODO make it return Sequence<UtExecution>
31
30
suspend fun fuzz (): Sequence <UtExecution > {
@@ -65,62 +64,68 @@ class GreyBoxFuzzer(
65
64
prevMethodCoverage : Set <Int >
66
65
) {
67
66
val parametersToGenericsReplacer = method.parameters.map { it to GenericsReplacer () }
67
+ val thisInstancesHistory = ArrayDeque <ThisInstance >()
68
68
repeat(numberOfIterations) { iterationNumber ->
69
- logger.debug { " Iteration number $iterationNumber " }
70
- if (! methodUnderTest.isStatic && thisInstance == null ) {
71
- thisInstance = generateThisInstance(methodUnderTest.classId.jClass)
72
- }
73
- if (thisInstance != null && iterationNumber != 0 ) {
74
- if (Random .getTrue(20 )) {
75
- logger.debug { " Trying to regenerate this instance" }
76
- generateThisInstance(clazz)?.let { thisInstance = it }
77
- } else if (Random .getTrue(50 ) && thisInstance is UtAssembleModel ) {
78
- thisInstance =
79
- Mutator .regenerateFields(
80
- clazz,
81
- thisInstance as UtAssembleModel ,
82
- classFieldsUsedByFunc.toList()
83
- )
69
+ try {
70
+ logger.debug { " Iteration number $iterationNumber " }
71
+ while (thisInstancesHistory.size > 1 ) {
72
+ thisInstancesHistory.removeLast()
84
73
}
85
- }
86
- /* *
87
- * Replacing unresolved generics to random compatible to bounds type
88
- */
89
- when {
90
- Random .getTrue(10 ) -> parametersToGenericsReplacer.map { it.second.revert() }
91
- Random .getTrue(50 ) -> parametersToGenericsReplacer.map {
92
- it.second.replaceUnresolvedGenericsToRandomTypes(
93
- it.first
94
- )
74
+ if (thisInstancesHistory.isEmpty()) {
75
+ thisInstancesHistory + = generateThisInstance(methodUnderTest.classId)
95
76
}
96
- }
97
- val generatedParameters =
98
- method.parameters.mapIndexed { index, parameter ->
99
- DataGenerator .generate(
100
- parameter,
101
- index,
102
- GreyBoxFuzzerGenerators .sourceOfRandomness,
103
- GreyBoxFuzzerGenerators .genStatus
104
- )
77
+ if (iterationNumber != 0 ) {
78
+ if (Random .getTrue(20 )) {
79
+ logger.debug { " Trying to regenerate this instance" }
80
+ thisInstancesHistory.clear()
81
+ thisInstancesHistory + = generateThisInstance(methodUnderTest.classId)
82
+ } else if (Random .getTrue(50 )) {
83
+ thisInstancesHistory + = Mutator .mutateThisInstance(thisInstancesHistory.last(), classFieldsUsedByFunc.toList())
84
+ }
105
85
}
106
- logger.debug { " Generated params = $generatedParameters " }
107
- logger.debug { " This instance = $thisInstance " }
108
- val stateBefore =
109
- EnvironmentModels (thisInstance, generatedParameters.map { it.utModel }, mapOf ())
110
- try {
111
- val executionResult = execute(stateBefore, methodUnderTest) ? : return @repeat
112
- logger.debug { " Execution result: $executionResult " }
113
- val seedScore =
114
- handleCoverage(
115
- executionResult,
116
- prevMethodCoverage,
117
- methodLinesToCover
118
- )
119
- seeds.addSeed(Seed (thisInstance, generatedParameters, seedScore.toDouble()))
120
- logger.debug { " Execution result: ${executionResult.result} " }
121
- } catch (e: Throwable ) {
122
- logger.debug(e) { " Exception while execution :(" }
123
- return @repeat
86
+ /* *
87
+ * Replacing unresolved generics to random compatible to bounds type
88
+ */
89
+ when {
90
+ Random .getTrue(10 ) -> parametersToGenericsReplacer.map { it.second.revert() }
91
+ Random .getTrue(50 ) -> parametersToGenericsReplacer.map {
92
+ it.second.replaceUnresolvedGenericsToRandomTypes(
93
+ it.first
94
+ )
95
+ }
96
+ }
97
+ val thisInstance = thisInstancesHistory.last()
98
+ val generatedParameters =
99
+ method.parameters.mapIndexed { index, parameter ->
100
+ DataGenerator .generate(
101
+ parameter,
102
+ index,
103
+ GreyBoxFuzzerGenerators .sourceOfRandomness,
104
+ GreyBoxFuzzerGenerators .genStatus
105
+ )
106
+ }
107
+ logger.debug { " Generated params = $generatedParameters " }
108
+ logger.debug { " This instance = $thisInstance " }
109
+ val stateBefore =
110
+ EnvironmentModels (thisInstance.utModelForExecution, generatedParameters.map { it.utModel }, mapOf ())
111
+ try {
112
+ val executionResult = execute(stateBefore, methodUnderTest)
113
+ logger.debug { " Execution result: $executionResult " }
114
+ val seedScore =
115
+ handleCoverage(
116
+ executionResult,
117
+ prevMethodCoverage,
118
+ methodLinesToCover
119
+ )
120
+ seeds.addSeed(Seed (thisInstance, generatedParameters, seedScore.toDouble()))
121
+ logger.debug { " Execution result: ${executionResult.result} " }
122
+ } catch (e: Throwable ) {
123
+ logger.debug(e) { " Exception while execution :(" }
124
+ thisInstancesHistory.clear()
125
+ return @repeat
126
+ }
127
+ } catch (e: FuzzerIllegalStateException ) {
128
+ logger.error(e) { " Something wrong in the fuzzing process" }
124
129
}
125
130
}
126
131
}
@@ -225,32 +230,26 @@ class GreyBoxFuzzer(
225
230
private suspend fun execute (
226
231
stateBefore : EnvironmentModels ,
227
232
methodUnderTest : ExecutableId
228
- ): UtFuzzingConcreteExecutionResult ? =
229
- try {
230
- val executor =
231
- ConcreteExecutor (
232
- UtFuzzingExecutionInstrumentation ,
233
- pathsToUserClasses,
234
- pathsToDependencyClasses
235
- ).apply { this .classLoader = utContext.classLoader }
236
- executor.executeConcretely(methodUnderTest, stateBefore, listOf ())
237
- } catch (e: Throwable ) {
238
- logger.debug { " Exception in $methodUnderTest :( $e " }
239
- null
240
- }
233
+ ): UtFuzzingConcreteExecutionResult = run {
234
+ val executor =
235
+ ConcreteExecutor (
236
+ UtFuzzingExecutionInstrumentation ,
237
+ pathsToUserClasses,
238
+ pathsToDependencyClasses
239
+ ).apply { this .classLoader = utContext.classLoader }
240
+ executor.executeConcretely(methodUnderTest, stateBefore, listOf ())
241
+ }
242
+
241
243
242
- private fun generateThisInstance (clazz : Class <* >) =
243
- try {
244
+ private fun generateThisInstance (classId : ClassId ): ThisInstance =
244
245
if (! methodUnderTest.isStatic) {
245
- DataGenerator .generate (
246
- clazz ,
246
+ DataGenerator .generateThis (
247
+ classId ,
247
248
GreyBoxFuzzerGenerators .sourceOfRandomness,
248
249
GreyBoxFuzzerGenerators .genStatus
249
250
)
250
251
} else {
251
- null
252
+ StaticMethodThisInstance
252
253
}
253
- } catch (_: Throwable ) {
254
- null
255
- }
254
+
256
255
}
0 commit comments