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
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
15 changes: 15 additions & 0 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
Loading