@@ -13,7 +13,6 @@ import org.utbot.framework.util.sootMethod
13
13
import org.utbot.instrumentation.ConcreteExecutor
14
14
import java.lang.reflect.Field
15
15
import java.lang.reflect.Method
16
- import java.lang.reflect.Modifier
17
16
import kotlin.random.Random
18
17
19
18
class GreyBoxFuzzer (
@@ -23,15 +22,15 @@ class GreyBoxFuzzer(
23
22
) {
24
23
25
24
private val seeds = SeedCollector ()
26
- private val explorationStageIterations = 50
25
+ private val explorationStageIterations = 100
27
26
private val exploitationStageIterations = 100
28
27
29
28
// TODO make it return Sequence<UtExecution>
30
29
suspend fun fuzz (): Sequence <UtExecution > {
31
30
logger.debug { " Started to fuzz ${methodUnderTest.name} " }
32
31
val javaClazz = methodUnderTest.classId.jClass
33
- val javaMethod = methodUnderTest.sootMethod.toJavaMethod()!!
34
32
val sootMethod = methodUnderTest.sootMethod
33
+ val javaMethod = sootMethod.toJavaMethod()!!
35
34
val classFieldsUsedByFunc = sootMethod.getClassFieldsUsedByFunc(javaClazz)
36
35
val methodLines = sootMethod.activeBody.units.map { it.javaSourceStartLineNumber }.filter { it != - 1 }.toSet()
37
36
val currentCoverageByLines = CoverageCollector .coverage
@@ -43,13 +42,12 @@ class GreyBoxFuzzer(
43
42
javaMethod,
44
43
explorationStageIterations,
45
44
methodLines,
46
- javaClazz,
47
45
classFieldsUsedByFunc,
48
46
methodUnderTest,
49
47
currentCoverageByLines
50
48
)
51
49
logger.debug { " SEEDS AFTER EXPLORATION STAGE = ${seeds.seedsSize()} " }
52
- // exploitationStage(exploitationStageIterations, javaClazz, methodLines, currentCoverageByLines)
50
+ exploitationStage(exploitationStageIterations, javaClazz, methodLines, currentCoverageByLines)
53
51
// UtModelGenerator.reset()
54
52
return sequenceOf()
55
53
}
@@ -58,11 +56,32 @@ class GreyBoxFuzzer(
58
56
method : Method ,
59
57
numberOfIterations : Int ,
60
58
methodLinesToCover : Set <Int >,
61
- clazz : Class <* >,
62
59
classFieldsUsedByFunc : Set <Field >,
63
60
methodUnderTest : ExecutableId ,
64
61
prevMethodCoverage : Set <Int >
65
62
) {
63
+ // val param = method.parameters.first()
64
+ // val firstGenerator = GreyBoxFuzzerGenerators.generatorRepository.getOrProduceGenerator(param, 0)!!
65
+ // var generator = firstGenerator
66
+ // println("GENERATOR = $generator")
67
+ // val generatedValue = generator.generateImpl(GreyBoxFuzzerGenerators.sourceOfRandomness, GreyBoxFuzzerGenerators.genStatus)
68
+ // println("GENERATED VALUE = $generatedValue")
69
+ // generator.generationState = GenerationState.CACHE
70
+ // val valueFromCache = generator.generateImpl(GreyBoxFuzzerGenerators.sourceOfRandomness, GreyBoxFuzzerGenerators.genStatus)
71
+ // println("VALUE FROM CACHE = $valueFromCache")
72
+ // //generator = firstGenerator.copy()
73
+ // generator.generationState = GenerationState.MODIFY
74
+ // val modifiedValue = generator.generateImpl(GreyBoxFuzzerGenerators.sourceOfRandomness, GreyBoxFuzzerGenerators.genStatus)
75
+ // println("MODIFIED VALUE = $modifiedValue")
76
+ // //generator = firstGenerator.copy()
77
+ // generator.generationState = GenerationState.MODIFY
78
+ // val modifiedValue2 = generator.generateImpl(GreyBoxFuzzerGenerators.sourceOfRandomness, GreyBoxFuzzerGenerators.genStatus)
79
+ // println("MODIFIED VALUE = $modifiedValue2")
80
+ // //generator = firstGenerator.copy()
81
+ // generator.generationState = GenerationState.MODIFY
82
+ // val modifiedValue3 = generator.generateImpl(GreyBoxFuzzerGenerators.sourceOfRandomness, GreyBoxFuzzerGenerators.genStatus)
83
+ // println("MODIFIED VALUE = $modifiedValue3")
84
+ // exitProcess(0)
66
85
val parametersToGenericsReplacer = method.parameters.map { it to GenericsReplacer () }
67
86
val thisInstancesHistory = ArrayDeque <ThisInstance >()
68
87
repeat(numberOfIterations) { iterationNumber ->
@@ -119,6 +138,7 @@ class GreyBoxFuzzer(
119
138
)
120
139
seeds.addSeed(Seed (thisInstance, generatedParameters, seedScore.toDouble()))
121
140
logger.debug { " Execution result: ${executionResult.result} " }
141
+ logger.debug { " Seed score = $seedScore " }
122
142
} catch (e: Throwable ) {
123
143
logger.debug(e) { " Exception while execution :(" }
124
144
thisInstancesHistory.clear()
@@ -140,24 +160,44 @@ class GreyBoxFuzzer(
140
160
.map { it.lineNumber }
141
161
// .filter { it in currentMethodLines }
142
162
.toSet()
163
+ val currentMethodCoverage = coverage.filter { it in currentMethodLines }
143
164
executionResult.coverage.coveredInstructions.forEach { CoverageCollector .coverage.add(it) }
144
- return (coverage - prevMethodCoverage).size
165
+ return (currentMethodCoverage - prevMethodCoverage).size
145
166
}
146
167
147
168
148
169
// TODO under construction
149
- private fun exploitationStage (
170
+ private suspend fun exploitationStage (
150
171
numberOfIterations : Int ,
151
172
clazz : Class <* >,
152
173
methodLinesToCover : Set <Int >,
153
174
prevMethodCoverage : Set <Int >
154
175
) {
155
176
logger.debug { " Exploitation began" }
156
177
repeat(numberOfIterations) {
178
+ logger.debug { " Mutation iteration $it " }
157
179
val randomSeed = seeds.getRandomWeightedSeed() ? : return @repeat
158
- val randomSeedArgs = randomSeed.arguments.toMutableList()
159
- val randomParameter = randomSeedArgs.random()
160
- Mutator .mutateParameter(randomParameter)
180
+ logger.debug { " Random seed params = ${randomSeed.parameters} " }
181
+ val mutatedSeed = Mutator .mutateSeed(randomSeed, GreyBoxFuzzerGenerators .sourceOfRandomness, GreyBoxFuzzerGenerators .genStatus)
182
+ logger.debug { " Mutated params = ${mutatedSeed.parameters} " }
183
+ val stateBefore = mutatedSeed.createEnvironmentModels()
184
+ try {
185
+ val executionResult = execute(stateBefore, methodUnderTest)
186
+ logger.debug { " Execution result: $executionResult " }
187
+ val seedScore =
188
+ handleCoverage(
189
+ executionResult,
190
+ prevMethodCoverage,
191
+ methodLinesToCover
192
+ )
193
+ mutatedSeed.score = seedScore.toDouble()
194
+ seeds.addSeed(mutatedSeed)
195
+ logger.debug { " Execution result: ${executionResult.result} " }
196
+ logger.debug { " Seed score = $seedScore " }
197
+ } catch (e: Throwable ) {
198
+ logger.debug(e) { " Exception while execution :(" }
199
+ return @repeat
200
+ }
161
201
}
162
202
}
163
203
// private suspend fun exploitationStage(
0 commit comments