Skip to content

Commit 7e97dfd

Browse files
catenacybervictorjulien
authored andcommitted
detect: replace stack alloc by member of DetectEngineThreadCtx
To avoid running a big (when many signatures) stack allocation on each detection loop with postmatches Ticket: 8001
1 parent b7a3a6b commit 7e97dfd

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/detect-engine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,6 +3333,10 @@ static TmEcode ThreadCtxDoInit (DetectEngineCtx *de_ctx, DetectEngineThreadCtx *
33333333
if (det_ctx->match_array == NULL) {
33343334
return TM_ECODE_FAILED;
33353335
}
3336+
det_ctx->replace = SCCalloc(de_ctx->sig_array_len, sizeof(Signature *));
3337+
if (det_ctx->replace == NULL) {
3338+
return TM_ECODE_FAILED;
3339+
}
33363340

33373341
RuleMatchCandidateTxArrayInit(det_ctx, de_ctx->sig_array_len);
33383342
}
@@ -3585,6 +3589,8 @@ static void DetectEngineThreadCtxFree(DetectEngineThreadCtx *det_ctx)
35853589
}
35863590
if (det_ctx->match_array != NULL)
35873591
SCFree(det_ctx->match_array);
3592+
if (det_ctx->replace != NULL)
3593+
SCFree(det_ctx->replace);
35883594

35893595
RuleMatchCandidateTxArrayFree(det_ctx);
35903596

src/detect.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -793,12 +793,9 @@ static inline uint8_t DetectRulePacketRules(ThreadVars *const tv,
793793
/* undo "prefetch" */
794794
if (next_s)
795795
match_array--;
796-
/* create temporary rule pointer array starting
797-
* at where we are in the current match array */
798-
const Signature *replace[de_ctx->sig_array_len]; // TODO heap?
799796
SCLogDebug("sig_array_len %u det_ctx->pmq.rule_id_array_cnt %u",
800797
de_ctx->sig_array_len, det_ctx->pmq.rule_id_array_cnt);
801-
const Signature **r = replace;
798+
const Signature **r = det_ctx->replace;
802799
for (uint32_t x = 0; x < match_cnt; x++) {
803800
*r++ = match_array[x];
804801
SCLogDebug("appended %u", match_array[x]->id);
@@ -814,7 +811,7 @@ static inline uint8_t DetectRulePacketRules(ThreadVars *const tv,
814811
}
815812
}
816813
if (match_cnt > 1) {
817-
qsort(replace, match_cnt, sizeof(Signature *), SortHelper);
814+
qsort(det_ctx->replace, match_cnt, sizeof(Signature *), SortHelper);
818815
}
819816
/* rewrite match_array to include the new additions, and deduplicate
820817
* while at it. */
@@ -828,7 +825,7 @@ static inline uint8_t DetectRulePacketRules(ThreadVars *const tv,
828825
continue;
829826
}
830827
last_sig = *m;
831-
*m++ = (Signature *)replace[x];
828+
*m++ = (Signature *)det_ctx->replace[x];
832829
}
833830
match_cnt -= skipped;
834831
/* prefetch next */

src/detect.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,9 @@ typedef struct DetectEngineThreadCtx_ {
13321332
/** array of signature pointers we're going to inspect in the detection
13331333
* loop. */
13341334
Signature **match_array;
1335+
/** temporary array of signature pointers we're going to inspect in the
1336+
* detection loop. */
1337+
const Signature **replace;
13351338
/** size of the array in items (mem size if * sizeof(Signature *)
13361339
* Only used during initialization. */
13371340
uint32_t match_array_len;

0 commit comments

Comments
 (0)