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 996348d3e21015388d7da2a2796361d74640dac9
Author: huojianchao <[email protected]>
AuthorDate: Mon Nov 30 11:25:26 2026 +0800

    testing/mm/iob: put Test 12 in order and print only ASCII
    
    Move the extended tail chain test after Test 11, in numeric order, drop
    the unused <setjmp.h> and <stdarg.h>, and print ASCII instead of the
    box drawing and check mark characters, which garble on many serial
    consoles.  No functional change.
    
    Assisted-by: Kiro:claude-opus-5-5
    Signed-off-by: huojianchao <[email protected]>
---
 testing/mm/iob/iob_main.c | 784 +++++++++++++++++++++++-----------------------
 1 file changed, 391 insertions(+), 393 deletions(-)

diff --git a/testing/mm/iob/iob_main.c b/testing/mm/iob/iob_main.c
index 267d3e9bc..6cd398b96 100644
--- a/testing/mm/iob/iob_main.c
+++ b/testing/mm/iob/iob_main.c
@@ -28,8 +28,6 @@
 #include <nuttx/sched.h>
 
 #include <pthread.h>
-#include <setjmp.h>
-#include <stdarg.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -175,7 +173,7 @@ static void print_iob_queue(FAR struct iob_queue_s *queue, 
const char *label)
           if (IOB_FLAG_TEST(current, IOB_CHAIN_END))
             {
               chain_head = IOB_LINK_GET(current);
-              printf("  └─> End of chain (next chain at %p)\n",
+              printf("  -> End of chain (next chain at %p)\n",
                      chain_head);
               break;
             }
@@ -1158,450 +1156,190 @@ static int test_iob_queue_concat(void)
 }
 
 /****************************************************************************
- * Name: count_chain_iobs
+ * Name: test_iob_tailroom
  *
  * Description:
- *   Count the number of IOBs belonging to a single chain, walking up to and
- *   including the IOB that carries the IOB_CHAIN_END flag.  A correct chain
- *   is always terminated by IOB_CHAIN_END; if the flag has been lost the
- *   walk falls back to the raw end of the singly linked list.
+ *   Test IOB tailroom calculation
  *
  ****************************************************************************/
 
-static int count_chain_iobs(FAR struct iob_s *chain)
+static int test_iob_tailroom(void)
 {
-  FAR struct iob_s *iob = chain;
-  int count = 0;
+  FAR struct iob_s *iob;
+  uint8_t data[50];
+  unsigned int tailroom;
+  int i;
 
-  while (iob != NULL)
+  printf("\n=== Test 9: IOB Tailroom ===\n");
+
+  /* Allocate IOB */
+
+  iob = iob_alloc(false);
+  if (iob == NULL)
     {
-      count++;
+      printf("ERROR: Failed to allocate IOB\n");
+      return -1;
+    }
 
-      if (IOB_FLAG_TEST(iob, IOB_CHAIN_END))
-        {
-          break;
-        }
+  /* Check initial tailroom: all of the buffer after io_offset */
 
-      iob = IOB_LINK_GET(iob);
+  tailroom = iob_tailroom(iob);
+  printf("Initial tailroom: %u bytes\n", tailroom);
+  if (tailroom != IOB_BUFSIZE(iob) - iob->io_offset)
+    {
+      printf("ERROR: Initial tailroom %u, expected %u\n",
+             tailroom, IOB_BUFSIZE(iob) - iob->io_offset);
+      iob_free_chain(iob);
+      return -1;
     }
 
-  return count;
+  /* Add some data */
+
+  for (i = 0; i < sizeof(data); i++)
+    {
+      data[i] = i & 0xff;
+    }
+
+  if (iob_copyin(iob, data, sizeof(data), 0, false) != sizeof(data))
+    {
+      printf("ERROR: iob_copyin failed\n");
+      iob_free_chain(iob);
+      return -1;
+    }
+
+  /* Check tailroom after adding data */
+
+  tailroom = iob_tailroom(iob);
+  printf("Tailroom after adding %zu bytes: %u bytes\n",
+         sizeof(data), tailroom);
+
+  if (IOB_BUFSIZE(iob) >= sizeof(data) + iob->io_offset &&
+      tailroom != IOB_BUFSIZE(iob) - iob->io_offset - sizeof(data))
+    {
+      printf("ERROR: Tailroom %u, expected %zu\n", tailroom,
+             IOB_BUFSIZE(iob) - iob->io_offset - sizeof(data));
+      iob_free_chain(iob);
+      return -1;
+    }
+
+  /* Clean up */
+
+  iob_free_chain(iob);
+  printf("Test 9 PASSED\n");
+  return 0;
 }
 
 /****************************************************************************
- * Name: extend_tail_case
+ * Name: test_iob_pointer_flags
  *
  * Description:
- *   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.
+ *   Test pointer low-bit flags implementation (the key feature of commit
+ *   9a4364d3ce5b2fdd8ead81ffd8a913ae4f6b88a6)
  *
  ****************************************************************************/
 
