Skip to content

Commit b361b0f

Browse files
authored
Merge pull request #20310 from MathiasVP/add-comptr-model-now-with-more-models
C++: Add `ComPtr` conversion operators
2 parents 45d6fc0 + b8917c8 commit b361b0f

File tree

7 files changed

+387
-154
lines changed

7 files changed

+387
-154
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added flow summaries for the `Microsoft::WRL::ComPtr` member functions.

cpp/ql/lib/ext/ComPtr.model.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ extensions:
1313
- ["Microsoft::WRL", "ComPtr<T>", True, "CopyTo", "(T **)", "", "Argument[-1].Element[@]", "Argument[**@0]", "value", "manual"]
1414
- ["Microsoft::WRL", "ComPtr", True, "CopyTo<T>", "(T **)", "", "Argument[-1].Element[@]", "Argument[**@0]", "value", "manual"]
1515
- ["Microsoft::WRL", "ComPtr", True, "CopyTo", "(REFIID,void **)", "", "Argument[-1].Element[@]", "Argument[**@1]", "value", "manual"]
16-
- ["Microsoft::WRL", "ComPtr", True, "Detach", "", "", "Argument[-1].Element[@]", "ReturnValue[*]", "value", "manual"]
17-
- ["Microsoft::WRL", "ComPtr", True, "Get", "", "", "Argument[-1].Element[@]", "ReturnValue[*]", "value", "manual"]
18-
- ["Microsoft::WRL", "ComPtr", True, "GetAddressOf", "", "", "Argument[-1].Element[@]", "ReturnValue[**]", "value", "manual"]
19-
- ["Microsoft::WRL", "ComPtr", True, "ReleaseAndGetAddressOf", "", "", "Argument[-1].Element[@]", "ReturnValue[**]", "value", "manual"]
16+
- ["Microsoft::WRL", "ComPtr", True, "Detach", "", "", "Argument[-1].Element[@]", "ReturnValue[*@]", "value", "manual"]
17+
- ["Microsoft::WRL", "ComPtr", True, "Get", "", "", "Argument[-1].Element[@]", "ReturnValue[*@]", "value", "manual"]
18+
- ["Microsoft::WRL", "ComPtr", True, "GetAddressOf", "", "", "Argument[-1].Element[@]", "ReturnValue[**@]", "value", "manual"]
19+
- ["Microsoft::WRL", "ComPtr", True, "ReleaseAndGetAddressOf", "", "", "Argument[-1].Element[@]", "ReturnValue[**@]", "value", "manual"]
2020
- ["Microsoft::WRL", "ComPtr", True, "Swap", "", "", "Argument[-1]", "Argument[*0]", "value", "manual"]
2121
- ["Microsoft::WRL", "ComPtr", True, "Swap", "", "", "Argument[*0]", "Argument[-1]", "value", "manual"]
22+
- ["Microsoft::WRL", "ComPtr", True, "operator&", "", "", "Argument[-1]", "ReturnValue.Element", "value", "manual"]
23+
- ["Microsoft::WRL", "ComPtr", True, "operator->", "", "", "Argument[-1].Element[@]", "ReturnValue[*@]", "value", "manual"]
24+
- ["Microsoft::WRL", "ComPtr<T>", True, "operator=", "(T *)", "", "Argument[*@0]", "Argument[-1].Element[@]", "value", "manual"]
25+
- ["Microsoft::WRL", "ComPtr<T>", True, "operator=", "(T *)", "", "Argument[*@0]", "ReturnValue[*].Element[@]", "value", "manual"]
26+
- ["Microsoft::WRL", "ComPtr", True, "operator=<U>", "(U *)", "", "Argument[*@0]", "Argument[-1].Element[@]", "value", "manual"]
27+
- ["Microsoft::WRL", "ComPtr", True, "operator=<U>", "(U *)", "", "Argument[*@0]", "ReturnValue[*].Element[@]", "value", "manual"]
28+
- ["Microsoft::WRL", "ComPtr", True, "operator=", "(const ComPtr &)", "", "Argument[*0]", "Argument[-1]", "value", "manual"]
29+
- ["Microsoft::WRL", "ComPtr", True, "operator=", "(const ComPtr &)", "", "Argument[*0]", "ReturnValue[*]", "value", "manual"]
30+
- ["Microsoft::WRL", "ComPtr", True, "operator=", "(ComPtr &&)", "", "Argument[*0]", "Argument[-1]", "value", "manual"]
31+
- ["Microsoft::WRL", "ComPtr", True, "operator=", "(ComPtr &&)", "", "Argument[*0]", "ReturnValue[*]", "value", "manual"]

