-
Notifications
You must be signed in to change notification settings - Fork 26
TS interpreter observer #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
60ca88b
Add observer implementation
CaelmBleidd c6cba2c
Add detector
CaelmBleidd 3fccf06
Fix tests
CaelmBleidd a18c572
Format
Lipen c66537e
Fix
Lipen 93bd9e0
Run on real project, filter out methods with empty cfg
Lipen 026d980
Fix tests
CaelmBleidd 6c80308
Tests update
CaelmBleidd 051c0ab
Additional tests
CaelmBleidd fa86cb9
Extract mock method
CaelmBleidd 3191cad
Simplify detector
CaelmBleidd fc89ae6
Style fixes
CaelmBleidd ca99356
Comma fixes
CaelmBleidd 558e0d9
Rebase fixes
CaelmBleidd b8e8cdf
Rebase fixes
CaelmBleidd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
usvm-ts/src/main/kotlin/org/usvm/api/checkers/UnreachableCodeDetector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package org.usvm.api.checkers | ||
|
|
||
| import org.jacodb.ets.model.EtsIfStmt | ||
| import org.jacodb.ets.model.EtsMethod | ||
| import org.jacodb.ets.model.EtsStmt | ||
| import org.usvm.machine.TsInterpreterObserver | ||
| import org.usvm.machine.expr.TsSimpleValueResolver | ||
| import org.usvm.machine.interpreter.TsStepScope | ||
| import org.usvm.machine.state.TsState | ||
| import org.usvm.statistics.UMachineObserver | ||
| import kotlin.collections.filter | ||
| import kotlin.collections.isNotEmpty | ||
|
|
||
| data class UncoveredIfSuccessors(val ifStmt: EtsIfStmt, val successors: Set<EtsStmt>) | ||
|
|
||
| class UnreachableCodeDetector : TsInterpreterObserver, UMachineObserver<TsState> { | ||
| private val uncoveredSuccessorsOfVisitedIfStmts = hashMapOf<EtsMethod, MutableMap<EtsIfStmt, MutableSet<EtsStmt>>>() | ||
| lateinit var result: Map<EtsMethod, List<UncoveredIfSuccessors>> | ||
|
|
||
| override fun onIfStatement(simpleValueResolver: TsSimpleValueResolver, stmt: EtsIfStmt, scope: TsStepScope) { | ||
| val ifStmts = uncoveredSuccessorsOfVisitedIfStmts.getOrPut(stmt.method) { mutableMapOf() } | ||
| // We've already added its successors in the map | ||
| if (stmt in ifStmts) return | ||
|
|
||
| val successors = stmt.method.cfg.successors(stmt) | ||
| ifStmts[stmt] = successors.toMutableSet() | ||
| } | ||
|
|
||
| override fun onState(parent: TsState, forks: Sequence<TsState>) { | ||
| val previousStatement = parent.pathNode.parent?.statement | ||
| if (previousStatement !is EtsIfStmt) return | ||
|
|
||
| val method = parent.currentStatement.method | ||
| val remainingUncoveredIfSuccessors = uncoveredSuccessorsOfVisitedIfStmts.getValue(method) | ||
| val remainingSuccessorsForCurrentIf = remainingUncoveredIfSuccessors[previousStatement] | ||
| ?: return // No uncovered successors for this if statement | ||
|
|
||
| remainingSuccessorsForCurrentIf -= parent.currentStatement | ||
| forks.forEach { remainingSuccessorsForCurrentIf -= it.currentStatement } | ||
| } | ||
|
|
||
| override fun onMachineStopped() { | ||
| result = uncoveredSuccessorsOfVisitedIfStmts | ||
| .mapNotNull { (method, uncoveredIfSuccessors) -> | ||
| uncoveredIfSuccessors | ||
| .filter { it.value.isNotEmpty() } | ||
| .takeIf { it.isNotEmpty() } | ||
| ?.let { ifSucc -> method to ifSucc.map { UncoveredIfSuccessors(it.key, it.value) } } | ||
| }.toMap() | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
usvm-ts/src/main/kotlin/org/usvm/machine/TsInterpreterObserver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.usvm.machine | ||
|
|
||
| import org.jacodb.ets.model.EtsAssignStmt | ||
| import org.jacodb.ets.model.EtsCallExpr | ||
| import org.jacodb.ets.model.EtsIfStmt | ||
| import org.jacodb.ets.model.EtsReturnStmt | ||
| import org.jacodb.ets.model.EtsThrowStmt | ||
| import org.usvm.UBoolExpr | ||
| import org.usvm.machine.expr.TsSimpleValueResolver | ||
| import org.usvm.machine.interpreter.TsStepScope | ||
| import org.usvm.statistics.UInterpreterObserver | ||
|
|
||
| @Suppress("unused") | ||
| interface TsInterpreterObserver : UInterpreterObserver { | ||
| fun onAssignStatement( | ||
| simpleValueResolver: TsSimpleValueResolver, | ||
| stmt: EtsAssignStmt, | ||
| scope: TsStepScope, | ||
| ) { | ||
| // default empty implementation | ||
| } | ||
|
|
||
| // TODO on entry point | ||
CaelmBleidd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fun onCallWithUnresolvedArguments( | ||
| simpleValueResolver: TsSimpleValueResolver, | ||
| stmt: EtsCallExpr, | ||
| scope: TsStepScope, | ||
| ) { | ||
| // default empty implementation | ||
| } | ||
|
|
||
| // TODO onCallWithResolvedArguments | ||
|
||
|
|
||
| fun onIfStatement( | ||
| simpleValueResolver: TsSimpleValueResolver, | ||
| stmt: EtsIfStmt, | ||
| scope: TsStepScope, | ||
| ) { | ||
| // default empty implementation | ||
| } | ||
|
|
||
| fun onIfStatementWithResolvedCondition( | ||
| simpleValueResolver: TsSimpleValueResolver, | ||
| stmt: EtsIfStmt, | ||
| condition: UBoolExpr, | ||
| scope: TsStepScope, | ||
| ) { | ||
| // default empty implementation | ||
| } | ||
|
|
||
| fun onReturnStatement( | ||
| simpleValueResolver: TsSimpleValueResolver, | ||
| stmt: EtsReturnStmt, | ||
| scope: TsStepScope, | ||
| ) { | ||
| // default empty implementation | ||
| } | ||
|
|
||
| fun onThrowStatement( | ||
| simpleValueResolver: TsSimpleValueResolver, | ||
| stmt: EtsThrowStmt, | ||
| scope: TsStepScope, | ||
| ) { | ||
| // default empty implementation | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package org.usvm.machine | ||
|
|
||
| data class TsOptions( | ||
| val interproceduralAnalysis: Boolean = true, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.