Skip to content

Commit b2ff4ae

Browse files
fixes to contest
1 parent 4ad520a commit b2ff4ae

File tree

25 files changed

+337
-166
lines changed

25 files changed

+337
-166
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
244244
/**
245245
* Set to true to use grey-box fuzzing
246246
*/
247-
var useGreyBoxFuzzing: Boolean by getBooleanProperty(false)
247+
var useGreyBoxFuzzing: Boolean by getBooleanProperty(true)
248248

249249
/**
250250
* Set to true to use grey-box fuzzing in competition mode (without asserts generation)
251251
*/
252-
var greyBoxFuzzingCompetitionMode: Boolean by getBooleanProperty(false)
252+
var greyBoxFuzzingCompetitionMode: Boolean by getBooleanProperty(true)
253253

254254
/**
255255
* Set to true to use UtCompositeModels in grey-box fuzzing process

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.utbot.common.currentThreadInfo
55
import org.utbot.framework.plugin.api.util.UtContext.Companion.setUtContext
66
import kotlin.coroutines.CoroutineContext
77
import kotlinx.coroutines.ThreadContextElement
8-
import mu.KotlinLogging
8+
//import mu.KotlinLogging
99

1010
val utContext: UtContext
1111
get() = UtContext.currentContext()
@@ -75,7 +75,7 @@ inline fun <T> withUtContext(context: UtContext, block: () -> T): T = setUtConte
7575
try {
7676
return@use block.invoke()
7777
} catch (e: Exception) {
78-
KotlinLogging.logger("withUtContext").error { e }
78+
//KotlinLogging.logger("withUtContext").error { e }
7979
throw e
8080
}
8181
}

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import org.utbot.framework.UtSettings.useDebugVisualization
3535
import org.utbot.framework.concrete.UtConcreteExecutionData
3636
import org.utbot.framework.concrete.UtConcreteExecutionResult
3737
import org.utbot.framework.concrete.UtExecutionInstrumentation
38-
import org.utbot.framework.concrete.constructors.UtModelConstructor
38+
import org.utbot.framework.concrete.UtFuzzingExecutionInstrumentation
3939
import org.utbot.framework.concrete.FuzzerConcreteExecutor
40+
import org.utbot.framework.concrete.constructors.UtModelConstructor
41+
import org.utbot.framework.concrete.phases.ValueConstructionContext
4042
import org.utbot.framework.plugin.api.*
4143
import org.utbot.framework.plugin.api.Step
4244
import org.utbot.framework.plugin.api.util.*
@@ -445,13 +447,14 @@ class UtBotSymbolicEngine(
445447
concreteExecutor.pathsToUserClasses,
446448
concreteExecutor.pathsToDependencyClasses
447449
)::execute,
450+
ValueConstructionContext(UtFuzzingExecutionInstrumentation.instrumentationContext, true)::constructParameters,
448451
timeBudget
449452
).fuzz()
450453
)
451454
} catch (e: CancellationException) {
452455
logger.debug { "Cancelled by timeout" }
453456
} catch (e: Throwable) {
454-
emit(UtError("Unexpected fuzzing crash", e))
457+
emit(UtError("Unexpected fuzzing crash\n${e.stackTraceToString()}", e))
455458
}
456459
return@flow
457460
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/context/CgContext.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,9 @@ data class CgContext(
551551
}
552552
}
553553

554+
private var nestedClassIndex = 0
554555
private fun createClassIdForNestedClass(testClassModel: TestClassModel): ClassId {
555-
val simpleName = "${testClassModel.classUnderTest.simpleName}Tests"
556+
val simpleName = "${testClassModel.classUnderTest.simpleName}Test${nestedClassIndex++}"
556557
return BuiltinClassId(
557558
canonicalName = currentTestClass.canonicalName + "." + simpleName,
558559
simpleName = simpleName,

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgAbstractRenderer.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import org.utbot.framework.plugin.api.util.isArray
9797
import org.utbot.framework.plugin.api.util.isRefType
9898
import org.utbot.framework.plugin.api.util.longClassId
9999
import org.utbot.framework.plugin.api.util.shortClassId
100+
import org.utbot.framework.plugin.api.util.isPublic
100101

101102
abstract class CgAbstractRenderer(
102103
val context: CgRendererContext,
@@ -611,7 +612,15 @@ abstract class CgAbstractRenderer(
611612
is Boolean -> toStringConstant()
612613
// String is "\"" + "str" + "\"", RawString is "str"
613614
is String -> if (asRawString) "$this".escapeCharacters() else toStringConstant()
614-
else -> "$this"
615+
else -> {
616+
val t = this@toStringConstant.type
617+
val illegalType = t.toString().contains("$") || !t.isPublic
618+
if (this == null && UtSettings.greyBoxFuzzingCompetitionMode && !illegalType) {
619+
"(${this@toStringConstant.type}) null"
620+
} else {
621+
"$this"
622+
}
623+
}
615624
}
616625
}
617626

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,14 +1427,28 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
14271427
}
14281428
}
14291429

1430-
if (statics.isNotEmpty()) {
1430+
if (UtSettings.greyBoxFuzzingCompetitionMode) {
14311431
+tryBlock {
1432+
if (statics.isNotEmpty()) {
1433+
+tryBlock {
1434+
mainBody()
1435+
}.finally {
1436+
recoverStaticFields()
1437+
}
1438+
} else {
1439+
mainBody()
1440+
}
1441+
}.catch(Throwable::class.id) {}
1442+
} else {
1443+
if (statics.isNotEmpty()) {
1444+
+tryBlock {
1445+
mainBody()
1446+
}.finally {
1447+
recoverStaticFields()
1448+
}
1449+
} else {
14321450
mainBody()
1433-
}.finally {
1434-
recoverStaticFields()
14351451
}
1436-
} else {
1437-
mainBody()
14381452
}
14391453

14401454
mockFrameworkManager.getAndClearMethodResources()?.let { resources ->

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgTestClassConstructor.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,19 @@ open class CgTestClassConstructor(val context: CgContext) :
120120
)
121121
}
122122

123-
if (currentTestClass == outerMostTestClass) {
124-
val utilEntities = collectUtilEntities()
125-
// If utilMethodProvider is TestClassUtilMethodProvider, then util entities should be declared
126-
// in the test class. Otherwise, util entities will be located elsewhere (e.g. another class).
127-
if (utilMethodProvider is TestClassUtilMethodProvider && utilEntities.isNotEmpty()) {
128-
staticDeclarationRegions += CgStaticsRegion("Util methods", utilEntities)
129-
}
123+
//if (currentTestClass == outerMostTestClass) {
124+
//existingMethodNames.clear()
125+
val existingMethodsBeforeCollectingUtils = existingMethodNames.toSet()
126+
val utilEntities = collectUtilEntities()
127+
val addedUtilityMethods = existingMethodNames - existingMethodsBeforeCollectingUtils
128+
existingMethodNames.removeAll(addedUtilityMethods)
129+
// If utilMethodProvider is TestClassUtilMethodProvider, then util entities should be declared
130+
// in the test class. Otherwise, util entities will be located elsewhere (e.g. another class).
131+
if (utilMethodProvider is TestClassUtilMethodProvider && utilEntities.isNotEmpty()) {
132+
staticDeclarationRegions += CgStaticsRegion("Util methods", utilEntities)
130133
}
134+
135+
//}
131136
}
132137
}
133138
// It is important that annotations, superclass and interfaces assignment is run after

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgVariableConstructor.kt

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.framework.codegen.tree
22

33
import org.utbot.common.isStatic
4+
import org.utbot.framework.UtSettings
45
import org.utbot.framework.codegen.domain.builtin.forName
56
import org.utbot.framework.codegen.domain.builtin.setArrayElement
67
import org.utbot.framework.codegen.domain.context.CgContext
@@ -24,15 +25,10 @@ import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getC
2425
import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getMockFrameworkManagerBy
2526
import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getNameGeneratorBy
2627
import org.utbot.framework.codegen.tree.CgTestClassConstructor.CgComponents.getStatementConstructorBy
27-
import org.utbot.framework.codegen.util.at
28+
import org.utbot.framework.codegen.util.*
2829
import org.utbot.framework.codegen.util.canBeSetFrom
2930
import org.utbot.framework.codegen.util.fieldThatIsGotWith
3031
import org.utbot.framework.codegen.util.fieldThatIsSetWith
31-
import org.utbot.framework.codegen.util.inc
32-
import org.utbot.framework.codegen.util.isAccessibleFrom
33-
import org.utbot.framework.codegen.util.lessThan
34-
import org.utbot.framework.codegen.util.nullLiteral
35-
import org.utbot.framework.codegen.util.resolve
3632
import org.utbot.framework.plugin.api.BuiltinClassId
3733
import org.utbot.framework.plugin.api.ClassId
3834
import org.utbot.framework.plugin.api.CodegenLanguage
@@ -51,19 +47,7 @@ import org.utbot.framework.plugin.api.UtNullModel
5147
import org.utbot.framework.plugin.api.UtPrimitiveModel
5248
import org.utbot.framework.plugin.api.UtReferenceModel
5349
import org.utbot.framework.plugin.api.UtVoidModel
54-
import org.utbot.framework.plugin.api.util.classClassId
55-
import org.utbot.framework.plugin.api.util.defaultValueModel
56-
import org.utbot.framework.plugin.api.util.jField
57-
import org.utbot.framework.plugin.api.util.findFieldByIdOrNull
58-
import org.utbot.framework.plugin.api.util.id
59-
import org.utbot.framework.plugin.api.util.intClassId
60-
import org.utbot.framework.plugin.api.util.isArray
61-
import org.utbot.framework.plugin.api.util.isEnum
62-
import org.utbot.framework.plugin.api.util.isPrimitiveWrapperOrString
63-
import org.utbot.framework.plugin.api.util.isStatic
64-
import org.utbot.framework.plugin.api.util.stringClassId
65-
import org.utbot.framework.plugin.api.util.supertypeOfAnonymousClass
66-
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
50+
import org.utbot.framework.plugin.api.util.*
6751

6852
/**
6953
* Constructs CgValue or CgVariable given a UtModel
@@ -114,7 +98,12 @@ open class CgVariableConstructor(val context: CgContext) :
11498
}
11599
} else valueByModel.getOrPut(model) {
116100
when (model) {
117-
is UtNullModel -> nullLiteral()
101+
is UtNullModel ->
102+
if (UtSettings.greyBoxFuzzingCompetitionMode) {
103+
nullLiteralWithCast(model.classId)
104+
} else {
105+
nullLiteral()
106+
}
118107
is UtPrimitiveModel -> CgLiteral(model.classId, model.value)
119108
is UtReferenceModel -> error("Unexpected UtReferenceModel: ${model::class}")
120109
is UtVoidModel -> error("Unexpected UtVoidModel: ${model::class}")
@@ -213,7 +202,19 @@ open class CgVariableConstructor(val context: CgContext) :
213202
is UtDirectSetFieldModel -> {
214203
val instance = declareOrGet(statementModel.instance)
215204
// fields here are supposed to be accessible, so we assign them directly without any checks
216-
instance[statementModel.fieldId] `=` declareOrGet(statementModel.fieldModel)
205+
val fieldValue = declareOrGet(statementModel.fieldModel)
206+
if (statementModel.fieldId.canBeSetFrom(context) && fieldValue.type isSubtypeOf statementModel.fieldId.type) {
207+
instance[statementModel.fieldId] `=` fieldValue
208+
} else {
209+
with(statementModel) {
210+
+utilsClassId[setField](
211+
instance,
212+
fieldId.declaringClass.name,
213+
fieldId.name,
214+
fieldValue
215+
)
216+
}
217+
}
217218
}
218219
is UtExecutableCallModel -> {
219220
val call = createCgExecutableCallFromUtExecutableCall(statementModel)
@@ -332,7 +333,7 @@ open class CgVariableConstructor(val context: CgContext) :
332333
// and the size of an array is not greater than the fixed maximum size
333334
val canInitWithValues = (allPrimitives || allNulls) && elementModels.size <= MAX_ARRAY_INITIALIZER_SIZE
334335

335-
val initializer = if (canInitWithValues) {
336+
val initializer = if (canInitWithValues && !UtSettings.greyBoxFuzzingCompetitionMode) {
336337
val elements = elementModels.map { model ->
337338
when (model) {
338339
is UtPrimitiveModel -> model.value.resolve()

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/util/DslUtil.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ infix fun CgExpression.greaterThan(other: Any?): CgGreaterThan =
4646
// TODO: is it OK to use Object as a type of null literal?
4747
fun nullLiteral() = CgLiteral(objectClassId, null)
4848

49+
fun nullLiteralWithCast(classId: ClassId) = CgLiteral(classId, null)
50+
4951
fun intLiteral(num: Int) = CgLiteral(intClassId, num)
5052

5153
fun longLiteral(num: Long) = CgLiteral(longClassId, num)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.utbot.framework.plugin.services.JdkInfo
3838
import org.utbot.framework.util.SootUtils
3939
import org.utbot.framework.util.jimpleBody
4040
import org.utbot.framework.util.toModel
41+
import org.utbot.framework.util.sootMethodExists
4142
import org.utbot.greyboxfuzzer.util.CoverageCollector
4243
import org.utbot.greyboxfuzzer.util.GreyBoxFuzzingStatisticPrinter
4344
import org.utbot.instrumentation.ConcreteExecutor
@@ -232,7 +233,7 @@ open class TestCaseGenerator(
232233
if (UtSettings.useGreyBoxFuzzing) {
233234
GreyBoxFuzzingStatisticPrinter.printFuzzingStats(method2executions)
234235
}
235-
return methods.map { method ->
236+
return methods.filter { it.sootMethodExists }.map { method ->
236237
UtMethodTestSet(
237238
method,
238239
minimizeExecutions(method2executions.getValue(method)),

utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ private fun initSoot(buildDirs: List<Path>, classpath: String?, jdkInfo: JdkInfo
122122

123123
fun JimpleBody.graph() = ExceptionalUnitGraph(this)
124124

125+
val ExecutableId.sootMethodExists: Boolean
126+
get() {
127+
val clazz = Scene.v().getSootClass(classId.name)
128+
return clazz.methods.any { it.pureJavaSignature == signature }
129+
}
125130
val ExecutableId.sootMethod: SootMethod
126131
get() {
127132
val clazz = Scene.v().getSootClass(classId.name)

0 commit comments

Comments
 (0)