@@ -23,9 +23,8 @@ class GreyBoxFuzzer(
2323) {
2424
2525 private val seeds = SeedCollector ()
26- private val explorationStageIterations = 100
26+ private val explorationStageIterations = 50
2727 private val exploitationStageIterations = 100
28- private var thisInstance: UtModel ? = null
2928
3029 // TODO make it return Sequence<UtExecution>
3130 suspend fun fuzz (): Sequence <UtExecution > {
@@ -65,62 +64,68 @@ class GreyBoxFuzzer(
6564 prevMethodCoverage : Set <Int >
6665 ) {
6766 val parametersToGenericsReplacer = method.parameters.map { it to GenericsReplacer () }
67+ val thisInstancesHistory = ArrayDeque <ThisInstance >()
6868 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()
8473 }
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)
9576 }
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+ }
10585 }
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" }
124129 }
125130 }
126131 }
@@ -225,32 +230,26 @@ class GreyBoxFuzzer(
225230 private suspend fun execute (
226231 stateBefore : EnvironmentModels ,
227232 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+
241243
242- private fun generateThisInstance (clazz : Class <* >) =
243- try {
244+ private fun generateThisInstance (classId : ClassId ): ThisInstance =
244245 if (! methodUnderTest.isStatic) {
245- DataGenerator .generate (
246- clazz ,
246+ DataGenerator .generateThis (
247+ classId ,
247248 GreyBoxFuzzerGenerators .sourceOfRandomness,
248249 GreyBoxFuzzerGenerators .genStatus
249250 )
250251 } else {
251- null
252+ StaticMethodThisInstance
252253 }
253- } catch (_: Throwable ) {
254- null
255- }
254+
256255}
0 commit comments