xref: /llvm-project/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp (revision 2f9cd43a736008bdecdd920f84c702209ddbd20f)
1 //===-------------------- InterpBuiltinBitCast.cpp --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "InterpBuiltinBitCast.h"
9 #include "BitcastBuffer.h"
10 #include "Boolean.h"
11 #include "Context.h"
12 #include "Floating.h"
13 #include "Integral.h"
14 #include "InterpState.h"
15 #include "MemberPointer.h"
16 #include "Pointer.h"
17 #include "Record.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/Basic/TargetInfo.h"
21 
22 using namespace clang;
23 using namespace clang::interp;
24 
25 /// Implement __builtin_bit_cast and related operations.
26 /// Since our internal representation for data is more complex than
27 /// something we can simply memcpy or memcmp, we first bitcast all the data
28 /// into a buffer, which we then later use to copy the data into the target.
29 
30 // TODO:
31 //  - Try to minimize heap allocations.
32 //  - Optimize the common case of only pushing and pulling full
33 //    bytes to/from the buffer.
34 
35 /// Used to iterate over pointer fields.
36 using DataFunc = llvm::function_ref<bool(const Pointer &P, PrimType Ty,
37                                          Bits BitOffset, bool PackedBools)>;
38 
39 #define BITCAST_TYPE_SWITCH(Expr, B)                                           \
40   do {                                                                         \
41     switch (Expr) {                                                            \
42       TYPE_SWITCH_CASE(PT_Sint8, B)                                            \
43       TYPE_SWITCH_CASE(PT_Uint8, B)                                            \
44       TYPE_SWITCH_CASE(PT_Sint16, B)                                           \
45       TYPE_SWITCH_CASE(PT_Uint16, B)                                           \
46       TYPE_SWITCH_CASE(PT_Sint32, B)                                           \
47       TYPE_SWITCH_CASE(PT_Uint32, B)                                           \
48       TYPE_SWITCH_CASE(PT_Sint64, B)                                           \
49       TYPE_SWITCH_CASE(PT_Uint64, B)                                           \
50       TYPE_SWITCH_CASE(PT_IntAP, B)                                            \
51       TYPE_SWITCH_CASE(PT_IntAPS, B)                                           \
52       TYPE_SWITCH_CASE(PT_Bool, B)                                             \
53     default:                                                                   \
54       llvm_unreachable("Unhandled bitcast type");                              \
55     }                                                                          \
56   } while (0)
57 
58 #define BITCAST_TYPE_SWITCH_FIXED_SIZE(Expr, B)                                \
59   do {                                                                         \
60     switch (Expr) {                                                            \
61       TYPE_SWITCH_CASE(PT_Sint8, B)                                            \
62       TYPE_SWITCH_CASE(PT_Uint8, B)                                            \
63       TYPE_SWITCH_CASE(PT_Sint16, B)                                           \
64       TYPE_SWITCH_CASE(PT_Uint16, B)                                           \
65       TYPE_SWITCH_CASE(PT_Sint32, B)                                           \
66       TYPE_SWITCH_CASE(PT_Uint32, B)                                           \
67       TYPE_SWITCH_CASE(PT_Sint64, B)                                           \
68       TYPE_SWITCH_CASE(PT_Uint64, B)                                           \
69       TYPE_SWITCH_CASE(PT_Bool, B)                                             \
70     default:                                                                   \
71       llvm_unreachable("Unhandled bitcast type");                              \
72     }                                                                          \
73   } while (0)
74 
75 static void swapBytes(std::byte *M, size_t N) {
76   for (size_t I = 0; I != (N / 2); ++I)
77     std::swap(M[I], M[N - 1 - I]);
78 }
79 
80 /// We use this to recursively iterate over all fields and elements of a pointer
81 /// and extract relevant data for a bitcast.
82 static bool enumerateData(const Pointer &P, const Context &Ctx, Bits Offset,
83                           Bits BitsToRead, DataFunc F) {
84   const Descriptor *FieldDesc = P.getFieldDesc();
85   assert(FieldDesc);
86 
87   // Primitives.
88   if (FieldDesc->isPrimitive())
89     return F(P, FieldDesc->getPrimType(), Offset, /*PackedBools=*/false);
90 
91   // Primitive arrays.
92   if (FieldDesc->isPrimitiveArray()) {
93     QualType ElemType = FieldDesc->getElemQualType();
94     size_t ElemSizeInBits = Ctx.getASTContext().getTypeSize(ElemType);
95     PrimType ElemT = *Ctx.classify(ElemType);
96     // Special case, since the bools here are packed.
97     bool PackedBools = FieldDesc->getType()->isExtVectorBoolType();
98     unsigned NumElems = FieldDesc->getNumElems();
99     bool Ok = true;
100     for (unsigned I = P.getIndex(); I != NumElems; ++I) {
101       Ok = Ok && F(P.atIndex(I), ElemT, Offset, PackedBools);
102       Offset += PackedBools ? 1 : ElemSizeInBits;
103       if (Offset >= BitsToRead)
104         break;
105     }
106     return Ok;
107   }
108 
109   // Composite arrays.
110   if (FieldDesc->isCompositeArray()) {
111     QualType ElemType = FieldDesc->getElemQualType();
112     size_t ElemSizeInBits = Ctx.getASTContext().getTypeSize(ElemType);
113     for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I) {
114       enumerateData(P.atIndex(I).narrow(), Ctx, Offset, BitsToRead, F);
115       Offset += ElemSizeInBits;
116       if (Offset >= BitsToRead)
117         break;
118     }
119     return true;
120   }
121 
122   // Records.
123   if (FieldDesc->isRecord()) {
124     const Record *R = FieldDesc->ElemRecord;
125     const ASTRecordLayout &Layout =
126         Ctx.getASTContext().getASTRecordLayout(R->getDecl());
127     bool Ok = true;
128 
129     for (const Record::Field &Fi : R->fields()) {
130       if (Fi.isUnnamedBitField())
131         continue;
132       Pointer Elem = P.atField(Fi.Offset);
133       Bits BitOffset =
134           Offset + Bits(Layout.getFieldOffset(Fi.Decl->getFieldIndex()));
135       Ok = Ok && enumerateData(Elem, Ctx, BitOffset, BitsToRead, F);
136     }
137     for (const Record::Base &B : R->bases()) {
138       Pointer Elem = P.atField(B.Offset);
139       CharUnits ByteOffset =
140           Layout.getBaseClassOffset(cast<CXXRecordDecl>(B.Decl));
141       Bits BitOffset = Offset + Bits(Ctx.getASTContext().toBits(ByteOffset));
142       Ok = Ok && enumerateData(Elem, Ctx, BitOffset, BitsToRead, F);
143       // FIXME: We should only (need to) do this when bitcasting OUT of the
144       // buffer, not when copying data into it.
145       if (Ok)
146         Elem.initialize();
147     }
148 
149     return Ok;
150   }
151 
152   llvm_unreachable("Unhandled data type");
153 }
154 
155 static bool enumeratePointerFields(const Pointer &P, const Context &Ctx,
156                                    Bits BitsToRead, DataFunc F) {
157   return enumerateData(P, Ctx, Bits::zero(), BitsToRead, F);
158 }
159 
160 //  This function is constexpr if and only if To, From, and the types of
161 //  all subobjects of To and From are types T such that...
162 //  (3.1) - is_union_v<T> is false;
163 //  (3.2) - is_pointer_v<T> is false;
164 //  (3.3) - is_member_pointer_v<T> is false;
165 //  (3.4) - is_volatile_v<T> is false; and
166 //  (3.5) - T has no non-static data members of reference type
167 //
168 // NOTE: This is a version of checkBitCastConstexprEligibilityType() in
169 // ExprConstant.cpp.
170 static bool CheckBitcastType(InterpState &S, CodePtr OpPC, QualType T,
171                              bool IsToType) {
172   enum {
173     E_Union = 0,
174     E_Pointer,
175     E_MemberPointer,
176     E_Volatile,
177     E_Reference,
178   };
179   enum { C_Member, C_Base };
180 
181   auto diag = [&](int Reason) -> bool {
182     const Expr *E = S.Current->getExpr(OpPC);
183     S.FFDiag(E, diag::note_constexpr_bit_cast_invalid_type)
184         << static_cast<int>(IsToType) << (Reason == E_Reference) << Reason
185         << E->getSourceRange();
186     return false;
187   };
188   auto note = [&](int Construct, QualType NoteType, SourceRange NoteRange) {
189     S.Note(NoteRange.getBegin(), diag::note_constexpr_bit_cast_invalid_subtype)
190         << NoteType << Construct << T.getUnqualifiedType() << NoteRange;
191     return false;
192   };
193 
194   T = T.getCanonicalType();
195 
196   if (T->isUnionType())
197     return diag(E_Union);
198   if (T->isPointerType())
199     return diag(E_Pointer);
200   if (T->isMemberPointerType())
201     return diag(E_MemberPointer);
202   if (T.isVolatileQualified())
203     return diag(E_Volatile);
204 
205   if (const RecordDecl *RD = T->getAsRecordDecl()) {
206     if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
207       for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
208         if (!CheckBitcastType(S, OpPC, BS.getType(), IsToType))
209           return note(C_Base, BS.getType(), BS.getBeginLoc());
210       }
211     }
212     for (const FieldDecl *FD : RD->fields()) {
213       if (FD->getType()->isReferenceType())
214         return diag(E_Reference);
215       if (!CheckBitcastType(S, OpPC, FD->getType(), IsToType))
216         return note(C_Member, FD->getType(), FD->getSourceRange());
217     }
218   }
219 
220   if (T->isArrayType() &&
221       !CheckBitcastType(S, OpPC, S.getASTContext().getBaseElementType(T),
222                         IsToType))
223     return false;
224 
225   return true;
226 }
227 
228 static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
229                                 BitcastBuffer &Buffer, bool ReturnOnUninit) {
230   const ASTContext &ASTCtx = Ctx.getASTContext();
231   Endian TargetEndianness =
232       ASTCtx.getTargetInfo().isLittleEndian() ? Endian::Little : Endian::Big;
233 
234   return enumeratePointerFields(
235       FromPtr, Ctx, Buffer.size(),
236       [&](const Pointer &P, PrimType T, Bits BitOffset,
237           bool PackedBools) -> bool {
238         CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
239         Bits BitWidth = Bits(ASTCtx.toBits(ObjectReprChars));
240         Bits FullBitWidth = BitWidth;
241 
242         if (const FieldDecl *FD = P.getField(); FD && FD->isBitField()) {
243           BitWidth = Bits(std::min(FD->getBitWidthValue(ASTCtx),
244                                    (unsigned)FullBitWidth.getQuantity()));
245         } else if (T == PT_Bool && PackedBools)
246           BitWidth = Bits(1);
247 
248         if (BitWidth.isZero())
249           return true;
250 
251         if (!P.isInitialized()) {
252           assert(false && "Implement uninitialized value tracking");
253           return ReturnOnUninit;
254         }
255 
256         assert(P.isInitialized());
257         if (T == PT_Ptr) {
258           assert(P.getType()->isNullPtrType());
259           // Clang treats nullptr_t has having NO bits in its value
260           // representation. So, we accept it here and leave its bits
261           // uninitialized.
262           return true;
263         }
264 
265         auto Buff =
266             std::make_unique<std::byte[]>(ObjectReprChars.getQuantity());
267         // Work around floating point types that contain unused padding bytes.
268         // This is really just `long double` on x86, which is the only
269         // fundamental type with padding bytes.
270         if (T == PT_Float) {
271           const Floating &F = P.deref<Floating>();
272           Bits NumBits = Bits(
273               llvm::APFloatBase::getSizeInBits(F.getAPFloat().getSemantics()));
274           assert(NumBits.isFullByte());
275           assert(NumBits.getQuantity() <= FullBitWidth.getQuantity());
276           F.bitcastToMemory(Buff.get());
277           // Now, only (maybe) swap the actual size of the float, excluding the
278           // padding bits.
279           if (llvm::sys::IsBigEndianHost)
280             swapBytes(Buff.get(), NumBits.roundToBytes());
281 
282         } else {
283           BITCAST_TYPE_SWITCH(T, { P.deref<T>().bitcastToMemory(Buff.get()); });
284 
285           if (llvm::sys::IsBigEndianHost)
286             swapBytes(Buff.get(), FullBitWidth.roundToBytes());
287         }
288 
289         Buffer.pushData(Buff.get(), BitOffset, BitWidth, TargetEndianness);
290         Buffer.markInitialized(BitOffset, BitWidth);
291         return true;
292       });
293 }
294 
295 bool clang::interp::DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
296                               std::byte *Buff, size_t BuffSize,
297                               bool &HasIndeterminateBits) {
298   assert(Ptr.isLive());
299   assert(Ptr.isBlockPointer());
300   assert(Buff);
301 
302   Bits BitSize = Bytes(BuffSize).toBits();
303   BitcastBuffer Buffer(BitSize);
304   if (!CheckBitcastType(S, OpPC, Ptr.getType(), /*IsToType=*/false))
305     return false;
306 
307   bool Success = readPointerToBuffer(S.getContext(), Ptr, Buffer,
308                                      /*ReturnOnUninit=*/false);
309   HasIndeterminateBits = !Buffer.allInitialized();
310 
311   const ASTContext &ASTCtx = S.getASTContext();
312   Endian TargetEndianness =
313       ASTCtx.getTargetInfo().isLittleEndian() ? Endian::Little : Endian::Big;
314   auto B = Buffer.copyBits(Bits::zero(), BitSize, BitSize, TargetEndianness);
315 
316   std::memcpy(Buff, B.get(), BuffSize);
317 
318   if (llvm::sys::IsBigEndianHost)
319     swapBytes(Buff, BuffSize);
320 
321   return Success;
322 }
323 bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
324                                  const Pointer &FromPtr, Pointer &ToPtr) {
325   const ASTContext &ASTCtx = S.getASTContext();
326   CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(ToPtr.getType());
327 
328   return DoBitCastPtr(S, OpPC, FromPtr, ToPtr, ObjectReprChars.getQuantity());
329 }
330 
331 bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
332                                  const Pointer &FromPtr, Pointer &ToPtr,
333                                  size_t Size) {
334   assert(FromPtr.isLive());
335   assert(FromPtr.isBlockPointer());
336   assert(ToPtr.isBlockPointer());
337 
338   QualType FromType = FromPtr.getType();
339   QualType ToType = ToPtr.getType();
340 
341   if (!CheckBitcastType(S, OpPC, ToType, /*IsToType=*/true))
342     return false;
343   if (!CheckBitcastType(S, OpPC, FromType, /*IsToType=*/false))
344     return false;
345 
346   const ASTContext &ASTCtx = S.getASTContext();
347   BitcastBuffer Buffer(Bytes(Size).toBits());
348   readPointerToBuffer(S.getContext(), FromPtr, Buffer,
349                       /*ReturnOnUninit=*/false);
350 
351   // Now read the values out of the buffer again and into ToPtr.
352   Endian TargetEndianness =
353       ASTCtx.getTargetInfo().isLittleEndian() ? Endian::Little : Endian::Big;
354   bool Success = enumeratePointerFields(
355       ToPtr, S.getContext(), Buffer.size(),
356       [&](const Pointer &P, PrimType T, Bits BitOffset,
357           bool PackedBools) -> bool {
358         CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
359         Bits FullBitWidth = Bits(ASTCtx.toBits(ObjectReprChars));
360         if (T == PT_Float) {
361           const auto &Semantics = ASTCtx.getFloatTypeSemantics(P.getType());
362           Bits NumBits = Bits(llvm::APFloatBase::getSizeInBits(Semantics));
363           assert(NumBits.isFullByte());
364           assert(NumBits.getQuantity() <= FullBitWidth.getQuantity());
365           auto M = Buffer.copyBits(BitOffset, NumBits, FullBitWidth,
366                                    TargetEndianness);
367 
368           if (llvm::sys::IsBigEndianHost)
369             swapBytes(M.get(), NumBits.roundToBytes());
370 
371           P.deref<Floating>() = Floating::bitcastFromMemory(M.get(), Semantics);
372           P.initialize();
373           return true;
374         }
375 
376         Bits BitWidth;
377         if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
378           BitWidth = Bits(std::min(FD->getBitWidthValue(ASTCtx),
379                                    (unsigned)FullBitWidth.getQuantity()));
380         else if (T == PT_Bool && PackedBools)
381           BitWidth = Bits(1);
382         else
383           BitWidth = FullBitWidth;
384 
385         auto Memory = Buffer.copyBits(BitOffset, BitWidth, FullBitWidth,
386                                       TargetEndianness);
387         if (llvm::sys::IsBigEndianHost)
388           swapBytes(Memory.get(), FullBitWidth.roundToBytes());
389 
390         BITCAST_TYPE_SWITCH_FIXED_SIZE(T, {
391           if (BitWidth.nonZero())
392             P.deref<T>() = T::bitcastFromMemory(Memory.get(), T::bitWidth())
393                                .truncate(BitWidth.getQuantity());
394           else
395             P.deref<T>() = T::zero();
396         });
397         P.initialize();
398         return true;
399       });
400 
401   return Success;
402 }
403