Skip to content

Commit 30cbd09

Browse files
authored
[clang][bytecode] Fix memcmp/bcmp failures on big-endian hosts (#119851)
See the discussion in #119678 (comment) and #119544 (comment)
1 parent 8820de6 commit 30cbd09

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,10 +1943,18 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
19431943
BitcastBuffer BufferA(
19441944
Bits(S.getASTContext().getTypeSize(PtrA.getFieldDesc()->getType())));
19451945
readPointerToBuffer(S.getContext(), PtrA, BufferA, false);
1946+
// FIXME: The swapping here is UNDOING something we do when reading the
1947+
// data into the buffer.
1948+
if (S.getASTContext().getTargetInfo().isBigEndian())
1949+
swapBytes(BufferA.Data.get(), BufferA.byteSize().getQuantity());
19461950

19471951
BitcastBuffer BufferB(
19481952
Bits(S.getASTContext().getTypeSize(PtrB.getFieldDesc()->getType())));
19491953
readPointerToBuffer(S.getContext(), PtrB, BufferB, false);
1954+
// FIXME: The swapping here is UNDOING something we do when reading the
1955+
// data into the buffer.
1956+
if (S.getASTContext().getTargetInfo().isBigEndian())
1957+
swapBytes(BufferB.Data.get(), BufferB.byteSize().getQuantity());
19501958

19511959
size_t MinBufferSize = std::min(BufferA.byteSize().getQuantity(),
19521960
BufferB.byteSize().getQuantity());

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ using DataFunc =
7373
} \
7474
} while (0)
7575

76-
static void swapBytes(std::byte *M, size_t N) {
77-
for (size_t I = 0; I != (N / 2); ++I)
78-
std::swap(M[I], M[N - 1 - I]);
79-
}
80-
8176
/// We use this to recursively iterate over all fields and elements of a pointer
8277
/// and extract relevant data for a bitcast.
8378
static bool enumerateData(const Pointer &P, const Context &Ctx, Bits Offset,

clang/lib/AST/ByteCode/InterpBuiltinBitCast.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class InterpState;
1919
class CodePtr;
2020
class Context;
2121

22+
inline static void swapBytes(std::byte *M, size_t N) {
23+
for (size_t I = 0; I != (N / 2); ++I)
24+
std::swap(M[I], M[N - 1 - I]);
25+
}
26+
2227
bool DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
2328
std::byte *Buff, Bits BitWidth, Bits FullBitWidth,
2429
bool &HasIndeterminateBits);

0 commit comments

Comments
 (0)