[clang] [clang][bytecode] Reject memcpy sizes with element size remainder (PR #119209)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 9 05:25:35 PST 2024


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

None

>From 0778f47716355e0916727c0bdb098e1f61b99bd9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 9 Dec 2024 14:23:51 +0100
Subject: [PATCH] [clang][bytecode] Reject memcpy sizes with element size
 remainder

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp      | 15 +++++++++++++++
 clang/test/AST/ByteCode/builtin-functions.cpp | 10 ++++++++++
 2 files changed, 25 insertions(+)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 2469648d68edb1..bd284e3ddd2cb5 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1846,6 +1846,21 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     return false;
   }
 
+  QualType ElemType;
+  if (SrcPtr.getFieldDesc()->isArray())
+    ElemType = SrcPtr.getFieldDesc()->getElemQualType();
+  else
+    ElemType = SrcPtr.getType();
+
+  unsigned ElemSize =
+      S.getASTContext().getTypeSizeInChars(ElemType).getQuantity();
+  if (Size.urem(ElemSize) != 0) {
+    S.FFDiag(S.Current->getSource(OpPC),
+             diag::note_constexpr_memcpy_unsupported)
+        << Move << /*IsWchar=*/false << 0 << ElemType << Size << ElemSize;
+    return false;
+  }
+
   // As a last resort, reject dummy pointers.
   if (DestPtr.isDummy() || SrcPtr.isDummy())
     return false;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 4c21496d3972c9..1e0132b8d9bb75 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1188,4 +1188,14 @@ namespace BuiltinMemcpy {
     return b;
   }
   static_assert(simpleMove() == 12);
+
+  constexpr int memcpyTypeRem() { // ref-error {{never produces a constant expression}}
+    int a = 12;
+    int b = 0;
+    __builtin_memmove(&b, &a, 1); // both-note {{'memmove' not supported: size to copy (1) is not a multiple of size of element type 'int'}} \
+                                  // ref-note {{not supported}}
+    return b;
+  }
+  static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \
+                                        // both-note {{in call to}}
 }



More information about the cfe-commits mailing list