@@ -13,7 +13,6 @@ import org.utbot.framework.util.sootMethod
1313import org.utbot.instrumentation.ConcreteExecutor
1414import java.lang.reflect.Field
1515import java.lang.reflect.Method
16- import java.lang.reflect.Modifier
1716import kotlin.random.Random
1817
1918class GreyBoxFuzzer (
@@ -23,15 +22,15 @@ class GreyBoxFuzzer(
2322) {
2423
2524 private val seeds = SeedCollector ()
26- private val explorationStageIterations = 50
25+ private val explorationStageIterations = 100
2726 private val exploitationStageIterations = 100
2827
2928 // TODO make it return Sequence<UtExecution>
3029 suspend fun fuzz (): Sequence <UtExecution > {
3130 logger.debug { " Started to fuzz ${methodUnderTest.name} " }
3231 val javaClazz = methodUnderTest.classId.jClass
33- val javaMethod = methodUnderTest.sootMethod.toJavaMethod()!!
3432 val sootMethod = methodUnderTest.sootMethod
33+ val javaMethod = sootMethod.toJavaMethod()!!
3534 val classFieldsUsedByFunc = sootMethod.getClassFieldsUsedByFunc(javaClazz)
3635 val methodLines = sootMethod.activeBody.units.map { it.javaSourceStartLineNumber }.filter { it != - 1 }.toSet()
3736 val currentCoverageByLines = CoverageCollector .coverage
@@ -43,13 +42,12 @@ class GreyBoxFuzzer(
4342 javaMethod,
4443 explorationStageIterations,
4544 methodLines,
46- javaClazz,
4745 classFieldsUsedByFunc,
4846 methodUnderTest,
4947 currentCoverageByLines
5048 )
5149 logger.debug { " SEEDS AFTER EXPLORATION STAGE = ${seeds.seedsSize()} " }
52- // exploitationStage(exploitationStageIterations, javaClazz, methodLines, currentCoverageByLines)
50+ exploitationStage(exploitationStageIterations, javaClazz, methodLines, currentCoverageByLines)
5351 // UtModelGenerator.reset()
5452 return sequenceOf()
5553 }
@@ -58,11 +56,32 @@ class GreyBoxFuzzer(
5856 method : Method ,
5957 numberOfIterations : Int ,
6058 methodLinesToCover : Set <Int >,
61- clazz : Class <* >,
6259 classFieldsUsedByFunc : Set <Field >,
6360 methodUnderTest : ExecutableId ,
6461 prevMethodCoverage : Set <Int >
6562 ) {
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)
6685 val parametersToGenericsReplacer = method.parameters.map { it to GenericsReplacer () }
6786 val thisInstancesHistory = ArrayDeque <ThisInstance >()
6887 repeat(numberOfIterations) { iterationNumber ->
@@ -119,6 +138,7 @@ class GreyBoxFuzzer(
119138 )
120139 seeds.addSeed(Seed (thisInstance, generatedParameters, seedScore.toDouble()))
121140 logger.debug { " Execution result: ${executionResult.result} " }
141+ logger.debug { " Seed score = $seedScore " }
122142 } catch (e: Throwable ) {
123143 logger.debug(e) { " Exception while execution :(" }
124144 thisInstancesHistory.clear()
@@ -140,24 +160,44 @@ class GreyBoxFuzzer(
140160 .map { it.lineNumber }
141161 // .filter { it in currentMethodLines }
142162 .toSet()
163+ val currentMethodCoverage = coverage.filter { it in currentMethodLines }
143164 executionResult.coverage.coveredInstructions.forEach { CoverageCollector .coverage.add(it) }
144- return (coverage - prevMethodCoverage).size
165+ return (currentMethodCoverage - prevMethodCoverage).size
145166 }
146167
147168
148169 // TODO under construction
149- private fun exploitationStage (
170+ private suspend fun exploitationStage (
150171 numberOfIterations : Int ,
151172 clazz : Class <* >,
152173 methodLinesToCover : Set <Int >,
153174 prevMethodCoverage : Set <Int >
154175 ) {
155176 logger.debug { " Exploitation began" }
156177 repeat(numberOfIterations) {
178+ logger.debug { " Mutation iteration $it " }
157179 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+ }
161201 }
162202 }
163203// private suspend fun exploitationStage(
0 commit comments