cpp/ql/lib/ext/ComPtrRef.model.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/cpp-all
4+
extensible: summaryModel
5+
data: # namespace, type, subtypes, name, signature, ext, input, output, kind, provenance
6+
- ["Microsoft::WRL::Details", "ComPtrRef", True, "ComPtrRef", "", "", "Argument[*0]", "Argument[-1].Element[@]", "value", "manual"]
7+
- ["Microsoft::WRL::Details", "ComPtrRef", True, "GetAddressOf", "", "", "Argument[-1].Element[@]", "ReturnValue[*@]", "value", "manual"]
8+
# TODO: We cannot yet model https://learn.microsoft.com/en-us/cpp/cppcx/wrl/comptrref-class?view=msvc-170#operator-interfacetype-star-star
9+
- ["Microsoft::WRL::Details", "ComPtrRef", True, "operator*", "", "", "Argument[-1].Element[@]", "ReturnValue[*@]", "value", "manual"]
10+
# TODO: We cannot yet model https://learn.microsoft.com/en-us/cpp/cppcx/wrl/comptrref-class?view=msvc-170#operator-t-star
11+
- ["Microsoft::WRL::Details", "ComPtrRef", True, "operator void**", "", "", "Argument[-1].Element[@]", "ReturnValue[**@]", "value", "manual"]
12+
- ["Microsoft::WRL::Details", "ComPtrRef", True, "ReleaseAndGetAddressOf", "", "", "Argument[-1].Element[@]", "ReturnValue[**@]", "value", "manual"]

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
| Dubious member name "operator LPWSTR" in summary model. |
77
| Dubious member name "operator PCXSTR" in summary model. |
88
| Dubious member name "operator PXSTR" in summary model. |
9+
| Dubious member name "operator void**" in summary model. |
910
| Dubious member name "operator&" in summary model. |
1011
| Dubious member name "operator*" in summary model. |
1112
| Dubious member name "operator+" in summary model. |
1213
| Dubious member name "operator+=" in summary model. |
1314
| Dubious member name "operator->" in summary model. |
1415
| Dubious member name "operator=" in summary model. |
16+
| Dubious member name "operator=<U>" in summary model. |
1517
| Dubious member name "operator[]" in summary model. |
1618
| Dubious signature "(..(*)(..))" in summary model. |
1719
| Dubious signature "(..(*)(..),..(*)(..),..(*)(..),..(*)(..))" in summary model. |
@@ -2497,6 +2499,7 @@
24972499
| Dubious signature "(TS_VERIFY_CTX *,unsigned char *,long)" in summary model. |
24982500
| Dubious signature "(TXT_DB *,OPENSSL_STRING *)" in summary model. |
24992501
| Dubious signature "(TXT_DB *,int,..(*)(..),OPENSSL_LH_HASHFUNC,OPENSSL_LH_COMPFUNC)" in summary model. |
2502+
| Dubious signature "(U *)" in summary model. |
25002503
| Dubious signature "(UI *)" in summary model. |
25012504
| Dubious signature "(UI *,UI_STRING *,const char *)" in summary model. |
25022505
| Dubious signature "(UI *,UI_STRING *,const char *,int)" in summary model. |

