[clang] 8dc7f75 - [clang][bytecode] Always check the source pointer in `Memcpy()` (#239563)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 05:52:50 PST 2026


Author: Timm Baeder
Date: 2026-11-06T14:52:45+01:00
New Revision: 8dc7f75fd31fb2fb50c879ee7336e4b3d24df63a

URL: https://github.com/llvm/llvm-project/commit/8dc7f75fd31fb2fb50c879ee7336e4b3d24df63a
DIFF: https://github.com/llvm/llvm-project/commit/8dc7f75fd31fb2fb50c879ee7336e4b3d24df63a.diff

LOG: [clang][bytecode] Always check the source pointer in `Memcpy()` (#239563)

Loading from an uninitialized pointer will otherwise cause us problems
later.
Make empty record fields be initialized by default, so we don't diagnose
those. Also add the initialization state to `Pointer::operator<<`.

This fixes `SemaCXX/ext-int-vector.cpp`.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Descriptor.cpp
    clang/lib/AST/ByteCode/Interp.cpp
    clang/lib/AST/ByteCode/Pointer.h
    clang/lib/AST/ByteCode/Record.h
    clang/test/SemaCXX/ext-int-vector.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 16baad40fdee7..67e09cb004a88 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -100,7 +100,8 @@ static void initField(std::byte *Ptr, bool IsConst, bool IsMutable,
   auto *Desc = reinterpret_cast<InlineDescriptor *>(Ptr + FieldOffset) - 1;
   Desc->Offset = FieldOffset;
   Desc->Desc = D;
-  Desc->IsInitialized = D->IsArray;
+  Desc->IsInitialized =
+      D->IsArray || (D->isRecord() && D->ElemRecord->isEmpty());
   Desc->IsBase = false;
   Desc->IsActive = IsActive && !IsUnionField;
   Desc->InUnion = InUnion;

diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index d8e90666762db..03088ec013f14 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -3723,12 +3723,8 @@ bool Memcpy(InterpState &S, CodePtr OpPC) {
   if (!Dest.isBlockPointer())
     return false;
 
-  if ((Src.getRecord() && Src.getRecord()->isUnion() &&
-       !Src.getRecord()->isAnonymousUnion()) ||
-      Src.inUnion()) {
-    if (!CheckLoad(S, OpPC, Src))
-      return false;
-  }
+  if (!CheckLoad(S, OpPC, Src))
+    return false;
 
   return DoMemcpy(S, OpPC, Src, Dest);
 }

diff  --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 91e04e12e32e6..d9123a7658d10 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -1413,6 +1413,10 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
     OS << " dead";
   if (P.isBlockPointer() && P.isBaseClass())
     OS << " base-class";
+  if (!P.isInitialized()) {
+    ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
+    OS << " uninitialized";
+  }
   return OS;
 }
 

diff  --git a/clang/lib/AST/ByteCode/Record.h b/clang/lib/AST/ByteCode/Record.h
index 838643335b3b9..6984c4957b0a2 100644
--- a/clang/lib/AST/ByteCode/Record.h
+++ b/clang/lib/AST/ByteCode/Record.h
@@ -83,7 +83,10 @@ class Record final {
   }
   /// If this record (or any of its bases) contains a field of type PT_Ptr.
   bool hasPtrField() const { return HasPtrField; }
-
+  /// Whether this record has no fields, no bases and no virtual bases.
+  bool isEmpty() const {
+    return Fields.empty() && Bases.empty() && VirtualBases.empty();
+  }
   /// Returns true for anonymous unions and records
   /// with no destructor or for those with a trivial destructor.
   bool hasTrivialDtor() const;

diff  --git a/clang/test/SemaCXX/ext-int-vector.cpp b/clang/test/SemaCXX/ext-int-vector.cpp
index cdef72b389100..4e7fc1ea67701 100644
--- a/clang/test/SemaCXX/ext-int-vector.cpp
+++ b/clang/test/SemaCXX/ext-int-vector.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-gnu %s
+// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-gnu %s -fexperimental-new-constant-interpreter
 
 namespace GH200665 {
 typedef _BitInt(2) ExtVec2x4 __attribute__((ext_vector_type(4)));


        


More information about the cfe-commits mailing list