|
| 1 | +/** |
| 2 | + * @id c/misra/object-copied-to-an-overlapping-object |
| 3 | + * @name RULE-19-1: An object shall not be copied to an overlapping object |
| 4 | + * @description An object shall not be copied to an overlapping object. |
| 5 | + * @kind problem |
| 6 | + * @precision high |
| 7 | + * @problem.severity error |
| 8 | + * @tags external/misra/id/rule-19-1 |
| 9 | + * correctness |
| 10 | + * external/misra/obligation/mandatory |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import codingstandards.c.misra |
| 15 | +import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 16 | +import semmle.code.cpp.dataflow.DataFlow |
| 17 | + |
| 18 | +/** |
| 19 | + * Models calls to memcpy on overlapping objects |
| 20 | + */ |
| 21 | +class MemcpyCall extends Locatable { |
| 22 | + Expr src; |
| 23 | + Expr dst; |
| 24 | + |
| 25 | + MemcpyCall() { |
| 26 | + this.(MacroInvocation).getMacroName() = "memcpy" and |
| 27 | + src = this.(MacroInvocation).getExpr().getChild(1) and |
| 28 | + dst = this.(MacroInvocation).getExpr().getChild(0) |
| 29 | + or |
| 30 | + this.(FunctionCall).getTarget().hasGlobalName("memcpy") and |
| 31 | + src = this.(FunctionCall).getArgument(1) and |
| 32 | + dst = this.(FunctionCall).getArgument(0) |
| 33 | + } |
| 34 | + |
| 35 | + Expr getSrc() { result = src } |
| 36 | + |
| 37 | + Expr getDst() { result = dst } |
| 38 | + |
| 39 | + Expr getBase(Expr e) { |
| 40 | + result = |
| 41 | + [ |
| 42 | + e.(VariableAccess), e.(PointerAddExpr).getLeftOperand(), |
| 43 | + e.(AddressOfExpr).getOperand().(ArrayExpr).getArrayBase() |
| 44 | + ] |
| 45 | + } |
| 46 | + |
| 47 | + int getOffset(Expr e) { |
| 48 | + result = |
| 49 | + [ |
| 50 | + e.(PointerAddExpr).getRightOperand().getValue().toInt(), |
| 51 | + e.(AddressOfExpr).getOperand().(ArrayExpr).getArrayOffset().getValue().toInt() |
| 52 | + ] |
| 53 | + or |
| 54 | + e instanceof VariableAccess and result = 0 |
| 55 | + } |
| 56 | + |
| 57 | + // maximum amount of element copied |
| 58 | + int getCount() { |
| 59 | + result = |
| 60 | + upperBound([this.(MacroInvocation).getExpr().getChild(2), this.(FunctionCall).getArgument(2)]) |
| 61 | + } |
| 62 | + |
| 63 | + // source and destination overlap |
| 64 | + predicate overlap() { |
| 65 | + globalValueNumber(this.getBase(src)) = globalValueNumber(this.getBase(dst)) and |
| 66 | + exists(int dstStart, int dstEnd, int srcStart, int srcEnd | |
| 67 | + dstStart = this.getOffset(dst) and |
| 68 | + dstEnd = dstStart + this.getCount() - 1 and |
| 69 | + srcStart = this.getOffset(src) and |
| 70 | + srcEnd = srcStart + this.getCount() - 1 and |
| 71 | + ( |
| 72 | + srcStart >= dstStart and srcEnd <= dstEnd |
| 73 | + or |
| 74 | + srcStart <= dstStart and srcEnd > dstStart |
| 75 | + or |
| 76 | + srcStart < dstEnd and srcEnd >= dstStart |
| 77 | + ) and |
| 78 | + // Exception 1: exact overlap and compatible type |
| 79 | + not ( |
| 80 | + srcStart = dstStart and |
| 81 | + srcEnd = dstEnd and |
| 82 | + this.getBase(src).getUnspecifiedType() = this.getBase(dst).getUnspecifiedType() |
| 83 | + ) |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +from MemcpyCall memcpy |
| 89 | +where |
| 90 | + not isExcluded(memcpy, Contracts7Package::objectCopiedToAnOverlappingObjectQuery()) and |
| 91 | + memcpy.overlap() |
| 92 | +select memcpy, "The object to copy $@ overlaps the object to copy $@.", memcpy.getSrc(), "from", |
| 93 | + memcpy.getDst(), "to" |
0 commit comments