]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Rework initialisation to reduce static leaks count
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 27 Sep 2019 12:32:42 +0000 (13:32 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 27 Sep 2019 12:32:42 +0000 (13:32 +0100)
config.h.in
src/libutil/http_context.c
src/libutil/mem_pool.c
src/libutil/regexp.c
src/libutil/regexp.h
src/lua/lua_common.c
src/lua/lua_regexp.c
test/rspamd_test_suite.c

index f36a3a474f87ed9d21bea8e69bdb2b5021550130..1ab588506b7218e0a85f2c7f579453862d713788 100644 (file)
@@ -420,27 +420,16 @@ extern uint64_t ottery_rand_uint64(void);
 #endif
 #endif
 
-#ifdef __cplusplus
-    #define RSPAMD_CONSTRUCTOR(f) \
-        static void f(void); \
-        struct f##_t_ { f##_t_(void) { f(); } }; static f##_t_ f##_; \
-        static void f(void)
-#elif defined(_MSC_VER)
-    #pragma section(".CRT$XCU",read)
-    #define INITIALIZER2_(f,p) \
-        static void f(void); \
-        __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
-        __pragma(comment(linker,"/include:" p #f "_")) \
-        static void f(void)
-    #ifdef _WIN64
-        #define RSPAMD_CONSTRUCTOR(f) INITIALIZER2_(f,"")
-    #else
-        #define RSPAMD_CONSTRUCTOR(f) INITIALIZER2_(f,"_")
-    #endif
+#if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
+  #define RSPAMD_CONSTRUCTOR(f) \
+          static void f(void) __attribute__((constructor)); \
+          static void f(void)
+  #define RSPAMD_DESTRUCTOR(f) \
+          static void f(void) __attribute__((destructor)); \
+          static void f(void)
 #else
-    #define RSPAMD_CONSTRUCTOR(f) \
-        static void f(void) __attribute__((constructor)); \
-        static void f(void)
+  /* In fact, everything else is not supported ¯\_(ツ)_/¯ */
+  #error incompatible compiler found, need gcc > 2.7 or clang
 #endif
 
 #endif
index 95ab7021c66633de5eced68dd583a5977f02efef..f2ecaa037550a2acd3ecbe66715bb73780f18db8 100644 (file)
@@ -297,8 +297,7 @@ rspamd_http_context_free (struct rspamd_http_context *ctx)
        }
 
        if (ctx->config.client_key_rotate_time > 0) {
-               /* Event is removed on base event loop termination */
-               /* event_del (&ctx->client_rotate_ev); */
+               ev_timer_stop (ctx->event_loop, &ctx->client_rotate_ev);
 
                if (ctx->client_kp) {
                        rspamd_keypair_unref (ctx->client_kp);
index c03e7ae2cbffcdbabfaf8836ad101253302b2979..7033376634d419981f7084352327a21e73849501 100644 (file)
@@ -142,23 +142,34 @@ rspamd_mempool_entry_new (const gchar *loc)
        return entry;
 }
 
+RSPAMD_CONSTRUCTOR (rspamd_mempool_entries_ctor)
+{
+       mempool_entries = kh_init (mempool_entry);
+}
+
+RSPAMD_DESTRUCTOR (rspamd_mempool_entries_dtor)
+{
+       struct rspamd_mempool_entry_point *elt;
+
+       kh_foreach_value (mempool_entries, elt, {
+               g_free (elt);
+       });
+
+       kh_destroy (mempool_entry, mempool_entries);
+}
+
 static inline struct rspamd_mempool_entry_point *
 rspamd_mempool_get_entry (const gchar *loc)
 {
        khiter_t k;
        struct rspamd_mempool_entry_point *elt;
 
-       if (mempool_entries == NULL) {
-               mempool_entries = kh_init (mempool_entry);
-       }
-       else {
-               k = kh_get (mempool_entry, mempool_entries, loc);
+       k = kh_get (mempool_entry, mempool_entries, loc);
 
-               if (k != kh_end (mempool_entries)) {
-                       elt = kh_value (mempool_entries, k);
+       if (k != kh_end (mempool_entries)) {
+               elt = kh_value (mempool_entries, k);
 
-                       return elt;
-               }
+               return elt;
        }
 
        return rspamd_mempool_entry_new (loc);
index fa606273c7e0e57c64cd8df654bfe46586261ea7..6f449539ff55f0cd087a7ba4513a3f51988cb3b0 100644 (file)
@@ -998,7 +998,7 @@ rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache,
        res = rspamd_regexp_new (pattern, flags, err);
 
        if (res) {
-               REF_RETAIN (res);
+               /* REF_RETAIN (res); */
                g_hash_table_insert (cache->tbl, res->id, res);
        }
 
@@ -1058,6 +1058,24 @@ rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache)
        }
 }
 
+RSPAMD_CONSTRUCTOR (rspamd_re_static_pool_ctor)
+{
+       global_re_cache = rspamd_regexp_cache_new ();
+#ifdef WITH_PCRE2
+       pcre2_ctx = pcre2_compile_context_create (NULL);
+       pcre2_set_newline (pcre2_ctx, PCRE_FLAG(NEWLINE_ANY));
+#endif
+}
+
+RSPAMD_DESTRUCTOR (rspamd_re_static_pool_dtor)
+{
+       rspamd_regexp_cache_destroy (global_re_cache);
+#ifdef WITH_PCRE2
+       pcre2_compile_context_free (pcre2_ctx);
+#endif
+}
+
+
 void
 rspamd_regexp_library_init (struct rspamd_config *cfg)
 {
@@ -1066,19 +1084,16 @@ rspamd_regexp_library_init (struct rspamd_config *cfg)
                        can_jit = FALSE;
                        check_jit = FALSE;
                }
+               else if (!can_jit) {
+                       check_jit = TRUE;
+               }
        }
 
-       if (global_re_cache == NULL) {
-               global_re_cache = rspamd_regexp_cache_new ();
+       if (check_jit) {
 #ifdef HAVE_PCRE_JIT
                gint jit, rc;
                gchar *str;
 
-               if (check_jit) {
-#ifdef WITH_PCRE2
-                       pcre2_ctx = pcre2_compile_context_create (NULL);
-                       pcre2_set_newline (pcre2_ctx, PCRE_FLAG(NEWLINE_ANY));
-#endif
 #ifndef WITH_PCRE2
                        rc = pcre_config (PCRE_CONFIG_JIT, &jit);
 #else
@@ -1118,23 +1133,12 @@ rspamd_regexp_library_init (struct rspamd_config *cfg)
                                                  " are impossible");
                                can_jit = FALSE;
                        }
-               }
 #else
                msg_info ("pcre is too old and has no JIT support, so many optimizations"
-                               " are impossible");
+                                 " are impossible");
                can_jit = FALSE;
 #endif
