|
| 1 | +/** |
| 2 | + * @id c/misra/unsequenced-side-effects |
| 3 | + * @name RULE-13-2: The value of an expression and its persistent side effects depend on its evaluation order |
| 4 | + * @description The value of an expression and its persistent side effects are depending on the |
| 5 | + * evaluation order resulting in unpredictable behavior. |
| 6 | + * @kind problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-13-2 |
| 10 | + * correctness |
| 11 | + * external/misra/obligation/required |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import codingstandards.c.misra |
| 16 | +import codingstandards.c.Expr |
| 17 | +import codingstandards.c.SideEffects |
| 18 | +import codingstandards.c.Ordering |
| 19 | + |
| 20 | +class VariableEffectOrAccess extends Expr { |
| 21 | + VariableEffectOrAccess() { |
| 22 | + this instanceof VariableEffect or |
| 23 | + this instanceof VariableAccess |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +pragma[noinline] |
| 28 | +predicate partOfFullExpr(VariableEffectOrAccess e, FullExpr fe) { |
| 29 | + ( |
| 30 | + exists(VariableEffect ve | e = ve and ve.getAnAccess() = fe.getAChild+() and not ve.isPartial()) |
| 31 | + or |
| 32 | + e.(VariableAccess) = fe.getAChild+() |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +class ConstituentExprOrdering extends Ordering::Configuration { |
| 37 | + ConstituentExprOrdering() { this = "ConstituentExprOrdering" } |
| 38 | + |
| 39 | + override predicate isCandidate(Expr e1, Expr e2) { |
| 40 | + exists(FullExpr fe | |
| 41 | + partOfFullExpr(e1, fe) and |
| 42 | + partOfFullExpr(e2, fe) |
| 43 | + ) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +predicate sameFullExpr(FullExpr fe, VariableAccess va1, VariableAccess va2) { |
| 48 | + partOfFullExpr(va1, fe) and |
| 49 | + partOfFullExpr(va2, fe) and |
| 50 | + va1 != va2 and |
| 51 | + exists(Variable v1, Variable v2 | |
| 52 | + // Use `pragma[only_bind_into]` to prevent CP between variable accesses. |
| 53 | + va1.getTarget() = pragma[only_bind_into](v1) and va2.getTarget() = pragma[only_bind_into](v2) |
| 54 | + | |
| 55 | + v1.isVolatile() and v2.isVolatile() |
| 56 | + or |
| 57 | + not (v1.isVolatile() and v2.isVolatile()) and |
| 58 | + v1 = v2 |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +int getLeafCount(LeftRightOperation bop) { |
| 63 | + if |
| 64 | + not bop.getLeftOperand() instanceof BinaryOperation and |
| 65 | + not bop.getRightOperand() instanceof BinaryOperation |
| 66 | + then result = 2 |
| 67 | + else |
| 68 | + if |
| 69 | + bop.getLeftOperand() instanceof BinaryOperation and |
| 70 | + not bop.getRightOperand() instanceof BinaryOperation |
| 71 | + then result = 1 + getLeafCount(bop.getLeftOperand()) |
| 72 | + else |
| 73 | + if |
| 74 | + not bop.getLeftOperand() instanceof BinaryOperation and |
| 75 | + bop.getRightOperand() instanceof BinaryOperation |
| 76 | + then result = 1 + getLeafCount(bop.getRightOperand()) |
| 77 | + else result = getLeafCount(bop.getLeftOperand()) + getLeafCount(bop.getRightOperand()) |
| 78 | +} |
| 79 | + |
| 80 | +class LeftRightOperation extends Expr { |
| 81 | + LeftRightOperation() { |
| 82 | + this instanceof BinaryOperation or |
| 83 | + this instanceof AssignOperation or |
| 84 | + this instanceof AssignExpr |
| 85 | + } |
| 86 | + |
| 87 | + Expr getLeftOperand() { |
| 88 | + result = this.(BinaryOperation).getLeftOperand() |
| 89 | + or |
| 90 | + result = this.(AssignOperation).getLValue() |
| 91 | + or |
| 92 | + result = this.(AssignExpr).getLValue() |
| 93 | + } |
| 94 | + |
| 95 | + Expr getRightOperand() { |
| 96 | + result = this.(BinaryOperation).getRightOperand() |
| 97 | + or |
| 98 | + result = this.(AssignOperation).getRValue() |
| 99 | + or |
| 100 | + result = this.(AssignExpr).getRValue() |
| 101 | + } |
| 102 | + |
| 103 | + Expr getAnOperand() { |
| 104 | + result = getLeftOperand() or |
| 105 | + result = getRightOperand() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +int getOperandIndexIn(FullExpr fullExpr, Expr operand) { |
| 110 | + result = getOperandIndex(fullExpr, operand) |
| 111 | + or |
| 112 | + fullExpr.(Call).getArgument(result).getAChild*() = operand |
| 113 | +} |
| 114 | + |
| 115 | +int getOperandIndex(LeftRightOperation binop, Expr operand) { |
| 116 | + if operand = binop.getAnOperand() |
| 117 | + then |
| 118 | + operand = binop.getLeftOperand() and |
| 119 | + result = 0 |
| 120 | + or |
| 121 | + operand = binop.getRightOperand() and |
| 122 | + result = getLeafCount(binop.getLeftOperand()) + 1 |
| 123 | + or |
| 124 | + operand = binop.getRightOperand() and |
| 125 | + not binop.getLeftOperand() instanceof LeftRightOperation and |
| 126 | + result = 1 |
| 127 | + else ( |
| 128 | + // Child of left operand that is a binary operation. |
| 129 | + result = getOperandIndex(binop.getLeftOperand(), operand) |
| 130 | + or |
| 131 | + // Child of left operand that is not a binary operation. |
| 132 | + result = 0 and |
| 133 | + not binop.getLeftOperand() instanceof LeftRightOperation and |
| 134 | + binop.getLeftOperand().getAChild+() = operand |
| 135 | + or |
| 136 | + // Child of right operand and both left and right operands are binary operations. |
| 137 | + result = |
| 138 | + getLeafCount(binop.getLeftOperand()) + getOperandIndex(binop.getRightOperand(), operand) |
| 139 | + or |
| 140 | + // Child of right operand and left operand is not a binary operation. |
| 141 | + result = 1 + getOperandIndex(binop.getRightOperand(), operand) and |
| 142 | + not binop.getLeftOperand() instanceof LeftRightOperation |
| 143 | + or |
| 144 | + // Child of right operand that is not a binary operation and the left operand is a binary operation. |
| 145 | + result = getLeafCount(binop.getLeftOperand()) + 1 and |
| 146 | + binop.getRightOperand().getAChild+() = operand and |
| 147 | + not binop.getRightOperand() instanceof LeftRightOperation |
| 148 | + or |
| 149 | + // Child of right operand that is not a binary operation and the left operand is not a binary operation. |
| 150 | + result = 1 and |
| 151 | + not binop.getLeftOperand() instanceof LeftRightOperation and |
| 152 | + not binop.getRightOperand() instanceof LeftRightOperation and |
| 153 | + binop.getRightOperand().getAChild+() = operand |
| 154 | + ) |
| 155 | +} |
| 156 | + |
| 157 | +predicate inConditionalThen(ConditionalExpr ce, Expr e) { |
| 158 | + e = ce.getThen() |
| 159 | + or |
| 160 | + exists(Expr parent | |
| 161 | + inConditionalThen(ce, parent) and |
| 162 | + parent.getAChild() = e |
| 163 | + ) |
| 164 | +} |
| 165 | + |
| 166 | +predicate inConditionalElse(ConditionalExpr ce, Expr e) { |
| 167 | + e = ce.getElse() |
| 168 | + or |
| 169 | + exists(Expr parent | |
| 170 | + inConditionalElse(ce, parent) and |
| 171 | + parent.getAChild() = e |
| 172 | + ) |
| 173 | +} |
| 174 | + |
| 175 | +predicate isUnsequencedEffect( |
| 176 | + ConstituentExprOrdering orderingConfig, FullExpr fullExpr, VariableEffect variableEffect1, |
| 177 | + VariableAccess va1, VariableAccess va2, Locatable placeHolder, string label |
| 178 | +) { |
| 179 | + // The two access are scoped to the same full expression. |
| 180 | + sameFullExpr(fullExpr, va1, va2) and |
| 181 | + // We are only interested in effects that change an object, |
| 182 | + // i.e., exclude patterns suchs as `b->data[b->cursor++]` where `b` is considered modified and read or `foo.bar = 1` where `=` modifies to both `foo` and `bar`. |
| 183 | + not variableEffect1.isPartial() and |
| 184 | + variableEffect1.getAnAccess() = va1 and |
| 185 | + ( |
| 186 | + exists(VariableEffect variableEffect2 | |
| 187 | + not variableEffect2.isPartial() and |
| 188 | + variableEffect2.getAnAccess() = va2 and |
| 189 | + // If the effect is not local (happens in a different function) we use the call with the access as a proxy. |
| 190 | + ( |
| 191 | + va1.getEnclosingStmt() = variableEffect1.getEnclosingStmt() and |
| 192 | + va2.getEnclosingStmt() = variableEffect2.getEnclosingStmt() and |
| 193 | + orderingConfig.isUnsequenced(variableEffect1, variableEffect2) |
| 194 | + or |
| 195 | + va1.getEnclosingStmt() = variableEffect1.getEnclosingStmt() and |
| 196 | + not va2.getEnclosingStmt() = variableEffect2.getEnclosingStmt() and |
| 197 | + exists(Call call | |
| 198 | + call.getAnArgument() = va2 and call.getEnclosingStmt() = va1.getEnclosingStmt() |
| 199 | + | |
| 200 | + orderingConfig.isUnsequenced(variableEffect1, call) |
| 201 | + ) |
| 202 | + or |
| 203 | + not va1.getEnclosingStmt() = variableEffect1.getEnclosingStmt() and |
| 204 | + va2.getEnclosingStmt() = variableEffect2.getEnclosingStmt() and |
| 205 | + exists(Call call | |
| 206 | + call.getAnArgument() = va1 and call.getEnclosingStmt() = va2.getEnclosingStmt() |
| 207 | + | |
| 208 | + orderingConfig.isUnsequenced(call, variableEffect2) |
| 209 | + ) |
| 210 | + ) and |
| 211 | + // Break the symmetry of the ordering relation by requiring that the first expression is located before the second. |
| 212 | + // This builds upon the assumption that the expressions are part of the same full expression as specified in the ordering configuration. |
| 213 | + getOperandIndexIn(fullExpr, va1) < getOperandIndexIn(fullExpr, va2) and |
| 214 | + placeHolder = variableEffect2 and |
| 215 | + label = "side effect" |
| 216 | + ) |
| 217 | + or |
| 218 | + placeHolder = va2 and |
| 219 | + label = "read" and |
| 220 | + not exists(VariableEffect variableEffect2 | variableEffect1 != variableEffect2 | |
| 221 | + variableEffect2.getAnAccess() = va2 |
| 222 | + ) and |
| 223 | + ( |
| 224 | + va1.getEnclosingStmt() = variableEffect1.getEnclosingStmt() and |
| 225 | + orderingConfig.isUnsequenced(variableEffect1, va2) |
| 226 | + or |
| 227 | + not va1.getEnclosingStmt() = variableEffect1.getEnclosingStmt() and |
| 228 | + exists(Call call | |
| 229 | + call.getAnArgument() = va1 and call.getEnclosingStmt() = va2.getEnclosingStmt() |
| 230 | + | |
| 231 | + orderingConfig.isUnsequenced(call, va2) |
| 232 | + ) |
| 233 | + ) and |
| 234 | + // The read is not used to compute the effect on the variable. |
| 235 | + // E.g., exclude x = x + 1 |
| 236 | + not variableEffect1.getAChild+() = va2 |
| 237 | + ) and |
| 238 | + // Both are evaluated |
| 239 | + not exists(ConditionalExpr ce | inConditionalThen(ce, va1) and inConditionalElse(ce, va2)) |
| 240 | +} |
| 241 | + |
| 242 | +from |
| 243 | + ConstituentExprOrdering orderingConfig, FullExpr fullExpr, VariableEffect variableEffect1, |
| 244 | + VariableAccess va1, VariableAccess va2, Locatable placeHolder, string label |
| 245 | +where |
| 246 | + not isExcluded(fullExpr, SideEffects3Package::unsequencedSideEffectsQuery()) and |
| 247 | + isUnsequencedEffect(orderingConfig, fullExpr, variableEffect1, va1, va2, placeHolder, label) |
| 248 | +select fullExpr, "The expression contains unsequenced $@ to $@ and $@ to $@.", variableEffect1, |
| 249 | + "side effect", va1, va1.getTarget().getName(), placeHolder, label, va2, va2.getTarget().getName() |
0 commit comments