Skip to content

Commit 90cd814

Browse files
Merge branch 'tmp' into HEAD
# Conflicts: # utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt # utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt # utbot-framework/src/main/kotlin/org/utbot/external/api/UtBotJavaApi.kt # utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgMethodConstructor.kt # utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgTestClassConstructor.kt # utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/ConcreteExecutor.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/BigNumberConstructor.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/BitSetConstructor.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/DateTimeConstructors.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/FuzzerConcreteExecutor.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/LocaleConstructor.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UUIDConstructor.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtExecutionInstrumentationWithStatsCollection.kt # utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/UtFuzzingExecutionInstrumentation.kt # utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt # utbot-junit-contest/src/main/kotlin/org/utbot/contest/GreyBoxFuzzerContest.kt
2 parents 698c3cd + 636e474 commit 90cd814

File tree

204 files changed

+11513
-402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+11513
-402
lines changed

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include("utbot-intellij")
2323
include("utbot-sample")
2424
include("utbot-fuzzers")
2525
include("utbot-fuzzing")
26+
include("utbot-greyboxfuzzer")
2627
include("utbot-junit-contest")
2728
include("utbot-analytics")
2829
include("utbot-analytics-torch")

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import java.nio.file.Path
4040
import java.nio.file.Paths
4141
import java.time.LocalDateTime
4242
import java.time.temporal.ChronoUnit
43-
import org.utbot.engine.greyboxfuzzer.util.CustomClassLoader
4443

4544
private const val LONG_GENERATION_TIMEOUT = 1_200_000L
4645