-static int extend_tail_case(bool concat)
+static int test_iob_pointer_flags(void)
 {
   struct iob_queue_s queue;
-  struct iob_queue_s queue2;
   FAR struct iob_s *chain1;
   FAR struct iob_s *chain2;
+  FAR struct iob_s *chain3;
+  FAR struct iob_s *chain4;
   FAR struct iob_s *removed;
-  unsigned int bufsize;
-  unsigned int biglen;
-  unsigned int chain1_pktlen;
-  unsigned int chain2_pktlen;
-  unsigned int i;
+  unsigned int count;
+  unsigned int size;
   int navail;
-  int ret;
-
-  printf("Linking chain2 with %s\n",
-         concat ? "iob_concat_queue()" : "iob_add_queue()");
 
-  navail = iob_navail(false);
-  IOB_QINIT(&queue);
-  IOB_QINIT(&queue2);
+  printf("\n=== Test 10: IOB Pointer Low-Bit Flags (Enhanced) ===\n");
 
-  /* chain1: a single IOB carrying a few bytes. */
+  /* The chains below need POINTER_FLAGS_NIOBS free IOBs at the same time.
+   * Smaller pools (sim:nsh has 8) cannot run this test, which is not a
+   * failure of the IOB code.
+   */
 
-  chain1 = iob_alloc(false);
-  if (chain1 == NULL)
+  navail = iob_navail(false);
+  if (navail < POINTER_FLAGS_NIOBS)
     {
-      printf("ERROR: Failed to allocate chain1\n");
-      return -1;
+      printf("Test 10 SKIPPED: needs %d free IOBs, %d available\n",
+             POINTER_FLAGS_NIOBS, navail);
+      return 0;
     }
 
-  bufsize = IOB_BUFSIZE(chain1);
-
-  iob_copyin(chain1, (FAR const uint8_t *)"hello", 5, 0, false);
+  printf("This test verifies the key feature of commit 9a4364d3ce5b2fd:\n");
+  printf("Using pointer low-bit flags for queue management\n");
+  printf("Testing with multi-IOB chains to fully validate the design\n\n");
 
-  /* Enqueue chain1 while it is still a single IOB.  qh_tail now points at
-   * this IOB and it carries the IOB_CHAIN_END flag.
-   */
+  /* Initialize queue */
 
-  iob_add_queue(chain1, &queue);
-  ASSERT(iob_get_queue_entry_count(&queue) == 1);
+  IOB_QINIT(&queue);
 
-  /* Extend chain1 past a single IOB's capacity so iob_copyin() must
-   * allocate a second IOB, moving IOB_CHAIN_END to it and leaving qh_tail
-   * stale.  Append after the existing "hello" (offset 5).
+  /* Create chains with different lengths:
+   * Chain 1: 3 IOBs
+   * Chain 2: 2 IOBs
+   * Chain 3: 3 IOBs
+   * Chain 4: 1 IOB
+   * Total: 9 IOBs
    */
 
-  biglen = bufsize + (bufsize / 2) + 1;
-  memset(g_buffer1, 0xa5, biglen);
-
-  ret = iob_copyin(chain1, g_buffer1, biglen, 5, false);
-  if (ret != (int)biglen)
+  printf("Creating IOB chains...\n");
+  chain1 = create_iob_chain(3, 0xaa);
+  if (chain1 == NULL)
     {
-      printf("ERROR: iob_copyin extend failed, ret=%d\n", ret);
-      iob_free_queue(&queue);
+      printf("ERROR: Failed to create chain1\n");
       return -1;
     }
 
-  chain1_pktlen = chain1->io_pktlen;
-  ASSERT(chain1_pktlen == 5 + biglen);
-
-  /* chain1 must now span more than one IOB. */
-
-  if (count_chain_iobs(chain1) < 2)
+  chain2 = create_iob_chain(2, 0xbb);
+  if (chain2 == NULL)
     {
-      printf("ERROR: chain1 did not extend to a second IOB\n");
-      iob_free_queue(&queue);
+      printf("ERROR: Failed to create chain2\n");
+      iob_free_chain(chain1);
       return -1;
     }
 
-  /* chain2: another independent single-IOB chain. */
+  chain3 = create_iob_chain(3, 0xcc);
+  if (chain3 == NULL)
+    {
+      printf("ERROR: Failed to create chain3\n");
+      iob_free_chain(chain1);
+      iob_free_chain(chain2);
+      return -1;
+    }
 
-  chain2 = iob_alloc(false);
-  if (chain2 == NULL)
+  chain4 = create_iob_chain(1, 0xdd);
+  if (chain4 == NULL)
     {
-      printf("ERROR: Failed to allocate chain2\n");
-      iob_free_queue(&queue);
+      printf("ERROR: Failed to create chain4\n");
+      iob_free_chain(chain1);
+      iob_free_chain(chain2);
+      iob_free_chain(chain3);
       return -1;
     }
 
-  iob_copyin(chain2, (FAR const uint8_t *)"world", 5, 0, false);
-  chain2_pktlen = chain2->io_pktlen;
+  printf("Created 4 chains: 3, 2, 3, 1 IOBs each (total 9 IOBs)\n\n");
 
-  /* The operation that used to corrupt the extended tail chain. */
+  /* Print initial chains before adding to queue */
 
-  if (concat)
-    {
-      iob_add_queue(chain2, &queue2);
-      iob_concat_queue(&queue, &queue2);
-      ASSERT(IOB_QEMPTY(&queue2));
-    }
-  else
-    {
-      iob_add_queue(chain2, &queue);
-    }
+  print_iob_chain(chain1, "Chain 1 (3 IOBs) - before queue");
+  print_iob_chain(chain2, "Chain 2 (2 IOBs) - before queue");
+  print_iob_chain(chain3, "Chain 3 (3 IOBs) - before queue");
+  print_iob_chain(chain4, "Chain 4 (1 IOB) - before queue");
 
-  /* 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.
-   */
+  /* Add chains to queue */
 
-  if (iob_get_queue_entry_count(&queue) != 2 ||
-      iob_get_queue_size(&queue) != chain1_pktlen + chain2_pktlen)
-    {
-      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;
-    }
+  printf("\n--- Adding chains to queue ---\n");
 
-  /* Dequeue the first chain and confirm it still contains all of its IOBs
-   * and bytes: "hello" followed by the 0xa5 pattern.
-   */
+  iob_add_queue(chain1, &queue);
 
-  removed = iob_remove_queue(&queue);
-  if (removed != chain1 || count_chain_iobs(removed) < 2 ||
-      removed->io_pktlen != chain1_pktlen)
-    {
-      printf("ERROR: chain1 corrupted after dequeue\n");
-      iob_free_chain(removed);
-      iob_free_queue(&queue);
-      return -1;
-    }
+  printf("Added chain1 (3 IOBs) to queue\n");
+  print_iob_queue(&queue, "Queue after adding chain1");
 
-  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;
-    }
+  iob_add_queue(chain2, &queue);
 
