From 0eaf0b0129576a899175e4287c9c3e96cd3f97af Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Mon, 2 Jul 2012 22:24:37 +0530 Subject: [PATCH] mpm engine and ac mem free fixes --- src/detect-engine-mpm.c | 170 ++++++++++++++++++++++++++++++++++++++++ src/util-mpm-ac.c | 21 +++++ 2 files changed, 191 insertions(+) diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index aeddc04b45..909dd1ee90 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -886,6 +886,176 @@ void PatternMatchDestroyGroup(SigGroupHead *sh) { } } + if (sh->mpm_hcbd_ctx_ts != NULL || sh->mpm_hcbd_ctx_tc != NULL) { + if (sh->mpm_hcbd_ctx_ts != NULL) { + if (!sh->mpm_hcbd_ctx_ts->global) { + mpm_table[sh->mpm_hcbd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hcbd_ctx_ts); + SCFree(sh->mpm_hcbd_ctx_ts); + } + sh->mpm_hcbd_ctx_ts = NULL; + } + if (sh->mpm_hcbd_ctx_tc != NULL) { + if (!sh->mpm_hcbd_ctx_tc->global) { + mpm_table[sh->mpm_hcbd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hcbd_ctx_tc); + SCFree(sh->mpm_hcbd_ctx_tc); + } + sh->mpm_hcbd_ctx_tc = NULL; + } + } + + if (sh->mpm_hsbd_ctx_ts != NULL || sh->mpm_hsbd_ctx_tc != NULL) { + if (sh->mpm_hsbd_ctx_ts != NULL) { + if (!sh->mpm_hsbd_ctx_ts->global) { + mpm_table[sh->mpm_hsbd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hsbd_ctx_ts); + SCFree(sh->mpm_hsbd_ctx_ts); + } + sh->mpm_hsbd_ctx_ts = NULL; + } + if (sh->mpm_hsbd_ctx_tc != NULL) { + if (!sh->mpm_hsbd_ctx_tc->global) { + mpm_table[sh->mpm_hsbd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hsbd_ctx_tc); + SCFree(sh->mpm_hsbd_ctx_tc); + } + sh->mpm_hsbd_ctx_tc = NULL; + } + } + + if (sh->mpm_hhd_ctx_ts != NULL || sh->mpm_hhd_ctx_tc != NULL) { + if (sh->mpm_hhd_ctx_ts != NULL) { + if (!sh->mpm_hhd_ctx_ts->global) { + mpm_table[sh->mpm_hhd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hhd_ctx_ts); + SCFree(sh->mpm_hhd_ctx_ts); + } + sh->mpm_hhd_ctx_ts = NULL; + } + if (sh->mpm_hhd_ctx_tc != NULL) { + if (!sh->mpm_hhd_ctx_tc->global) { + mpm_table[sh->mpm_hhd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hhd_ctx_tc); + SCFree(sh->mpm_hhd_ctx_tc); + } + sh->mpm_hhd_ctx_tc = NULL; + } + } + + if (sh->mpm_hrhd_ctx_ts != NULL || sh->mpm_hrhd_ctx_tc != NULL) { + if (sh->mpm_hrhd_ctx_ts != NULL) { + if (!sh->mpm_hrhd_ctx_ts->global) { + mpm_table[sh->mpm_hrhd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hrhd_ctx_ts); + SCFree(sh->mpm_hrhd_ctx_ts); + } + sh->mpm_hrhd_ctx_ts = NULL; + } + if (sh->mpm_hrhd_ctx_tc != NULL) { + if (!sh->mpm_hrhd_ctx_tc->global) { + mpm_table[sh->mpm_hrhd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hrhd_ctx_tc); + SCFree(sh->mpm_hrhd_ctx_tc); + } + sh->mpm_hrhd_ctx_tc = NULL; + } + } + + if (sh->mpm_hmd_ctx_ts != NULL || sh->mpm_hmd_ctx_tc != NULL) { + if (sh->mpm_hmd_ctx_ts != NULL) { + if (!sh->mpm_hmd_ctx_ts->global) { + mpm_table[sh->mpm_hmd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hmd_ctx_ts); + SCFree(sh->mpm_hmd_ctx_ts); + } + sh->mpm_hmd_ctx_ts = NULL; + } + if (sh->mpm_hmd_ctx_tc != NULL) { + if (!sh->mpm_hmd_ctx_tc->global) { + mpm_table[sh->mpm_hmd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hmd_ctx_tc); + SCFree(sh->mpm_hmd_ctx_tc); + } + sh->mpm_hmd_ctx_tc = NULL; + } + } + + if (sh->mpm_hcd_ctx_ts != NULL || sh->mpm_hcd_ctx_tc != NULL) { + if (sh->mpm_hcd_ctx_ts != NULL) { + if (!sh->mpm_hcd_ctx_ts->global) { + mpm_table[sh->mpm_hcd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hcd_ctx_ts); + SCFree(sh->mpm_hcd_ctx_ts); + } + sh->mpm_hcd_ctx_ts = NULL; + } + if (sh->mpm_hcd_ctx_tc != NULL) { + if (!sh->mpm_hcd_ctx_tc->global) { + mpm_table[sh->mpm_hcd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hcd_ctx_tc); + SCFree(sh->mpm_hcd_ctx_tc); + } + sh->mpm_hcd_ctx_tc = NULL; + } + } + + if (sh->mpm_hrud_ctx_ts != NULL || sh->mpm_hrud_ctx_tc != NULL) { + if (sh->mpm_hrud_ctx_ts != NULL) { + if (!sh->mpm_hrud_ctx_ts->global) { + mpm_table[sh->mpm_hrud_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hrud_ctx_ts); + SCFree(sh->mpm_hrud_ctx_ts); + } + sh->mpm_hrud_ctx_ts = NULL; + } + if (sh->mpm_hrud_ctx_tc != NULL) { + if (!sh->mpm_hrud_ctx_tc->global) { + mpm_table[sh->mpm_hrud_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hrud_ctx_tc); + SCFree(sh->mpm_hrud_ctx_tc); + } + sh->mpm_hrud_ctx_tc = NULL; + } + } + + if (sh->mpm_hsmd_ctx_ts != NULL || sh->mpm_hsmd_ctx_tc != NULL) { + if (sh->mpm_hsmd_ctx_ts != NULL) { + if (!sh->mpm_hsmd_ctx_ts->global) { + mpm_table[sh->mpm_hsmd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hsmd_ctx_ts); + SCFree(sh->mpm_hsmd_ctx_ts); + } + sh->mpm_hsmd_ctx_ts = NULL; + } + if (sh->mpm_hsmd_ctx_tc != NULL) { + if (!sh->mpm_hsmd_ctx_tc->global) { + mpm_table[sh->mpm_hsmd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hsmd_ctx_tc); + SCFree(sh->mpm_hsmd_ctx_tc); + } + sh->mpm_hsmd_ctx_tc = NULL; + } + } + + if (sh->mpm_hscd_ctx_ts != NULL || sh->mpm_hscd_ctx_tc != NULL) { + if (sh->mpm_hscd_ctx_ts != NULL) { + if (!sh->mpm_hscd_ctx_ts->global) { + mpm_table[sh->mpm_hscd_ctx_ts->mpm_type].DestroyCtx(sh->mpm_hscd_ctx_ts); + SCFree(sh->mpm_hscd_ctx_ts); + } + sh->mpm_hscd_ctx_ts = NULL; + } + if (sh->mpm_hscd_ctx_tc != NULL) { + if (!sh->mpm_hscd_ctx_tc->global) { + mpm_table[sh->mpm_hscd_ctx_tc->mpm_type].DestroyCtx(sh->mpm_hscd_ctx_tc); + SCFree(sh->mpm_hscd_ctx_tc); + } + sh->mpm_hscd_ctx_tc = NULL; + } + } + + if (sh->mpm_huad_ctx_ts != NULL || sh->mpm_huad_ctx_tc != NULL) { + if (sh->mpm_huad_ctx_ts != NULL) { + if (!sh->mpm_huad_ctx_ts->global) { + mpm_table[sh->mpm_huad_ctx_ts->mpm_type].DestroyCtx(sh->mpm_huad_ctx_ts); + SCFree(sh->mpm_huad_ctx_ts); + } + sh->mpm_huad_ctx_ts = NULL; + } + if (sh->mpm_huad_ctx_tc != NULL) { + if (!sh->mpm_huad_ctx_tc->global) { + mpm_table[sh->mpm_huad_ctx_tc->mpm_type].DestroyCtx(sh->mpm_huad_ctx_tc); + SCFree(sh->mpm_huad_ctx_tc); + } + sh->mpm_huad_ctx_tc = NULL; + } + } + return; } diff --git a/src/util-mpm-ac.c b/src/util-mpm-ac.c index dc520f1124..e6306e2aa6 100644 --- a/src/util-mpm-ac.c +++ b/src/util-mpm-ac.c @@ -234,6 +234,7 @@ static inline SCACPattern *SCACAllocPattern(MpmCtx *mpm_ctx) * * \param mpm_ctx Pointer to the mpm context. * \param p Pointer to the SCACPattern instance to be freed. + * \param free Free the above pointer or not. */ static inline void SCACFreePattern(MpmCtx *mpm_ctx, SCACPattern *p) { @@ -1174,6 +1175,25 @@ void SCACDestroyCtx(MpmCtx *mpm_ctx) sizeof(SC_AC_STATE_TYPE_U32) * 256); } + if (ctx->output_table != NULL) { + uint32_t state_count; + for (state_count = 0; state_count < ctx->state_count; state_count++) { + if (ctx->output_table[state_count].pids != NULL) { + SCFree(ctx->output_table[state_count].pids); + } + } + SCFree(ctx->output_table); + } + + if (ctx->pid_pat_list != NULL) { + int i; + for (i = 0; i < (ctx->max_pat_id + 1); i++) { + if (ctx->pid_pat_list[i].cs != NULL) + SCFree(ctx->pid_pat_list[i].cs); + } + SCFree(ctx->pid_pat_list); + } + SCFree(mpm_ctx->ctx); mpm_ctx->memory_cnt--; mpm_ctx->memory_size -= sizeof(SCACCtx); @@ -2043,6 +2063,7 @@ static int SCACTest19(void) printf("1 != %" PRIu32 " ",cnt); SCACDestroyCtx(&mpm_ctx); + SCACDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx); PmqFree(&pmq); return result; } -- 2.47.2