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