]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util/mpm: factorize code
authorPhilippe Antoine <pantoine@oisf.net>
Tue, 17 Jun 2025 13:06:08 +0000 (15:06 +0200)
committerPhilippe Antoine <pantoine@oisf.net>
Thu, 19 Jun 2025 12:50:02 +0000 (14:50 +0200)
(cherry picked from commit 679bd23cb70a7299f1b7f56275a334d7f1b15d78)

src/Makefile.am
src/util-mpm-ac-ks.c
src/util-mpm-ac-queue.c [new file with mode: 0644]
src/util-mpm-ac-queue.h [new file with mode: 0644]
src/util-mpm-ac.c

index 8377d39e4819ca56994ba8b04a65595e004e048b..2ac4679f2b464c02f6f2786f24ed01fb0399b58e 100755 (executable)
@@ -582,6 +582,7 @@ noinst_HEADERS = \
        util-memrchr.h \
        util-misc.h \
        util-mpm-ac-bs.h \
+       util-mpm-ac-queue.h \
        util-mpm-ac.h \
        util-mpm-ac-ks.h \
        util-mpm.h \
@@ -1183,6 +1184,7 @@ libsuricata_c_a_SOURCES = \
        util-memrchr.c \
        util-misc.c \
        util-mpm-ac-bs.c \
+       util-mpm-ac-queue.c \
        util-mpm-ac.c \
        util-mpm-ac-ks.c \
        util-mpm-ac-ks-small.c \
index e6d0f67ff1ca487ec953429a1c6c50bf97b4647d..a26484097be40c92b56fc6e489d4a1efcc3a76f1 100644 (file)
@@ -81,6 +81,7 @@
 #include "util-memcpy.h"
 #include "util-validate.h"
 #include "util-mpm-ac-ks.h"
+#include "util-mpm-ac-queue.h"
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 
@@ -148,17 +149,6 @@ static void SCACTileDestroyInitCtx(MpmCtx *mpm_ctx);
 /* a placeholder to denote a failure transition in the goto table */
 #define SC_AC_TILE_FAIL (-1)
 
-#define STATE_QUEUE_CONTAINER_SIZE 65536
-
-/**
- * \brief Helper structure used by AC during state table creation
- */
-typedef struct StateQueue_ {
-    int32_t store[STATE_QUEUE_CONTAINER_SIZE];
-    int top;
-    int bot;
-} StateQueue;
-
 /**
  * \internal
  * \brief Initialize the AC context with user specified conf parameters.  We
@@ -402,48 +392,6 @@ static void SCACTileCreateGotoTable(MpmCtx *mpm_ctx)
     }
 }
 
-static inline int SCACStateQueueIsEmpty(StateQueue *q)
-{
-    if (q->top == q->bot)
-        return 1;
-    else
-        return 0;
-}
-
-static inline void SCACEnqueue(StateQueue *q, int32_t state)
-{
-    int i = 0;
-
-    /*if we already have this */
-    for (i = q->bot; i < q->top; i++) {
-        if (q->store[i] == state)
-            return;
-    }
-
-    q->store[q->top++] = state;
-
-    if (q->top == STATE_QUEUE_CONTAINER_SIZE)
-        q->top = 0;
-
-    if (q->top == q->bot) {
-        FatalError("Just ran out of space in the queue.  "
-                   "Fatal Error.  Exiting.  Please file a bug report on this");
-    }
-}
-
-static inline int32_t SCACDequeue(StateQueue *q)
-{
-    if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
-        q->bot = 0;
-
-    if (q->bot == q->top) {
-        FatalError("StateQueue behaving weirdly.  "
-                   "Fatal Error.  Exiting.  Please file a bug report on this");
-    }
-
-    return q->store[q->bot++];
-}
-
 /**
  * \internal
  * \brief Club the output data from 2 states and store it in the 1st state.
@@ -506,10 +454,7 @@ static void SCACTileCreateFailureTable(MpmCtx *mpm_ctx)
     int32_t state = 0;
     int32_t r_state = 0;
 
-    StateQueue *q = SCCalloc(1, sizeof(StateQueue));
-    if (q == NULL) {
-        FatalError("Error allocating memory");
-    }
+    StateQueue *q = SCACStateQueueAlloc();
 
     /* Allocate space for the failure table.  A failure entry in the table for
      * every state(SCACTileCtx->state_count) */
