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 8fbac5c2ede81216728b7d8e85129aa60a5481a7
Author: dengwenqi <[email protected]>
AuthorDate: Sat May 23 02:27:26 2026 +0800

    libs/libc/backtrace: skip KASAN instrumentation in backtrace_find hot path
    
    backtrace_find() runs for every backtrace record.  On debug builds with
    KASAN the inner memcmp() compares the recorded stack pointer array
    byte-by-byte and each byte read goes through __asan_load1_noabort, which
    multiplies the cost of each record.  When backtraces are recorded at a
    high rate, e.g. for every heap allocation during the mount of a large
    romfs, this kept the system busy long enough to look hung.
    
    Tag backtrace_find() with nosanitize_address and replace the memcmp()
    call with an inline word-by-word comparison.  The stack array stores
    word-aligned function pointers, so word compare is semantically
    equivalent to byte memcmp().  The entry->depth pre-check is preserved
    to avoid reading past the recorded depth.
    
    This mirrors the existing nosanitize_address treatment of
    backtrace_format() and only affects internal data accesses inside the
    backtrace pool -- user buffers are unchanged.
    
    Signed-off-by: dengwenqi <[email protected]>
---
 libs/libc/misc/lib_backtrace.c | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/libs/libc/misc/lib_backtrace.c b/libs/libc/misc/lib_backtrace.c
index d5d4664d232..6730ab15c63 100644
--- a/libs/libc/misc/lib_backtrace.c
+++ b/libs/libc/misc/lib_backtrace.c
@@ -253,23 +253,49 @@ static int backtrace_expand(FAR struct backtrace_pool_s 
*bp)
  * Name: backtrace_exist
  ****************************************************************************/
 
+nosanitize_address
 static FAR struct backtrace_entry_s *
 backtrace_find(FAR struct backtrace_pool_s *bp, uint32_t slot,
                FAR const void *stack, int depth)
 {
   FAR struct backtrace_entry_s *entry = bp->bucket[slot];
+  FAR const uintptr_t *s1 = (FAR const uintptr_t *)stack;
+  int i;
 
   while (entry != NULL)
     {
-      if (entry->depth == depth &&
-          memcmp(stack, entry->stack, depth * sizeof(FAR void *)) == 0)
+      if (entry->depth == depth)
         {
-          /* If the backtrace record already exists, just increase the
-           * reference count
+          FAR const uintptr_t *s2 = (FAR const uintptr_t *)entry->stack;
+
+          /* Inline word-by-word compare to avoid the per-byte KASAN shadow
+           * checks inside memcmp().  This function is on the hot path of
+           * every recorded allocation when CONFIG_MM_RECORD_STACK is on; on
+           * KASAN-enabled debug builds the per-byte instrumentation in
+           * memcmp() multiplies the cost of each lookup and can stretch
+           * heavy allocation phases (e.g. romfs_cachenode during mount of a
+           * large partition) past the test framework's busy-loop threshold.
+           * The stack array stores word-aligned function pointers, so word
+           * comparison is semantically equivalent to memcmp().
            */
 
-          entry->ref++;
-          return entry;
+          for (i = 0; i < depth; i++)
+            {
+              if (s1[i] != s2[i])
+                {
+                  break;
+                }
+            }
+
+          if (i == depth)
+            {
+              /* If the backtrace record already exists, just increase the
+               * reference count
+               */
+
+              entry->ref++;
+              return entry;
+            }
         }
 
       entry = entry->next;

Reply via email to