|
| 1 | +import python |
| 2 | + |
| 3 | +/** Gets the comment on the line above `ast` */ |
| 4 | +Comment commentFor(AstNode ast) { |
| 5 | + exists(int line | line = ast.getLocation().getStartLine() - 1 | |
| 6 | + result |
| 7 | + .getLocation() |
| 8 | + .hasLocationInfo(ast.getLocation().getFile().getAbsolutePath(), line, _, line, _) |
| 9 | + ) |
| 10 | +} |
| 11 | + |
| 12 | +/** Gets the value from `tag:value` in the comment for `ast` */ |
| 13 | +string getAnnotation(AstNode ast, string tag) { |
| 14 | + exists(Comment comment, string match, string theRegex | |
| 15 | + theRegex = "([\\w]+):([\\w.]+)" and |
| 16 | + comment = commentFor(ast) and |
| 17 | + match = comment.getText().regexpFind(theRegex, _, _) and |
| 18 | + tag = match.regexpCapture(theRegex, 1) and |
| 19 | + result = match.regexpCapture(theRegex, 2) |
| 20 | + ) |
| 21 | +} |
| 22 | + |
| 23 | +/** Gets a callable annotated with `name:name` */ |
| 24 | +Function annotatedCallable(string name) { name = getAnnotation(result, "name") } |
| 25 | + |
| 26 | +/** Gets a call annotated with `calls:name` */ |
| 27 | +Call annotatedCall(string name) { name = getAnnotation(result, "calls") } |
| 28 | + |
| 29 | +predicate missingAnnotationForCallable(string name, Call call) { |
| 30 | + call = annotatedCall(name) and |
| 31 | + not exists(annotatedCallable(name)) |
| 32 | +} |
| 33 | + |
| 34 | +predicate nonUniqueAnnotationForCallable(string name, Function callable) { |
| 35 | + strictcount(annotatedCallable(name)) > 1 and |
| 36 | + callable = annotatedCallable(name) |
| 37 | +} |
| 38 | + |
| 39 | +predicate missingAnnotationForCall(string name, Function callable) { |
| 40 | + not exists(annotatedCall(name)) and |
| 41 | + callable = annotatedCallable(name) |
| 42 | +} |
| 43 | + |
| 44 | +/** There is an obvious problem with the annotation `name` */ |
| 45 | +predicate nameInErrorState(string name) { |
| 46 | + missingAnnotationForCallable(name, _) |
| 47 | + or |
| 48 | + nonUniqueAnnotationForCallable(name, _) |
| 49 | + or |
| 50 | + missingAnnotationForCall(name, _) |
| 51 | +} |
| 52 | + |
| 53 | +/** Source code has annotation with `name` showing that `call` will call `callable` */ |
| 54 | +predicate annotatedCallEdge(string name, Call call, Function callable) { |
| 55 | + not nameInErrorState(name) and |
| 56 | + call = annotatedCall(name) and |
| 57 | + callable = annotatedCallable(name) |
| 58 | +} |
| 59 | + |
| 60 | +// ------------------------- Annotation debug query predicates ------------------------- |
| 61 | +query predicate debug_missingAnnotationForCallable(Call call, string message) { |
| 62 | + exists(string name | |
| 63 | + message = |
| 64 | + "This call is annotated with '" + name + |
| 65 | + "', but no callable with that annotation was extracted. Please fix." and |
| 66 | + missingAnnotationForCallable(name, call) |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +query predicate debug_nonUniqueAnnotationForCallable(Function callable, string message) { |
| 71 | + exists(string name | |
| 72 | + message = "Multiple callables are annotated with '" + name + "'. Please fix." and |
| 73 | + nonUniqueAnnotationForCallable(name, callable) |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +query predicate debug_missingAnnotationForCall(Function callable, string message) { |
| 78 | + exists(string name | |
| 79 | + message = |
| 80 | + "This callable is annotated with '" + name + |
| 81 | + "', but no call with that annotation was extracted. Please fix." and |
| 82 | + missingAnnotationForCall(name, callable) |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +// ------------------------- Call Graph resolution ------------------------- |
| 87 | +private newtype TCallGraphResolver = |
| 88 | + TPointsToResolver() or |
| 89 | + TTypeTrackerResolver() |
| 90 | + |
| 91 | +/** Describes a method of call graph resolution */ |
| 92 | +abstract class CallGraphResolver extends TCallGraphResolver { |
| 93 | + abstract predicate callEdge(Call call, Function callable); |
| 94 | + |
| 95 | + /** |
| 96 | + * Holds if annotations show that `call` will call `callable`, |
| 97 | + * but our call graph resolver was not able to figure that out |
| 98 | + */ |
| 99 | + predicate expectedCallEdgeNotFound(Call call, Function callable) { |
| 100 | + annotatedCallEdge(_, call, callable) and |
| 101 | + not this.callEdge(call, callable) |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Holds if there are no annotations that show that `call` will call `callable` (where at least one of these are annotated), |
| 106 | + * but the call graph resolver claims that `call` will call `callable` |
| 107 | + */ |
| 108 | + predicate unexpectedCallEdgeFound(Call call, Function callable, string message) { |
| 109 | + this.callEdge(call, callable) and |
| 110 | + not annotatedCallEdge(_, call, callable) and |
| 111 | + ( |
| 112 | + exists(string name | |
| 113 | + message = "Call resolved to the callable named '" + name + "' but was not annotated as such" and |
| 114 | + callable = annotatedCallable(name) and |
| 115 | + not nameInErrorState(name) |
| 116 | + ) |
| 117 | + or |
| 118 | + exists(string name | |
| 119 | + message = "Annotated call resolved to unannotated callable" and |
| 120 | + call = annotatedCall(name) and |
| 121 | + not nameInErrorState(name) and |
| 122 | + not exists( | callable = annotatedCallable(_)) |
| 123 | + ) |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + string toString() { result = "CallGraphResolver" } |
| 128 | +} |
| 129 | + |
| 130 | +/** A call graph resolver based on the existing points-to analysis */ |
| 131 | +class PointsToResolver extends CallGraphResolver, TPointsToResolver { |
| 132 | + override predicate callEdge(Call call, Function callable) { |
| 133 | + exists(PythonFunctionValue funcValue | |
| 134 | + funcValue.getScope() = callable and |
| 135 | + call = funcValue.getACall().getNode() |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + override string toString() { result = "PointsToResolver" } |
| 140 | +} |
| 141 | + |
| 142 | +/** A call graph resolved based on Type Trackers */ |
| 143 | +class TypeTrackerResolver extends CallGraphResolver, TTypeTrackerResolver { |
| 144 | + override predicate callEdge(Call call, Function callable) { none() } |
| 145 | + |
| 146 | + override string toString() { result = "TypeTrackerResolver" } |
| 147 | +} |
0 commit comments