|
| 1 | +/** |
| 2 | + * @id c/cert/dependence-on-order-of-function-arguments-for-side-effects |
| 3 | + * @name EXP30-C: Do not depend on the order of evaluation of function call arguments for side effects |
| 4 | + * @description Depending on the order of evaluation for side effects in function call arguments can |
| 5 | + * result in unexpected program behavior. |
| 6 | + * @kind problem |
| 7 | + * @precision high |
| 8 | + * @problem.severity warning |
| 9 | + * @tags external/cert/id/exp30-c |
| 10 | + * correctness |
| 11 | + * external/cert/obligation/rule |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import codingstandards.c.cert |
| 16 | +import codingstandards.cpp.SideEffect |
| 17 | +import semmle.code.cpp.dataflow.DataFlow |
| 18 | +import semmle.code.cpp.dataflow.TaintTracking |
| 19 | +import semmle.code.cpp.valuenumbering.GlobalValueNumberingImpl |
| 20 | + |
| 21 | +/** Holds if the function's return value is derived from the `AliasParamter` p. */ |
| 22 | +predicate returnValueDependsOnAliasParameter(AliasParameter p) { |
| 23 | + exists(ReturnStmt ret | ret = p.getFunction().getBlock().getAStmt() | |
| 24 | + TaintTracking::localTaint(DataFlow::parameterNode(p), DataFlow::exprNode(ret.getExpr())) |
| 25 | + or |
| 26 | + exists(FieldAccess fa, VariableAccess va | fa.getQualifier() = va and va.getTarget() = p | |
| 27 | + TaintTracking::localTaint(DataFlow::exprNode(fa), DataFlow::exprNode(ret.getExpr())) |
| 28 | + ) |
| 29 | + or |
| 30 | + exists(FunctionCall call, VariableAccess va | call.getQualifier() = va and va.getTarget() = p | |
| 31 | + TaintTracking::localTaint(DataFlow::exprNode(call), DataFlow::exprNode(ret.getExpr())) |
| 32 | + ) |
| 33 | + or |
| 34 | + exists(VariableAccess va | va.getTarget() = p | ret.getAChild+() = va) |
| 35 | + ) |
| 36 | + or |
| 37 | + exists(FunctionCall call, ReturnStmt ret, int i, AliasParameter q | |
| 38 | + ret = p.getFunction().getBlock().getAStmt() and call.getEnclosingFunction() = p.getFunction() |
| 39 | + | |
| 40 | + DataFlow::localFlow(DataFlow::parameterNode(p), DataFlow::exprNode(call.getArgument(i))) and |
| 41 | + q = call.getTarget().getParameter(i) and |
| 42 | + returnValueDependsOnAliasParameter(q) and |
| 43 | + TaintTracking::localTaint(DataFlow::exprNode(call), DataFlow::exprNode(ret.getExpr())) |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/** Holds if the function `f`'s return value is derived from the global variable `v`. */ |
| 48 | +predicate returnValueDependsOnGlobalVariable(Function f, GlobalVariable v) { |
| 49 | + exists(ReturnStmt ret, VariableAccess va | |
| 50 | + ret = f.getBlock().getAStmt() and va.getTarget() = v and va.getEnclosingFunction() = f |
| 51 | + | |
| 52 | + TaintTracking::localTaint(DataFlow::exprNode(va), DataFlow::exprNode(ret.getExpr())) |
| 53 | + ) |
| 54 | + or |
| 55 | + exists(ReturnStmt ret, FunctionCall call | |
| 56 | + ret = f.getBlock().getAStmt() and |
| 57 | + call.getEnclosingFunction() = f and |
| 58 | + returnValueDependsOnGlobalVariable(call.getTarget(), v) and |
| 59 | + TaintTracking::localTaint(DataFlow::exprNode(call), DataFlow::exprNode(ret.getExpr())) |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +/** Holds if the member function `f`'s return value is derived from the member variable `v`. */ |
| 64 | +predicate returnValueDependsOnMemberVariable(MemberFunction f, MemberVariable v) { |
| 65 | + exists(ReturnStmt ret, VariableAccess va | |
| 66 | + ret = f.getBlock().getAStmt() and |
| 67 | + va.getTarget() = v and |
| 68 | + va.getEnclosingFunction() = f and |
| 69 | + v.getDeclaringType() = f.getDeclaringType() |
| 70 | + | |
| 71 | + TaintTracking::localTaint(DataFlow::exprNode(va), DataFlow::exprNode(ret.getExpr())) |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +from |
| 76 | + FunctionCall call, Function f1, Function f2, int i, int j, FunctionCall arg1, FunctionCall arg2, |
| 77 | + Variable v1, Variable v2 |
| 78 | +where |
| 79 | + not isExcluded(call, |
| 80 | + SideEffects1Package::dependenceOnOrderOfFunctionArgumentsForSideEffectsQuery()) and |
| 81 | + arg1 = call.getArgument(i) and |
| 82 | + arg2 = call.getArgument(j) and |
| 83 | + i < j and |
| 84 | + arg1.getTarget() = f1 and |
| 85 | + arg2.getTarget() = f2 and |
| 86 | + ( |
| 87 | + // Considering the shared states: |
| 88 | + // - pointer or reference arguments being used in both functions |
| 89 | + exists(AliasParameter p1, AliasParameter p2 | |
| 90 | + v1 = p1 and |
| 91 | + v2 = p2 and |
| 92 | + f1.getAParameter() = p1 and |
| 93 | + f2.getAParameter() = p2 and |
| 94 | + p1.isModified() and |
| 95 | + p2.isModified() and |
| 96 | + globalValueNumber(arg1.getArgument(p1.getIndex())) = |
| 97 | + globalValueNumber(arg2.getArgument(p2.getIndex())) and |
| 98 | + returnValueDependsOnAliasParameter(p1) and |
| 99 | + returnValueDependsOnAliasParameter(p2) |
| 100 | + ) |
| 101 | + or |
| 102 | + // - global variables being used in both functions |
| 103 | + exists(GlobalVariable v, VariableEffect ve1, VariableEffect ve2 | |
| 104 | + v1 = v and |
| 105 | + v2 = v and |
| 106 | + returnValueDependsOnGlobalVariable(f1, v) and |
| 107 | + returnValueDependsOnGlobalVariable(f2, v) and |
| 108 | + ve1.getTarget() = v and |
| 109 | + ve2.getTarget() = v |
| 110 | + ) |
| 111 | + or |
| 112 | + // - member variables that can be modified in both functions |
| 113 | + exists(MemberVariable v | |
| 114 | + v1 = v and |
| 115 | + v2 = v and |
| 116 | + returnValueDependsOnMemberVariable(f1, v) and |
| 117 | + returnValueDependsOnMemberVariable(f2, v) and |
| 118 | + v = getAMemberVariableEffect(f1).getTarget() and |
| 119 | + v = getAMemberVariableEffect(f2).getTarget() and |
| 120 | + ( |
| 121 | + globalValueNumber(arg1.getQualifier()) = globalValueNumber(arg2.getQualifier()) |
| 122 | + or |
| 123 | + v.isStatic() and arg1.getQualifier().getType() = arg2.getQualifier().getType() |
| 124 | + ) |
| 125 | + ) |
| 126 | + ) |
| 127 | +select call, |
| 128 | + "Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior.", |
| 129 | + arg1, arg1.toString(), arg2, arg2.toString() |
0 commit comments