-  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;
-        }
-    }
+  printf("Added chain2 (2 IOBs) to queue\n");
+  print_iob_queue(&queue, "Queue after adding chain2");
 
-  iob_free_chain(removed);
-
-  removed = iob_remove_queue(&queue);
-  if (removed != chain2 || removed->io_pktlen != chain2_pktlen)
-    {
-      printf("ERROR: chain2 corrupted after dequeue\n");
-      iob_free_chain(removed);
-      iob_free_queue(&queue);
-      return -1;
-    }
-
-  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;
-}
-
-/****************************************************************************
- * Name: test_iob_tailroom
- *
- * Description:
- *   Test IOB tailroom calculation
- *
- ****************************************************************************/
-
-static int test_iob_tailroom(void)
-{
-  FAR struct iob_s *iob;
-  uint8_t data[50];
-  unsigned int tailroom;
-  int i;
-
-  printf("\n=== Test 9: IOB Tailroom ===\n");
-
-  /* Allocate IOB */
-
-  iob = iob_alloc(false);
-  if (iob == NULL)
-    {
-      printf("ERROR: Failed to allocate IOB\n");
-      return -1;
-    }
-
-  /* Check initial tailroom: all of the buffer after io_offset */
-
-  tailroom = iob_tailroom(iob);
-  printf("Initial tailroom: %u bytes\n", tailroom);
-  if (tailroom != IOB_BUFSIZE(iob) - iob->io_offset)
-    {
-      printf("ERROR: Initial tailroom %u, expected %u\n",
-             tailroom, IOB_BUFSIZE(iob) - iob->io_offset);
-      iob_free_chain(iob);
-      return -1;
-    }
-
-  /* Add some data */
-
-  for (i = 0; i < sizeof(data); i++)
-    {
-      data[i] = i & 0xff;
-    }
-
-  if (iob_copyin(iob, data, sizeof(data), 0, false) != sizeof(data))
-    {
-      printf("ERROR: iob_copyin failed\n");
-      iob_free_chain(iob);
-      return -1;
-    }
-
-  /* Check tailroom after adding data */
-
-  tailroom = iob_tailroom(iob);
-  printf("Tailroom after adding %zu bytes: %u bytes\n",
-         sizeof(data), tailroom);
-
-  if (IOB_BUFSIZE(iob) >= sizeof(data) + iob->io_offset &&
-      tailroom != IOB_BUFSIZE(iob) - iob->io_offset - sizeof(data))
-    {
-      printf("ERROR: Tailroom %u, expected %zu\n", tailroom,
-             IOB_BUFSIZE(iob) - iob->io_offset - sizeof(data));
-      iob_free_chain(iob);
-      return -1;
-    }
-
-  /* Clean up */
-
-  iob_free_chain(iob);
-  printf("Test 9 PASSED\n");
-  return 0;
-}
-
-/****************************************************************************
- * Name: test_iob_pointer_flags
- *
- * Description:
- *   Test pointer low-bit flags implementation (the key feature of commit
- *   9a4364d3ce5b2fdd8ead81ffd8a913ae4f6b88a6)
- *
- ****************************************************************************/
-
-static int test_iob_pointer_flags(void)
-{
-  struct iob_queue_s queue;
-  FAR struct iob_s *chain1;
-  FAR struct iob_s *chain2;
-  FAR struct iob_s *chain3;
-  FAR struct iob_s *chain4;
-  FAR struct iob_s *removed;
-  unsigned int count;
-  unsigned int size;
-  int navail;
-
-  printf("\n=== Test 10: IOB Pointer Low-Bit Flags (Enhanced) ===\n");
-
-  /* The chains below need POINTER_FLAGS_NIOBS free IOBs at the same time.
-   * Smaller pools (sim:nsh has 8) cannot run this test, which is not a
-   * failure of the IOB code.
-   */
-
-  navail = iob_navail(false);
-  if (navail < POINTER_FLAGS_NIOBS)
-    {
-      printf("Test 10 SKIPPED: needs %d free IOBs, %d available\n",
-             POINTER_FLAGS_NIOBS, navail);
-      return 0;
-    }
-
-  printf("This test verifies the key feature of commit 9a4364d3ce5b2fd:\n");
-  printf("Using pointer low-bit flags for queue management\n");
-  printf("Testing with multi-IOB chains to fully validate the design\n\n");
-
-  /* Initialize queue */
-
-  IOB_QINIT(&queue);
-
-  /* Create chains with different lengths:
-   * Chain 1: 3 IOBs
-   * Chain 2: 2 IOBs
-   * Chain 3: 3 IOBs
-   * Chain 4: 1 IOB
-   * Total: 9 IOBs
-   */
-
-  printf("Creating IOB chains...\n");
-  chain1 = create_iob_chain(3, 0xaa);
-  if (chain1 == NULL)
-    {
-      printf("ERROR: Failed to create chain1\n");
-      return -1;
-    }
-
-  chain2 = create_iob_chain(2, 0xbb);
-  if (chain2 == NULL)
-    {
-      printf("ERROR: Failed to create chain2\n");
-      iob_free_chain(chain1);
-      return -1;
-    }
-
-  chain3 = create_iob_chain(3, 0xcc);
-  if (chain3 == NULL)
-    {
-      printf("ERROR: Failed to create chain3\n");
-      iob_free_chain(chain1);
-      iob_free_chain(chain2);
-      return -1;
-    }
-
-  chain4 = create_iob_chain(1, 0xdd);
-  if (chain4 == NULL)
-    {
-      printf("ERROR: Failed to create chain4\n");
-      iob_free_chain(chain1);
-      iob_free_chain(chain2);
-      iob_free_chain(chain3);
-      return -1;
-    }
-
-  printf("✓ Created 4 chains: 3, 2, 3, 1 IOBs each (total 9 IOBs)\n\n");
-
-  /* Print initial chains before adding to queue */
-
-  print_iob_chain(chain1, "Chain 1 (3 IOBs) - before queue");
-  print_iob_chain(chain2, "Chain 2 (2 IOBs) - before queue");
-  print_iob_chain(chain3, "Chain 3 (3 IOBs) - before queue");
-  print_iob_chain(chain4, "Chain 4 (1 IOB) - before queue");
-
-  /* Add chains to queue */
-
-  printf("\n--- Adding chains to queue ---\n");
-
-  iob_add_queue(chain1, &queue);
-
-  printf("Added chain1 (3 IOBs) to queue\n");
-  print_iob_queue(&queue, "Queue after adding chain1");
-
-  iob_add_queue(chain2, &queue);
-
-  printf("Added chain2 (2 IOBs) to queue\n");
-  print_iob_queue(&queue, "Queue after adding chain2");
-
-  iob_add_queue(chain3, &queue);
+  iob_add_queue(chain3, &queue);
 
   printf("Added chain3 (3 IOBs) to queue\n");
   print_iob_queue(&queue, "Queue after adding chain3");
@@ -1638,7 +1376,7 @@ static int test_iob_pointer_flags(void)
       goto cleanup;
     }
 
