Skip to content

Fuzzing breaks on nested classes #1537 #1552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class UtBotSymbolicEngine(
var attempts = 0
val attemptsLimit = UtSettings.fuzzingMaxAttempts
val names = graph.body.method.tags.filterIsInstance<ParamNamesTag>().firstOrNull()?.names ?: emptyList()

var testEmittedByFuzzer = 0
runJavaFuzzing(
defaultIdGenerator,
methodUnderTest,
Expand All @@ -349,6 +349,7 @@ class UtBotSymbolicEngine(
) { thisInstance, descr, values ->
if (controller.job?.isActive == false || System.currentTimeMillis() >= until) {
logger.info { "Fuzzing overtime: $methodUnderTest" }
logger.info { "Test created by fuzzer: $testEmittedByFuzzer" }
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.STOP)
}

Expand Down Expand Up @@ -404,6 +405,7 @@ class UtBotSymbolicEngine(
)
)

testEmittedByFuzzer++
BaseFeedback(result = trieNode ?: Trie.emptyNode(), control = Control.CONTINUE)
}
}
Expand Down
28 changes: 23 additions & 5 deletions utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.utbot.fuzzing

import mu.KotlinLogging
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.ExecutableId
import org.utbot.framework.plugin.api.Instruction
Expand All @@ -8,6 +9,10 @@ import org.utbot.fuzzer.*
import org.utbot.fuzzing.providers.*
import org.utbot.fuzzing.utils.Trie
import java.lang.reflect.*
import java.util.concurrent.TimeUnit
import kotlin.system.measureNanoTime

private val logger = KotlinLogging.logger {}

typealias JavaValueProvider = ValueProvider<FuzzedType, FuzzedValue, FuzzedDescription>

Expand Down Expand Up @@ -91,12 +96,25 @@ suspend fun runJavaFuzzing(
val tracer = Trie(Instruction::id)
val descriptionWithOptionalThisInstance = FuzzedDescription(createFuzzedMethodDescription(thisInstance), tracer)
val descriptionWithOnlyParameters = FuzzedDescription(createFuzzedMethodDescription(null), tracer)
runFuzzing(ValueProvider.of(providers), descriptionWithOptionalThisInstance) { _, t ->
if (thisInstance == null) {
exec(null, descriptionWithOnlyParameters, t)
} else {
exec(t.first(), descriptionWithOnlyParameters, t.drop(1))
try {
logger.info { "Starting fuzzing for method: $methodUnderTest" }
logger.info { "\tuse thisInstance = ${thisInstance != null}" }
logger.info { "\tparameters = $parameters" }
var totalExecutionCalled = 0
val totalFuzzingTime = measureNanoTime {
runFuzzing(ValueProvider.of(providers), descriptionWithOptionalThisInstance) { _, t ->
totalExecutionCalled++
if (thisInstance == null) {
exec(null, descriptionWithOnlyParameters, t)
} else {
exec(t.first(), descriptionWithOnlyParameters, t.drop(1))
}
}
}
logger.info { "Finishing fuzzing for method: $methodUnderTest in ${TimeUnit.NANOSECONDS.toMillis(totalFuzzingTime)} ms" }
logger.info { "\tTotal execution called: $totalExecutionCalled" }
} catch (t: Throwable) {
logger.info(t) { "Fuzzing is stopped because of an error" }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.utbot.fuzzing.samples;

public class DeepNested {
public class Nested1 {
public class Nested2 {
public int f(int i) {
if (i > 0) {
return 10;
}
return 0;
}
}
}
}
23 changes: 23 additions & 0 deletions utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@ package org.utbot.fuzzing
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.utbot.framework.plugin.api.MethodId
import org.utbot.framework.plugin.api.TestIdentityPreservingIdGenerator
import org.utbot.framework.plugin.api.UtPrimitiveModel
import org.utbot.framework.plugin.api.util.*
import org.utbot.fuzzer.FuzzedConcreteValue
import org.utbot.fuzzing.samples.DeepNested
import org.utbot.fuzzing.samples.Stubs
import org.utbot.fuzzing.utils.Trie

class JavaFuzzingTest {

@Test
fun `fuzzing doesn't throw an exception when type is unknown`() {
assertDoesNotThrow {
runBlockingWithContext {
runJavaFuzzing(
TestIdentityPreservingIdGenerator,
methodUnderTest = MethodId(
DeepNested.Nested1.Nested2::class.id,
"f",
intClassId,
listOf(intClassId)
),
constants = emptyList(),
names = emptyList(),
) { _, _, _ ->
Assertions.fail("This method is never called")
}
}
}
}

@Test
fun `string generates same values`() {
fun collect(): List<String> {
Expand Down