From e0fc1a80cba8b91e3943f3287e7dcf68c6bb9b7f Mon Sep 17 00:00:00 2001 From: Josh Magee Date: Tue, 11 Feb 2014 01:35:14 +0000 Subject: [PATCH] [stackprotector] Add command line option -fstack-protector-strong This option has the following effects: * It adds the sspstrong IR attribute to each function within the CU. * It defines the macro __SSP_STRONG__ with the value of 2. Differential Revision: http://llvm-reviews.chandlerc.com/D2717 llvm-svn: 201120 --- clang/include/clang/Basic/LangOptions.h | 2 +- clang/include/clang/Driver/Options.td | 11 ++++++++--- clang/include/clang/Driver/ToolChain.h | 2 +- clang/lib/CodeGen/CodeGenModule.cpp | 2 ++ clang/lib/Driver/Tools.cpp | 8 ++++++-- clang/lib/Frontend/CompilerInvocation.cpp | 3 ++- clang/lib/Frontend/InitPreprocessor.cpp | 4 +++- clang/test/CodeGen/stack-protector.c | 6 +++++- clang/test/Driver/stack-protector.c | 8 ++++++++ 9 files changed, 36 insertions(+), 10 deletions(-) diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index d4e8b4e..7b2b18d 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -58,7 +58,7 @@ public: typedef clang::Visibility Visibility; enum GCMode { NonGC, GCOnly, HybridGC }; - enum StackProtectorMode { SSPOff, SSPOn, SSPReq }; + enum StackProtectorMode { SSPOff, SSPOn, SSPStrong, SSPReq }; enum SignedOverflowBehaviorTy { SOB_Undefined, // Default C standard behavior. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index d860d8f..de31361 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -675,7 +675,8 @@ def fno_show_source_location : Flag<["-"], "fno-show-source-location">, Group, HelpText<"Do not include source location information with diagnostics">; def fno_spell_checking : Flag<["-"], "fno-spell-checking">, Group, Flags<[CC1Option]>, HelpText<"Disable spell-checking">; -def fno_stack_protector : Flag<["-"], "fno-stack-protector">, Group; +def fno_stack_protector : Flag<["-"], "fno-stack-protector">, Group, + HelpText<"Disable the use of stack protectors">; def fno_strict_aliasing : Flag<["-"], "fno-strict-aliasing">, Group; def fstruct_path_tbaa : Flag<["-"], "fstruct-path-tbaa">, Group; def fno_struct_path_tbaa : Flag<["-"], "fno-struct-path-tbaa">, Group; @@ -773,8 +774,12 @@ def fsigned_char : Flag<["-"], "fsigned-char">, Group; def fno_signed_char : Flag<["-"], "fno-signed-char">, Flags<[CC1Option]>, Group, HelpText<"Char is unsigned">; def fsplit_stack : Flag<["-"], "fsplit-stack">, Group; -def fstack_protector_all : Flag<["-"], "fstack-protector-all">, Group; -def fstack_protector : Flag<["-"], "fstack-protector">, Group; +def fstack_protector_all : Flag<["-"], "fstack-protector-all">, Group, + HelpText<"Force the usage of stack protectors for all functions">; +def fstack_protector_strong : Flag<["-"], "fstack-protector-strong">, Group, + HelpText<"Use a strong heuristic to apply stack protectors to functions">; +def fstack_protector : Flag<["-"], "fstack-protector">, Group, + HelpText<"Enable stack protectors for functions potentially vulnerable to stack smashing">; def fstandalone_debug : Flag<["-"], "fstandalone-debug">, Group, Flags<[CC1Option]>, HelpText<"Emit full debug info for all types used by the program">; def fno_standalone_debug : Flag<["-"], "fno-standalone-debug">, Group, Flags<[CC1Option]>, diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h index ceb1c76..e775f63 100644 --- a/clang/include/clang/Driver/ToolChain.h +++ b/clang/include/clang/Driver/ToolChain.h @@ -196,7 +196,7 @@ public: virtual bool UseObjCMixedDispatch() const { return false; } /// GetDefaultStackProtectorLevel - Get the default stack protector level for - /// this tool chain (0=off, 1=on, 2=all). + /// this tool chain (0=off, 1=on, 2=strong, 3=all). virtual unsigned GetDefaultStackProtectorLevel(bool KernelOrKext) const { return 0; } diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index c0c1219..7b1eab9 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -651,6 +651,8 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, if (LangOpts.getStackProtector() == LangOptions::SSPOn) B.addAttribute(llvm::Attribute::StackProtect); + else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) + B.addAttribute(llvm::Attribute::StackProtectStrong); else if (LangOpts.getStackProtector() == LangOptions::SSPReq) B.addAttribute(llvm::Attribute::StackProtectReq); diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index d2bb9ba..5792f42 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -10,6 +10,7 @@ #include "Tools.h" #include "InputInfo.h" #include "ToolChains.h" +#include "clang/Basic/LangOptions.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/Version.h" #include "clang/Driver/Action.h" @@ -3114,11 +3115,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, unsigned StackProtectorLevel = 0; if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, options::OPT_fstack_protector_all, + options::OPT_fstack_protector_strong, options::OPT_fstack_protector)) { if (A->getOption().matches(options::OPT_fstack_protector)) - StackProtectorLevel = 1; + StackProtectorLevel = LangOptions::SSPOn; + else if (A->getOption().matches(options::OPT_fstack_protector_strong)) + StackProtectorLevel = LangOptions::SSPStrong; else if (A->getOption().matches(options::OPT_fstack_protector_all)) - StackProtectorLevel = 2; + StackProtectorLevel = LangOptions::SSPReq; } else { StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel(KernelOrKext); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 3c75dd5..034730f 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1435,7 +1435,8 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK, break; case 0: Opts.setStackProtector(LangOptions::SSPOff); break; case 1: Opts.setStackProtector(LangOptions::SSPOn); break; - case 2: Opts.setStackProtector(LangOptions::SSPReq); break; + case 2: Opts.setStackProtector(LangOptions::SSPStrong); break; + case 3: Opts.setStackProtector(LangOptions::SSPReq); break; } // Parse -fsanitize= arguments. diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 7b3166d..f975c9f 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -695,8 +695,10 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.getStackProtector() == LangOptions::SSPOn) Builder.defineMacro("__SSP__"); + else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) + Builder.defineMacro("__SSP_STRONG__", "2"); else if (LangOpts.getStackProtector() == LangOptions::SSPReq) - Builder.defineMacro("__SSP_ALL__", "2"); + Builder.defineMacro("__SSP_ALL__", "3"); if (FEOpts.ProgramAction == frontend::RewriteObjC) Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))"); diff --git a/clang/test/CodeGen/stack-protector.c b/clang/test/CodeGen/stack-protector.c index e47e5b3..2fb9b2c 100644 --- a/clang/test/CodeGen/stack-protector.c +++ b/clang/test/CodeGen/stack-protector.c @@ -2,7 +2,9 @@ // NOSSP: define void @test1(i8* %msg) #0 { // RUN: %clang_cc1 -emit-llvm -o - %s -stack-protector 1 | FileCheck -check-prefix=WITHSSP %s // WITHSSP: define void @test1(i8* %msg) #0 { -// RUN: %clang_cc1 -emit-llvm -o - %s -stack-protector 2 | FileCheck -check-prefix=SSPREQ %s +// RUN: %clang_cc1 -emit-llvm -o - %s -stack-protector 2 | FileCheck -check-prefix=SSPSTRONG %s +// SSPSTRONG: define void @test1(i8* %msg) #0 { +// RUN: %clang_cc1 -emit-llvm -o - %s -stack-protector 3 | FileCheck -check-prefix=SSPREQ %s // SSPREQ: define void @test1(i8* %msg) #0 { typedef __SIZE_TYPE__ size_t; @@ -21,4 +23,6 @@ void test1(const char *msg) { // WITHSSP: attributes #{{.*}} = { nounwind ssp{{.*}} } +// SSPSTRONG: attributes #{{.*}} = { nounwind sspstrong{{.*}} } + // SSPREQ: attributes #{{.*}} = { nounwind sspreq{{.*}} } diff --git a/clang/test/Driver/stack-protector.c b/clang/test/Driver/stack-protector.c index 2eb0f53..e6212acf 100644 --- a/clang/test/Driver/stack-protector.c +++ b/clang/test/Driver/stack-protector.c @@ -15,3 +15,11 @@ // RUN: %clang -target i386-pc-openbsd -fno-stack-protector -### %s 2>&1 | FileCheck %s -check-prefix=OPENBSD_OFF // OPENBSD_OFF-NOT: "-stack-protector" + +// RUN: %clang -fstack-protector-strong -### %s 2>&1 | FileCheck %s -check-prefix=SSP-STRONG +// SSP-STRONG: "-stack-protector" "2" +// SSP-STRONG-NOT: "-stack-protector-buffer-size" + +// RUN: %clang -fstack-protector-all -### %s 2>&1 | FileCheck %s -check-prefix=SSP-ALL +// SSP-ALL: "-stack-protector" "3" +// SSP-ALL-NOT: "-stack-protector-buffer-size" -- 2.7.4