Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Dec 9, 2024

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 9, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/119209.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+15)
  • (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+10)
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}}
 }

@tbaederr tbaederr merged commit 6168739 into llvm:main Dec 9, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants