Skip to content

Commit 0be3663

Browse files
committed
C++: Add tests with missing flow.
In the upcoming commits we will add a new extensional predicate which allows us to model that a function forwards it arguments to the constructor of a given type. This initial commit adds the test YAML models for this new extensional predicate.
1 parent f7caf55 commit 0be3663

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎cpp/ql/test/library-tests/dataflow/external-models/flow.ext.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,16 @@ extensions:
4242
- ["", "ReverseFlow", True, "get_ptr", "", "", "ReturnValue[*]", "Argument[-1].Field[ReverseFlow::value]", "value", "manual"]
4343
- ["", "MyString", True, "operator[]", "", "", "ReturnValue[*]", "Argument[-1]", "taint", "manual"]
4444
- ["", "MyString", True, "operator[]", "", "", "Argument[-1]", "ReturnValue[*]", "taint", "manual"]
45+
- ["", "Forwarder", True, "forward", "", "", "Argument[*0]", "Argument[forward].Parameter[0]", "value", "manual"]
46+
- ["", "Forwarder", True, "forward", "", "", "Argument[forward].Parameter[-1]", "Argument[-1]", "value", "manual"]
47+
- ["", "Forwarder", True, "get", "", "", "Argument[-1]", "ReturnValue", "value", "manual"]
48+
- ["", "Container", True, "emplace", "", "", "Argument[*0]", "Argument[forward].Parameter[0]", "value", "manual"]
49+
- ["", "Container", True, "emplace", "", "", "Argument[forward].Parameter[-1]", "Argument[-1].Element", "value", "manual"]
50+
- ["", "Container", True, "get", "", "", "Argument[-1].Element", "ReturnValue[*]", "value", "manual"]
51+
- ["", "Element", True, "Element", "", "", "Argument[0]", "Argument[-1].Field[Element::x]", "value", "manual"]
52+
- addsTo:
53+
pack: codeql/cpp-all
54+
extensible: forwardsModel
55+
data: # namespace, type, subtypes, name, signature, ext, start, constructor, provenance
56+
- ["", "Forwarder<T>", True, "forward<Args>", "(Args &&)", "", "0", "T", "manual"]
57+
- ["", "Container<T>", True, "emplace<Args>", "(Args &&)", "", "0", "T", "manual"]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@
4343
| test.cpp:331:10:331:19 | * ... | test-sink |
4444
| test.cpp:333:15:333:20 | source | test-sink |
4545
| test.cpp:334:10:334:16 | * ... | test-sink |
46+
| test.cpp:364:13:364:13 | s | test-sink |
47+
| test.cpp:365:13:365:14 | ul | test-sink |
48+
| test.cpp:373:13:373:13 | s | test-sink |
49+
| test.cpp:374:13:374:14 | ul | test-sink |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
| test.cpp:222:10:222:18 | call to ymlSource | local |
2020
| test.cpp:297:33:297:41 | call to ymlSource | local |
2121
| test.cpp:317:51:317:59 | call to ymlSource | local |
22+
| test.cpp:360:13:360:21 | call to ymlSource | local |
23+
| test.cpp:369:22:369:30 | call to ymlSource | local |
2224
| windows.cpp:22:15:22:29 | call to GetCommandLineA | local |
2325
| windows.cpp:34:17:34:38 | call to GetEnvironmentStringsA | local |
2426
| windows.cpp:39:36:39:38 | GetEnvironmentVariableA output argument | local |

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,68 @@ void test_parameter(SourceWrapper* p, SourceWrapper s, int* source) {
332332

333333
ymlSink((int)source); // clean
334334
ymlSink(*source); // $ ir
335+
}
336+
337+
338+
struct ConstructableFromInt {
339+
short s;
340+
unsigned long ul;
341+
ConstructableFromInt(short arg) {
342+
this->s = arg;
343+
}
344+
345+
ConstructableFromInt(unsigned long arg) {
346+
this->ul = arg;
347+
}
348+
};
349+
350+
template<typename T>
351+
struct Forwarder {
352+
template<typename... Args>
353+
void forward(Args&&... args);
354+
355+
T get();
356+
};
357+
358+
void forward_test() {
359+
{
360+
Forwarder<ConstructableFromInt> f;
361+
short x = ymlSource();
362+
f.forward(x);
363+
364+
ConstructableFromInt c = f.get();
365+
ymlSink(c.s); // $ MISSING: ir
366+
ymlSink(c.ul); // clean
367+
}
368+
{
369+
Forwarder<ConstructableFromInt> f;
370+
unsigned long ul = ymlSource();
371+
f.forward(ul);
372+
373+
ConstructableFromInt c = f.get();
374+
ymlSink(c.s); // clean
375+
ymlSink(c.ul); // $ MISSING: ir
376+
}
377+
}
378+
379+
template<typename T>
380+
struct Container {
381+
template<typename... Args>
382+
void emplace(Args&&... args);
383+
384+
T& get();
385+
};
386+
387+
struct Element {
388+
int x;
389+
Element(int);
390+
};
391+
392+
void forward_test_model() {
393+
Container<Element> c;
394+
int x = ymlSource();
395+
c.emplace(x);
396+
397+
Element e = c.get();
398+
ymlSink(e.x); // $ MISSING: ir
335399
}

0 commit comments

Comments
 (0)