Skip to content

[clang][bytecode] Add missing __builtin_memcpy checks #135975

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
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,23 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
return false;
}

// Check if we have enough elements to read from and write to/
if (DestElemType->isIncompleteType() ||
DestPtr.getType()->isIncompleteType()) {
QualType DiagType =
DestElemType->isIncompleteType() ? DestElemType : DestPtr.getType();
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_memcpy_incomplete_type)
<< Move << DiagType;
return false;
}

if (!DestElemType.isTriviallyCopyableType(ASTCtx)) {
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_nontrivial)
<< Move << DestElemType;
return false;
}

// Check if we have enough elements to read from and write to.
size_t RemainingDestBytes = RemainingDestElems * DestElemSize;
size_t RemainingSrcBytes = RemainingSrcElems * SrcElemSize;
if (Size.ugt(RemainingDestBytes) || Size.ugt(RemainingSrcBytes)) {
Expand Down
35 changes: 35 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ extern "C" {
extern wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n);
}


constexpr int test_address_of_incomplete_array_type() { // both-error {{never produces a constant expression}}
extern int arr[];
__builtin_memmove(&arr, &arr, 4 * sizeof(arr[0])); // both-note 2{{cannot constant evaluate 'memmove' between objects of incomplete type 'int[]'}}
return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
}
static_assert(test_address_of_incomplete_array_type() == 1234, ""); // both-error {{constant}} \
// both-note {{in call}}


struct NonTrivial {
constexpr NonTrivial() : n(0) {}
constexpr NonTrivial(const NonTrivial &) : n(1) {}
int n;
};
constexpr bool test_nontrivial_memcpy() { // ref-error {{never produces a constant}}
NonTrivial arr[3] = {};
__builtin_memcpy(arr, arr + 1, sizeof(NonTrivial)); // both-note {{non-trivially-copyable}} \
// ref-note {{non-trivially-copyable}}
return true;
}
static_assert(test_nontrivial_memcpy()); // both-error {{constant}} \
// both-note {{in call}}

namespace strcmp {
constexpr char kFoobar[6] = {'f','o','o','b','a','r'};
constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'};
Expand Down Expand Up @@ -1349,6 +1373,17 @@ namespace BuiltinMemcpy {
// both-note {{source of 'memcpy' is (void *)123}}
static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
// both-note {{destination of 'memcpy' is (void *)123}}


constexpr float type_pun(const unsigned &n) {
float f = 0.0f;
__builtin_memcpy(&f, &n, 4); // both-note {{cannot constant evaluate 'memcpy' from object of type 'const unsigned int' to object of type 'float'}}
return f;
}
static_assert(type_pun(0x3f800000) == 1.0f); // both-error {{constant}} \
// both-note {{in call}}


}

namespace Memcmp {
Expand Down
Loading