suricata
threads.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
23  *
24  * This file now only contains unit tests see macros in threads.h
25  */
26 
27 #include "suricata-common.h"
28 #include "thread-storage.h"
29 #include "util-debug.h"
30 #include "threads.h"
31 
32 thread_local char t_thread_name[THREAD_NAME_LEN + 1];
33 #ifdef UNITTESTS /* UNIT TESTS */
34 
35 /**
36  * \brief Test Mutex macros
37  */
38 static int ThreadMacrosTest01Mutex(void)
39 {
40  SCMutex mut;
41  FAIL_IF(SCMutexInit(&mut, NULL) != 0);
42  FAIL_IF(SCMutexLock(&mut) != 0);
43  FAIL_IF(SCMutexTrylock(&mut) != EBUSY);
44  FAIL_IF(SCMutexIsLocked(&mut) == 0);
45  FAIL_IF(SCMutexUnlock(&mut) != 0);
46  FAIL_IF(SCMutexDestroy(&mut) != 0);
47 
48  PASS;
49 }
50 
51 /**
52  * \brief Test Spinlock Macros
53  *
54  * Valgrind's DRD tool (valgrind-3.5.0-Debian) reports:
55  *
56  * ==31156== Recursive locking not allowed: mutex 0x7fefff97c, recursion count 1, owner 1.
57  * ==31156== at 0x4C2C77E: pthread_spin_trylock (drd_pthread_intercepts.c:829)
58  * ==31156== by 0x40EB3E: ThreadMacrosTest02Spinlocks (threads.c:40)
59  * ==31156== by 0x532E8A: UtRunTests (util-unittest.c:182)
60  * ==31156== by 0x4065C3: main (suricata.c:789)
61  *
62  * To me this is a false positive, as the whole point of "trylock" is to see
63  * if a spinlock is actually locked.
64  *
65  */
66 static int ThreadMacrosTest02Spinlocks(void)
67 {
68  SCSpinlock mut;
69  FAIL_IF(SCSpinInit(&mut, 0) != 0);
70  FAIL_IF(SCSpinLock(&mut) != 0);
71 #ifndef __OpenBSD__
72  FAIL_IF(SCSpinTrylock(&mut) != EBUSY);
73 #else
74  FAIL_IF(SCSpinTrylock(&mut) != EDEADLK);
75 #endif
76  FAIL_IF(SCSpinUnlock(&mut) != 0);
77  FAIL_IF(SCSpinDestroy(&mut) != 0);
78 
79  PASS;
80 }
81 
82 /**
83  * \brief Test RWLock macros
84  */
85 static int ThreadMacrosTest03RWLocks(void)
86 {
87  SCRWLock rwl_write;
88  FAIL_IF(SCRWLockInit(&rwl_write, NULL) != 0);
89  FAIL_IF(SCRWLockWRLock(&rwl_write) != 0);
90 /* OS X/macOS 10.10 (Yosemite) and newer return EDEADLK. Older versions
91  * and other tested OS's return EBUSY. */
92 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__>=101000
93  FAIL_IF(SCRWLockTryWRLock(&rwl_write) != EDEADLK);
94 #else
95  FAIL_IF(SCRWLockTryWRLock(&rwl_write) != EBUSY);
96 #endif
97  FAIL_IF(SCRWLockUnlock(&rwl_write) != 0);
98  FAIL_IF(SCRWLockDestroy(&rwl_write) != 0);
99 
100  PASS;
101 }
102 
103 /**
104  * \brief Test RWLock macros
105  */
106 static int ThreadMacrosTest04RWLocks(void)
107 {
108  SCRWLock rwl_read;
109  FAIL_IF(SCRWLockInit(&rwl_read, NULL) != 0);
110  FAIL_IF(SCRWLockRDLock(&rwl_read) != 0);
111  FAIL_IF(SCRWLockTryWRLock(&rwl_read) != EBUSY);
112  FAIL_IF(SCRWLockUnlock(&rwl_read) != 0);
113  FAIL_IF(SCRWLockDestroy(&rwl_read) != 0);
114 
115  PASS;
116 }
117 
118 #if 0 // broken on OSX
119 /**
120  * \brief Test RWLock macros
121  */
122 static int ThreadMacrosTest05RWLocks(void)
123 {
124  SCRWLock rwl_read;
125  int r = 0;
126  r |= SCRWLockInit(&rwl_read, NULL);
127  r |= SCRWLockWRLock(&rwl_read);
128  r |= (SCRWLockTryRDLock(&rwl_read) == EBUSY)? 0 : 1;
129  r |= SCRWLockUnlock(&rwl_read);
130  r |= SCRWLockDestroy(&rwl_read);
131 
132  return (r == 0)? 1 : 0;
133 }
134 #endif
135 
136 #endif /* UNIT TESTS */
137 
138 /**
139  * \brief this function registers unit tests for DetectId
140  */
142 {
143 #ifdef UNITTESTS /* UNIT TESTS */
144  UtRegisterTest("ThreadMacrosTest01Mutex", ThreadMacrosTest01Mutex);
145  UtRegisterTest("ThreadMacrosTest02Spinlocks", ThreadMacrosTest02Spinlocks);
146  UtRegisterTest("ThreadMacrosTest03RWLocks", ThreadMacrosTest03RWLocks);
147  UtRegisterTest("ThreadMacrosTest04RWLocks", ThreadMacrosTest04RWLocks);
148 // UtRegisterTest("ThreadMacrosTest05RWLocks", ThreadMacrosTest05RWLocks);
150 #endif /* UNIT TESTS */
151 }
SCSpinDestroy
#define SCSpinDestroy
Definition: threads-debug.h:240
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SCMutexIsLocked
#define SCMutexIsLocked(mut)
Definition: threads-debug.h:119
threads.h
SCSpinTrylock
#define SCSpinTrylock
Definition: threads-debug.h:237
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SCSpinLock
#define SCSpinLock
Definition: threads-debug.h:236
SCRWLockTryWRLock
#define SCRWLockTryWRLock(rwl)
Definition: threads-debug.h:368
SCRWLockUnlock
#define SCRWLockUnlock(rwl)
Definition: threads-debug.h:370
t_thread_name
thread_local char t_thread_name[THREAD_NAME_LEN+1]
Definition: threads.c:32
SCRWLockDestroy
#define SCRWLockDestroy
Definition: threads-debug.h:371
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
THREAD_NAME_LEN
#define THREAD_NAME_LEN
Definition: threads.h:33
SCSpinUnlock
#define SCSpinUnlock
Definition: threads-debug.h:238
thread-storage.h
SCRWLockInit
#define SCRWLockInit(rwl, rwlattrs)
Definition: threads-debug.h:365
SCRWLockWRLock
#define SCRWLockWRLock(rwl)
Definition: threads-debug.h:367
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
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
SCSpinInit
#define SCSpinInit
Definition: threads-debug.h:239
SCRWLockTryRDLock
#define SCRWLockTryRDLock(rwl)
Definition: threads-debug.h:369
SCRWLock
#define SCRWLock
Definition: threads-debug.h:364
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:235
ThreadMacrosRegisterTests
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:141
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:121
SCMutex
#define SCMutex
Definition: threads-debug.h:114
SCRWLockRDLock
#define SCRWLockRDLock(rwl)
Definition: threads-debug.h:366
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118