Skip to contentSkip to conversation

plugins: enable C++ plugins

PATCH Superseded Pierrick Bouvier 2 participants 16 messages Jan 19 – Jan 24 Arm/Amlogic Meson SoC support

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover plugins: enable C++ plugins

Writing plugins in C can be sometimes tedious, especially when using Glib to keep track of execution state. We can directly use the same C API but write our plugin in C++, benefiting from its great standard library offering strings, smart pointers, data structures and synchronization mechanisms.

It's common for downstream QEMU forks to provide C++ for plugins, like this:
- https://github.com/panda-re/panda/tree/dev/panda/plugins
- https://github.com/FlorentRevest/DejaView/tree/main/src/qemu_plugin

Hopefully this will help more people to use upstream QEMU, and as a benefit, get their contribution back and help to develop plugins ecosystem upstream directly.

This series first cleans up build system for plugins, factorizing details between contrib/plugins and tests/tcg/plugins folders.
Then, we perform codebase cleanups to fix conflicts between existing headers and C++ headers.
After that, we can update the C++ standard used by QEMU, to benefit fully from latest updates of the language.
Finally, we define an empty C++ plugin, making sure we can keep track of possible regression in qemu-plugin header.

Note: This series is *not* a trojan horse to bring C++ in QEMU codebase, nor to define an alternative C++ API for plugins. It's just enabling more users to get the most out of existing C plugin API.

CI: https://gitlab.com/pbo-linaro/qemu/-/pipelines/2242427013

v3
--

- fix indentation in patch 2

v2
--

- drop coroutine.h rename patch as it's not needed
- drop ctype.h rename patch, and move qemu-plugin.h to include/plugins
- fix mem.c to not depend on other QEMU headers

Pierrick Bouvier (11):
plugins: move win32_linker.c file to plugins directory
plugins: factorize plugin dependencies and library details
plugins: use complete filename for defining plugins sources
plugins: define plugin API symbols as extern "C" when compiling in C++ tests/tcg/plugins/mem.c: remove dependency on qemu headers
plugins: move qemu-plugin.h to include/plugins/
meson: fix supported compiler arguments in other languages than C
meson: enable cpp (optionally) for plugins
qga/vss-win32: fix clang warning with C++20
meson: update C++ standard to C++23
contrib/plugins: add empty cpp plugin

 docs/devel/tcg-plugins.rst                  |   4 +-
 meson.build                                 |  26 +++--
 include/{qemu => plugins}/qemu-plugin.h     |  11 +-
 include/qemu/plugin.h                       |   2 +-
 plugins/core.c                              |   2 +-
 {contrib/plugins => plugins}/win32_linker.c |   0
 tests/tcg/plugins/mem.c                     |  59 ++++------
 contrib/plugins/cpp.cpp                     | 119 ++++++++++++++++++++
 contrib/plugins/meson.build                 |  25 ++--
 plugins/meson.build                         |  17 ++-
 qga/vss-win32/requester.cpp                 |   6 +-
 scripts/clean-includes                      |   2 +-
 tests/tcg/plugins/meson.build               |  18 +--
 13 files changed, 204 insertions(+), 87 deletions(-)
 rename include/{qemu => plugins}/qemu-plugin.h (99%)
 rename {contrib/plugins => plugins}/win32_linker.c (100%)
 create mode 100644 contrib/plugins/cpp.cpp
Signature · 1 line
2.47.3
#1 lore raw

PATCH v3 1/11 plugins: move win32_linker.c file to plugins directory

---
 {contrib/plugins => plugins}/win32_linker.c | 0
 contrib/plugins/meson.build                 | 2 +-
 tests/tcg/plugins/meson.build               | 2 +-
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename {contrib/plugins => plugins}/win32_linker.c (100%)
plugins/win32_linker.c renamed from contrib/plugins/win32_linker.c +0 -0
similarity index 100%
rename from contrib/plugins/win32_linker.c
rename to plugins/win32_linker.c
contrib/plugins/meson.build +1 -1
index eb944b5159a..6f72b2ce0c9 100644
@@ -10,7 +10,7 @@ t = []
 if get_option('plugins')
   foreach i : contrib_plugins
     if host_os == 'windows'
