shared: Add more dlopen() tests
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 1 Mar 2022 17:04:13 +0000 (17:04 +0000)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sat, 5 Mar 2022 21:03:32 +0000 (21:03 +0000)
Add dlopen_dw(), dlopen_elf() and dlopen_pcre2() to the dlopen test.
To enable adding dlopen_pcre2(), we move pcre2-dlopen.h/c from
src/journal to src/shared.

(cherry picked from commit ee48779e05831a0ec5e1ba5e7ed5fe92aaca1d9e)

src/journal/meson.build
src/journal/pcre2-dlopen.c [deleted file]
src/journal/pcre2-dlopen.h [deleted file]
src/shared/elf-util.c
src/shared/elf-util.h
src/shared/meson.build
src/shared/pcre2-dlopen.c [new file with mode: 0644]
src/shared/pcre2-dlopen.h [new file with mode: 0644]
src/test/test-dlopen-so.c

index eb66bfd584a13ab0f4b970132d80679a3ceb7e33..270592f2aca36108fa90e18fc632b611be31bdfc 100644 (file)
@@ -49,8 +49,6 @@ systemd_cat_sources = files('cat.c')
 
 journalctl_sources = files('''
         journalctl.c
-        pcre2-dlopen.c
-        pcre2-dlopen.h
 '''.split())
 
 if install_sysconfdir_samples
diff --git a/src/journal/pcre2-dlopen.c b/src/journal/pcre2-dlopen.c
deleted file mode 100644 (file)
index 475d7eb..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "dlfcn-util.h"
-#include "log.h"
-#include "pcre2-dlopen.h"
-
-#if HAVE_PCRE2
-static void *pcre2_dl = NULL;
-
-pcre2_match_data* (*sym_pcre2_match_data_create)(uint32_t, pcre2_general_context *);
-void (*sym_pcre2_match_data_free)(pcre2_match_data *);
-void (*sym_pcre2_code_free)(pcre2_code *);
-pcre2_code* (*sym_pcre2_compile)(PCRE2_SPTR, PCRE2_SIZE, uint32_t, int *, PCRE2_SIZE *, pcre2_compile_context *);
-int (*sym_pcre2_get_error_message)(int, PCRE2_UCHAR *, PCRE2_SIZE);
-int (*sym_pcre2_match)(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, uint32_t, pcre2_match_data *, pcre2_match_context *);
-PCRE2_SIZE* (*sym_pcre2_get_ovector_pointer)(pcre2_match_data *);
-
-int dlopen_pcre2(void) {
-        /* So here's something weird: PCRE2 actually renames the symbols exported by the library via C
-         * macros, so that the exported symbols carry a suffix "_8" but when used from C the suffix is
-         * gone. In the argument list below we ignore this mangling. Surprisingly (at least to me), we
-         * actually get away with that. That's because DLSYM_ARG() useses STRINGIFY() to generate a string
-         * version of the symbol name, and that resolves the macro mapping implicitly already, so that the
-         * string actually contains the "_8" suffix already due to that and we don't have to append it
-         * manually anymore. C is weird. ðŸ¤¯ */
-
-        return dlopen_many_sym_or_warn(
-                        &pcre2_dl, "libpcre2-8.so.0", LOG_ERR,
-                        DLSYM_ARG(pcre2_match_data_create),
-                        DLSYM_ARG(pcre2_match_data_free),
-                        DLSYM_ARG(pcre2_code_free),
-                        DLSYM_ARG(pcre2_compile),
-                        DLSYM_ARG(pcre2_get_error_message),
-                        DLSYM_ARG(pcre2_match),
-                        DLSYM_ARG(pcre2_get_ovector_pointer));
-}
-
-#else
-
-int dlopen_pcre2(void) {
-        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
-                               "PCRE2 support is not compiled in.");
-}
-#endif
diff --git a/src/journal/pcre2-dlopen.h b/src/journal/pcre2-dlopen.h
deleted file mode 100644 (file)
index 1306334..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#if HAVE_PCRE2
-
-#define PCRE2_CODE_UNIT_WIDTH 8
-#include <pcre2.h>
-
-extern pcre2_match_data* (*sym_pcre2_match_data_create)(uint32_t, pcre2_general_context *);
-extern void (*sym_pcre2_match_data_free)(pcre2_match_data *);
-extern void (*sym_pcre2_code_free)(pcre2_code *);
-extern pcre2_code* (*sym_pcre2_compile)(PCRE2_SPTR, PCRE2_SIZE, uint32_t, int *, PCRE2_SIZE *, pcre2_compile_context *);
-extern int (*sym_pcre2_get_error_message)(int, PCRE2_UCHAR *, PCRE2_SIZE);
-extern int (*sym_pcre2_match)(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, uint32_t, pcre2_match_data *, pcre2_match_context *);
-extern PCRE2_SIZE* (*sym_pcre2_get_ovector_pointer)(pcre2_match_data *);
-#endif
-
-int dlopen_pcre2(void);
index 4d93e7eabaa5c802ac33f9acb53770603747ef9d..6d9fcfbbf2aa88b71c339bd5713c831e0f22ab20 100644 (file)
@@ -80,7 +80,7 @@ unsigned int (*sym_elf_version)(unsigned int);
 GElf_Phdr *(*sym_gelf_getphdr)(Elf *, int, GElf_Phdr *);
 size_t (*sym_gelf_getnote)(Elf_Data *, size_t, GElf_Nhdr *, size_t *, size_t *);
 
