|
| 1 | +import cpp |
| 2 | + |
| 3 | +/** |
| 4 | + * The configuration signature for the FeasiblePath module. |
| 5 | + * |
| 6 | + * Typically, the `Node` type should be a `ControlFlowNode`, but it can be overridden to enable |
| 7 | + * other kinds of graphs. |
| 8 | + */ |
| 9 | +signature module FeasiblePathConfigSig { |
| 10 | + /** The state type used to carry context through the CFG exploration. */ |
| 11 | + class State; |
| 12 | + |
| 13 | + /** The node type used to represent CFG nodes. Overridable. */ |
| 14 | + class Node { |
| 15 | + Node getASuccessor(); |
| 16 | + } |
| 17 | + |
| 18 | + predicate isStart(State state, Node node); |
| 19 | + |
| 20 | + predicate isExcludedPath(State state, Node node); |
| 21 | + |
| 22 | + predicate isEnd(State state, Node node); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * A module that finds whether a feasible path exists between two control flow nodes, and |
| 27 | + * additionally support configuration that should not be traversed. |
| 28 | + * |
| 29 | + * Also accepts a state parameter to allow a context to flow through the CFG. |
| 30 | + * |
| 31 | + * ## Usage |
| 32 | + * |
| 33 | + * Implement the module `ConfigSig`, with some context type: |
| 34 | + * |
| 35 | + * ```ql |
| 36 | + * module MyConfig implements FeasiblePathConfigSig { |
| 37 | + * predicate isStart(SomeContext state, ControlFlowNode node) { |
| 38 | + * node = state.someStartCondition() |
| 39 | + * } |
| 40 | + * |
| 41 | + * predicate isExcludedPath(SomeContext state, ControlFlowNode node) { |
| 42 | + * node = state.someExcludedPathCondition() |
| 43 | + * } |
| 44 | + * |
| 45 | + * predicate isEnd(SomeContext state, ControlFlowNode node) { |
| 46 | + * node = state.someEndCondition() |
| 47 | + * } |
| 48 | + * } |
| 49 | + * |
| 50 | + * import FeasiblePath<MyConfig> as MyFeasiblePath |
| 51 | + * ``` |
| 52 | + * |
| 53 | + * ## Rationale |
| 54 | + * |
| 55 | + * Why does this module exist? While it may be tempting to write: |
| 56 | + * |
| 57 | + * ```ql |
| 58 | + * exists(ControlFlowNode start, ControlFlowNode end) { |
| 59 | + * isStart(start) and |
| 60 | + * isEnd(end) and |
| 61 | + * end = start.getASuccessor*() and |
| 62 | + * not exists(ControlFlowNode mid | |
| 63 | + * mid = start.getASuccessor+() and |
| 64 | + * end = mid.getASuccessor*() and |
| 65 | + * isExcludedPath(mid) |
| 66 | + * ) |
| 67 | + * } |
| 68 | + * ``` |
| 69 | + * |
| 70 | + * This has an unintuitive trap case in looping CFGs: |
| 71 | + * |
| 72 | + * ```c |
| 73 | + * while (cond) { |
| 74 | + * start(); |
| 75 | + * end(); |
| 76 | + * excluded(); |
| 77 | + * } |
| 78 | + * ``` |
| 79 | + * |
| 80 | + * In the above code, `excluded()` is a successor of `start()`, and `end()` is also a successor of |
| 81 | + * `excluded()` (via the loop back edge). However, there is no path from `start()` to `end()` that |
| 82 | + * does not pass through `excluded()`. |
| 83 | + * |
| 84 | + * This module will correctly handle this case. Forward exploration through the graph will stop |
| 85 | + * at the `excluded()` nodes, such that only paths from `start()` to `end()` that do not pass |
| 86 | + * through `excluded()` nodes will be found. |
| 87 | + */ |
| 88 | +module FeasiblePath<FeasiblePathConfigSig Config> { |
| 89 | + predicate isSuccessor(Config::State state, Config::Node start, Config::Node end) { |
| 90 | + isMid(state, start, end) and |
| 91 | + Config::isEnd(state, end) |
| 92 | + } |
| 93 | + |
| 94 | + private predicate isMid(Config::State state, Config::Node start, Config::Node mid) { |
| 95 | + // TODO: Explore if forward-reverse pruning would be beneficial for performance here. |
| 96 | + Config::isStart(state, start) and |
| 97 | + ( |
| 98 | + mid = start |
| 99 | + or |
| 100 | + exists(Config::Node prevMid | |
| 101 | + isMid(state, start, prevMid) and |
| 102 | + mid = prevMid.getASuccessor() and |
| 103 | + not Config::isExcludedPath(state, mid) and |
| 104 | + not Config::isEnd(state, prevMid) |
| 105 | + ) |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
0 commit comments