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 6e689011c8d6cd284c3e67ff543de43be947f2c6
Author: dengwenqi <[email protected]>
AuthorDate: Wed May 27 23:36:06 2026 +0800

    libs/libc/backtrace: prevent compiler from rewriting hot loop back into 
memcmp
    
    The previous change replaced memcmp() inside backtrace_find() with an
    inline word-by-word loop and tagged the function nosanitize_address, to
    avoid the per-byte KASAN __asan_load1_noabort calls.  GCC defeats it at
    -O2: -ftree-loop-distribute-patterns recognizes the loop as a memcmp
    pattern and rewrites it back into a call to memcmp(), which is in
    another translation unit, is KASAN-instrumented, and brings the cost
    back.  The disassembly of backtrace_find() shows the `bl memcmp`.
    
    Add the no_builtin("memcmp") attribute (already used the same way in
    libs/libc/machine/arch_libc.c for the same reason) so GCC keeps the
    loop literal.
    
    Signed-off-by: dengwenqi <[email protected]>
---
 libs/libc/misc/lib_backtrace.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/libs/libc/misc/lib_backtrace.c b/libs/libc/misc/lib_backtrace.c
index 6730ab15c63..2a13857d6ba 100644
--- a/libs/libc/misc/lib_backtrace.c
+++ b/libs/libc/misc/lib_backtrace.c
@@ -253,7 +253,7 @@ static int backtrace_expand(FAR struct backtrace_pool_s *bp)
  * Name: backtrace_exist
  ****************************************************************************/
 
-nosanitize_address
+nosanitize_address no_builtin("memcmp")
 static FAR struct backtrace_entry_s *
 backtrace_find(FAR struct backtrace_pool_s *bp, uint32_t slot,
                FAR const void *stack, int depth)
@@ -277,6 +277,12 @@ backtrace_find(FAR struct backtrace_pool_s *bp, uint32_t 
slot,
            * 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().
+           *
+           * The no_builtin("memcmp") attribute is required because GCC's
+           * -ftree-loop-distribute-patterns optimization (enabled at -O2)
+           * recognizes this loop as a memcmp pattern and rewrites it back
+           * into a memcmp() call, defeating the nosanitize_address intent
+           * (the called memcmp is still KASAN-instrumented per byte read).
            */
 
           for (i = 0; i < depth; i++)

Reply via email to