suricata
thread-storage.c
Go to the documentation of this file.
1 /* Copyright (C) 2024 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "suricata-common.h"
19 #include "thread-storage.h"
20 #include "util-storage.h"
21 #include "util-unittest.h"
22 
24 
25 unsigned int SCThreadStorageSize(void)
26 {
28 }
29 
31 {
32  return SCStorageGetById(tv->storage, storage_type, id.id);
33 }
34 
36 {
37  return SCStorageSetById(tv->storage, storage_type, id.id, ptr);
38 }
39 
41 {
43 }
44 
46 {
47  if (SCThreadStorageSize() > 0)
49 }
50 
51 SCThreadStorageId SCThreadStorageRegister(const char *name, void (*Free)(void *))
52 {
53  int id = SCStorageRegister(storage_type, name, Free);
54  SCThreadStorageId tsi = { .id = id };
55  return tsi;
56 }
57 
58 #ifdef UNITTESTS
59 
60 static void StorageTestFree(void *x)
61 {
62  SCFree(x);
63 }
64 
65 static int ThreadStorageTest01(void)
66 {
68  SCStorageInit();
69 
70  SCThreadStorageId id1 = SCThreadStorageRegister("test", StorageTestFree);
71  FAIL_IF(id1.id < 0);
72 
73  SCThreadStorageId id2 = SCThreadStorageRegister("variable", StorageTestFree);
74  FAIL_IF(id2.id < 0);
75 
76  SCThreadStorageId id3 = SCThreadStorageRegister("store", StorageTestFree);
77  FAIL_IF(id3.id < 0);
78 
80 
83 
84  void *ptr = SCThreadGetStorageById(tv, id1);
85  FAIL_IF_NOT_NULL(ptr);
86 
87  ptr = SCThreadGetStorageById(tv, id2);
88  FAIL_IF_NOT_NULL(ptr);
89 
90  ptr = SCThreadGetStorageById(tv, id3);
91  FAIL_IF_NOT_NULL(ptr);
92 
93  void *ptr1a = SCMalloc(8);
94  FAIL_IF_NULL(ptr1a);
95  FAIL_IF(SCThreadSetStorageById(tv, id1, ptr1a) != 0);
96 
97  void *ptr2a = SCMalloc(24);
98  FAIL_IF_NULL(ptr2a);
99  FAIL_IF(SCThreadSetStorageById(tv, id2, ptr2a) != 0);
100 
101  void *ptr3a = SCMalloc(16);
102  FAIL_IF_NULL(ptr3a);
103  FAIL_IF(SCThreadSetStorageById(tv, id3, ptr3a) != 0);
104 
105  void *ptr1b = SCThreadGetStorageById(tv, id1);
106  FAIL_IF(ptr1a != ptr1b);
107 
108  void *ptr2b = SCThreadGetStorageById(tv, id2);
109  FAIL_IF(ptr2a != ptr2b);
110 
111  void *ptr3b = SCThreadGetStorageById(tv, id3);
112  FAIL_IF(ptr3a != ptr3b);
113 
116  SCFree(tv);
117  PASS;
118 }
119 
120 static int ThreadStorageTest02(void)
121 {
123  SCStorageInit();
124 
125  SCThreadStorageId id1 = SCThreadStorageRegister("test", StorageTestFree);
126  FAIL_IF(id1.id < 0);
127 
128  FAIL_IF(SCStorageFinalize() < 0);
129 
131  FAIL_IF_NULL(tv);
132 
133  void *ptr = SCThreadGetStorageById(tv, id1);
134  FAIL_IF_NOT_NULL(ptr);
135 
136  void *ptr1a = SCMalloc(128);
137  FAIL_IF_NULL(ptr1a);
138 
139  SCThreadSetStorageById(tv, id1, ptr1a);
140 
141  void *ptr1b = SCThreadGetStorageById(tv, id1);
142  FAIL_IF(ptr1a != ptr1b);
143 
146  SCFree(tv);
147  PASS;
148 }
149 
150 static int ThreadStorageTest03(void)
151 {
153  SCStorageInit();
154 
155  SCThreadStorageId id1 = SCThreadStorageRegister("test1", StorageTestFree);
156  FAIL_IF(id1.id < 0);
157 
158  SCThreadStorageId id2 = SCThreadStorageRegister("test2", StorageTestFree);
159  FAIL_IF(id2.id < 0);
160 
161  SCThreadStorageId id3 = SCThreadStorageRegister("test3", StorageTestFree);
162  FAIL_IF(id3.id < 0);
163 
164  FAIL_IF(SCStorageFinalize() < 0);
165 
167  FAIL_IF_NULL(tv);
168 
169  void *ptr = SCThreadGetStorageById(tv, id1);
170  FAIL_IF_NOT_NULL(ptr);
171 
172  void *ptr1a = SCMalloc(128);
173  FAIL_IF_NULL(ptr1a);
174 
175  SCThreadSetStorageById(tv, id1, ptr1a);
176 
177  void *ptr2a = SCMalloc(256);
178  FAIL_IF_NULL(ptr2a);
179 
180  SCThreadSetStorageById(tv, id2, ptr2a);
181 
182  void *ptr3a = SCMalloc(32);
183  FAIL_IF_NULL(ptr3a);
184  SCThreadSetStorageById(tv, id3, ptr3a);
185 
186  void *ptr1b = SCThreadGetStorageById(tv, id1);
187  FAIL_IF(ptr1a != ptr1b);
188 
189  void *ptr2b = SCThreadGetStorageById(tv, id2);
190  FAIL_IF(ptr2a != ptr2b);
191 
192  void *ptr3b = SCThreadGetStorageById(tv, id3);
193  FAIL_IF(ptr3a != ptr3b);
194 
197  SCFree(tv);
198  PASS;
199 }
200 #endif
201 
203 {
204 #ifdef UNITTESTS
205  UtRegisterTest("ThreadStorageTest01", ThreadStorageTest01);
206  UtRegisterTest("ThreadStorageTest02", ThreadStorageTest02);
207  UtRegisterTest("ThreadStorageTest03", ThreadStorageTest03);
208 #endif
209 }
SCThreadFreeStorageById
void SCThreadFreeStorageById(ThreadVars *tv, SCThreadStorageId id)
Definition: thread-storage.c:40
ThreadVars_::storage
Storage storage[]
Definition: threadvars.h:137
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SCStorageInit
void SCStorageInit(void)
Definition: util-storage.c:68
storage_type
const StorageEnum storage_type
Definition: thread-storage.c:23
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
name
const char * name
Definition: detect-engine-proto.c:48
SCThreadStorageRegister
SCThreadStorageId SCThreadStorageRegister(const char *name, void(*Free)(void *))
Definition: thread-storage.c:51
SCStorageFreeById
void SCStorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:230
SCThreadSetStorageById
int SCThreadSetStorageById(ThreadVars *tv, SCThreadStorageId id, void *ptr)
Definition: thread-storage.c:35
SCStorageSetById
int SCStorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:218
StorageEnum
enum StorageEnum_ StorageEnum
util-unittest.h
SCThreadStorageId::id
int id
Definition: thread-storage.h:28
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
STORAGE_THREAD
@ STORAGE_THREAD
Definition: util-storage.h:34
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
SCThreadStorageId
Definition: thread-storage.h:27
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCStorageGetById
void * SCStorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:207
SCStorageRegister
int SCStorageRegister(const StorageEnum type, const char *name, void(*Free)(void *))
Register new storage.
Definition: util-storage.c:100
SCThreadStorageSize
unsigned int SCThreadStorageSize(void)
Definition: thread-storage.c:25
thread-storage.h
SCStorageGetSize
unsigned int SCStorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:202
SCThreadGetStorageById
void * SCThreadGetStorageById(const ThreadVars *tv, SCThreadStorageId id)
Definition: thread-storage.c:30
SCRegisterThreadStorageTests
void SCRegisterThreadStorageTests(void)
Definition: thread-storage.c:202
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SCStorageFreeAll
void SCStorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:252
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCStorageCleanup
void SCStorageCleanup(void)
Definition: util-storage.c:76
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCThreadFreeStorage
void SCThreadFreeStorage(ThreadVars *tv)
Definition: thread-storage.c:45
util-storage.h
SCStorageFinalize
int SCStorageFinalize(void)
Definition: util-storage.c:135