util/mpm: factorize code

pull/13483/head
Philippe Antoine 1 month ago committed by Victor Julien
parent 330cff94e8
commit 679bd23cb7

@ -567,6 +567,7 @@ noinst_HEADERS = \
util-memrchr.h \
util-misc.h \
util-mpm-ac-ks.h \
util-mpm-ac-queue.h \
util-mpm-ac.h \
util-mpm-hs-cache.h \
util-mpm-hs-core.h \
@ -1149,6 +1150,7 @@ libsuricata_c_a_SOURCES = \
util-misc.c \
util-mpm-ac-ks-small.c \
util-mpm-ac-ks.c \
util-mpm-ac-queue.c \
util-mpm-ac.c \
util-mpm-hs-cache.c \
util-mpm-hs-core.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
@ -147,17 +148,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
@ -401,48 +391,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.
@ -505,10 +453,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) */
@ -545,7 +490,7 @@ static void SCACTileCreateFailureTable(MpmCtx *mpm_ctx)
mpm_ctx);
}
}
SCFree(q);
SCACStateQueueFree(q);
}
/*
@ -679,10 +624,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];
@ -703,7 +645,7 @@ static inline void SCACTileCreateDeltaTable(MpmCtx *mpm_ctx)
}
}
}
SCFree(q);
SCACStateQueueFree(q);
}
static void SCACTileClubOutputStatePresenceWithDeltaTable(MpmCtx *mpm_ctx)

@ -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);
}

@ -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 */

@ -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 SCACDestroyCtx(MpmCtx *);
@ -79,23 +80,12 @@ static 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
@ -360,46 +350,6 @@ static inline void SCACDetermineLevel1Gap(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. 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. 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.
@ -457,10 +407,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) */
@ -497,7 +444,7 @@ static inline void SCACCreateFailureTable(MpmCtx *mpm_ctx)
mpm_ctx);
}
}
SCFree(q);
SCACStateQueueFree(q);
}
/**
@ -520,10 +467,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);
@ -548,7 +492,7 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
}
}
}
SCFree(q);
SCACStateQueueFree(q);
}
if (!(ctx->state_count < 32767) || construct_both_16_and_32_state_tables) {
@ -562,10 +506,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];
@ -588,7 +529,7 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
}
}
}
SCFree(q);
SCACStateQueueFree(q);
}
}

Loading…
Cancel
Save