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 35176f627711736a0beef7243c95e19cb1ee015b
Author: wangmingrong1 <[email protected]>
AuthorDate: Mon Nov 2 16:08:35 2026 +0800

    tools/gcov: accept little-endian profraw magic in is_profraw_file
    
    Root cause: the LLVM raw profile header magic is a uint64_t written by
    the runtime in native byte order (high byte 0xff, low byte 0x81 with
    "lprofr"/"lprofR" in the middle). On big-endian targets this lands on
    disk as \xff..\x81, but on little-endian targets (ARM aarch64, x86_64,
    sim) it lands as \x81..\xff. The previous magic check only matched the
    big-endian form, so profraw files dumped on goldfish-arm64-v8a-ap (and
    every other LE board) were misclassified as text and routed into
    parse_gcda_data(), which opens them as utf-8 and immediately raises
    "UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 0",
    failing Vela_System_Tools_Gcov_Integration.
    
    Fix: accept both the native and byte-swapped 8-byte magic in
    is_profraw_file(), mirroring LLVM's RawInstrProfReader::hasFormat which
    compares the on-disk magic against both forms. This unblocks the clang
    coverage pipeline on every little-endian target without affecting the
    big-endian path or the gcc / hex-dump / binpack branches.
    
    Signed-off-by: wangmingrong1 <[email protected]>
---
 tools/gcov.py | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/tools/gcov.py b/tools/gcov.py
index 01b90d22c85..a13e4fcba0c 100755
--- a/tools/gcov.py
+++ b/tools/gcov.py
@@ -26,9 +26,17 @@ import struct
 import subprocess
 import sys
 
-# LLVM raw profile magic bytes (first 8 bytes of .profraw)
-# 64-bit: \xff lprofr \x81
-# 32-bit: \xff lprofR \x81
+# LLVM raw profile magic bytes (first 8 bytes of .profraw).
+#
+# The magic is a uint64_t written by the runtime in native byte order
+# (high byte 0xff, low byte 0x81; "lprofr"/"lprofR" in the middle), so
+# big-endian platforms produce \xff..\x81 and little-endian platforms
+# (ARM aarch64, x86_64, etc.) produce \x81..\xff. Both forms must be
+# accepted, mirroring LLVM's RawInstrProfReader::hasFormat which compares
+# the on-disk magic against both the native and byte-swapped values.
+#
+# 64-bit BE: \xff lprofr \x81   /   64-bit LE: \x81 rforpl \xff
+# 32-bit BE: \xff lprofR \x81   /   32-bit LE: \x81 Rforpl \xff
 
 PROFRAW_MAGIC_64 = b"\xff\x6c\x70\x72\x6f\x66\x72\x81"
 PROFRAW_MAGIC_32 = b"\xff\x6c\x70\x72\x6f\x66\x52\x81"
@@ -42,11 +50,20 @@ GCOV_BINPACK_PATH_LEN_MAX = 4096
 
 
 def is_profraw_file(path):
-    """Check if a file is an LLVM profraw file by magic number."""
+    """Check if a file is an LLVM profraw file by magic number.
+
+    Accepts both native and byte-swapped magic so that profraw files
+    produced on either little- or big-endian targets are recognised.
+    """
     try:
         with open(path, "rb") as f:
             magic = f.read(8)
-            return magic == PROFRAW_MAGIC_64 or magic == PROFRAW_MAGIC_32
+            return magic in (
+                PROFRAW_MAGIC_64,
+                PROFRAW_MAGIC_64[::-1],
+                PROFRAW_MAGIC_32,
+                PROFRAW_MAGIC_32[::-1],
+            )
     except (IOError, OSError):
         return False
 

Reply via email to