scratch: remove quick validity check

Roll back fix for github issue #350

About Scratch Usage:
For compile time, scratch space is strongly recommended to be
allocated immediately after database generation.
For runtime, besides using scratch for corresponding database,
Hyperscan also allows user to use larger scratch space allocated
for another database.
When multiple concurrent threads need to use the same databases
and a new scratch space is required, cloning the largest one is
always safe. This is realized based on API hs_scratch_size() and
hs_clone_scratch().
Behaviors beyond above are discouraged and results are undefined.
This commit is contained in:
Hong, Yang A
2023-03-01 14:42:27 +00:00
committed by Konstantinos Margaritis
parent 5131c37698
commit e4ffd75282
5 changed files with 25 additions and 27 deletions

View File

@@ -90,7 +90,7 @@ u8 *getHistory(char *state, const struct RoseEngine *t, u64a offset) {
* callers.
*/
static really_inline
char validScratch(const struct hs_scratch *s, u32 crc) {
char validScratch(const struct RoseEngine *t, const struct hs_scratch *s) {
if (!ISALIGNED_CL(s)) {
DEBUG_PRINTF("bad alignment %p\n", s);
return 0;
@@ -101,12 +101,18 @@ char validScratch(const struct hs_scratch *s, u32 crc) {
return 0;
}
/* add quick rose sanity checks by db crc*/
if (s->db_crc != crc) {
DEBUG_PRINTF("Improper scratch for current db\n");
if (t->mode == HS_MODE_BLOCK && t->stateOffsets.end > s->bStateSize) {
DEBUG_PRINTF("bad state size\n");
return 0;
}
if (t->queueCount > s->queueCount) {
DEBUG_PRINTF("bad queue count\n");
return 0;
}
/* TODO: add quick rose sanity checks */
return 1;
}
@@ -329,7 +335,7 @@ hs_error_t HS_CDECL hs_scan(const hs_database_t *db, const char *data,
return HS_DB_MODE_ERROR;
}
if (unlikely(!validScratch(scratch, db->crc32))) {
if (unlikely(!validScratch(rose, scratch))) {
return HS_INVALID;
}
@@ -503,7 +509,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, u32 crc) {
char init_history) {
char *state = getMultiState(s);
if (init_history) {
@@ -518,7 +524,6 @@ 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);
@@ -563,7 +568,7 @@ hs_error_t HS_CDECL hs_open_stream(const hs_database_t *db,
return HS_NOMEM;
}
init_stream(s, rose, 1, db->crc32);
init_stream(s, rose, 1);
*stream = s;
return HS_SUCCESS;
@@ -751,7 +756,7 @@ hs_error_t HS_CDECL hs_reset_and_copy_stream(hs_stream_t *to_id,
}
if (onEvent) {
if (!scratch || !validScratch(scratch, to_id->crc32)) {
if (!scratch || !validScratch(to_id->rose, scratch)) {
return HS_INVALID;
}
if (unlikely(markScratchInUse(scratch))) {
@@ -977,7 +982,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(scratch, id->crc32))) {
!validScratch(id->rose, scratch))) {
return HS_INVALID;
}
@@ -999,7 +1004,7 @@ hs_error_t HS_CDECL hs_close_stream(hs_stream_t *id, hs_scratch_t *scratch,
}
if (onEvent) {
if (!scratch || !validScratch(scratch, id->crc32)) {
if (!scratch || !validScratch(id->rose, scratch)) {
return HS_INVALID;
}
if (unlikely(markScratchInUse(scratch))) {
@@ -1029,7 +1034,7 @@ hs_error_t HS_CDECL hs_reset_stream(hs_stream_t *id, UNUSED unsigned int flags,
}
if (onEvent) {
if (!scratch || !validScratch(scratch, id->crc32)) {
if (!scratch || !validScratch(id->rose, scratch)) {
return HS_INVALID;
}
if (unlikely(markScratchInUse(scratch))) {
@@ -1044,7 +1049,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, id->crc32);
init_stream(id, id->rose, 0);
return HS_SUCCESS;
}
@@ -1123,7 +1128,7 @@ hs_error_t HS_CDECL hs_scan_vector(const hs_database_t *db,
return HS_DB_MODE_ERROR;
}
if (unlikely(!validScratch(scratch, db->crc32))) {
if (unlikely(!validScratch(rose, scratch))) {
return HS_INVALID;
}
@@ -1133,7 +1138,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, db->crc32); /* open stream */
init_stream(id, rose, 1); /* open stream */
for (u32 i = 0; i < count; i++) {
DEBUG_PRINTF("block %u/%u offset=%llu len=%u\n", i, count, id->offset,
@@ -1248,7 +1253,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(scratch, to_stream->crc32)) {
if (!scratch || !validScratch(to_stream->rose, scratch)) {
return HS_INVALID;
}
if (unlikely(markScratchInUse(scratch))) {