-      t += shared_module(i, files(i + '.c') + 'win32_linker.c',
+      t += shared_module(i, files(i + '.c') + '../../plugins/win32_linker.c',
                         include_directories: '../../include/qemu',
                         link_depends: [win32_qemu_plugin_api_lib],
                         link_args: win32_qemu_plugin_api_link_flags,
tests/tcg/plugins/meson.build +1 -1
index 561584159eb..a6e78438510 100644
@@ -2,7 +2,7 @@ t = []
 if get_option('plugins')
   foreach i : ['bb', 'discons', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall', 'patch']
     if host_os == 'windows'
-      t += shared_module(i, files(i + '.c') + '../../../contrib/plugins/win32_linker.c',
+      t += shared_module(i, files(i + '.c') + '../../../plugins/win32_linker.c',
                         include_directories: '../../../include/qemu',
                         link_depends: [win32_qemu_plugin_api_lib],
                         link_args: win32_qemu_plugin_api_link_flags,
Signature · 1 line
2.47.3
Reviewed-by on v3 1/11
#2 lore raw

Re: [PATCH v3 01/11] plugins: move win32_linker.c file to plugins directory

Quoted 9 lines · Pierrick Bouvier
---
  {contrib/plugins => plugins}/win32_linker.c | 0
  contrib/plugins/meson.build                 | 2 +-
  tests/tcg/plugins/meson.build               | 2 +-
  3 files changed, 2 insertions(+), 2 deletions(-)

rename {contrib/plugins => plugins}/win32_linker.c (100%)

#3 lore raw

PATCH v3 2/11 plugins: factorize plugin dependencies and library details

In reply to Pierrick Bouvier
---
 meson.build                   |  2 +-
 contrib/plugins/meson.build   | 13 ++-----------
 plugins/meson.build           | 15 ++++++++++++++-
 tests/tcg/plugins/meson.build | 13 ++-----------
 4 files changed, 19 insertions(+), 24 deletions(-)
meson.build +1 -1
index 3108f01e887..6f02547df9b 100644
@@ -4447,7 +4447,7 @@ if get_option('plugins')
   if host_os == 'windows'
     # On windows, we want to deliver the qemu_plugin_api.lib file in the qemu installer,
     # so that plugin authors can compile against it.
-    install_data(win32_qemu_plugin_api_lib, install_dir: 'lib')
+    install_data(win32_qemu_plugin_api, install_dir: 'lib')
   endif
 endif
 
contrib/plugins/meson.build +2 -11
index 6f72b2ce0c9..6915ffa5fbc 100644
@@ -9,17 +9,8 @@ endif
 t = []
 if get_option('plugins')
   foreach i : contrib_plugins
-    if host_os == 'windows'
-      t += shared_module(i, files(i + '.c') + '../../plugins/win32_linker.c',
-                        include_directories: '../../include/qemu',
-                        link_depends: [win32_qemu_plugin_api_lib],
-                        link_args: win32_qemu_plugin_api_link_flags,
-                        dependencies: glib)
-    else
-      t += shared_module(i, files(i + '.c'),
-                        include_directories: '../../include/qemu',
-                        dependencies: glib)
-    endif
+    t += shared_module(i, files(i + '.c'),
+                       dependencies: plugins_deps)
   endforeach
 endif
 if t.length() > 0
plugins/meson.build +14 -1
index 62c991d87fc..4318e3a1671 100644
@@ -51,11 +51,24 @@ if host_os == 'windows'
     dlltool_cmd = [dlltool, '--input-def', '@INPUT@',
                    '--output-delaylib', '@OUTPUT@', '--dllname', 'qemu.exe']
   endif
-  win32_qemu_plugin_api_lib = configure_file(
+  win32_qemu_plugin_api = configure_file(
     input: win32_plugin_def,
     output: 'libqemu_plugin_api.a',
     command: dlltool_cmd
   )
+  win32_qemu_plugin_api_lib = static_library('win32_qemu_plugin_api',
+                                             link_depends: win32_qemu_plugin_api)
+endif
+
+if host_os == 'windows'
+  plugins_deps = declare_dependency(sources: [files('win32_linker.c')],
+                                    include_directories: '../include/qemu',
+                                    link_with: win32_qemu_plugin_api_lib,
+                                    link_args: win32_qemu_plugin_api_link_flags,
+                                    dependencies: glib)
+else
+  plugins_deps = declare_dependency(include_directories: '../include/qemu',
+                                    dependencies: glib)
 endif
 
 user_ss.add(files('user.c', 'api-user.c'))
tests/tcg/plugins/meson.build +2 -11
index a6e78438510..d7823704616 100644
@@ -1,17 +1,8 @@
 t = []
 if get_option('plugins')
   foreach i : ['bb', 'discons', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall', 'patch']
-    if host_os == 'windows'
-      t += shared_module(i, files(i + '.c') + '../../../plugins/win32_linker.c',
-                        include_directories: '../../../include/qemu',
-                        link_depends: [win32_qemu_plugin_api_lib],
-                        link_args: win32_qemu_plugin_api_link_flags,
-                        dependencies: glib)
-    else
-      t += shared_module(i, files(i + '.c'),
-                        include_directories: '../../../include/qemu',
-                        dependencies: glib)
-    endif
+    t += shared_module(i, files(i + '.c'),
+                       dependencies: plugins_deps)
   endforeach
 endif
 if t.length() > 0
Signature · 1 line
2.47.3
#4 lore raw

PATCH v3 3/11 plugins: use complete filename for defining plugins sources

In reply to Pierrick Bouvier
---
 contrib/plugins/meson.build   | 10 +++++-----
 tests/tcg/plugins/meson.build |  7 +++++--
 2 files changed, 10 insertions(+), 7 deletions(-)
contrib/plugins/meson.build +5 -5
index 6915ffa5fbc..3d2d7862e0c 100644
@@ -1,15 +1,15 @@
-contrib_plugins = ['bbv', 'cache', 'cflow', 'drcov', 'execlog', 'hotblocks',
-                   'hotpages', 'howvec', 'hwprofile', 'ips', 'stoptrigger',
-                   'traps', 'uftrace']
+contrib_plugins = ['bbv.c', 'cache.c', 'cflow.c', 'drcov.c', 'execlog.c',
+                   'hotblocks.c', 'hotpages.c', 'howvec.c', 'hwprofile.c',
+                   'ips.c', 'stoptrigger.c', 'traps.c', 'uftrace.c']
 if host_os != 'windows'
   # lockstep uses socket.h
-  contrib_plugins += 'lockstep'
+  contrib_plugins += 'lockstep.c'
 endif
 
 t = []
 if get_option('plugins')
   foreach i : contrib_plugins
-    t += shared_module(i, files(i + '.c'),
+    t += shared_module(fs.stem(i), files(i),
                        dependencies: plugins_deps)
   endforeach
 endif
tests/tcg/plugins/meson.build +5 -2
index d7823704616..303f97f9679 100644
@@ -1,7 +1,10 @@
+test_plugins = ['bb.c', 'discons.c', 'empty.c', 'inline.c', 'insn.c', 'mem.c',
+                'reset.c', 'syscall.c', 'patch.c']
+
 t = []
 if get_option('plugins')
-  foreach i : ['bb', 'discons', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall', 'patch']
-    t += shared_module(i, files(i + '.c'),
+  foreach i : test_plugins
+    t += shared_module(fs.stem(i), files(i),
                        dependencies: plugins_deps)
   endforeach
 endif
Signature · 1 line
2.47.3
#5 lore raw

PATCH v3 4/11 plugins: define plugin API symbols as extern "C" when compiling in C++

In reply to Pierrick Bouvier
---
 include/qemu/qemu-plugin.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/qemu/qemu-plugin.h +8 -0
index 60de4fdd3fa..e44f863d839 100644
@@ -16,6 +16,10 @@
 #include <stdbool.h>
 #include <stddef.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * For best performance, build the plugin with -fvisibility=hidden so that
  * QEMU_PLUGIN_LOCAL is implicit. Then, just mark qemu_plugin_install with
@@ -1210,4 +1214,8 @@ void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,
 QEMU_PLUGIN_API
 uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry);
 
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
 #endif /* QEMU_QEMU_PLUGIN_H */
Signature · 1 line
2.47.3
#6 lore raw

PATCH v3 5/11 tests/tcg/plugins/mem.c: remove dependency on qemu headers

In reply to Pierrick Bouvier

This plugin uses endianness conversion primitives from QEMU headers. As next commit will strongly isolate plugins code from those headers, those primitives can't be used anymore.

glib.h provides such primitives:
https://docs.gtk.org/glib/conversion-macros.html#byte-order-conversion

---
 tests/tcg/plugins/mem.c | 59 ++++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 36 deletions(-)
tests/tcg/plugins/mem.c +23 -36
index 9649bce99ca..7d64e7018f2 100644
@@ -12,16 +12,7 @@
 #include <stdio.h>
 #include <glib.h>
 
-/*
- * plugins should not include anything from QEMU aside from the
- * API header. However as this is a test plugin to exercise the
- * internals of QEMU and we want to avoid needless code duplication we
- * do so here. bswap.h is pretty self-contained although it needs a
- * few things provided by compiler.h.
- */
-#include <compiler.h>
 #include <stdbool.h>
-#include <bswap.h>
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -152,56 +143,52 @@ static void update_region_info(uint64_t region, uint64_t offset,
         ri->reads++;
     }
 
+    void *ri_data = &ri->data[offset];
     switch (value.type) {
     case QEMU_PLUGIN_MEM_VALUE_U8:
+    {
+        uint8_t val = value.data.u8;
+        uint8_t *p = ri_data;
         if (is_store) {
-            ri->data[offset] = value.data.u8;
-        } else if (ri->data[offset] != value.data.u8) {
-            unseen_data = true;
+            *p = val;
+        } else {
+            unseen_data = *p != val;
         }
         break;
+    }
     case QEMU_PLUGIN_MEM_VALUE_U16:
     {
-        uint16_t *p = (uint16_t *) &ri->data[offset];
+        uint16_t val = be ? GUINT16_FROM_BE(value.data.u16) :
+                            GUINT16_FROM_LE(value.data.u16);
+        uint16_t *p = ri_data;
         if (is_store) {
-            if (be) {
-                stw_be_p(p, value.data.u16);
-            } else {
-                stw_le_p(p, value.data.u16);
-            }
+            *p = val;
         } else {
-            uint16_t val = be ? lduw_be_p(p) : lduw_le_p(p);
-            unseen_data = val != value.data.u16;
+            unseen_data = *p != val;
         }
         break;
     }
     case QEMU_PLUGIN_MEM_VALUE_U32:
     {
-        uint32_t *p = (uint32_t *) &ri->data[offset];
+        uint32_t val = be ? GUINT32_FROM_BE(value.data.u32) :
+                            GUINT32_FROM_LE(value.data.u32);
+        uint32_t *p = ri_data;
         if (is_store) {
-            if (be) {
-                stl_be_p(p, value.data.u32);
-            } else {
-                stl_le_p(p, value.data.u32);
-            }
+            *p = val;
         } else {
-            uint32_t val = be ? ldl_be_p(p) : ldl_le_p(p);
-            unseen_data = val != value.data.u32;
+            unseen_data = *p != val;
         }
         break;
     }
     case QEMU_PLUGIN_MEM_VALUE_U64:
     {
-        uint64_t *p = (uint64_t *) &ri->data[offset];
+        uint64_t val = be ? GUINT64_FROM_BE(value.data.u64) :
+                            GUINT64_FROM_LE(value.data.u64);
+        uint64_t *p = ri_data;
         if (is_store) {
-            if (be) {
-                stq_be_p(p, value.data.u64);
-            } else {
-                stq_le_p(p, value.data.u64);
-            }
+            *p = val;
         } else {
-            uint64_t val = be ? ldq_be_p(p) : ldq_le_p(p);
-            unseen_data = val != value.data.u64;
+            unseen_data = *p != val;
         }
         break;
     }
Signature · 1 line
2.47.3
#7 lore raw

PATCH v3 6/11 plugins: move qemu-plugin.h to include/plugins/

In reply to Pierrick Bouvier

This change has two benefits:
- ensure plugins can't include anything else from QEMU than plugins API
- when compiling a C++ module, solves the header conflict with iostream header that includes transitively the wrong ctype.h, which already exists in include/qemu.

By Hyrum's law, there was already one usage of other headers with mem plugin, which has been eliminated in previous commit.

---
 docs/devel/tcg-plugins.rst              | 4 ++--
 meson.build                             | 2 +-
 include/{qemu => plugins}/qemu-plugin.h | 3 ---
 include/qemu/plugin.h                   | 2 +-
 plugins/core.c                          | 2 +-
 plugins/meson.build                     | 6 +++---
 scripts/clean-includes                  | 2 +-
 7 files changed, 9 insertions(+), 12 deletions(-)
 rename include/{qemu => plugins}/qemu-plugin.h (99%)
docs/devel/tcg-plugins.rst +2 -2
index 9463692c411..f48c32bd844 100644
@@ -166,7 +166,7 @@ Plugin API
 ==========
 
 The following API is generated from the inline documentation in
-``include/qemu/qemu-plugin.h``. Please ensure any updates to the API
+``include/plugins/qemu-plugin.h``. Please ensure any updates to the API
 include the full kernel-doc annotations.
 
-.. kernel-doc:: include/qemu/qemu-plugin.h
+.. kernel-doc:: include/plugins/qemu-plugin.h
meson.build +1 -1
index 6f02547df9b..3ed62a406b7 100644
@@ -4443,7 +4443,7 @@ endforeach
 # Other build targets
 
 if get_option('plugins')
-  install_headers('include/qemu/qemu-plugin.h')
+  install_headers('include/plugins/qemu-plugin.h')
   if host_os == 'windows'
     # On windows, we want to deliver the qemu_plugin_api.lib file in the qemu installer,
     # so that plugin authors can compile against it.
include/plugins/qemu-plugin.h renamed from include/qemu/qemu-plugin.h +0 -3
similarity index 99%
rename from include/qemu/qemu-plugin.h
rename to include/plugins/qemu-plugin.h
index e44f863d839..78872716246 100644
@@ -2,9 +2,6 @@
  * Copyright (C) 2017, Emilio G. Cota <[email protected]>
  * Copyright (C) 2019, Linaro
  *
- * License: GNU GPL, version 2 or later.
- *   See the COPYING file in the top-level directory.
- *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
include/qemu/plugin.h +1 -1
index cea0a68858b..cdd4f68c0c1 100644
@@ -8,7 +8,7 @@
 #define QEMU_PLUGIN_H
 
 #include "qemu/config-file.h"
-#include "qemu/qemu-plugin.h"
+#include "plugins/qemu-plugin.h"
 #include "qemu/error-report.h"
 #include "qemu/queue.h"
 #include "qemu/option.h"
plugins/core.c +1 -1
index b4b783008f7..3f66533d749 100644
@@ -15,7 +15,7 @@
 #include "qemu/lockable.h"
 #include "qemu/option.h"
 #include "qemu/plugin.h"
-#include "qemu/qemu-plugin.h"
+#include "plugins/qemu-plugin.h"
 #include "qemu/queue.h"
 #include "qemu/rcu_queue.h"
 #include "qemu/rcu.h"
plugins/meson.build +3 -3
index 4318e3a1671..34643e2cea3 100644
@@ -3,7 +3,7 @@ if not get_option('plugins')
 endif
 
 qemu_plugin_symbols = configure_file(
-  input: files('../include/qemu/qemu-plugin.h'),
+  input: files('../include/plugins/qemu-plugin.h'),
   output: 'qemu-plugin.symbols',
   capture: true,
   command: [files('../scripts/qemu-plugin-symbols.py'), '@INPUT@'])
@@ -62,12 +62,12 @@ endif
 
 if host_os == 'windows'
   plugins_deps = declare_dependency(sources: [files('win32_linker.c')],
-                                    include_directories: '../include/qemu',
+                                    include_directories: '../include/plugins',
                                     link_with: win32_qemu_plugin_api_lib,
                                     link_args: win32_qemu_plugin_api_link_flags,
                                     dependencies: glib)
 else
-  plugins_deps = declare_dependency(include_directories: '../include/qemu',
+  plugins_deps = declare_dependency(include_directories: '../include/plugins',
                                     dependencies: glib)
 endif
 
scripts/clean-includes +1 -1
index 25dbf16c021..3fae8e00e67 100755
@@ -128,7 +128,7 @@ for f in "$@"; do
       ;;
     *include/qemu/osdep.h | \
     *include/qemu/compiler.h | \
-    *include/qemu/qemu-plugin.h | \
+    *include/plugins/qemu-plugin.h | \
     *include/glib-compat.h | \
     *include/system/os-posix.h | \
     *include/system/os-win32.h | \
Signature · 1 line
2.47.3
#8 lore raw

PATCH v3 7/11 meson: fix supported compiler arguments in other languages than C

In reply to Pierrick Bouvier

qemu_common_flags are only checked for c compiler, even though they are applied to c++ and objc. This is a problem when C compiler is gcc, and C++ compiler is clang, creating a possible mismatch.

One concrete example is option -fzero-call-used-regs=used-gpr with ubuntu2204 container, which is supported by gcc, but not by clang, thus leading to a failure when compiling a C++ TCG plugin.

---
 meson.build | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
meson.build +9 -8
index 3ed62a406b7..8820a9832c0 100644
@@ -682,10 +682,7 @@ if cc.compiles('extern struct { void (*cb)(void); } s; void f(void) { s.cb(); }'
     hardening_flags += '-fzero-call-used-regs=used-gpr'
 endif
 
-qemu_common_flags += cc.get_supported_arguments(hardening_flags)
-
-add_global_arguments(qemu_common_flags, native: false, language: all_languages)
-add_global_link_arguments(qemu_ldflags, native: false, language: all_languages)
+qemu_common_flags += hardening_flags
 
 # Collect warning flags we want to set, sorted alphabetically
 warn_flags = [
@@ -744,15 +741,19 @@ if 'cpp' in all_languages
   qemu_cxxflags = ['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'] + qemu_cflags
 endif
 
-add_project_arguments(qemu_cflags, native: false, language: 'c')
-add_project_arguments(cc.get_supported_arguments(warn_flags), native: false, language: 'c')
+add_project_arguments(cc.get_supported_arguments(qemu_common_flags + qemu_cflags + warn_flags),
+                      native: false, language: 'c')
+add_global_link_arguments(qemu_ldflags, native: false, language: all_languages)
+
 if 'cpp' in all_languages
-  add_project_arguments(qemu_cxxflags, native: false, language: 'cpp')
+  add_project_arguments(cxx.get_supported_arguments(qemu_common_flags + qemu_cxxflags),
+                        native: false, language: 'cpp')
   add_project_arguments(cxx.get_supported_arguments(warn_flags), native: false, language: 'cpp')
 endif
 if 'objc' in all_languages
   # Note sanitizer flags are not applied to Objective-C sources!
-  add_project_arguments(objc.get_supported_arguments(warn_flags), native: false, language: 'objc')
+  add_project_arguments(objc.get_supported_arguments(qemu_common_flags + warn_flags),
+                        native: false, language: 'objc')
 endif
 if host_os == 'linux'
   add_project_arguments('-isystem', meson.current_source_dir() / 'linux-headers',
Signature · 1 line
2.47.3
#9 lore raw

PATCH v3 8/11 meson: enable cpp (optionally) for plugins

In reply to Pierrick Bouvier
---
 meson.build | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
meson.build +2 -1
index 8820a9832c0..a2671a325a6 100644
@@ -77,7 +77,8 @@ python = import('python').find_installation()
 
 cc = meson.get_compiler('c')
 all_languages = ['c']
-if host_os == 'windows' and add_languages('cpp', required: false, native: false)
+enable_cpp = host_os == 'windows' or get_option('plugins')
+if enable_cpp and add_languages('cpp', required: false, native: false)
   all_languages += ['cpp']
   cxx = meson.get_compiler('cpp')
 endif
Signature · 1 line
2.47.3
#10 lore raw

PATCH v3 9/11 qga/vss-win32: fix clang warning with C++20

In reply to Pierrick Bouvier

C++20 deprecated such constructs.

../qga/vss-win32/requester.cpp:380:32: error: bitwise operation between different enumeration types ('_VSS_SNAPSHOT_CONTEXT' and '_VSS_VOLUME_SNAPSHOT_ATTRIBUTES') is deprecated [-Werror,-Wdeprecated-enum-enum-conversion]
380 | ctx = VSS_CTX_APP_ROLLBACK | VSS_VOLSNAP_ATTR_TRANSPORTABLE |

This is a false positive, since VSS_CTX_APP_ROLLBACK is not a value defined in _VSS_VOLUME_SNAPSHOT_ATTRIBUTES enum.

---
 qga/vss-win32/requester.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
qga/vss-win32/requester.cpp +4 -2
index 5615955b6f3..74489fcd0ae 100644
@@ -377,8 +377,10 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset)
      * To prevent the final commit (which requires to write to snapshots),
      * ATTR_NO_AUTORECOVERY and ATTR_TRANSPORTABLE are specified here.
      */
-    ctx = VSS_CTX_APP_ROLLBACK | VSS_VOLSNAP_ATTR_TRANSPORTABLE |
-        VSS_VOLSNAP_ATTR_NO_AUTORECOVERY | VSS_VOLSNAP_ATTR_TXF_RECOVERY;
+    ctx = VSS_CTX_APP_ROLLBACK;
+    ctx |= VSS_VOLSNAP_ATTR_TRANSPORTABLE |
+           VSS_VOLSNAP_ATTR_NO_AUTORECOVERY |
+           VSS_VOLSNAP_ATTR_TXF_RECOVERY;
     hr = vss_ctx.pVssbc->SetContext(ctx);
     if (hr == (HRESULT)VSS_E_UNSUPPORTED_CONTEXT) {
         /* Non-server version of Windows doesn't support ATTR_TRANSPORTABLE */
Signature · 1 line
2.47.3
#11 lore raw

PATCH v3 10/11 meson: update C++ standard to C++23

In reply to Pierrick Bouvier

C++ is evolving faster than C, so it's useful to enable new standards, especially for standard library.
Update to most recent standard available in our build environments.

---
 meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
meson.build +1 -1
index a2671a325a6..d57623e4f79 100644
@@ -1,5 +1,5 @@
 project('qemu', ['c'], meson_version: '>=1.5.0',
-        default_options: ['warning_level=1', 'c_std=gnu11', 'cpp_std=gnu++11', 'b_colorout=auto',
+        default_options: ['warning_level=1', 'c_std=gnu11', 'cpp_std=gnu++23', 'b_colorout=auto',
                           'b_staticpic=false', 'stdsplit=false', 'optimization=2', 'b_pie=true',
                           'rust_std=2021', 'build.rust_std=2021'],
         version: files('VERSION'))
Signature · 1 line
2.47.3
Reviewed-by on v3 10/11
#12 lore raw

Re: [PATCH v3 10/11] meson: update C++ standard to C++23

Quoted 9 lines · Pierrick Bouvier

C++ is evolving faster than C, so it's useful to enable new standards, especially for standard library.
Update to most recent standard available in our build environments.

---
  meson.build | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#13 lore raw

PATCH v3 11/11 contrib/plugins: add empty cpp plugin

In reply to Pierrick Bouvier

This plugin makes sure we can compile in C++ while including qemu-plugin header. It includes all C++ standard headers, up to C++23 standard, minus the ones that are missing in the oldest environments we need to build for.

---
 contrib/plugins/cpp.cpp     | 119 ++++++++++++++++++++++++++++++++++++
 contrib/plugins/meson.build |   4 ++
 2 files changed, 123 insertions(+)
 create mode 100644 contrib/plugins/cpp.cpp
contrib/plugins/cpp.cpp new file +119 -0
new file mode 100644
index 00000000000..1ff54896d97
@@ -0,0 +1,119 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This C++ plugin ensures we don't have regression when compiling C++.
+ */
+
+#include <qemu-plugin.h>
+
+/* https://en.cppreference.com/w/cpp/headers.html */
+#include <algorithm>
+#include <any>
+#include <array>
+#include <atomic>
+#include <barrier>
+#include <bit>
+#include <bitset>
+#include <cassert>
+#include <cctype>
+#include <cerrno>
+#include <cfenv>
+#include <cfloat>
+#include <charconv>
+#include <chrono>
+#include <cinttypes>
+#include <climits>
+#include <clocale>
+#include <cmath>
+#include <codecvt>
+#include <compare>
+#include <complex>
+#include <concepts>
+#include <condition_variable>
+#include <coroutine>
+#include <csetjmp>
+#include <csignal>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
+#include <cuchar>
+#include <cwchar>
+#include <cwctype>
+#include <deque>
+#include <exception>
+#include <execution>
+#include <filesystem>
+#include <forward_list>
+#include <fstream>
+#include <functional>
+#include <future>
+#include <initializer_list>
+#include <iomanip>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <istream>
+#include <iterator>
+#include <latch>
+#include <limits>
+#include <list>
+#include <locale>
+#include <map>
+#include <memory>
+#include <memory_resource>
+#include <mutex>
+#include <new>
+#include <numbers>
+#include <numeric>
+#include <optional>
+#include <ostream>
+#include <queue>
+#include <random>
+#include <ranges>
+#include <ratio>
+#include <regex>
+#include <scoped_allocator>
+#include <semaphore>
+#include <set>
+#include <shared_mutex>
+#include <source_location>
+#include <span>
+#include <sstream>
+#include <stack>
+#include <stdexcept>
+#include <stop_token>
+#include <streambuf>
+#include <string>
+#include <string_view>
+#include <syncstream>
+#include <system_error>
+#include <thread>
+#include <tuple>
+#include <typeindex>
+#include <typeinfo>
+#include <type_traits>
+#include <unordered_map>
+#include <unordered_set>
+#include <utility>
+#include <valarray>
+#include <variant>
+#include <vector>
+#include <version>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
+{
+}
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+                                           const qemu_info_t *info,
+                                           int argc, char **argv)
+{
+    qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+    return 0;
+}
contrib/plugins/meson.build +4 -0
index 3d2d7862e0c..53d52c97967 100644
@@ -6,6 +6,10 @@ if host_os != 'windows'
   contrib_plugins += 'lockstep.c'
 endif
 
+if 'cpp' in all_languages
+  contrib_plugins += 'cpp.cpp'
+endif
+
 t = []
 if get_option('plugins')
   foreach i : contrib_plugins
Signature · 1 line
2.47.3
#14 lore raw
In reply to Pierrick Bouvier

AFAICT this series is fully reviewed.

Quoted 12 lines · Pierrick Bouvier

Pierrick Bouvier (11):
plugins: move win32_linker.c file to plugins directory
plugins: factorize plugin dependencies and library details
plugins: use complete filename for defining plugins sources
plugins: define plugin API symbols as extern "C" when compiling in C++ tests/tcg/plugins/mem.c: remove dependency on qemu headers
plugins: move qemu-plugin.h to include/plugins/
meson: fix supported compiler arguments in other languages than C
meson: enable cpp (optionally) for plugins
qga/vss-win32: fix clang warning with C++20
meson: update C++ standard to C++23
contrib/plugins: add empty cpp plugin

#15 lore raw
In reply to Pierrick Bouvier
Quoted 66 lines · Pierrick Bouvier

Writing plugins in C can be sometimes tedious, especially when using Glib to keep track of execution state. We can directly use the same C API but write our plugin in C++, benefiting from its great standard library offering strings, smart pointers, data structures and synchronization mechanisms.

It's common for downstream QEMU forks to provide C++ for plugins, like this:
- https://github.com/panda-re/panda/tree/dev/panda/plugins
- https://github.com/FlorentRevest/DejaView/tree/main/src/qemu_plugin

Hopefully this will help more people to use upstream QEMU, and as a benefit, get their contribution back and help to develop plugins ecosystem upstream directly.

This series first cleans up build system for plugins, factorizing details between contrib/plugins and tests/tcg/plugins folders.
Then, we perform codebase cleanups to fix conflicts between existing headers and C++ headers.
After that, we can update the C++ standard used by QEMU, to benefit fully from latest updates of the language.
Finally, we define an empty C++ plugin, making sure we can keep track of possible regression in qemu-plugin header.

Note: This series is *not* a trojan horse to bring C++ in QEMU codebase, nor to define an alternative C++ API for plugins. It's just enabling more users to get the most out of existing C plugin API.

CI: https://gitlab.com/pbo-linaro/qemu/-/pipelines/2242427013

v3
--

- fix indentation in patch 2

v2
--

- drop coroutine.h rename patch as it's not needed
- drop ctype.h rename patch, and move qemu-plugin.h to include/plugins
- fix mem.c to not depend on other QEMU headers

Pierrick Bouvier (11):
plugins: move win32_linker.c file to plugins directory
plugins: factorize plugin dependencies and library details
plugins: use complete filename for defining plugins sources
plugins: define plugin API symbols as extern "C" when compiling in C++ tests/tcg/plugins/mem.c: remove dependency on qemu headers
plugins: move qemu-plugin.h to include/plugins/
meson: fix supported compiler arguments in other languages than C
meson: enable cpp (optionally) for plugins
qga/vss-win32: fix clang warning with C++20
meson: update C++ standard to C++23
contrib/plugins: add empty cpp plugin

  docs/devel/tcg-plugins.rst                  |   4 +-
  meson.build                                 |  26 +++--
  include/{qemu => plugins}/qemu-plugin.h     |  11 +-
  include/qemu/plugin.h                       |   2 +-
  plugins/core.c                              |   2 +-
  {contrib/plugins => plugins}/win32_linker.c |   0
  tests/tcg/plugins/mem.c                     |  59 ++++------
  contrib/plugins/cpp.cpp                     | 119 ++++++++++++++++++++
  contrib/plugins/meson.build                 |  25 ++--
  plugins/meson.build                         |  17 ++-
  qga/vss-win32/requester.cpp                 |   6 +-
  scripts/clean-includes                      |   2 +-
  tests/tcg/plugins/meson.build               |  18 +--
  13 files changed, 204 insertions(+), 87 deletions(-)
Quoted 4 lines

rename include/{qemu => plugins}/qemu-plugin.h (99%) rename {contrib/plugins => plugins}/win32_linker.c (100%) create mode 100644 contrib/plugins/cpp.cpp

sent v4 (fixed conflict with master):
https://lore.kernel.org/qemu-devel/[email protected]/T/#t