Skip to content

Commit bf70395

Browse files
committed
[MemoryLocation] Support memcpy_chk in getForArgument.
Similar to 9f9e8ba, add support for memcyp_chk to MemoryLocation::getForArgument. The size argument for memcpy_chk is an upper bound for the size of the pointer argument. memcpy_chk may read/write less than the specified length, if it exceeds the specified max size and aborts. Reviewed By: xbolva00, jdoerfert Differential Revision: https://reviews.llvm.org/D138613
1 parent 34107e2 commit bf70395

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

llvm/lib/Analysis/MemoryLocation.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,17 @@ MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
253253
assert((ArgIdx == 0 || ArgIdx == 1) && "Invalid argument index for str function");
254254
return MemoryLocation::getAfter(Arg, AATags);
255255

256-
case LibFunc_memset_chk: {
256+
case LibFunc_memset_chk:
257257
assert(ArgIdx == 0 && "Invalid argument index for memset_chk");
258+
LLVM_FALLTHROUGH;
259+
case LibFunc_memcpy_chk: {
260+
assert((ArgIdx == 0 || ArgIdx == 1) &&
261+
"Invalid argument index for memcpy_chk");
258262
LocationSize Size = LocationSize::afterPointer();
259263
if (const auto *Len = dyn_cast<ConstantInt>(Call->getArgOperand(2))) {
260-
// memset_chk writes at most Len bytes. It may write less, if Len
261-
// exceeds the specified max size and aborts.
264+
// memset_chk writes at most Len bytes, memcpy_chk reads/writes at most
265+
// Len bytes. They may read/write less, if Len exceeds the specified max
266+
// size and aborts.
262267
Size = LocationSize::upperBound(Len->getZExtValue());
263268
}
264269
return MemoryLocation(Arg, Size, AATags);

llvm/test/Analysis/BasicAA/libfuncs.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ define i8* @test_memcpy_chk_const_size(i8* noalias %a, i8* noalias %b, i64 %n) {
323323
; CHECK: Just Mod: Ptr: i8* %a <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
324324
; CHECK-NEXT: Just Mod: Ptr: i8* %res <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
325325
; CHECK-NEXT: Just Mod: Ptr: i8* %a.gep.1 <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
326-
; CHECK-NEXT: Just Mod: Ptr: i8* %a.gep.5 <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
326+
; CHECK-NEXT: NoModRef: Ptr: i8* %a.gep.5 <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
327327
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.1 <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
328-
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.5 <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
328+
; CHECK-NEXT: NoModRef: Ptr: i8* %b.gep.5 <-> %res = tail call i8* @__memcpy_chk(i8* %a, i8* %b, i64 4, i64 %n)
329329
;
330330
entry:
331331
load i8, i8* %a

0 commit comments

Comments
 (0)