-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][bytecode] Remove base casts before doing memcpy #137754
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We have to copy the entire thing, not just one of the bases.
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesWe have to copy the entire thing, not just one of the bases. Full diff: https://github.com/llvm/llvm-project/pull/137754.diff 3 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Disasm.cpp b/clang/lib/AST/ByteCode/Disasm.cpp
index 83b9af9de0796..46c5cb96fa9f4 100644
--- a/clang/lib/AST/ByteCode/Disasm.cpp
+++ b/clang/lib/AST/ByteCode/Disasm.cpp
@@ -422,6 +422,7 @@ LLVM_DUMP_METHOD void InlineDescriptor::dump(llvm::raw_ostream &OS) const {
OS << "IsActive: " << IsActive << "\n";
OS << "InUnion: " << InUnion << "\n";
OS << "IsFieldMutable: " << IsFieldMutable << "\n";
+ OS << "IsArrayElement: " << IsArrayElement << "\n";
OS << "Desc: ";
if (Desc)
Desc->dump(OS);
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 34baae1986c35..55c944e31c0ca 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1849,8 +1849,17 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
// Check for overlapping memory regions.
if (!Move && Pointer::pointToSameBlock(SrcPtr, DestPtr)) {
- unsigned SrcIndex = SrcPtr.getIndex() * SrcPtr.elemSize();
- unsigned DstIndex = DestPtr.getIndex() * DestPtr.elemSize();
+ // Remove base casts.
+ Pointer SrcP = SrcPtr;
+ while (SrcP.isBaseClass())
+ SrcP = SrcP.getBase();
+
+ Pointer DestP = DestPtr;
+ while (DestP.isBaseClass())
+ DestP = DestP.getBase();
+
+ unsigned SrcIndex = SrcP.expand().getIndex() * SrcP.elemSize();
+ unsigned DstIndex = DestP.expand().getIndex() * DestP.elemSize();
unsigned N = Size.getZExtValue();
if ((SrcIndex <= DstIndex && (SrcIndex + N) > DstIndex) ||
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index a4c8ec4856ecc..f879360c5357f 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1383,7 +1383,19 @@ namespace BuiltinMemcpy {
static_assert(type_pun(0x3f800000) == 1.0f); // both-error {{constant}} \
// both-note {{in call}}
-
+ struct Base { int a; };
+ struct Derived : Base { int b; };
+ constexpr int test_derived_to_base(int n) {
+ Derived arr[2] = {1, 2, 3, 4};
+ Base *p = &arr[0];
+ Base *q = &arr[1];
+ __builtin_memcpy(p, q, sizeof(Base) * n); // both-note {{source is not a contiguous array of at least 2 elements of type 'BuiltinMemcpy::Base'}}
+ return arr[0].a * 1000 + arr[0].b * 100 + arr[1].a * 10 + arr[1].b;
+ }
+ static_assert(test_derived_to_base(0) == 1234);
+ static_assert(test_derived_to_base(1) == 3234);
+ static_assert(test_derived_to_base(2) == 3434); // both-error {{constant}} \
+ // both-note {{in call}}
}
namespace Memcmp {
|
gizmondo
pushed a commit
to gizmondo/llvm-project
that referenced
this pull request
Apr 29, 2025
We have to copy the entire thing, not just one of the bases.
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
We have to copy the entire thing, not just one of the bases.
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
We have to copy the entire thing, not just one of the bases.
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
We have to copy the entire thing, not just one of the bases.
GeorgeARM
pushed a commit
to GeorgeARM/llvm-project
that referenced
this pull request
May 7, 2025
We have to copy the entire thing, not just one of the bases.
Ankur-0429
pushed a commit
to Ankur-0429/llvm-project
that referenced
this pull request
May 9, 2025
We have to copy the entire thing, not just one of the bases.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:bytecode
Issues for the clang bytecode constexpr interpreter
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We have to copy the entire thing, not just one of the bases.