Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen/refactor: transform and misc related changes for improved transform support #12502

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,15 @@ static char DetectBufferTypeCompareIdFunc(void *data1, uint16_t len1, void *data
return map1->id == map2->id;
}

static void DetectBufferTypeFreeFunc(void *data)
static void DetectBufferTypeFreeFuncWithCtx(void *ctx, void *data)
{
DetectBufferType *map = (DetectBufferType *)data;

if (map == NULL) {
if (data == NULL) {
return;
}

DetectBufferType *map = (DetectBufferType *)data;
DetectEngineCtx *de_ctx = (DetectEngineCtx *)ctx;

/* Release transformation option memory, if any */
for (int i = 0; i < map->transforms.cnt; i++) {
if (map->transforms.transforms[i].options == NULL)
Expand All @@ -977,12 +978,18 @@ static void DetectBufferTypeFreeFunc(void *data)
sigmatch_table[map->transforms.transforms[i].transform].name);
continue;
}
sigmatch_table[map->transforms.transforms[i].transform].Free(NULL, map->transforms.transforms[i].options);
sigmatch_table[map->transforms.transforms[i].transform].Free(
de_ctx, map->transforms.transforms[i].options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok but feels painful

Why will we need this de_ctx when freeing ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Luaxform will need the de_ctx to unregister the thread ctx (see #12425 detect-transform-luaxform.c, function DetectTransformLuaxformFree)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this thread ctx ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to have sigmatch_table having 2 different function pointers Free and
FreeCtx (not sure it is better, but requires less modifications)

}

SCFree(map);
}

static void DetectBufferTypeFreeFunc(void *data)
{
DetectBufferTypeFreeFuncWithCtx(NULL, data);
}

static int DetectBufferTypeInit(void)
{
BUG_ON(g_buffer_type_hash);
Expand Down Expand Up @@ -1731,8 +1738,8 @@ static void DetectBufferTypeSetupDetectEngine(DetectEngineCtx *de_ctx)
const int size = g_buffer_type_id;
BUG_ON(!(size > 0));

de_ctx->buffer_type_hash_name = HashListTableInit(256, DetectBufferTypeHashNameFunc,
DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
de_ctx->buffer_type_hash_name = HashListTableInitWithCtx(256, DetectBufferTypeHashNameFunc,
DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFuncWithCtx);
BUG_ON(de_ctx->buffer_type_hash_name == NULL);
de_ctx->buffer_type_hash_id =
HashListTableInit(256, DetectBufferTypeHashIdFunc, DetectBufferTypeCompareIdFunc,
Expand Down Expand Up @@ -1773,7 +1780,7 @@ static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx)
{
if (de_ctx) {
if (de_ctx->buffer_type_hash_name)
HashListTableFree(de_ctx->buffer_type_hash_name);
HashListTableFreeWithCtx(de_ctx, de_ctx->buffer_type_hash_name);
if (de_ctx->buffer_type_hash_id)
HashListTableFree(de_ctx->buffer_type_hash_id);

Expand Down
78 changes: 78 additions & 0 deletions src/util-hashlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,58 @@
#include "util-debug.h"
#include "util-memcmp.h"

HashListTable *HashListTableInitWithCtx(uint32_t size,
uint32_t (*Hash)(struct HashListTable_ *, void *, uint16_t),
char (*Compare)(void *, uint16_t, void *, uint16_t), void (*FreeWithCtx)(void *, void *))
{
sc_errno = SC_OK;
HashListTable *ht = NULL;

if (size == 0) {
sc_errno = SC_EINVAL;
goto error;
}

if (Hash == NULL) {
sc_errno = SC_EINVAL;
goto error;
}

/* setup the filter */
ht = SCCalloc(1, sizeof(HashListTable));
if (unlikely(ht == NULL)) {
sc_errno = SC_ENOMEM;
goto error;
}
ht->array_size = size;
ht->Hash = Hash;
ht->FreeWithCtx = FreeWithCtx;

if (Compare != NULL)
ht->Compare = Compare;
else
ht->Compare = HashListTableDefaultCompare;

/* setup the bitarray */
ht->array = SCCalloc(ht->array_size, sizeof(HashListTableBucket *));
if (ht->array == NULL) {
sc_errno = SC_ENOMEM;
goto error;
}

ht->listhead = NULL;
ht->listtail = NULL;
return ht;

error:
if (ht != NULL) {
if (ht->array != NULL)
SCFree(ht->array);

SCFree(ht);
}
return NULL;
}
HashListTable *HashListTableInit(uint32_t size,
uint32_t (*Hash)(struct HashListTable_ *, void *, uint16_t),
char (*Compare)(void *, uint16_t, void *, uint16_t), void (*Free)(void *))
Expand Down Expand Up @@ -85,6 +137,32 @@ HashListTable *HashListTableInit(uint32_t size,
return NULL;
}

void HashListTableFreeWithCtx(void *ctx, HashListTable *ht)
{
uint32_t i = 0;

if (ht == NULL)
return;

/* free the buckets */
for (i = 0; i < ht->array_size; i++) {
HashListTableBucket *hashbucket = ht->array[i];
while (hashbucket != NULL) {
HashListTableBucket *next_hashbucket = hashbucket->bucknext;
if (ht->FreeWithCtx != NULL)
ht->FreeWithCtx(ctx, hashbucket->data);
SCFree(hashbucket);
hashbucket = next_hashbucket;
}
}

/* free the array */
if (ht->array != NULL)
SCFree(ht->array);

SCFree(ht);
}

void HashListTableFree(HashListTable *ht)
{
uint32_t i = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/util-hashlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ typedef struct HashListTable_ {
uint32_t (*Hash)(struct HashListTable_ *, void *, uint16_t);
char (*Compare)(void *, uint16_t, void *, uint16_t);
void (*Free)(void *);
void (*FreeWithCtx)(void *, void *);
} HashListTable;

/* prototypes */
HashListTable* HashListTableInit(uint32_t, uint32_t (*Hash)(struct HashListTable_ *, void *, uint16_t), char (*Compare)(void *, uint16_t, void *, uint16_t), void (*Free)(void *));
HashListTable *HashListTableInitWithCtx(uint32_t,
uint32_t (*Hash)(struct HashListTable_ *, void *, uint16_t),
char (*Compare)(void *, uint16_t, void *, uint16_t), void (*FreeWithCtx)(void *, void *));

void HashListTableFreeWithCtx(void *, HashListTable *);
void HashListTableFree(HashListTable *);
int HashListTableAdd(HashListTable *, void *, uint16_t);
int HashListTableRemove(HashListTable *, void *, uint16_t);
Expand Down