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 ThreadStorageSize(void)
26 {
28 }
29 
31 {
32  return StorageGetById(tv->storage, storage_type, id.id);
33 }
34 
36 {
37  return StorageSetById(tv->storage, storage_type, id.id, ptr);
38 }
39 
41 {
43 }
44 
46 {
48 }
49 
51 {
52  if (ThreadStorageSize() > 0)
54 }
55 
56 ThreadStorageId ThreadStorageRegister(const char *name, const unsigned int size,
57  void *(*Alloc)(unsigned int), void (*Free)(void *))
58 {
59  int id = StorageRegister(storage_type, name, size, Alloc, Free);
60  ThreadStorageId tsi = { .id = id };
61  return tsi;
62 }
63 
64 #ifdef UNITTESTS
65 
66 static void *StorageTestAlloc(unsigned int size)
67 {
68  return SCCalloc(1, size);
69 }
70 
71 static void StorageTestFree(void *x)
72 {
73  SCFree(x);
74 }
75 
76 static int ThreadStorageTest01(void)
77 {
79  StorageInit();
80 
81  ThreadStorageId id1 = ThreadStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
82  FAIL_IF(id1.id < 0);
83 
84  ThreadStorageId id2 = ThreadStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
85  FAIL_IF(id2.id < 0);
86 
87  ThreadStorageId id3 =
88  ThreadStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
89  FAIL_IF(id3.id < 0);
90 
91  FAIL_IF(StorageFinalize() < 0);
92 
95 
96  void *ptr = ThreadGetStorageById(tv, id1);
97  FAIL_IF_NOT_NULL(ptr);
98 
99  ptr = ThreadGetStorageById(tv, id2);
100  FAIL_IF_NOT_NULL(ptr);
101 
102  ptr = ThreadGetStorageById(tv, id3);
103  FAIL_IF_NOT_NULL(ptr);
104 
105  void *ptr1a = ThreadAllocStorageById(tv, id1);
106  FAIL_IF_NULL(ptr1a);
107 
108  void *ptr2a = ThreadAllocStorageById(tv, id2);
109  FAIL_IF_NULL(ptr2a);
110 
111  void *ptr3a = ThreadAllocStorageById(tv, id3);
112  FAIL_IF_NULL(ptr3a);
113 
114  void *ptr1b = ThreadGetStorageById(tv, id1);
115  FAIL_IF(ptr1a != ptr1b);
116 
117  void *ptr2b = ThreadGetStorageById(tv, id2);
118  FAIL_IF(ptr2a != ptr2b);
119 
120  void *ptr3b = ThreadGetStorageById(tv, id3);
121  FAIL_IF(ptr3a != ptr3b);
122 
124  StorageCleanup();
125  SCFree(tv);
126  PASS;
127 }
128 
129 static int ThreadStorageTest02(void)
130 {
131  StorageCleanup();
132  StorageInit();
133 
134  ThreadStorageId id1 = ThreadStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
135  FAIL_IF(id1.id < 0);
136 
137  FAIL_IF(StorageFinalize() < 0);
138 
140  FAIL_IF_NULL(tv);
141 
142  void *ptr = ThreadGetStorageById(tv, id1);
143  FAIL_IF_NOT_NULL(ptr);
144 
145  void *ptr1a = SCMalloc(128);
146  FAIL_IF_NULL(ptr1a);
147 
148  ThreadSetStorageById(tv, id1, ptr1a);
149 
150  void *ptr1b = ThreadGetStorageById(tv, id1);
151  FAIL_IF(ptr1a != ptr1b);
152 
154  StorageCleanup();
155  SCFree(tv);
156  PASS;
157 }
158 
159 static int ThreadStorageTest03(void)
160 {
161  StorageCleanup();
162  StorageInit();
163 
164  ThreadStorageId id1 = ThreadStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
165  FAIL_IF(id1.id < 0);
166 
167  ThreadStorageId id2 = ThreadStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
168  FAIL_IF(id2.id < 0);
169 
170  ThreadStorageId id3 = ThreadStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
171  FAIL_IF(id3.id < 0);
172 
173  FAIL_IF(StorageFinalize() < 0);
174 
176  FAIL_IF_NULL(tv);
177 
178  void *ptr = ThreadGetStorageById(tv, id1);
179  FAIL_IF_NOT_NULL(ptr);
180 
181  void *ptr1a = SCMalloc(128);
182  FAIL_IF_NULL(ptr1a);
183 
184  ThreadSetStorageById(tv, id1, ptr1a);
185 
186  void *ptr2a = SCMalloc(256);
187  FAIL_IF_NULL(ptr2a);
188 
189  ThreadSetStorageById(tv, id2, ptr2a);
190 
191  void *ptr3a = ThreadAllocStorageById(tv, id3);
192  FAIL_IF_NULL(ptr3a);
193 
194  void *ptr1b = ThreadGetStorageById(tv, id1);
195  FAIL_IF(ptr1a != ptr1b);
196 
197  void *ptr2b = ThreadGetStorageById(tv, id2);
198  FAIL_IF(ptr2a != ptr2b);
199 
200  void *ptr3b = ThreadGetStorageById(tv, id3);
201  FAIL_IF(ptr3a != ptr3b);
202 
204  StorageCleanup();
205  SCFree(tv);
206  PASS;
207 }
208 #endif
209 
211 {
212 #ifdef UNITTESTS
213  UtRegisterTest("ThreadStorageTest01", ThreadStorageTest01);
214  UtRegisterTest("ThreadStorageTest02", ThreadStorageTest02);
215  UtRegisterTest("ThreadStorageTest03", ThreadStorageTest03);
216 #endif
217 }
StorageFreeAll
void StorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:278
ThreadVars_::storage
Storage storage[]
Definition: threadvars.h:141
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
storage_type
const StorageEnum storage_type
Definition: thread-storage.c:23
StorageInit
void StorageInit(void)
Definition: util-storage.c:70
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
StorageCleanup
void StorageCleanup(void)
Definition: util-storage.c:78
ThreadAllocStorageById
void * ThreadAllocStorageById(ThreadVars *tv, ThreadStorageId id)
Definition: thread-storage.c:40
StorageEnum
enum StorageEnum_ StorageEnum
util-unittest.h
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
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
thread-storage.h
ThreadStorageId
Definition: thread-storage.h:27
StorageFinalize
int StorageFinalize(void)
Definition: util-storage.c:140
name
const char * name
Definition: tm-threads.c:2163
ThreadStorageRegister
ThreadStorageId ThreadStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Definition: thread-storage.c:56
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
ThreadFreeStorageById
void ThreadFreeStorageById(ThreadVars *tv, ThreadStorageId id)
Definition: thread-storage.c:45
StorageSetById
int StorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:226
StorageAllocByIdPrealloc
void * StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id)
AllocById func for prealloc'd base storage (storage ptrs are part of another memory block)
Definition: util-storage.c:238
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
ThreadStorageSize
unsigned int ThreadStorageSize(void)
Definition: thread-storage.c:25
StorageFreeById
void StorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:256
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ThreadStorageId::id
int id
Definition: thread-storage.h:28
ThreadGetStorageById
void * ThreadGetStorageById(const ThreadVars *tv, ThreadStorageId id)
Definition: thread-storage.c:30
RegisterThreadStorageTests
void RegisterThreadStorageTests(void)
Definition: thread-storage.c:210
ThreadSetStorageById
int ThreadSetStorageById(ThreadVars *tv, ThreadStorageId id, void *ptr)
Definition: thread-storage.c:35
ThreadFreeStorage
void ThreadFreeStorage(ThreadVars *tv)
Definition: thread-storage.c:50
StorageGetById
void * StorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:215
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
StorageGetSize
unsigned int StorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:210
util-storage.h
StorageRegister
int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Register new storage.
Definition: util-storage.c:102