Skip to content

Commit 59cf7b4

Browse files
authored
Fuzzing breaks on nested classes #1537 (#1552)
1 parent ac31b7f commit 59cf7b4

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class UtBotSymbolicEngine(
339339
var attempts = 0
340340
val attemptsLimit = UtSettings.fuzzingMaxAttempts
341341
val names = graph.body.method.tags.filterIsInstance<ParamNamesTag>().firstOrNull()?.names ?: emptyList()
342-
342+
var testEmittedByFuzzer = 0
343343
runJavaFuzzing(
344344
defaultIdGenerator,
345345
methodUnderTest,
@@ -349,6 +349,7 @@ class UtBotSymbolicEngine(
349349
) { thisInstance, descr, values ->
350350
if (controller.job?.isActive == false || System.currentTimeMillis() >= until) {
351351
logger.info { "Fuzzing overtime: $methodUnderTest" }
352+
logger.info { "Test created by fuzzer: $testEmittedByFuzzer" }
352353
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.STOP)
353354
}
354355

@@ -404,6 +405,7 @@ class UtBotSymbolicEngine(
404405
)
405406
)
406407

408+
testEmittedByFuzzer++
407409
BaseFeedback(result = trieNode ?: Trie.emptyNode(), control = Control.CONTINUE)
408410
}
409411
}

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt

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

3+
import mu.KotlinLogging
34
import org.utbot.framework.plugin.api.ClassId
45
import org.utbot.framework.plugin.api.ExecutableId
56
import org.utbot.framework.plugin.api.Instruction
@@ -8,6 +9,10 @@ import org.utbot.fuzzer.*
89
import org.utbot.fuzzing.providers.*
910
import org.utbot.fuzzing.utils.Trie
1011
import java.lang.reflect.*
12+
import java.util.concurrent.TimeUnit
13+
import kotlin.system.measureNanoTime
14+
15+
private val logger = KotlinLogging.logger {}
1116

1217
typealias JavaValueProvider = ValueProvider<FuzzedType, FuzzedValue, FuzzedDescription>
1318

@@ -91,12 +96,25 @@ suspend fun runJavaFuzzing(
9196
val tracer = Trie(Instruction::id)
9297
val descriptionWithOptionalThisInstance = FuzzedDescription(createFuzzedMethodDescription(thisInstance), tracer)
9398
val descriptionWithOnlyParameters = FuzzedDescription(createFuzzedMethodDescription(null), tracer)
94-
runFuzzing(ValueProvider.of(providers), descriptionWithOptionalThisInstance) { _, t ->
95-
if (thisInstance == null) {
96-
exec(null, descriptionWithOnlyParameters, t)
97-
} else {
98-
exec(t.first(), descriptionWithOnlyParameters, t.drop(1))
99+
try {
100+
logger.info { "Starting fuzzing for method: $methodUnderTest" }
101+
logger.info { "\tuse thisInstance = ${thisInstance != null}" }
102+
logger.info { "\tparameters = $parameters" }
103+
var totalExecutionCalled = 0
104+
val totalFuzzingTime = measureNanoTime {
105+
runFuzzing(ValueProvider.of(providers), descriptionWithOptionalThisInstance) { _, t ->
106+
totalExecutionCalled++
107+
if (thisInstance == null) {
108+
exec(null, descriptionWithOnlyParameters, t)
109+
} else {
110+
exec(t.first(), descriptionWithOnlyParameters, t.drop(1))
111+
}
112+
}
99113
}
114+
logger.info { "Finishing fuzzing for method: $methodUnderTest in ${TimeUnit.NANOSECONDS.toMillis(totalFuzzingTime)} ms" }
115+
logger.info { "\tTotal execution called: $totalExecutionCalled" }
116+
} catch (t: Throwable) {
117+
logger.info(t) { "Fuzzing is stopped because of an error" }
100118
}
101119
}
102120

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.utbot.fuzzing.samples;
2+
3+
public class DeepNested {
4+
public class Nested1 {
5+
public class Nested2 {
6+
public int f(int i) {
7+
if (i > 0) {
8+
return 10;
9+
}
10+
return 0;
11+
}
12+
}
13+
}
14+
}

utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,39 @@ package org.utbot.fuzzing
33
import kotlinx.coroutines.runBlocking
44
import org.junit.jupiter.api.Assertions
55
import org.junit.jupiter.api.Test
6+
import org.junit.jupiter.api.assertDoesNotThrow
67
import org.utbot.framework.plugin.api.MethodId
78
import org.utbot.framework.plugin.api.TestIdentityPreservingIdGenerator
89
import org.utbot.framework.plugin.api.UtPrimitiveModel
910
import org.utbot.framework.plugin.api.util.*
1011
import org.utbot.fuzzer.FuzzedConcreteValue
12+
import org.utbot.fuzzing.samples.DeepNested
1113
import org.utbot.fuzzing.samples.Stubs
1214
import org.utbot.fuzzing.utils.Trie
1315

1416
class JavaFuzzingTest {
1517

18+
@Test
19+
fun `fuzzing doesn't throw an exception when type is unknown`() {
20+
assertDoesNotThrow {
21+
runBlockingWithContext {
22+
runJavaFuzzing(
23+
TestIdentityPreservingIdGenerator,
24+
methodUnderTest = MethodId(
25+
DeepNested.Nested1.Nested2::class.id,
26+
"f",
27+
intClassId,
28+
listOf(intClassId)
29+
),
30+
constants = emptyList(),
31+
names = emptyList(),
32+
) { _, _, _ ->
33+
Assertions.fail("This method is never called")
34+
}
35+
}
36+
}
37+
}
38+
1639
@Test
1740
fun `string generates same values`() {
1841
fun collect(): List<String> {

0 commit comments

Comments
 (0)