|
| 1 | +package org.usvm.api.checkers |
| 2 | + |
| 3 | +import org.jacodb.api.JcType |
| 4 | +import org.jacodb.api.cfg.JcAssignInst |
| 5 | +import org.jacodb.api.cfg.JcCallExpr |
| 6 | +import org.jacodb.api.cfg.JcCallInst |
| 7 | +import org.jacodb.api.cfg.JcCatchInst |
| 8 | +import org.jacodb.api.cfg.JcEnterMonitorInst |
| 9 | +import org.jacodb.api.cfg.JcExitMonitorInst |
| 10 | +import org.jacodb.api.cfg.JcGotoInst |
| 11 | +import org.jacodb.api.cfg.JcIfInst |
| 12 | +import org.jacodb.api.cfg.JcInstVisitor |
| 13 | +import org.jacodb.api.cfg.JcReturnInst |
| 14 | +import org.jacodb.api.cfg.JcSwitchInst |
| 15 | +import org.jacodb.api.cfg.JcThrowInst |
| 16 | +import org.jacodb.api.cfg.JcValue |
| 17 | +import org.usvm.UBoolExpr |
| 18 | +import org.usvm.UExpr |
| 19 | +import org.usvm.machine.JcContext |
| 20 | +import org.usvm.machine.JcInterpreterObserver |
| 21 | +import org.usvm.machine.JcMethodCallBaseInst |
| 22 | +import org.usvm.machine.JcMethodEntrypointInst |
| 23 | +import org.usvm.machine.interpreter.JcSimpleValueResolver |
| 24 | +import org.usvm.machine.interpreter.JcStepScope |
| 25 | +import org.usvm.memory.UReadOnlyMemory |
| 26 | + |
| 27 | +internal class JcCheckerApiImpl : JcCheckerApi { |
| 28 | + // TODO How to retrieve it properly? |
| 29 | + private var internalStepScope: JcStepScope? = null |
| 30 | + private var internalExprResolver: JcSimpleValueResolver? = null |
| 31 | + |
| 32 | + private val stepScope: JcStepScope |
| 33 | + get() = ensureProcessing(internalStepScope) |
| 34 | + |
| 35 | + private val exprResolver: JcSimpleValueResolver |
| 36 | + get() = ensureProcessing(internalExprResolver) |
| 37 | + |
| 38 | + private fun <T> ensureProcessing(e: T?): T = |
| 39 | + e ?: error("Checker API can be used during instruction processing only") |
| 40 | + |
| 41 | + override val ctx: JcContext |
| 42 | + get() = stepScope.calcOnState { ctx } |
| 43 | + |
| 44 | + override val memory: UReadOnlyMemory<JcType> |
| 45 | + get() = stepScope.calcOnState { memory } |
| 46 | + |
| 47 | + override fun resolveValue(value: JcValue): UExpr<*> = value.accept(exprResolver) |
| 48 | + |
| 49 | + override fun checkSat(condition: UBoolExpr): JcCheckerResult = |
| 50 | + stepScope.checkSat(condition)?.let { |
| 51 | + JcCheckerSatResultImpl |
| 52 | + } ?: JcCheckerUnsatResultImpl |
| 53 | + |
| 54 | + internal fun setResolverAndScope(simpleValueResolver: JcSimpleValueResolver, stepScope: JcStepScope) { |
| 55 | + internalExprResolver = simpleValueResolver |
| 56 | + internalStepScope = stepScope |
| 57 | + } |
| 58 | + |
| 59 | + internal fun resetResolverAndScope() { |
| 60 | + internalStepScope = null |
| 61 | + internalExprResolver = null |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +internal object JcCheckerSatResultImpl : JcCheckerSatResult |
| 66 | +internal object JcCheckerUnsatResultImpl : JcCheckerUnsatResult |
| 67 | +internal object JcCheckerUnknownSatResultImpl : JcCheckerUnknownResult |
| 68 | + |
| 69 | +internal class JcCheckerObserver<T>( |
| 70 | + private val visitor: JcInstVisitor<T>, |
| 71 | + private val jcCheckerApi: JcCheckerApiImpl, |
| 72 | +) : JcInterpreterObserver { |
| 73 | + override fun onAssignStatement( |
| 74 | + simpleValueResolver: JcSimpleValueResolver, |
| 75 | + stmt: JcAssignInst, |
| 76 | + stepScope: JcStepScope, |
| 77 | + ) { |
| 78 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcAssignInst(stmt) } |
| 79 | + } |
| 80 | + |
| 81 | + override fun onEntryPoint( |
| 82 | + simpleValueResolver: JcSimpleValueResolver, |
| 83 | + stmt: JcMethodEntrypointInst, |
| 84 | + stepScope: JcStepScope, |
| 85 | + ) { |
| 86 | + withScopeAndResolver(simpleValueResolver, stepScope) { |
| 87 | + // Looks like we do not need this signal in the ast-based checker |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override fun onMethodCallWithUnresolvedArguments( |
| 92 | + simpleValueResolver: JcSimpleValueResolver, |
| 93 | + stmt: JcCallExpr, |
| 94 | + stepScope: JcStepScope, |
| 95 | + ) { |
| 96 | + withScopeAndResolver(simpleValueResolver, stepScope) { |
| 97 | + // Looks like we do not need this signal in the ast-based checker |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + override fun onMethodCallWithResolvedArguments( |
| 102 | + simpleValueResolver: JcSimpleValueResolver, |
| 103 | + stmt: JcMethodCallBaseInst, |
| 104 | + stepScope: JcStepScope, |
| 105 | + ) { |
| 106 | + withScopeAndResolver(simpleValueResolver, stepScope) { |
| 107 | + // Looks like we do not need this signal in the ast-based checker |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + override fun onIfStatement(simpleValueResolver: JcSimpleValueResolver, stmt: JcIfInst, stepScope: JcStepScope) { |
| 112 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcIfInst(stmt) } |
| 113 | + } |
| 114 | + |
| 115 | + override fun onReturnStatement( |
| 116 | + simpleValueResolver: JcSimpleValueResolver, |
| 117 | + stmt: JcReturnInst, |
| 118 | + stepScope: JcStepScope, |
| 119 | + ) { |
| 120 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcReturnInst(stmt) } |
| 121 | + } |
| 122 | + |
| 123 | + override fun onGotoStatement(simpleValueResolver: JcSimpleValueResolver, stmt: JcGotoInst, stepScope: JcStepScope) { |
| 124 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcGotoInst(stmt) } |
| 125 | + } |
| 126 | + |
| 127 | + override fun onCatchStatement( |
| 128 | + simpleValueResolver: JcSimpleValueResolver, |
| 129 | + stmt: JcCatchInst, |
| 130 | + stepScope: JcStepScope |
| 131 | + ) { |
| 132 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcCatchInst(stmt) } |
| 133 | + } |
| 134 | + |
| 135 | + override fun onSwitchStatement( |
| 136 | + simpleValueResolver: JcSimpleValueResolver, |
| 137 | + stmt: JcSwitchInst, |
| 138 | + stepScope: JcStepScope, |
| 139 | + ) { |
| 140 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcSwitchInst(stmt) } |
| 141 | + } |
| 142 | + |
| 143 | + override fun onThrowStatement( |
| 144 | + simpleValueResolver: JcSimpleValueResolver, |
| 145 | + stmt: JcThrowInst, |
| 146 | + stepScope: JcStepScope, |
| 147 | + ) { |
| 148 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcThrowInst(stmt) } |
| 149 | + } |
| 150 | + |
| 151 | + override fun onCallStatement(simpleValueResolver: JcSimpleValueResolver, stmt: JcCallInst, stepScope: JcStepScope) { |
| 152 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcCallInst(stmt) } |
| 153 | + } |
| 154 | + |
| 155 | + override fun onEnterMonitorStatement( |
| 156 | + simpleValueResolver: JcSimpleValueResolver, |
| 157 | + stmt: JcEnterMonitorInst, |
| 158 | + stepScope: JcStepScope, |
| 159 | + ) { |
| 160 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcEnterMonitorInst(stmt) } |
| 161 | + } |
| 162 | + |
| 163 | + override fun onExitMonitorStatement( |
| 164 | + simpleValueResolver: JcSimpleValueResolver, |
| 165 | + stmt: JcExitMonitorInst, |
| 166 | + stepScope: JcStepScope, |
| 167 | + ) { |
| 168 | + withScopeAndResolver(simpleValueResolver, stepScope) { visitor.visitJcExitMonitorInst(stmt) } |
| 169 | + } |
| 170 | + |
| 171 | + // TODO it's very dirty way to get required fields, rewrite |
| 172 | + private inline fun withScopeAndResolver( |
| 173 | + simpleValueResolver: JcSimpleValueResolver, |
| 174 | + stepScope: JcStepScope, |
| 175 | + blockOnStmt: () -> Unit |
| 176 | + ) = try { |
| 177 | + jcCheckerApi.setResolverAndScope(simpleValueResolver, stepScope) |
| 178 | + |
| 179 | + blockOnStmt() |
| 180 | + } finally { |
| 181 | + jcCheckerApi.resetResolverAndScope() |
| 182 | + } |
| 183 | +} |
0 commit comments