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 949c83617fc375389e31f57e4ccb18f490828e43
Author: dengwenqi <[email protected]>
AuthorDate: Tue Jul 7 07:00:54 2026 +0800

    backtrace: skip KASAN instrumentation in backtrace_alloc to fix busyloop
    
    backtrace_alloc() runs for every new backtrace record.  With
    CONFIG_MM_KASAN=y its memcpy() and field stores go through
    __asan_store4_noabort for each word, which adds up when backtraces are
    recorded at a high rate, e.g. for every allocation while a large module
    is loaded.  backtrace_find() was already hardened with
    nosanitize_address and an inline word compare for the same reason.
    
    Mark backtrace_alloc() nosanitize_address and no_builtin("memcpy") and
    replace memcpy() with an inline word-by-word copy, mirroring
    backtrace_find().
    
    Signed-off-by: dengwenqi <[email protected]>
---
 libs/libc/misc/lib_backtrace.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/libs/libc/misc/lib_backtrace.c b/libs/libc/misc/lib_backtrace.c
index ed1b34265f0..fd53fbdad93 100644
--- a/libs/libc/misc/lib_backtrace.c
+++ b/libs/libc/misc/lib_backtrace.c
@@ -361,11 +361,15 @@ backtrace_find(FAR struct backtrace_pool_s *bp, uint32_t 
slot,
  * Name: backtrace_alloc
  ****************************************************************************/
 
+nosanitize_address no_builtin("memcpy")
 static FAR struct backtrace_entry_s *
 backtrace_alloc(FAR struct backtrace_pool_s *bp, uint32_t slot,
                 const FAR void *stack, int depth)
 {
   FAR struct backtrace_entry_s *entry;
+  FAR const uintptr_t *src = (FAR const uintptr_t *)stack;
+  FAR uintptr_t *dst;
+  int i;
 
   /* Get the first entry from the free list */
 
@@ -382,7 +386,23 @@ backtrace_alloc(FAR struct backtrace_pool_s *bp, uint32_t 
slot,
   /* Copy the backtrace to the entry */
 
   bp->used++;
-  memcpy(entry->stack, stack, depth * sizeof(FAR void *));
+
+  /* Inline word-by-word copy to avoid the per-byte KASAN shadow checks
+   * inside memcpy().  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 memcpy() multiplies the cost
+   * of each insertion and can stretch heavy allocation phases (e.g. WAMR
+   * AOT module loading) past the test framework's busy-loop threshold.
+   * The stack array stores word-aligned function pointers, so word
+   * copying is semantically equivalent to memcpy().
+   */
+
+  dst = (FAR uintptr_t *)entry->stack;
+  for (i = 0; i < depth; i++)
+    {
+      dst[i] = src[i];
+    }
+
   entry->depth = depth;
   entry->ref = 1;
 

Reply via email to