[clang] [clang][bytecode] Fix memcmp/bcmp failures on big-endian hosts (PR #119851)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 13 02:06:37 PST 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/119851

See the discussion in
https://github.com/llvm/llvm-project/pull/119678#issuecomment-2539680746 and
https://github.com/llvm/llvm-project/pull/119544#issuecomment-2539678561

>From 0cf9d2b6bb83de7db94411a5a325ba4c83be6274 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 13 Dec 2024 11:05:09 +0100
Subject: [PATCH] [clang][bytecode] Fix memcmp/bcmp failures on big-endian
 hosts

See the discussion in
https://github.com/llvm/llvm-project/pull/119678#issuecomment-2539680746
and
https://github.com/llvm/llvm-project/pull/119544#issuecomment-2539678561
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp        | 8 ++++++++
 clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp | 5 -----
 clang/lib/AST/ByteCode/InterpBuiltinBitCast.h   | 5 +++++
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index a343280b5ce50f..172e26332474c0 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1943,10 +1943,18 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
   BitcastBuffer BufferA(
       Bits(S.getASTContext().getTypeSize(PtrA.getFieldDesc()->getType())));
   readPointerToBuffer(S.getContext(), PtrA, BufferA, false);
+  // FIXME: The swapping here is UNDOING something we do when reading the
+  // data into the buffer.
+  if (S.getASTContext().getTargetInfo().isBigEndian())
+    swapBytes(BufferA.Data.get(), BufferA.byteSize().getQuantity());
 
   BitcastBuffer BufferB(
       Bits(S.getASTContext().getTypeSize(PtrB.getFieldDesc()->getType())));
   readPointerToBuffer(S.getContext(), PtrB, BufferB, false);
+  // FIXME: The swapping here is UNDOING something we do when reading the
+  // data into the buffer.
+  if (S.getASTContext().getTargetInfo().isBigEndian())
+    swapBytes(BufferB.Data.get(), BufferB.byteSize().getQuantity());
 
   size_t MinBufferSize = std::min(BufferA.byteSize().getQuantity(),
                                   BufferB.byteSize().getQuantity());
diff --git a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
index c87993b8739a77..07f76943708216 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
@@ -73,11 +73,6 @@ using DataFunc =
     }                                                                          \
   } while (0)
 
-static void swapBytes(std::byte *M, size_t N) {
-  for (size_t I = 0; I != (N / 2); ++I)
-    std::swap(M[I], M[N - 1 - I]);
-}
-
 /// We use this to recursively iterate over all fields and elements of a pointer
 /// and extract relevant data for a bitcast.
 static bool enumerateData(const Pointer &P, const Context &Ctx, Bits Offset,
diff --git a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.h b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.h
index 08c207c7415dfa..b45613b2f21e20 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.h
+++ b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.h
@@ -19,6 +19,11 @@ class InterpState;
 class CodePtr;
 class Context;
 
+inline static void swapBytes(std::byte *M, size_t N) {
+  for (size_t I = 0; I != (N / 2); ++I)
+    std::swap(M[I], M[N - 1 - I]);
+}
+
 bool DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
                std::byte *Buff, Bits BitWidth, Bits FullBitWidth,
                bool &HasIndeterminateBits);



More information about the cfe-commits mailing list