Skip to content

Commit e6accca

Browse files
Global refactorings
1 parent 1eba966 commit e6accca

File tree

198 files changed

+1629
-1932
lines changed

Some content is hidden

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

198 files changed

+1629
-1932
lines changed

settings.gradle.kts

+1
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

+10-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import java.nio.file.Path
3939
import java.nio.file.Paths
4040
import java.time.LocalDateTime
4141
import java.time.temporal.ChronoUnit
42-
import org.utbot.engine.greyboxfuzzer.util.CustomClassLoader
4342

4443
private const val LONG_GENERATION_TIMEOUT = 1_200_000L
4544

@@ -145,11 +144,16 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
145144
val classRelativePath = classFqnToPath(classFqn) + ".class"
146145
val classAbsoluteURL = classLoader.getResource(classRelativePath) ?: return null
147146
val classAbsolutePath =
148-
if (classAbsoluteURL.toURI().scheme == "jar") {
149-
replaceSeparator(classAbsoluteURL.file.removePrefix("file:"))
150-
.removeSuffix(classRelativePath)
151-
.removeSuffix("/")
152-
.removeSuffix("!")
147+
if (UtSettings.useGreyBoxFuzzing) {
148+
if (classAbsoluteURL.toURI().scheme == "jar") {
149+
replaceSeparator(classAbsoluteURL.file.removePrefix("file:"))
150+
.removeSuffix(classRelativePath)
151+
.removeSuffix("/")
152+
.removeSuffix("!")
153+
} else {
154+
replaceSeparator(classAbsoluteURL.toPath().toString())
155+
.removeSuffix(classRelativePath)
156+
}
153157
} else {
154158
replaceSeparator(classAbsoluteURL.toPath().toString())
155159
.removeSuffix(classRelativePath)
@@ -164,7 +168,6 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
164168
searchDirectory: Path,
165169
chosenClassesToMockAlways: Set<ClassId>
166170
): List<UtMethodTestSet> {
167-
CustomClassLoader.classLoader = classLoader
168171
return testCaseGenerator.generate(
169172
targetMethods,
170173
mockStrategy,

utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import java.lang.reflect.Method
88
* NOTE: vararg parameters must be passed as an array of the corresponding type.
99
*/
1010
fun Method.invokeCatching(obj: Any?, args: List<Any?>) = try {
11-
val invocation =
12-
try {
13-
invoke(obj, *args.toTypedArray())
14-
} catch (e: Throwable) {
15-
null
16-
}
11+
val invocation = invoke(obj, *args.toTypedArray())
1712
Result.success(invocation)
1813
} catch (e: InvocationTargetException) {
1914
Result.failure<Nothing>(e.targetException)

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +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(true)
247+
var useGreyBoxFuzzing: Boolean by getBooleanProperty(false)
248+
249+
/**
250+
* Set to true to use grey-box fuzzing in competition mode (without asserts generation)
251+
*/
252+
var greyBoxFuzzingCompetitionMode: Boolean by getBooleanProperty(false)
248253

249254
/**
250255
* Set to true to use UtCompositeModels in grey-box fuzzing process

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ data class UtArrayModel(
480480
* @param instantiationCall is an [UtExecutableCallModel] to instantiate represented object. It **must** not return `null`.
481481
* @param modificationsChain is a chain of [UtStatementModel] to construct object state.
482482
*/
483-
data class UtAssembleModel constructor(
483+
data class UtAssembleModel private constructor(
484484
override val id: Int?,
485485
override val classId: ClassId,
486486
override val modelName: String,

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

+3-5
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) {
@@ -103,11 +106,6 @@ inline fun UtExecutionResult.onFailure(action: (exception: Throwable) -> Unit):
103106
return this
104107
}
105108

106-
fun UtExecutionResult.getOrThrow(): UtModel = when (this) {
107-
is UtExecutionSuccess -> model
108-
is UtExecutionFailure -> throw exception
109-
}
110-
111109
fun UtExecutionResult.exceptionOrNull(): Throwable? = when (this) {
112110
is UtExecutionFailure -> rootCauseException
113111
is UtExecutionSuccess -> null

utbot-framework/build.gradle

+1
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

+18-8
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
@@ -33,9 +32,8 @@ import org.utbot.framework.UtSettings.pathSelectorStepsLimit
3332
import org.utbot.framework.UtSettings.pathSelectorType
3433
import org.utbot.framework.UtSettings.processUnknownStatesDuringConcreteExecution
3534
import org.utbot.framework.UtSettings.useDebugVisualization
36-
import org.utbot.framework.concrete.UtConcreteExecutionData
37-
import org.utbot.framework.concrete.UtConcreteExecutionResult
38-
import org.utbot.framework.concrete.UtExecutionInstrumentation
35+
import org.utbot.framework.concrete.*
36+
import org.utbot.framework.concrete.constructors.UtModelConstructor
3937
import org.utbot.framework.plugin.api.*
4038
import org.utbot.framework.plugin.api.Step
4139
import org.utbot.framework.plugin.api.util.*
@@ -44,11 +42,14 @@ import org.utbot.framework.util.sootMethod
4442
import org.utbot.fuzzer.*
4543
import org.utbot.fuzzing.*
4644
import org.utbot.fuzzing.utils.Trie
45+
import org.utbot.greyboxfuzzer.GreyBoxFuzzer
46+
import org.utbot.greyboxfuzzer.util.FuzzerUtModelConstructor
4747
import org.utbot.instrumentation.ConcreteExecutor
4848
import ru.vyarus.java.generics.resolver.context.GenericsInfoFactory
4949
import soot.jimple.Stmt
5050
import soot.tagkit.ParamNamesTag
5151
import java.lang.reflect.Method
52+
import java.util.*
5253
import kotlin.system.measureTimeMillis
5354

5455
val logger = KotlinLogging.logger {}
@@ -330,7 +331,7 @@ class UtBotSymbolicEngine(
330331
fun fuzzing(until: Long = Long.MAX_VALUE, transform: (JavaValueProvider) -> JavaValueProvider = { it }) = flow {
331332
val isFuzzable = methodUnderTest.parameters.all { classId ->
332333
classId != Method::class.java.id && // causes the instrumented process crash at invocation
333-
classId != Class::class.java.id // causes java.lang.IllegalAccessException: java.lang.Class at sun.misc.Unsafe.allocateInstance(Native Method)
334+
classId != Class::class.java.id // causes java.lang.IllegalAccessException: java.lang.Class at sun.misc.Unsafe.allocateInstance(Native Method)
334335
}
335336
val hasMethodUnderTestParametersToFuzz = methodUnderTest.parameters.isNotEmpty()
336337
if (!isFuzzable || !hasMethodUnderTestParametersToFuzz && methodUnderTest.isStatic) {
@@ -422,13 +423,22 @@ class UtBotSymbolicEngine(
422423
if (!isFuzzable) {
423424
return@flow
424425
}
426+
val utModelConstructor = UtModelConstructor(IdentityHashMap())
427+
val fuzzerUtModelConstructor = FuzzerUtModelConstructor(
428+
utModelConstructor::construct,
429+
utModelConstructor::computeUnusedIdAndUpdate
430+
)
425431

426432
try {
427433
emitAll(
428434
GreyBoxFuzzer(
429-
concreteExecutor.pathsToUserClasses,
430-
concreteExecutor.pathsToDependencyClasses,
431435
methodUnderTest,
436+
collectConstantsForGreyBoxFuzzer(methodUnderTest.sootMethod, utModelConstructor),
437+
fuzzerUtModelConstructor,
438+
FuzzerConcreteExecutor(
439+
concreteExecutor.pathsToUserClasses,
440+
concreteExecutor.pathsToDependencyClasses
441+
)::execute,
432442
timeBudget
433443
).fuzz()
434444
)
@@ -567,7 +577,7 @@ private fun ResolvedModels.constructStateForMethod(methodUnderTest: ExecutableId
567577
return EnvironmentModels(thisInstanceBefore, paramsBefore, statics)
568578
}
569579

570-
private suspend fun ConcreteExecutor<UtConcreteExecutionResult, UtExecutionInstrumentation>.executeConcretely(
580+
internal suspend fun ConcreteExecutor<UtConcreteExecutionResult, UtExecutionInstrumentation>.executeConcretely(
571581
methodUnderTest: ExecutableId,
572582
stateBefore: EnvironmentModels,
573583
instrumentation: List<UtInstrumentation>

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/quickcheck/internal/Zilch.kt

-3
This file was deleted.

utbot-framework/src/main/kotlin/org/utbot/engine/greyboxfuzzer/quickcheck/internal/generator/ZilchGenerator.kt

-20
This file was deleted.

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

-9
This file was deleted.

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

-51
This file was deleted.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.utbot.common.PathUtil
44
import org.utbot.common.WorkaroundReason
55
import org.utbot.common.isStatic
66
import org.utbot.common.workaround
7-
import org.utbot.engine.greyboxfuzzer.util.UtGreyBoxFuzzedExecution
87
import org.utbot.framework.assemble.assemble
98
import org.utbot.framework.codegen.domain.ForceStaticMocking
109
import org.utbot.framework.codegen.domain.ParametrizedTestSource
@@ -155,6 +154,7 @@ import org.utbot.framework.plugin.api.util.longStreamToArrayMethodId
155154
import org.utbot.framework.plugin.api.util.streamClassId
156155
import org.utbot.framework.plugin.api.util.streamToArrayMethodId
157156
import org.utbot.framework.plugin.api.util.isStatic
157+
import org.utbot.greyboxfuzzer.util.UtGreyBoxFuzzedExecution
158158

159159
private const val DEEP_EQUALS_MAX_DEPTH = 5 // TODO move it to plugin settings?
160160

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.utbot.framework.concrete
2+
3+
import org.utbot.framework.UtSettings
4+
import org.utbot.framework.plugin.api.EnvironmentModels
5+
import org.utbot.framework.plugin.api.ExecutableId
6+
import org.utbot.framework.plugin.api.UtInstrumentation
7+
import org.utbot.framework.plugin.api.util.utContext
8+
import org.utbot.greyboxfuzzer.util.UtFuzzingConcreteExecutionResult
9+
import org.utbot.instrumentation.ConcreteExecutor
10+
11+
class FuzzerConcreteExecutor(
12+
private val pathsToUserClasses: String,
13+
private val pathsToDependencyClasses: String,
14+
) {
15+
16+
suspend fun execute(
17+
methodUnderTest: ExecutableId,
18+
stateBefore: EnvironmentModels,
19+
instrumentation: List<UtInstrumentation>
20+
): UtFuzzingConcreteExecutionResult {
21+
// val fuzzingExecutor =
22+
// ConcreteExecutor(
23+
// UtFuzzingExecutionInstrumentation/*(UtSettings.greyBoxFuzzingCompetitionMode)*/,
24+
// pathsToUserClasses,
25+
// pathsToDependencyClasses
26+
// ).apply { this.classLoader = utContext.classLoader }
27+
// val executionResult = fuzzingExecutor.executeConcretelyFuzz(methodUnderTest, stateBefore, instrumentation)
28+
// return UtFuzzingConcreteExecutionResult(
29+
// null,
30+
// executionResult.result,
31+
// executionResult.coverage,
32+
// executionResult.methodInstructionsIds
33+
// )
34+
return if (UtSettings.greyBoxFuzzingCompetitionMode) {
35+
val fuzzingExecutor =
36+
ConcreteExecutor(
37+
UtFuzzingExecutionInstrumentation,
38+
pathsToUserClasses,
39+
pathsToDependencyClasses
40+
).apply { this.classLoader = utContext.classLoader }
41+
val executionResult = fuzzingExecutor.executeConcretelyFuzz(methodUnderTest, stateBefore, instrumentation)
42+
UtFuzzingConcreteExecutionResult(
43+
null,
44+
executionResult.result,
45+
executionResult.coverage,
46+
executionResult.methodInstructions
47+
)
48+
} else {
49+
val fuzzingExecutor =
50+
ConcreteExecutor(
51+
UtFuzzingExecutionInstrumentationWithStateAfterCollection,
52+
pathsToUserClasses,
53+
pathsToDependencyClasses
54+
).apply { this.classLoader = utContext.classLoader }
55+
val executionResult = fuzzingExecutor.executeConcretelyFuzzWithStateAfterCollection(
56+
methodUnderTest,
57+
stateBefore,
58+
instrumentation
59+
)
60+
UtFuzzingConcreteExecutionResult(
61+
executionResult.stateAfter,
62+
executionResult.result,
63+
executionResult.coverage,
64+
executionResult.methodInstructions
65+
)
66+
}
67+
}
68+
69+
private suspend fun ConcreteExecutor<UtFuzzingConcreteExecutionResult, UtFuzzingExecutionInstrumentation>.executeConcretelyFuzz(
70+
methodUnderTest: ExecutableId,
71+
stateBefore: EnvironmentModels,
72+
instrumentation: List<UtInstrumentation>
73+
): UtFuzzingConcreteExecutionResult = executeAsync(
74+
methodUnderTest.classId.name,
75+
methodUnderTest.signature,
76+
arrayOf(),
77+
parameters = UtConcreteExecutionData(stateBefore, instrumentation)
78+
)
79+
80+
private suspend fun ConcreteExecutor<UtFuzzingConcreteExecutionResult, UtFuzzingExecutionInstrumentationWithStateAfterCollection>.executeConcretelyFuzzWithStateAfterCollection(
81+
methodUnderTest: ExecutableId,
82+
stateBefore: EnvironmentModels,
83+
instrumentation: List<UtInstrumentation>
84+
): UtFuzzingConcreteExecutionResult = executeAsync(
85+
methodUnderTest.classId.name,
86+
methodUnderTest.signature,
87+
arrayOf(),
88+
parameters = UtConcreteExecutionData(stateBefore, instrumentation)
89+
)
90+
91+
}

0 commit comments

Comments
 (0)