Skip to content

Commit b53c354

Browse files
committed
C++: Add lambda dispatch for functors.
1 parent 663c3e7 commit b53c354

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,16 +1382,63 @@ predicate neverSkipInPathGraph(Node n) {
13821382
exists(n.asIndirectDefinition())
13831383
}
13841384

1385-
class LambdaCallKind = Unit;
1385+
private newtype TLambdaCallKind =
1386+
TFunctionPointer() or
1387+
TFunctor()
1388+
1389+
class LambdaCallKind extends TLambdaCallKind {
1390+
predicate isFunctionPointer() { this = TFunctionPointer() }
1391+
1392+
predicate isFunctor() { this = TFunctor() }
1393+
1394+
string toString() {
1395+
this.isFunctionPointer() and
1396+
result = "Function pointer kind"
1397+
or
1398+
this.isFunctor() and
1399+
result = "Functor kind"
1400+
}
1401+
}
1402+
1403+
private class ConstructorCallInstruction extends CallInstruction {
1404+
Cpp::Class constructedType;
1405+
1406+
ConstructorCallInstruction() {
1407+
this.getStaticCallTarget().(Cpp::Constructor).getDeclaringType() = constructedType
1408+
}
1409+
1410+
Cpp::Class getConstructedType() { result = constructedType }
1411+
}
1412+
1413+
private class OperatorCall extends Cpp::MemberFunction {
1414+
OperatorCall() { this.hasName("operator()") }
1415+
}
1416+
1417+
private predicate isFunctorCreationWithConstructor(Node creation, OperatorCall operator) {
1418+
exists(DataFlowCall constructorCall, IndirectionPosition pos |
1419+
// A construction of an object with a constructor. In this case we use
1420+
// the post-update node of the qualifier
1421+
pos.getArgumentIndex() = -1 and
1422+
isArgumentNode(creation.(PostUpdateNode).getPreUpdateNode(), constructorCall, pos) and
1423+
operator.getDeclaringType() =
1424+
constructorCall.asCallInstruction().(ConstructorCallInstruction).getConstructedType()
1425+
)
1426+
}
13861427

13871428
/** Holds if `creation` is an expression that creates a lambda of kind `kind` for `c`. */
13881429
predicate lambdaCreation(Node creation, LambdaCallKind kind, DataFlowCallable c) {
1389-
creation.asInstruction().(FunctionAddressInstruction).getFunctionSymbol() = c.asSourceCallable() and
1390-
exists(kind)
1430+
kind.isFunctionPointer() and
1431+
creation.asInstruction().(FunctionAddressInstruction).getFunctionSymbol() = c.asSourceCallable()
1432+
or
1433+
kind.isFunctor() and
1434+
exists(OperatorCall operator | operator = c.asSourceCallable() |
1435+
isFunctorCreationWithConstructor(creation, operator)
1436+
)
13911437
}
13921438

13931439
/** Holds if `call` is a lambda call of kind `kind` where `receiver` is the lambda expression. */
13941440
predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
1441+
kind.isFunctionPointer() and
13951442
(
13961443
call.(SummaryCall).getReceiver() = receiver.(FlowSummaryNode).getSummaryNode()
13971444
or
@@ -1400,8 +1447,15 @@ predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
14001447
// has a result for `getStaticCallTarget`.
14011448
not exists(call.getStaticCallTarget()) and
14021449
call.asCallInstruction().getCallTargetOperand() = receiver.asOperand()
1403-
) and
1404-
exists(kind)
1450+
)
1451+
or
1452+
kind.isFunctor() and
1453+
(
1454+
call.(SummaryCall).getReceiver() = receiver.(FlowSummaryNode).getSummaryNode()
1455+
or
1456+
not exists(call.getStaticCallTarget()) and
1457+
call.asCallInstruction().getThisArgumentOperand() = receiver.asOperand()
1458+
)
14051459
}
14061460

14071461
/** Extra data-flow steps needed for lambda flow analysis. */

