From 3037eda1ef960209980985cb46d3d8b074c40a13 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sat, 27 Dec 2008 16:20:53 +0000 Subject: [PATCH] Check that the function prototypes are correct before assuming that the parameters are pointers. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61451 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/IPA/Andersens.cpp | 46 +++++++++++-------- .../Andersens/2008-12-27-BuiltinWrongType.ll | 19 ++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 test/Analysis/Andersens/2008-12-27-BuiltinWrongType.ll diff --git a/lib/Analysis/IPA/Andersens.cpp b/lib/Analysis/IPA/Andersens.cpp index 0cf31abf20f..38b29fed0b3 100644 --- a/lib/Analysis/IPA/Andersens.cpp +++ b/lib/Analysis/IPA/Andersens.cpp @@ -945,30 +945,40 @@ bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) { F->getName() == "llvm.memmove" || F->getName() == "memmove") { - // *Dest = *Src, which requires an artificial graph node to represent the - // constraint. It is broken up into *Dest = temp, temp = *Src - unsigned FirstArg = getNode(CS.getArgument(0)); - unsigned SecondArg = getNode(CS.getArgument(1)); - unsigned TempArg = GraphNodes.size(); - GraphNodes.push_back(Node()); - Constraints.push_back(Constraint(Constraint::Store, - FirstArg, TempArg)); - Constraints.push_back(Constraint(Constraint::Load, - TempArg, SecondArg)); - // In addition, Dest = Src - Constraints.push_back(Constraint(Constraint::Copy, - FirstArg, SecondArg)); - return true; + const FunctionType *FTy = F->getFunctionType(); + if (FTy->getNumParams() > 1 && + isa(FTy->getParamType(0)) && + isa(FTy->getParamType(1))) { + + // *Dest = *Src, which requires an artificial graph node to represent the + // constraint. It is broken up into *Dest = temp, temp = *Src + unsigned FirstArg = getNode(CS.getArgument(0)); + unsigned SecondArg = getNode(CS.getArgument(1)); + unsigned TempArg = GraphNodes.size(); + GraphNodes.push_back(Node()); + Constraints.push_back(Constraint(Constraint::Store, + FirstArg, TempArg)); + Constraints.push_back(Constraint(Constraint::Load, + TempArg, SecondArg)); + // In addition, Dest = Src + Constraints.push_back(Constraint(Constraint::Copy, + FirstArg, SecondArg)); + return true; + } } // Result = Arg0 if (F->getName() == "realloc" || F->getName() == "strchr" || F->getName() == "strrchr" || F->getName() == "strstr" || F->getName() == "strtok") { - Constraints.push_back(Constraint(Constraint::Copy, - getNode(CS.getInstruction()), - getNode(CS.getArgument(0)))); - return true; + const FunctionType *FTy = F->getFunctionType(); + if (FTy->getNumParams() > 0 && + isa(FTy->getParamType(0))) { + Constraints.push_back(Constraint(Constraint::Copy, + getNode(CS.getInstruction()), + getNode(CS.getArgument(0)))); + return true; + } } return false; diff --git a/test/Analysis/Andersens/2008-12-27-BuiltinWrongType.ll b/test/Analysis/Andersens/2008-12-27-BuiltinWrongType.ll new file mode 100644 index 00000000000..5f5da7464db --- /dev/null +++ b/test/Analysis/Andersens/2008-12-27-BuiltinWrongType.ll @@ -0,0 +1,19 @@ +; RUN: llvm-as < %s | opt -anders-aa +; PR3262 + +@.str15 = external global [3 x i8] ; <[3 x i8]*> [#uses=1] + +declare i8* @strtok(...) +declare i8* @memmove(...) + +define void @test1(i8* %want1) nounwind { +entry: + %0 = call i8* (...)* @strtok(i32 0, i8* getelementptr ([3 x i8]* @.str15, i32 0, i32 0)) nounwind ; [#uses=0] + unreachable +} + +define void @test2() nounwind { +entry: + %0 = call i8* (...)* @memmove() + unreachable +} -- 2.34.1