-  printf("✓ Removed chain1 (3 IOBs)\n");
+  printf("Removed chain1 (3 IOBs)\n");
   print_iob_queue(&queue, "Queue after removing chain1");
   iob_free_chain(removed);
 
@@ -1650,7 +1388,7 @@ static int test_iob_pointer_flags(void)
       goto cleanup;
     }
 
-  printf("✓ Removed chain2 (2 IOBs)\n");
+  printf("Removed chain2 (2 IOBs)\n");
   print_iob_queue(&queue, "Queue after removing chain2");
   iob_free_chain(removed);
 
@@ -1662,7 +1400,7 @@ static int test_iob_pointer_flags(void)
       goto cleanup;
     }
 
-  printf("✓ Removed chain3 (3 IOBs)\n");
+  printf("Removed chain3 (3 IOBs)\n");
   print_iob_queue(&queue, "Queue after removing chain3");
   iob_free_chain(removed);
 
@@ -1674,7 +1412,7 @@ static int test_iob_pointer_flags(void)
       goto cleanup;
     }
 
-  printf("✓ Removed chain4 (1 IOB)\n");
+  printf("Removed chain4 (1 IOB)\n");
   print_iob_queue(&queue, "Queue after removing all chains");
   iob_free_chain(removed);
 
@@ -1686,7 +1424,7 @@ static int test_iob_pointer_flags(void)
       return -1;
     }
 