@@ -547,7 +492,7 @@ static void SCACTileCreateFailureTable(MpmCtx *mpm_ctx)
                                      mpm_ctx);
         }
     }
-    SCFree(q);
+    SCACStateQueueFree(q);
 }
 
 /*
@@ -681,10 +626,7 @@ static inline void SCACTileCreateDeltaTable(MpmCtx *mpm_ctx)
         ctx->alphabet_storage = 256; /* Change? */
     }
 
-    StateQueue *q = SCCalloc(1, sizeof(StateQueue));
-    if (q == NULL) {
-        FatalError("Error allocating memory");
-    }
+    StateQueue *q = SCACStateQueueAlloc();
 
     for (aa = 0; aa < ctx->alphabet_size; aa++) {
         int temp_state = ctx->goto_table[0][aa];
@@ -705,7 +647,7 @@ static inline void SCACTileCreateDeltaTable(MpmCtx *mpm_ctx)
             }
         }
     }
-    SCFree(q);
+    SCACStateQueueFree(q);
 }
 
 static void SCACTileClubOutputStatePresenceWithDeltaTable(MpmCtx *mpm_ctx)
diff --git a/src/util-mpm-ac-queue.c b/src/util-mpm-ac-queue.c
new file mode 100644 (file)
index 0000000..40bb951
--- /dev/null
@@ -0,0 +1,34 @@
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "suricata-common.h"
+#include "util-debug.h"
+#include "util-mpm-ac-queue.h"
+
+StateQueue *SCACStateQueueAlloc(void)
+{
+    StateQueue *q = SCCalloc(1, sizeof(StateQueue));
+    if (q == NULL) {
+        FatalError("Error allocating memory");
+    }
+    return q;
+}
+
+void SCACStateQueueFree(StateQueue *q)
+{
+    SCFree(q);
+}
diff --git a/src/util-mpm-ac-queue.h b/src/util-mpm-ac-queue.h
new file mode 100644 (file)
index 0000000..3bf8b41
--- /dev/null
@@ -0,0 +1,84 @@
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Anoop Saldanha <anoopsaldanha@gmail.com>
+ *
+ */
+
+#ifndef SURICATA_UTIL_MPM_AC_QUEUE_H
+#define SURICATA_UTIL_MPM_AC_QUEUE_H
+
+#define STATE_QUEUE_CONTAINER_SIZE 65536
+
+/**
+ * \brief Helper structure used by AC during state table creation
+ */
+typedef struct StateQueue_ {
+    int32_t store[STATE_QUEUE_CONTAINER_SIZE];
+    int top;
+    int bot;
+} StateQueue;
+
+StateQueue *SCACStateQueueAlloc(void);
+void SCACStateQueueFree(StateQueue *q);
+
+static inline int SCACStateQueueIsEmpty(StateQueue *q)
+{
+    if (q->top == q->bot)
+        return 1;
+    else
+        return 0;
+}
+
+static inline void SCACEnqueue(StateQueue *q, int32_t state)
+{
+    int i = 0;
+
+    /*if we already have this */
+    for (i = q->bot; i < q->top; i++) {
+        if (q->store[i] == state)
+            return;
+    }
+
+    q->store[q->top++] = state;
+
+    if (q->top == STATE_QUEUE_CONTAINER_SIZE)
+        q->top = 0;
+
+    if (q->top == q->bot) {
+        FatalError("Just ran out of space in the queue.  "
+                   "Fatal Error.  Exiting.  Please file a bug report on this");
+    }
+}
+
+static inline int32_t SCACDequeue(StateQueue *q)
+{
+    if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
+        q->bot = 0;
+
+    if (q->bot == q->top) {
+        FatalError("StateQueue behaving weirdly.  "
+                   "Fatal Error.  Exiting.  Please file a bug report on this");
+    }
+
+    return q->store[q->bot++];
+}
+
+#endif /* SURICATA_UTIL_MPM_AC_QUEUE_H */
index 961031e3a918a432129bb5884b4ba8a451149e5f..74a60756942f62cb5c00f970edafbabbeceb96f0 100644 (file)
@@ -61,6 +61,7 @@
 #include "util-mpm-ac.h"
 #include "util-memcpy.h"
 #include "util-validate.h"
+#include "util-mpm-ac-queue.h"
 
 void SCACInitCtx(MpmCtx *);
 void SCACInitThreadCtx(MpmCtx *, MpmThreadCtx *);
