]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
debug: packet pool init/destroy validation
authorVictor Julien <victor@inliniac.net>
Wed, 22 Apr 2015 10:41:57 +0000 (12:41 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 23 Jul 2015 17:36:16 +0000 (19:36 +0200)
Validate packet pool handling:
- pools are initialized before use
- pools are not used after destroy
- pools are not double initialized/destroyed

src/tmqh-packetpool.c
src/tmqh-packetpool.h

index 4c4bf40691afa011cf8a7d86d1c143151b08c3dc..a1f19dca467c130916655123103bdc7e20c79d1a 100644 (file)
@@ -241,7 +241,10 @@ static void PacketPoolGetReturnedPackets(PktPool *pool)
 Packet *PacketPoolGetPacket(void)
 {
     PktPool *pool = GetThreadPacketPool();
-
+#ifdef DEBUG_VALIDATION
+    BUG_ON(pool->initialized == 0);
+    BUG_ON(pool->destroyed == 1);
+#endif /* DEBUG_VALIDATION */
     if (pool->head) {
         /* Stack is not empty. */
         Packet *p = pool->head;
@@ -286,6 +289,12 @@ void PacketPoolReturnPacket(Packet *p)
         PacketFree(p);
         return;
     }
+#ifdef DEBUG_VALIDATION
+    BUG_ON(pool->initialized == 0);
+    BUG_ON(pool->destroyed == 1);
+    BUG_ON(my_pool->initialized == 0);
+    BUG_ON(my_pool->destroyed == 1);
+#endif /* DEBUG_VALIDATION */
 
     if (pool == my_pool) {
         /* Push back onto this thread's own stack, so no locking. */
@@ -338,6 +347,12 @@ void PacketPoolInitEmpty(void)
 
     PktPool *my_pool = GetThreadPacketPool();
 
+#ifdef DEBUG_VALIDATION
+    BUG_ON(my_pool->initialized);
+    my_pool->initialized = 1;
+    my_pool->destroyed = 0;
+#endif /* DEBUG_VALIDATION */
+
     SCMutexInit(&my_pool->return_stack.mutex, NULL);
     SCCondInit(&my_pool->return_stack.cond, NULL);
     SC_ATOMIC_INIT(my_pool->return_stack.sync_now);
@@ -353,6 +368,12 @@ void PacketPoolInit(void)
 
     PktPool *my_pool = GetThreadPacketPool();
 
+#ifdef DEBUG_VALIDATION
+    BUG_ON(my_pool->initialized);
+    my_pool->initialized = 1;
+    my_pool->destroyed = 0;
+#endif /* DEBUG_VALIDATION */
+
     SCMutexInit(&my_pool->return_stack.mutex, NULL);
     SCCondInit(&my_pool->return_stack.cond, NULL);
     SC_ATOMIC_INIT(my_pool->return_stack.sync_now);
@@ -371,12 +392,18 @@ void PacketPoolInit(void)
     }
     SCLogInfo("preallocated %"PRIiMAX" packets. Total memory %"PRIuMAX"",
             max_pending_packets, (uintmax_t)(max_pending_packets*SIZE_OF_PACKET));
+
 }
 
 void PacketPoolDestroy(void)
 {
     Packet *p = NULL;
     PktPool *my_pool = GetThreadPacketPool();
+
+#ifdef DEBUG_VALIDATION
+    BUG_ON(my_pool->destroyed);
+#endif /* DEBUG_VALIDATION */
+
     if (my_pool && my_pool->pending_pool != NULL) {
         p = my_pool->pending_head;
         while (p) {
@@ -385,7 +412,9 @@ void PacketPoolDestroy(void)
             p = next_p;
             my_pool->pending_count--;
         }
+#ifdef DEBUG_VALIDATION
         BUG_ON(my_pool->pending_count);
+#endif /* DEBUG_VALIDATION */
         my_pool->pending_pool = NULL;
         my_pool->pending_head = NULL;
         my_pool->pending_tail = NULL;
@@ -396,6 +425,11 @@ void PacketPoolDestroy(void)
     }
 
     SC_ATOMIC_DESTROY(my_pool->return_stack.sync_now);
+
+#ifdef DEBUG_VALIDATION
+    my_pool->initialized = 0;
+    my_pool->destroyed = 1;
+#endif /* DEBUG_VALIDATION */
 }
 
 Packet *TmqhInputPacketpool(ThreadVars *tv)
index 12a3f5cabdd48336a5a47fdc5a0caf83a359b22b..ab45184c654f9e50167828b6912b7d1023853a6d 100644 (file)
@@ -38,7 +38,7 @@ typedef struct PktPoolLockedStack_{
 } __attribute__((aligned(CLS))) PktPoolLockedStack;
 
 typedef struct PktPool_ {
-    /* link listed of free packets local to this thread. 
+    /* link listed of free packets local to this thread.
      * No mutex is needed.
      */
     Packet *head;
@@ -51,8 +51,13 @@ typedef struct PktPool_ {
     Packet *pending_head;
     Packet *pending_tail;
     uint32_t pending_count;
-    
-    /* All members above this point are accessed locally by only one thread, so 
+
+#ifdef DEBUG_VALIDATION
+    int initialized;
+    int destroyed;
+#endif /* DEBUG_VALIDATION */
+
+    /* All members above this point are accessed locally by only one thread, so
      * these should live on their own cache line.
      */
 
@@ -60,7 +65,6 @@ typedef struct PktPool_ {
      * to this thread.
      */
     PktPoolLockedStack return_stack;
-
 } PktPool;
 
 Packet *TmqhInputPacketpool(ThreadVars *);