Skip to content

Commit f129147

Browse files
hongyang7fatchanghao
authored andcommitted
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.
1 parent c37166d commit f129147

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

src/runtime.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ u8 *getHistory(char *state, const struct RoseEngine *t, u64a offset) {
9090
* callers.
9191
*/
9292
static really_inline
93-
char validScratch(const struct hs_scratch *s, u32 crc) {
93+
char validScratch(const struct RoseEngine *t, const struct hs_scratch *s) {
9494
if (!ISALIGNED_CL(s)) {
9595
DEBUG_PRINTF("bad alignment %p\n", s);
9696
return 0;
@@ -101,12 +101,18 @@ char validScratch(const struct hs_scratch *s, u32 crc) {
101101
return 0;
102102
}
103103

104-
/* add quick rose sanity checks by db crc*/
105-
if (s->db_crc != crc) {
106-
DEBUG_PRINTF("Improper scratch for current db\n");
104+
if (t->mode == HS_MODE_BLOCK && t->stateOffsets.end > s->bStateSize) {
105+
DEBUG_PRINTF("bad state size\n");
107106
return 0;
108107
}
109108

109+
if (t->queueCount > s->queueCount) {
110+
DEBUG_PRINTF("bad queue count\n");
111+
return 0;
112+
}
113+
114+
/* TODO: add quick rose sanity checks */
115+
110116
return 1;
111117
}
112118

@@ -329,7 +335,7 @@ hs_error_t HS_CDECL hs_scan(const hs_database_t *db, const char *data,
329335
return HS_DB_MODE_ERROR;
330336
}
331337

332-
if (unlikely(!validScratch(scratch, db->crc32))) {
338+
if (unlikely(!validScratch(rose, scratch))) {
333339
return HS_INVALID;
334340
}
335341

@@ -503,7 +509,7 @@ void maintainHistoryBuffer(const struct RoseEngine *rose, char *state,
503509

504510
static really_inline
505511
void init_stream(struct hs_stream *s, const struct RoseEngine *rose,
506-
char init_history, u32 crc) {
512+
char init_history) {
507513
char *state = getMultiState(s);
508514

509515
if (init_history) {
@@ -518,7 +524,6 @@ void init_stream(struct hs_stream *s, const struct RoseEngine *rose,
518524

519525
s->rose = rose;
520526
s->offset = 0;
521-
s->crc32 = crc;
522527

523528
setStreamStatus(state, 0);
524529
roseInitState(rose, state);
@@ -563,7 +568,7 @@ hs_error_t HS_CDECL hs_open_stream(const hs_database_t *db,
563568
return HS_NOMEM;
564569
}
565570

566-
init_stream(s, rose, 1, db->crc32);
571+
init_stream(s, rose, 1);
567572

568573
*stream = s;
569574
return HS_SUCCESS;
@@ -751,7 +756,7 @@ hs_error_t HS_CDECL hs_reset_and_copy_stream(hs_stream_t *to_id,
751756
}
752757

753758
if (onEvent) {
754-
if (!scratch || !validScratch(scratch, to_id->crc32)) {
759+
if (!scratch || !validScratch(to_id->rose, scratch)) {
755760
return HS_INVALID;
756761
}
757762
if (unlikely(markScratchInUse(scratch))) {
@@ -977,7 +982,7 @@ hs_error_t HS_CDECL hs_scan_stream(hs_stream_t *id, const char *data,
977982
hs_scratch_t *scratch,
978983
match_event_handler onEvent, void *context) {
979984
if (unlikely(!id || !scratch || !data ||
980-
!validScratch(scratch, id->crc32))) {
985+
!validScratch(id->rose, scratch))) {
981986
return HS_INVALID;
982987
}
983988

@@ -999,7 +1004,7 @@ hs_error_t HS_CDECL hs_close_stream(hs_stream_t *id, hs_scratch_t *scratch,
9991004
}
10001005

10011006
if (onEvent) {
1002-
if (!scratch || !validScratch(scratch, id->crc32)) {
1007+
if (!scratch || !validScratch(id->rose, scratch)) {
10031008
return HS_INVALID;
10041009
}
10051010
if (unlikely(markScratchInUse(scratch))) {
@@ -1029,7 +1034,7 @@ hs_error_t HS_CDECL hs_reset_stream(hs_stream_t *id, UNUSED unsigned int flags,
10291034
}
10301035

10311036
if (onEvent) {
1032-
if (!scratch || !validScratch(scratch, id->crc32)) {
1037+
if (!scratch || !validScratch(id->rose, scratch)) {
10331038
return HS_INVALID;
10341039
}
10351040
if (unlikely(markScratchInUse(scratch))) {
@@ -1044,7 +1049,7 @@ hs_error_t HS_CDECL hs_reset_stream(hs_stream_t *id, UNUSED unsigned int flags,
10441049
}
10451050

10461051
// history already initialised
1047-
init_stream(id, id->rose, 0, id->crc32);
1052+
init_stream(id, id->rose, 0);
10481053

10491054
return HS_SUCCESS;
10501055
}
@@ -1123,7 +1128,7 @@ hs_error_t HS_CDECL hs_scan_vector(const hs_database_t *db,
11231128
return HS_DB_MODE_ERROR;
11241129
}
11251130

1126-
if (unlikely(!validScratch(scratch, db->crc32))) {
1131+
if (unlikely(!validScratch(rose, scratch))) {
11271132
return HS_INVALID;
11281133
}
11291134

@@ -1133,7 +1138,7 @@ hs_error_t HS_CDECL hs_scan_vector(const hs_database_t *db,
11331138

11341139
hs_stream_t *id = (hs_stream_t *)(scratch->bstate);
11351140

1136-
init_stream(id, rose, 1, db->crc32); /* open stream */
1141+
init_stream(id, rose, 1); /* open stream */
11371142

11381143
for (u32 i = 0; i < count; i++) {
11391144
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,
12481253
const struct RoseEngine *rose = to_stream->rose;
12491254

12501255
if (onEvent) {
1251-
if (!scratch || !validScratch(scratch, to_stream->crc32)) {
1256+
if (!scratch || !validScratch(to_stream->rose, scratch)) {
12521257
return HS_INVALID;
12531258
}
12541259
if (unlikely(markScratchInUse(scratch))) {

src/scratch.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2022, Intel Corporation
2+
* Copyright (c) 2015-2023, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are met:
@@ -373,15 +373,13 @@ hs_error_t HS_CDECL hs_alloc_scratch(const hs_database_t *db,
373373
hs_scratch_free((*scratch)->scratch_alloc);
374374
}
375375

376-
proto->db_crc = db->crc32;
377376
hs_error_t alloc_ret = alloc_scratch(proto, scratch);
378377
hs_scratch_free(proto_tmp); /* kill off temp used for sizing */
379378
if (alloc_ret != HS_SUCCESS) {
380379
*scratch = NULL;
381380
return alloc_ret;
382381
}
383382
} else {
384-
(*scratch)->db_crc = db->crc32;
385383
hs_scratch_free(proto_tmp); /* kill off temp used for sizing */
386384
unmarkScratchInUse(*scratch);
387385
}

src/scratch.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2022, Intel Corporation
2+
* Copyright (c) 2015-2023, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are met:
@@ -171,7 +171,6 @@ struct match_deduper {
171171
*/
172172
struct ALIGN_CL_DIRECTIVE hs_scratch {
173173
u32 magic;
174-
u32 db_crc; /**< identity of a scratch space, for validity check */
175174
u8 in_use; /**< non-zero when being used by an API call. */
176175
u32 queueCount;
177176
u32 activeQueueArraySize; /**< size of active queue array fatbit in bytes */

src/state.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2022, Intel Corporation
2+
* Copyright (c) 2015-2023, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are met:
@@ -57,9 +57,6 @@ struct hs_stream {
5757

5858
/** \brief The current stream offset. */
5959
u64a offset;
60-
61-
/** \brief Identity of hs_stream, for scratch validity check. */
62-
u32 crc32;
6360
};
6461

6562
#define getMultiState(hs_s) ((char *)(hs_s) + sizeof(*(hs_s)))

src/stream_compress_impl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2022, Intel Corporation
2+
* Copyright (c) 2017-2023, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are met:
@@ -116,7 +116,6 @@ size_t JOIN(sc_, FN_SUFFIX)(const struct RoseEngine *rose,
116116
= ((STREAM_QUAL char *)stream) + sizeof(struct hs_stream);
117117

118118
COPY_FIELD(stream->offset);
119-
COPY_FIELD(stream->crc32);
120119
ASSIGN(stream->rose, rose);
121120

122121
COPY(stream_body + ROSE_STATE_OFFSET_STATUS_FLAGS, 1);

0 commit comments

Comments
 (0)