@@ -80,23 +81,12 @@ void SCACRegisterTests(void);
 /* a placeholder to denote a failure transition in the goto table */
 #define SC_AC_FAIL (-1)
 
-#define STATE_QUEUE_CONTAINER_SIZE 65536
-
 #define AC_CASE_MASK    0x80000000
 #define AC_PID_MASK     0x7FFFFFFF
 #define AC_CASE_BIT     31
 
 static int construct_both_16_and_32_state_tables = 0;
 
-/**
- * \brief Helper structure used by AC during state table creation
- */
-typedef struct StateQueue_ {
-    int32_t store[STATE_QUEUE_CONTAINER_SIZE];
-    int top;
-    int bot;
-} StateQueue;
-
 /**
  * \internal
  * \brief Initialize the AC context with user specified conf parameters.  We
@@ -371,48 +361,6 @@ static inline void SCACDetermineLevel1Gap(MpmCtx *mpm_ctx)
     return;
 }
 
-static inline int SCACStateQueueIsEmpty(StateQueue *q)
-{
-    if (q->top == q->bot)
-        return 1;
-    else
-        return 0;
-}
-
-static inline void SCACEnqueue(StateQueue *q, int32_t state)
-{
-    int i = 0;
-
-    /*if we already have this */
-    for (i = q->bot; i < q->top; i++) {
-        if (q->store[i] == state)
-            return;
-    }
-
-    q->store[q->top++] = state;
-
-    if (q->top == STATE_QUEUE_CONTAINER_SIZE)
-        q->top = 0;
-
-    if (q->top == q->bot) {
-        FatalError("Just ran out of space in the queue. Please file a bug report on this");
-    }
-
-    return;
-}
-
-static inline int32_t SCACDequeue(StateQueue *q)
-{
-    if (q->bot == STATE_QUEUE_CONTAINER_SIZE)
-        q->bot = 0;
-
-    if (q->bot == q->top) {
-        FatalError("StateQueue behaving weirdly. Please file a bug report on this");
-    }
-
-    return q->store[q->bot++];
-}
-
 /**
  * \internal
  * \brief Club the output data from 2 states and store it in the 1st state.
@@ -472,10 +420,7 @@ static inline void SCACCreateFailureTable(MpmCtx *mpm_ctx)
     int32_t state = 0;
     int32_t r_state = 0;
 
-    StateQueue *q = SCCalloc(1, sizeof(StateQueue));
-    if (q == NULL) {
-        FatalError("Error allocating memory");
-    }
+    StateQueue *q = SCACStateQueueAlloc();
 
     /* allot space for the failure table.  A failure entry in the table for
      * every state(SCACCtx->state_count) */
@@ -513,9 +458,7 @@ static inline void SCACCreateFailureTable(MpmCtx *mpm_ctx)
                                  mpm_ctx);
         }
     }
-    SCFree(q);
-
-    return;
+    SCACStateQueueFree(q);
 }
 
 /**
@@ -538,10 +481,7 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
         mpm_ctx->memory_cnt++;
         mpm_ctx->memory_size += (ctx->state_count * sizeof(*ctx->state_table_u16));
 
-        StateQueue *q = SCCalloc(1, sizeof(StateQueue));
-        if (q == NULL) {
-            FatalError("Error allocating memory");
-        }
+        StateQueue *q = SCACStateQueueAlloc();
 
         for (ascii_code = 0; ascii_code < 256; ascii_code++) {
             DEBUG_VALIDATE_BUG_ON(ctx->goto_table[0][ascii_code] > UINT16_MAX);
@@ -566,7 +506,7 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
                 }
             }
         }
-        SCFree(q);
+        SCACStateQueueFree(q);
     }
 
     if (!(ctx->state_count < 32767) || construct_both_16_and_32_state_tables) {
@@ -580,10 +520,7 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
         mpm_ctx->memory_cnt++;
         mpm_ctx->memory_size += (ctx->state_count * sizeof(*ctx->state_table_u32));
 
-        StateQueue *q = SCCalloc(1, sizeof(StateQueue));
-        if (q == NULL) {
-            FatalError("Error allocating memory");
-        }
+        StateQueue *q = SCACStateQueueAlloc();
 
         for (ascii_code = 0; ascii_code < 256; ascii_code++) {
             SC_AC_STATE_TYPE_U32 temp_state = ctx->goto_table[0][ascii_code];
@@ -606,7 +543,7 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
                 }
             }
         }
-        SCFree(q);
+        SCACStateQueueFree(q);
     }
 
     return;