From: Philippe Antoine Date: Tue, 17 Jun 2025 13:06:08 +0000 (+0200) Subject: util/mpm: factorize code X-Git-Tag: suricata-7.0.11~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51b3254dfaff8a89055f8cc83a924c262f34012a;p=thirdparty%2Fsuricata.git util/mpm: factorize code (cherry picked from commit 679bd23cb70a7299f1b7f56275a334d7f1b15d78) --- diff --git a/src/Makefile.am b/src/Makefile.am index 8377d39e48..2ac4679f2b 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/util-mpm-ac-ks.c b/src/util-mpm-ac-ks.c index e6d0f67ff1..a26484097b 100644 --- a/src/util-mpm-ac-ks.c +++ b/src/util-mpm-ac-ks.c @@ -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 index 0000000000..40bb951a03 --- /dev/null +++ b/src/util-mpm-ac-queue.c @@ -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 index 0000000000..3bf8b41f9a --- /dev/null +++ b/src/util-mpm-ac-queue.h @@ -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 + * + */ + +#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 */ diff --git a/src/util-mpm-ac.c b/src/util-mpm-ac.c index 961031e3a9..74a6075694 100644 --- a/src/util-mpm-ac.c +++ b/src/util-mpm-ac.c @@ -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;