Skip to content

Commit 09f441a

Browse files
authored
Merge pull request #2009 from AndreiDiaconu1/ircsharp-rangeanalysis
C# IR: Add range analysis library
2 parents 18b28b1 + a7a5eaa commit 09f441a

File tree

13 files changed

+2364
-0
lines changed

13 files changed

+2364
-0
lines changed

csharp/ql/src/semmle/code/csharp/ir/internal/IRGuards.qll

Lines changed: 667 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import csharp
2+
private import semmle.code.csharp.ir.IR
3+
private import semmle.code.csharp.ir.ValueNumbering
4+
5+
private newtype TBound =
6+
TBoundZero() or
7+
TBoundValueNumber(ValueNumber vn) {
8+
exists(Instruction i |
9+
vn.getAnInstruction() = i and
10+
(
11+
i.getResultType() instanceof IntegralType or
12+
i.getResultType() instanceof PointerType
13+
) and
14+
not vn.getAnInstruction() instanceof ConstantInstruction
15+
|
16+
i instanceof PhiInstruction
17+
or
18+
i instanceof InitializeParameterInstruction
19+
or
20+
i instanceof CallInstruction
21+
or
22+
i instanceof VariableAddressInstruction
23+
or
24+
i instanceof FieldAddressInstruction
25+
or
26+
i.(LoadInstruction).getSourceAddress() instanceof VariableAddressInstruction
27+
or
28+
i.(LoadInstruction).getSourceAddress() instanceof FieldAddressInstruction
29+
or
30+
i.getAUse() instanceof ArgumentOperand
31+
)
32+
}
33+
34+
/**
35+
* A bound that may be inferred for an expression plus/minus an integer delta.
36+
*/
37+
abstract class Bound extends TBound {
38+
abstract string toString();
39+
40+
/** Gets an expression that equals this bound plus `delta`. */
41+
abstract Instruction getInstruction(int delta);
42+
43+
/** Gets an expression that equals this bound. */
44+
Instruction getInstruction() { result = getInstruction(0) }
45+
46+
abstract Location getLocation();
47+
}
48+
49+
/**
50+
* The bound that corresponds to the integer 0. This is used to represent all
51+
* integer bounds as bounds are always accompanied by an added integer delta.
52+
*/
53+
class ZeroBound extends Bound, TBoundZero {
54+
override string toString() { result = "0" }
55+
56+
override Instruction getInstruction(int delta) {
57+
result.(ConstantValueInstruction).getValue().toInt() = delta
58+
}
59+
60+
override Location getLocation() { result instanceof EmptyLocation }
61+
}
62+
63+
/**
64+
* A bound corresponding to the value of an `Instruction`.
65+
*/
66+
class ValueNumberBound extends Bound, TBoundValueNumber {
67+
ValueNumber vn;
68+
69+
ValueNumberBound() { this = TBoundValueNumber(vn) }
70+
71+
/** Gets the SSA variable that equals this bound. */
72+
override Instruction getInstruction(int delta) {
73+
this = TBoundValueNumber(valueNumber(result)) and delta = 0
74+
}
75+
76+
override string toString() { result = vn.getExampleInstruction().toString() }
77+
78+
override Location getLocation() { result = vn.getLocation() }
79+
}

0 commit comments

Comments
 (0)