1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #pragma once
4 
5 #include "alloc-util.h"
6 #include "macro.h"
7 
8 /* A framework for registering static variables that shall be freed on shutdown of a process. It's a bit like gcc's
9  * destructor attribute, but allows us to precisely schedule when we want to free the variables. This is supposed to
10  * feel a bit like the gcc cleanup attribute, but for static variables. Note that this does not work for static
11  * variables declared in .so's, as the list is private to the same linking unit. But maybe that's a good thing. */
12 
13 typedef struct StaticDestructor {
14         void *data;
15         free_func_t destroy;
16 } StaticDestructor;
17 
18 #define STATIC_DESTRUCTOR_REGISTER(variable, func) \
19         _STATIC_DESTRUCTOR_REGISTER(UNIQ, variable, func)
20 
21 #define _STATIC_DESTRUCTOR_REGISTER(uq, variable, func)                 \
22         /* Type-safe destructor */                                      \
23         static void UNIQ_T(static_destructor_wrapper, uq)(void *p) {    \
24                 typeof(variable) *q = p;                                \
25                 func(q);                                                \
26         }                                                               \
27         /* Older compilers don't know "retain" attribute. */            \
28         _Pragma("GCC diagnostic ignored \"-Wattributes\"")              \
29         /* The actual destructor structure we place in a special section to find it. */ \
30         _section_("SYSTEMD_STATIC_DESTRUCT")                            \
31         /* Use pointer alignment, since that is apparently what gcc does for static variables. */ \
32         _alignptr_                                                      \
33         /* Make sure this is not dropped from the image despite not being explicitly referenced. */ \
34         _used_                                                          \
35         /* Prevent garbage collection by the linker. */                 \
36         _retain_                                                        \
37         /* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section
38          * packed next to each other so that we can enumerate it. */     \
39         _variable_no_sanitize_address_                                  \
40         static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
41                 .data = &(variable),                                    \
42                 .destroy = UNIQ_T(static_destructor_wrapper, uq),       \
43         }
44 
45 /* Beginning and end of our section listing the destructors. We define these as weak as we want this to work
46  * even if no destructors are defined and the section is missing. */
47 extern const struct StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[];
48 extern const struct StaticDestructor _weak_ __stop_SYSTEMD_STATIC_DESTRUCT[];
49 
50 /* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in
51  * the same linking unit as the variables we want to destroy.) */
static_destruct(void)52 static inline void static_destruct(void) {
53         const StaticDestructor *d;
54 
55         if (!__start_SYSTEMD_STATIC_DESTRUCT)
56                 return;
57 
58         d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, sizeof(void*));
59         while (d < __stop_SYSTEMD_STATIC_DESTRUCT) {
60                 d->destroy(d->data);
61                 d = ALIGN_TO_PTR(d + 1, sizeof(void*));
62         }
63 }
64