Skip to content

Commit 16d6218

Browse files
committed
C++: Use this new predicate everywhere we need to convert an instruction to an expression.
1 parent 60819ad commit 16d6218

File tree

35 files changed

+1269
-719
lines changed

35 files changed

+1269
-719
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,34 +1091,24 @@ private import GetConvertedResultExpression
10911091
predicate exprNodeShouldBeOperand(OperandNode node, Expr e) {
10921092
exists(Instruction def |
10931093
unique( | | getAUse(def)) = node.getOperand() and
1094-
e = def.getConvertedResultExpression()
1094+
e = getConvertedResultExpression(def)
10951095
)
10961096
}
10971097

1098-
private predicate indirectExprNodeShouldBeIndirectOperand0(
1099-
VariableAddressInstruction instr, RawIndirectOperand node, Expr e
1100-
) {
1101-
instr = node.getOperand().getDef() and
1102-
e = instr.getAst().(Expr).getUnconverted()
1103-
}
1104-
11051098
/** Holds if `node` should be an `IndirectOperand` that maps `node.asIndirectExpr()` to `e`. */
1106-
private predicate indirectExprNodeShouldBeIndirectOperand(RawIndirectOperand node, Expr e) {
1107-
exists(Instruction instr | instr = node.getOperand().getDef() |
1108-
exists(Expr e0 |
1109-
indirectExprNodeShouldBeIndirectOperand0(instr, node, e0) and
1110-
e = e0.getFullyConverted()
1111-
)
1112-
or
1113-
not indirectExprNodeShouldBeIndirectOperand0(_, node, _) and
1114-
e = instr.getConvertedResultExpression()
1099+
private predicate indirectExprNodeShouldBeIndirectOperand(
1100+
IndirectOperand node, Expr e, int indirectionIndex
1101+
) {
1102+
exists(Instruction def |
1103+
node.hasOperandAndIndirectionIndex(unique( | | getAUse(def)), indirectionIndex) and
1104+
e = getConvertedResultExpression(def)
11151105
)
11161106
}
11171107

11181108
private predicate exprNodeShouldBeIndirectOutNode(IndirectArgumentOutNode node, Expr e) {
11191109
exists(CallInstruction call |
11201110
call.getStaticCallTarget() instanceof Constructor and
1121-
e = call.getConvertedResultExpression() and
1111+
e = getConvertedResultExpression(call) and
11221112
call.getThisArgumentOperand() = node.getAddressOperand()
11231113
)
11241114
}
@@ -1127,37 +1117,17 @@ private predicate exprNodeShouldBeIndirectOutNode(IndirectArgumentOutNode node,
11271117
predicate exprNodeShouldBeInstruction(Node node, Expr e) {
11281118
not exprNodeShouldBeOperand(_, e) and
11291119
not exprNodeShouldBeIndirectOutNode(_, e) and
1130-
(
1131-
e = node.asInstruction().getConvertedResultExpression()
1132-
or
1133-
// The instruction that contains the result of an `AssignOperation` is
1134-
// the unloaded left operand (see the comments in `TranslatedAssignOperation`).
1135-
// That means that for cases like
1136-
// ```cpp
1137-
// int x = ...;
1138-
// x += 1;
1139-
// ```
1140-
// the result of `x += 1` is the `VariableAddressInstruction` that represents `x`. But
1141-
// that instruction doesn't receive the flow from this `AssignOperation`. So instead we
1142-
// map the operation to the `AddInstruction`.
1143-
node.asInstruction().getAst() = e.(AssignOperation)
1144-
or
1145-
// Same story for `CrementOperation`s (cf. the comments in the subclasses
1146-
// of `TranslatedCrementOperation`).
1147-
node.asInstruction().getAst() = e.(CrementOperation)
1148-
)
1120+
e = getConvertedResultExpression(node.asInstruction())
11491121
}
11501122

11511123
/** Holds if `node` should be an `IndirectInstruction` that maps `node.asIndirectExpr()` to `e`. */
1152-
predicate indirectExprNodeShouldBeIndirectInstruction(IndirectInstruction node, Expr e) {
1124+
predicate indirectExprNodeShouldBeIndirectInstruction(
1125+
IndirectInstruction node, Expr e, int indirectionIndex
1126+
) {
1127+
not indirectExprNodeShouldBeIndirectOperand(_, e, indirectionIndex) and
11531128
exists(Instruction instr |
1154-
node.hasInstructionAndIndirectionIndex(instr, _) and
1155-
not indirectExprNodeShouldBeIndirectOperand(_, e)
1156-
|
1157-
e = instr.(VariableAddressInstruction).getAst().(Expr).getFullyConverted()
1158-
or
1159-
not instr instanceof VariableAddressInstruction and
1160-
e = instr.getConvertedResultExpression()
1129+
node.hasInstructionAndIndirectionIndex(instr, indirectionIndex) and
1130+
e = getConvertedResultExpression(instr)
11611131
)
11621132
}
11631133

@@ -1169,27 +1139,19 @@ abstract private class ExprNodeBase extends Node {
11691139
abstract Expr getConvertedExpr();
11701140

11711141
/** Gets the non-conversion expression corresponding to this node, if any. */
1172-
abstract Expr getExpr();
1142+
final Expr getExpr() { result = this.getConvertedExpr().getUnconverted() }
11731143
}
11741144

11751145
private class InstructionExprNode extends ExprNodeBase, InstructionNode {
11761146
InstructionExprNode() { exprNodeShouldBeInstruction(this, _) }
11771147

11781148
final override Expr getConvertedExpr() { exprNodeShouldBeInstruction(this, result) }
1179-
1180-
final override Expr getExpr() { result = this.getConvertedExpr().getUnconverted() }
1181-
1182-
final override string toStringImpl() { result = this.getConvertedExpr().toString() }
11831149
}
11841150

11851151
private class OperandExprNode extends ExprNodeBase, OperandNode {
11861152
OperandExprNode() { exprNodeShouldBeOperand(this, _) }
11871153

11881154
final override Expr getConvertedExpr() { exprNodeShouldBeOperand(this, result) }
1189-
1190-
final override Expr getExpr() { result = this.getConvertedExpr().getUnconverted() }
1191-
1192-
final override string toStringImpl() { result = this.getConvertedExpr().toString() }
11931155
}
11941156

11951157
abstract private class IndirectExprNodeBase extends Node {
@@ -1200,45 +1162,33 @@ abstract private class IndirectExprNodeBase extends Node {
12001162
abstract Expr getConvertedExpr(int indirectionIndex);
12011163

12021164
/** Gets the non-conversion expression corresponding to this node, if any. */
1203-
abstract Expr getExpr(int indirectionIndex);
1165+
final Expr getExpr(int indirectionIndex) {
1166+
result = this.getConvertedExpr(indirectionIndex).getUnconverted()
1167+
}
12041168
}
12051169

1206-
private class IndirectOperandIndirectExprNode extends IndirectExprNodeBase, RawIndirectOperand {
1207-
IndirectOperandIndirectExprNode() { indirectExprNodeShouldBeIndirectOperand(this, _) }
1170+
private class IndirectOperandIndirectExprNode extends IndirectExprNodeBase instanceof IndirectOperand
1171+
{
1172+
IndirectOperandIndirectExprNode() { indirectExprNodeShouldBeIndirectOperand(this, _, _) }
12081173

12091174
final override Expr getConvertedExpr(int index) {
1210-
this.getIndirectionIndex() = index and
1211-
indirectExprNodeShouldBeIndirectOperand(this, result)
1212-
}
1213-
1214-
final override Expr getExpr(int index) {
1215-
this.getIndirectionIndex() = index and
1216-
result = this.getConvertedExpr(index).getUnconverted()
1175+
indirectExprNodeShouldBeIndirectOperand(this, result, index)
12171176
}
12181177
}
12191178

1220-
private class IndirectInstructionIndirectExprNode extends IndirectExprNodeBase,
1221-
RawIndirectInstruction
1179+
private class IndirectInstructionIndirectExprNode extends IndirectExprNodeBase instanceof IndirectInstruction
12221180
{
1223-
IndirectInstructionIndirectExprNode() { indirectExprNodeShouldBeIndirectInstruction(this, _) }
1181+
IndirectInstructionIndirectExprNode() { indirectExprNodeShouldBeIndirectInstruction(this, _, _) }
12241182

12251183
final override Expr getConvertedExpr(int index) {
1226-
this.getIndirectionIndex() = index and
1227-
indirectExprNodeShouldBeIndirectInstruction(this, result)
1228-
}
1229-
1230-
final override Expr getExpr(int index) {
1231-
this.getIndirectionIndex() = index and
1232-
result = this.getConvertedExpr(index).getUnconverted()
1184+
indirectExprNodeShouldBeIndirectInstruction(this, result, index)
12331185
}
12341186
}
12351187

12361188
private class IndirectArgumentOutExprNode extends ExprNodeBase, IndirectArgumentOutNode {
12371189
IndirectArgumentOutExprNode() { exprNodeShouldBeIndirectOutNode(this, _) }
12381190

12391191
final override Expr getConvertedExpr() { exprNodeShouldBeIndirectOutNode(this, result) }
1240-
1241-
final override Expr getExpr() { result = this.getConvertedExpr() }
12421192
}
12431193

12441194
/**
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
WARNING: Module TaintedWithPath has been deprecated and may be removed in future (tainted.ql:9,8-47)
22
WARNING: Predicate tainted has been deprecated and may be removed in future (tainted.ql:20,49-74)
3-
failures
43
testFailures
4+
| test_diff.cpp:100:10:100:14 | * ... | Unexpected result: ir-sink=98:17 |
5+
| test_diff.cpp:102:26:102:30 | (const char *)... | Unexpected result: ir-path=98:17 |
6+
failures

0 commit comments

Comments
 (0)