]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Add mmap support to lua_text
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 8 Oct 2016 12:43:13 +0000 (13:43 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 8 Oct 2016 15:35:42 +0000 (16:35 +0100)
src/lua/lua_common.h
src/lua/lua_html.c
src/lua/lua_mimepart.c
src/lua/lua_regexp.c
src/lua/lua_task.c
src/lua/lua_tcp.c
src/lua/lua_util.c

index 6fdff599051d230561ff5888364012c2dea8defe..3f2fa6ba5bf00ad8142da8f0b198b30b1e48057e 100644 (file)
@@ -64,10 +64,12 @@ struct rspamd_lua_ip {
        rspamd_inet_addr_t *addr;
 };
 
+#define RSPAMD_TEXT_FLAG_OWN (1 << 0)
+#define RSPAMD_TEXT_FLAG_MMAPED (1 << 1)
 struct rspamd_lua_text {
        const gchar *start;
        guint len;
-       gboolean own;
+       guint flags;
 };
 
 struct rspamd_lua_url {
index bad085450532143f772f8c6b3b8235358619ba2d..1e1ab10aef0283d9e3f82f0a585cd29a1dcb3894 100644 (file)
@@ -355,7 +355,7 @@ lua_html_push_block (lua_State *L, struct html_block *bl)
                t = lua_newuserdata (L, sizeof (*t));
                t->start = bl->style.start;
                t->len = bl->style.len;
-               t->own = FALSE;
+               t->flags = 0;
                lua_settable (L, -3);
        }
 }
@@ -562,7 +562,7 @@ lua_html_tag_get_content (lua_State *L)
                        rspamd_lua_setclass (L, "rspamd{text}", -1);
                        t->start = tag->content;
                        t->len = tag->content_length;
-                       t->own = FALSE;
+                       t->flags = 0;
                }
                else {
                        lua_pushnil (L);
index a2b6cc49c446efeb38cfa440498e0f1a9d09c0df..e8c7bd9c8473c2aed732a1562e489f62a6a00b6e 100644 (file)
@@ -357,7 +357,7 @@ lua_textpart_get_content (lua_State * L)
        rspamd_lua_setclass (L, "rspamd{text}", -1);
        t->start = part->content->data;
        t->len = part->content->len;
-       t->own = FALSE;
+       t->flags = 0;
 
        return 1;
 }
@@ -377,7 +377,7 @@ lua_textpart_get_raw_content (lua_State * L)
        rspamd_lua_setclass (L, "rspamd{text}", -1);
        t->start = part->orig->data;
        t->len = part->orig->len;
-       t->own = FALSE;
+       t->flags = 0;
 
        return 1;
 }
@@ -397,7 +397,7 @@ lua_textpart_get_content_oneline (lua_State * L)
        rspamd_lua_setclass (L, "rspamd{text}", -1);
        t->start = part->stripped_content->data;
        t->len = part->stripped_content->len;
-       t->own = FALSE;
+       t->flags = 0;
 
        return 1;
 }
@@ -609,7 +609,7 @@ lua_mimepart_get_content (lua_State * L)
        rspamd_lua_setclass (L, "rspamd{text}", -1);
        t->start = part->content->data;
        t->len = part->content->len;
-       t->own = FALSE;
+       t->flags = 0;
 
        return 1;
 }
index 80b7521723fd6a3a1527861285e174114f0c688d..1e6122b2ee62f77ec3cf1677015554c73ba54cd9 100644 (file)
@@ -640,7 +640,7 @@ lua_regexp_split (lua_State *L)
                                                rspamd_lua_setclass (L, "rspamd{text}", -1);
                                                t->start = old_start;
                                                t->len = start - old_start;
-                                               t->own = FALSE;
+                                               t->flags = 0;
                                        }
 
                                        lua_rawseti (L, -2, ++i);
@@ -665,7 +665,7 @@ lua_regexp_split (lua_State *L)
                                        rspamd_lua_setclass (L, "rspamd{text}", -1);
                                        t->start = end;
                                        t->len = (data + len) - end;
-                                       t->own = FALSE;
+                                       t->flags = 0;
                                }
 
                                lua_rawseti (L, -2, ++i);
index 4706d69a06a84fc63b756a0c789e4c204fd7745a..a3354acb64b842aee992379d7bab7db2decc7198 100644 (file)
@@ -25,6 +25,7 @@
 #include "email_addr.h"
 #include "utlist.h"
 #include "cryptobox.h"
