|
| 1 | +/** |
| 2 | + * Provides models for C++ constructors and user-defined operators. |
| 3 | + */ |
| 4 | + |
| 5 | +import cpp |
| 6 | +import semmle.code.cpp.models.interfaces.DataFlow |
| 7 | +import semmle.code.cpp.models.interfaces.Taint |
| 8 | + |
| 9 | +/** |
| 10 | + * Model for C++ conversion constructors. |
| 11 | + */ |
| 12 | +class ConversionConstructorModel extends ConversionConstructor, TaintFunction { |
| 13 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 14 | + // taint flow from the first constructor argument to the returned object |
| 15 | + input.isParameter(0) and |
| 16 | + output.isReturnValue() // TODO: this should be `isQualifierObject` by our current definitions, but that flow is not yet supported. |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Model for C++ copy constructors. |
| 22 | + */ |
| 23 | +class CopyConstructorModel extends CopyConstructor, DataFlowFunction { |
| 24 | + override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { |
| 25 | + // data flow from the first constructor argument to the returned object |
| 26 | + input.isParameter(0) and |
| 27 | + output.isReturnValue() // TODO: this should be `isQualifierObject` by our current definitions, but that flow is not yet supported. |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Model for C++ move constructors. |
| 33 | + */ |
| 34 | +class MoveConstructorModel extends MoveConstructor, DataFlowFunction { |
| 35 | + override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { |
| 36 | + // data flow from the first constructor argument to the returned object |
| 37 | + input.isParameter(0) and |
| 38 | + output.isReturnValue() // TODO: this should be `isQualifierObject` by our current definitions, but that flow is not yet supported. |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Model for C++ copy assignment operators. |
| 44 | + */ |
| 45 | +class CopyAssignmentOperatorModel extends CopyAssignmentOperator, TaintFunction { |
| 46 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 47 | + // taint flow from argument to self |
| 48 | + input.isParameterDeref(0) and |
| 49 | + output.isQualifierObject() |
| 50 | + or |
| 51 | + // taint flow from argument to return value |
| 52 | + input.isParameterDeref(0) and |
| 53 | + output.isReturnValueDeref() |
| 54 | + // TODO: it would be more accurate to model copy assignment as data flow |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Model for C++ move assignment operators. |
| 60 | + */ |
| 61 | +class MoveAssignmentOperatorModel extends MoveAssignmentOperator, TaintFunction { |
| 62 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 63 | + // taint flow from argument to self |
| 64 | + input.isParameterDeref(0) and |
| 65 | + output.isQualifierObject() |
| 66 | + or |
| 67 | + // taint flow from argument to return value |
| 68 | + input.isParameterDeref(0) and |
| 69 | + output.isReturnValueDeref() |
| 70 | + // TODO: it would be more accurate to model move assignment as data flow |
| 71 | + } |
| 72 | +} |
0 commit comments