|
| 1 | +/** |
| 2 | + * This library proves that a subset of pointer dereferences in a program are |
| 3 | + * safe, i.e. in-bounds. |
| 4 | + * It does so by first defining what a pointer dereference is (on the IR |
| 5 | + * `Instruction` level), and then using the array length analysis and the range |
| 6 | + * analysis together to prove that some of these pointer dereferences are safe. |
| 7 | + * |
| 8 | + * The analysis is soundy, i.e. it is sound if no undefined behaviour is present |
| 9 | + * in the program. |
| 10 | + * Furthermore, it crucially depends on the soundiness of the range analysis and |
| 11 | + * the array length analysis. |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +private import experimental.semmle.code.cpp.rangeanalysis.ArrayLengthAnalysis |
| 16 | +private import semmle.code.cpp.rangeanalysis.RangeAnalysis |
| 17 | + |
| 18 | +/** |
| 19 | + * Gets the instruction that computes the address of memory that `i` accesses. |
| 20 | + * Only holds if `i` dereferences a pointer, not when the computation of the |
| 21 | + * memory address is constant, or if the address of a local variable is loaded/stored to. |
| 22 | + */ |
| 23 | +private Instruction getMemoryAddressInstruction(Instruction i) { |
| 24 | + ( |
| 25 | + result = i.(FieldAddressInstruction).getObjectAddress() or |
| 26 | + result = i.(LoadInstruction).getSourceAddress() or |
| 27 | + result = i.(StoreInstruction).getDestinationAddress() |
| 28 | + ) and |
| 29 | + not result instanceof FieldAddressInstruction and |
| 30 | + not result instanceof VariableAddressInstruction and |
| 31 | + not result instanceof ConstantValueInstruction |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * All instructions that dereference a pointer. |
| 36 | + */ |
| 37 | +class PointerDereferenceInstruction extends Instruction { |
| 38 | + PointerDereferenceInstruction() { exists(getMemoryAddressInstruction(this)) } |
| 39 | + |
| 40 | + Instruction getAddress() { result = getMemoryAddressInstruction(this) } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Holds if `ptrDeref` can be proven to always access allocated memory. |
| 45 | + */ |
| 46 | +predicate inBounds(PointerDereferenceInstruction ptrDeref) { |
| 47 | + exists(Length length, int lengthDelta, Offset offset, int offsetDelta | |
| 48 | + knownArrayLength(ptrDeref.getAddress(), length, lengthDelta, offset, offsetDelta) and |
| 49 | + // lower bound - note that we treat a pointer that accesses an array of |
| 50 | + // length 0 as on upper-bound violation, but not as a lower-bound violation |
| 51 | + ( |
| 52 | + offset instanceof ZeroOffset and |
| 53 | + offsetDelta >= 0 |
| 54 | + or |
| 55 | + offset instanceof OpOffset and |
| 56 | + exists(int lowerBoundDelta | |
| 57 | + boundedOperand(offset.(OpOffset).getOperand(), any(ZeroBound b), lowerBoundDelta, |
| 58 | + /*upper*/ false, _) and |
| 59 | + lowerBoundDelta + offsetDelta >= 0 |
| 60 | + ) |
| 61 | + ) and |
| 62 | + // upper bound |
| 63 | + ( |
| 64 | + // both offset and length are only integers |
| 65 | + length instanceof ZeroLength and |
| 66 | + offset instanceof ZeroOffset and |
| 67 | + offsetDelta < lengthDelta |
| 68 | + or |
| 69 | + exists(int lengthBound | |
| 70 | + // array length is variable+integer, and there's a fixed (integer-only) |
| 71 | + // lower bound on the variable, so we can guarantee this access is always in-bounds |
| 72 | + length instanceof VNLength and |
| 73 | + offset instanceof ZeroOffset and |
| 74 | + boundedInstruction(length.(VNLength).getInstruction(), any(ZeroBound b), lengthBound, |
| 75 | + /* upper*/ false, _) and |
| 76 | + offsetDelta < lengthBound + lengthDelta |
| 77 | + ) |
| 78 | + or |
| 79 | + exists(int offsetBoundDelta | |
| 80 | + length instanceof ZeroLength and |
| 81 | + offset instanceof OpOffset and |
| 82 | + boundedOperand(offset.(OpOffset).getOperand(), any(ZeroBound b), offsetBoundDelta, |
| 83 | + /* upper */ true, _) and |
| 84 | + // offset <= offsetBoundDelta, so offset + offsetDelta <= offsetDelta + offsetBoundDelta |
| 85 | + // Thus, in-bounds if offsetDelta + offsetBoundDelta < lengthDelta |
| 86 | + // as we have length instanceof ZeroLength |
| 87 | + offsetDelta + offsetBoundDelta < lengthDelta |
| 88 | + ) |
| 89 | + or |
| 90 | + exists(ValueNumberBound b, int offsetBoundDelta | |
| 91 | + length instanceof VNLength and |
| 92 | + offset instanceof OpOffset and |
| 93 | + b.getValueNumber() = length.(VNLength).getValueNumber() and |
| 94 | + boundedOperand(offset.(OpOffset).getOperand(), b, offsetBoundDelta, /*upper*/ true, _) and |
| 95 | + // this ensures that offset <= length holds |
| 96 | + offsetBoundDelta <= 0 and |
| 97 | + // with that we get that offset + offsetDelta < length offsetBoundDelta + lengthDelta - offsetBoundDelta |
| 98 | + offsetDelta < lengthDelta - offsetBoundDelta |
| 99 | + ) |
| 100 | + ) |
| 101 | + ) |
| 102 | +} |
0 commit comments