@@ -146,11 +145,16 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
146145
val classRelativePath = classFqnToPath(classFqn) + ".class"
147146
val classAbsoluteURL = classLoader.getResource(classRelativePath) ?: return null
148147
val classAbsolutePath =
149-
if (classAbsoluteURL.toURI().scheme == "jar") {
150-
replaceSeparator(classAbsoluteURL.file.removePrefix("file:"))
151-
.removeSuffix(classRelativePath)
152-
.removeSuffix("/")
153-
.removeSuffix("!")
148+
if (UtSettings.useGreyBoxFuzzing) {
149+
if (classAbsoluteURL.toURI().scheme == "jar") {
150+
replaceSeparator(classAbsoluteURL.file.removePrefix("file:"))
151+
.removeSuffix(classRelativePath)
152+
.removeSuffix("/")
153+
.removeSuffix("!")
154+
} else {
155+
replaceSeparator(classAbsoluteURL.toPath().toString())
156+
.removeSuffix(classRelativePath)
157+
}
154158
} else {
155159
replaceSeparator(classAbsoluteURL.toPath().toString())
156160
.removeSuffix(classRelativePath)
@@ -164,17 +168,15 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
164168
sourceCodeFile: Path? = null,
165169
searchDirectory: Path,
166170
chosenClassesToMockAlways: Set<ClassId>
167-
): List<UtMethodTestSet> {
168-
CustomClassLoader.classLoader = classLoader
169-
return testCaseGenerator.generate(
171+
): List<UtMethodTestSet> =
172+
testCaseGenerator.generate(
170173
targetMethods,
171174
mockStrategy,
172175
chosenClassesToMockAlways,
173176
generationTimeout
174177
).map {
175178
if (sourceCodeFile != null) it.summarize(searchDirectory, sourceCodeFile.toFile()) else it
176179
}
177-
}
178180

179181

180182
protected fun withLogger(targetClassFqn: String, block: Runnable) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
252252
*/
253253
var useCompositeModelsInGreyBoxFuzzing: Boolean by getBooleanProperty(true)
254254

255+
/**
256+
* Set to true to use grey-box fuzzing
257+
*/
258+
var useGreyBoxFuzzing: Boolean by getBooleanProperty(true)
259+
260+
/**
261+
* Set to true to use grey-box fuzzing in competition mode (without asserts generation)
262+
*/
263+
var greyBoxFuzzingCompetitionMode: Boolean by getBooleanProperty(true)
264+
265+
/**
266+
* Set to true to use UtCompositeModels in grey-box fuzzing process
267+
*/
268+
var useCompositeModelsInGreyBoxFuzzing: Boolean by getBooleanProperty(false)
269+
255270
/**
256271
* Set the total attempts to improve coverage by fuzzer.
257272
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class ConcreteExecutionFailureException(cause: Throwable, errorFile: File, val p
7070
appendLine("Cause:\n${cause.message}")
7171
appendLine("Last 1000 lines of the error log ${errorFile.absolutePath}:")
7272
appendLine("----------------------------------------")
73+
if (!errorFile.exists()) {
74+
errorFile.createNewFile()
75+
}
7376
errorFile.useLines { lines ->
7477
val lastLines = LinkedList<String>()
7578
for (line in lines) {

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/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515
api project(':utbot-summary')
1616
api project(':utbot-framework-api')
1717
api project(':utbot-rd')
18+
api project(':utbot-greyboxfuzzer')
1819

1920
implementation group: 'com.jetbrains.rd', name: 'rd-framework', version: rdVersion
2021
implementation group: 'com.jetbrains.rd', name: 'rd-core', version: rdVersion

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import org.utbot.api.exception.UtMockAssumptionViolatedException
1111
import org.utbot.common.bracket
1212
import org.utbot.common.debug
1313
import org.utbot.engine.MockStrategy.NO_MOCKS
14-
import org.utbot.engine.greyboxfuzzer.GreyBoxFuzzer
1514
import org.utbot.engine.pc.*
1615
import org.utbot.engine.selectors.*
1716
import org.utbot.engine.selectors.nurs.NonUniformRandomSearch
@@ -42,6 +41,8 @@ import org.utbot.framework.util.sootMethod
4241
import org.utbot.fuzzer.*
4342
import org.utbot.fuzzing.*
4443
import org.utbot.fuzzing.utils.Trie
44+
import org.utbot.greyboxfuzzer.GreyBoxFuzzer
45+
import org.utbot.greyboxfuzzer.util.FuzzerUtModelConstructor
4546
import org.utbot.instrumentation.ConcreteExecutor
4647
import ru.vyarus.java.generics.resolver.context.GenericsInfoFactory
4748
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionData
@@ -50,6 +51,7 @@ import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrument
5051
import soot.jimple.Stmt
5152
import soot.tagkit.ParamNamesTag
5253
import java.lang.reflect.Method
54+
import java.util.IdentityHashMap
5355
import kotlin.system.measureTimeMillis
5456

5557
val logger = KotlinLogging.logger {}
@@ -421,27 +423,36 @@ class UtBotSymbolicEngine(
421423
//Simple fuzzing
422424
fun greyBoxFuzzing(timeBudget: Long = Long.MAX_VALUE) =
423425
flow {
424-
GenericsInfoFactory.disableCache()
425426
val isFuzzable = methodUnderTest.parameters.all { classId ->
426427
classId != Method::class.java.id // causes the child process crash at invocation
427428
}
428429
if (!isFuzzable) {
429430
return@flow
430431
}
432+
val utModelConstructor = UtModelConstructor(IdentityHashMap())
433+
val fuzzerUtModelConstructor = FuzzerUtModelConstructor(
434+
utModelConstructor::construct,
435+
utModelConstructor::computeUnusedIdAndUpdate
436+
)
431437

432438
try {
433439
emitAll(
434440
GreyBoxFuzzer(
435-
concreteExecutor.pathsToUserClasses,
436-
concreteExecutor.pathsToDependencyClasses,
437441
methodUnderTest,
442+
collectConstantsForGreyBoxFuzzer(methodUnderTest.sootMethod, utModelConstructor),
443+
fuzzerUtModelConstructor,
444+
FuzzerConcreteExecutor(
445+
concreteExecutor.pathsToUserClasses,
446+
concreteExecutor.pathsToDependencyClasses
447+
)::execute,
448+
ValueConstructionContext(UtFuzzingExecutionInstrumentation.instrumentationContext, true)::constructParameters,
438449
timeBudget
439450
).fuzz()
440451
)
441452
} catch (e: CancellationException) {
442453
logger.debug { "Cancelled by timeout" }
443454
} catch (e: Throwable) {
444-
emit(UtError("Unexpected fuzzing crash", e))
455+
emit(UtError("Unexpected fuzzing crash\n${e.stackTraceToString()}", e))
445456
}
446457
return@flow
447458
}
@@ -586,7 +597,7 @@ private fun ResolvedModels.constructStateForMethod(methodUnderTest: ExecutableId
586597
return EnvironmentModels(thisInstanceBefore, paramsBefore, statics)
587598
}
588599

589-
private suspend fun ConcreteExecutor<UtConcreteExecutionResult, UtExecutionInstrumentation>.executeConcretely(
600+
internal suspend fun ConcreteExecutor<UtConcreteExecutionResult, UtExecutionInstrumentation>.executeConcretely(
590601
methodUnderTest: ExecutableId,
591602
stateBefore: EnvironmentModels,
592603
instrumentation: List<UtInstrumentation>

utbot-framework/src/main/kotlin/org/utbot/external/api/UtBotJavaApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object UtBotJavaApi {
7070

7171
val concreteExecutor = ConcreteExecutor(
7272
UtExecutionInstrumentation,
73-
classpath,
73+
classpath
7474
)
7575

7676
testSets.addAll(generateUnitTests(concreteExecutor, methodsForGeneration, classUnderTest))
@@ -184,7 +184,7 @@ object UtBotJavaApi {
184184
generationTimeoutInMillis,
185185
generate = { symbolicEngine ->
186186
if (isGreyBoxFuzzing) {
187-
symbolicEngine.greyBoxFuzzing()
187+
symbolicEngine.greyBoxFuzzing(generationTimeoutInMillis)
188188
} else {
189189
symbolicEngine.fuzzing { defaultModelProvider ->
190190
customModelProvider.withFallback(defaultModelProvider)

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}Test"
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/domain/models/CgElement.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ interface CgElement {
4545
is CgAuxiliaryClass -> visit(element)
4646
is CgUtilMethod -> visit(element)
4747
is CgTestMethod -> visit(element)
48+
is CgMockMethod -> visit(element)
4849
is CgErrorTestMethod -> visit(element)
4950
is CgParameterizedTestDataProviderMethod -> visit(element)
5051
is CgCommentedAnnotation -> visit(element)
@@ -133,7 +134,7 @@ class CgClass(
133134
val body: CgClassBody,
134135
val isStatic: Boolean,
135136
val isNested: Boolean,
136-
): CgElement {
137+
): CgStatement {
137138
val packageName
138139
get() = id.packageName
139140

@@ -282,6 +283,17 @@ class CgTestMethod(
282283
override val requiredFields: List<CgParameterDeclaration> = emptyList(),
283284
) : CgMethod(false)
284285

286+
class CgMockMethod(
287+
override val name: String,
288+
override val returnType: ClassId,
289+
override val parameters: List<CgParameterDeclaration>,
290+
override val statements: List<CgStatement>,
291+
override val exceptions: Set<ClassId>,
292+
override val annotations: List<CgAnnotation>,
293+
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList()),
294+
override val requiredFields: List<CgParameterDeclaration> = emptyList(),
295+
) : CgMethod(false)
296+
285297
class CgErrorTestMethod(
286298
override val name: String,
287299
override val statements: List<CgStatement>,

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import org.utbot.framework.codegen.domain.models.CgStaticFieldAccess
7474
import org.utbot.framework.codegen.domain.models.CgStaticRunnable
7575
import org.utbot.framework.codegen.domain.models.CgStaticsRegion
7676
import org.utbot.framework.codegen.domain.models.CgTestMethod
77+
import org.utbot.framework.codegen.domain.models.CgMockMethod
7778
import org.utbot.framework.codegen.domain.models.CgTestMethodCluster
7879
import org.utbot.framework.codegen.domain.models.CgThisInstance
7980
import org.utbot.framework.codegen.domain.models.CgThrowStatement
@@ -97,6 +98,7 @@ import org.utbot.framework.plugin.api.util.isArray
9798
import org.utbot.framework.plugin.api.util.isRefType
9899
import org.utbot.framework.plugin.api.util.longClassId
99100
import org.utbot.framework.plugin.api.util.shortClassId
101+
import org.utbot.framework.plugin.api.util.isPublic
100102

101103
abstract class CgAbstractRenderer(
102104
val context: CgRendererContext,
@@ -245,6 +247,15 @@ abstract class CgAbstractRenderer(
245247
visit(element as CgMethod)
246248
}
247249

250+
override fun visit(element: CgMockMethod) {
251+
renderMethodDocumentation(element)
252+
for (annotation in element.annotations) {
253+
annotation.accept(this)
254+
}
255+
renderMethodSignature(element)
256+
visit(element as CgMethod)
257+
}
258+
248259
override fun visit(element: CgErrorTestMethod) {
249260
renderMethodDocumentation(element)
250261
renderMethodSignature(element)
@@ -612,7 +623,15 @@ abstract class CgAbstractRenderer(
612623
is Boolean -> toStringConstant()
613624
// String is "\"" + "str" + "\"", RawString is "str"
614625
is String -> if (asRawString) "$this".escapeCharacters() else toStringConstant()
615-
else -> "$this"
626+
else -> {
627+
val t = this@toStringConstant.type
628+
val illegalType = t.toString().contains("$") || !t.isPublic
629+
if (this == null && UtSettings.greyBoxFuzzingCompetitionMode && !illegalType) {
630+
"(${this@toStringConstant.type}) null"
631+
} else {
632+
"$this"
633+
}
634+
}
616635
}
617636
}
618637

@@ -749,6 +768,7 @@ abstract class CgAbstractRenderer(
749768
protected val maxParametersAmountInOneLine = 3
750769

751770
protected abstract fun renderMethodSignature(element: CgTestMethod)
771+
protected abstract fun renderMethodSignature(element: CgMockMethod)
752772
protected abstract fun renderMethodSignature(element: CgErrorTestMethod)
753773
protected abstract fun renderMethodSignature(element: CgParameterizedTestDataProviderMethod)
754774

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@ import org.utbot.framework.codegen.domain.models.CgClassBody
3535
import org.utbot.framework.codegen.domain.models.CgFormattedString
3636
import org.utbot.framework.codegen.domain.models.CgLiteral
3737
import org.utbot.framework.codegen.domain.models.CgTestMethod
38+
import org.utbot.framework.codegen.domain.models.CgMockMethod
3839
import org.utbot.framework.codegen.domain.models.CgTypeCast
3940
import org.utbot.framework.codegen.domain.models.CgVariable
4041
import org.utbot.framework.codegen.util.nullLiteral
4142
import org.utbot.framework.plugin.api.ClassId
4243
import org.utbot.framework.plugin.api.TypeParameters
43-
import org.utbot.framework.plugin.api.util.isFinal
44-
import org.utbot.framework.plugin.api.util.isPrivate
45-
import org.utbot.framework.plugin.api.util.isProtected
46-
import org.utbot.framework.plugin.api.util.isPublic
47-
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
44+
import org.utbot.framework.plugin.api.util.*
4845

4946
internal class CgJavaRenderer(context: CgRendererContext, printer: CgPrinter = CgPrinterImpl()) :
5047
CgAbstractRenderer(context, printer) {
@@ -241,6 +238,25 @@ internal class CgJavaRenderer(context: CgRendererContext, printer: CgPrinter = C
241238
renderExceptions(element)
242239
}
243240

241+
private fun getTypeStringRepresentation(typeId: ClassId): String =
242+
when {
243+
typeId.isArray -> getTypeStringRepresentation(typeId.elementClassId!!) + "[]"
244+
else -> typeId.toString()
245+
}
246+
247+
override fun renderMethodSignature(element: CgMockMethod) {
248+
val returnType = element.returnType.asString()
249+
print("public $returnType ")
250+
print(element.name)
251+
252+
print("(")
253+
val newLinesNeeded = element.parameters.size > maxParametersAmountInOneLine
254+
element.parameters.renderSeparated(newLinesNeeded)
255+
print(")")
256+
257+
renderExceptions(element)
258+
}
259+
244260
override fun renderMethodSignature(element: CgErrorTestMethod) {
245261
// error test methods always have void return type
246262
println("public void ${element.name}()")

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.utbot.framework.codegen.domain.models.CgClassBody
4141
import org.utbot.framework.codegen.domain.models.CgFormattedString
4242
import org.utbot.framework.codegen.domain.models.CgLiteral
4343
import org.utbot.framework.codegen.domain.models.CgTestMethod
44+
import org.utbot.framework.codegen.domain.models.CgMockMethod
4445
import org.utbot.framework.codegen.domain.models.CgTypeCast
4546
import org.utbot.framework.codegen.domain.models.CgValue
4647
import org.utbot.framework.codegen.domain.models.CgVariable
@@ -403,6 +404,16 @@ internal class CgKotlinRenderer(context: CgRendererContext, printer: CgPrinter =
403404
renderMethodReturnType(element)
404405
}
405406

407+
override fun renderMethodSignature(element: CgMockMethod) {
408+
print("fun ")
409+
print(element.name)
410+
print("(")
411+
val newLines = element.parameters.size > maxParametersAmountInOneLine
412+
element.parameters.renderSeparated(newLines)
413+
print(")")
414+
renderMethodReturnType(element)
415+
}
416+
406417
override fun renderMethodSignature(element: CgErrorTestMethod) {
407418
// error test methods always have void return type
408419
print("fun ")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import org.utbot.framework.codegen.domain.models.CgDocRegularLineStmt
7979
import org.utbot.framework.codegen.domain.models.CgFormattedString
8080
import org.utbot.framework.codegen.domain.models.CgNestedClassesRegion
8181
import org.utbot.framework.codegen.domain.models.CgTestMethod
82+
import org.utbot.framework.codegen.domain.models.CgMockMethod
8283
import org.utbot.framework.codegen.domain.models.CgTestMethodCluster
8384
import org.utbot.framework.codegen.domain.models.CgThisInstance
8485
import org.utbot.framework.codegen.domain.models.CgThrowStatement
@@ -111,6 +112,7 @@ interface CgVisitor<R> {
111112
// Methods
112113
fun visit(element: CgMethod): R
113114
fun visit(element: CgTestMethod): R
115+
fun visit(element: CgMockMethod): R
114116
fun visit(element: CgErrorTestMethod): R
115117
fun visit(element: CgParameterizedTestDataProviderMethod): R
116118

0 commit comments

Comments
 (0)