-static int dlopen_dw(void) {
+int dlopen_dw(void) {
         int r;
 
         r = dlopen_many_sym_or_warn(
@@ -123,7 +123,7 @@ static int dlopen_dw(void) {
         return 1;
 }
 
-static int dlopen_elf(void) {
+int dlopen_elf(void) {
         int r;
 
         r = dlopen_many_sym_or_warn(
index cf3d9be12844f4972ca341059af6e57ef47221bf..b28e64cea6519cdd0dda79e67e3e7807f09c322c 100644 (file)
@@ -4,6 +4,9 @@
 #include "json.h"
 
 #if HAVE_ELFUTILS
+int dlopen_dw(void);
+int dlopen_elf(void);
+
 /* Parse an ELF object in a forked process, so that errors while iterating over
  * untrusted and potentially malicious data do not propagate to the main caller's process.
  * If fork_disable_dump, the child process will not dump core if it crashes. */
index 5dc58a863d4768ad611b7d64873e383ecacc9c60..006310a917129263936c2f89664cc75cd142b676 100644 (file)
@@ -241,6 +241,8 @@ shared_sources = files('''
         parse-argument.h
         parse-socket-bind-item.c
         parse-socket-bind-item.h
+        pcre2-dlopen.c
+        pcre2-dlopen.h
         pe-header.h
         pkcs11-util.c
         pkcs11-util.h
diff --git a/src/shared/pcre2-dlopen.c b/src/shared/pcre2-dlopen.c
new file mode 100644 (file)
index 0000000..475d7eb
--- /dev/null
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "dlfcn-util.h"
+#include "log.h"
+#include "pcre2-dlopen.h"
+
+#if HAVE_PCRE2
+static void *pcre2_dl = NULL;
+
+pcre2_match_data* (*sym_pcre2_match_data_create)(uint32_t, pcre2_general_context *);
+void (*sym_pcre2_match_data_free)(pcre2_match_data *);
+void (*sym_pcre2_code_free)(pcre2_code *);
+pcre2_code* (*sym_pcre2_compile)(PCRE2_SPTR, PCRE2_SIZE, uint32_t, int *, PCRE2_SIZE *, pcre2_compile_context *);
+int (*sym_pcre2_get_error_message)(int, PCRE2_UCHAR *, PCRE2_SIZE);
+int (*sym_pcre2_match)(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, uint32_t, pcre2_match_data *, pcre2_match_context *);
+PCRE2_SIZE* (*sym_pcre2_get_ovector_pointer)(pcre2_match_data *);
+
+int dlopen_pcre2(void) {
+        /* So here's something weird: PCRE2 actually renames the symbols exported by the library via C
+         * macros, so that the exported symbols carry a suffix "_8" but when used from C the suffix is
+         * gone. In the argument list below we ignore this mangling. Surprisingly (at least to me), we
+         * actually get away with that. That's because DLSYM_ARG() useses STRINGIFY() to generate a string
+         * version of the symbol name, and that resolves the macro mapping implicitly already, so that the
+         * string actually contains the "_8" suffix already due to that and we don't have to append it
+         * manually anymore. C is weird. ðŸ¤¯ */
+
+        return dlopen_many_sym_or_warn(
+                        &pcre2_dl, "libpcre2-8.so.0", LOG_ERR,
+                        DLSYM_ARG(pcre2_match_data_create),
+                        DLSYM_ARG(pcre2_match_data_free),
+                        DLSYM_ARG(pcre2_code_free),
+                        DLSYM_ARG(pcre2_compile),
+                        DLSYM_ARG(pcre2_get_error_message),
+                        DLSYM_ARG(pcre2_match),
+                        DLSYM_ARG(pcre2_get_ovector_pointer));
+}
+
+#else
+
+int dlopen_pcre2(void) {
+        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                               "PCRE2 support is not compiled in.");
+}
+#endif
diff --git a/src/shared/pcre2-dlopen.h b/src/shared/pcre2-dlopen.h
new file mode 100644 (file)
index 0000000..1306334
--- /dev/null
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#if HAVE_PCRE2
+
+#define PCRE2_CODE_UNIT_WIDTH 8
+#include <pcre2.h>
+
+extern pcre2_match_data* (*sym_pcre2_match_data_create)(uint32_t, pcre2_general_context *);
+extern void (*sym_pcre2_match_data_free)(pcre2_match_data *);
+extern void (*sym_pcre2_code_free)(pcre2_code *);
+extern pcre2_code* (*sym_pcre2_compile)(PCRE2_SPTR, PCRE2_SIZE, uint32_t, int *, PCRE2_SIZE *, pcre2_compile_context *);
+extern int (*sym_pcre2_get_error_message)(int, PCRE2_UCHAR *, PCRE2_SIZE);
+extern int (*sym_pcre2_match)(const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, uint32_t, pcre2_match_data *, pcre2_match_context *);
+extern PCRE2_SIZE* (*sym_pcre2_get_ovector_pointer)(pcre2_match_data *);
+#endif
+
+int dlopen_pcre2(void);
index ea2ef31b1fdd2a28c7f305134ae5571c258fdbd9..002f666ed85556130833111c9438df851d1afb70 100644 (file)
@@ -5,10 +5,12 @@
 
 #include "bpf-dlopen.h"
 #include "cryptsetup-util.h"
+#include "elf-util.h"
 #include "idn-util.h"
 #include "libfido2-util.h"
 #include "macro.h"
 #include "main-func.h"
+#include "pcre2-dlopen.h"
 #include "pwquality-util.h"
 #include "qrcode-util.h"
 #include "tests.h"
@@ -49,6 +51,15 @@ static int run(int argc, char **argv) {
         assert_se(dlopen_bpf() >= 0);
 #endif
 
+#if HAVE_ELFUTILS
+        assert_se(dlopen_dw() >= 0);
+        assert_se(dlopen_elf() >= 0);
+#endif
+
+#if HAVE_PCRE2
+        assert_se(dlopen_pcre2() >= 0);
+#endif
+
         return 0;
 }