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.git

commit c54ca0f12a76ffa1aca31a5cbd90a6e9086d88ba
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 03:52:19 2026 +0100

    libs/libc/search: Resolve FDPIC descriptors in the tsearch() family.
    
    tsearch(), tfind(), tdelete(), twalk() and tdestroy() call the compar,
    action and free_node functions that the caller gives.  An FDPIC module
    gives the address of a function descriptor, not a code address.  The
    firmware branched to the descriptor in the module's data segment, and
    the module stopped with a usage fault.
    
    With CONFIG_FDPIC, each function now resolves its callback one time at
    entry with fdpic_callback(), as lfind(), bsearch() and qsort() do.
    twalk() and tdestroy() recurse, so the public function resolves and the
    recursive helper gets the resolved pointer: a second resolve would read
    a code address as a descriptor.  tdestroy() gets the static helper
    tdestroy_node() for this.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/search/lib_tdelete.c  | 13 +++++++++++
 libs/libc/search/lib_tdestroy.c | 49 ++++++++++++++++++++++++++++++-----------
 libs/libc/search/lib_tfind.c    | 13 +++++++++++
 libs/libc/search/lib_tsearch.c  | 13 +++++++++++
 libs/libc/search/lib_twalk.c    | 15 +++++++++++++
 5 files changed, 90 insertions(+), 13 deletions(-)

diff --git a/libs/libc/search/lib_tdelete.c b/libs/libc/search/lib_tdelete.c
index 68b139c0858..92981426fc9 100644
--- a/libs/libc/search/lib_tdelete.c
+++ b/libs/libc/search/lib_tdelete.c
@@ -24,8 +24,14 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <search.h>
 
+#ifdef CONFIG_FDPIC
+#  include <nuttx/fdpic.h>
+#endif
+
 #include "libc.h"
 #include "search/tsearch.h"
 
@@ -64,6 +70,13 @@ FAR void *tdelete(FAR const void *key, FAR void **rootp,
       return NULL;
     }
 
+#ifdef CONFIG_FDPIC
+  /* See qsort(): an FDPIC caller passes a descriptor, not a code address */
+
+  compar = (CODE int (*)(FAR const void *, FAR const void *))
+           fdpic_callback((FAR void *)compar);
+#endif
+
   link = (FAR struct tnode_s **)rootp;
   path[n++] = link;
   while (*link != NULL && (c = compar(key, (*link)->key)) != 0)
diff --git a/libs/libc/search/lib_tdestroy.c b/libs/libc/search/lib_tdestroy.c
index 2f719385434..ffc55284695 100644
--- a/libs/libc/search/lib_tdestroy.c
+++ b/libs/libc/search/lib_tdestroy.c
@@ -24,11 +24,39 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <search.h>
 
+#ifdef CONFIG_FDPIC
+#  include <nuttx/fdpic.h>
+#endif
+
 #include "libc.h"
 #include "search/tsearch.h"
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void tdestroy_node(FAR struct tnode_s *node,
+                          CODE void (*free_node)(FAR void *))
+{
+  if (node == NULL)
+    {
+      return;
+    }
+
+  tdestroy_node(node->child[0], free_node);
+  tdestroy_node(node->child[1], free_node);
+  if (free_node != NULL)
+    {
+      free_node((FAR void *)node->key);
+    }
+
+  lib_free(node);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -44,19 +72,14 @@
 
 void tdestroy(FAR void *root, CODE void (*free_node)(FAR void *))
 {
-  FAR struct tnode_s *node = root;
-
-  if (node == NULL)
-    {
-      return;
-    }
+#ifdef CONFIG_FDPIC
+  /* See qsort(): an FDPIC caller passes a descriptor, not a code address.
+   * tdestroy_node() gets the resolved free_node.
+   */
 
-  tdestroy(node->child[0], free_node);
-  tdestroy(node->child[1], free_node);
-  if (free_node != NULL)
-    {
-      free_node((FAR void *)node->key);
-    }
+  free_node = (CODE void (*)(FAR void *))
+              fdpic_callback((FAR void *)free_node);
+#endif
 
-  lib_free(node);
+  tdestroy_node(root, free_node);
 }
diff --git a/libs/libc/search/lib_tfind.c b/libs/libc/search/lib_tfind.c
index bcec1abc9af..fd3d44fa84a 100644
--- a/libs/libc/search/lib_tfind.c
+++ b/libs/libc/search/lib_tfind.c
@@ -24,8 +24,14 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <search.h>
 
+#ifdef CONFIG_FDPIC
+#  include <nuttx/fdpic.h>
+#endif
+
 #include "search/tsearch.h"
 
 /****************************************************************************
@@ -54,6 +60,13 @@ FAR void *tfind(FAR const void *key, FAR void * const *rootp,
       return NULL;
     }
 
+#ifdef CONFIG_FDPIC
+  /* See qsort(): an FDPIC caller passes a descriptor, not a code address */
+
+  compar = (CODE int (*)(FAR const void *, FAR const void *))
+           fdpic_callback((FAR void *)compar);
+#endif
+
   for (node = *rootp; node != NULL; node = node->child[c > 0])
     {
       c = compar(key, node->key);
diff --git a/libs/libc/search/lib_tsearch.c b/libs/libc/search/lib_tsearch.c
index 530e9e374df..fc2a9b453bf 100644
--- a/libs/libc/search/lib_tsearch.c
+++ b/libs/libc/search/lib_tsearch.c
@@ -24,8 +24,14 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <search.h>
 
+#ifdef CONFIG_FDPIC
+#  include <nuttx/fdpic.h>
+#endif
+
 #include "libc.h"
 #include "search/tsearch.h"
 
@@ -130,6 +136,13 @@ FAR void *tsearch(FAR const void *key, FAR void **rootp,
       return NULL;
     }
 
+#ifdef CONFIG_FDPIC
+  /* See qsort(): an FDPIC caller passes a descriptor, not a code address */
+
+  compar = (CODE int (*)(FAR const void *, FAR const void *))
+           fdpic_callback((FAR void *)compar);
+#endif
+
   link = (FAR struct tnode_s **)rootp;
   path[n++] = link;
   while (*link != NULL)
diff --git a/libs/libc/search/lib_twalk.c b/libs/libc/search/lib_twalk.c
index ee21dc3e17a..2731b520399 100644
--- a/libs/libc/search/lib_twalk.c
+++ b/libs/libc/search/lib_twalk.c
@@ -24,8 +24,14 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <search.h>
 
+#ifdef CONFIG_FDPIC
+#  include <nuttx/fdpic.h>
+#endif
+
 #include "search/tsearch.h"
 
 /****************************************************************************
@@ -76,6 +82,15 @@ void twalk(FAR const void *root,
 {
   if (root != NULL && action != NULL)
     {
+#ifdef CONFIG_FDPIC
+      /* See qsort(): an FDPIC caller passes a descriptor, not a code
+       * address.  twalk_node() gets the resolved action.
+       */
+
+      action = (CODE void (*)(FAR const void *, VISIT, int))
+               fdpic_callback((FAR void *)action);
+#endif
+
       twalk_node(root, action, 0);
     }
 }

Reply via email to