-  printf("\n✓ Queue is empty\n");
+  printf("\nQueue is empty\n");
 
   printf("\nTest 10 PASSED\n");
   return 0;
@@ -1954,6 +1692,266 @@ static int test_iob_thread(void)
   return 0;
 }
 
+/****************************************************************************
+ * Name: count_chain_iobs
+ *
+ * Description:
+ *   Count the number of IOBs belonging to a single chain, walking up to and
+ *   including the IOB that carries the IOB_CHAIN_END flag.  A correct chain
+ *   is always terminated by IOB_CHAIN_END; if the flag has been lost the
+ *   walk falls back to the raw end of the singly linked list.
+ *
+ ****************************************************************************/
+
+static int count_chain_iobs(FAR struct iob_s *chain)
+{
+  FAR struct iob_s *iob = chain;
+  int count = 0;
+
+  while (iob != NULL)
+    {
+      count++;
+
+      if (IOB_FLAG_TEST(iob, IOB_CHAIN_END))
+        {
+          break;
+        }
+
+      iob = IOB_LINK_GET(iob);
+    }
+
+  return count;
+}
+
+/****************************************************************************
+ * Name: extend_tail_case
+ *
+ * Description:
+ *   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 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;
+  unsigned int bufsize;
+  unsigned int biglen;
+  unsigned int chain1_pktlen;
+  unsigned int chain2_pktlen;
+  unsigned int i;
+  int navail;
+  int ret;
+
+  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. */
+
+  chain1 = iob_alloc(false);
+  if (chain1 == NULL)
+    {
+      printf("ERROR: Failed to allocate chain1\n");
+      return -1;
+    }
+
+  bufsize = IOB_BUFSIZE(chain1);
+
+  iob_copyin(chain1, (FAR const uint8_t *)"hello", 5, 0, false);
+
+  /* Enqueue chain1 while it is still a single IOB.  qh_tail now points at
+   * this IOB and it carries the IOB_CHAIN_END flag.
+   */
+
+  iob_add_queue(chain1, &queue);
+  ASSERT(iob_get_queue_entry_count(&queue) == 1);
+
+  /* Extend chain1 past a single IOB's capacity so iob_copyin() must
+   * allocate a second IOB, moving IOB_CHAIN_END to it and leaving qh_tail
+   * stale.  Append after the existing "hello" (offset 5).
+   */
+
+  biglen = bufsize + (bufsize / 2) + 1;
+  memset(g_buffer1, 0xa5, biglen);
+
+  ret = iob_copyin(chain1, g_buffer1, biglen, 5, false);
+  if (ret != (int)biglen)
+    {
+      printf("ERROR: iob_copyin extend failed, ret=%d\n", ret);
+      iob_free_queue(&queue);
+      return -1;
+    }
+
+  chain1_pktlen = chain1->io_pktlen;
+  ASSERT(chain1_pktlen == 5 + biglen);
+
+  /* chain1 must now span more than one IOB. */
+
+  if (count_chain_iobs(chain1) < 2)
+    {
+      printf("ERROR: chain1 did not extend to a second IOB\n");
+      iob_free_queue(&queue);
+      return -1;
+    }
+
+  /* chain2: another independent single-IOB chain. */
+
+  chain2 = iob_alloc(false);
+  if (chain2 == NULL)
+    {
+      printf("ERROR: Failed to allocate chain2\n");
+      iob_free_queue(&queue);
+      return -1;
+    }
+
+  iob_copyin(chain2, (FAR const uint8_t *)"world", 5, 0, false);
+  chain2_pktlen = chain2->io_pktlen;
+
+  /* The operation that used to corrupt the extended tail chain. */
+
+  if (concat)
+    {
+      iob_add_queue(chain2, &queue2);
+      iob_concat_queue(&queue, &queue2);
+      ASSERT(IOB_QEMPTY(&queue2));
+    }
+  else
+    {
+      iob_add_queue(chain2, &queue);
+    }
+
+  /* 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 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: "hello" followed by the 0xa5 pattern.
+   */
+
+  removed = iob_remove_queue(&queue);
+  if (removed != chain1 || count_chain_iobs(removed) < 2 ||
+      removed->io_pktlen != chain1_pktlen)
+    {
+      printf("ERROR: chain1 corrupted after dequeue\n");
+      iob_free_chain(removed);
+      iob_free_queue(&queue);
+      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);
+  if (removed != chain2 || removed->io_pktlen != chain2_pktlen)
+    {
+      printf("ERROR: chain2 corrupted after dequeue\n");
+      iob_free_chain(removed);
+      iob_free_queue(&queue);
+      return -1;
+    }
+
+  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;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/

Reply via email to