suricata
util-mpm-hs.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Jim Xu <jim.xu@windriver.com>
22  * \author Justin Viiret <justin.viiret@intel.com>
23  *
24  * MPM pattern matcher that calls the Hyperscan regex matcher.
25  */
26 
27 #include "suricata-common.h"
28 #include "suricata.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-engine-build.h"
34 
35 #include "conf.h"
36 #include "util-conf.h"
37 #include "util-debug.h"
38 #include "util-unittest.h"
39 #include "util-unittest-helper.h"
40 #include "util-memcmp.h"
41 #include "util-mpm-hs.h"
42 #include "util-mpm-hs-cache.h"
43 #include "util-mpm-hs-core.h"
44 #include "util-memcpy.h"
45 #include "util-hash.h"
46 #include "util-hash-lookup3.h"
47 #include "util-hyperscan.h"
48 #include "util-path.h"
49 
50 #ifdef BUILD_HYPERSCAN
51 
52 void SCHSInitCtx(MpmCtx *);
53 void SCHSInitThreadCtx(MpmCtx *, MpmThreadCtx *);
54 void SCHSDestroyCtx(MpmCtx *);
55 void SCHSDestroyThreadCtx(MpmCtx *, MpmThreadCtx *);
56 int SCHSAddPatternCI(
57  MpmCtx *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t);
58 int SCHSAddPatternCS(MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t,
59  uint32_t, SigIntId, uint8_t);
60 int SCHSPreparePatterns(MpmConfig *mpm_conf, MpmCtx *mpm_ctx);
61 uint32_t SCHSSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
62  PrefilterRuleStore *pmq, const uint8_t *buf, const uint32_t buflen);
63 void SCHSPrintInfo(MpmCtx *mpm_ctx);
64 void SCHSPrintSearchStats(MpmThreadCtx *mpm_thread_ctx);
65 #ifdef UNITTESTS
66 static void SCHSRegisterTests(void);
67 #endif
68 
69 /* size of the hash table used to speed up pattern insertions initially */
70 #define INIT_HASH_SIZE 65536
71 
72 /* Initial size of the global database hash (used for de-duplication). */
73 #define INIT_DB_HASH_SIZE 1000
74 
75 /* Global prototype scratch, built incrementally as Hyperscan databases are
76  * built and then cloned for each thread context. Access is serialised via
77  * g_scratch_proto_mutex. */
78 static hs_scratch_t *g_scratch_proto = NULL;
79 static SCMutex g_scratch_proto_mutex = SCMUTEX_INITIALIZER;
80 
81 /* Global hash table of Hyperscan databases, used for de-duplication. Access is
82  * serialised via g_db_table_mutex. */
83 static HashTable *g_db_table = NULL;
84 static SCMutex g_db_table_mutex = SCMUTEX_INITIALIZER;
85 
86 /**
87  * \internal
88  * \brief Wraps SCMalloc (which is a macro) so that it can be passed to
89  * hs_set_allocator() for Hyperscan's use.
90  */
91 static void *SCHSMalloc(size_t size)
92 {
93  return SCMalloc(size);
94 }
95 
96 /**
97  * \internal
98  * \brief Wraps SCFree (which is a macro) so that it can be passed to
99  * hs_set_allocator() for Hyperscan's use.
100  */
101 static void SCHSFree(void *ptr)
102 {
103  SCFree(ptr);
104 }
105 
106 /** \brief Register Suricata malloc/free with Hyperscan.
107  *
108  * Requests that Hyperscan use Suricata's allocator for allocation of
109  * databases, scratch space, etc.
110  */
111 static void SCHSSetAllocators(void)
112 {
113  hs_error_t err = hs_set_allocator(SCHSMalloc, SCHSFree);
114  if (err != HS_SUCCESS) {
115  FatalError("Failed to set Hyperscan allocator.");
116  }
117 }
118 
119 /**
120  * \internal
121  * \brief Creates a hash of the pattern. We use it for the hashing process
122  * during the initial pattern insertion time, to cull duplicate sigs.
123  *
124  * \param pat Pointer to the pattern.
125  * \param patlen Pattern length.
126  *
127  * \retval hash A 32 bit unsigned hash.
128  */
129 static inline uint32_t SCHSInitHashRaw(const uint8_t *pat, uint16_t patlen)
130 {
131  uint32_t hash = patlen * pat[0];
132  if (patlen > 1)
133  hash += pat[1];
134 
135  return (hash % INIT_HASH_SIZE);
136 }
137 
138 /**
139  * \internal
140  * \brief Looks up a pattern. We use it for the hashing process during
141  * the initial pattern insertion time, to cull duplicate sigs.
142  *
143  * \param ctx Pointer to the HS ctx.
144  * \param pat Pointer to the pattern.
145  * \param patlen Pattern length.
146  * \param flags Flags. We don't need this.
147  *
148  * \retval hash A 32 bit unsigned hash.
149  */
150 static inline SCHSPattern *SCHSInitHashLookup(SCHSCtx *ctx, const uint8_t *pat, uint16_t patlen,
151  uint16_t offset, uint16_t depth, char flags, uint32_t pid)
152 {
153  uint32_t hash = SCHSInitHashRaw(pat, patlen);
154 
155  if (ctx->init_hash == NULL) {
156  return NULL;
157  }
158 
159  SCHSPattern *t = ctx->init_hash[hash];
160  for (; t != NULL; t = t->next) {
161  /* Since Hyperscan uses offset/depth, we must distinguish between
162  * patterns with the same ID but different offset/depth here. */
163  if (t->id == pid && t->offset == offset && t->depth == depth) {
164  BUG_ON(t->len != patlen);
165  BUG_ON(SCMemcmp(t->original_pat, pat, patlen) != 0);
166  return t;
167  }
168  }
169 
170  return NULL;
171 }
172 
173 /**
174  * \internal
175  * \brief Allocates a new pattern instance.
176  *
177  * \param mpm_ctx Pointer to the mpm context.
178  *
179  * \retval p Pointer to the newly created pattern.
180  */
181 static inline SCHSPattern *SCHSAllocPattern(MpmCtx *mpm_ctx)
182 {
183  SCHSPattern *p = SCCalloc(1, sizeof(SCHSPattern));
184  if (unlikely(p == NULL)) {
185  exit(EXIT_FAILURE);
186  }
187 
188  mpm_ctx->memory_cnt++;
189  mpm_ctx->memory_size += sizeof(SCHSPattern);
190 
191  return p;
192 }
193 
194 /**
195  * \internal
196  * \brief Used to free SCHSPattern instances.
197  *
198  * \param mpm_ctx Pointer to the mpm context.
199  * \param p Pointer to the SCHSPattern instance to be freed.
200  * \param free Free the above pointer or not.
201  */
202 static inline void SCHSFreePattern(MpmCtx *mpm_ctx, SCHSPattern *p)
203 {
204  if (p != NULL && p->original_pat != NULL) {
205  SCFree(p->original_pat);
206  mpm_ctx->memory_cnt--;
207  mpm_ctx->memory_size -= p->len;
208  }
209 
210  if (p != NULL && p->sids != NULL) {
211  SCFree(p->sids);
212  }
213 
214  if (p != NULL) {
215  SCFree(p);
216  mpm_ctx->memory_cnt--;
217  mpm_ctx->memory_size -= sizeof(SCHSPattern);
218  }
219 }
220 
221 static inline uint32_t SCHSInitHash(SCHSPattern *p)
222 {
223  uint32_t hash = p->len * p->original_pat[0];
224  if (p->len > 1)
225  hash += p->original_pat[1];
226 
227  return (hash % INIT_HASH_SIZE);
228 }
229 
230 static inline int SCHSInitHashAdd(SCHSCtx *ctx, SCHSPattern *p)
231 {
232  uint32_t hash = SCHSInitHash(p);
233 
234  if (ctx->init_hash == NULL) {
235  return -1;
236  }
237 
238  if (ctx->init_hash[hash] == NULL) {
239  ctx->init_hash[hash] = p;
240  return 0;
241  }
242 
243  SCHSPattern *tt = NULL;
244  SCHSPattern *t = ctx->init_hash[hash];
245 
246  /* get the list tail */
247  do {
248  tt = t;
249  t = t->next;
250  } while (t != NULL);
251 
252  tt->next = p;
253 
254  return 0;
255 }
256 
257 /**
258  * \internal
259  * \brief Add a pattern to the mpm-hs context.
260  *
261  * \param mpm_ctx Mpm context.
262  * \param pat Pointer to the pattern.
263  * \param patlen Length of the pattern.
264  * \param pid Pattern id
265  * \param sid Signature id (internal id).
266  * \param flags Pattern's MPM_PATTERN_* flags.
267  *
268  * \retval 0 On success.
269  * \retval -1 On failure.
270  */
271 static int SCHSAddPattern(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset,
272  uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
273 {
274  SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
275 
276  if (offset != 0) {
278  }
279  if (depth != 0) {
281  }
282 
283  if (patlen == 0) {
284  SCLogWarning("pattern length 0");
285  return 0;
286  }
287 
288  /* check if we have already inserted this pattern */
289  SCHSPattern *p =
290  SCHSInitHashLookup(ctx, pat, patlen, offset, depth, flags, pid);
291  if (p == NULL) {
292  SCLogDebug("Allocing new pattern");
293 
294  /* p will never be NULL */
295  p = SCHSAllocPattern(mpm_ctx);
296 
297  p->len = patlen;
298  p->flags = flags;
299  p->id = pid;
300 
301  p->offset = offset;
302  p->depth = depth;
303 
304  p->original_pat = SCMalloc(patlen);
305  if (p->original_pat == NULL)
306  goto error;
307  mpm_ctx->memory_cnt++;
308  mpm_ctx->memory_size += patlen;
309  memcpy(p->original_pat, pat, patlen);
310 
311  /* put in the pattern hash */
312  if (SCHSInitHashAdd(ctx, p) != 0)
313  goto error;
314 
315  mpm_ctx->pattern_cnt++;
316 
317  if (!(mpm_ctx->flags & MPMCTX_FLAGS_NODEPTH)) {
318  if (depth) {
319  mpm_ctx->maxdepth = MAX(mpm_ctx->maxdepth, depth);
320  SCLogDebug("%p: depth %u max %u", mpm_ctx, depth, mpm_ctx->maxdepth);
321  } else {
322  mpm_ctx->flags |= MPMCTX_FLAGS_NODEPTH;
323  mpm_ctx->maxdepth = 0;
324  SCLogDebug("%p: alas, no depth for us", mpm_ctx);
325  }
326  }
327 
328  if (mpm_ctx->maxlen < patlen)
329  mpm_ctx->maxlen = patlen;
330 
331  if (mpm_ctx->minlen == 0) {
332  mpm_ctx->minlen = patlen;
333  } else {
334  if (mpm_ctx->minlen > patlen)
335  mpm_ctx->minlen = patlen;
336  }
337 
338  p->sids_size = 1;
339  p->sids = SCMalloc(p->sids_size * sizeof(SigIntId));
340  BUG_ON(p->sids == NULL);
341  p->sids[0] = sid;
342  } else {
343  /* TODO figure out how we can be called multiple times for the same CTX with the same sid */
344 
345  int found = 0;
346  uint32_t x = 0;
347  for (x = 0; x < p->sids_size; x++) {
348  if (p->sids[x] == sid) {
349  found = 1;
350  break;
351  }
352  }
353  if (!found) {
354  SigIntId *sids = SCRealloc(p->sids, (sizeof(SigIntId) * (p->sids_size + 1)));
355  BUG_ON(sids == NULL);
356  p->sids = sids;
357  p->sids[p->sids_size] = sid;
358  p->sids_size++;
359  }
360  }
361 
362  return 0;
363 
364 error:
365  SCHSFreePattern(mpm_ctx, p);
366  return -1;
367 }
368 
369 /**
370  * \brief Pattern database information used only as input to the Hyperscan
371  * compiler.
372  */
373 typedef struct SCHSCompileData_ {
374  unsigned int *ids;
375  unsigned int *flags;
376  char **expressions;
377  hs_expr_ext_t **ext;
378  unsigned int pattern_cnt;
379 } SCHSCompileData;
380 
381 static SCHSCompileData *CompileDataAlloc(unsigned int pattern_cnt)
382 {
383  SCHSCompileData *cd = SCCalloc(pattern_cnt, sizeof(SCHSCompileData));
384  if (cd == NULL) {
385  goto error;
386  }
387 
388  cd->pattern_cnt = pattern_cnt;
389 
390  cd->ids = SCCalloc(pattern_cnt, sizeof(unsigned int));
391  if (cd->ids == NULL) {
392  goto error;
393  }
394 
395  cd->flags = SCCalloc(pattern_cnt, sizeof(unsigned int));
396  if (cd->flags == NULL) {
397  goto error;
398  }
399 
400  cd->expressions = SCCalloc(pattern_cnt, sizeof(char *));
401  if (cd->expressions == NULL) {
402  goto error;
403  }
404 
405  cd->ext = SCCalloc(pattern_cnt, sizeof(hs_expr_ext_t *));
406  if (cd->ext == NULL) {
407  goto error;
408  }
409 
410  return cd;
411 
412 error:
413  SCLogDebug("SCHSCompileData alloc failed");
414  if (cd) {
415  SCFree(cd->ids);
416  SCFree(cd->flags);
417  SCFree(cd->expressions);
418  SCFree(cd->ext);
419  SCFree(cd);
420  }
421  return NULL;
422 }
423 
424 static void CompileDataFree(SCHSCompileData *cd)
425 {
426  if (cd == NULL) {
427  return;
428  }
429 
430  SCFree(cd->ids);
431  SCFree(cd->flags);
432  if (cd->expressions) {
433  for (unsigned int i = 0; i < cd->pattern_cnt; i++) {
434  SCFree(cd->expressions[i]);
435  }
436  SCFree(cd->expressions);
437  }
438  if (cd->ext) {
439  for (unsigned int i = 0; i < cd->pattern_cnt; i++) {
440  SCFree(cd->ext[i]);
441  }
442  SCFree(cd->ext);
443  }
444  SCFree(cd);
445 }
446 
447 static uint32_t SCHSPatternHash(const SCHSPattern *p, uint32_t hash)
448 {
449  BUG_ON(p->original_pat == NULL);
450  BUG_ON(p->sids == NULL);
451 
452  hash = hashlittle_safe(&p->len, sizeof(p->len), hash);
453  hash = hashlittle_safe(&p->flags, sizeof(p->flags), hash);
454  hash = hashlittle_safe(p->original_pat, p->len, hash);
455  hash = hashlittle_safe(&p->id, sizeof(p->id), hash);
456  hash = hashlittle_safe(&p->offset, sizeof(p->offset), hash);
457  hash = hashlittle_safe(&p->depth, sizeof(p->depth), hash);
458  hash = hashlittle_safe(&p->sids_size, sizeof(p->sids_size), hash);
459  hash = hashlittle_safe(p->sids, p->sids_size * sizeof(SigIntId), hash);
460  return hash;
461 }
462 
463 static char SCHSPatternCompare(const SCHSPattern *p1, const SCHSPattern *p2)
464 {
465  if ((p1->len != p2->len) || (p1->flags != p2->flags) ||
466  (p1->id != p2->id) || (p1->offset != p2->offset) ||
467  (p1->depth != p2->depth) || (p1->sids_size != p2->sids_size)) {
468  return 0;
469  }
470 
471  if (SCMemcmp(p1->original_pat, p2->original_pat, p1->len) != 0) {
472  return 0;
473  }
474 
475  if (SCMemcmp(p1->sids, p2->sids, p1->sids_size * sizeof(p1->sids[0])) !=
476  0) {
477  return 0;
478  }
479 
480  return 1;
481 }
482 
483 static uint32_t PatternDatabaseHash(HashTable *ht, void *data, uint16_t len)
484 {
485  const PatternDatabase *pd = data;
486  uint32_t hash = 0;
487  hash = hashword(&pd->pattern_cnt, 1, hash);
488 
489  for (uint32_t i = 0; i < pd->pattern_cnt; i++) {
490  hash = SCHSPatternHash(pd->parray[i], hash);
491  }
492 
493  hash %= ht->array_size;
494  return hash;
495 }
496 
497 static char PatternDatabaseCompare(void *data1, uint16_t len1, void *data2,
498  uint16_t len2)
499 {
500  const PatternDatabase *pd1 = data1;
501  const PatternDatabase *pd2 = data2;
502 
503  if (pd1->pattern_cnt != pd2->pattern_cnt) {
504  return 0;
505  }
506 
507  for (uint32_t i = 0; i < pd1->pattern_cnt; i++) {
508  if (SCHSPatternCompare(pd1->parray[i], pd2->parray[i]) == 0) {
509  return 0;
510  }
511  }
512 
513  return 1;
514 }
515 
516 static void PatternDatabaseFree(PatternDatabase *pd)
517 {
518  BUG_ON(pd->ref_cnt != 0);
519 
520  if (pd->parray != NULL) {
521  for (uint32_t i = 0; i < pd->pattern_cnt; i++) {
522  SCHSPattern *p = pd->parray[i];
523  if (p != NULL) {
524  SCFree(p->original_pat);
525  SCFree(p->sids);
526  SCFree(p);
527  }
528  }
529  SCFree(pd->parray);
530  }
531 
532  hs_free_database(pd->hs_db);
533 
534  SCFree(pd);
535 }
536 
537 static void PatternDatabaseTableFree(void *data)
538 {
539  /* Stub function handed to hash table; actual freeing of PatternDatabase
540  * structures is done in MPM destruction when the ref_cnt drops to zero. */
541 }
542 
543 static PatternDatabase *PatternDatabaseAlloc(uint32_t pattern_cnt)
544 {
545  PatternDatabase *pd = SCCalloc(1, sizeof(PatternDatabase));
546  if (pd == NULL) {
547  return NULL;
548  }
549  pd->pattern_cnt = pattern_cnt;
550  pd->ref_cnt = 0;
551  pd->hs_db = NULL;
552  pd->cached = false;
553 
554  /* alloc the pattern array */
555  pd->parray = (SCHSPattern **)SCCalloc(pd->pattern_cnt, sizeof(SCHSPattern *));
556  if (pd->parray == NULL) {
557  SCFree(pd);
558  return NULL;
559  }
560 
561  return pd;
562 }
563 
564 static int HSCheckPatterns(MpmCtx *mpm_ctx, SCHSCtx *ctx)
565 {
566  if (mpm_ctx->pattern_cnt == 0 || ctx->init_hash == NULL) {
567  SCLogDebug("no patterns supplied to this mpm_ctx");
568  return 0;
569  }
570  return 1;
571 }
572 
573 static void HSPatternArrayPopulate(SCHSCtx *ctx, PatternDatabase *pd)
574 {
575  for (uint32_t i = 0, p = 0; i < INIT_HASH_SIZE; i++) {
576  SCHSPattern *node = ctx->init_hash[i];
577  SCHSPattern *nnode = NULL;
578  while (node != NULL) {
579  nnode = node->next;
580  node->next = NULL;
581  pd->parray[p++] = node;
582  node = nnode;
583  }
584  }
585 }
586 
587 static void HSPatternArrayInit(SCHSCtx *ctx, PatternDatabase *pd)
588 {
589  HSPatternArrayPopulate(ctx, pd);
590  /* we no longer need the hash, so free its memory */
591  SCFree(ctx->init_hash);
592  ctx->init_hash = NULL;
593 }
594 
595 static int HSGlobalPatternDatabaseInit(void)
596 {
597  if (g_db_table == NULL) {
598  g_db_table = HashTableInit(INIT_DB_HASH_SIZE, PatternDatabaseHash,
599  PatternDatabaseCompare,
600  PatternDatabaseTableFree);
601  if (g_db_table == NULL) {
602  return -1;
603  }
604  }
605  return 0;
606 }
607 
608 static int HSScratchAlloc(const hs_database_t *db)
609 {
610  SCMutexLock(&g_scratch_proto_mutex);
611  hs_error_t err = hs_alloc_scratch(db, &g_scratch_proto);
612  SCMutexUnlock(&g_scratch_proto_mutex);
613  if (err != HS_SUCCESS) {
614  SCLogError("failed to allocate scratch");
615  return -1;
616  }
617  return 0;
618 }
619 
620 static int PatternDatabaseGetSize(PatternDatabase *pd, size_t *db_size)
621 {
622  hs_error_t err = hs_database_size(pd->hs_db, db_size);
623  if (err != HS_SUCCESS) {
624  SCLogError("failed to query database size: %s", HSErrorToStr(err));
625  return -1;
626  }
627  return 0;
628 }
629 
630 static void SCHSCleanupOnError(PatternDatabase *pd, SCHSCompileData *cd)
631 {
632  if (pd) {
633  PatternDatabaseFree(pd);
634  }
635  if (cd) {
636  CompileDataFree(cd);
637  }
638 }
639 
640 static int CompileDataExtensionsInit(hs_expr_ext_t **ext, const SCHSPattern *p)
641 {
642  if (p->flags & (MPM_PATTERN_FLAG_OFFSET | MPM_PATTERN_FLAG_DEPTH)) {
643  *ext = SCCalloc(1, sizeof(hs_expr_ext_t));
644  if ((*ext) == NULL) {
645  return -1;
646  }
647  if (p->flags & MPM_PATTERN_FLAG_OFFSET) {
648  (*ext)->flags |= HS_EXT_FLAG_MIN_OFFSET;
649  (*ext)->min_offset = p->offset + p->len;
650  }
651  if (p->flags & MPM_PATTERN_FLAG_DEPTH) {
652  (*ext)->flags |= HS_EXT_FLAG_MAX_OFFSET;
653  (*ext)->max_offset = p->offset + p->depth;
654  }
655  }
656 
657  return 0;
658 }
659 
660 /**
661  * \brief Initialize the pattern database - try to get existing pd
662  * from the global hash table, or load it from disk if caching is enabled.
663  *
664  * \param PatternDatabase* [in/out] Pointer to the pattern database to use.
665  * \param SCHSCompileData* [in] Pointer to the compile data.
666  * \retval 0 On success, negative value on failure.
667  */
668 static int PatternDatabaseGetCached(
669  PatternDatabase **pd, SCHSCompileData *cd, const char *cache_dir_path)
670 {
671  /* Check global hash table to see if we've seen this pattern database
672  * before, and reuse the Hyperscan database if so. */
673  PatternDatabase *pd_cached = HashTableLookup(g_db_table, *pd, 1);
674  if (pd_cached != NULL) {
675  SCLogDebug("Reusing cached database %p with %" PRIu32
676  " patterns (ref_cnt=%" PRIu32 ")",
677  pd_cached->hs_db, pd_cached->pattern_cnt,
678  pd_cached->ref_cnt);
679  pd_cached->ref_cnt++;
680  PatternDatabaseFree(*pd);
681  CompileDataFree(cd);
682  *pd = pd_cached;
683  return 0;
684  } else if (cache_dir_path) {
685  pd_cached = *pd;
686  char hs_db_hash[SC_SHA256_LEN * 2 + 1]; // * 2 for hex +1 for nul terminator
687  if (HSHashDb(pd_cached, hs_db_hash, ARRAY_SIZE(hs_db_hash)) != 0) {
688  return -1;
689  }
690  if (HSLoadCache(&pd_cached->hs_db, hs_db_hash, cache_dir_path) == 0) {
691  pd_cached->ref_cnt = 1;
692  pd_cached->cached = true;
693  if (HSScratchAlloc(pd_cached->hs_db) != 0) {
694  goto recover;
695  }
696  if (HashTableAdd(g_db_table, pd_cached, 1) < 0) {
697  goto recover;
698  }
699  CompileDataFree(cd);
700  return 0;
701 
702  recover:
703  pd_cached->ref_cnt = 0;
704  pd_cached->cached = false;
705  return -1;
706  }
707  }
708 
709  return -1; // not cached
710 }
711 
712 static int PatternDatabaseCompile(PatternDatabase *pd, SCHSCompileData *cd)
713 {
714  for (uint32_t i = 0; i < pd->pattern_cnt; i++) {
715  const SCHSPattern *p = pd->parray[i];
716  cd->ids[i] = i;
717  cd->flags[i] = HS_FLAG_SINGLEMATCH;
718  if (p->flags & MPM_PATTERN_FLAG_NOCASE) {
719  cd->flags[i] |= HS_FLAG_CASELESS;
720  }
721  cd->expressions[i] = HSRenderPattern(p->original_pat, p->len);
722  if (CompileDataExtensionsInit(&cd->ext[i], p) != 0) {
723  return -1;
724  }
725  }
726 
727  hs_compile_error_t *compile_err = NULL;
728  hs_error_t err = hs_compile_ext_multi((const char *const *)cd->expressions, cd->flags, cd->ids,
729  (const hs_expr_ext_t *const *)cd->ext, cd->pattern_cnt, HS_MODE_BLOCK, NULL, &pd->hs_db,
730  &compile_err);
731  if (err != HS_SUCCESS) {
732  HSLogCompileError("multiple patterns", compile_err, err);
733  return -1;
734  }
735 
736  if (HSScratchAlloc(pd->hs_db) != 0) {
737  return -1;
738  }
739 
740  if (HashTableAdd(g_db_table, pd, 1) < 0) {
741  return -1;
742  }
743  pd->ref_cnt = 1;
744  return 0;
745 }
746 
747 /**
748  * \brief Process the patterns added to the mpm, and create the internal tables.
749  *
750  * \param mpm_conf Pointer to the generic MPM matcher configuration
751  * \param mpm_ctx Pointer to the mpm context.
752  */
753 int SCHSPreparePatterns(MpmConfig *mpm_conf, MpmCtx *mpm_ctx)
754 {
755  SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
756 
757  if (HSCheckPatterns(mpm_ctx, ctx) == 0) {
758  return 0;
759  }
760 
761  SCHSCompileData *cd = CompileDataAlloc(mpm_ctx->pattern_cnt);
762  PatternDatabase *pd = PatternDatabaseAlloc(mpm_ctx->pattern_cnt);
763  if (cd == NULL || pd == NULL) {
764  goto error;
765  }
766 
767  HSPatternArrayInit(ctx, pd);
768  pd->no_cache = !(mpm_ctx->flags & MPMCTX_FLAGS_CACHE_TO_DISK);
769  /* Serialise whole database compilation as a relatively easy way to ensure
770  * dedupe is safe. */
771  SCMutexLock(&g_db_table_mutex);
772  if (HSGlobalPatternDatabaseInit() == -1) {
773  SCMutexUnlock(&g_db_table_mutex);
774  goto error;
775  }
776 
777  const char *cache_path = pd->no_cache || !mpm_conf ? NULL : mpm_conf->cache_dir_path;
778  if (PatternDatabaseGetCached(&pd, cd, cache_path) == 0 && pd != NULL) {
779  cd = NULL;
780  ctx->pattern_db = pd;
781  if (PatternDatabaseGetSize(pd, &ctx->hs_db_size) != 0) {
782  SCMutexUnlock(&g_db_table_mutex);
783  goto error;
784  }
785 
786  if (pd->ref_cnt == 1) {
787  // freshly allocated
788  mpm_ctx->memory_cnt++;
789  mpm_ctx->memory_size += ctx->hs_db_size;
790  }
791  SCMutexUnlock(&g_db_table_mutex);
792  return 0;
793  }
794 
795  BUG_ON(ctx->pattern_db != NULL); /* already built? */
796  BUG_ON(mpm_ctx->pattern_cnt == 0);
797 
798  if (PatternDatabaseCompile(pd, cd) != 0) {
799  SCMutexUnlock(&g_db_table_mutex);
800  goto error;
801  }
802 
803  ctx->pattern_db = pd;
804  if (PatternDatabaseGetSize(pd, &ctx->hs_db_size) != 0) {
805  SCMutexUnlock(&g_db_table_mutex);
806  goto error;
807  }
808 
809  mpm_ctx->memory_cnt++;
810  mpm_ctx->memory_size += ctx->hs_db_size;
811 
812  SCMutexUnlock(&g_db_table_mutex);
813  CompileDataFree(cd);
814  return 0;
815 
816 error:
817  SCHSCleanupOnError(pd, cd);
818  return -1;
819 }
820 
821 /**
822  * \brief Cache the initialized and compiled ruleset
823  */
824 static int SCHSCacheRuleset(MpmConfig *mpm_conf)
825 {
826  if (!mpm_conf || !mpm_conf->cache_dir_path) {
827  return -1;
828  }
829 
830  SCLogDebug("Caching the loaded ruleset to %s", mpm_conf->cache_dir_path);
831  if (SCCreateDirectoryTree(mpm_conf->cache_dir_path, true) != 0) {
832  SCLogWarning("Failed to create Hyperscan cache folder, make sure "
833  "the parent folder is writeable "
834  "or adjust sgh-mpm-caching-path setting (%s)",
835  mpm_conf->cache_dir_path);
836  return -1;
837  }
838  PatternDatabaseCache *pd_stats = mpm_conf->cache_stats;
839  struct HsIteratorData iter_data = { .pd_stats = pd_stats,
840  .cache_path = mpm_conf->cache_dir_path };
841  SCMutexLock(&g_db_table_mutex);
842  HashTableIterate(g_db_table, HSSaveCacheIterator, &iter_data);
843  SCMutexUnlock(&g_db_table_mutex);
844  return 0;
845 }
846 
847 static uint32_t FilenameTableHash(HashTable *ht, void *data, uint16_t len)
848 {
849  const char *fname = data;
850  uint32_t hash = hashlittle_safe(data, strlen(fname), 0);
851  hash %= ht->array_size;
852  return hash;
853 }
854 
855 static void FilenameTableFree(void *data)
856 {
857  SCFree(data);
858 }
859 
860 static int SCHSCachePrune(MpmConfig *mpm_conf)
861 {
862  if (!mpm_conf || !mpm_conf->cache_dir_path) {
863  return -1;
864  }
865 
866  SCLogDebug("Pruning the Hyperscan cache folder %s", mpm_conf->cache_dir_path);
867  // we need to initialize hash map of in-use cache files
868  HashTable *inuse_caches =
869  HashTableInit(INIT_DB_HASH_SIZE, FilenameTableHash, NULL, FilenameTableFree);
870  if (inuse_caches == NULL) {
871  return -1;
872  }
873  struct HsInUseCacheFilesIteratorData iter_data = { .tbl = inuse_caches,
874  .cache_path = mpm_conf->cache_dir_path };
875 
876  SCMutexLock(&g_db_table_mutex);
877  HashTableIterate(g_db_table, HSCacheFilenameUsedIterator, &iter_data);
878  SCMutexUnlock(&g_db_table_mutex);
879 
880  int r = SCHSCachePruneEvaluate(mpm_conf, inuse_caches);
881  HashTableFree(inuse_caches);
882  return r;
883 }
884 
885 /**
886  * \brief Init the mpm thread context.
887  *
888  * \param mpm_ctx Pointer to the mpm context.
889  * \param mpm_thread_ctx Pointer to the mpm thread context.
890  */
891 void SCHSInitThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx)
892 {
893  memset(mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
894 
895  SCHSThreadCtx *ctx = SCCalloc(1, sizeof(SCHSThreadCtx));
896  if (ctx == NULL) {
897  exit(EXIT_FAILURE);
898  }
899  mpm_thread_ctx->ctx = ctx;
900 
901  mpm_thread_ctx->memory_cnt++;
902  mpm_thread_ctx->memory_size += sizeof(SCHSThreadCtx);
903 
904  ctx->scratch = NULL;
905  ctx->scratch_size = 0;
906 
907  SCMutexLock(&g_scratch_proto_mutex);
908 
909  if (g_scratch_proto == NULL) {
910  /* There is no scratch prototype: this means that we have not compiled
911  * any Hyperscan databases. */
912  SCMutexUnlock(&g_scratch_proto_mutex);
913  SCLogDebug("No scratch space prototype");
914  return;
915  }
916 
917  hs_error_t err = hs_clone_scratch(g_scratch_proto,
918  (hs_scratch_t **)&ctx->scratch);
919 
920  SCMutexUnlock(&g_scratch_proto_mutex);
921 
922  if (err != HS_SUCCESS) {
923  FatalError("Unable to clone scratch prototype");
924  }
925 
926  err = hs_scratch_size(ctx->scratch, &ctx->scratch_size);
927  if (err != HS_SUCCESS) {
928  FatalError("Unable to query scratch size");
929  }
930 
931  mpm_thread_ctx->memory_cnt++;
932  mpm_thread_ctx->memory_size += ctx->scratch_size;
933 }
934 
935 /**
936  * \brief Initialize the HS context.
937  *
938  * \param mpm_ctx Mpm context.
939  */
940 void SCHSInitCtx(MpmCtx *mpm_ctx)
941 {
942  if (mpm_ctx->ctx != NULL)
943  return;
944 
945  mpm_ctx->ctx = SCCalloc(1, sizeof(SCHSCtx));
946  if (mpm_ctx->ctx == NULL) {
947  exit(EXIT_FAILURE);
948  }
949 
950  mpm_ctx->memory_cnt++;
951  mpm_ctx->memory_size += sizeof(SCHSCtx);
952 
953  /* initialize the hash we use to speed up pattern insertions */
954  SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
955  ctx->init_hash = SCCalloc(INIT_HASH_SIZE, sizeof(SCHSPattern *));
956  if (ctx->init_hash == NULL) {
957  exit(EXIT_FAILURE);
958  }
959 }
960 
961 /**
962  * \brief Destroy the mpm thread context.
963  *
964  * \param mpm_ctx Pointer to the mpm context.
965  * \param mpm_thread_ctx Pointer to the mpm thread context.
966  */
967 void SCHSDestroyThreadCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx)
968 {
969  SCHSPrintSearchStats(mpm_thread_ctx);
970 
971  if (mpm_thread_ctx->ctx != NULL) {
972  SCHSThreadCtx *thr_ctx = (SCHSThreadCtx *)mpm_thread_ctx->ctx;
973 
974  if (thr_ctx->scratch != NULL) {
975  hs_free_scratch(thr_ctx->scratch);
976  mpm_thread_ctx->memory_cnt--;
977  mpm_thread_ctx->memory_size -= thr_ctx->scratch_size;
978  }
979 
980  SCFree(mpm_thread_ctx->ctx);
981  mpm_thread_ctx->ctx = NULL;
982  mpm_thread_ctx->memory_cnt--;
983  mpm_thread_ctx->memory_size -= sizeof(SCHSThreadCtx);
984  }
985 }
986 
987 /**
988  * \brief Destroy the mpm context.
989  *
990  * \param mpm_ctx Pointer to the mpm context.
991  */
992 void SCHSDestroyCtx(MpmCtx *mpm_ctx)
993 {
994  SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
995  if (ctx == NULL)
996  return;
997 
998  if (ctx->init_hash != NULL) {
999  SCFree(ctx->init_hash);
1000  ctx->init_hash = NULL;
1001  mpm_ctx->memory_cnt--;
1002  mpm_ctx->memory_size -= (INIT_HASH_SIZE * sizeof(SCHSPattern *));
1003  }
1004 
1005  /* Decrement pattern database ref count, and delete it entirely if the
1006  * count has dropped to zero. */
1007  SCMutexLock(&g_db_table_mutex);
1008  PatternDatabase *pd = ctx->pattern_db;
1009  if (pd) {
1010  BUG_ON(pd->ref_cnt == 0);
1011  pd->ref_cnt--;
1012  if (pd->ref_cnt == 0) {
1013  HashTableRemove(g_db_table, pd, 1);
1014  PatternDatabaseFree(pd);
1015  }
1016  }
1017  SCMutexUnlock(&g_db_table_mutex);
1018 
1019  SCFree(mpm_ctx->ctx);
1020  mpm_ctx->ctx = NULL;
1021  mpm_ctx->memory_cnt--;
1022  mpm_ctx->memory_size -= sizeof(SCHSCtx);
1023 }
1024 
1025 typedef struct SCHSCallbackCtx_ {
1026  SCHSCtx *ctx;
1027  void *pmq;
1028  uint32_t match_count;
1029 } SCHSCallbackCtx;
1030 
1031 /* Hyperscan MPM match event handler */
1032 static int SCHSMatchEvent(unsigned int id, unsigned long long from,
1033  unsigned long long to, unsigned int flags,
1034  void *ctx)
1035 {
1036  SCHSCallbackCtx *cctx = ctx;
1037  PrefilterRuleStore *pmq = cctx->pmq;
1038  const PatternDatabase *pd = cctx->ctx->pattern_db;
1039  const SCHSPattern *pat = pd->parray[id];
1040 
1041  SCLogDebug("Hyperscan Match %" PRIu32 ": id=%" PRIu32 " @ %" PRIuMAX
1042  " (pat id=%" PRIu32 ")",
1043  cctx->match_count, (uint32_t)id, (uintmax_t)to, pat->id);
1044 
1045  PrefilterAddSids(pmq, pat->sids, pat->sids_size);
1046 
1047  cctx->match_count++;
1048  return 0;
1049 }
1050 
1051 /**
1052  * \brief The Hyperscan search function.
1053  *
1054  * \param mpm_ctx Pointer to the mpm context.
1055  * \param mpm_thread_ctx Pointer to the mpm thread context.
1056  * \param pmq Pointer to the Pattern Matcher Queue to hold
1057  * search matches.
1058  * \param buf Buffer to be searched.
1059  * \param buflen Buffer length.
1060  *
1061  * \retval matches Match count.
1062  */
1063 uint32_t SCHSSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
1064  PrefilterRuleStore *pmq, const uint8_t *buf, const uint32_t buflen)
1065 {
1066  uint32_t ret = 0;
1067  SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
1068  SCHSThreadCtx *hs_thread_ctx = (SCHSThreadCtx *)(mpm_thread_ctx->ctx);
1069  const PatternDatabase *pd = ctx->pattern_db;
1070 
1071  if (unlikely(buflen == 0)) {
1072  return 0;
1073  }
1074 
1075  SCHSCallbackCtx cctx = {.ctx = ctx, .pmq = pmq, .match_count = 0};
1076 
1077  /* scratch should have been cloned from g_scratch_proto at thread init. */
1078  hs_scratch_t *scratch = hs_thread_ctx->scratch;
1079  DEBUG_VALIDATE_BUG_ON(pd->hs_db == NULL);
1080  DEBUG_VALIDATE_BUG_ON(scratch == NULL);
1081 
1082  hs_error_t err = hs_scan(pd->hs_db, (const char *)buf, buflen, 0, scratch,
1083  SCHSMatchEvent, &cctx);
1084  if (err != HS_SUCCESS) {
1085  /* An error value (other than HS_SCAN_TERMINATED) from hs_scan()
1086  * indicates that it was passed an invalid database or scratch region,
1087  * which is not something we can recover from at scan time. */
1088  SCLogError("Hyperscan returned error %d", err);
1089  exit(EXIT_FAILURE);
1090  } else {
1091  ret = cctx.match_count;
1092  }
1093 
1094  return ret;
1095 }
1096 
1097 /**
1098  * \brief Add a case insensitive pattern. Although we have different calls for
1099  * adding case sensitive and insensitive patterns, we make a single call
1100  * for either case. No special treatment for either case.
1101  *
1102  * \param mpm_ctx Pointer to the mpm context.
1103  * \param pat The pattern to add.
1104  * \param patlen The pattern length.
1105  * \param offset The pattern offset.
1106  * \param depth The pattern depth.
1107  * \param pid The pattern id.
1108  * \param sid The pattern signature id.
1109  * \param flags Flags associated with this pattern.
1110  *
1111  * \retval 0 On success.
1112  * \retval -1 On failure.
1113  */
1114 int SCHSAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset,
1115  uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
1116 {
1118  return SCHSAddPattern(mpm_ctx, pat, patlen, offset, depth, pid, sid, flags);
1119 }
1120 
1121 /**
1122  * \brief Add a case sensitive pattern. Although we have different calls for
1123  * adding case sensitive and insensitive patterns, we make a single call
1124  * for either case. No special treatment for either case.
1125  *
1126  * \param mpm_ctx Pointer to the mpm context.
1127  * \param pat The pattern to add.
1128  * \param patlen The pattern length.
1129  * \param offset The pattern offset.
1130  * \param depth The pattern depth.
1131  * \param pid The pattern id.
1132  * \param sid The pattern signature id.
1133  * \param flags Flags associated with this pattern.
1134  *
1135  * \retval 0 On success.
1136  * \retval -1 On failure.
1137  */
1138 int SCHSAddPatternCS(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
1139  uint16_t offset, uint16_t depth, uint32_t pid,
1140  SigIntId sid, uint8_t flags)
1141 {
1142  return SCHSAddPattern(mpm_ctx, pat, patlen, offset, depth, pid, sid, flags);
1143 }
1144 
1145 void SCHSPrintSearchStats(MpmThreadCtx *mpm_thread_ctx)
1146 {
1147 }
1148 
1149 void SCHSPrintInfo(MpmCtx *mpm_ctx)
1150 {
1151  SCHSCtx *ctx = (SCHSCtx *)mpm_ctx->ctx;
1152 
1153  printf("MPM HS Information:\n");
1154  printf("Memory allocs: %" PRIu32 "\n", mpm_ctx->memory_cnt);
1155  printf("Memory alloced: %" PRIu32 "\n", mpm_ctx->memory_size);
1156  printf(" Sizeof:\n");
1157  printf(" MpmCtx %" PRIuMAX "\n", (uintmax_t)sizeof(MpmCtx));
1158  printf(" SCHSCtx: %" PRIuMAX "\n", (uintmax_t)sizeof(SCHSCtx));
1159  printf(" SCHSPattern %" PRIuMAX "\n", (uintmax_t)sizeof(SCHSPattern));
1160  printf("Unique Patterns: %" PRIu32 "\n", mpm_ctx->pattern_cnt);
1161  printf("Smallest: %" PRIu32 "\n", mpm_ctx->minlen);
1162  printf("Largest: %" PRIu32 "\n", mpm_ctx->maxlen);
1163  printf("\n");
1164 
1165  if (ctx) {
1166  PatternDatabase *pd = ctx->pattern_db;
1167  char *db_info = NULL;
1168  if (hs_database_info(pd->hs_db, &db_info) == HS_SUCCESS) {
1169  printf("HS Database Info: %s\n", db_info);
1170  SCFree(db_info);
1171  }
1172  printf("HS Database Size: %" PRIuMAX " bytes\n",
1173  (uintmax_t)ctx->hs_db_size);
1174  }
1175 
1176  printf("\n");
1177 }
1178 
1179 static MpmConfig *SCHSConfigInit(void)
1180 {
1181  MpmConfig *c = SCCalloc(1, sizeof(MpmConfig));
1182  return c;
1183 }
1184 
1185 static void SCHSConfigDeinit(MpmConfig **c)
1186 {
1187  if (c != NULL) {
1188  SCFree(*c);
1189  (*c) = NULL;
1190  }
1191 }
1192 
1193 static void SCHSConfigCacheDirSet(MpmConfig *c, const char *dir_path)
1194 {
1195  c->cache_dir_path = dir_path;
1196 }
1197 
1198 /************************** Mpm Registration ***************************/
1199 
1200 /**
1201  * \brief Register the Hyperscan MPM.
1202  */
1203 void MpmHSRegister(void)
1204 {
1205  mpm_table[MPM_HS].name = "hs";
1206  mpm_table[MPM_HS].InitCtx = SCHSInitCtx;
1207  mpm_table[MPM_HS].InitThreadCtx = SCHSInitThreadCtx;
1208  mpm_table[MPM_HS].DestroyCtx = SCHSDestroyCtx;
1209  mpm_table[MPM_HS].DestroyThreadCtx = SCHSDestroyThreadCtx;
1210  mpm_table[MPM_HS].ConfigInit = SCHSConfigInit;
1211  mpm_table[MPM_HS].ConfigDeinit = SCHSConfigDeinit;
1212  mpm_table[MPM_HS].ConfigCacheDirSet = SCHSConfigCacheDirSet;
1213  mpm_table[MPM_HS].AddPattern = SCHSAddPatternCS;
1214  mpm_table[MPM_HS].AddPatternNocase = SCHSAddPatternCI;
1215  mpm_table[MPM_HS].Prepare = SCHSPreparePatterns;
1216  mpm_table[MPM_HS].CacheStatsInit = SCHSCacheStatsInit;
1217  mpm_table[MPM_HS].CacheStatsPrint = SCHSCacheStatsPrint;
1218  mpm_table[MPM_HS].CacheStatsDeinit = SCHSCacheStatsDeinit;
1219  mpm_table[MPM_HS].CacheRuleset = SCHSCacheRuleset;
1220  mpm_table[MPM_HS].CachePrune = SCHSCachePrune;
1221  mpm_table[MPM_HS].Search = SCHSSearch;
1222  mpm_table[MPM_HS].PrintCtx = SCHSPrintInfo;
1223  mpm_table[MPM_HS].PrintThreadCtx = SCHSPrintSearchStats;
1224 #ifdef UNITTESTS
1225  mpm_table[MPM_HS].RegisterUnittests = SCHSRegisterTests;
1226 #endif
1228  /* Set Hyperscan memory allocators */
1229  SCHSSetAllocators();
1230 }
1231 
1232 /**
1233  * \brief Clean up global memory used by all Hyperscan MPM instances.
1234  *
1235  * Currently, this is just the global scratch prototype.
1236  */
1237 void MpmHSGlobalCleanup(void)
1238 {
1239  SCMutexLock(&g_scratch_proto_mutex);
1240  if (g_scratch_proto) {
1241  SCLogDebug("Cleaning up Hyperscan global scratch");
1242  hs_free_scratch(g_scratch_proto);
1243  g_scratch_proto = NULL;
1244  }
1245  SCMutexUnlock(&g_scratch_proto_mutex);
1246 
1247  SCMutexLock(&g_db_table_mutex);
1248  if (g_db_table != NULL) {
1249  SCLogDebug("Clearing Hyperscan database cache");
1250  HashTableFree(g_db_table);
1251  g_db_table = NULL;
1252  }
1253  SCMutexUnlock(&g_db_table_mutex);
1254 }
1255 
1256 /*************************************Unittests********************************/
1257 
1258 #ifdef UNITTESTS
1259 #include "detect-engine-alert.h"
1260 
1261 static int SCHSTest01(void)
1262 {
1263  int result = 0;
1264  MpmCtx mpm_ctx;
1265  MpmThreadCtx mpm_thread_ctx;
1266  PrefilterRuleStore pmq;
1267 
1268  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1269  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1270  MpmInitCtx(&mpm_ctx, MPM_HS);
1271 
1272  /* 1 match */
1273  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1274  PmqSetup(&pmq);
1275 
1276  SCHSPreparePatterns(NULL, &mpm_ctx);
1277  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1278 
1279  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1280 
1281  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1282  strlen(buf));
1283 
1284  if (cnt == 1)
1285  result = 1;
1286  else
1287  printf("1 != %" PRIu32 " ", cnt);
1288 
1289  SCHSDestroyCtx(&mpm_ctx);
1290  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1291  PmqFree(&pmq);
1292  return result;
1293 }
1294 
1295 static int SCHSTest02(void)
1296 {
1297  int result = 0;
1298  MpmCtx mpm_ctx;
1299  MpmThreadCtx mpm_thread_ctx;
1300  PrefilterRuleStore pmq;
1301 
1302  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1303  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1304  MpmInitCtx(&mpm_ctx, MPM_HS);
1305 
1306  /* 1 match */
1307  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0, 0);
1308  PmqSetup(&pmq);
1309 
1310  SCHSPreparePatterns(NULL, &mpm_ctx);
1311  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1312 
1313  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1314  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1315  strlen(buf));
1316 
1317  if (cnt == 0)
1318  result = 1;
1319  else
1320  printf("0 != %" PRIu32 " ", cnt);
1321 
1322  SCHSDestroyCtx(&mpm_ctx);
1323  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1324  PmqFree(&pmq);
1325  return result;
1326 }
1327 
1328 static int SCHSTest03(void)
1329 {
1330  int result = 0;
1331  MpmCtx mpm_ctx;
1332  MpmThreadCtx mpm_thread_ctx;
1333  PrefilterRuleStore pmq;
1334 
1335  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1336  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1337  MpmInitCtx(&mpm_ctx, MPM_HS);
1338 
1339  /* 1 match */
1340  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1341  /* 1 match */
1342  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"bcde", 4, 0, 0, 1, 0, 0);
1343  /* 1 match */
1344  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"fghj", 4, 0, 0, 2, 0, 0);
1345  PmqSetup(&pmq);
1346 
1347  SCHSPreparePatterns(NULL, &mpm_ctx);
1348  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1349 
1350  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1351  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1352  strlen(buf));
1353 
1354  if (cnt == 3)
1355  result = 1;
1356  else
1357  printf("3 != %" PRIu32 " ", cnt);
1358 
1359  SCHSDestroyCtx(&mpm_ctx);
1360  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1361  PmqFree(&pmq);
1362  return result;
1363 }
1364 
1365 static int SCHSTest04(void)
1366 {
1367  int result = 0;
1368  MpmCtx mpm_ctx;
1369  MpmThreadCtx mpm_thread_ctx;
1370  PrefilterRuleStore pmq;
1371 
1372  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1373  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1374  MpmInitCtx(&mpm_ctx, MPM_HS);
1375 
1376  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1377  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"bcdegh", 6, 0, 0, 1, 0, 0);
1378  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"fghjxyz", 7, 0, 0, 2, 0, 0);
1379  PmqSetup(&pmq);
1380 
1381  SCHSPreparePatterns(NULL, &mpm_ctx);
1382  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1383 
1384  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1385  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1386  strlen(buf));
1387 
1388  if (cnt == 1)
1389  result = 1;
1390  else
1391  printf("1 != %" PRIu32 " ", cnt);
1392 
1393  SCHSDestroyCtx(&mpm_ctx);
1394  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1395  PmqFree(&pmq);
1396  return result;
1397 }
1398 
1399 static int SCHSTest05(void)
1400 {
1401  int result = 0;
1402  MpmCtx mpm_ctx;
1403  MpmThreadCtx mpm_thread_ctx;
1404  PrefilterRuleStore pmq;
1405 
1406  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1407  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1408  MpmInitCtx(&mpm_ctx, MPM_HS);
1409 
1410  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0);
1411  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0);
1412  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"fghJikl", 7, 0, 0, 2, 0, 0);
1413  PmqSetup(&pmq);
1414 
1415  SCHSPreparePatterns(NULL, &mpm_ctx);
1416  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1417 
1418  const char *buf = "abcdefghjiklmnopqrstuvwxyz";
1419  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1420  strlen(buf));
1421 
1422  if (cnt == 3)
1423  result = 1;
1424  else
1425  printf("3 != %" PRIu32 " ", cnt);
1426 
1427  SCHSDestroyCtx(&mpm_ctx);
1428  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1429  PmqFree(&pmq);
1430  return result;
1431 }
1432 
1433 static int SCHSTest06(void)
1434 {
1435  int result = 0;
1436  MpmCtx mpm_ctx;
1437  MpmThreadCtx mpm_thread_ctx;
1438  PrefilterRuleStore pmq;
1439 
1440  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1441  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1442  MpmInitCtx(&mpm_ctx, MPM_HS);
1443 
1444  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1445  PmqSetup(&pmq);
1446 
1447  SCHSPreparePatterns(NULL, &mpm_ctx);
1448  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1449 
1450  const char *buf = "abcd";
1451  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1452  strlen(buf));
1453 
1454  if (cnt == 1)
1455  result = 1;
1456  else
1457  printf("1 != %" PRIu32 " ", cnt);
1458 
1459  SCHSDestroyCtx(&mpm_ctx);
1460  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1461  PmqFree(&pmq);
1462  return result;
1463 }
1464 
1465 static int SCHSTest07(void)
1466 {
1467  int result = 0;
1468  MpmCtx mpm_ctx;
1469  MpmThreadCtx mpm_thread_ctx;
1470  PrefilterRuleStore pmq;
1471 
1472  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1473  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1474  MpmInitCtx(&mpm_ctx, MPM_HS);
1475 
1476  /* should match 30 times */
1477  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0, 0);
1478  /* should match 29 times */
1479  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 1, 0, 0);
1480  /* should match 28 times */
1481  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAA", 3, 0, 0, 2, 0, 0);
1482  /* 26 */
1483  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAA", 5, 0, 0, 3, 0, 0);
1484  /* 21 */
1485  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAA", 10, 0, 0, 4, 0, 0);
1486  /* 1 */
1487  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 30,
1488  0, 0, 5, 0, 0);
1489  PmqSetup(&pmq);
1490 
1491  SCHSPreparePatterns(NULL, &mpm_ctx);
1492  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1493 
1494  const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1495  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1496  strlen(buf));
1497 
1498  if (cnt == 6)
1499  result = 1;
1500  else
1501  printf("6 != %" PRIu32 " ", cnt);
1502 
1503  SCHSDestroyCtx(&mpm_ctx);
1504  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1505  PmqFree(&pmq);
1506  return result;
1507 }
1508 
1509 static int SCHSTest08(void)
1510 {
1511  int result = 0;
1512  MpmCtx mpm_ctx;
1513  MpmThreadCtx mpm_thread_ctx;
1514  PrefilterRuleStore pmq;
1515 
1516  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1517  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1518  MpmInitCtx(&mpm_ctx, MPM_HS);
1519 
1520  /* 1 match */
1521  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
1522  PmqSetup(&pmq);
1523 
1524  SCHSPreparePatterns(NULL, &mpm_ctx);
1525  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1526 
1527  uint32_t cnt =
1528  SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"a", 1);
1529 
1530  if (cnt == 0)
1531  result = 1;
1532  else
1533  printf("0 != %" PRIu32 " ", cnt);
1534 
1535  SCHSDestroyCtx(&mpm_ctx);
1536  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1537  PmqFree(&pmq);
1538  return result;
1539 }
1540 
1541 static int SCHSTest09(void)
1542 {
1543  int result = 0;
1544  MpmCtx mpm_ctx;
1545  MpmThreadCtx mpm_thread_ctx;
1546  PrefilterRuleStore pmq;
1547 
1548  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1549  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1550  MpmInitCtx(&mpm_ctx, MPM_HS);
1551 
1552  /* 1 match */
1553  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0, 0);
1554  PmqSetup(&pmq);
1555 
1556  SCHSPreparePatterns(NULL, &mpm_ctx);
1557  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1558 
1559  uint32_t cnt =
1560  SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"ab", 2);
1561 
1562  if (cnt == 1)
1563  result = 1;
1564  else
1565  printf("1 != %" PRIu32 " ", cnt);
1566 
1567  SCHSDestroyCtx(&mpm_ctx);
1568  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1569  PmqFree(&pmq);
1570  return result;
1571 }
1572 
1573 static int SCHSTest10(void)
1574 {
1575  int result = 0;
1576  MpmCtx mpm_ctx;
1577  MpmThreadCtx mpm_thread_ctx;
1578  PrefilterRuleStore pmq;
1579 
1580  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1581  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1582  MpmInitCtx(&mpm_ctx, MPM_HS);
1583 
1584  /* 1 match */
1585  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0, 0);
1586  PmqSetup(&pmq);
1587 
1588  SCHSPreparePatterns(NULL, &mpm_ctx);
1589  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1590 
1591  const char *buf = "01234567890123456789012345678901234567890123456789"
1592  "01234567890123456789012345678901234567890123456789"
1593  "abcdefgh"
1594  "01234567890123456789012345678901234567890123456789"
1595  "01234567890123456789012345678901234567890123456789";
1596  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1597  strlen(buf));
1598 
1599  if (cnt == 1)
1600  result = 1;
1601  else
1602  printf("1 != %" PRIu32 " ", cnt);
1603 
1604  SCHSDestroyCtx(&mpm_ctx);
1605  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1606  PmqFree(&pmq);
1607  return result;
1608 }
1609 
1610 static int SCHSTest11(void)
1611 {
1612  int result = 0;
1613  MpmCtx mpm_ctx;
1614  MpmThreadCtx mpm_thread_ctx;
1615  PrefilterRuleStore pmq;
1616 
1617  memset(&mpm_ctx, 0, sizeof(MpmCtx));
1618  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1619  MpmInitCtx(&mpm_ctx, MPM_HS);
1620 
1621  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"he", 2, 0, 0, 1, 0, 0) == -1)
1622  goto end;
1623  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"she", 3, 0, 0, 2, 0, 0) == -1)
1624  goto end;
1625  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"his", 3, 0, 0, 3, 0, 0) == -1)
1626  goto end;
1627  if (MpmAddPatternCS(&mpm_ctx, (uint8_t *)"hers", 4, 0, 0, 4, 0, 0) == -1)
1628  goto end;
1629  PmqSetup(&pmq);
1630 
1631  if (SCHSPreparePatterns(NULL, &mpm_ctx) == -1)
1632  goto end;
1633 
1634  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1635 
1636  result = 1;
1637 
1638  const char *buf = "he";
1639  result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1640  strlen(buf)) == 1);
1641  buf = "she";
1642  result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1643  strlen(buf)) == 2);
1644  buf = "his";
1645  result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1646  strlen(buf)) == 1);
1647  buf = "hers";
1648  result &= (SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1649  strlen(buf)) == 2);
1650 
1651 end:
1652  SCHSDestroyCtx(&mpm_ctx);
1653  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1654  PmqFree(&pmq);
1655  return result;
1656 }
1657 
1658 static int SCHSTest12(void)
1659 {
1660  int result = 0;
1661  MpmCtx mpm_ctx;
1662  MpmThreadCtx mpm_thread_ctx;
1663  PrefilterRuleStore pmq;
1664 
1665  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1666  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1667  MpmInitCtx(&mpm_ctx, MPM_HS);
1668 
1669  /* 1 match */
1670  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0, 0);
1671  /* 1 match */
1672  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"vwxyz", 5, 0, 0, 1, 0, 0);
1673  PmqSetup(&pmq);
1674 
1675  SCHSPreparePatterns(NULL, &mpm_ctx);
1676  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1677 
1678  const char *buf = "abcdefghijklmnopqrstuvwxyz";
1679  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1680  strlen(buf));
1681 
1682  if (cnt == 2)
1683  result = 1;
1684  else
1685  printf("2 != %" PRIu32 " ", cnt);
1686 
1687  SCHSDestroyCtx(&mpm_ctx);
1688  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1689  PmqFree(&pmq);
1690  return result;
1691 }
1692 
1693 static int SCHSTest13(void)
1694 {
1695  int result = 0;
1696  MpmCtx mpm_ctx;
1697  MpmThreadCtx mpm_thread_ctx;
1698  PrefilterRuleStore pmq;
1699 
1700  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1701  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1702  MpmInitCtx(&mpm_ctx, MPM_HS);
1703 
1704  /* 1 match */
1705  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCD";
1706  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1707  PmqSetup(&pmq);
1708 
1709  SCHSPreparePatterns(NULL, &mpm_ctx);
1710  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1711 
1712  const char *buf = "abcdefghijklmnopqrstuvwxyzABCD";
1713  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1714  strlen(buf));
1715 
1716  if (cnt == 1)
1717  result = 1;
1718  else
1719  printf("1 != %" PRIu32 " ", cnt);
1720 
1721  SCHSDestroyCtx(&mpm_ctx);
1722  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1723  PmqFree(&pmq);
1724  return result;
1725 }
1726 
1727 static int SCHSTest14(void)
1728 {
1729  int result = 0;
1730  MpmCtx mpm_ctx;
1731  MpmThreadCtx mpm_thread_ctx;
1732  PrefilterRuleStore pmq;
1733 
1734  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1735  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1736  MpmInitCtx(&mpm_ctx, MPM_HS);
1737 
1738  /* 1 match */
1739  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCDE";
1740  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1741  PmqSetup(&pmq);
1742 
1743  SCHSPreparePatterns(NULL, &mpm_ctx);
1744  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1745 
1746  const char *buf = "abcdefghijklmnopqrstuvwxyzABCDE";
1747  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1748  strlen(buf));
1749 
1750  if (cnt == 1)
1751  result = 1;
1752  else
1753  printf("1 != %" PRIu32 " ", cnt);
1754 
1755  SCHSDestroyCtx(&mpm_ctx);
1756  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1757  PmqFree(&pmq);
1758  return result;
1759 }
1760 
1761 static int SCHSTest15(void)
1762 {
1763  int result = 0;
1764  MpmCtx mpm_ctx;
1765  MpmThreadCtx mpm_thread_ctx;
1766  PrefilterRuleStore pmq;
1767 
1768  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1769  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1770  MpmInitCtx(&mpm_ctx, MPM_HS);
1771 
1772  /* 1 match */
1773  const char pat[] = "abcdefghijklmnopqrstuvwxyzABCDEF";
1774  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1775  PmqSetup(&pmq);
1776 
1777  SCHSPreparePatterns(NULL, &mpm_ctx);
1778  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1779 
1780  const char *buf = "abcdefghijklmnopqrstuvwxyzABCDEF";
1781  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1782  strlen(buf));
1783 
1784  if (cnt == 1)
1785  result = 1;
1786  else
1787  printf("1 != %" PRIu32 " ", cnt);
1788 
1789  SCHSDestroyCtx(&mpm_ctx);
1790  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1791  PmqFree(&pmq);
1792  return result;
1793 }
1794 
1795 static int SCHSTest16(void)
1796 {
1797  int result = 0;
1798  MpmCtx mpm_ctx;
1799  MpmThreadCtx mpm_thread_ctx;
1800  PrefilterRuleStore pmq;
1801 
1802  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1803  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1804  MpmInitCtx(&mpm_ctx, MPM_HS);
1805 
1806  /* 1 match */
1807  const char pat[] = "abcdefghijklmnopqrstuvwxyzABC";
1808  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1809  PmqSetup(&pmq);
1810 
1811  SCHSPreparePatterns(NULL, &mpm_ctx);
1812  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1813 
1814  const char *buf = "abcdefghijklmnopqrstuvwxyzABC";
1815  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1816  strlen(buf));
1817 
1818  if (cnt == 1)
1819  result = 1;
1820  else
1821  printf("1 != %" PRIu32 " ", cnt);
1822 
1823  SCHSDestroyCtx(&mpm_ctx);
1824  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1825  PmqFree(&pmq);
1826  return result;
1827 }
1828 
1829 static int SCHSTest17(void)
1830 {
1831  int result = 0;
1832  MpmCtx mpm_ctx;
1833  MpmThreadCtx mpm_thread_ctx;
1834  PrefilterRuleStore pmq;
1835 
1836  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1837  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1838  MpmInitCtx(&mpm_ctx, MPM_HS);
1839 
1840  /* 1 match */
1841  const char pat[] = "abcdefghijklmnopqrstuvwxyzAB";
1842  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1843  PmqSetup(&pmq);
1844 
1845  SCHSPreparePatterns(NULL, &mpm_ctx);
1846  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1847 
1848  const char *buf = "abcdefghijklmnopqrstuvwxyzAB";
1849  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1850  strlen(buf));
1851 
1852  if (cnt == 1)
1853  result = 1;
1854  else
1855  printf("1 != %" PRIu32 " ", cnt);
1856 
1857  SCHSDestroyCtx(&mpm_ctx);
1858  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1859  PmqFree(&pmq);
1860  return result;
1861 }
1862 
1863 static int SCHSTest18(void)
1864 {
1865  int result = 0;
1866  MpmCtx mpm_ctx;
1867  MpmThreadCtx mpm_thread_ctx;
1868  PrefilterRuleStore pmq;
1869 
1870  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1871  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1872  MpmInitCtx(&mpm_ctx, MPM_HS);
1873 
1874  /* 1 match */
1875  const char pat[] = "abcde"
1876  "fghij"
1877  "klmno"
1878  "pqrst"
1879  "uvwxy"
1880  "z";
1881  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1882  PmqSetup(&pmq);
1883 
1884  SCHSPreparePatterns(NULL, &mpm_ctx);
1885  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1886 
1887  const char *buf = "abcde"
1888  "fghij"
1889  "klmno"
1890  "pqrst"
1891  "uvwxy"
1892  "z";
1893  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1894  strlen(buf));
1895 
1896  if (cnt == 1)
1897  result = 1;
1898  else
1899  printf("1 != %" PRIu32 " ", cnt);
1900 
1901  SCHSDestroyCtx(&mpm_ctx);
1902  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1903  PmqFree(&pmq);
1904  return result;
1905 }
1906 
1907 static int SCHSTest19(void)
1908 {
1909  int result = 0;
1910  MpmCtx mpm_ctx;
1911  MpmThreadCtx mpm_thread_ctx;
1912  PrefilterRuleStore pmq;
1913 
1914  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1915  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1916  MpmInitCtx(&mpm_ctx, MPM_HS);
1917 
1918  /* 1 */
1919  const char pat[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1920  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1921  PmqSetup(&pmq);
1922 
1923  SCHSPreparePatterns(NULL, &mpm_ctx);
1924  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1925 
1926  const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1927  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1928  strlen(buf));
1929 
1930  if (cnt == 1)
1931  result = 1;
1932  else
1933  printf("1 != %" PRIu32 " ", cnt);
1934 
1935  SCHSDestroyCtx(&mpm_ctx);
1936  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1937  PmqFree(&pmq);
1938  return result;
1939 }
1940 
1941 static int SCHSTest20(void)
1942 {
1943  int result = 0;
1944  MpmCtx mpm_ctx;
1945  MpmThreadCtx mpm_thread_ctx;
1946  PrefilterRuleStore pmq;
1947 
1948  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1949  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1950  MpmInitCtx(&mpm_ctx, MPM_HS);
1951 
1952  /* 1 */
1953  const char pat[] = "AAAAA"
1954  "AAAAA"
1955  "AAAAA"
1956  "AAAAA"
1957  "AAAAA"
1958  "AAAAA"
1959  "AA";
1960  MpmAddPatternCS(&mpm_ctx, (uint8_t *)pat, sizeof(pat) - 1, 0, 0, 0, 0, 0);
1961  PmqSetup(&pmq);
1962 
1963  SCHSPreparePatterns(NULL, &mpm_ctx);
1964  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1965 
1966  const char *buf = "AAAAA"
1967  "AAAAA"
1968  "AAAAA"
1969  "AAAAA"
1970  "AAAAA"
1971  "AAAAA"
1972  "AA";
1973  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
1974  strlen(buf));
1975 
1976  if (cnt == 1)
1977  result = 1;
1978  else
1979  printf("1 != %" PRIu32 " ", cnt);
1980 
1981  SCHSDestroyCtx(&mpm_ctx);
1982  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
1983  PmqFree(&pmq);
1984  return result;
1985 }
1986 
1987 static int SCHSTest21(void)
1988 {
1989  int result = 0;
1990  MpmCtx mpm_ctx;
1991  MpmThreadCtx mpm_thread_ctx;
1992  PrefilterRuleStore pmq;
1993 
1994  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
1995  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
1996  MpmInitCtx(&mpm_ctx, MPM_HS);
1997 
1998  /* 1 */
1999  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2000  PmqSetup(&pmq);
2001 
2002  SCHSPreparePatterns(NULL, &mpm_ctx);
2003  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2004 
2005  uint32_t cnt =
2006  SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"AA", 2);
2007 
2008  if (cnt == 1)
2009  result = 1;
2010  else
2011  printf("1 != %" PRIu32 " ", cnt);
2012 
2013  SCHSDestroyCtx(&mpm_ctx);
2014  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2015  PmqFree(&pmq);
2016  return result;
2017 }
2018 
2019 static int SCHSTest22(void)
2020 {
2021  int result = 0;
2022  MpmCtx mpm_ctx;
2023  MpmThreadCtx mpm_thread_ctx;
2024  PrefilterRuleStore pmq;
2025 
2026  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2027  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2028  MpmInitCtx(&mpm_ctx, MPM_HS);
2029 
2030  /* 1 match */
2031  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0);
2032  /* 1 match */
2033  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"abcde", 5, 0, 0, 1, 0, 0);
2034  PmqSetup(&pmq);
2035 
2036  SCHSPreparePatterns(NULL, &mpm_ctx);
2037  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2038 
2039  const char *buf = "abcdefghijklmnopqrstuvwxyz";
2040  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2041  strlen(buf));
2042 
2043  if (cnt == 2)
2044  result = 1;
2045  else
2046  printf("2 != %" PRIu32 " ", cnt);
2047 
2048  SCHSDestroyCtx(&mpm_ctx);
2049  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2050  PmqFree(&pmq);
2051  return result;
2052 }
2053 
2054 static int SCHSTest23(void)
2055 {
2056  int result = 0;
2057  MpmCtx mpm_ctx;
2058  MpmThreadCtx mpm_thread_ctx;
2059  PrefilterRuleStore pmq;
2060 
2061  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2062  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2063  MpmInitCtx(&mpm_ctx, MPM_HS);
2064 
2065  /* 1 */
2066  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2067  PmqSetup(&pmq);
2068 
2069  SCHSPreparePatterns(NULL, &mpm_ctx);
2070  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2071 
2072  uint32_t cnt =
2073  SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"aa", 2);
2074 
2075  if (cnt == 0)
2076  result = 1;
2077  else
2078  printf("1 != %" PRIu32 " ", cnt);
2079 
2080  SCHSDestroyCtx(&mpm_ctx);
2081  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2082  PmqFree(&pmq);
2083  return result;
2084 }
2085 
2086 static int SCHSTest24(void)
2087 {
2088  int result = 0;
2089  MpmCtx mpm_ctx;
2090  MpmThreadCtx mpm_thread_ctx;
2091  PrefilterRuleStore pmq;
2092 
2093  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2094  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2095  MpmInitCtx(&mpm_ctx, MPM_HS);
2096 
2097  /* 1 */
2098  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0);
2099  PmqSetup(&pmq);
2100 
2101  SCHSPreparePatterns(NULL, &mpm_ctx);
2102  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2103 
2104  uint32_t cnt =
2105  SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)"aa", 2);
2106 
2107  if (cnt == 1)
2108  result = 1;
2109  else
2110  printf("1 != %" PRIu32 " ", cnt);
2111 
2112  SCHSDestroyCtx(&mpm_ctx);
2113  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2114  PmqFree(&pmq);
2115  return result;
2116 }
2117 
2118 static int SCHSTest25(void)
2119 {
2120  int result = 0;
2121  MpmCtx mpm_ctx;
2122  MpmThreadCtx mpm_thread_ctx;
2123  PrefilterRuleStore pmq;
2124 
2125  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2126  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2127  MpmInitCtx(&mpm_ctx, MPM_HS);
2128 
2129  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0);
2130  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0);
2131  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"fghiJkl", 7, 0, 0, 2, 0, 0);
2132  PmqSetup(&pmq);
2133 
2134  SCHSPreparePatterns(NULL, &mpm_ctx);
2135  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2136 
2137  const char *buf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2138  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2139  strlen(buf));
2140 
2141  if (cnt == 3)
2142  result = 1;
2143  else
2144  printf("3 != %" PRIu32 " ", cnt);
2145 
2146  SCHSDestroyCtx(&mpm_ctx);
2147  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2148  PmqFree(&pmq);
2149  return result;
2150 }
2151 
2152 static int SCHSTest26(void)
2153 {
2154  int result = 0;
2155  MpmCtx mpm_ctx;
2156  MpmThreadCtx mpm_thread_ctx;
2157  PrefilterRuleStore pmq;
2158 
2159  memset(&mpm_ctx, 0x00, sizeof(MpmCtx));
2160  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2161  MpmInitCtx(&mpm_ctx, MPM_HS);
2162 
2163  SCMpmAddPatternCI(&mpm_ctx, (uint8_t *)"Works", 5, 0, 0, 0, 0, 0);
2164  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"Works", 5, 0, 0, 1, 0, 0);
2165  PmqSetup(&pmq);
2166 
2167  SCHSPreparePatterns(NULL, &mpm_ctx);
2168  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2169 
2170  const char *buf = "works";
2171  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2172  strlen(buf));
2173 
2174  if (cnt == 1)
2175  result = 1;
2176  else
2177  printf("3 != %" PRIu32 " ", cnt);
2178 
2179  SCHSDestroyCtx(&mpm_ctx);
2180  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2181  PmqFree(&pmq);
2182  return result;
2183 }
2184 
2185 static int SCHSTest27(void)
2186 {
2187  int result = 0;
2188  MpmCtx mpm_ctx;
2189  MpmThreadCtx mpm_thread_ctx;
2190  PrefilterRuleStore pmq;
2191 
2192  memset(&mpm_ctx, 0, sizeof(MpmCtx));
2193  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2194  MpmInitCtx(&mpm_ctx, MPM_HS);
2195 
2196  /* 0 match */
2197  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"ONE", 3, 0, 0, 0, 0, 0);
2198  PmqSetup(&pmq);
2199 
2200  SCHSPreparePatterns(NULL, &mpm_ctx);
2201  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2202 
2203  const char *buf = "tone";
2204  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2205  strlen(buf));
2206 
2207  if (cnt == 0)
2208  result = 1;
2209  else
2210  printf("0 != %" PRIu32 " ", cnt);
2211 
2212  SCHSDestroyCtx(&mpm_ctx);
2213  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2214  PmqFree(&pmq);
2215  return result;
2216 }
2217 
2218 static int SCHSTest28(void)
2219 {
2220  int result = 0;
2221  MpmCtx mpm_ctx;
2222  MpmThreadCtx mpm_thread_ctx;
2223  PrefilterRuleStore pmq;
2224 
2225  memset(&mpm_ctx, 0, sizeof(MpmCtx));
2226  memset(&mpm_thread_ctx, 0, sizeof(MpmThreadCtx));
2227  MpmInitCtx(&mpm_ctx, MPM_HS);
2228 
2229  /* 0 match */
2230  MpmAddPatternCS(&mpm_ctx, (uint8_t *)"one", 3, 0, 0, 0, 0, 0);
2231  PmqSetup(&pmq);
2232 
2233  SCHSPreparePatterns(NULL, &mpm_ctx);
2234  SCHSInitThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2235 
2236  const char *buf = "tONE";
2237  uint32_t cnt = SCHSSearch(&mpm_ctx, &mpm_thread_ctx, &pmq, (uint8_t *)buf,
2238  strlen(buf));
2239 
2240  if (cnt == 0)
2241  result = 1;
2242  else
2243  printf("0 != %" PRIu32 " ", cnt);
2244 
2245  SCHSDestroyCtx(&mpm_ctx);
2246  SCHSDestroyThreadCtx(&mpm_ctx, &mpm_thread_ctx);
2247  PmqFree(&pmq);
2248  return result;
2249 }
2250 
2251 static int SCHSTest29(void)
2252 {
2253  uint8_t buf[] = "onetwothreefourfivesixseveneightnine";
2254  uint16_t buflen = sizeof(buf) - 1;
2255  Packet *p = NULL;
2256  ThreadVars th_v;
2257  DetectEngineThreadCtx *det_ctx = NULL;
2258  int result = 0;
2259 
2260  memset(&th_v, 0, sizeof(th_v));
2261  StatsThreadInit(&th_v.stats);
2262  p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
2263 
2265  if (de_ctx == NULL)
2266  goto end;
2268 
2269  de_ctx->flags |= DE_QUIET;
2270 
2271  de_ctx->sig_list = SigInit(
2272  de_ctx, "alert tcp any any -> any any "
2273  "(content:\"onetwothreefourfivesixseveneightnine\"; sid:1;)");
2274  if (de_ctx->sig_list == NULL)
2275  goto end;
2276  de_ctx->sig_list->next =
2277  SigInit(de_ctx, "alert tcp any any -> any any "
2278  "(content:\"onetwothreefourfivesixseveneightnine\"; "
2279  "fast_pattern:3,3; sid:2;)");
2280  if (de_ctx->sig_list->next == NULL)
2281  goto end;
2282 
2284  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
2285 
2286  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
2287  if (PacketAlertCheck(p, 1) != 1) {
2288  printf("if (PacketAlertCheck(p, 1) != 1) failure\n");
2289  goto end;
2290  }
2291  if (PacketAlertCheck(p, 2) != 1) {
2292  printf("if (PacketAlertCheck(p, 1) != 2) failure\n");
2293  goto end;
2294  }
2295 
2296  result = 1;
2297 end:
2298  UTHFreePackets(&p, 1);
2299  if (de_ctx != NULL) {
2300  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
2302  }
2303 
2304  StatsThreadCleanup(&th_v.stats);
2305  return result;
2306 }
2307 
2308 static void SCHSRegisterTests(void)
2309 {
2310  UtRegisterTest("SCHSTest01", SCHSTest01);
2311  UtRegisterTest("SCHSTest02", SCHSTest02);
2312  UtRegisterTest("SCHSTest03", SCHSTest03);
2313  UtRegisterTest("SCHSTest04", SCHSTest04);
2314  UtRegisterTest("SCHSTest05", SCHSTest05);
2315  UtRegisterTest("SCHSTest06", SCHSTest06);
2316  UtRegisterTest("SCHSTest07", SCHSTest07);
2317  UtRegisterTest("SCHSTest08", SCHSTest08);
2318  UtRegisterTest("SCHSTest09", SCHSTest09);
2319  UtRegisterTest("SCHSTest10", SCHSTest10);
2320  UtRegisterTest("SCHSTest11", SCHSTest11);
2321  UtRegisterTest("SCHSTest12", SCHSTest12);
2322  UtRegisterTest("SCHSTest13", SCHSTest13);
2323  UtRegisterTest("SCHSTest14", SCHSTest14);
2324  UtRegisterTest("SCHSTest15", SCHSTest15);
2325  UtRegisterTest("SCHSTest16", SCHSTest16);
2326  UtRegisterTest("SCHSTest17", SCHSTest17);
2327  UtRegisterTest("SCHSTest18", SCHSTest18);
2328  UtRegisterTest("SCHSTest19", SCHSTest19);
2329  UtRegisterTest("SCHSTest20", SCHSTest20);
2330  UtRegisterTest("SCHSTest21", SCHSTest21);
2331  UtRegisterTest("SCHSTest22", SCHSTest22);
2332  UtRegisterTest("SCHSTest23", SCHSTest23);
2333  UtRegisterTest("SCHSTest24", SCHSTest24);
2334  UtRegisterTest("SCHSTest25", SCHSTest25);
2335  UtRegisterTest("SCHSTest26", SCHSTest26);
2336  UtRegisterTest("SCHSTest27", SCHSTest27);
2337  UtRegisterTest("SCHSTest28", SCHSTest28);
2338  UtRegisterTest("SCHSTest29", SCHSTest29);
2339 }
2340 #endif /* UNITTESTS */
2341 #endif /* BUILD_HYPERSCAN */
MpmTableElmt_::PrintThreadCtx
void(* PrintThreadCtx)(struct MpmThreadCtx_ *)
Definition: util-mpm.h:188
len
uint8_t len
Definition: app-layer-dnp3.h:2
detect-engine.h
hashword
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:172
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
MpmTableElmt_::InitThreadCtx
void(* InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:157
MpmThreadCtx_
Definition: util-mpm.h:48
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
MpmTableElmt_::name
const char * name
Definition: util-mpm.h:155
PrefilterRuleStore_
structure for storing potential rule matches
Definition: util-prefilter.h:34
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
HashTableIterate
void HashTableIterate(HashTable *ht, void(*CallbackFn)(void *, void *), void *aux)
Definition: util-hash.c:211
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:142
MpmThreadCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:51
SC_SHA256_LEN
#define SC_SHA256_LEN
Definition: util-file.h:104
util-hash.h
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
MPM_FEATURE_FLAG_OFFSET
#define MPM_FEATURE_FLAG_OFFSET
Definition: util-mpm.h:151
util-memcpy.h
MpmCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:112
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2684
MpmTableElmt_::CacheStatsPrint
void(* CacheStatsPrint)(void *data)
Definition: util-mpm.h:181
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
HashTable_
Definition: util-hash.h:35
DE_QUIET
#define DE_QUIET
Definition: detect.h:329
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:109
MpmTableElmt_::AddPattern
int(* AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:176
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:365
MpmTableElmt_::feature_flags
uint8_t feature_flags
Definition: util-mpm.h:192
util-hyperscan.h
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2420
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
MpmCtx_::maxdepth
uint16_t maxdepth
Definition: util-mpm.h:103
MAX
#define MAX(x, y)
Definition: suricata-common.h:412
MpmTableElmt_::InitCtx
void(* InitCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:156
util-unittest.h
util-unittest-helper.h
HashTableFree
void HashTableFree(HashTable *ht)
Free a HashTable and all its contents.
Definition: util-hash.c:100
MpmConfig_::cache_dir_path
const char * cache_dir_path
Definition: util-mpm.h:92
HashTable_::array_size
uint32_t array_size
Definition: util-hash.h:37
util-memcmp.h
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:209
Signature_::next
struct Signature_ * next
Definition: detect.h:750
MpmTableElmt_::PrintCtx
void(* PrintCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:187
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
MpmThreadCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:52
SCMpmAddPatternCI
int SCMpmAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:258
HashTableLookup
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:182
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3414
MpmTableElmt_::CacheStatsDeinit
void(* CacheStatsDeinit)(void *data)
Definition: util-mpm.h:182
HashTableRemove
int HashTableRemove(HashTable *ht, void *data, uint16_t datalen)
Remove an item from the hash table.
Definition: util-hash.c:166
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:936
MPM_PATTERN_FLAG_OFFSET
#define MPM_PATTERN_FLAG_OFFSET
Definition: util-mpm.h:144
MPM_FEATURE_FLAG_DEPTH
#define MPM_FEATURE_FLAG_DEPTH
Definition: util-mpm.h:150
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
HashTableAdd
int HashTableAdd(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:120
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3104
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:108
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
hashlittle_safe
uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:482
SigIntId
#define SigIntId
Definition: detect-engine-state.h:38
Packet_
Definition: decode.h:501
detect-engine-build.h
MpmTableElmt_::CacheStatsInit
void *(* CacheStatsInit)(void)
Definition: util-mpm.h:180
detect-engine-alert.h
conf.h
MpmTableElmt_::CacheRuleset
int(* CacheRuleset)(MpmConfig *)
Definition: util-mpm.h:183
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:186
MpmAddPatternCS
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:249
MPMCTX_FLAGS_NODEPTH
#define MPMCTX_FLAGS_NODEPTH
Definition: util-mpm.h:88
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2194
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1333
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
MPM_PATTERN_FLAG_NOCASE
#define MPM_PATTERN_FLAG_NOCASE
Definition: util-mpm.h:140
ARRAY_SIZE
#define ARRAY_SIZE(arr)
Definition: suricata-common.h:562
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
util-conf.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
MpmCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm.h:106
util-path.h
util-mpm-hs-core.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3651
MpmConfig_::cache_stats
void * cache_stats
Definition: util-mpm.h:94
MpmTableElmt_::AddPatternNocase
int(* AddPatternNocase)(struct MpmCtx_ *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:177
MPM_HS
@ MPM_HS
Definition: util-mpm.h:40
FatalError
#define FatalError(...)
Definition: util-debug.h:517
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:942
util-hash-lookup3.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
MpmTableElmt_::Prepare
int(* Prepare)(MpmConfig *, struct MpmCtx_ *)
Definition: util-mpm.h:179
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:158
SCFree
#define SCFree(p)
Definition: util-mem.h:61
MpmConfig_
Definition: util-mpm.h:91
detect-parse.h
MpmCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:111
HashTableInit
HashTable * HashTableInit(uint32_t size, uint32_t(*Hash)(struct HashTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hash.c:35
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2645
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
MpmTableElmt_::ConfigDeinit
void(* ConfigDeinit)(MpmConfig **)
Definition: util-mpm.h:162
suricata.h
MpmTableElmt_::ConfigCacheDirSet
void(* ConfigCacheDirSet)(MpmConfig *, const char *dir_path)
Definition: util-mpm.h:163
util-mpm-hs.h
PmqFree
void PmqFree(PrefilterRuleStore *pmq)
Cleanup and free a Pmq.
Definition: util-prefilter.c:126
MpmTableElmt_::CachePrune
int(* CachePrune)(MpmConfig *)
Definition: util-mpm.h:184
MpmTableElmt_::ConfigInit
MpmConfig *(* ConfigInit)(void)
Definition: util-mpm.h:161
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:935
MpmTableElmt_::DestroyThreadCtx
void(* DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:159
MpmCtx_
Definition: util-mpm.h:97
util-mpm-hs-cache.h
MPM_PATTERN_FLAG_DEPTH
#define MPM_PATTERN_FLAG_DEPTH
Definition: util-mpm.h:142
MpmCtx_::flags
uint8_t flags
Definition: util-mpm.h:101
MPMCTX_FLAGS_CACHE_TO_DISK
#define MPMCTX_FLAGS_CACHE_TO_DISK
Definition: util-mpm.h:89
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
MpmHSRegister
void MpmHSRegister(void)
MpmCtx_::ctx
void * ctx
Definition: util-mpm.h:98
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1429
SCMemcmp
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:290
SCMutex
#define SCMutex
Definition: threads-debug.h:114
MpmThreadCtx_::ctx
void * ctx
Definition: util-mpm.h:49
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
MpmTableElmt_::RegisterUnittests
void(* RegisterUnittests)(void)
Definition: util-mpm.h:190
PmqSetup
int PmqSetup(PrefilterRuleStore *pmq)
Setup a pmq.
Definition: util-prefilter.c:37
MpmHSGlobalCleanup
void MpmHSGlobalCleanup(void)
SCCreateDirectoryTree
int SCCreateDirectoryTree(const char *path, const bool final)
Recursively create a directory.
Definition: util-path.c:137
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:456