Skip to content

[clang][bytecode] Check for overlapping memcpy regions #119535

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 11, 2024

Conversation

tbaederr
Copy link
Contributor

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 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+14)
  • (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+15)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index a0de193ec32a2f..4fe17ec01906e9 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1875,6 +1875,20 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     return false;
   }
 
+  // Check for overlapping memory regions.
+  if (!Move && SrcPtr.block() == DestPtr.block()) {
+    unsigned SrcIndex = SrcPtr.getIndex() * SrcPtr.elemSize();
+    unsigned DstIndex = DestPtr.getIndex() * DestPtr.elemSize();
+    unsigned N = Size.getZExtValue();
+
+    if ((SrcIndex <= DstIndex && (SrcIndex + N) > DstIndex) ||
+        (DstIndex <= SrcIndex && (DstIndex + N) > SrcIndex)) {
+      S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_overlap)
+          << /*IsWChar=*/false;
+      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 7dd08cb5fa1c35..ef6faae030a8f2 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1207,4 +1207,19 @@ namespace BuiltinMemcpy {
   }
   static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \
                                         // both-note {{in call to}}
+
+  template<typename T>
+  constexpr T result(T (&arr)[4]) {
+    return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
+  }
+
+  constexpr int test_memcpy(int a, int b, int n) {
+    int arr[4] = {1, 2, 3, 4};
+    __builtin_memcpy(arr + a, arr + b, n); // both-note {{overlapping memory regions}}
+    return result(arr);
+  }
+
+  static_assert(test_memcpy(1, 2, sizeof(int)) == 1334);
+  static_assert(test_memcpy(0, 1, sizeof(int) * 2) == 2334); // both-error {{not an integral constant expression}} \
+                                                             // both-note {{in call}}
 }

@tbaederr tbaederr merged commit 4dde52d into llvm:main Dec 11, 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