cpp/ql/test/library-tests/dataflow/taint-tests/atl.cpp

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,24 @@ namespace Microsoft {
12581258

12591259
class WeakRef;
12601260

1261+
namespace Details {
1262+
template <typename T>
1263+
class ComPtrRef {
1264+
public:
1265+
using InterfaceType = T;
1266+
1267+
ComPtrRef(T*);
1268+
1269+
InterfaceType* const * GetAddressOf() const;
1270+
InterfaceType** ReleaseAndGetAddressOf();
1271+
1272+
operator InterfaceType**();
1273+
operator T*();
1274+
operator void**() const;
1275+
InterfaceType* operator *();
1276+
};
1277+
}
1278+
12611279
template <typename T>
12621280
class ComPtr
12631281
{
@@ -1301,7 +1319,22 @@ namespace Microsoft {
13011319
void Swap(ComPtr &&r);
13021320

13031321
void Swap(ComPtr &r);
1304-
};
1322+
1323+
Details::ComPtrRef<ComPtr<T>> operator&();
1324+
const Details::ComPtrRef<const ComPtr<T>> operator&() const;
1325+
1326+
InterfaceType* operator->() const; // return type simplified from Microsoft::WRL::Details::RemoveIUnknown<InterfaceType>*
1327+
1328+
ComPtr& operator=(T *);
1329+
template <typename U>
1330+
ComPtr& operator=(U *);
1331+
ComPtr& operator=(const ComPtr &);
1332+
template<class U>
1333+
ComPtr& operator=(const ComPtr<U>&);
1334+
ComPtr& operator=(ComPtr &&);
1335+
template<class U>
1336+
ComPtr& operator=(ComPtr<U>&&);
1337+
};
13051338

13061339
}
13071340
}
@@ -1331,9 +1364,9 @@ void test_As()
13311364
{
13321365
int x = source<int>();
13331366
Microsoft::WRL::ComPtr<int> p1(new int(x));
1334-
Microsoft::WRL::ComPtr<int> p2;
1335-
p1.As(&p2);
1336-
sink(*p2.Get()); // $ ir MISSING: ast
1367+
Microsoft::WRL::ComPtr<int>* p2;
1368+
p1.As(p2);
1369+
sink(*p2->Get()); // $ ir MISSING: ast
13371370
}
13381371

13391372
void test_CopyTo()
@@ -1377,4 +1410,52 @@ void test_GetAddressOf()
13771410
Microsoft::WRL::ComPtr<int> p3(new int(x));
13781411
int **pp = p3.ReleaseAndGetAddressOf();
13791412
sink(**pp); // $ ir MISSING: ast
1413+
}
1414+
1415+
struct S {
1416+
int x;
1417+
};
1418+
1419+
void test_address_of_deref_operators() {
1420+
int x = source<int>();
1421+
Microsoft::WRL::ComPtr<int> p1(new int(x));
1422+
Microsoft::WRL::Details::ComPtrRef<Microsoft::WRL::ComPtr<int>> pp = &p1;
1423+
Microsoft::WRL::ComPtr<int>* qq = *pp;
1424+
sink(*qq->Get()); // $ ir MISSING: ast
1425+
1426+
const Microsoft::WRL::ComPtr<int> p2(new int(x));
1427+
Microsoft::WRL::Details::ComPtrRef<const Microsoft::WRL::ComPtr<int>> pp2 = &p2;
1428+
const Microsoft::WRL::ComPtr<int>* qq2 = *pp2;
1429+
sink(*qq2->Get()); // $ ir MISSING: ast
1430+
1431+
S s;
1432+
s.x = source<int>();
1433+
Microsoft::WRL::ComPtr<S> p3(&s);
1434+
sink(p3->x); // $ ir MISSING: ast
1435+
}
1436+
1437+
void test_assignments() {
1438+
Microsoft::WRL::ComPtr<int> p1;
1439+
p1 = new int(source<int>());
1440+
sink(*p1.Get()); // $ ir MISSING: ast
1441+
1442+
Microsoft::WRL::ComPtr<int> p2;
1443+
p2 = new long(source<long>());
1444+
sink(*p2.Get()); // $ ir MISSING: ast
1445+
1446+
Microsoft::WRL::ComPtr<int> p3;
1447+
p3 = p1;
1448+
sink(*p3.Get()); // $ ir MISSING: ast
1449+
1450+
Microsoft::WRL::ComPtr<long> p4;
1451+
p4 = p1;
1452+
sink(*p4.Get()); // $ ir MISSING: ast
1453+
1454+
Microsoft::WRL::ComPtr<int> p5;
1455+
p5 = std::move(p1);
1456+
sink(*p5.Get()); // $ ir MISSING: ast
1457+
1458+
Microsoft::WRL::ComPtr<long> p6;
1459+
p6 = std::move(p1);
1460+
sink(*p6.Get()); // $ ir MISSING: ast
13801461
}

0 commit comments

Comments
 (0)