cpp/ql/test/library-tests/dataflow/external-models/flow.expected

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ models
2121
| 20 | Summary: ; ; false; CreateRemoteThreadEx; ; ; Argument[@4]; Argument[3].Parameter[@0]; value; manual |
2222
| 21 | Summary: ; ; false; CreateThread; ; ; Argument[@3]; Argument[2].Parameter[@0]; value; manual |
2323
| 22 | Summary: ; ; false; ReadFileEx; ; ; Argument[*3].Field[@hEvent]; Argument[4].Parameter[*2].Field[@hEvent]; value; manual |
24-
| 23 | Summary: ; ; false; pthread_create; ; ; Argument[@3]; Argument[2].Parameter[@0]; value; manual |
25-
| 24 | Summary: ; ; false; ymlStepGenerated; ; ; Argument[0]; ReturnValue; taint; df-generated |
26-
| 25 | Summary: ; ; false; ymlStepManual; ; ; Argument[0]; ReturnValue; taint; manual |
27-
| 26 | Summary: ; ; false; ymlStepManual_with_body; ; ; Argument[0]; ReturnValue; taint; manual |
28-
| 27 | Summary: boost::asio; ; false; buffer; ; ; Argument[*0]; ReturnValue; taint; manual |
24+
| 23 | Summary: ; ; false; callWithArgument; ; ; Argument[1]; Argument[0].Parameter[0]; value; manual |
25+
| 24 | Summary: ; ; false; pthread_create; ; ; Argument[@3]; Argument[2].Parameter[@0]; value; manual |
26+
| 25 | Summary: ; ; false; ymlStepGenerated; ; ; Argument[0]; ReturnValue; taint; df-generated |
27+
| 26 | Summary: ; ; false; ymlStepManual; ; ; Argument[0]; ReturnValue; taint; manual |
28+
| 27 | Summary: ; ; false; ymlStepManual_with_body; ; ; Argument[0]; ReturnValue; taint; manual |
29+
| 28 | Summary: boost::asio; ; false; buffer; ; ; Argument[*0]; ReturnValue; taint; manual |
2930
edges
30-
| asio_streams.cpp:56:18:56:23 | [summary param] *0 in buffer | asio_streams.cpp:56:18:56:23 | [summary] to write: ReturnValue in buffer | provenance | MaD:27 |
31+
| asio_streams.cpp:56:18:56:23 | [summary param] *0 in buffer | asio_streams.cpp:56:18:56:23 | [summary] to write: ReturnValue in buffer | provenance | MaD:28 |
3132
| asio_streams.cpp:87:34:87:44 | read_until output argument | asio_streams.cpp:91:7:91:17 | recv_buffer | provenance | Src:MaD:17 |
3233
| asio_streams.cpp:87:34:87:44 | read_until output argument | asio_streams.cpp:93:29:93:39 | *recv_buffer | provenance | Src:MaD:17 Sink:MaD:2 |
3334
| asio_streams.cpp:97:37:97:44 | call to source | asio_streams.cpp:98:7:98:14 | send_str | provenance | TaintFunction |
@@ -36,10 +37,10 @@ edges
3637
| asio_streams.cpp:100:44:100:62 | call to buffer | asio_streams.cpp:101:7:101:17 | send_buffer | provenance | |
3738
| asio_streams.cpp:100:44:100:62 | call to buffer | asio_streams.cpp:103:29:103:39 | *send_buffer | provenance | Sink:MaD:2 |
3839
| asio_streams.cpp:100:64:100:71 | *send_str | asio_streams.cpp:56:18:56:23 | [summary param] *0 in buffer | provenance | |
39-
| asio_streams.cpp:100:64:100:71 | *send_str | asio_streams.cpp:100:44:100:62 | call to buffer | provenance | MaD:27 |
40-
| test.cpp:4:5:4:17 | [summary param] 0 in ymlStepManual | test.cpp:4:5:4:17 | [summary] to write: ReturnValue in ymlStepManual | provenance | MaD:25 |
41-
| test.cpp:5:5:5:20 | [summary param] 0 in ymlStepGenerated | test.cpp:5:5:5:20 | [summary] to write: ReturnValue in ymlStepGenerated | provenance | MaD:24 |
42-
| test.cpp:6:5:6:27 | [summary param] 0 in ymlStepManual_with_body | test.cpp:6:5:6:27 | [summary] to write: ReturnValue in ymlStepManual_with_body | provenance | MaD:26 |
40+
| asio_streams.cpp:100:64:100:71 | *send_str | asio_streams.cpp:100:44:100:62 | call to buffer | provenance | MaD:28 |
41+
| test.cpp:4:5:4:17 | [summary param] 0 in ymlStepManual | test.cpp:4:5:4:17 | [summary] to write: ReturnValue in ymlStepManual | provenance | MaD:26 |
42+
| test.cpp:5:5:5:20 | [summary param] 0 in ymlStepGenerated | test.cpp:5:5:5:20 | [summary] to write: ReturnValue in ymlStepGenerated | provenance | MaD:25 |
43+
| test.cpp:6:5:6:27 | [summary param] 0 in ymlStepManual_with_body | test.cpp:6:5:6:27 | [summary] to write: ReturnValue in ymlStepManual_with_body | provenance | MaD:27 |
4344
| test.cpp:7:47:7:52 | value2 | test.cpp:7:64:7:69 | value2 | provenance | |
4445
| test.cpp:7:64:7:69 | value2 | test.cpp:7:5:7:30 | *ymlStepGenerated_with_body | provenance | |
4546
| test.cpp:10:10:10:18 | call to ymlSource | test.cpp:10:10:10:18 | call to ymlSource | provenance | Src:MaD:16 |
@@ -51,28 +52,39 @@ edges
5152
| test.cpp:17:10:17:22 | call to ymlStepManual | test.cpp:17:10:17:22 | call to ymlStepManual | provenance | |
5253
| test.cpp:17:10:17:22 | call to ymlStepManual | test.cpp:18:10:18:10 | y | provenance | Sink:MaD:1 |
5354
| test.cpp:17:24:17:24 | x | test.cpp:4:5:4:17 | [summary param] 0 in ymlStepManual | provenance | |
54-
| test.cpp:17:24:17:24 | x | test.cpp:17:10:17:22 | call to ymlStepManual | provenance | MaD:25 |
55+
| test.cpp:17:24:17:24 | x | test.cpp:17:10:17:22 | call to ymlStepManual | provenance | MaD:26 |
5556
| test.cpp:21:10:21:25 | call to ymlStepGenerated | test.cpp:21:10:21:25 | call to ymlStepGenerated | provenance | |
5657
| test.cpp:21:10:21:25 | call to ymlStepGenerated | test.cpp:22:10:22:10 | z | provenance | Sink:MaD:1 |
5758
| test.cpp:21:27:21:27 | x | test.cpp:5:5:5:20 | [summary param] 0 in ymlStepGenerated | provenance | |
58-
| test.cpp:21:27:21:27 | x | test.cpp:21:10:21:25 | call to ymlStepGenerated | provenance | MaD:24 |
59+
| test.cpp:21:27:21:27 | x | test.cpp:21:10:21:25 | call to ymlStepGenerated | provenance | MaD:25 |
5960
| test.cpp:25:11:25:33 | call to ymlStepManual_with_body | test.cpp:25:11:25:33 | call to ymlStepManual_with_body | provenance | |
6061
| test.cpp:25:11:25:33 | call to ymlStepManual_with_body | test.cpp:26:10:26:11 | y2 | provenance | Sink:MaD:1 |
6162
| test.cpp:25:35:25:35 | x | test.cpp:6:5:6:27 | [summary param] 0 in ymlStepManual_with_body | provenance | |
62-
| test.cpp:25:35:25:35 | x | test.cpp:25:11:25:33 | call to ymlStepManual_with_body | provenance | MaD:26 |
63+
| test.cpp:25:35:25:35 | x | test.cpp:25:11:25:33 | call to ymlStepManual_with_body | provenance | MaD:27 |
6364
| test.cpp:32:11:32:36 | call to ymlStepGenerated_with_body | test.cpp:32:11:32:36 | call to ymlStepGenerated_with_body | provenance | |
6465
| test.cpp:32:11:32:36 | call to ymlStepGenerated_with_body | test.cpp:33:10:33:11 | z2 | provenance | Sink:MaD:1 |
6566
| test.cpp:32:41:32:41 | x | test.cpp:7:47:7:52 | value2 | provenance | |
6667
| test.cpp:32:41:32:41 | x | test.cpp:32:11:32:36 | call to ymlStepGenerated_with_body | provenance | |
6768
| test.cpp:46:30:46:32 | *arg [x] | test.cpp:47:12:47:19 | *arg [x] | provenance | |
6869
| test.cpp:47:12:47:19 | *arg [x] | test.cpp:48:13:48:13 | *s [x] | provenance | |
6970
| test.cpp:48:13:48:13 | *s [x] | test.cpp:48:16:48:16 | x | provenance | Sink:MaD:1 |
70-
| test.cpp:52:5:52:18 | [summary param] *3 in pthread_create [x] | test.cpp:52:5:52:18 | [summary] to write: Argument[2].Parameter[*0] in pthread_create [x] | provenance | MaD:23 |
71+
| test.cpp:52:5:52:18 | [summary param] *3 in pthread_create [x] | test.cpp:52:5:52:18 | [summary] to write: Argument[2].Parameter[*0] in pthread_create [x] | provenance | MaD:24 |
7172
| test.cpp:52:5:52:18 | [summary] to write: Argument[2].Parameter[*0] in pthread_create [x] | test.cpp:46:30:46:32 | *arg [x] | provenance | |
7273
| test.cpp:56:2:56:2 | *s [post update] [x] | test.cpp:59:55:59:64 | *& ... [x] | provenance | |
7374
| test.cpp:56:2:56:18 | ... = ... | test.cpp:56:2:56:2 | *s [post update] [x] | provenance | |
7475
| test.cpp:56:8:56:16 | call to ymlSource | test.cpp:56:2:56:18 | ... = ... | provenance | Src:MaD:16 |
7576
| test.cpp:59:55:59:64 | *& ... [x] | test.cpp:52:5:52:18 | [summary param] *3 in pthread_create [x] | provenance | |
77+
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | provenance | MaD:23 |
78+
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | provenance | MaD:23 |
79+
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | test.cpp:68:22:68:22 | y | provenance | |
80+
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | test.cpp:82:22:82:22 | y | provenance | |
81+
| test.cpp:68:22:68:22 | y | test.cpp:69:11:69:11 | y | provenance | Sink:MaD:1 |
82+
| test.cpp:82:22:82:22 | y | test.cpp:83:11:83:11 | y | provenance | Sink:MaD:1 |
83+
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:94:10:94:18 | call to ymlSource | provenance | Src:MaD:16 |
84+
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:97:26:97:26 | x | provenance | |
85+
| test.cpp:94:10:94:18 | call to ymlSource | test.cpp:103:63:103:63 | x | provenance | |
86+
| test.cpp:97:26:97:26 | x | test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | provenance | |
87+
| test.cpp:103:63:103:63 | x | test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | provenance | |
7688
| windows.cpp:17:8:17:25 | [summary param] *0 in CommandLineToArgvA | windows.cpp:17:8:17:25 | [summary] to write: ReturnValue[**] in CommandLineToArgvA | provenance | MaD:18 |
7789
| windows.cpp:22:15:22:29 | *call to GetCommandLineA | windows.cpp:22:15:22:29 | *call to GetCommandLineA | provenance | Src:MaD:3 |
7890
| windows.cpp:22:15:22:29 | *call to GetCommandLineA | windows.cpp:24:8:24:11 | * ... | provenance | |
@@ -209,6 +221,18 @@ nodes
209221
| test.cpp:56:2:56:18 | ... = ... | semmle.label | ... = ... |
210222
| test.cpp:56:8:56:16 | call to ymlSource | semmle.label | call to ymlSource |
211223
| test.cpp:59:55:59:64 | *& ... [x] | semmle.label | *& ... [x] |
224+
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | semmle.label | [summary param] 1 in callWithArgument |
225+
| test.cpp:63:6:63:21 | [summary param] 1 in callWithArgument | semmle.label | [summary param] 1 in callWithArgument |
226+
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | semmle.label | [summary] to write: Argument[0].Parameter[0] in callWithArgument |
227+
| test.cpp:63:6:63:21 | [summary] to write: Argument[0].Parameter[0] in callWithArgument | semmle.label | [summary] to write: Argument[0].Parameter[0] in callWithArgument |
228+
| test.cpp:68:22:68:22 | y | semmle.label | y |
229+
| test.cpp:69:11:69:11 | y | semmle.label | y |
230+
| test.cpp:82:22:82:22 | y | semmle.label | y |
231+
| test.cpp:83:11:83:11 | y | semmle.label | y |
232+
| test.cpp:94:10:94:18 | call to ymlSource | semmle.label | call to ymlSource |
233+
| test.cpp:94:10:94:18 | call to ymlSource | semmle.label | call to ymlSource |
234+
| test.cpp:97:26:97:26 | x | semmle.label | x |
235+
| test.cpp:103:63:103:63 | x | semmle.label | x |
212236
| windows.cpp:17:8:17:25 | [summary param] *0 in CommandLineToArgvA | semmle.label | [summary param] *0 in CommandLineToArgvA |
213237
| windows.cpp:17:8:17:25 | [summary] to write: ReturnValue[**] in CommandLineToArgvA | semmle.label | [summary] to write: ReturnValue[**] in CommandLineToArgvA |
214238
| windows.cpp:22:15:22:29 | *call to GetCommandLineA | semmle.label | *call to GetCommandLineA |

cpp/ql/test/library-tests/dataflow/external-models/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct StructWithOperatorCall_has_constructor {
6666
StructWithOperatorCall_has_constructor();
6767

6868
void operator()(int y) {
69-
ymlSink(y); // $ MISSING: ir
69+
ymlSink(y); // $ ir
7070
}
7171
};
7272

@@ -80,7 +80,7 @@ struct StructWithOperatorCall_has_constructor_2 {
8080
StructWithOperatorCall_has_constructor_2();
8181

8282
void operator()(int y) {
83-
ymlSink(y); // $ MISSING: ir
83+
ymlSink(y); // $ ir
8484
}
8585
};
8686

0 commit comments

Comments
 (0)