Skip to content
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
Fix unnecessary reflection calls and infinite loop in self-reference …
…initializations
  • Loading branch information
sofurihafe committed Dec 7, 2022
commit de865a56cc4d7e02e2d995e81205e14e42e99fe0
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ class AssembleModelGenerator(private val basePackageName: String) {
assembleModel
}

private val modelsInAnalysis = mutableListOf<UtCompositeModel>()

/**
* Assembles internal structure of [UtCompositeModel] if possible and handles assembling exceptions.
*/
Expand All @@ -243,7 +245,14 @@ class AssembleModelGenerator(private val basePackageName: String) {
val constructorId = findBestConstructorOrNull(compositeModel)
?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.classId}")

val constructorInfo = constructorAnalyzer.analyze(constructorId)
// we do not analyze a constructor which is currently in the analysis
// thus, we do not encounter an infinite loop in self or cross-reference situations
val shouldAnalyzeConstructor = compositeModel !in modelsInAnalysis
modelsInAnalysis.add(compositeModel)

val constructorInfo =
if (shouldAnalyzeConstructor) constructorAnalyzer.analyze(constructorId)
else ConstructorAssembleInfo(constructorId)

val instantiationCall = constructorCall(compositeModel, constructorInfo)
return UtAssembleModel(
Expand Down Expand Up @@ -284,6 +293,8 @@ class AssembleModelGenerator(private val basePackageName: String) {
} catch (e: AssembleException) {
instantiatedModels.remove(compositeModel)
throw e
} finally {
modelsInAnalysis.remove(compositeModel)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import soot.jimple.internal.JimpleLocal
* */
data class ConstructorAssembleInfo(
val constructorId: ConstructorId,
val params: Map<Int, FieldId>,
val setFields: Set<FieldId>,
val affectedFields: Set<FieldId>
val params: Map<Int, FieldId> = mapOf(),
val setFields: Set<FieldId> = setOf(),
val affectedFields: Set<FieldId> = setOf()
)

/**
Expand Down Expand Up @@ -109,18 +109,11 @@ class ConstructorAnalyzer {
return jimpleLocal.name.first() != '$'
}

private val visitedConstructors = mutableSetOf<SootMethod>()

private fun analyze(
sootConstructor: SootMethod,
setFields: MutableSet<FieldId>,
affectedFields: MutableSet<FieldId>,
): Map<Int, FieldId> {
if (sootConstructor in visitedConstructors) {
return emptyMap()
}
visitedConstructors.add(sootConstructor)

val jimpleBody = retrieveJimpleBody(sootConstructor) ?: return emptyMap()
analyzeAssignments(jimpleBody, setFields, affectedFields)

Expand Down