+#include "unix-std.h"
 
 /***
  * @module rspamd_task
@@ -1184,7 +1185,7 @@ lua_task_get_content (lua_State * L)
                rspamd_lua_setclass (L, "rspamd{text}", -1);
                t->len = task->msg.len;
                t->start = task->msg.begin;
-               t->own = FALSE;
+               t->flags = 0;
        }
        else {
                return luaL_error (L, "invalid arguments");
@@ -1213,7 +1214,7 @@ lua_task_get_rawbody (lua_State * L)
                        t->start = task->msg.begin;
                }
 
-               t->own = FALSE;
+               t->flags = 0;
        }
        else {
                return luaL_error (L, "invalid arguments");
@@ -1311,7 +1312,7 @@ lua_task_get_request_header (lua_State *L)
                        rspamd_lua_setclass (L, "rspamd{text}", -1);
                        t->start = hdr->begin;
                        t->len = hdr->len;
-                       t->own = FALSE;
+                       t->flags = 0;
 
                        return 1;
                }
@@ -1490,7 +1491,7 @@ lua_task_get_raw_headers (lua_State *L)
                rspamd_lua_setclass (L, "rspamd{text}", -1);
                t->start = task->raw_headers_content.begin;
                t->len = task->raw_headers_content.len;
-               t->own = FALSE;
+               t->flags = 0;
        }
        else {
                return luaL_error (L, "invalid arguments");
@@ -3473,8 +3474,16 @@ lua_text_gc (lua_State *L)
 {
        struct rspamd_lua_text *t = lua_check_text (L, 1);
 
-       if (t != NULL && t->own) {
-               g_free ((gpointer)t->start);
+       if (t != NULL) {
+               if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
+                       if (t->flags & RSPAMD_TEXT_FLAG_MMAPED) {
+                               munmap ((gpointer)t->start, t->len);
+                       }
+                       else {
+                               g_free ((gpointer)t->start);
+                       }
+               }
+
        }
 
        return 0;
index 23140b79a9c1a90eeb344075cdd47b9780757c33..c80c311f87bf7d61f95f645d8769aed62c350559 100644 (file)
@@ -199,7 +199,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
        rspamd_lua_setclass (cbd->L, "rspamd{text}", -1);
        t->start = (const gchar *)str;
        t->len = len;
-       t->own = FALSE;
+       t->flags = 0;
        /* Connection */
        pcbd = lua_newuserdata (cbd->L, sizeof (*pcbd));
        *pcbd = cbd;
@@ -467,9 +467,9 @@ lua_tcp_arg_toiovec (lua_State *L, gint pos, rspamd_mempool_t *pool,
                        vec->iov_base = (void *)t->start;
                        vec->iov_len = t->len;
 
-                       if (t->own) {
+                       if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
                                /* Steal ownership */
-                               t->own = FALSE;
+                               t->flags = 0;
                                rspamd_mempool_add_destructor (pool, g_free, (void *)t->start);
                        }
                }
index 9c37bc8617f6fa94c303512b619a840df646f030..4ce7cbe64c61ce64b00e3eb53fe68a2523c22039 100644 (file)
@@ -627,7 +627,7 @@ lua_util_encode_base64 (lua_State *L)
                        t->start = out;
                        t->len = outlen;
                        /* Need destruction */
-                       t->own = TRUE;
+                       t->flags = RSPAMD_TEXT_FLAG_OWN;
                }
                else {
                        lua_pushnil (L);
@@ -657,8 +657,8 @@ lua_util_decode_base64 (lua_State *L)
                        s = t->start;
                        inlen = t->len;
                        zero_copy = TRUE;
-                       if (t->own) {
-                               t->own = FALSE;
+                       if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
+                               t->flags = 0;
                                grab_own = TRUE;
                        }
                }
@@ -672,7 +672,13 @@ lua_util_decode_base64 (lua_State *L)
                        rspamd_lua_setclass (L, "rspamd{text}", -1);
                        t->start = s;
                        t->len = outlen;
-                       t->own = grab_own;
+
+                       if (grab_own) {
+                               t->flags |= RSPAMD_TEXT_FLAG_OWN;
+                       }
+                       else {
+                               t->flags = 0;
+                       }
                }
                else {
                        t = lua_newuserdata (L, sizeof (*t));
@@ -682,7 +688,7 @@ lua_util_decode_base64 (lua_State *L)
                        outlen = g_base64_decode_step (s, inlen, (guchar *)t->start,
                                        &state, &save);
                        t->len = outlen;
-                       t->own = TRUE;
+                       t->flags = RSPAMD_TEXT_FLAG_OWN;
                }
        }
        else {
@@ -725,7 +731,7 @@ lua_util_encode_base32 (lua_State *L)
                        t->start = out;
                        t->len = outlen;
                        /* Need destruction */
-                       t->own = TRUE;
+                       t->flags = RSPAMD_TEXT_FLAG_OWN;
                }
                else {
                        lua_pushnil (L);
@@ -759,7 +765,7 @@ lua_util_decode_base32 (lua_State *L)
                rspamd_lua_setclass (L, "rspamd{text}", -1);
                t->start = rspamd_decode_base32 (s, inlen, &outlen);
                t->len = outlen;
-               t->own = TRUE;
+               t->flags = RSPAMD_TEXT_FLAG_OWN;
        }
        else {
                lua_pushnil (L);
@@ -793,7 +799,7 @@ lua_util_decode_url (lua_State *L)
                t->start = g_malloc (inlen);
                memcpy ((char *)t->start, s, inlen);
                t->len = rspamd_decode_url ((char *)t->start, s, inlen);
-               t->own = TRUE;
+               t->flags = RSPAMD_TEXT_FLAG_OWN;
        }
        else {
                lua_pushnil (L);
@@ -938,7 +944,7 @@ lua_util_parse_html (lua_State *L)
                rspamd_lua_setclass (L, "rspamd{text}", -1);
                t->start = res->data;
                t->len = res->len;
-               t->own = TRUE;
+               t->flags = RSPAMD_TEXT_FLAG_OWN;
 
                g_byte_array_free (res, FALSE);
                g_byte_array_free (in, TRUE);