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

Lua xform/v21 #12425

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
18 changes: 10 additions & 8 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,14 +958,15 @@ static char DetectBufferTypeCompareIdFunc(void *data1, uint16_t len1, void *data
return map1->id == map2->id;
}

static void DetectBufferTypeFreeFunc(void *data)
static void DetectBufferTypeFreeFunc(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 @@ -975,7 +976,8 @@ 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);
}

SCFree(map);
Expand All @@ -984,7 +986,7 @@ static void DetectBufferTypeFreeFunc(void *data)
static int DetectBufferTypeInit(void)
{
BUG_ON(g_buffer_type_hash);
g_buffer_type_hash = HashListTableInit(256, DetectBufferTypeHashNameFunc,
g_buffer_type_hash = HashListTableInitWithCtx(256, DetectBufferTypeHashNameFunc,
Copy link
Member Author

Choose a reason for hiding this comment

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

is this never freed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it is. I checked master and I'm not seeing the free happen by the time exit is called

 $ git diff
diff --git a/src/util-hashlist.c b/src/util-hashlist.c
index 085a988afe..0b340ea606 100644
--- a/src/util-hashlist.c
+++ b/src/util-hashlist.c
@@ -108,7 +108,9 @@ void HashListTableFree(HashListTable *ht)
     if (ht->array != NULL)
         SCFree(ht->array);

-    SCFree(ht);
+    void *ptr = ht;
+    memset(ht, 0, sizeof(*ht));
+    SCFree(ptr);
 }

 int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)

And via gdb

Thread 1 "Suricata-Main" hit Breakpoint 1, __GI_exit (status=status@entry=0) at ./stdlib/exit.c:137
warning: 137	./stdlib/exit.c: No such file or directory
(gdb) p *g_buffer_type_hash
$1 = {array = 0x55555636a240, listhead = 0x555556295de0, listtail = 0x5555563e8680, array_size = 256, Hash = 0x5555557b3680 <DetectBufferTypeHashNameFunc>,
 Compare = 0x5555557b3700 <DetectBufferTypeCompareNameFunc>, Free = 0x5555557b3770 <DetectBufferTypeFreeFunc>}

If it was freed, the dump from gdb should've shown 0's.

Am I missing something?

DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
if (g_buffer_type_hash == NULL)
return -1;
Expand Down Expand Up @@ -1729,7 +1731,7 @@ 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,
de_ctx->buffer_type_hash_name = HashListTableInitWithCtx(256, DetectBufferTypeHashNameFunc,
DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
BUG_ON(de_ctx->buffer_type_hash_name == NULL);
de_ctx->buffer_type_hash_id =
Expand Down Expand Up @@ -1771,7 +1773,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