Skip to content

[clang][bytecode] Reject memcpy dummy pointers after null check #118460

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

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Dec 3, 2024

To match the diagnostic output of the current interpreter.

To match the diagnostic output of the current interpreter.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 3, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

To match the diagnostic output of the current interpreter.


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+4-3)
  • (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+4)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index c5473322ecb280..b788656f9484fc 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1813,9 +1813,6 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
 
   bool Move = (ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove);
 
-  if (DestPtr.isDummy() || SrcPtr.isDummy())
-    return false;
-
   // If the size is zero, we treat this as always being a valid no-op.
   if (Size.isZero()) {
     S.Stk.push<Pointer>(DestPtr);
@@ -1830,6 +1827,10 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     return false;
   }
 
+  // As a last resort, reject dummy pointers.
+  if (DestPtr.isDummy() || SrcPtr.isDummy())
+    return false;
+
   if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
     return false;
 
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 211ca6e164cbfb..b951c04dde5980 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1169,6 +1169,10 @@ namespace BuiltinMemcpy {
   static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
                                                                                       // both-note {{source of 'memcpy' is nullptr}}
 
+  wchar_t global;
+  constexpr wchar_t *null = 0;
+  static_assert(__builtin_memcpy(&global, null, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
+                                                                   // both-note {{source of 'memcpy' is nullptr}}
 
   constexpr int simpleMove() {
     int a = 12;

@tbaederr tbaederr merged commit 3f39c5d into llvm:main Dec 3, 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