Skip to content

Commit abc2703

Browse files
authored
[clang][bytecode] Pass __builtin_memcpy size along (#118649)
To DoBitCastPtr, so we know how many bytes we want to read.
1 parent 3e0e1c1 commit abc2703

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
18311831
if (DestPtr.isDummy() || SrcPtr.isDummy())
18321832
return false;
18331833

1834-
if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
1834+
if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr, Size.getZExtValue()))
18351835
return false;
18361836

18371837
S.Stk.push<Pointer>(DestPtr);

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,17 @@ bool clang::interp::DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
315315

316316
return Success;
317317
}
318-
319318
bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
320319
const Pointer &FromPtr, Pointer &ToPtr) {
320+
const ASTContext &ASTCtx = S.getASTContext();
321+
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(ToPtr.getType());
322+
323+
return DoBitCastPtr(S, OpPC, FromPtr, ToPtr, ObjectReprChars.getQuantity());
324+
}
325+
326+
bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
327+
const Pointer &FromPtr, Pointer &ToPtr,
328+
size_t Size) {
321329
assert(FromPtr.isLive());
322330
assert(FromPtr.isBlockPointer());
323331
assert(ToPtr.isBlockPointer());
@@ -331,9 +339,7 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
331339
return false;
332340

333341
const ASTContext &ASTCtx = S.getASTContext();
334-
335-
CharUnits ObjectReprChars = ASTCtx.getTypeSizeInChars(ToType);
336-
BitcastBuffer Buffer(Bits(ASTCtx.toBits(ObjectReprChars)));
342+
BitcastBuffer Buffer(Bytes(Size).toBits());
337343
readPointerToBuffer(S.getContext(), FromPtr, Buffer,
338344
/*ReturnOnUninit=*/false);
339345

clang/lib/AST/ByteCode/InterpBuiltinBitCast.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ bool DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
2121
std::byte *Buff, size_t BuffSize, bool &HasIndeterminateBits);
2222
bool DoBitCastPtr(InterpState &S, CodePtr OpPC, const Pointer &FromPtr,
2323
Pointer &ToPtr);
24+
bool DoBitCastPtr(InterpState &S, CodePtr OpPC, const Pointer &FromPtr,
25+
Pointer &ToPtr, size_t Size);
2426

2527
} // namespace interp
2628
} // namespace clang

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,13 @@ namespace BuiltinMemcpy {
11511151
}
11521152
static_assert(simple() == 12);
11531153

1154+
constexpr bool arrayMemcpy() {
1155+
char src[] = "abc";
1156+
char dst[4] = {};
1157+
__builtin_memcpy(dst, src, 4);
1158+
return dst[0] == 'a' && dst[1] == 'b' && dst[2] == 'c' && dst[3] == '\0';
1159+
}
1160+
static_assert(arrayMemcpy());
11541161

11551162
extern struct Incomplete incomplete;
11561163
constexpr struct Incomplete *null_incomplete = 0;

0 commit comments

Comments
 (0)