[clang] [clang][bytecode] Fix bitcasting from null pointers (PR #116999)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 08:15:47 PST 2024


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

This looks a little ugly now, I should wait for #116843.

>From c316394e5948539beadd62aab71566a42b76a760 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 20 Nov 2024 17:14:01 +0100
Subject: [PATCH] [clang][bytecode] Fix bitcasting from null pointers

This looks a little ugly now, I should wait for #116843.
---
 clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp | 14 ++++++++++++--
 clang/test/AST/ByteCode/builtin-bit-cast.cpp    |  2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
index 7e8853d3469317..a5de8c3f6dcae3 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
@@ -299,6 +299,9 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
                    llvm::sys::IsLittleEndianHost);
   bool BigEndianTarget = ASTCtx.getTargetInfo().isBigEndian();
 
+  uint64_t PointerSizeInBits =
+      ASTCtx.getTargetInfo().getPointerWidth(LangAS::Default);
+
   return enumeratePointerFields(
       FromPtr, Ctx,
       [&](const Pointer &P, PrimType T, size_t BitOffset,
@@ -310,8 +313,15 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
 
         assert(P.isInitialized());
         // nullptr_t is a PT_Ptr for us, but it's still not std::is_pointer_v.
-        if (T == PT_Ptr)
-          assert(false && "Implement casting to pointer types");
+        if (T == PT_Ptr) {
+          assert(P.getType()->isNullPtrType());
+          std::byte Zeroes[] = {std::byte{0}, std::byte{0}, std::byte{0},
+                                std::byte{0}, std::byte{0}, std::byte{0},
+                                std::byte{0}, std::byte{0}};
+          assert(PointerSizeInBits <= (8 * 8));
+          Buffer.pushData(Zeroes, PointerSizeInBits, BigEndianTarget);
+          return true;
+        }
 
         CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(P.getType());
         unsigned BitWidth = ASTCtx.toBits(ObjectReprChars);
diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 60e8c3a615c5e6..fbaff4ba226d58 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -135,6 +135,8 @@ namespace simple {
   /// diagnoses it.
   static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // ref-error {{not an integral constant expression}} \
                                                              // ref-note {{indeterminate value can only initialize an object}}
+
+  constexpr int test_from_nullptr_pass = (__builtin_bit_cast(unsigned char[8], nullptr), 0);
 }
 
 namespace Fail {



More information about the cfe-commits mailing list