-       }
-}
-
-void
-rspamd_regexp_library_finalize (void)
-{
-       if (global_re_cache != NULL) {
-               rspamd_regexp_cache_destroy (global_re_cache);
-#ifdef WITH_PCRE2
-               pcre2_compile_context_free (pcre2_ctx);
-#endif
+               check_jit = FALSE;
        }
 }
 
index f7ed05daddbd18769e4d4f4a39c8b626bf175c99..2e414892ae34ab09ed0d7dbee64b3cf7ba2309f4 100644 (file)
@@ -261,11 +261,6 @@ gint rspamd_regexp_cmp (gconstpointer a, gconstpointer b);
  */
 void rspamd_regexp_library_init (struct rspamd_config *cfg);
 
-/**
- * Cleanup internal library structures
- */
-void rspamd_regexp_library_finalize (void);
-
 /**
  * Create regexp from glob
  * @param gl
index bfd572532cf5266113f80ec6c5d88016e7e2e5a5..bbcec4c1ce02be99bbbb184938533ba45dd3ec01 100644 (file)
@@ -64,6 +64,16 @@ lua_error_quark (void)
 KHASH_INIT (lua_class_set, const gchar *, bool, 0, rspamd_str_hash, rspamd_str_equal);
 khash_t (lua_class_set) *lua_classes = NULL;
 
+RSPAMD_CONSTRUCTOR (lua_classes_ctor)
+{
+       lua_classes = kh_init (lua_class_set);
+}
+
+RSPAMD_DESTRUCTOR (lua_classes_dtor)
+{
+       kh_destroy (lua_class_set, lua_classes);
+}
+
 /* Util functions */
 /**
  * Create new class and store metatable on top of the stack (must be popped if not needed)
@@ -80,10 +90,6 @@ rspamd_lua_new_class (lua_State * L,
        khiter_t k;
        gint r, nmethods = 0;
 
-       if (lua_classes == NULL) {
-               lua_classes = kh_init (lua_class_set);
-       }
-
        k = kh_put (lua_class_set, lua_classes, classname, &r);
        class_ptr = RSPAMD_LIGHTUSERDATA_MASK (kh_key (lua_classes, k));
 
index 7277f192f1e9584302b52f2a543e1edcf87a2356..ae1f2007ada5bc93b9ff5df0f27b9ef42869be0a 100644 (file)
@@ -892,9 +892,13 @@ luaopen_regexp (lua_State * L)
        rspamd_lua_new_class (L, "rspamd{regexp}", regexplib_m);
        lua_pop (L, 1);
        rspamd_lua_add_preload (L, "rspamd_regexp", lua_load_regexp);
+}
 
-       if (regexp_static_pool == NULL) {
-               regexp_static_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
-                               "regexp_lua_pool");
-       }
+RSPAMD_CONSTRUCTOR (lua_re_static_pool_ctor) {
+       regexp_static_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
+                       "regexp_lua_pool");
 }
+
+RSPAMD_DESTRUCTOR (lua_re_static_pool_dtor) {
+       rspamd_mempool_delete (regexp_static_pool);
+}
\ No newline at end of file
index 3cbb81460459f70011b2faf023c2aafbf73c0907..db12a1a0cc23c25946edee21a39d9ad05b07ad27 100644 (file)
@@ -87,7 +87,6 @@ main (int argc, char **argv)
        g_test_add_func ("/rspamd/aio", rspamd_async_test_func);
 #endif
        g_test_run ();
-       rspamd_regexp_library_finalize ();
 
        return 0;
 }