Skip to contentSkip to conversation

modules: cleanup and test

PATCH New Paolo Bonzini 1 participant 4 messages Aug 6, 2019

lore
Share & tools
lore
#0 lore raw

cover modules: cleanup and test

Improve coverage for modules: add a test that asks QEMU to check that no symbols are missing from any of the modules.

Markus, is using qtest ok or should it use an x- qmp command instead?

Paolo

Marc-André Lureau (3):
module: use g_hash_table_add()
module: return success on module load
tests: add module loading test

 include/qemu/module.h  |  2 +-
 qtest.c                |  9 +++++++
 tests/Makefile.include |  1 +
 tests/libqtest.c       |  6 +++++
 tests/libqtest.h       |  2 ++
 tests/modules-test.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 util/module.c          | 17 ++++++++----
 7 files changed, 102 insertions(+), 6 deletions(-)
 create mode 100644 tests/modules-test.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 module: use g_hash_table_add()

From: Marc-André Lureau <[email protected]>

The hashtable is used like a set, use the convenience g_hash_table_add() function.

---
 util/module.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
util/module.c +1 -2
index 142db7e..ca9885c 100644
@@ -179,11 +179,10 @@ void module_load_one(const char *prefix, const char *lib_name)
 
     module_name = g_strdup_printf("%s%s", prefix, lib_name);
 
-    if (g_hash_table_lookup(loaded_modules, module_name)) {
+    if (!g_hash_table_add(loaded_modules, module_name)) {
         g_free(module_name);
         return;
     }
-    g_hash_table_insert(loaded_modules, module_name, module_name);
 
     exec_dir = qemu_get_exec_dir();
     search_dir = getenv("QEMU_MODULE_DIR");
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 module: return success on module load

In reply to Paolo Bonzini

From: Marc-André Lureau <[email protected]>

Let the caller know of load success.

Note that this also changes slightly the behaviour of the function to try loading on subsequent calls if the previous ones failed.

---
 include/qemu/module.h |  2 +-
 util/module.c         | 14 +++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)
include/qemu/module.h +1 -1
index db30653..65ba596 100644
@@ -65,6 +65,6 @@ void register_module_init(void (*fn)(void), module_init_type type);
 void register_dso_module_init(void (*fn)(void), module_init_type type);
 
 void module_call_init(module_init_type type);
-void module_load_one(const char *prefix, const char *lib_name);
+bool module_load_one(const char *prefix, const char *lib_name);
 
 #endif
util/module.c +11 -3
index ca9885c..e9fe3e5 100644
@@ -156,8 +156,10 @@ out:
 }
 #endif
 
-void module_load_one(const char *prefix, const char *lib_name)
+bool module_load_one(const char *prefix, const char *lib_name)
 {
+    bool success = false;
+
 #ifdef CONFIG_MODULES
     char *fname = NULL;
     char *exec_dir;
@@ -170,7 +172,7 @@ void module_load_one(const char *prefix, const char *lib_name)
 
     if (!g_module_supported()) {
         fprintf(stderr, "Module is not supported by system.\n");
-        return;
+        return false;
     }
 
     if (!loaded_modules) {
@@ -181,7 +183,7 @@ void module_load_one(const char *prefix, const char *lib_name)
 
     if (!g_hash_table_add(loaded_modules, module_name)) {
         g_free(module_name);
-        return;
+        return true;
     }
 
     exec_dir = qemu_get_exec_dir();
@@ -205,13 +207,19 @@ void module_load_one(const char *prefix, const char *lib_name)
         fname = NULL;
         /* Try loading until loaded a module file */
         if (!ret) {
+            success = true;
             break;
         }
     }
 
+    if (!success) {
+        g_hash_table_remove(loaded_modules, module_name);
+    }
+
     for (i = 0; i < n_dirs; i++) {
         g_free(dirs[i]);
     }
 
 #endif
+    return success;
 }
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/3 tests: add module loading test

In reply to Paolo Bonzini

From: Marc-André Lureau <[email protected]>

This test will simply check that modules can be loaded, and no symbols are missing.

---
 qtest.c                |  9 +++++++
 tests/Makefile.include |  1 +
 tests/libqtest.c       |  6 +++++
 tests/libqtest.h       |  2 ++
 tests/modules-test.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 89 insertions(+)
 create mode 100644 tests/modules-test.c
qtest.c +9 -0
index 15e27e9..40c3609 100644
@@ -661,6 +661,15 @@ static void qtest_process_command(CharBackend *chr, gchar **words)
         qtest_send_prefix(chr);
         qtest_sendf(chr, "OK %"PRIi64"\n",
                     (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
+    } else if (strcmp(words[0], "module_load") == 0) {
+        g_assert(words[1] && words[2]);
+
+        qtest_send_prefix(chr);
+        if (module_load_one(words[1], words[2])) {
+            qtest_sendf(chr, "OK\n");
+        } else {
+            qtest_sendf(chr, "FAIL\n");
+        }
     } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
         int64_t ns;
         int ret;
tests/Makefile.include +1 -0
index db750dd..17dd439 100644
@@ -147,6 +147,7 @@ check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
 check-qtest-generic-y += tests/qmp-test$(EXESUF)
 check-qtest-generic-y += tests/qmp-cmd-test$(EXESUF)
+check-qtest-generic-$(CONFIG_MODULES) += tests/modules-test$(EXESUF)
 
 check-qtest-generic-y += tests/device-introspect-test$(EXESUF)
 check-qtest-generic-y += tests/cdrom-test$(EXESUF)
tests/libqtest.c +6 -0
index 3c5c3f4..e158e50 100644
@@ -811,6 +811,12 @@ bool qtest_get_irq(QTestState *s, int num)
     return s->irq_level[num];
 }
 
+void qtest_module_load(QTestState *s, const char *prefix, const char *libname)
+{
+    qtest_sendf(s, "module_load %s %s\n", prefix, libname);
+    qtest_rsp(s, 0);
+}
+
 static int64_t qtest_clock_rsp(QTestState *s)
 {
     gchar **words;
tests/libqtest.h +2 -0
index cadf1d4..2a09102 100644
@@ -262,6 +262,8 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
     GCC_FMT_ATTR(2, 0);
 
+void qtest_module_load(QTestState *s, const char *prefix, const char *libname);
+
 /**
  * qtest_get_irq:
  * @s: #QTestState instance to operate on.
tests/modules-test.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
1.8.3.1