|
| 1 | +package org.usvm.machine.expr |
| 2 | + |
| 3 | +import mu.KotlinLogging |
| 4 | +import org.jacodb.ets.model.EtsClassType |
| 5 | +import org.jacodb.ets.model.EtsMethod |
| 6 | +import org.jacodb.ets.model.EtsMethodSignature |
| 7 | +import org.jacodb.ets.model.EtsStaticCallExpr |
| 8 | +import org.jacodb.ets.utils.UNKNOWN_CLASS_NAME |
| 9 | +import org.usvm.UExpr |
| 10 | +import org.usvm.machine.Constants |
| 11 | +import org.usvm.machine.TsConcreteMethodCallStmt |
| 12 | +import org.usvm.machine.expr.TsExprApproximationResult.NoApproximation |
| 13 | +import org.usvm.machine.expr.TsExprApproximationResult.ResolveFailure |
| 14 | +import org.usvm.machine.expr.TsExprApproximationResult.SuccessfulApproximation |
| 15 | +import org.usvm.machine.state.TsMethodResult |
| 16 | +import org.usvm.machine.state.lastStmt |
| 17 | +import org.usvm.machine.state.newStmt |
| 18 | +import org.usvm.util.TsResolutionResult |
| 19 | + |
| 20 | +private val logger = KotlinLogging.logger {} |
| 21 | + |
| 22 | +internal fun TsExprResolver.handleStaticCall( |
| 23 | + expr: EtsStaticCallExpr, |
| 24 | +): UExpr<*>? = with(ctx) { |
| 25 | + // Check if the method was already called and returned a value. |
| 26 | + when (val result = scope.calcOnState { methodResult }) { |
| 27 | + is TsMethodResult.Success -> { |
| 28 | + scope.doWithState { methodResult = TsMethodResult.NoCall } |
| 29 | + return result.value |
| 30 | + } |
| 31 | + |
| 32 | + is TsMethodResult.TsException -> { |
| 33 | + error("Exception should be handled earlier") |
| 34 | + } |
| 35 | + |
| 36 | + is TsMethodResult.NoCall -> {} // proceed to call |
| 37 | + } |
| 38 | + |
| 39 | + // Try to approximate the call. |
| 40 | + when (val result = tryApproximateStaticCall(expr)) { |
| 41 | + is SuccessfulApproximation -> return result.expr |
| 42 | + is ResolveFailure -> return null |
| 43 | + is NoApproximation -> {} |
| 44 | + } |
| 45 | + |
| 46 | + // Resolve the static method. |
| 47 | + when (val resolved = resolveStaticMethod(expr.callee)) { |
| 48 | + is TsResolutionResult.Empty -> { |
| 49 | + logger.error { "Could not resolve static call: ${expr.callee}" } |
| 50 | + scope.assert(falseExpr) ?: return null |
| 51 | + } |
| 52 | + |
| 53 | + is TsResolutionResult.Ambiguous -> { |
| 54 | + processAmbiguousStaticMethod(resolved, expr) |
| 55 | + } |
| 56 | + |
| 57 | + is TsResolutionResult.Unique -> { |
| 58 | + processUniqueStaticMethod(resolved, expr) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Return null to indicate that we are awaiting the call to be executed. |
| 63 | + null |
| 64 | +} |
| 65 | + |
| 66 | +private fun TsExprResolver.resolveStaticMethod( |
| 67 | + method: EtsMethodSignature, |
| 68 | +): TsResolutionResult<EtsMethod> { |
| 69 | + // Perfect signature: |
| 70 | + if (method.enclosingClass.name != UNKNOWN_CLASS_NAME) { |
| 71 | + val classes = hierarchy.classesForType(EtsClassType(method.enclosingClass)) |
| 72 | + if (classes.size > 1) { |
| 73 | + val methods = classes.map { it.methods.single { it.name == method.name } } |
| 74 | + return TsResolutionResult.create(methods) |
| 75 | + } |
| 76 | + |
| 77 | + if (classes.isEmpty()) return TsResolutionResult.Empty |
| 78 | + |
| 79 | + val clazz = classes.single() |
| 80 | + val methods = clazz.methods.filter { it.name == method.name } |
| 81 | + return TsResolutionResult.create(methods) |
| 82 | + } |
| 83 | + |
| 84 | + // Unknown signature: |
| 85 | + val methods = ctx.scene.projectAndSdkClasses |
| 86 | + .flatMap { it.methods } |
| 87 | + .filter { it.name == method.name } |
| 88 | + |
| 89 | + return TsResolutionResult.create(methods) |
| 90 | +} |
| 91 | + |
| 92 | +private fun TsExprResolver.processAmbiguousStaticMethod( |
| 93 | + resolved: TsResolutionResult.Ambiguous<EtsMethod>, |
| 94 | + expr: EtsStaticCallExpr, |
| 95 | +) { |
| 96 | + val args = expr.args.map { resolve(it) ?: return } |
| 97 | + val staticProperties = resolved.properties.take(Constants.STATIC_METHODS_FORK_LIMIT) |
| 98 | + val staticInstances = scope.calcOnState { |
| 99 | + staticProperties.map { getStaticInstance(it.enclosingClass!!) } |
| 100 | + } |
| 101 | + val concreteCalls = staticProperties.mapIndexed { index, value -> |
| 102 | + TsConcreteMethodCallStmt( |
| 103 | + callee = value, |
| 104 | + instance = staticInstances[index], |
| 105 | + args = args, |
| 106 | + returnSite = scope.calcOnState { lastStmt } |
| 107 | + ) |
| 108 | + } |
| 109 | + scope.forkMulti( |
| 110 | + concreteCalls.map { stmt -> |
| 111 | + ctx.mkTrue() to { newStmt(stmt) } |
| 112 | + } |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +private fun TsExprResolver.processUniqueStaticMethod( |
| 117 | + resolved: TsResolutionResult.Unique<EtsMethod>, |
| 118 | + expr: EtsStaticCallExpr, |
| 119 | +) { |
| 120 | + val instance = scope.calcOnState { |
| 121 | + getStaticInstance(resolved.property.enclosingClass!!) |
| 122 | + } |
| 123 | + val args = expr.args.map { resolve(it) ?: return } |
| 124 | + val concreteCall = TsConcreteMethodCallStmt( |
| 125 | + callee = resolved.property, |
| 126 | + instance = instance, |
| 127 | + args = args, |
| 128 | + returnSite = scope.calcOnState { lastStmt }, |
| 129 | + ) |
| 130 | + scope.doWithState { newStmt(concreteCall) } |
| 131 | +} |
0 commit comments