[clang] [clang][bytecode] Add some convenience API to BitcastBuffer (PR #169516)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 25 08:28:46 PST 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/169516
So we check the offsets before using them.
>From 3af793bad4b15e429a2eb0cb626ac1aea736054c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 25 Nov 2025 17:25:55 +0100
Subject: [PATCH] [clang][bytecode] Add some convenience API to BitcastBuffer
So we check the offsets before using them.
---
clang/lib/AST/ByteCode/BitcastBuffer.h | 13 +++++++++++++
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 8 ++++----
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/ByteCode/BitcastBuffer.h b/clang/lib/AST/ByteCode/BitcastBuffer.h
index d1d6ee39ad17b..8d32351883ae9 100644
--- a/clang/lib/AST/ByteCode/BitcastBuffer.h
+++ b/clang/lib/AST/ByteCode/BitcastBuffer.h
@@ -89,6 +89,12 @@ struct BitcastBuffer {
Data = std::make_unique<std::byte[]>(ByteSize);
}
+ /// Returns the byte at the given offset.
+ std::byte *atByte(unsigned Offset) {
+ assert(Offset < FinalBitSize.roundToBytes());
+ return Data.get() + Offset;
+ }
+
/// Returns the buffer size in bits.
Bits size() const { return FinalBitSize; }
Bytes byteSize() const { return FinalBitSize.toBytes(); }
@@ -113,6 +119,13 @@ struct BitcastBuffer {
std::unique_ptr<std::byte[]> copyBits(Bits BitOffset, Bits BitWidth,
Bits FullBitWidth,
Endian TargetEndianness) const;
+
+ /// Dereferences the value at the given offset.
+ template <typename T> T deref(Bytes Offset) const {
+ assert(Offset.getQuantity() < FinalBitSize.roundToBytes());
+ assert((Offset.getQuantity() + sizeof(T)) <= FinalBitSize.roundToBytes());
+ return *reinterpret_cast<T *>(Data.get() + Offset.getQuantity());
+ }
};
} // namespace interp
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 83e40f64fd979..2ab40ac9cc89c 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1997,8 +1997,8 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
for (size_t I = 0; I != CmpSize; I += ElemSize) {
if (IsWide) {
INT_TYPE_SWITCH(*S.getContext().classify(ASTCtx.getWCharType()), {
- T A = *reinterpret_cast<T *>(BufferA.Data.get() + I);
- T B = *reinterpret_cast<T *>(BufferB.Data.get() + I);
+ T A = *reinterpret_cast<T *>(BufferA.atByte(I));
+ T B = *reinterpret_cast<T *>(BufferB.atByte(I));
if (A < B) {
pushInteger(S, -1, Call->getType());
return true;
@@ -2009,8 +2009,8 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
}
});
} else {
- std::byte A = BufferA.Data[I];
- std::byte B = BufferB.Data[I];
+ std::byte A = BufferA.deref<std::byte>(Bytes(I));
+ std::byte B = BufferB.deref<std::byte>(Bytes(I));
if (A < B) {
pushInteger(S, -1, Call->getType());
More information about the cfe-commits
mailing list