From: Victor Julien Date: Wed, 12 Jul 2023 06:00:47 +0000 (+0200) Subject: spm: reduce spm_matcher size to uint8_t X-Git-Tag: suricata-7.0.0~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd36459c967219dbb2563408d03fcfe94f893f2d;p=thirdparty%2Fsuricata.git spm: reduce spm_matcher size to uint8_t No more space is needed. --- diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index 39e36bd317..4ae69350df 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -1728,7 +1728,7 @@ int AppLayerProtoDetectSetup(void) memset(&alpd_ctx, 0, sizeof(alpd_ctx)); - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); uint8_t mpm_matcher = PatternMatchDefaultMatcher(); alpd_ctx.spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); diff --git a/src/detect-content.c b/src/detect-content.c index 6a73daa521..6026cf2956 100644 --- a/src/detect-content.c +++ b/src/detect-content.c @@ -913,7 +913,7 @@ static int DetectContentParseTest01 (void) const char *teststring = "abc\\:def"; const char *teststringparsed = "abc:def"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -944,7 +944,7 @@ static int DetectContentParseTest02 (void) const char *teststring = "abc\\;def"; const char *teststringparsed = "abc;def"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -975,7 +975,7 @@ static int DetectContentParseTest03 (void) const char *teststring = "abc\\\"def"; const char *teststringparsed = "abc\"def"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -1006,7 +1006,7 @@ static int DetectContentParseTest04 (void) const char *teststring = "abc\\\\def"; const char *teststringparsed = "abc\\def"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -1037,7 +1037,7 @@ static int DetectContentParseTest05 (void) DetectContentData *cd = NULL; const char *teststring = "abc\\def"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -1063,7 +1063,7 @@ static int DetectContentParseTest06 (void) const char *teststring = "a|42|c|44|e|46|"; const char *teststringparsed = "abcdef"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -1094,7 +1094,7 @@ static int DetectContentParseTest07 (void) DetectContentData *cd = NULL; const char *teststring = ""; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -1117,7 +1117,7 @@ static int DetectContentParseTest08 (void) DetectContentData *cd = NULL; const char *teststring = ""; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -1407,7 +1407,7 @@ static int DetectContentParseTest09(void) DetectContentData *cd = NULL; const char *teststring = "boo"; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -2072,7 +2072,7 @@ static int DetectContentParseTest41(void) } teststring[idx++] = '\0'; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -2105,7 +2105,7 @@ static int DetectContentParseTest42(void) } teststring[idx++] = '\0'; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -2139,7 +2139,7 @@ static int DetectContentParseTest43(void) } teststring[idx++] = '\0'; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); @@ -2176,7 +2176,7 @@ static int DetectContentParseTest44(void) } teststring[idx++] = '\0'; - uint16_t spm_matcher = SinglePatternMatchDefaultMatcher(); + uint8_t spm_matcher = SinglePatternMatchDefaultMatcher(); SpmGlobalThreadCtx *spm_global_thread_ctx = SpmInitGlobalThreadCtx(spm_matcher); FAIL_IF(spm_global_thread_ctx == NULL); diff --git a/src/detect.h b/src/detect.h index 971072b0b3..e462933c2c 100644 --- a/src/detect.h +++ b/src/detect.h @@ -872,7 +872,7 @@ typedef struct DetectEngineCtx_ { uint8_t flags; /**< only DE_QUIET */ uint8_t mpm_matcher; /**< mpm matcher this ctx uses */ - uint16_t spm_matcher; /**< spm matcher this ctx uses */ + uint8_t spm_matcher; /**< spm matcher this ctx uses */ /* maximum recursion depth for content inspection */ int inspection_recursion_limit; diff --git a/src/util-spm.c b/src/util-spm.c index 5352e64ad1..15b33398a6 100644 --- a/src/util-spm.c +++ b/src/util-spm.c @@ -65,7 +65,7 @@ SpmTableElmt spm_table[SPM_TABLE_SIZE]; * \brief Returns the single pattern matcher algorithm to be used, based on the * spm-algo setting in yaml. */ -uint16_t SinglePatternMatchDefaultMatcher(void) +uint8_t SinglePatternMatchDefaultMatcher(void) { const char *spm_algo; if ((ConfGet("spm-algo", &spm_algo)) == 1) { @@ -73,7 +73,7 @@ uint16_t SinglePatternMatchDefaultMatcher(void) if (strcmp("auto", spm_algo) == 0) { goto default_matcher; } - for (uint16_t i = 0; i < SPM_TABLE_SIZE; i++) { + for (uint8_t i = 0; i < SPM_TABLE_SIZE; i++) { if (spm_table[i].name == NULL) { continue; } @@ -135,7 +135,7 @@ void SpmTableSetup(void) #endif } -SpmGlobalThreadCtx *SpmInitGlobalThreadCtx(uint16_t matcher) +SpmGlobalThreadCtx *SpmInitGlobalThreadCtx(uint8_t matcher) { BUG_ON(spm_table[matcher].InitGlobalThreadCtx == NULL); return spm_table[matcher].InitGlobalThreadCtx(); @@ -146,7 +146,7 @@ void SpmDestroyGlobalThreadCtx(SpmGlobalThreadCtx *global_thread_ctx) if (global_thread_ctx == NULL) { return; } - uint16_t matcher = global_thread_ctx->matcher; + uint8_t matcher = global_thread_ctx->matcher; spm_table[matcher].DestroyGlobalThreadCtx(global_thread_ctx); } @@ -155,7 +155,7 @@ SpmThreadCtx *SpmMakeThreadCtx(const SpmGlobalThreadCtx *global_thread_ctx) if (global_thread_ctx == NULL) { return NULL; } - uint16_t matcher = global_thread_ctx->matcher; + uint8_t matcher = global_thread_ctx->matcher; BUG_ON(spm_table[matcher].MakeThreadCtx == NULL); return spm_table[matcher].MakeThreadCtx(global_thread_ctx); } @@ -165,7 +165,7 @@ void SpmDestroyThreadCtx(SpmThreadCtx *thread_ctx) if (thread_ctx == NULL) { return; } - uint16_t matcher = thread_ctx->matcher; + uint8_t matcher = thread_ctx->matcher; BUG_ON(spm_table[matcher].DestroyThreadCtx == NULL); spm_table[matcher].DestroyThreadCtx(thread_ctx); } @@ -174,7 +174,7 @@ SpmCtx *SpmInitCtx(const uint8_t *needle, uint16_t needle_len, int nocase, SpmGlobalThreadCtx *global_thread_ctx) { BUG_ON(global_thread_ctx == NULL); - uint16_t matcher = global_thread_ctx->matcher; + uint8_t matcher = global_thread_ctx->matcher; BUG_ON(spm_table[matcher].InitCtx == NULL); return spm_table[matcher].InitCtx(needle, needle_len, nocase, global_thread_ctx); @@ -185,7 +185,7 @@ void SpmDestroyCtx(SpmCtx *ctx) if (ctx == NULL) { return; } - uint16_t matcher = ctx->matcher; + uint8_t matcher = ctx->matcher; BUG_ON(spm_table[matcher].DestroyCtx == NULL); spm_table[matcher].DestroyCtx(ctx); } @@ -193,7 +193,7 @@ void SpmDestroyCtx(SpmCtx *ctx) uint8_t *SpmScan(const SpmCtx *ctx, SpmThreadCtx *thread_ctx, const uint8_t *haystack, uint32_t haystack_len) { - uint16_t matcher = ctx->matcher; + uint8_t matcher = ctx->matcher; return spm_table[matcher].Scan(ctx, thread_ctx, haystack, haystack_len); } @@ -2478,7 +2478,7 @@ typedef struct SpmTestData_ { } SpmTestData; /* Helper function to conduct a search with a particular SPM matcher. */ -static int SpmTestSearch(const SpmTestData *d, uint16_t matcher) +static int SpmTestSearch(const SpmTestData *d, uint8_t matcher) { int ret = 1; SpmGlobalThreadCtx *global_thread_ctx = NULL; @@ -2572,7 +2572,7 @@ static int SpmSearchTest01(void) { int ret = 1; - uint16_t matcher; + uint8_t matcher; for (matcher = 0; matcher < SPM_TABLE_SIZE; matcher++) { const SpmTableElmt *m = &spm_table[matcher]; if (m->name == NULL) { @@ -2614,7 +2614,7 @@ static int SpmSearchTest02(void) { int ret = 1; - uint16_t matcher; + uint8_t matcher; for (matcher = 0; matcher < SPM_TABLE_SIZE; matcher++) { const SpmTableElmt *m = &spm_table[matcher]; if (m->name == NULL) { diff --git a/src/util-spm.h b/src/util-spm.h index ae3fe7f025..9893dcd422 100644 --- a/src/util-spm.h +++ b/src/util-spm.h @@ -33,26 +33,26 @@ enum { SPM_TABLE_SIZE }; -uint16_t SinglePatternMatchDefaultMatcher(void); +uint8_t SinglePatternMatchDefaultMatcher(void); /** Structure holding an immutable "built" SPM matcher (such as the Boyer-Moore * tables, Hyperscan database etc) that is passed to the Scan call. */ typedef struct SpmCtx_ { - uint16_t matcher; + uint8_t matcher; void *ctx; } SpmCtx; /** Structure holding a global prototype for per-thread scratch space, passed * to each InitCtx call. */ typedef struct SpmGlobalThreadCtx_ { - uint16_t matcher; + uint8_t matcher; void *ctx; } SpmGlobalThreadCtx; /** Structure holding some mutable per-thread space for use by a matcher at * scan time. Constructed from SpmGlobalThreadCtx by the MakeThreadCtx call. */ typedef struct SpmThreadCtx_ { - uint16_t matcher; + uint8_t matcher; void *ctx; } SpmThreadCtx; @@ -73,7 +73,7 @@ extern SpmTableElmt spm_table[SPM_TABLE_SIZE]; void SpmTableSetup(void); -SpmGlobalThreadCtx *SpmInitGlobalThreadCtx(uint16_t matcher); +SpmGlobalThreadCtx *SpmInitGlobalThreadCtx(uint8_t matcher); void SpmDestroyGlobalThreadCtx(SpmGlobalThreadCtx *g_thread_ctx);