Skip to content

[clang][bytecode] Pass __builtin_memcpy size along #118649

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 5, 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
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
if (DestPtr.isDummy() || SrcPtr.isDummy())
return false;

if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr, Size.getZExtValue()))
return false;

S.Stk.push<Pointer>(DestPtr);
Expand Down
14 changes: 10 additions & 4 deletions clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,17 @@ bool clang::interp::DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,

return Success;
}

bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
const Pointer &FromPtr, Pointer &ToPtr) {
const ASTContext &ASTCtx = S.getASTContext();
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(ToPtr.getType());

return DoBitCastPtr(S, OpPC, FromPtr, ToPtr, ObjectReprChars.getQuantity());
}

bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
const Pointer &FromPtr, Pointer &ToPtr,
size_t Size) {
assert(FromPtr.isLive());
assert(FromPtr.isBlockPointer());
assert(ToPtr.isBlockPointer());
Expand All @@ -321,9 +329,7 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
return false;

const ASTContext &ASTCtx = S.getASTContext();

CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(ToType);
BitcastBuffer Buffer(Bits(ASTCtx.toBits(ObjectReprChars)));
BitcastBuffer Buffer(Bytes(Size).toBits());
readPointerToBuffer(S.getContext(), FromPtr, Buffer,
/*ReturnOnUninit=*/false);

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/ByteCode/InterpBuiltinBitCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ bool DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
std::byte *Buff, size_t BuffSize, bool &HasIndeterminateBits);
bool DoBitCastPtr(InterpState &S, CodePtr OpPC, const Pointer &FromPtr,
Pointer &ToPtr);
bool DoBitCastPtr(InterpState &S, CodePtr OpPC, const Pointer &FromPtr,
Pointer &ToPtr, size_t Size);

} // namespace interp
} // namespace clang
Expand Down
7 changes: 7 additions & 0 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,13 @@ namespace BuiltinMemcpy {
}
static_assert(simple() == 12);

constexpr bool arrayMemcpy() {
char src[] = "abc";
char dst[4] = {};
__builtin_memcpy(dst, src, 4);
return dst[0] == 'a' && dst[1] == 'b' && dst[2] == 'c' && dst[3] == '\0';
}
static_assert(arrayMemcpy());

extern struct Incomplete incomplete;
constexpr struct Incomplete *null_incomplete = 0;
Expand Down
Loading