This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 209d2857f1526c06424716e2edd88ca65efc2ffb
Author: huojianchao <[email protected]>
AuthorDate: Mon Nov 30 11:25:06 2026 +0800

    testing/mm/iob: make the extended tail chain test catch the bug
    
    With the bug that 69c67a58493 fixed, the entry count and the size of
    the queue still look right after chain2 is linked: chain1's head keeps
    IOB_CHAIN_END and the io_pktlen of the whole chain.  Only the IOB count
    of the dequeued chain1 showed the bug, while the comment said the two
    queue helpers would.
    
    Correct the comment, and also check the data of the dequeued chain1 and
    that every IOB returns to the pool, since the lost IOB is never freed.
    69c67a58493 fixed iob_concat_queue() as well, so run the same sequence
    with iob_concat_queue() of a second queue.
    
    Assisted-by: Kiro:claude-opus-5-5
    Signed-off-by: huojianchao <[email protected]>
---
 testing/mm/iob/iob_main.c | 151 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 108 insertions(+), 43 deletions(-)

diff --git a/testing/mm/iob/iob_main.c b/testing/mm/iob/iob_main.c
index d125df6a4..267d3e9bc 100644
--- a/testing/mm/iob/iob_main.c
+++ b/testing/mm/iob/iob_main.c
@@ -30,6 +30,7 @@
 #include <pthread.h>
 #include <setjmp.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -1188,38 +1189,19 @@ static int count_chain_iobs(FAR struct iob_s *chain)
 }
 
 /****************************************************************************
- * Name: test_iob_queue_extend_tail
+ * Name: extend_tail_case
  *
  * Description:
- *   Regression test for 69c67a58493 ("mm/iob: fix stale qh_tail corrupting
- *   enqueued multi-IOB chains").
- *
- *   iob_add_queue()/iob_concat_queue() used to link a newly enqueued chain
- *   directly onto iobq->qh_tail.  But a chain may be extended (e.g. via
- *   iob_copyin(), as the UDP/TCP read-ahead buffering path does) AFTER it
- *   has been enqueued: iob_copyin() carries the IOB_CHAIN_END flag onto the
- *   freshly allocated last IOB while qh_tail still points at the original,
- *   now mid-chain, IOB.  Linking the next chain onto that stale tail
- *   overwrote the extended chain's internal io_flink, truncating it and
- *   planting a false chain boundary.
- *
- *   Reproduction sequence:
- *     1. Enqueue a small single-IOB chain (chain1).  qh_tail -> chain1.
- *     2. iob_copyin() enough data into chain1 to force allocation of a
- *        second IOB.  IOB_CHAIN_END moves to the new tail, but qh_tail is
- *        unchanged (still the original IOB).
- *     3. Enqueue a second chain (chain2).
- *
- *   With the bug, step 3 overwrites chain1's internal link: chain1 is
- *   truncated to a single IOB and the queue byte/entry accounting is
- *   corrupted.  After the fix the queue holds exactly two chains with all
- *   bytes and both of chain1's IOBs intact.
+ *   One case of test_iob_queue_extend_tail(): enqueue chain1, extend it
+ *   past one IOB with iob_copyin(), then link chain2 behind it, either with
+ *   iob_add_queue() or by iob_concat_queue() of a second queue.
  *
  ****************************************************************************/
 
