Skip to content

Add options for clinit sections #1636

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 3 commits into from
Jan 10, 2023
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 @@ -505,6 +505,20 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
* The behaviour of further analysis if tests generation cancellation is requested.
*/
var cancellationStrategyType by getEnumProperty(CancellationStrategyType.SAVE_PROCESSED_RESULTS)

/**
* Depending on this option, <clinit> sections might be analyzed or not.
* Note that some clinit sections still will be initialized using runtime information.
*/
var enableClinitSectionsAnalysis by getBooleanProperty(true)

/**
* Process all clinit sections concretely.
*
* If [enableClinitSectionsAnalysis] is false, it disables effect of this option as well.
* Note that values processed concretely won't be replaced with unbounded symbolic variables.
*/
var processAllClinitSectionsConcretely by getBooleanProperty(false)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ inline fun <reified T> withoutConcrete(block: () -> T): T {
}
}

inline fun <reified T> withProcessingClinitSections(value: Boolean, block: () -> T): T {
val prev = UtSettings.enableClinitSectionsAnalysis
UtSettings.enableClinitSectionsAnalysis = value
try {
return block()
} finally {
UtSettings.enableClinitSectionsAnalysis = prev
}
}

inline fun <reified T> withProcessingAllClinitSectionsConcretely(value: Boolean, block: () -> T): T {
val prev = UtSettings.processAllClinitSectionsConcretely
UtSettings.processAllClinitSectionsConcretely = value
try {
return block()
} finally {
UtSettings.processAllClinitSectionsConcretely = prev
}
}


/**
* Run [block] with disabled sandbox in the concrete executor
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.utbot.examples.objects

import org.junit.jupiter.api.Test
import org.utbot.testcheckers.eq
import org.utbot.testcheckers.withProcessingAllClinitSectionsConcretely
import org.utbot.testcheckers.withoutConcrete
import org.utbot.testcheckers.withProcessingClinitSections
import org.utbot.testing.UtValueTestCaseChecker
import org.utbot.testing.atLeast

internal class ClassForTestClinitSectionsTest : UtValueTestCaseChecker(testClass = ClassForTestClinitSections::class) {
@Test
fun testClinitWithoutClinitAnalysis() {
withoutConcrete {
withProcessingClinitSections(value = false) {
check(
ClassForTestClinitSections::resultDependingOnStaticSection,
eq(2),
{ r -> r == -1 },
{ r -> r == 1 }
)
}
}
}

@Test
fun testClinitWithClinitAnalysis() {
withoutConcrete {
check(
ClassForTestClinitSections::resultDependingOnStaticSection,
eq(2),
{ r -> r == -1 },
{ r -> r == 1 }
)
}
}

@Test
fun testProcessConcretelyWithoutClinitAnalysis() {
withoutConcrete {
withProcessingClinitSections(value = false) {
withProcessingAllClinitSectionsConcretely(value = true) {
check(
ClassForTestClinitSections::resultDependingOnStaticSection,
eq(2),
{ r -> r == -1 },
{ r -> r == 1 }
)
}
}
}
}

@Test
fun testProcessClinitConcretely() {
withoutConcrete {
withProcessingAllClinitSectionsConcretely(value = true) {
check(
ClassForTestClinitSections::resultDependingOnStaticSection,
eq(1),
{ r -> r == -1 },
coverage = atLeast(71)
)
}
}
}
}

13 changes: 13 additions & 0 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,23 @@ class Traverser(
fieldRef: StaticFieldRef,
stmt: Stmt
): Boolean {
// This order of processing options is important.
// First, we should process classes that
// cannot be analyzed without clinit sections, e.g., enums
if (shouldProcessStaticFieldConcretely(fieldRef)) {
return processStaticFieldConcretely(fieldRef, stmt)
}

// Then we should check if we should analyze clinit sections at all
if (!UtSettings.enableClinitSectionsAnalysis) {
return false
}

// Finally, we decide whether we should analyze clinit sections concretely or not
if (UtSettings.processAllClinitSectionsConcretely) {
return processStaticFieldConcretely(fieldRef, stmt)
}

val field = fieldRef.field
val declaringClass = field.declaringClass
val declaringClassId = declaringClass.id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.utbot.examples.objects;

public class ClassForTestClinitSections {
private static int x = 5;

public int resultDependingOnStaticSection() {
if (x == 5) {
return -1;
}

return 1;
}
}