From f83f14bab2b81fa262e1896f62c84d85a5a4fb12 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Fri, 28 Mar 2025 16:31:01 +0100 Subject: [PATCH 1/3] C++: add calling convention specifier class --- cpp/ql/lib/semmle/code/cpp/Specifier.qll | 12 ++++++++++++ .../calling-convention/calling-convention.ql | 5 +++++ 2 files changed, 17 insertions(+) create mode 100644 cpp/ql/test/library-tests/calling-convention/calling-convention.ql diff --git a/cpp/ql/lib/semmle/code/cpp/Specifier.qll b/cpp/ql/lib/semmle/code/cpp/Specifier.qll index 2f1976d220c4..28ba21956561 100644 --- a/cpp/ql/lib/semmle/code/cpp/Specifier.qll +++ b/cpp/ql/lib/semmle/code/cpp/Specifier.qll @@ -97,6 +97,18 @@ class AccessSpecifier extends Specifier { override string getAPrimaryQlClass() { result = "AccessSpecifier" } } +/** + * A C/C++ calling convention specifier: `cdecl`, `fastcall`, `stdcall`, `thiscall`, + * `vectorcall`, or `clrcall`. + */ +class CallingConventionSpecifier extends Specifier { + CallingConventionSpecifier() { + this.hasName(["cdecl", "fastcall", "stdcall", "thiscall", "vectorcall", "clrcall"]) + } + + override string getAPrimaryQlClass() { result = "CallingConventionSpecifier" } +} + /** * An attribute introduced by GNU's `__attribute__((name))` syntax, * Microsoft's `__declspec(name)` syntax, Microsoft's `[name]` syntax, the diff --git a/cpp/ql/test/library-tests/calling-convention/calling-convention.ql b/cpp/ql/test/library-tests/calling-convention/calling-convention.ql new file mode 100644 index 000000000000..02e3b3af5ce0 --- /dev/null +++ b/cpp/ql/test/library-tests/calling-convention/calling-convention.ql @@ -0,0 +1,5 @@ +import cpp + +from FunctionDeclarationEntry func, CallingConventionSpecifier ccs +where ccs.hasName(func.getASpecifier()) +select func, func.getASpecifier() From 9ec7f3c9a5587398a5796dd530f7fca2f1c79ed8 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Mon, 31 Mar 2025 11:57:32 +0200 Subject: [PATCH 2/3] C++: add test for calling conventions --- .../calling-convention.expected | 7 +++++++ .../library-tests/calling-convention/test.cpp | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 cpp/ql/test/library-tests/calling-convention/calling-convention.expected create mode 100644 cpp/ql/test/library-tests/calling-convention/test.cpp diff --git a/cpp/ql/test/library-tests/calling-convention/calling-convention.expected b/cpp/ql/test/library-tests/calling-convention/calling-convention.expected new file mode 100644 index 000000000000..a2dd41066169 --- /dev/null +++ b/cpp/ql/test/library-tests/calling-convention/calling-convention.expected @@ -0,0 +1,7 @@ +| test.cpp:4:21:4:35 | definition of thiscall_method | thiscall | +| test.cpp:7:14:7:23 | definition of func_cdecl | cdecl | +| test.cpp:9:16:9:27 | definition of func_stdcall | stdcall | +| test.cpp:11:17:11:29 | definition of func_fastcall | fastcall | +| test.cpp:13:20:13:34 | definition of func_vectorcall | vectorcall | +| test.cpp:15:13:15:25 | definition of func_overload | cdecl | +| test.cpp:16:15:16:27 | definition of func_overload | stdcall | diff --git a/cpp/ql/test/library-tests/calling-convention/test.cpp b/cpp/ql/test/library-tests/calling-convention/test.cpp new file mode 100644 index 000000000000..982c3c0caea9 --- /dev/null +++ b/cpp/ql/test/library-tests/calling-convention/test.cpp @@ -0,0 +1,16 @@ +// semmle-extractor-options: --microsoft + +struct call_conventions { + void __thiscall thiscall_method() {} +}; + +void __cdecl func_cdecl() {} + +void __stdcall func_stdcall() {} + +void __fastcall func_fastcall() {} + +void __vectorcall func_vectorcall() {} + +int __cdecl func_overload() {} +int __stdcall func_overload(int x) {} From d61d9730c8a304170baf9e52c84d03b08d1bf5c3 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Mon, 31 Mar 2025 11:58:45 +0200 Subject: [PATCH 3/3] C++: add change note for calling conventions --- cpp/ql/lib/change-notes/2025-03-31-calling-convention.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 cpp/ql/lib/change-notes/2025-03-31-calling-convention.md diff --git a/cpp/ql/lib/change-notes/2025-03-31-calling-convention.md b/cpp/ql/lib/change-notes/2025-03-31-calling-convention.md new file mode 100644 index 000000000000..12d9547eb035 --- /dev/null +++ b/cpp/ql/lib/change-notes/2025-03-31-calling-convention.md @@ -0,0 +1,5 @@ +--- +category: feature +--- +* Calling conventions explicitly specified on function declarations (`__cdecl`, `__stdcall`, `__fastcall`, etc.) are now represented as specifiers of those declarations. +* A new class `CallingConventionSpecifier` extending the `Specifier` class was introduced, which represents explicitly specified calling conventions.