From 82637e89647924b395c4d4cdfc835ceebbd0b661 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 27 Sep 2019 13:32:42 +0100 Subject: [PATCH] [Rework] Rework initialisation to reduce static leaks count --- config.h.in | 29 ++++++++---------------- src/libutil/http_context.c | 3 +-- src/libutil/mem_pool.c | 29 ++++++++++++++++-------- src/libutil/regexp.c | 46 +++++++++++++++++++++----------------- src/libutil/regexp.h | 5 ----- src/lua/lua_common.c | 14 ++++++++---- src/lua/lua_regexp.c | 12 ++++++---- test/rspamd_test_suite.c | 1 - 8 files changed, 73 insertions(+), 66 deletions(-) diff --git a/config.h.in b/config.h.in index f36a3a474..1ab588506 100644 --- a/config.h.in +++ b/config.h.in @@ -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 diff --git a/src/libutil/http_context.c b/src/libutil/http_context.c index 95ab7021c..f2ecaa037 100644 --- a/src/libutil/http_context.c +++ b/src/libutil/http_context.c @@ -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); diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c index c03e7ae2c..703337663 100644 --- a/src/libutil/mem_pool.c +++ b/src/libutil/mem_pool.c @@ -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); diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index fa606273c..6f449539f 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -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; } } diff --git a/src/libutil/regexp.h b/src/libutil/regexp.h index f7ed05dad..2e414892a 100644 --- a/src/libutil/regexp.h +++ b/src/libutil/regexp.h @@ -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 diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index bfd572532..bbcec4c1c 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -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)); diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c index 7277f192f..ae1f2007a 100644 --- a/src/lua/lua_regexp.c +++ b/src/lua/lua_regexp.c @@ -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 diff --git a/test/rspamd_test_suite.c b/test/rspamd_test_suite.c index 3cbb81460..db12a1a0c 100644 --- a/test/rspamd_test_suite.c +++ b/test/rspamd_test_suite.c @@ -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; } -- 2.39.5