|
| 1 | +package org.usvm.dataflow.ts.ifds |
| 2 | + |
| 3 | +import org.jacodb.ets.model.EtsStmt |
| 4 | +import org.usvm.dataflow.ifds.Edge |
| 5 | +import org.usvm.dataflow.ifds.Vertex |
| 6 | +import org.usvm.dataflow.ts.ifds.EtsIfdsMethodRunner.PathEdge |
| 7 | +import org.usvm.dataflow.ts.util.EtsTraits |
| 8 | + |
| 9 | +internal class EtsIfdsSourceRunner<Fact>( |
| 10 | + val traits: EtsTraits, |
| 11 | + val methodRunner: EtsIfdsMethodRunner<Fact, *>, |
| 12 | + val entrypoint: EtsStmt, |
| 13 | + val startingFact: Fact, |
| 14 | +) { |
| 15 | + private var enqueued: Boolean = false |
| 16 | + private val pathEdgeQueue: ArrayDeque<PathEdge<Fact>> = ArrayDeque() |
| 17 | + |
| 18 | + fun processFacts() { |
| 19 | + while (!pathEdgeQueue.isEmpty()) { |
| 20 | + val currentEdge = pathEdgeQueue.removeFirst() |
| 21 | + tabulationAlgorithmStep(currentEdge) |
| 22 | + } |
| 23 | + enqueued = false |
| 24 | + } |
| 25 | + |
| 26 | + private val successors = methodRunner.successors |
| 27 | + private val stmts = methodRunner.stmts |
| 28 | + private val mockStmt = methodRunner.mockStmt |
| 29 | + private val flowSpace = methodRunner.flowSpace |
| 30 | + val graph = methodRunner.graph |
| 31 | + |
| 32 | + private val factsAtStmt = Array(stmts.size) { hashSetOf<Fact>() } |
| 33 | + |
| 34 | + internal fun propagate(localEdge: PathEdge<Fact>) { |
| 35 | + val (endStmtIndex, endFact) = localEdge |
| 36 | + if (factsAtStmt[endStmtIndex].add(endFact)) { |
| 37 | + val startVertex = Vertex(mockStmt, startingFact) |
| 38 | + val endVertex = Vertex(stmts[endStmtIndex], endFact) |
| 39 | + val edge = Edge(startVertex, endVertex) |
| 40 | + val events = methodRunner.analyzer.handleNewEdge(edge) |
| 41 | + |
| 42 | + for (event in events) { |
| 43 | + methodRunner.manager.handleEvent(event) |
| 44 | + } |
| 45 | + |
| 46 | + pathEdgeQueue.add(localEdge) |
| 47 | + if (!enqueued) { |
| 48 | + enqueued = true |
| 49 | + methodRunner.enqueue(this) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private val callerPathEdges = hashSetOf<Edge<Fact, EtsStmt>>() |
| 55 | + internal val summaryEdges = hashSetOf<Vertex<Fact, EtsStmt>>() |
| 56 | + |
| 57 | + private fun tabulationAlgorithmStep( |
| 58 | + currentEdge: PathEdge<Fact>, |
| 59 | + ) = with(traits) { |
| 60 | + val (currentStmtIndex, currentFact) = currentEdge |
| 61 | + val currentStmt = stmts[currentStmtIndex] |
| 62 | + |
| 63 | + val currentIsCall = getCallExpr(currentStmt) != null |
| 64 | + val currentIsExit = methodRunner.isExit[currentStmtIndex] |
| 65 | + |
| 66 | + if (currentIsCall) { |
| 67 | + val callToReturnFlowFunction = flowSpace |
| 68 | + .obtainCallToReturnSiteFlowFunction(currentStmt, mockStmt) |
| 69 | + |
| 70 | + // Propagate through the call-to-return-site edge: |
| 71 | + val factsAtReturnSite = callToReturnFlowFunction.compute(currentFact) |
| 72 | + for (returnSite in successors[currentStmtIndex]) { |
| 73 | + for (returnSiteFact in factsAtReturnSite) { |
| 74 | + val edge = PathEdge(returnSite, returnSiteFact) |
| 75 | + propagate(edge) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + for (callee in graph.callees(currentStmt)) { |
| 80 | + for (calleeStart in graph.entryPoints(callee)) { |
| 81 | + val callToStartFlowFunction = flowSpace.obtainCallToStartFlowFunction(currentStmt, calleeStart) |
| 82 | + val factsAtCalleeStart = callToStartFlowFunction.compute(currentFact) |
| 83 | + |
| 84 | + for (calleeStartFact in factsAtCalleeStart) { |
| 85 | + val calleeStartVertex = Vertex(calleeStart, calleeStartFact) |
| 86 | + |
| 87 | + // Save info about the call for summary edges that will be found later: |
| 88 | + val calleeRunner = methodRunner.commonRunner.getMethodRunner(callee) |
| 89 | + val currentVertex = Vertex(currentStmt, currentFact) |
| 90 | + val startingVertex = Vertex(entrypoint, startingFact) |
| 91 | + val callerEdge = Edge(startingVertex, currentVertex) |
| 92 | + for (calleeSourceRunner in calleeRunner.getSourceRunners(startingFact)) { |
| 93 | + calleeSourceRunner.callerPathEdges.add(callerEdge) |
| 94 | + } |
| 95 | + |
| 96 | + // Initialize analysis of callee: |
| 97 | + val summaryEdges = calleeRunner.propagateStartingFact(calleeStartFact) |
| 98 | + |
| 99 | + // Handle already-found summary edges: |
| 100 | + for (exitVertex in summaryEdges) { |
| 101 | + val summaryEdge = Edge(calleeStartVertex, exitVertex) |
| 102 | + methodRunner.handleSummaryEdgeInCaller(callerEdge, summaryEdge) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } else { |
| 108 | + if (currentIsExit) { |
| 109 | + val startVertex = Vertex(entrypoint, startingFact) |
| 110 | + val currentVertex = Vertex(currentStmt, currentFact) |
| 111 | + |
| 112 | + // Propagate through the summary edge: |
| 113 | + for (callerPathEdge in callerPathEdges) { |
| 114 | + val summaryEdge = Edge(startVertex, currentVertex) |
| 115 | + val caller = callerPathEdge.from.statement.method |
| 116 | + val callerRunner = methodRunner.commonRunner.getMethodRunner(caller) |
| 117 | + callerRunner.handleSummaryEdgeInCaller(currentEdge = callerPathEdge, summaryEdge = summaryEdge) |
| 118 | + } |
| 119 | + |
| 120 | + // Add new summary edge: |
| 121 | + summaryEdges.add(currentVertex) |
| 122 | + } |
| 123 | + |
| 124 | + // Simple (sequential) propagation to the next instruction: |
| 125 | + val factsAtNext = flowSpace |
| 126 | + .obtainSequentFlowFunction(currentStmt, mockStmt) |
| 127 | + .compute(currentFact) |
| 128 | + for (next in successors[currentStmtIndex]) { |
| 129 | + for (nextFact in factsAtNext) { |
| 130 | + val edge = PathEdge(next, nextFact) |
| 131 | + propagate(edge) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + internal fun getPathEdges(): List<Edge<Fact, EtsStmt>> { |
| 138 | + val startVertex = Vertex(entrypoint, startingFact) |
| 139 | + return factsAtStmt.flatMapIndexed { index, facts -> |
| 140 | + val stmt = stmts[index] |
| 141 | + facts.map { |
| 142 | + val endVertex = Vertex(stmt, it) |
| 143 | + Edge(startVertex, endVertex) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments