From b118c710decbc685364bc1bd27eb4003ecb3049c Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Thu, 9 Nov 2023 16:01:19 +0300 Subject: [PATCH 1/3] Very simple UtAssembleModel improver --- .../contest/usvm/JcToUtExecutionConverter.kt | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt b/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt index 0f76278ec5..14b187421c 100644 --- a/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt +++ b/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt @@ -16,19 +16,27 @@ import org.usvm.instrumentation.testcase.descriptor.Descriptor2ValueConverter import org.usvm.instrumentation.testcase.descriptor.UTestExceptionDescriptor import org.usvm.instrumentation.util.enclosingClass import org.usvm.instrumentation.util.enclosingMethod +import org.utbot.common.isPublic import org.utbot.contest.usvm.executor.JcExecution import org.utbot.framework.codegen.domain.builtin.UtilMethodProvider +import org.utbot.framework.plugin.api.ClassId import org.utbot.framework.plugin.api.Coverage import org.utbot.framework.plugin.api.EnvironmentModels import org.utbot.framework.plugin.api.ExecutableId import org.utbot.framework.plugin.api.Instruction +import org.utbot.framework.plugin.api.UtAssembleModel +import org.utbot.framework.plugin.api.UtExecutableCallModel import org.utbot.framework.plugin.api.UtExecution import org.utbot.framework.plugin.api.UtExecutionFailure import org.utbot.framework.plugin.api.UtExecutionSuccess import org.utbot.framework.plugin.api.UtExplicitlyThrownException import org.utbot.framework.plugin.api.UtImplicitlyThrownException import org.utbot.framework.plugin.api.UtInstrumentation +import org.utbot.framework.plugin.api.UtPrimitiveModel import org.utbot.framework.plugin.api.UtVoidModel +import org.utbot.framework.plugin.api.mapper.UtModelDeepMapper +import org.utbot.framework.plugin.api.util.executableId +import org.utbot.framework.plugin.api.util.jClass import org.utbot.framework.plugin.api.util.utContext import org.utbot.fuzzer.IdGenerator @@ -98,7 +106,42 @@ class JcToUtExecutionConverter( } } ?: return null - return utUsvmExecution + return utUsvmExecution.mapModels(constructAssemblingMapper()) + } + + private fun constructAssemblingMapper(): UtModelDeepMapper = UtModelDeepMapper { model -> + // TODO usvm-sbft: support constructors with parameters here if it is really required + // Unfortunately, it is not possible to use [AssembleModelGeneral] as it requires soot being initialized. + if (model !is UtAssembleModel + || model.instantiationCall.statement.name != "createInstance" || model.modificationsChain.isNotEmpty()) { + return@UtModelDeepMapper model + } + + val instantiatingClassName = model + .instantiationCall + .params + .mapNotNull { (it as? UtPrimitiveModel)?.value.toString() } + .singleOrNull() + ?: return@UtModelDeepMapper model + + val defaultConstructor = ClassId(instantiatingClassName) + .jClass + .constructors + .firstOrNull { it.isPublic && it.parameters.isEmpty() } + + + defaultConstructor?.let { ctor -> + UtAssembleModel( + id = idGenerator.createId(), + classId = model.classId, + modelName = "", + instantiationCall = UtExecutableCallModel( + instance = null, + executable = ctor.executableId, + params = emptyList(), + ) + ) + } ?: model } private fun convertException(exceptionDescriptor: UTestExceptionDescriptor): Throwable = From 49821fa7165d9438199bf5dc82c943c463b02a92 Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Fri, 10 Nov 2023 17:02:22 +0300 Subject: [PATCH 2/3] Use `IdentityHashMap`-based cache when mapping `UtModel`s --- .../api/mapper/UtModelSafeCastingCachingShallowMapper.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelSafeCastingCachingShallowMapper.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelSafeCastingCachingShallowMapper.kt index 1f2164fad4..e511450550 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelSafeCastingCachingShallowMapper.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelSafeCastingCachingShallowMapper.kt @@ -1,11 +1,12 @@ package org.utbot.framework.plugin.api.mapper import org.utbot.framework.plugin.api.UtModel +import java.util.IdentityHashMap class UtModelSafeCastingCachingShallowMapper( val mapper: (UtModel) -> UtModel ) : UtModelMapper { - private val cache = mutableMapOf() + private val cache = IdentityHashMap() override fun map(model: T, clazz: Class): T { val mapped = cache.getOrPut(model) { mapper(model) } From 17cb7df6cc81d656102cbb2d33a224c69ca33e8a Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Sat, 11 Nov 2023 00:01:28 +0300 Subject: [PATCH 3/3] Improve checks for `UtAssembleModel`s created with `createInstance` method --- .../utbot/contest/usvm/JcToUtExecutionConverter.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt b/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt index 14b187421c..7845e9af76 100644 --- a/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt +++ b/utbot-junit-contest/src/main/kotlin/org/utbot/contest/usvm/JcToUtExecutionConverter.kt @@ -46,7 +46,7 @@ class JcToUtExecutionConverter( private val jcExecution: JcExecution, private val idGenerator: IdGenerator, private val instructionIdProvider: InstructionIdProvider, - utilMethodProvider: UtilMethodProvider, + private val utilMethodProvider: UtilMethodProvider, ) { private val toValueConverter = Descriptor2ValueConverter(utContext.classLoader) @@ -113,16 +113,15 @@ class JcToUtExecutionConverter( // TODO usvm-sbft: support constructors with parameters here if it is really required // Unfortunately, it is not possible to use [AssembleModelGeneral] as it requires soot being initialized. if (model !is UtAssembleModel - || model.instantiationCall.statement.name != "createInstance" || model.modificationsChain.isNotEmpty()) { + || utilMethodProvider.createInstanceMethodId != model.instantiationCall.statement + || model.modificationsChain.isNotEmpty()) { return@UtModelDeepMapper model } - val instantiatingClassName = model + val instantiatingClassName = (model .instantiationCall .params - .mapNotNull { (it as? UtPrimitiveModel)?.value.toString() } - .singleOrNull() - ?: return@UtModelDeepMapper model + .single() as UtPrimitiveModel).value.toString() val defaultConstructor = ClassId(instantiatingClassName) .jClass