Skip to content

Commit ac03242

Browse files
committed
C++: Add an SSAVariable for pointer-arithmetic expressions in guards.
1 parent 6d06234 commit ac03242

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticExprSpecific.qll

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ private import semmle.code.cpp.ir.IR as IR
77
private import Semantic
88
private import experimental.semmle.code.cpp.rangeanalysis.Bound as IRBound
99
private import semmle.code.cpp.controlflow.IRGuards as IRGuards
10+
private import semmle.code.cpp.ir.ValueNumbering
1011

1112
module SemanticExprConfig {
1213
class Location = Cpp::Location;
@@ -119,8 +120,17 @@ module SemanticExprConfig {
119120
int getBasicBlockUniqueId(BasicBlock block) { idOf(block.getFirstInstruction().getAst(), result) }
120121

121122
newtype TSsaVariable =
122-
TSsaInstruction(IR::Instruction instr) { instr.hasMemoryResult() } or
123-
TSsaOperand(IR::Operand op) { op.isDefinitionInexact() }
123+
TSsaInstruction(IR::Instruction instr) {
124+
instr.hasMemoryResult()
125+
} or
126+
TSsaOperand(IR::Operand op) { op.isDefinitionInexact() } or
127+
TSsaPointerArithmeticGuard(IR::PointerArithmeticInstruction instr) {
128+
exists(Guard g, IR::Operand use | use = instr.getAUse() |
129+
g.comparesLt(use, _, _, _, _) or
130+
g.comparesLt(_, use, _, _, _) or
131+
g.comparesEq(use, _, _, _, _) or
132+
g.comparesEq(_, use, _, _, _))
133+
}
124134

125135
class SsaVariable extends TSsaVariable {
126136
string toString() { none() }
@@ -129,6 +139,8 @@ module SemanticExprConfig {
129139

130140
IR::Instruction asInstruction() { none() }
131141

142+
IR::PointerArithmeticInstruction asPointerArithGuard() { none() }
143+
132144
IR::Operand asOperand() { none() }
133145
}
134146

@@ -144,6 +156,18 @@ module SemanticExprConfig {
144156
final override IR::Instruction asInstruction() { result = instr }
145157
}
146158

159+
class SsaPointerArithmeticGuard extends SsaVariable, TSsaPointerArithmeticGuard {
160+
IR::PointerArithmeticInstruction instr;
161+
162+
SsaPointerArithmeticGuard() { this = TSsaPointerArithmeticGuard(instr) }
163+
164+
final override string toString() { result = instr.toString() }
165+
166+
final override Location getLocation() { result = instr.getLocation() }
167+
168+
final override IR::PointerArithmeticInstruction asPointerArithGuard() { result = instr }
169+
}
170+
147171
class SsaOperand extends SsaVariable, TSsaOperand {
148172
IR::Operand op;
149173

@@ -168,7 +192,11 @@ module SemanticExprConfig {
168192
)
169193
}
170194

171-
Expr getAUse(SsaVariable v) { result.(IR::LoadInstruction).getSourceValue() = v.asInstruction() }
195+
Expr getAUse(SsaVariable v) {
196+
result.(IR::LoadInstruction).getSourceValue() = v.asInstruction()
197+
or
198+
result = valueNumber(v.asPointerArithGuard()).getAnInstruction()
199+
}
172200

173201
SemType getSsaVariableType(SsaVariable v) {
174202
result = getSemanticType(v.asInstruction().getResultIRType())
@@ -208,7 +236,9 @@ module SemanticExprConfig {
208236

209237
final override predicate hasRead(SsaVariable v) {
210238
exists(IR::Operand operand |
211-
operand.getDef() = v.asInstruction() and
239+
operand.getDef() = v.asInstruction() or
240+
operand.getDef() = valueNumber(v.asPointerArithGuard()).getAnInstruction()
241+
|
212242
not operand instanceof IR::PhiInputOperand and
213243
operand.getUse().getBlock() = block
214244
)
@@ -227,7 +257,9 @@ module SemanticExprConfig {
227257

228258
final override predicate hasRead(SsaVariable v) {
229259
exists(IR::PhiInputOperand operand |
230-
operand.getDef() = v.asInstruction() and
260+
operand.getDef() = v.asInstruction() or
261+
operand.getDef() = valueNumber(v.asPointerArithGuard()).getAnInstruction()
262+
|
231263
operand.getPredecessorBlock() = pred and
232264
operand.getUse().getBlock() = succ
233265
)

cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticSSA.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SemSsaVariable instanceof Specific::SsaVariable {
1010

1111
final Specific::Location getLocation() { result = super.getLocation() }
1212

13-
final SemLoadExpr getAUse() { result = Specific::getAUse(this) }
13+
final SemExpr getAUse() { result = Specific::getAUse(this) }
1414

1515
final SemType getType() { result = Specific::getSsaVariableType(this) }
1616

0 commit comments

Comments
 (0)