Skip to content

Commit 8ffccfd

Browse files
committed
C++: Generate IR for assertions in release builds.
1 parent 9453835 commit 8ffccfd

File tree

3 files changed

+385
-2
lines changed

3 files changed

+385
-2
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ newtype TInstructionTag =
104104
} or
105105
SizeofVlaDimensionTag(int index) {
106106
exists(VlaDeclStmt v | exists(v.getTransitiveVlaDimensionStmt(index)))
107-
}
107+
} or
108+
AssertionVarAddressTag() or
109+
AssertionVarLoadTag() or
110+
AssertionOpTag() or
111+
AssertionBranchTag()
108112

109113
class InstructionTag extends TInstructionTag {
110114
final string toString() { result = getInstructionTagId(this) }
@@ -296,4 +300,12 @@ string getInstructionTagId(TInstructionTag tag) {
296300
tag = CoAwaitBranchTag() and result = "CoAwaitBranch"
297301
or
298302
tag = BoolToIntConversionTag() and result = "BoolToIntConversion"
303+
or
304+
tag = AssertionVarAddressTag() and result = "AssertionVarAddress"
305+
or
306+
tag = AssertionVarLoadTag() and result = "AssertionVarLoad"
307+
or
308+
tag = AssertionOpTag() and result = "AssertionOp"
309+
or
310+
tag = AssertionBranchTag() and result = "AssertionBranch"
299311
}
Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
private import cpp
2+
private import semmle.code.cpp.ir.internal.IRUtilities
3+
private import semmle.code.cpp.ir.implementation.internal.OperandTag
4+
private import semmle.code.cpp.ir.internal.CppType
5+
private import semmle.code.cpp.ir.internal.TempVariableTag
6+
private import InstructionTag
7+
private import TranslatedElement
8+
private import TranslatedStmt
9+
private import TranslatedFunction
10+
11+
/**
12+
* Holds if `s` is a statement that may be an expanded assertion in a
13+
* release build.
14+
*/
15+
pragma[nomagic]
16+
private predicate stmtCandidate(Stmt s) {
17+
not s.isFromUninstantiatedTemplate(_) and
18+
(
19+
// The expansion of `__analysis_assume(x != 0);` when `__analysis_assume` is
20+
// empty is the empty statement.
21+
s instanceof EmptyStmt
22+
or
23+
// The expansion of `assert(x != 0)` when `assert` is `((void)0)` is a zero literal
24+
// with a void type.
25+
exists(Expr e |
26+
e = s.(ExprStmt).getExpr() and
27+
e.getValue() = "0" and
28+
e.getActualType() instanceof VoidType
29+
)
30+
)
31+
}
32+
33+
pragma[nomagic]
34+
private predicate macroInvocationLocation(int startline, Function f, MacroInvocation mi) {
35+
mi.getMacroName() = ["assert", "__analysis_assume"] and
36+
mi.getLocation().hasLocationInfo(_, startline, _, _, _) and
37+
f.getEntryPoint().isAffectedByMacro(mi)
38+
}
39+
40+
pragma[nomagic]
41+
private predicate stmtParentLocation(int startline, Function f, StmtParent p) {
42+
p.getEnclosingFunction() = f and
43+
p.getLocation().hasLocationInfo(_, startline, _, _, _)
44+
}
45+
46+
/**
47+
* Holds if `mi` is a macro invocation with a name that is known
48+
* to correspond to an assertion macro, and the macro invocation
49+
* is the only thing on the line.
50+
*/
51+
pragma[nomagic]
52+
private predicate assertion0(MacroInvocation mi, Stmt s) {
53+
stmtCandidate(s) and
54+
s =
55+
unique(StmtParent p, int startline, Function f |
56+
macroInvocationLocation(startline, f, mi) and
57+
stmtParentLocation(startline, f, p) and
58+
// Also do not count the elements from the expanded macro. i.e., when checking
59+
// if `assert(x)` is the only thing on the line we do not count the
60+
// generated `((void)0)` expression.
61+
not p = mi.getAnExpandedElement()
62+
|
63+
p
64+
)
65+
}
66+
67+
private Function getEnclosingFunctionForMacroInvocation(MacroInvocation mi) {
68+
exists(Stmt s |
69+
assertion0(mi, s) and
70+
result = s.getEnclosingFunction()
71+
)
72+
}
73+
74+
/**
75+
* Holds if `arg` has two components and the `i`'th component of the string
76+
* `arg` is `s`, and the components are seperated by an operation with
77+
* opcode `opcode`.
78+
*/
79+
bindingset[arg]
80+
pragma[inline_late]
81+
private predicate parseArgument(string arg, string s, int i, Opcode opcode) {
82+
s = arg.regexpCapture("(.+)\\s<=\\s(.+)", i + 1) and
83+
opcode instanceof Opcode::CompareLE
84+
or
85+
s = arg.regexpCapture("(.+)\\s>=\\s(.+)", i + 1) and
86+
opcode instanceof Opcode::CompareGE
87+
or
88+
not arg.regexpMatch("(.+)\\s<=\\s(.+)") and
89+
s = arg.regexpCapture("(.+)\\s<\\s(.+)", i + 1) and
90+
opcode instanceof Opcode::CompareLT
91+
or
92+
not arg.regexpMatch("(.+)\\s>=\\s(.+)") and
93+
s = arg.regexpCapture("(.+)\\s>\\s(.+)", i + 1) and
94+
opcode instanceof Opcode::CompareGT
95+
or
96+
s = arg.regexpCapture("(.+)\\s!=\\s(.+)", i + 1) and
97+
opcode instanceof Opcode::CompareNE
98+
or
99+
s = arg.regexpCapture("(.+)\\s==\\s(.+)", i + 1) and
100+
opcode instanceof Opcode::CompareEQ
101+
}
102+
103+
/** Gets a local variable named `s` in `f`. */
104+
pragma[nomagic]
105+
private LocalScopeVariable getAVariableWithNameInFunction(Function f, string s) {
106+
result.getName() = s and
107+
result.getFunction() = f
108+
}
109+
110+
/**
111+
* Holds if the `i`'th component of the macro invocation `mi` with opcode
112+
* `opcode` is a reference to `var`.
113+
*/
114+
private predicate hasVarAccessMacroArgument(MacroInvocation mi, Variable var, int i, Opcode opcode) {
115+
exists(string arg, string s, Function f |
116+
arg = mi.getUnexpandedArgument(0) and
117+
f = getEnclosingFunctionForMacroInvocation(mi) and
118+
not exists(s.toInt()) and
119+
parseArgument(arg, s, i, opcode) and
120+
var = unique( | | getAVariableWithNameInFunction(f, s))
121+
)
122+
}
123+
124+
/**
125+
* Holds if the `i`'th component of the macro invocation `mi` with opcode
126+
* `opcode` is a
127+
* constant with the value `k`.
128+
*/
129+
private predicate hasConstMacroArgument(MacroInvocation mi, int k, int i, Opcode opcode) {
130+
exists(string arg, string s |
131+
assertion0(mi, _) and
132+
arg = mi.getUnexpandedArgument(0) and
133+
s.toInt() = k and
134+
parseArgument(arg, s, i, opcode)
135+
)
136+
}
137+
138+
predicate hasAssertionOperand(MacroInvocation mi, int i) {
139+
hasVarAccessMacroArgument(mi, _, i, _)
140+
or
141+
hasConstMacroArgument(mi, _, i, _)
142+
}
143+
144+
private predicate hasAssertionOpcode(MacroInvocation mi, Opcode opcode) {
145+
hasVarAccessMacroArgument(mi, _, _, opcode)
146+
or
147+
hasConstMacroArgument(mi, _, _, opcode)
148+
}
149+
150+
/**
151+
* Holds if `mi` is a macro invocation that is an assertion that should be generated
152+
* in the control-flow graph at `s`.
153+
*/
154+
predicate assertion(MacroInvocation mi, Stmt s) {
155+
assertion0(mi, s) and
156+
hasAssertionOperand(mi, 0) and
157+
hasAssertionOperand(mi, 1)
158+
}
159+
160+
/** The translation of an operand of an assertion. */
161+
abstract private class TranslatedAssertionOperand extends TranslatedElement,
162+
TTranslatedAssertionOperand
163+
{
164+
MacroInvocation mi;
165+
int index;
166+
167+
TranslatedAssertionOperand() { this = TTranslatedAssertionOperand(mi, index) }
168+
169+
MacroInvocation getMacroInvocation() { result = mi }
170+
171+
/**
172+
* Gets the statement that is being replaced by the assertion that uses this
173+
* operand.
174+
*/
175+
Stmt getStmt() { assertion(mi, result) }
176+
177+
final override Locatable getAst() { result = this.getStmt() }
178+
179+
final override TranslatedElement getChild(int id) { none() }
180+
181+
final override Declaration getFunction() { result = this.getStmt().getEnclosingFunction() }
182+
183+
/** Gets the instruction which holds the result of this operand. */
184+
abstract Instruction getResult();
185+
186+
final override string toString() { result = "Operand of assertion: " + mi }
187+
188+
/** Gets the index of this operand (i.e., `0` or `1`). */
189+
final int getIndex() { result = index }
190+
}
191+
192+
/** An operand of an assertion that is a variable access. */
193+
private class TranslatedAssertionVarAccess extends TranslatedAssertionOperand {
194+
TranslatedAssertionVarAccess() { hasVarAccessMacroArgument(mi, _, index, _) }
195+
196+
Variable getVariable() { hasVarAccessMacroArgument(mi, result, index, _) }
197+
198+
final override IRUserVariable getInstructionVariable(InstructionTag tag) {
199+
tag = AssertionVarAddressTag() and
200+
result.getVariable() = this.getVariable()
201+
}
202+
203+
final override Instruction getFirstInstruction(EdgeKind kind) {
204+
result = this.getInstruction(AssertionVarAddressTag()) and kind instanceof GotoEdge
205+
}
206+
207+
final override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
208+
tag = AssertionVarAddressTag() and
209+
kind instanceof GotoEdge and
210+
result = this.getInstruction(AssertionVarLoadTag())
211+
or
212+
tag = AssertionVarLoadTag() and
213+
result = getTranslatedAssertionMacroInvocation(mi).getChildSuccessor(this, kind)
214+
}
215+
216+
final override Instruction getALastInstructionInternal() {
217+
result = this.getInstruction(AssertionVarLoadTag())
218+
}
219+
220+
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
221+
exists(Variable v | v = this.getVariable() |
222+
opcode instanceof Opcode::VariableAddress and
223+
tag = AssertionVarAddressTag() and
224+
resultType = getTypeForGLValue(v.getType())
225+
or
226+
opcode instanceof Opcode::Load and
227+
tag = AssertionVarLoadTag() and
228+
resultType = getTypeForPRValue(v.getType())
229+
)
230+
}
231+
232+
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
233+
tag = AssertionVarLoadTag() and
234+
operandTag instanceof AddressOperandTag and
235+
result = this.getInstruction(AssertionVarAddressTag())
236+
}
237+
238+
final override Instruction getResult() { result = this.getInstruction(AssertionVarLoadTag()) }
239+
}
240+
241+
/** An operand of an assertion that is a constant access. */
242+
private class TranslatedAssertionConst extends TranslatedAssertionOperand {
243+
TranslatedAssertionConst() { hasConstMacroArgument(mi, _, index, _) }
244+
245+
int getConst() { hasConstMacroArgument(mi, result, index, _) }
246+
247+
final override string getInstructionConstantValue(InstructionTag tag) {
248+
tag = OnlyInstructionTag() and
249+
result = this.getConst().toString()
250+
}
251+
252+
final override Instruction getFirstInstruction(EdgeKind kind) {
253+
result = this.getInstruction(OnlyInstructionTag()) and
254+
kind instanceof GotoEdge
255+
}
256+
257+
final override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
258+
tag = OnlyInstructionTag() and
259+
result = getTranslatedAssertionMacroInvocation(mi).getChildSuccessor(this, kind)
260+
}
261+
262+
final override Instruction getALastInstructionInternal() {
263+
result = this.getInstruction(OnlyInstructionTag())
264+
}
265+
266+
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
267+
opcode instanceof Opcode::Constant and
268+
tag = OnlyInstructionTag() and
269+
resultType = getIntType()
270+
}
271+
272+
final override Instruction getResult() { result = this.getInstruction(OnlyInstructionTag()) }
273+
}
274+
275+
/**
276+
* Gets the `TranslatedAssertionMacroInvocation` corresponding to the macro
277+
* invocation `mi`.
278+
*/
279+
TranslatedAssertionMacroInvocation getTranslatedAssertionMacroInvocation(MacroInvocation mi) {
280+
result.getMacroInvocation() = mi
281+
}
282+
283+
/**
284+
* A synthesized assertion which would have otherwise been invisible because the
285+
* database represents a release build where assertions are disabled.
286+
*/
287+
private class TranslatedAssertionMacroInvocation extends TranslatedStmt {
288+
MacroInvocation mi;
289+
290+
TranslatedAssertionMacroInvocation() { assertion(mi, stmt) }
291+
292+
final override Instruction getFirstInstruction(EdgeKind kind) {
293+
result = this.getLeft().getFirstInstruction(kind)
294+
}
295+
296+
TranslatedAssertionOperand getLeft() {
297+
result.getMacroInvocation() = mi and
298+
result.getIndex() = 0
299+
}
300+
301+
TranslatedAssertionOperand getRight() {
302+
result.getMacroInvocation() = mi and
303+
result.getIndex() = 1
304+
}
305+
306+
final override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
307+
tag = AssertionOpTag() and
308+
kind instanceof GotoEdge and
309+
result = this.getInstruction(AssertionBranchTag())
310+
or
311+
tag = AssertionBranchTag() and
312+
kind instanceof TrueEdge and
313+
result = this.getParent().getChildSuccessor(this, _)
314+
}
315+
316+
final override TranslatedElement getChildInternal(int id) {
317+
id = 0 and result = this.getLeft()
318+
or
319+
id = 1 and result = this.getRight()
320+
}
321+
322+
final override Instruction getALastInstructionInternal() {
323+
result = this.getInstruction(AssertionBranchTag())
324+
}
325+
326+
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
327+
tag = AssertionOpTag() and
328+
resultType = getBoolType() and
329+
hasAssertionOpcode(mi, opcode)
330+
or
331+
tag = AssertionBranchTag() and
332+
resultType = getVoidType() and
333+
opcode instanceof Opcode::ConditionalBranch
334+
}
335+
336+
final override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
337+
child = this.getLeft() and
338+
result = this.getRight().getFirstInstruction(kind)
339+
or
340+
child = this.getRight() and
341+
kind instanceof GotoEdge and
342+
result = this.getInstruction(AssertionOpTag())
343+
}
344+
345+
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
346+
tag = AssertionOpTag() and
347+
(
348+
operandTag instanceof LeftOperandTag and
349+
result = this.getLeft().getResult()
350+
or
351+
operandTag instanceof RightOperandTag and
352+
result = this.getRight().getResult()
353+
)
354+
or
355+
tag = AssertionBranchTag() and
356+
operandTag instanceof ConditionOperandTag and
357+
result = this.getInstruction(AssertionOpTag())
358+
}
359+
360+
MacroInvocation getMacroInvocation() { result = mi }
361+
}

0 commit comments

Comments
 (0)