-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathUtBotSymbolicEngine.kt
More file actions
641 lines (556 loc) · 28.1 KB
/
UtBotSymbolicEngine.kt
File metadata and controls
641 lines (556 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package org.utbot.engine
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import mu.KotlinLogging
import org.utbot.analytics.EngineAnalyticsContext
import org.utbot.analytics.FeatureProcessor
import org.utbot.analytics.Predictors
import org.utbot.api.exception.UtMockAssumptionViolatedException
import org.utbot.common.bracket
import org.utbot.common.debug
import org.utbot.engine.MockStrategy.NO_MOCKS
import org.utbot.engine.pc.*
import org.utbot.engine.selectors.*
import org.utbot.engine.selectors.nurs.NonUniformRandomSearch
import org.utbot.engine.selectors.strategies.GraphViz
import org.utbot.engine.state.ExecutionStackElement
import org.utbot.engine.state.ExecutionState
import org.utbot.engine.state.StateLabel
import org.utbot.engine.symbolic.SymbolicState
import org.utbot.engine.symbolic.asHardConstraint
import org.utbot.engine.types.TypeRegistry
import org.utbot.engine.types.TypeResolver
import org.utbot.engine.util.mockListeners.MockListener
import org.utbot.engine.util.mockListeners.MockListenerController
import org.utbot.framework.PathSelectorType
import org.utbot.framework.UtSettings
import org.utbot.framework.UtSettings.checkSolverTimeoutMillis
import org.utbot.framework.UtSettings.enableFeatureProcess
import org.utbot.framework.UtSettings.pathSelectorStepsLimit
import org.utbot.framework.UtSettings.pathSelectorType
import org.utbot.framework.UtSettings.processUnknownStatesDuringConcreteExecution
import org.utbot.framework.UtSettings.useDebugVisualization
import org.utbot.framework.util.convertToAssemble
import org.utbot.framework.plugin.api.*
import org.utbot.framework.plugin.api.Step
import org.utbot.framework.plugin.api.util.*
import org.utbot.framework.util.graph
import org.utbot.framework.util.sootMethod
import org.utbot.fuzzer.*
import org.utbot.fuzzing.*
import org.utbot.fuzzing.utils.Trie
import org.utbot.instrumentation.ConcreteExecutor
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionData
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrumentation
import soot.jimple.Stmt
import soot.tagkit.ParamNamesTag
import java.lang.reflect.Method
import kotlin.math.min
import kotlin.system.measureTimeMillis
val logger = KotlinLogging.logger {}
val pathLogger = KotlinLogging.logger(logger.name + ".path")
//in future we should put all timeouts here
class EngineController {
var paused: Boolean = false
var executeConcretely: Boolean = false
var stop: Boolean = false
var job: Job? = null
}
//for debugging purpose only
private var stateSelectedCount = 0
private val defaultIdGenerator = ReferencePreservingIntIdGenerator()
private fun pathSelector(graph: InterProceduralUnitGraph, typeRegistry: TypeRegistry) =
when (pathSelectorType) {
PathSelectorType.COVERED_NEW_SELECTOR -> coveredNewSelector(graph) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.INHERITORS_SELECTOR -> inheritorsSelector(graph, typeRegistry) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.SUBPATH_GUIDED_SELECTOR -> subpathGuidedSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.CPI_SELECTOR -> cpInstSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.FORK_DEPTH_SELECTOR -> forkDepthSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.ML_SELECTOR -> mlSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.TORCH_SELECTOR -> mlSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.RANDOM_SELECTOR -> randomSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
PathSelectorType.RANDOM_PATH_SELECTOR -> randomPathSelector(graph, StrategyOption.DISTANCE) {
withStepsLimit(pathSelectorStepsLimit)
}
}
class UtBotSymbolicEngine(
private val controller: EngineController,
private val methodUnderTest: ExecutableId,
classpath: String,
dependencyPaths: String,
val mockStrategy: MockStrategy = NO_MOCKS,
chosenClassesToMockAlways: Set<ClassId>,
private val solverTimeoutInMillis: Int = checkSolverTimeoutMillis
) : UtContextInitializer() {
private val graph = methodUnderTest.sootMethod.jimpleBody().apply {
logger.trace { "JIMPLE for $methodUnderTest:\n$this" }
}.graph()
private val methodUnderAnalysisStmts: Set<Stmt> = graph.stmts.toSet()
private val globalGraph = InterProceduralUnitGraph(graph)
private val typeRegistry: TypeRegistry = TypeRegistry()
private val pathSelector: PathSelector = pathSelector(globalGraph, typeRegistry)
internal val hierarchy: Hierarchy = Hierarchy(typeRegistry)
// TODO HACK violation of encapsulation
internal val typeResolver: TypeResolver = TypeResolver(typeRegistry, hierarchy)
private val classUnderTest: ClassId = methodUnderTest.classId
private val mocker: Mocker = Mocker(
mockStrategy,
classUnderTest,
hierarchy,
chosenClassesToMockAlways,
MockListenerController(controller)
)
fun attachMockListener(mockListener: MockListener) = mocker.mockListenerController?.attach(mockListener)
fun detachMockListener(mockListener: MockListener) = mocker.mockListenerController?.detach(mockListener)
private val statesForConcreteExecution: MutableList<ExecutionState> = mutableListOf()
private val traverser = Traverser(
methodUnderTest,
typeRegistry,
hierarchy,
typeResolver,
globalGraph,
mocker,
)
//HACK (long strings)
internal var softMaxArraySize = 40
private val concreteExecutor =
ConcreteExecutor(
UtExecutionInstrumentation,
classpath,
).apply { this.classLoader = utContext.classLoader }
private val featureProcessor: FeatureProcessor? =
if (enableFeatureProcess) EngineAnalyticsContext.featureProcessorFactory(globalGraph) else null
private val trackableResources: MutableSet<AutoCloseable> = mutableSetOf()
private fun postTraverse() {
for (r in trackableResources)
try {
r.close()
} catch (e: Throwable) {
logger.error(e) { "Closing resource failed" }
}
trackableResources.clear()
featureProcessor?.dumpFeatures()
}
private suspend fun preTraverse() {
//fixes leak in useless Context() created in AutoCloseable()
close()
if (!currentCoroutineContext().isActive) return
stateSelectedCount = 0
}
fun traverse(): Flow<UtResult> = traverseImpl()
.onStart { preTraverse() }
.onCompletion { postTraverse() }
private fun traverseImpl(): Flow<UtResult> = flow {
require(trackableResources.isEmpty())
if (useDebugVisualization) GraphViz(globalGraph, pathSelector)
val initStmt = graph.head
val initState = ExecutionState(
initStmt,
SymbolicState(UtSolver(typeRegistry, trackableResources, solverTimeoutInMillis)),
executionStack = persistentListOf(ExecutionStackElement(null, method = graph.body.method))
)
pathSelector.offer(initState)
pathSelector.use {
while (currentCoroutineContext().isActive) {
if (controller.stop)
break
if (controller.paused) {
try {
yield()
} catch (e: CancellationException) { //todo in future we should just throw cancellation
break
}
continue
}
stateSelectedCount++
pathLogger.trace {
"traverse<$methodUnderTest>: choosing next state($stateSelectedCount), " +
"queue size=${(pathSelector as? NonUniformRandomSearch)?.size ?: -1}"
}
if (controller.executeConcretely || statesForConcreteExecution.isNotEmpty()) {
val state = pathSelector.pollUntilFastSAT()
?: statesForConcreteExecution.pollUntilSat(processUnknownStatesDuringConcreteExecution)
?: break
// This state can contain inconsistent wrappers - for example, Map with keys but missing values.
// We cannot use withWrapperConsistencyChecks here because it needs solver to work.
// So, we have to process such cases accurately in wrappers resolving.
logger.trace { "executing $state concretely..." }
logger.debug().bracket("concolicStrategy<$methodUnderTest>: execute concretely") {
val resolver = Resolver(
hierarchy,
state.memory,
typeRegistry,
typeResolver,
state.solver.lastStatus as UtSolverStatusSAT,
methodUnderTest,
softMaxArraySize,
traverser.objectCounter
)
val resolvedParameters = state.methodUnderTestParameters
val (modelsBefore, _, instrumentation) = resolver.resolveModels(resolvedParameters)
val stateBefore = modelsBefore.constructStateForMethod(methodUnderTest)
try {
val concreteExecutionResult =
concreteExecutor.executeConcretely(methodUnderTest, stateBefore, instrumentation, UtSettings.concreteExecutionDefaultTimeoutInInstrumentedProcessMillis)
if (concreteExecutionResult.violatesUtMockAssumption()) {
logger.debug { "Generated test case violates the UtMock assumption: $concreteExecutionResult" }
return@bracket
}
val concreteUtExecution = UtSymbolicExecution(
stateBefore,
concreteExecutionResult.stateAfter,
concreteExecutionResult.result,
instrumentation,
mutableListOf(),
listOf(),
concreteExecutionResult.coverage
)
emit(concreteUtExecution)
logger.debug { "concolicStrategy<${methodUnderTest}>: returned $concreteUtExecution" }
} catch (e: CancellationException) {
logger.debug(e) { "Cancellation happened" }
} catch (e: ConcreteExecutionFailureException) {
emitFailedConcreteExecutionResult(stateBefore, e)
} catch (e: Throwable) {
emit(UtError("Concrete execution failed", e))
}
}
} else {
val state = pathSelector.poll()
// state is null in case states queue is empty
// or path selector exceed some limits (steps limit, for example)
if (state == null) {
// check do we have remaining states that we can execute concretely
val pathSelectorStatesForConcreteExecution = pathSelector
.remainingStatesForConcreteExecution
.map { it.withWrapperConsistencyChecks() }
if (pathSelectorStatesForConcreteExecution.isNotEmpty()) {
statesForConcreteExecution += pathSelectorStatesForConcreteExecution
logger.debug {
"${pathSelectorStatesForConcreteExecution.size} remaining states " +
"were moved from path selector for concrete execution"
}
continue // the next step in while loop processes concrete states
} else {
break
}
}
state.executingTime += measureTimeMillis {
val newStates = try {
traverser.traverse(state)
} catch (ex: Throwable) {
emit(UtError(ex.description, ex))
return@measureTimeMillis
}
for (newState in newStates) {
when (newState.label) {
StateLabel.INTERMEDIATE -> pathSelector.offer(newState)
StateLabel.CONCRETE -> statesForConcreteExecution.add(newState)
StateLabel.TERMINAL -> consumeTerminalState(newState)
}
}
// Here job can be cancelled from within traverse, e.g. by using force mocking without Mockito.
// So we need to make it throw CancelledException by method below:
currentCoroutineContext().job.ensureActive()
}
// TODO: think about concise modifying globalGraph in Traverser and UtBotSymbolicEngine
globalGraph.visitNode(state)
}
}
}
}
/**
* Run fuzzing flow.
*
* @param until is used by fuzzer to cancel all tasks if the current time is over this value
* @param transform provides model values for a method
*/
fun fuzzing(until: Long = Long.MAX_VALUE, transform: (JavaValueProvider) -> JavaValueProvider = { it }) = flow {
val isFuzzable = methodUnderTest.parameters.all { classId ->
classId != Method::class.java.id && // causes the instrumented process crash at invocation
classId != Class::class.java.id // causes java.lang.IllegalAccessException: java.lang.Class at sun.misc.Unsafe.allocateInstance(Native Method)
}
val hasMethodUnderTestParametersToFuzz = methodUnderTest.parameters.isNotEmpty()
if (!isFuzzable || !hasMethodUnderTestParametersToFuzz && methodUnderTest.isStatic) {
// Currently, fuzzer doesn't work with static methods with empty parameters
return@flow
}
val errorStackTraceTracker = Trie(StackTraceElement::toString)
var attempts = 0
val attemptsLimit = UtSettings.fuzzingMaxAttempts
val names = graph.body.method.tags.filterIsInstance<ParamNamesTag>().firstOrNull()?.names ?: emptyList()
var testEmittedByFuzzer = 0
runJavaFuzzing(
defaultIdGenerator,
methodUnderTest,
collectConstantsForFuzzer(graph),
names,
listOf(transform(ValueProvider.of(defaultValueProviders(defaultIdGenerator))))
) { thisInstance, descr, values ->
val diff = until - System.currentTimeMillis()
val thresholdMillisForFuzzingOperation = 0 // may be better use 10-20 millis as it might not be possible
// to concretely execute that values because request to instrumentation process involves
// 1. serializing/deserializing it with kryo
// 2. sending over rd
// 3. concrete execution itself
// 4. analyzing concrete result
if (controller.job?.isActive == false || diff <= thresholdMillisForFuzzingOperation) {
logger.info { "Fuzzing overtime: $methodUnderTest" }
logger.info { "Test created by fuzzer: $testEmittedByFuzzer" }
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.STOP)
}
val initialEnvironmentModels = EnvironmentModels(thisInstance?.model, values.map { it.model }, mapOf())
val concreteExecutionResult: UtConcreteExecutionResult? = try {
val timeoutMillis = min(UtSettings.concreteExecutionDefaultTimeoutInInstrumentedProcessMillis, diff)
concreteExecutor.executeConcretely(methodUnderTest, initialEnvironmentModels, listOf(), timeoutMillis)
} catch (e: CancellationException) {
logger.debug { "Cancelled by timeout" }; null
} catch (e: ConcreteExecutionFailureException) {
emitFailedConcreteExecutionResult(initialEnvironmentModels, e); null
} catch (e: Throwable) {
emit(UtError("Default concrete execution failed", e)); null
}
// in case an exception occurred from the concrete execution
concreteExecutionResult ?: return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.PASS)
if (concreteExecutionResult.violatesUtMockAssumption()) {
logger.debug { "Generated test case by fuzzer violates the UtMock assumption: $concreteExecutionResult" }
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.PASS)
}
val coveredInstructions = concreteExecutionResult.coverage.coveredInstructions
var trieNode: Trie.Node<Instruction>? = null
if (coveredInstructions.isNotEmpty()) {
trieNode = descr.tracer.add(coveredInstructions)
if (trieNode.count > 1) {
if (++attempts >= attemptsLimit) {
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.STOP)
}
return@runJavaFuzzing BaseFeedback(result = trieNode, control = Control.CONTINUE)
}
} else {
logger.error { "Coverage is empty for $methodUnderTest with $values" }
val result = concreteExecutionResult.result
if (result is UtSandboxFailure) {
val stackTraceElements = result.exception.stackTrace.reversed()
if (errorStackTraceTracker.add(stackTraceElements).count > 1) {
return@runJavaFuzzing BaseFeedback(result = Trie.emptyNode(), control = Control.PASS)
}
}
}
emit(
UtFuzzedExecution(
stateBefore = initialEnvironmentModels,
stateAfter = concreteExecutionResult.stateAfter,
result = concreteExecutionResult.result,
coverage = concreteExecutionResult.coverage,
fuzzingValues = values,
fuzzedMethodDescription = descr.description
)
)
testEmittedByFuzzer++
BaseFeedback(result = trieNode ?: Trie.emptyNode(), control = Control.CONTINUE)
}
}
private suspend fun FlowCollector<UtResult>.emitFailedConcreteExecutionResult(
stateBefore: EnvironmentModels,
e: ConcreteExecutionFailureException
) {
val failedConcreteExecution = UtFailedExecution(
stateBefore = stateBefore,
result = UtConcreteExecutionFailure(e)
)
emit(failedConcreteExecution)
}
private suspend fun FlowCollector<UtResult>.consumeTerminalState(
state: ExecutionState,
) {
// some checks to be sure the state is correct
require(state.label == StateLabel.TERMINAL) { "Can't process non-terminal state!" }
require(!state.isInNestedMethod()) { "The state has to correspond to the MUT" }
val memory = state.memory
val solver = state.solver
val parameters = state.parameters.map { it.value }
val symbolicResult = requireNotNull(state.methodResult?.symbolicResult) { "The state must have symbolicResult" }
val holder = requireNotNull(solver.lastStatus as? UtSolverStatusSAT) { "The state must be SAT!" }
val predictedTestName = Predictors.testName.predict(state.path)
Predictors.testName.provide(state.path, predictedTestName, "")
// resolving
val resolver = Resolver(
hierarchy,
memory,
typeRegistry,
typeResolver,
holder,
methodUnderTest,
softMaxArraySize,
traverser.objectCounter
)
val (modelsBefore, modelsAfter, instrumentation) = resolver.resolveModels(parameters)
val symbolicExecutionResult = resolver.resolveResult(symbolicResult)
val stateBefore = modelsBefore.constructStateForMethod(methodUnderTest)
val stateAfter = modelsAfter.constructStateForMethod(methodUnderTest)
require(stateBefore.parameters.size == stateAfter.parameters.size)
val symbolicUtExecution = UtSymbolicExecution(
stateBefore = stateBefore,
stateAfter = stateAfter,
result = symbolicExecutionResult,
instrumentation = instrumentation,
path = entryMethodPath(state),
fullPath = state.fullPath()
)
globalGraph.traversed(state)
if (!UtSettings.useConcreteExecution ||
// Can't execute concretely because overflows do not cause actual exceptions.
// Still, we need overflows to act as implicit exceptions.
(UtSettings.treatOverflowAsError && symbolicExecutionResult is UtOverflowFailure)
) {
logger.debug {
"processResult<${methodUnderTest}>: no concrete execution allowed, " +
"emit purely symbolic result $symbolicUtExecution"
}
emit(symbolicUtExecution)
return
}
// Check for lambda result as it cannot be emitted by concrete execution
(symbolicExecutionResult as? UtExecutionSuccess)?.takeIf { it.model is UtLambdaModel }?.run {
logger.debug {
"processResult<${methodUnderTest}>: impossible to create concrete value for lambda result ($model), " +
"emit purely symbolic result $symbolicUtExecution"
}
emit(symbolicUtExecution)
return
}
//It's possible that symbolic and concrete stateAfter/results are diverged.
//So we trust concrete results more.
try {
logger.debug().bracket("processResult<$methodUnderTest>: concrete execution") {
//this can throw CancellationException
val concreteExecutionResult = concreteExecutor.executeConcretely(
methodUnderTest,
stateBefore,
instrumentation,
UtSettings.concreteExecutionDefaultTimeoutInInstrumentedProcessMillis
)
if (concreteExecutionResult.violatesUtMockAssumption()) {
logger.debug { "Generated test case violates the UtMock assumption: $concreteExecutionResult" }
return
}
val concolicUtExecution = symbolicUtExecution.copy(
stateAfter = concreteExecutionResult.stateAfter,
result = concreteExecutionResult.result,
coverage = concreteExecutionResult.coverage
)
emit(concolicUtExecution)
logger.debug { "processResult<${methodUnderTest}>: returned $concolicUtExecution" }
}
} catch (e: ConcreteExecutionFailureException) {
emitFailedConcreteExecutionResult(stateBefore, e)
} catch (e: CancellationException) {
logger.debug(e) { "Cancellation happened" }
} catch (e: Throwable) {
emit(UtError("Default concrete execution failed", e));
}
}
/**
* Collects entry method statement path for ML. Eliminates duplicated statements, e.g. assignment with invocation
* in right part.
*/
private fun entryMethodPath(state: ExecutionState): MutableList<Step> {
val entryPath = mutableListOf<Step>()
state.fullPath().forEach { step ->
// TODO: replace step.stmt in methodUnderAnalysisStmts with step.depth == 0
// when fix SAT-812: [JAVA] Wrong depth when exception thrown
if (step.stmt in methodUnderAnalysisStmts && step.stmt !== entryPath.lastOrNull()?.stmt) {
entryPath += step
}
}
return entryPath
}
}
private fun ResolvedModels.constructStateForMethod(methodUnderTest: ExecutableId): EnvironmentModels {
val (thisInstanceBefore, paramsBefore) = when {
methodUnderTest.isStatic -> null to parameters
methodUnderTest.isConstructor -> null to parameters.drop(1)
else -> parameters.first() to parameters.drop(1)
}
return EnvironmentModels(thisInstanceBefore, paramsBefore, statics)
}
private suspend fun ConcreteExecutor<UtConcreteExecutionResult, UtExecutionInstrumentation>.executeConcretely(
methodUnderTest: ExecutableId,
stateBefore: EnvironmentModels,
instrumentation: List<UtInstrumentation>,
timeoutInMillis: Long
): UtConcreteExecutionResult = executeAsync(
methodUnderTest.classId.name,
methodUnderTest.signature,
arrayOf(),
parameters = UtConcreteExecutionData(
stateBefore,
instrumentation,
timeoutInMillis
)
).convertToAssemble(methodUnderTest.classId.packageName)
/**
* Before pushing our states for concrete execution, we have to be sure that every state is consistent.
* For now state could be inconsistent in case MUT parameters are wrappers that are not fully visited.
* For example, not fully visited map can contain duplicate keys that leads to incorrect behaviour.
* To prevent it, we need to add visited constraint for each MUT parameter-wrapper in state.
*/
private fun ExecutionState.withWrapperConsistencyChecks(): ExecutionState {
val visitedConstraints = mutableSetOf<UtBoolExpression>()
val methodUnderTestWrapperParameters = methodUnderTestParameters.filterNot { it.asWrapperOrNull == null }
val methodUnderTestWrapperParametersAddresses = methodUnderTestWrapperParameters.map { it.addr }.toSet()
if (methodUnderTestWrapperParameters.isEmpty()) {
return this
}
// make consistency checks for parameters-wrappers ...
methodUnderTestWrapperParameters.forEach { symbolicValue ->
symbolicValue.asWrapperOrNull?.let {
makeWrapperConsistencyCheck(symbolicValue, memory, visitedConstraints)
}
}
// ... and all locals that depends on these parameters-wrappers
val localReferenceValues = localVariableMemory
.localValues
.filterIsInstance<ReferenceValue>()
.filter { it.addr.internal is UtArraySelectExpression }
localReferenceValues.forEach {
val theMostNestedAddr = findTheMostNestedAddr(it.addr.internal as UtArraySelectExpression)
if (theMostNestedAddr in methodUnderTestWrapperParametersAddresses) {
makeWrapperConsistencyCheck(it, memory, visitedConstraints)
}
}
return copy(symbolicState = symbolicState + visitedConstraints.asHardConstraint())
}
private fun makeWrapperConsistencyCheck(
symbolicValue: SymbolicValue,
memory: Memory,
visitedConstraints: MutableSet<UtBoolExpression>
) {
val visitedSelectExpression = memory.isVisited(symbolicValue.addr)
visitedConstraints += mkEq(visitedSelectExpression, mkInt(1))
}
private fun UtConcreteExecutionResult.violatesUtMockAssumption(): Boolean {
// We should compare FQNs instead of `if (... is UtMockAssumptionViolatedException)`
// because the exception from the `concreteExecutionResult` is loaded by user's ClassLoader,
// but the `UtMockAssumptionViolatedException` is loaded by the current ClassLoader,
// so we can't cast them to each other.
return result.exceptionOrNull()?.javaClass?.name == UtMockAssumptionViolatedException::class.java.name
}