-static int test_iob_queue_extend_tail(void)
+static int extend_tail_case(bool concat)
 {
   struct iob_queue_s queue;
+  struct iob_queue_s queue2;
   FAR struct iob_s *chain1;
   FAR struct iob_s *chain2;
   FAR struct iob_s *removed;
@@ -1227,11 +1209,16 @@ static int test_iob_queue_extend_tail(void)
   unsigned int biglen;
   unsigned int chain1_pktlen;
   unsigned int chain2_pktlen;
+  unsigned int i;
+  int navail;
   int ret;
 
-  printf("\n=== Test 12: IOB Queue Extend Enqueued Tail Chain ===\n");
+  printf("Linking chain2 with %s\n",
+         concat ? "iob_concat_queue()" : "iob_add_queue()");
 
+  navail = iob_navail(false);
   IOB_QINIT(&queue);
+  IOB_QINIT(&queue2);
 
   /* chain1: a single IOB carrying a few bytes. */
 
@@ -1296,32 +1283,36 @@ static int test_iob_queue_extend_tail(void)
 
   /* The operation that used to corrupt the extended tail chain. */
 
-  iob_add_queue(chain2, &queue);
-
-  /* The queue must now report exactly two chains, and the total buffered
-   * bytes must equal the sum of both chains' packet lengths: nothing was
-   * lost to truncation.  Both helpers traverse the queue via IOB_CHAIN_END,
-   * so a corrupted boundary shows up here.
-   */
-
-  if (iob_get_queue_entry_count(&queue) != 2)
+  if (concat)
     {
-      printf("ERROR: expected 2 queue entries, got %u\n",
-             iob_get_queue_entry_count(&queue));
-      iob_free_queue(&queue);
-      return -1;
+      iob_add_queue(chain2, &queue2);
+      iob_concat_queue(&queue, &queue2);
+      ASSERT(IOB_QEMPTY(&queue2));
+    }
+  else
+    {
+      iob_add_queue(chain2, &queue);
     }
 
-  if (iob_get_queue_size(&queue) != chain1_pktlen + chain2_pktlen)
+  /* With the bug, linking chain2 overwrote the io_flink of chain1's head
+   * and lost chain1's second IOB.  The entry count and the queue size
+   * cannot show that: the head still carries IOB_CHAIN_END and the
+   * io_pktlen of the whole chain.  What shows it is the content of
+   * chain1 after the dequeue, and the lost IOB, which is never freed.
+   */
+
+  if (iob_get_queue_entry_count(&queue) != 2 ||
+      iob_get_queue_size(&queue) != chain1_pktlen + chain2_pktlen)
     {
-      printf("ERROR: queue size %u != expected %u (chain truncated)\n",
+      printf("ERROR: queue has %u entries and %u bytes, expected 2 and "
+             "%u\n", iob_get_queue_entry_count(&queue),
              iob_get_queue_size(&queue), chain1_pktlen + chain2_pktlen);
       iob_free_queue(&queue);
       return -1;
     }
 
   /* Dequeue the first chain and confirm it still contains all of its IOBs
-   * and bytes (i.e. it was not truncated at the stale tail).
+   * and bytes: "hello" followed by the 0xa5 pattern.
    */
 
   removed = iob_remove_queue(&queue);
@@ -1334,6 +1325,28 @@ static int test_iob_queue_extend_tail(void)
       return -1;
     }
 
+  memset(g_buffer2, 0, chain1_pktlen);
+  ret = iob_copyout(g_buffer2, removed, chain1_pktlen, 0);
+  if (ret != (int)chain1_pktlen || memcmp(g_buffer2, "hello", 5) != 0)
+    {
+      printf("ERROR: chain1 data lost, copied out %d of %u bytes\n",
+             ret, chain1_pktlen);
+      iob_free_chain(removed);
+      iob_free_queue(&queue);
+      return -1;
+    }
+
+  for (i = 5; i < chain1_pktlen; i++)
+    {
+      if (g_buffer2[i] != 0xa5)
+        {
+          printf("ERROR: chain1 data wrong at offset %u\n", i);
+          iob_free_chain(removed);
+          iob_free_queue(&queue);
+          return -1;
+        }
+    }
+
   iob_free_chain(removed);
 
   removed = iob_remove_queue(&queue);
@@ -1346,8 +1359,60 @@ static int test_iob_queue_extend_tail(void)
     }
 
   iob_free_chain(removed);
-
   ASSERT(IOB_QEMPTY(&queue));
+
+  /* Every IOB went back to the pool */
+
+  if (iob_navail(false) != navail)
+    {
+      printf("ERROR: %d IOBs available after the test, %d before\n",
+             iob_navail(false), navail);
+      return -1;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: test_iob_queue_extend_tail
+ *
+ * Description:
+ *   Regression test for 69c67a58493 ("mm/iob: fix stale qh_tail corrupting
+ *   enqueued multi-IOB chains").
+ *
+ *   iob_add_queue()/iob_concat_queue() used to link a newly enqueued chain
+ *   directly onto iobq->qh_tail.  But a chain may be extended (e.g. via
+ *   iob_copyin(), as the UDP/TCP read-ahead buffering path does) AFTER it
+ *   has been enqueued: iob_copyin() carries the IOB_CHAIN_END flag onto the
+ *   freshly allocated last IOB while qh_tail still points at the original,
+ *   now mid-chain, IOB.  Linking the next chain onto that stale tail
+ *   overwrote the extended chain's internal io_flink, truncating it and
+ *   planting a false chain boundary.
+ *
+ *   Reproduction sequence:
+ *     1. Enqueue a small single-IOB chain (chain1).  qh_tail -> chain1.
+ *     2. iob_copyin() enough data into chain1 to force allocation of a
+ *        second IOB.  IOB_CHAIN_END moves to the new tail, but qh_tail is
+ *        unchanged (still the original IOB).
+ *     3. Link a second chain (chain2) behind it, with iob_add_queue() and,
+ *        in a second case, with iob_concat_queue().
+ *
+ *   With the bug, step 3 overwrites chain1's internal link: chain1 is
+ *   truncated to a single IOB, and its second IOB is lost.  After the fix
+ *   chain1 comes out of the queue with all of its IOBs and data, and every
+ *   IOB returns to the pool.
+ *
+ ****************************************************************************/
+
+static int test_iob_queue_extend_tail(void)
+{
+  printf("\n=== Test 12: IOB Queue Extend Enqueued Tail Chain ===\n");
+
+  if (extend_tail_case(false) < 0 || extend_tail_case(true) < 0)
+    {
+      return -1;
+    }
+
   printf("Test 12 PASSED\n");
   return 0;
 }

Reply via email to