|
| 1 | +private import cpp |
| 2 | +private import trailofbits.itergator.Invalidations |
| 3 | + |
| 4 | +class Iterator extends Variable { |
| 5 | + Iterator() { |
| 6 | + this.getUnderlyingType().getName().matches("%iterator%") or |
| 7 | + // getType is inconsistent |
| 8 | + this.getAnAssignedValue() |
| 9 | + .(FunctionCall) |
| 10 | + .getTarget() |
| 11 | + .(MemberFunction) |
| 12 | + .getName() |
| 13 | + .regexpMatch("c?r?begin") or |
| 14 | + this.getAnAssignedValue() |
| 15 | + .(FunctionCall) |
| 16 | + .getTarget() |
| 17 | + .(MemberFunction) |
| 18 | + .getName() |
| 19 | + .regexpMatch("c?r?end") or |
| 20 | + this.getAnAssignedValue().(FunctionCall).getTarget().hasName("find") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// the location where a variable is being iterated over |
| 25 | +class Iterated extends VariableAccess { |
| 26 | + Iterator iterator; |
| 27 | + |
| 28 | + Iterated() { |
| 29 | + iterator.getAnAssignedValue().getChild(-1) = this and |
| 30 | + not this.getTarget().isCompilerGenerated() |
| 31 | + or |
| 32 | + // show the iterable assigned to __range in ranged based for loops |
| 33 | + iterator.getAnAssignedValue().getChild(-1).(VariableAccess).getTarget().isCompilerGenerated() and |
| 34 | + this = |
| 35 | + iterator.getAnAssignedValue().getChild(-1).(VariableAccess).getTarget().getAnAssignedValue() |
| 36 | + } |
| 37 | + |
| 38 | + Iterator iterator() { result = iterator } |
| 39 | +} |
| 40 | + |
| 41 | +// function call with utility predicates |
| 42 | +private class FunctionCallR extends FunctionCall { |
| 43 | + predicate containedBy(Stmt other) { |
| 44 | + other.getASuccessor*() = this and |
| 45 | + other.getAChild*() = this |
| 46 | + or |
| 47 | + // for destructors |
| 48 | + exists(Function f | f.getBlock() = other and this.getEnclosingFunction() = f) |
| 49 | + } |
| 50 | + |
| 51 | + predicate callsPotentialInvalidation() { |
| 52 | + this.getTarget().(PotentialInvalidation).invalidates(any(Iterated i)) |
| 53 | + } |
| 54 | + |
| 55 | + predicate callsPotentialInvalidation(Iterated i) { |
| 56 | + this.getTarget().(PotentialInvalidation).invalidates(i) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// a call to any function that could call a PotentialInvalidation |
| 61 | +class InvalidatorT extends FunctionCallR { |
| 62 | + InvalidatorT() { |
| 63 | + this.callsPotentialInvalidation() |
| 64 | + or |
| 65 | + exists(InvalidatorT i | i.containedBy(this.getTarget().getBlock())) |
| 66 | + } |
| 67 | + |
| 68 | + InvalidatorT child() { |
| 69 | + result = this.getTarget().getBlock().getAChild+().(InvalidatorT) |
| 70 | + or |
| 71 | + exists(DestructorCall d | |
| 72 | + d.getEnclosingFunction() = this.getTarget() and d.(InvalidatorT) = result |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + Iterated iterated_() { |
| 77 | + this.callsPotentialInvalidation(result) or |
| 78 | + result = this.child().iterated_() |
| 79 | + } |
| 80 | + |
| 81 | + InvalidatorT potentialInvalidation() { |
| 82 | + this.callsPotentialInvalidation(this.iterated_()) and result = this |
| 83 | + or |
| 84 | + result = this.child().potentialInvalidation() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// calls that actually perform the invalidation |
| 89 | +class Invalidation extends InvalidatorT { |
| 90 | + Invalidator invalidator; |
| 91 | + |
| 92 | + Invalidation() { |
| 93 | + this = invalidator.potentialInvalidation() and invalidator.iterated() = this.iterated_() |
| 94 | + } |
| 95 | + |
| 96 | + Invalidator invalidator() { result = invalidator } |
| 97 | +} |
| 98 | + |
| 99 | +// the top level invalidation calls (directly inside loop bodies) |
| 100 | +class Invalidator extends InvalidatorT { |
| 101 | + Iterated iterated; |
| 102 | + |
| 103 | + Invalidator() { |
| 104 | + iterated = this.iterated_() and |
| 105 | + this.containedBy(iterated.iterator().getParentScope()) |
| 106 | + } |
| 107 | + |
| 108 | + Iterated iterated() { result = iterated } |
| 109 | + |
| 110 | + Invalidation invalidation() { result = any(Invalidation i | i.invalidator() = this) } |
| 111 | +} |
0 commit comments