diff --git a/src/util-mpm-ac.c b/src/util-mpm-ac.c index a0518545d1..9f24bfc46c 100644 --- a/src/util-mpm-ac.c +++ b/src/util-mpm-ac.c @@ -861,9 +861,8 @@ uint32_t SCACSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, * to dig deeper */ /* \todo Change it for stateful MPM. Supply the state using mpm_thread_ctx */ const SCACPatternList *pid_pat_list = ctx->pid_pat_list; - - uint8_t bitarray[ctx->pattern_id_bitarray_size]; - memset(bitarray, 0, ctx->pattern_id_bitarray_size); + uint8_t *bitarray = (uint8_t *)mpm_thread_ctx->ctx; + memset(bitarray, 0, mpm_thread_ctx->memory_size); if (ctx->state_count < 32767) { register SC_AC_STATE_TYPE_U16 state = 0; @@ -1029,6 +1028,32 @@ void SCACPrintInfo(MpmCtx *mpm_ctx) printf("\n"); } +/** + * \brief Init the mpm thread context. + * + * \param mpm_ctx Pointer to the mpm context. + * \param mpm_thread_ctx Pointer to the mpm thread context. + */ +static void SCACInitThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) +{ + uint32_t size = (mpm_ctx->max_pat_id / 8) + 1; + + uint8_t *bitarray = SCCalloc(size, sizeof(uint8_t)); + if (bitarray == NULL) { + exit(EXIT_FAILURE); + } + mpm_thread_ctx->ctx = bitarray; + mpm_thread_ctx->memory_cnt = 1; + mpm_thread_ctx->memory_size = size; +} + +static void SCACDestroyThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) +{ + mpm_thread_ctx->memory_cnt = 0; + mpm_thread_ctx->memory_size = 0; + SCFree(mpm_thread_ctx->ctx); + mpm_thread_ctx->ctx = NULL; +} /************************** Mpm Registration ***************************/ @@ -1049,6 +1074,8 @@ void MpmACRegister(void) mpm_table[MPM_AC].CacheRuleset = NULL; mpm_table[MPM_AC].Search = SCACSearch; mpm_table[MPM_AC].PrintCtx = SCACPrintInfo; + mpm_table[MPM_AC].InitThreadCtx = SCACInitThreadCtx; + mpm_table[MPM_AC].DestroyThreadCtx = SCACDestroyThreadCtx; #ifdef UNITTESTS mpm_table[MPM_AC].RegisterUnittests = SCACRegisterTests; #endif @@ -1076,6 +1103,7 @@ static int SCACTest01(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghjiklmnopqrstuvwxyz"; @@ -1088,6 +1116,7 @@ static int SCACTest01(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1108,6 +1137,7 @@ static int SCACTest02(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghjiklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1119,6 +1149,7 @@ static int SCACTest02(void) printf("0 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1143,6 +1174,7 @@ static int SCACTest03(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghjiklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1154,6 +1186,7 @@ static int SCACTest03(void) printf("3 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1175,6 +1208,7 @@ static int SCACTest04(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghjiklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1186,6 +1220,7 @@ static int SCACTest04(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1207,6 +1242,7 @@ static int SCACTest05(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghjiklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1218,6 +1254,7 @@ static int SCACTest05(void) printf("3 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1237,6 +1274,7 @@ static int SCACTest06(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcd"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1248,6 +1286,7 @@ static int SCACTest06(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1279,6 +1318,7 @@ static int SCACTest07(void) /* total matches: 135: unique matches: 6 */ SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1286,6 +1326,7 @@ static int SCACTest07(void) FAIL_IF_NOT(cnt == 6); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); PASS; } @@ -1306,6 +1347,7 @@ static int SCACTest08(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"a", 1); @@ -1316,6 +1358,7 @@ static int SCACTest08(void) printf("0 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1336,6 +1379,7 @@ static int SCACTest09(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"ab", 2); @@ -1346,6 +1390,7 @@ static int SCACTest09(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1366,6 +1411,7 @@ static int SCACTest10(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "01234567890123456789012345678901234567890123456789" "01234567890123456789012345678901234567890123456789" @@ -1381,6 +1427,7 @@ static int SCACTest10(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1409,6 +1456,8 @@ static int SCACTest11(void) if (SCACPreparePatterns(NULL, &mpm_ctx) == -1) goto end; + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); + result = 1; const char *buf = "he"; @@ -1426,6 +1475,7 @@ static int SCACTest11(void) end: SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1448,6 +1498,7 @@ static int SCACTest12(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1459,6 +1510,7 @@ static int SCACTest12(void) printf("2 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1480,6 +1532,7 @@ static int SCACTest13(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyzABCD"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1491,6 +1544,7 @@ static int SCACTest13(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1512,6 +1566,7 @@ static int SCACTest14(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyzABCDE"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1523,6 +1578,7 @@ static int SCACTest14(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1544,6 +1600,7 @@ static int SCACTest15(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyzABCDEF"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1555,6 +1612,7 @@ static int SCACTest15(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1576,6 +1634,7 @@ static int SCACTest16(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyzABC"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1587,6 +1646,7 @@ static int SCACTest16(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1608,6 +1668,7 @@ static int SCACTest17(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyzAB"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1619,6 +1680,7 @@ static int SCACTest17(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1645,6 +1707,7 @@ static int SCACTest18(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcde""fghij""klmno""pqrst""uvwxy""z"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1656,6 +1719,7 @@ static int SCACTest18(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1677,6 +1741,7 @@ static int SCACTest19(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1688,6 +1753,7 @@ static int SCACTest19(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1715,6 +1781,7 @@ static int SCACTest20(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AA"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1726,6 +1793,7 @@ static int SCACTest20(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1746,6 +1814,7 @@ static int SCACTest21(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"AA", 2); @@ -1756,6 +1825,7 @@ static int SCACTest21(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1778,6 +1848,7 @@ static int SCACTest22(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "abcdefghijklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1789,6 +1860,7 @@ static int SCACTest22(void) printf("2 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1809,6 +1881,7 @@ static int SCACTest23(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"aa", 2); @@ -1819,6 +1892,7 @@ static int SCACTest23(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1839,6 +1913,7 @@ static int SCACTest24(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"aa", 2); @@ -1849,6 +1924,7 @@ static int SCACTest24(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1870,6 +1946,7 @@ static int SCACTest25(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1881,6 +1958,7 @@ static int SCACTest25(void) printf("3 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1901,6 +1979,7 @@ static int SCACTest26(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "works"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1912,6 +1991,7 @@ static int SCACTest26(void) printf("3 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1932,6 +2012,7 @@ static int SCACTest27(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "tone"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1943,6 +2024,7 @@ static int SCACTest27(void) printf("0 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } @@ -1962,6 +2044,7 @@ static int SCACTest28(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf = "tONE"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, @@ -1969,6 +2052,7 @@ static int SCACTest28(void) FAIL_IF_NOT(cnt == 0); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); PASS; } @@ -2030,6 +2114,7 @@ static int SCACTest30(void) PmqSetup(&pmq); SCACPreparePatterns(NULL, &mpm_ctx); + SCACInitThreadCtx(&mpm_ctx, &mpm_thread_ctx); const char *buf1 = "abcdefghijklmnopqrstuvwxyz"; uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf1, strlen(buf1)); @@ -2039,6 +2124,7 @@ static int SCACTest30(void) FAIL_IF_NOT(cnt == 0); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); PASS; }