Skip to content

Commit 7a8b22f

Browse files
committed
Correctly handle randomizing of Seed.Recursive modifications and ignore null-this in Java fuzzing
1 parent 9ec9f3b commit 7a8b22f

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ class UtBotSymbolicEngine(
357357
names,
358358
listOf(transform(ValueProvider.of(defaultValueProviders(defaultIdGenerator))))
359359
) { thisInstance, descr, values ->
360+
if (thisInstance?.model is UtNullModel) {
361+
// We should not try to run concretely any models with null-this.
362+
// But fuzzer does generate such values, because it can fail to generate any "good" values.
363+
return@runJavaFuzzing BaseFeedback(Trie.emptyNode(), Control.PASS)
364+
}
365+
360366
val diff = until - System.currentTimeMillis()
361367
val thresholdMillisForFuzzingOperation = 0 // may be better use 10-20 millis as it might not be possible
362368
// to concretely execute that values because request to instrumentation process involves

utbot-fuzzing/src/main/kotlin/org/utbot/fuzzing/Api.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private object EmptyFeedback : Feedback<Nothing, Nothing> {
248248
}
249249
}
250250

251-
private class NoSeedValueException(
251+
class NoSeedValueException internal constructor(
252252
// this type cannot be generalized because Java forbids types for [Throwable].
253253
val type: Any?
254254
) : Exception("No seed candidates generated for type: $type")
@@ -463,7 +463,11 @@ private fun <TYPE, RESULT, DESCRIPTION : Description<TYPE>, FEEDBACK : Feedback<
463463
State(state.recursionTreeDepth + 1, state.cache, state.missedTypes)
464464
),
465465
modify = task.modify
466-
.shuffled(random)
466+
.toMutableList()
467+
.transformIfNotEmpty {
468+
shuffle(random)
469+
take(random.nextInt(size + 1))
470+
}
467471
.mapTo(arrayListOf()) { routine ->
468472
fuzz(
469473
routine.types,
@@ -474,15 +478,13 @@ private fun <TYPE, RESULT, DESCRIPTION : Description<TYPE>, FEEDBACK : Feedback<
474478
routine,
475479
State(state.recursionTreeDepth + 1, state.cache, state.missedTypes)
476480
)
477-
}.transformIfNotEmpty {
478-
take(random.nextInt(size + 1).coerceAtLeast(1))
479481
}
480482
)
481483
} catch (nsv: NoSeedValueException) {
482484
@Suppress("UNCHECKED_CAST")
483485
state.missedTypes[nsv.type as TYPE] = task
484486
if (configuration.generateEmptyRecursiveForMissedTypes) {
485-
Result.Empty { task.empty() }
487+
Result.Empty { task.empty.builder() }
486488
} else {
487489
throw nsv
488490
}

utbot-fuzzing/src/main/kotlin/org/utbot/fuzzing/utils/Functions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal enum class BinaryFormat : (Int) -> Boolean {
8181
DOUBLE { override fun invoke(index: Int) = index % 16 == 0 && index != 0 },
8282
}
8383

84-
internal fun <T> List<T>.transformIfNotEmpty(transform: List<T>.() -> List<T>): List<T> {
84+
internal fun <T> MutableList<T>.transformIfNotEmpty(transform: MutableList<T>.() -> List<T>): List<T> {
8585
return if (isNotEmpty()) transform() else this
8686
}
8787

utbot-fuzzing/src/test/kotlin/org/utbot/fuzzing/FuzzerSmokeTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ class FuzzerSmokeTest {
2727

2828
@Test
2929
fun `fuzzing throws an exception if no values generated for some type`() {
30-
assertThrows<IllegalStateException> {
30+
assertThrows<NoSeedValueException> {
3131
runBlocking {
3232
var count = 0
3333
runFuzzing<Unit, Unit, Description<Unit>, BaseFeedback<Unit, Unit, Unit>>(
34-
{ _, _ -> sequenceOf() },
35-
Description(listOf(Unit))
34+
provider = { _, _ -> sequenceOf() },
35+
description = Description(listOf(Unit)),
36+
configuration = Configuration(
37+
generateEmptyCollectionsForMissedTypes = false
38+
)
3639
) { _, _ ->
3740
count += 1
3841
BaseFeedback(Unit, Control.STOP)

0 commit comments

Comments
 (0)