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 
23 
24 unsigned int SCThreadStorageSize(void)
25 {
27 }
28 
30 {
31  return SCStorageGetById(tv->storage, storage_type, id.id);
32 }
33 
35 {
36  return SCStorageSetById(tv->storage, storage_type, id.id, ptr);
37 }
38 
40 {
42 }
43 
45 {
46  if (SCThreadStorageSize() > 0)
48 }
49 
50 SCThreadStorageId SCThreadStorageRegister(const char *name, void (*Free)(void *))
51 {
52  int id = SCStorageRegister(storage_type, name, Free);
53  SCThreadStorageId tsi = { .id = id };
54  return tsi;
55 }
56 
57 #ifdef UNITTESTS
58 
59 static void StorageTestFree(void *x)
60 {
61  SCFree(x);
62 }
63 
64 static int ThreadStorageTest01(void)
65 {
67  SCStorageInit();
68 
69  SCThreadStorageId id1 = SCThreadStorageRegister("test", StorageTestFree);
70  FAIL_IF(id1.id < 0);
71 
72  SCThreadStorageId id2 = SCThreadStorageRegister("variable", StorageTestFree);
73  FAIL_IF(id2.id < 0);
74 
75  SCThreadStorageId id3 = SCThreadStorageRegister("store", StorageTestFree);
76  FAIL_IF(id3.id < 0);
77 
79 
82 
83  void *ptr = SCThreadGetStorageById(tv, id1);
84  FAIL_IF_NOT_NULL(ptr);
85 
86  ptr = SCThreadGetStorageById(tv, id2);
87  FAIL_IF_NOT_NULL(ptr);
88 
89  ptr = SCThreadGetStorageById(tv, id3);
90  FAIL_IF_NOT_NULL(ptr);
91 
92  void *ptr1a = SCMalloc(8);
93  FAIL_IF_NULL(ptr1a);
94  FAIL_IF(SCThreadSetStorageById(tv, id1, ptr1a) != 0);
95 
96  void *ptr2a = SCMalloc(24);
97  FAIL_IF_NULL(ptr2a);
98  FAIL_IF(SCThreadSetStorageById(tv, id2, ptr2a) != 0);
99 
100  void *ptr3a = SCMalloc(16);
101  FAIL_IF_NULL(ptr3a);
102  FAIL_IF(SCThreadSetStorageById(tv, id3, ptr3a) != 0);
103 
104  void *ptr1b = SCThreadGetStorageById(tv, id1);
105  FAIL_IF(ptr1a != ptr1b);
106 
107  void *ptr2b = SCThreadGetStorageById(tv, id2);
108  FAIL_IF(ptr2a != ptr2b);
109 
110  void *ptr3b = SCThreadGetStorageById(tv, id3);
111  FAIL_IF(ptr3a != ptr3b);
112 
115  SCFree(tv);
116  PASS;
117 }
118 
119 static int ThreadStorageTest02(void)
120 {
122  SCStorageInit();
123 
124  SCThreadStorageId id1 = SCThreadStorageRegister("test", StorageTestFree);
125  FAIL_IF(id1.id < 0);
126 
127  FAIL_IF(SCStorageFinalize() < 0);
128 
130  FAIL_IF_NULL(tv);
131 
132  void *ptr = SCThreadGetStorageById(tv, id1);
133  FAIL_IF_NOT_NULL(ptr);
134 
135  void *ptr1a = SCMalloc(128);
136  FAIL_IF_NULL(ptr1a);
137 
138  SCThreadSetStorageById(tv, id1, ptr1a);
139 
140  void *ptr1b = SCThreadGetStorageById(tv, id1);
141  FAIL_IF(ptr1a != ptr1b);
142 
145  SCFree(tv);
146  PASS;
147 }
148 
149 static int ThreadStorageTest03(void)
150 {
152  SCStorageInit();
153 
154  SCThreadStorageId id1 = SCThreadStorageRegister("test1", StorageTestFree);
155  FAIL_IF(id1.id < 0);
156 
157  SCThreadStorageId id2 = SCThreadStorageRegister("test2", StorageTestFree);
158  FAIL_IF(id2.id < 0);
159 
160  SCThreadStorageId id3 = SCThreadStorageRegister("test3", StorageTestFree);
161  FAIL_IF(id3.id < 0);
162 
163  FAIL_IF(SCStorageFinalize() < 0);
164 
166  FAIL_IF_NULL(tv);
167 
168  void *ptr = SCThreadGetStorageById(tv, id1);
169  FAIL_IF_NOT_NULL(ptr);
170 
171  void *ptr1a = SCMalloc(128);
172  FAIL_IF_NULL(ptr1a);
173 
174  SCThreadSetStorageById(tv, id1, ptr1a);
175 
176  void *ptr2a = SCMalloc(256);
177  FAIL_IF_NULL(ptr2a);
178 
179  SCThreadSetStorageById(tv, id2, ptr2a);
180 
181  void *ptr3a = SCMalloc(32);
182  FAIL_IF_NULL(ptr3a);
183  SCThreadSetStorageById(tv, id3, ptr3a);
184 
185  void *ptr1b = SCThreadGetStorageById(tv, id1);
186  FAIL_IF(ptr1a != ptr1b);
187 
188  void *ptr2b = SCThreadGetStorageById(tv, id2);
189  FAIL_IF(ptr2a != ptr2b);
190 
191  void *ptr3b = SCThreadGetStorageById(tv, id3);
192  FAIL_IF(ptr3a != ptr3b);
193 
196  SCFree(tv);
197  PASS;
198 }
199 #endif
200 
202 {
203 #ifdef UNITTESTS
204  UtRegisterTest("ThreadStorageTest01", ThreadStorageTest01);
205  UtRegisterTest("ThreadStorageTest02", ThreadStorageTest02);
206  UtRegisterTest("ThreadStorageTest03", ThreadStorageTest03);
207 #endif
208 }
SCThreadFreeStorageById
void SCThreadFreeStorageById(ThreadVars *tv, SCThreadStorageId id)
Definition: thread-storage.c:39
ThreadVars_::storage
Storage storage[]
Definition: threadvars.h:136
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:67
storage_type
const StorageEnum storage_type
Definition: thread-storage.c:22
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
name
const char * name
Definition: detect-engine-proto.c:47
SCThreadStorageRegister
SCThreadStorageId SCThreadStorageRegister(const char *name, void(*Free)(void *))
Definition: thread-storage.c:50
SCStorageFreeById
void SCStorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:229
SCThreadSetStorageById
int SCThreadSetStorageById(ThreadVars *tv, SCThreadStorageId id, void *ptr)
Definition: thread-storage.c:34
SCStorageSetById
int SCStorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:217
StorageEnum
enum StorageEnum_ StorageEnum
SCThreadStorageId::id
int id
Definition: thread-storage.h:30
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:29
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
SCStorageGetById
void * SCStorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:206
SCStorageRegister
int SCStorageRegister(const StorageEnum type, const char *name, void(*Free)(void *))
Register new storage.
Definition: util-storage.c:99
SCThreadStorageSize
unsigned int SCThreadStorageSize(void)
Definition: thread-storage.c:24
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:201
SCThreadGetStorageById
void * SCThreadGetStorageById(const ThreadVars *tv, SCThreadStorageId id)
Definition: thread-storage.c:29
SCRegisterThreadStorageTests
void SCRegisterThreadStorageTests(void)
Definition: thread-storage.c:201
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:251
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
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:75
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCThreadFreeStorage
void SCThreadFreeStorage(ThreadVars *tv)
Definition: thread-storage.c:44
util-storage.h
SCStorageFinalize
int SCStorageFinalize(void)
Definition: util-storage.c:134