Skip to content

Commit 43f2145

Browse files
this instance rewriting
1 parent 5fb0c93 commit 43f2145

31 files changed

+414
-303
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ data class UtAssembleModel constructor(
485485
override val classId: ClassId,
486486
override val modelName: String,
487487
val instantiationCall: UtExecutableCallModel,
488-
//TODO: get rid of var
489-
var modificationsChain: List<UtStatementModel>,
488+
val modificationsChain: List<UtStatementModel>,
490489
val origin: UtCompositeModel?
491490
) : UtReferenceModel(id, classId, modelName) {
492491

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/GreyBoxFuzzer.kt

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/generator/DataGenerator.kt

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.utbot.engine.greyboxfuzzer.generator
22

3+
import org.utbot.engine.greyboxfuzzer.util.FuzzerIllegalStateException
34
import org.utbot.quickcheck.generator.GenerationStatus
45
import org.utbot.quickcheck.generator.Generator
56
import org.utbot.quickcheck.internal.ParameterTypeContext
67
import org.utbot.quickcheck.random.SourceOfRandomness
78
import org.utbot.engine.logger
89
import org.utbot.external.api.classIdForType
10+
import org.utbot.framework.plugin.api.ClassId
911
import org.utbot.framework.plugin.api.UtModel
1012
import org.utbot.framework.plugin.api.UtNullModel
13+
import org.utbot.framework.plugin.api.util.jClass
1114
import soot.SootMethod
1215
import java.lang.reflect.Parameter
1316

@@ -45,6 +48,46 @@ object DataGenerator {
4548
return generate(generator, parameter, random, status)
4649
}
4750

51+
fun generateThis(
52+
classId: ClassId,
53+
random: SourceOfRandomness,
54+
status: GenerationStatus
55+
): NormalMethodThisInstance {
56+
val generator =
57+
generatorRepository.getOrProduceGenerator(classId.jClass)
58+
return generateThis(generator, classId, random, status)
59+
}
60+
61+
private fun generateThis(
62+
generator: Generator?,
63+
classId: ClassId,
64+
random: SourceOfRandomness,
65+
status: GenerationStatus,
66+
numberOfTries: Int = 3
67+
): NormalMethodThisInstance {
68+
logger.debug { "Trying to generate this instance of type ${classId.name} $numberOfTries times" }
69+
generatorRepository.removeGeneratorForObjectClass()
70+
if (generator == null) {
71+
throw FuzzerIllegalStateException("Can't find generator for ${classId.name}")
72+
}
73+
var generatedValue: UtModel
74+
repeat(numberOfTries) {
75+
logger.debug { "Try $it" }
76+
try {
77+
generatedValue = generator.generate(random, status)
78+
return NormalMethodThisInstance(
79+
generatedValue,
80+
generator,
81+
classId
82+
)
83+
} catch (e: Throwable) {
84+
logger.error(e) { "Exception while generation :(" }
85+
return@repeat
86+
}
87+
}
88+
throw FuzzerIllegalStateException("Can't generate for ${classId.name}")
89+
}
90+
4891
fun generate(
4992
generator: Generator?,
5093
parameter: Parameter,
@@ -63,15 +106,13 @@ object DataGenerator {
63106
logger.debug { "Try $it" }
64107
try {
65108
generatedValue = generator.generate(random, status)
66-
if (generatedValue != null) {
67-
return FParameter(
68-
parameter,
69-
null,
70-
generatedValue!!,
71-
generator,
72-
emptyList()
73-
)
74-
}
109+
return FParameter(
110+
parameter,
111+
null,
112+
generatedValue!!,
113+
generator,
114+
emptyList()
115+
)
75116
} catch (e: Throwable) {
76117
logger.error(e) { "Exception while generation :(" }
77118
return@repeat

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/generator/FParameter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.lang.reflect.Parameter
1414
data class FParameter(
1515
val parameter: Parameter,
1616
val value: Any?,
17+
//TODO make it val
1718
var utModel: UtModel,
1819
val generator: Generator?,
1920
val classId: ClassId,

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/generator/FakeAnnotatedTypeFactory.kt

Lines changed: 0 additions & 74 deletions
This file was deleted.

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/generator/GeneratorConfigurator.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ object GeneratorConfigurator {
3030
val minCollectionSize: Int = 1
3131
val maxCollectionSize: Int = 5
3232

33-
private val sizeAnnotationInstance: Size
34-
private val inRangeAnnotationInstance: InRange
33+
val sizeAnnotationInstance: Size
34+
val inRangeAnnotationInstance: InRange
3535

3636
init {
3737
val sizeAnnotationParams =
@@ -90,6 +90,13 @@ object GeneratorConfigurator {
9090
is CollectionGenerator -> generator.configure(sizeAnnotationInstance)
9191
is ArrayGenerator -> generator.configure(sizeAnnotationInstance)
9292
is MapGenerator -> generator.configure(sizeAnnotationInstance)
93+
is StringGenerator -> generator.configure(
94+
if (Random.getTrue(50)) {
95+
setOf('a'.code..'z'.code)
96+
} else {
97+
setOf(' '.code..'~'.code)
98+
}
99+
)
93100
else -> Unit
94101
}
95102

0 commit comments

Comments
 (0)