|
| 1 | +/** |
| 2 | + * C++-specific implementation of range analysis. |
| 3 | + */ |
| 4 | + |
| 5 | +private import experimental.semmle.code.cpp.semantic.Semantic |
| 6 | +private import RangeAnalysisStage |
| 7 | +private import experimental.semmle.code.cpp.semantic.analysis.FloatDelta |
| 8 | +private import experimental.semmle.code.cpp.semantic.analysis.IntDelta |
| 9 | +private import RangeAnalysisImpl |
| 10 | +private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils |
| 11 | + |
| 12 | +module CppLangImpl2 implements LangSig<FloatDelta> { |
| 13 | + /** |
| 14 | + * Holds if the specified expression should be excluded from the result of `ssaRead()`. |
| 15 | + * |
| 16 | + * This predicate is to keep the results identical to the original Java implementation. It should be |
| 17 | + * removed once we have the new implementation matching the old results exactly. |
| 18 | + */ |
| 19 | + predicate ignoreSsaReadCopy(SemExpr e) { none() } |
| 20 | + |
| 21 | + /** |
| 22 | + * Ignore the bound on this expression. |
| 23 | + * |
| 24 | + * This predicate is to keep the results identical to the original Java implementation. It should be |
| 25 | + * removed once we have the new implementation matching the old results exactly. |
| 26 | + */ |
| 27 | + predicate ignoreExprBound(SemExpr e) { |
| 28 | + exists(boolean upper, float delta, ConstantBounds::SemZeroBound b, float lb, float ub | |
| 29 | + ConstantStage::semBounded(e, b, delta, upper, _) and |
| 30 | + typeBounds(e.getSemType(), lb, ub) and |
| 31 | + ( |
| 32 | + upper = false and |
| 33 | + delta < lb or |
| 34 | + upper = true and |
| 35 | + delta > ub |
| 36 | + ) |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +private predicate typeBounds(SemType t, float lb, float ub) { |
| 42 | + exists(SemIntegerType integralType, float limit | |
| 43 | + integralType = t and limit = 2.pow(8 * integralType.getByteSize()) |
| 44 | + | |
| 45 | + if integralType instanceof SemBooleanType |
| 46 | + then lb = 0 and ub = 1 |
| 47 | + else |
| 48 | + if integralType.isSigned() |
| 49 | + then ( |
| 50 | + lb = -(limit / 2) and ub = (limit / 2) - 1 |
| 51 | + ) else ( |
| 52 | + lb = 0 and ub = limit - 1 |
| 53 | + ) |
| 54 | + ) |
| 55 | + or |
| 56 | + // This covers all floating point types. The range is (-Inf, +Inf). |
| 57 | + t instanceof SemFloatingPointType and lb = -(1.0 / 0.0) and ub = 1.0 / 0.0 |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Ignore any inferred zero lower bound on this expression. |
| 63 | + * |
| 64 | + * This predicate is to keep the results identical to the original Java implementation. It should be |
| 65 | + * removed once we have the new implementation matching the old results exactly. |
| 66 | + */ |
| 67 | + predicate ignoreZeroLowerBound(SemExpr e) { none() } |
| 68 | + |
| 69 | + /** |
| 70 | + * Holds if the specified expression should be excluded from the result of `ssaRead()`. |
| 71 | + * |
| 72 | + * This predicate is to keep the results identical to the original Java implementation. It should be |
| 73 | + * removed once we have the new implementation matching the old results exactly. |
| 74 | + */ |
| 75 | + predicate ignoreSsaReadArithmeticExpr(SemExpr e) { none() } |
| 76 | + |
| 77 | + /** |
| 78 | + * Holds if the specified variable should be excluded from the result of `ssaRead()`. |
| 79 | + * |
| 80 | + * This predicate is to keep the results identical to the original Java implementation. It should be |
| 81 | + * removed once we have the new implementation matching the old results exactly. |
| 82 | + */ |
| 83 | + predicate ignoreSsaReadAssignment(SemSsaVariable v) { none() } |
| 84 | + |
| 85 | + /** |
| 86 | + * Adds additional results to `ssaRead()` that are specific to Java. |
| 87 | + * |
| 88 | + * This predicate handles propagation of offsets for post-increment and post-decrement expressions |
| 89 | + * in exactly the same way as the old Java implementation. Once the new implementation matches the |
| 90 | + * old one, we should remove this predicate and propagate deltas for all similar patterns, whether |
| 91 | + * or not they come from a post-increment/decrement expression. |
| 92 | + */ |
| 93 | + SemExpr specificSsaRead(SemSsaVariable v, float delta) { none() } |
| 94 | + |
| 95 | + /** |
| 96 | + * Holds if `e >= bound` (if `upper = false`) or `e <= bound` (if `upper = true`). |
| 97 | + */ |
| 98 | + predicate hasConstantBound(SemExpr e, float bound, boolean upper) { none() } |
| 99 | + |
| 100 | + /** |
| 101 | + * Holds if `e >= bound + delta` (if `upper = false`) or `e <= bound + delta` (if `upper = true`). |
| 102 | + */ |
| 103 | + predicate hasBound(SemExpr e, SemExpr bound, float delta, boolean upper) { none() } |
| 104 | + |
| 105 | + /** |
| 106 | + * Holds if the value of `dest` is known to be `src + delta`. |
| 107 | + */ |
| 108 | + predicate additionalValueFlowStep(SemExpr dest, SemExpr src, float delta) { none() } |
| 109 | + |
| 110 | + /** |
| 111 | + * Gets the type that range analysis should use to track the result of the specified expression, |
| 112 | + * if a type other than the original type of the expression is to be used. |
| 113 | + * |
| 114 | + * This predicate is commonly used in languages that support immutable "boxed" types that are |
| 115 | + * actually references but whose values can be tracked as the type contained in the box. |
| 116 | + */ |
| 117 | + SemType getAlternateType(SemExpr e) { none() } |
| 118 | + |
| 119 | + /** |
| 120 | + * Gets the type that range analysis should use to track the result of the specified source |
| 121 | + * variable, if a type other than the original type of the expression is to be used. |
| 122 | + * |
| 123 | + * This predicate is commonly used in languages that support immutable "boxed" types that are |
| 124 | + * actually references but whose values can be tracked as the type contained in the box. |
| 125 | + */ |
| 126 | + SemType getAlternateTypeForSsaVariable(SemSsaVariable var) { none() } |
| 127 | +} |
0 commit comments