]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
scratch: add quick validity check
authorHong, Yang A <yang.a.hong@intel.com>
Thu, 20 Oct 2022 08:48:46 +0000 (08:48 +0000)
committerKonstantinos Margaritis <markos@freevec.org>
Tue, 5 Sep 2023 10:51:15 +0000 (13:51 +0300)
fix github issue #350

src/runtime.c
src/scratch.c
src/scratch.h
src/state.h
src/stream_compress_impl.h

index ab46db1a612f6aa02ff258efe515eede6f52a306..3c2d65338ba96a9ec1e7748bb27f0730c1837043 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2019, Intel Corporation
+ * Copyright (c) 2015-2022, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -90,7 +90,7 @@ u8 *getHistory(char *state, const struct RoseEngine *t, u64a offset) {
  * callers.
  */
 static really_inline
-char validScratch(const struct RoseEngine *t, const struct hs_scratch *s) {
+char validScratch(const struct hs_scratch *s, u32 crc) {
     if (!ISALIGNED_CL(s)) {
         DEBUG_PRINTF("bad alignment %p\n", s);
         return 0;
@@ -101,18 +101,12 @@ char validScratch(const struct RoseEngine *t, const struct hs_scratch *s) {
         return 0;
     }
 
-    if (t->mode == HS_MODE_BLOCK && t->stateOffsets.end > s->bStateSize) {
-        DEBUG_PRINTF("bad state size\n");
+    /* add quick rose sanity checks by db crc*/
+    if (s->db_crc != crc) {
+        DEBUG_PRINTF("Improper scratch for current db\n");
         return 0;
     }
 
-    if (t->queueCount > s->queueCount) {
-        DEBUG_PRINTF("bad queue count\n");
-        return 0;
-    }
-
-    /* TODO: add quick rose sanity checks */
-
     return 1;
 }
 
@@ -335,7 +329,7 @@ hs_error_t HS_CDECL hs_scan(const hs_database_t *db, const char *data,
         return HS_DB_MODE_ERROR;
     }
 
-    if (unlikely(!validScratch(rose, scratch))) {
+    if (unlikely(!validScratch(scratch, db->crc32))) {
         return HS_INVALID;
     }
 
@@ -509,7 +503,7 @@ void maintainHistoryBuffer(const struct RoseEngine *rose, char *state,
 
 static really_inline
 void init_stream(struct hs_stream *s, const struct RoseEngine *rose,
-                 char init_history) {
+                 char init_history, u32 crc) {
     char *state = getMultiState(s);
 
     if (init_history) {
@@ -524,6 +518,7 @@ void init_stream(struct hs_stream *s, const struct RoseEngine *rose,
 
     s->rose = rose;
     s->offset = 0;
+    s->crc32 = crc;
 
     setStreamStatus(state, 0);
     roseInitState(rose, state);
@@ -568,7 +563,7 @@ hs_error_t HS_CDECL hs_open_stream(const hs_database_t *db,
         return HS_NOMEM;
     }
 
-    init_stream(s, rose, 1);
+    init_stream(s, rose, 1, db->crc32);
 
     *stream = s;
     return HS_SUCCESS;
@@ -756,7 +751,7 @@ hs_error_t HS_CDECL hs_reset_and_copy_stream(hs_stream_t *to_id,
     }
 
     if (onEvent) {
-        if (!scratch || !validScratch(to_id->rose, scratch)) {
+        if (!scratch || !validScratch(scratch, to_id->crc32)) {
             return HS_INVALID;
         }
         if (unlikely(markScratchInUse(scratch))) {
@@ -982,7 +977,7 @@ hs_error_t HS_CDECL hs_scan_stream(hs_stream_t *id, const char *data,
                                    hs_scratch_t *scratch,
                                    match_event_handler onEvent, void *context) {
     if (unlikely(!id || !scratch || !data ||
-                 !validScratch(id->rose, scratch))) {
+                 !validScratch(scratch, id->crc32))) {
         return HS_INVALID;
     }
 
@@ -1004,7 +999,7 @@ hs_error_t HS_CDECL hs_close_stream(hs_stream_t *id, hs_scratch_t *scratch,
     }
 
     if (onEvent) {
-        if (!scratch || !validScratch(id->rose, scratch)) {
+        if (!scratch || !validScratch(scratch, id->crc32)) {
             return HS_INVALID;
         }
         if (unlikely(markScratchInUse(scratch))) {
@@ -1034,7 +1029,7 @@ hs_error_t HS_CDECL hs_reset_stream(hs_stream_t *id, UNUSED unsigned int flags,
     }
 
     if (onEvent) {
-        if (!scratch || !validScratch(id->rose, scratch)) {
+        if (!scratch || !validScratch(scratch, id->crc32)) {
             return HS_INVALID;
         }
         if (unlikely(markScratchInUse(scratch))) {
@@ -1049,7 +1044,7 @@ hs_error_t HS_CDECL hs_reset_stream(hs_stream_t *id, UNUSED unsigned int flags,
     }
 
     // history already initialised
-    init_stream(id, id->rose, 0);
+    init_stream(id, id->rose, 0, id->crc32);
 
     return HS_SUCCESS;
 }
@@ -1128,7 +1123,7 @@ hs_error_t HS_CDECL hs_scan_vector(const hs_database_t *db,
         return HS_DB_MODE_ERROR;
     }
 
-    if (unlikely(!validScratch(rose, scratch))) {
+    if (unlikely(!validScratch(scratch, db->crc32))) {
         return HS_INVALID;
     }
 
@@ -1138,7 +1133,7 @@ hs_error_t HS_CDECL hs_scan_vector(const hs_database_t *db,
 
     hs_stream_t *id = (hs_stream_t *)(scratch->bstate);
 
-    init_stream(id, rose, 1); /* open stream */
+    init_stream(id, rose, 1, db->crc32); /* open stream */
 
     for (u32 i = 0; i < count; i++) {
         DEBUG_PRINTF("block %u/%u offset=%llu len=%u\n", i, count, id->offset,
@@ -1253,7 +1248,7 @@ hs_error_t HS_CDECL hs_reset_and_expand_stream(hs_stream_t *to_stream,
     const struct RoseEngine *rose = to_stream->rose;
 
     if (onEvent) {
-        if (!scratch || !validScratch(to_stream->rose, scratch)) {
+        if (!scratch || !validScratch(scratch, to_stream->crc32)) {
             return HS_INVALID;
         }
         if (unlikely(markScratchInUse(scratch))) {
index 25991e2bbad481ba52926742d95b9ce351007887..5849380d66d8274deb7fa8a3b08697409cf28390 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2019, Intel Corporation
+ * Copyright (c) 2015-2022, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -373,6 +373,7 @@ hs_error_t HS_CDECL hs_alloc_scratch(const hs_database_t *db,
             hs_scratch_free((*scratch)->scratch_alloc);
         }
 
+        proto->db_crc = db->crc32;
         hs_error_t alloc_ret = alloc_scratch(proto, scratch);
         hs_scratch_free(proto_tmp); /* kill off temp used for sizing */
         if (alloc_ret != HS_SUCCESS) {
@@ -380,6 +381,7 @@ hs_error_t HS_CDECL hs_alloc_scratch(const hs_database_t *db,
             return alloc_ret;
         }
     } else {
+        (*scratch)->db_crc = db->crc32;
         hs_scratch_free(proto_tmp); /* kill off temp used for sizing */
         unmarkScratchInUse(*scratch);
     }
index 1256f7aba82e587b663ff694ae5f3ac599b312d8..efaa68841787db1cbea1b68701cc911d7185f1e2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2019, Intel Corporation
+ * Copyright (c) 2015-2022, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -171,6 +171,7 @@ struct match_deduper {
  */
 struct ALIGN_CL_DIRECTIVE hs_scratch {
     u32 magic;
+    u32 db_crc; /**< identity of a scratch space, for validity check */
     u8 in_use; /**< non-zero when being used by an API call. */
     u32 queueCount;
     u32 activeQueueArraySize; /**< size of active queue array fatbit in bytes */
index 9ade59db4bb9ce8e4680246e02a095850c279424..567001ea84e0637466bfc27adcd153ee68e13d27 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2022, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -57,6 +57,9 @@ struct hs_stream {
 
     /** \brief The current stream offset. */
     u64a offset;
+
+    /** \brief Identity of hs_stream, for scratch validity check. */
+    u32 crc32;
 };
 
 #define getMultiState(hs_s)      ((char *)(hs_s) + sizeof(*(hs_s)))
index d1ccf5e6d03485c508277a9b418bd9e0c93ddea1..ceea14a6f88ba2221cbb69040e78732f28597aa8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018, Intel Corporation
+ * Copyright (c) 2017-2022, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -116,6 +116,7 @@ size_t JOIN(sc_, FN_SUFFIX)(const struct RoseEngine *rose,
         = ((STREAM_QUAL char *)stream) + sizeof(struct hs_stream);
 
     COPY_FIELD(stream->offset);
+    COPY_FIELD(stream->crc32);
     ASSIGN(stream->rose, rose);
 
     COPY(stream_body + ROSE_STATE_OFFSET_STATUS_FLAGS, 1);