Skip to content

Make Spring-specific no NPE speculation to only trigger for mocks #2572

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 11 additions & 4 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ import org.utbot.framework.UtSettings
import org.utbot.framework.UtSettings.preferredCexOption
import org.utbot.framework.UtSettings.substituteStaticsWithSymbolicVariable
import org.utbot.framework.isFromTrustedLibrary
import org.utbot.framework.context.ApplicationContext
import org.utbot.framework.context.NonNullSpeculator
import org.utbot.framework.context.TypeReplacer
import org.utbot.framework.plugin.api.ClassId
Expand Down Expand Up @@ -2336,10 +2335,18 @@ class Traverser(
* Marks the [createdField] as speculatively not null if the [field] is considering
* as not producing [NullPointerException].
*
* See more detailed documentation in [ApplicationContext] mentioned methods.
* See more detailed documentation in [NonNullSpeculator] mentioned methods.
*/
private fun checkAndMarkLibraryFieldSpeculativelyNotNull(field: SootField, createdField: SymbolicValue) {
if (nonNullSpeculator.speculativelyCannotProduceNullPointerException(field, methodUnderTest.classId))
private fun checkAndMarkLibraryFieldSpeculativelyNotNull(
field: SootField,
createdField: SymbolicValue,
) {
if (nonNullSpeculator.speculativelyCannotProduceNullPointerException(
field = field,
isMocked = (createdField as? ObjectValue)?.asWrapperOrNull is UtMockWrapper,
classUnderTest = methodUnderTest.classId,
)
)
markAsSpeculativelyNotNull(createdField.addr)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ interface NonNullSpeculator {
* Checks whether accessing [field] (with a method invocation or field access) speculatively
* cannot produce [NullPointerException] (according to its finality or accessibility).
*
* @param [isMocked] true if engine decided it will use either `null` or mock for the [field] value
*
* @see docs/SpeculativeFieldNonNullability.md for more information.
*/
fun speculativelyCannotProduceNullPointerException(
field: SootField,
isMocked: Boolean,
classUnderTest: ClassId,
): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import soot.SootField
class SimpleNonNullSpeculator : NonNullSpeculator {
override fun speculativelyCannotProduceNullPointerException(
field: SootField,
isMocked: Boolean,
classUnderTest: ClassId,
): Boolean =
!UtSettings.maximizeCoverageUsingReflection &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.utbot.framework.context.spring

import mu.KotlinLogging
import org.utbot.framework.context.NonNullSpeculator
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.classId
import org.utbot.framework.plugin.api.FieldId
import org.utbot.framework.plugin.api.util.allDeclaredFieldIds
import org.utbot.framework.plugin.api.util.fieldId
import soot.SootField
Expand All @@ -11,9 +12,35 @@ class SpringNonNullSpeculator(
private val delegateNonNullSpeculator: NonNullSpeculator,
private val springApplicationContext: SpringApplicationContext
) : NonNullSpeculator {
override fun speculativelyCannotProduceNullPointerException(field: SootField, classUnderTest: ClassId): Boolean =
// TODO add ` || delegateNonNullSpeculator.speculativelyCannotProduceNullPointerException(field, classUnderTest)`
// (TODO is added as a part of only equivalent transformations refactoring PR and should be completed in the follow up PR)
field.fieldId in classUnderTest.allDeclaredFieldIds && field.type.classId !in springApplicationContext.allInjectedSuperTypes
companion object {
private val logger = KotlinLogging.logger {}
private val loggedSpeculations = mutableSetOf<Speculation>()
}

private data class Speculation(
val field: FieldId,
val isMocked: Boolean,
val classUnderTest: ClassId,
val speculativelyCannotProduceNPE: Boolean,
)

override fun speculativelyCannotProduceNullPointerException(
field: SootField,
isMocked: Boolean,
classUnderTest: ClassId
): Boolean {
if (delegateNonNullSpeculator.speculativelyCannotProduceNullPointerException(field, isMocked, classUnderTest))
return true

if (field.fieldId !in classUnderTest.allDeclaredFieldIds)
return false

val speculativelyCannotProduceNPE = isMocked

val speculation = Speculation(field.fieldId, isMocked, classUnderTest, speculativelyCannotProduceNPE)
if (loggedSpeculations.add(speculation))
logger.info { "New speculation: $speculation" }

return speculativelyCannotProduceNPE
}
}