Browse Source

[Rework] Rework initialisation to reduce static leaks count

tags/2.0
Vsevolod Stakhov 4 years ago
parent
commit
82637e8964

+ 9
- 20
config.h.in View 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

+ 1
- 2
src/libutil/http_context.c View 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);

+ 20
- 9
src/libutil/mem_pool.c View 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);

+ 25
- 21
src/libutil/regexp.c View 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;
}
}


+ 0
- 5
src/libutil/regexp.h View 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

+ 10
- 4
src/lua/lua_common.c View 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));


+ 8
- 4
src/lua/lua_regexp.c View 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);
}

+ 0
- 1
test/rspamd_test_suite.c View 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;
}

Loading…
Cancel
Save