Skip to content

Commit

Permalink
[Analysis] improve function signature checking for snprintf
Browse files Browse the repository at this point in the history
The check for size_t parameter 1 was already here for snprintf_chk,
but it wasn't applied to regular snprintf. This could lead to
mismatching and eventually crashing as shown in:
https://llvm.org/PR50885

(cherry picked from commit 7f55557)
  • Loading branch information
rotateright authored and tstellar committed Aug 3, 2021
1 parent 60c388a commit d6974c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions llvm/lib/Analysis/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,10 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
FTy.getReturnType()->isIntegerTy(32);

case LibFunc_snprintf:
return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(2)->isPointerTy() &&
FTy.getReturnType()->isIntegerTy(32));
return NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(1)) &&
FTy.getParamType(2)->isPointerTy() &&
FTy.getReturnType()->isIntegerTy(32);

case LibFunc_snprintf_chk:
return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/Transforms/InstCombine/simplify-libcalls.ll
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ define double @fake_ldexp_16(i16 %x) {
ret double %z
}

; PR50885 - this would crash in ValueTracking.

declare i32 @snprintf(i8*, double, i32*)

define i32 @fake_snprintf(i32 %buf, double %len, i32 * %str) {
; CHECK-LABEL: @fake_snprintf(
; CHECK-NEXT: [[CALL:%.*]] = call i32 @snprintf(i8* undef, double [[LEN:%.*]], i32* [[STR:%.*]])
; CHECK-NEXT: ret i32 [[CALL]]
;
%call = call i32 @snprintf(i8* undef, double %len, i32* %str)
ret i32 %call
}

attributes #0 = { nobuiltin }
attributes #1 = { builtin }

0 comments on commit d6974c0

Please sign in to comment.