Skip to content
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

Process clinit in enums concretely for Spring projects #2441

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions utbot-core/src/main/kotlin/org/utbot/common/HackUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ enum class WorkaroundReason {
* construct `toArray` invocation (because streams cannot be consumed twice).
*/
CONSUME_DIRTY_STREAMS,

/**
* During analyzing Spring projects, we process all static initializers in enums concretely because they are used
* very widely and are too big and complicated.
*/
PROCESS_CONCRETELY_STATIC_INITIALIZERS_IN_ENUMS_FOR_SPRING
}
51 changes: 3 additions & 48 deletions utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ import org.utbot.engine.types.NUMBER_OF_PREFERRED_TYPES
import org.utbot.engine.types.OBJECT_TYPE
import org.utbot.engine.types.SECURITY_FIELD_SIGNATURE
import org.utbot.engine.util.statics.concrete.associateEnumSootFieldsWithConcreteValues
import org.utbot.engine.util.statics.concrete.isEnumAffectingExternalStatics
import org.utbot.engine.util.statics.concrete.isEnumValuesFieldName
import org.utbot.engine.util.statics.concrete.makeEnumNonStaticFieldsUpdates
import org.utbot.engine.util.statics.concrete.makeEnumStaticFieldsUpdates
Expand All @@ -118,6 +117,7 @@ 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.StaticInitializerConcreteProcessor
import org.utbot.framework.context.TypeReplacer
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.ExecutableId
Expand Down Expand Up @@ -243,6 +243,7 @@ class Traverser(
private val mocker: Mocker,
private val typeReplacer: TypeReplacer,
private val nonNullSpeculator: NonNullSpeculator,
private val staticInitializerConcreteProcessor: StaticInitializerConcreteProcessor,
private val taintContext: TaintContext,
) : UtContextInitializer() {

Expand Down Expand Up @@ -498,7 +499,7 @@ class Traverser(
// 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)) {
if (staticInitializerConcreteProcessor.shouldProcessStaticFieldConcretely(fieldRef, typeResolver)) {
return processStaticFieldConcretely(fieldRef, stmt)
}

Expand Down Expand Up @@ -544,52 +545,6 @@ class Traverser(
return true
}

/**
* Decides should we read this static field concretely or not.
*/
private fun shouldProcessStaticFieldConcretely(fieldRef: StaticFieldRef): Boolean {
workaround(HACK) {
val className = fieldRef.field.declaringClass.name

// We should process clinit sections for classes from these packages.
// Note that this list is not exhaustive, so it may be supplemented in the future.
val packagesToProcessConcretely = javaPackagesToProcessConcretely + sunPackagesToProcessConcretely

val declaringClass = fieldRef.field.declaringClass

val isFromPackageToProcessConcretely = packagesToProcessConcretely.any { className.startsWith(it) }
// it is required to remove classes we override, since
// we could accidentally initialize their final fields
// with values that will later affect our overridden classes
&& fieldRef.field.declaringClass.type !in classToWrapper.keys
// because of the same reason we should not use
// concrete information from clinit sections for enums
&& !fieldRef.field.declaringClass.isEnum
//hardcoded string for class name is used cause class is not public
//this is a hack to avoid crashing on code with Math.random()
&& !className.endsWith("RandomNumberGeneratorHolder")

// we can process concretely only enums that does not affect the external system
val isEnumNotAffectingExternalStatics = declaringClass.let {
it.isEnum && !it.isEnumAffectingExternalStatics(typeResolver)
}

return isEnumNotAffectingExternalStatics || isFromPackageToProcessConcretely
}
}

private val javaPackagesToProcessConcretely = listOf(
"applet", "awt", "beans", "io", "lang", "math", "net",
"nio", "rmi", "security", "sql", "text", "time", "util"
).map { "java.$it" }

private val sunPackagesToProcessConcretely = listOf(
"applet", "audio", "awt", "corba", "font", "instrument",
"invoke", "io", "java2d", "launcher", "management", "misc",
"net", "nio", "print", "reflect", "rmi", "security",
"swing", "text", "tools.jar", "tracing", "util"
).map { "sun.$it" }

/**
* Checks if field was processed (read) already.
* Otherwise offers to path selector the same statement, but with memory and constraints updates for this field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class UtBotSymbolicEngine(
mocker,
applicationContext.typeReplacer,
applicationContext.nonNullSpeculator,
applicationContext.staticInitializerConcreteProcessor,
taintContext,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ApplicationContext {
val mockerContext: MockerContext
val typeReplacer: TypeReplacer
val nonNullSpeculator: NonNullSpeculator
val staticInitializerConcreteProcessor: StaticInitializerConcreteProcessor

fun createConcreteExecutionContext(
fullClasspath: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.utbot.framework.context

import org.utbot.engine.types.TypeResolver
import soot.jimple.StaticFieldRef

interface StaticInitializerConcreteProcessor {
/**
* Decides should we read this static field concretely or not.
*/
fun shouldProcessStaticFieldConcretely(fieldRef: StaticFieldRef, typeResolver: TypeResolver): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.utbot.framework.context.ApplicationContext
import org.utbot.framework.context.ConcreteExecutionContext
import org.utbot.framework.context.MockerContext
import org.utbot.framework.context.NonNullSpeculator
import org.utbot.framework.context.StaticInitializerConcreteProcessor
import org.utbot.framework.context.TypeReplacer

/**
Expand All @@ -15,7 +16,8 @@ import org.utbot.framework.context.TypeReplacer
class SimpleApplicationContext(
override val mockerContext: MockerContext,
override val typeReplacer: TypeReplacer = SimpleTypeReplacer(),
override val nonNullSpeculator: NonNullSpeculator = SimpleNonNullSpeculator()
override val nonNullSpeculator: NonNullSpeculator = SimpleNonNullSpeculator(),
override val staticInitializerConcreteProcessor: StaticInitializerConcreteProcessor = SimpleStaticInitializerConcreteProcessor
) : ApplicationContext {
override fun createConcreteExecutionContext(
fullClasspath: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.utbot.framework.context.simple

import org.utbot.common.WorkaroundReason
import org.utbot.common.workaround
import org.utbot.engine.classToWrapper
import org.utbot.engine.types.TypeResolver
import org.utbot.engine.util.statics.concrete.isEnumAffectingExternalStatics
import org.utbot.framework.context.StaticInitializerConcreteProcessor
import soot.jimple.StaticFieldRef

object SimpleStaticInitializerConcreteProcessor : StaticInitializerConcreteProcessor {
override fun shouldProcessStaticFieldConcretely(fieldRef: StaticFieldRef, typeResolver: TypeResolver): Boolean =
workaround(WorkaroundReason.HACK) {
val className = fieldRef.field.declaringClass.name

// We should process clinit sections for classes from these packages.
// Note that this list is not exhaustive, so it may be supplemented in the future.
val packagesToProcessConcretely = javaPackagesToProcessConcretely + sunPackagesToProcessConcretely

val declaringClass = fieldRef.field.declaringClass

val isFromPackageToProcessConcretely = packagesToProcessConcretely.any { className.startsWith(it) }
// it is required to remove classes we override, since
// we could accidentally initialize their final fields
// with values that will later affect our overridden classes
&& fieldRef.field.declaringClass.type !in classToWrapper.keys
// because of the same reason we should not use
// concrete information from clinit sections for enums
&& !fieldRef.field.declaringClass.isEnum
//hardcoded string for class name is used cause class is not public
//this is a hack to avoid crashing on code with Math.random()
&& !className.endsWith("RandomNumberGeneratorHolder")

// we can process concretely only enums that does not affect the external system
val isEnumNotAffectingExternalStatics = declaringClass.let {
it.isEnum && !it.isEnumAffectingExternalStatics(typeResolver)
}

return isEnumNotAffectingExternalStatics || isFromPackageToProcessConcretely
}

private val javaPackagesToProcessConcretely: List<String> = listOf(
"applet", "awt", "beans", "io", "lang", "math", "net",
"nio", "rmi", "security", "sql", "text", "time", "util"
).map { "java.$it" }

private val sunPackagesToProcessConcretely: List<String> = listOf(
"applet", "audio", "awt", "corba", "font", "instrument",
"invoke", "io", "java2d", "launcher", "management", "misc",
"net", "nio", "print", "reflect", "rmi", "security",
"swing", "text", "tools.jar", "tracing", "util"
).map { "sun.$it" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.utbot.framework.codegen.generator.SpringCodeGenerator
import org.utbot.framework.context.ApplicationContext
import org.utbot.framework.context.ConcreteExecutionContext
import org.utbot.framework.context.NonNullSpeculator
import org.utbot.framework.context.StaticInitializerConcreteProcessor
import org.utbot.framework.context.TypeReplacer
import org.utbot.framework.context.custom.CoverageFilteringConcreteExecutionContext
import org.utbot.framework.plugin.api.BeanDefinitionData
Expand All @@ -34,6 +35,7 @@ class SpringApplicationContextImpl(

override val typeReplacer: TypeReplacer = SpringTypeReplacer(delegateContext.typeReplacer, this)
override val nonNullSpeculator: NonNullSpeculator = SpringNonNullSpeculator(delegateContext.nonNullSpeculator, this)
override val staticInitializerConcreteProcessor: StaticInitializerConcreteProcessor = SpringStaticInitializerConcreteProcessor

override var concreteContextLoadingResult: ConcreteContextLoadingResult? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.utbot.framework.context.spring

import org.utbot.common.WorkaroundReason
import org.utbot.common.workaround
import org.utbot.engine.types.TypeResolver
import org.utbot.framework.context.StaticInitializerConcreteProcessor
import org.utbot.framework.context.simple.SimpleStaticInitializerConcreteProcessor
import soot.jimple.StaticFieldRef

object SpringStaticInitializerConcreteProcessor : StaticInitializerConcreteProcessor {
override fun shouldProcessStaticFieldConcretely(fieldRef: StaticFieldRef, typeResolver: TypeResolver): Boolean =
workaround(WorkaroundReason.PROCESS_CONCRETELY_STATIC_INITIALIZERS_IN_ENUMS_FOR_SPRING) {
val declaringClass = fieldRef.field.declaringClass

if (declaringClass.isEnum) {
// Since Spring projects have a lot of complicated enums, we cannot waste resources for theirs analysis,
// so always process theirs clinit sections concretely
return true
}

return SimpleStaticInitializerConcreteProcessor.shouldProcessStaticFieldConcretely(fieldRef, typeResolver)
}
}