Skip to content

Commit e28e7f2

Browse files
committedJun 15, 2016
Add support to clang-cl driver for /GS switch
Summary: This patch is adding command-line support for the MSVC buffer security check. The buffer security check is turned on with the '/GS' compiler switch. https://msdn.microsoft.com/en-us/library/8dbf701c.aspx The MSVC buffer security check in implemented here: http://reviews.llvm.org/D20346 Reviewers: hans, rnk Subscribers: chrisha, cfe-commits, rnk, hans, thakis Differential Revision: http://reviews.llvm.org/D20347 llvm-svn: 272832
1 parent 389a1e3 commit e28e7f2

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed
 

‎clang/include/clang/Driver/CLCompatOptions.td

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def _SLASH_GR : CLFlag<"GR">, HelpText<"Enable emission of RTTI data">;
7777
def _SLASH_GR_ : CLFlag<"GR-">, HelpText<"Disable emission of RTTI data">;
7878
def _SLASH_GF_ : CLFlag<"GF-">, HelpText<"Disable string pooling">,
7979
Alias<fwritable_strings>;
80+
def _SLASH_GS : CLFlag<"GS">, HelpText<"Enable buffer security check">;
81+
def _SLASH_GS_ : CLFlag<"GS-">, HelpText<"Disable buffer security check">;
8082
def _SLASH_Gs : CLJoined<"Gs">, HelpText<"Set stack probe size">,
8183
Alias<mstack_probe_size>;
8284
def _SLASH_Gy : CLFlag<"Gy">, HelpText<"Put each function in its own section">,
@@ -287,7 +289,6 @@ def _SLASH_Fd : CLIgnoredJoined<"Fd">;
287289
def _SLASH_FC : CLIgnoredFlag<"FC">;
288290
def _SLASH_FS : CLIgnoredFlag<"FS">, HelpText<"Force synchronous PDB writes">;
289291
def _SLASH_GF : CLIgnoredFlag<"GF">;
290-
def _SLASH_GS_ : CLIgnoredFlag<"GS-">;
291292
def _SLASH_kernel_ : CLIgnoredFlag<"kernel-">;
292293
def _SLASH_nologo : CLIgnoredFlag<"nologo">;
293294
def _SLASH_Ob1 : CLIgnoredFlag<"Ob1">;
@@ -329,7 +330,6 @@ def _SLASH_GL : CLFlag<"GL">;
329330
def _SLASH_GL_ : CLFlag<"GL-">;
330331
def _SLASH_Gm : CLFlag<"Gm">;
331332
def _SLASH_Gm_ : CLFlag<"Gm-">;
332-
def _SLASH_GS : CLFlag<"GS">;
333333
def _SLASH_GT : CLFlag<"GT">;
334334
def _SLASH_Guard : CLJoined<"guard:">;
335335
def _SLASH_GZ : CLFlag<"GZ">;

‎clang/lib/Driver/Tools.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -6183,6 +6183,14 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
61836183
/*default=*/false))
61846184
CmdArgs.push_back("-fno-rtti-data");
61856185

6186+
// This controls whether or not we emit stack-protector instrumentation.
6187+
// In MSVC, Buffer Security Check (/GS) is on by default.
6188+
if (Args.hasFlag(options::OPT__SLASH_GS, options::OPT__SLASH_GS_,
6189+
/*default=*/true)) {
6190+
CmdArgs.push_back("-stack-protector");
6191+
CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong)));
6192+
}
6193+
61866194
// Emit CodeView if -Z7 is present.
61876195
*EmitCodeView = Args.hasArg(options::OPT__SLASH_Z7);
61886196
if (*EmitCodeView)
@@ -10022,6 +10030,11 @@ std::unique_ptr<Command> visualstudio::Compiler::GetCommand(
1002210030
if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
1002310031
/*default=*/false))
1002410032
CmdArgs.push_back("/GR-");
10033+
10034+
if (Args.hasFlag(options::OPT__SLASH_GS_, options::OPT__SLASH_GS,
10035+
/*default=*/false))
10036+
CmdArgs.push_back("/GS-");
10037+
1002510038
if (Arg *A = Args.getLastArg(options::OPT_ffunction_sections,
1002610039
options::OPT_fno_function_sections))
1002710040
CmdArgs.push_back(A->getOption().getID() == options::OPT_ffunction_sections

‎clang/test/Driver/cl-fallback.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Note: %s must be preceded by --, otherwise it may be interpreted as a
22
// command-line option, e.g. on Mac where %s is commonly under /Users.
33

4-
// RUN: %clang_cl --target=i686-pc-win32 /fallback /Dfoo=bar /Ubaz /Ifoo /O0 /Ox /GR /GR- /Gy /Gy- \
4+
// RUN: %clang_cl --target=i686-pc-win32 /fallback /Dfoo=bar /Ubaz /Ifoo /O0 /Ox /GR /GR- /GS /GS- /Gy /Gy- \
55
// RUN: /Gw /Gw- /LD /LDd /EHs /EHs- /Zl /MD /MDd /MTd /MT /FImyheader.h /Zi \
66
// RUN: -garbage -moregarbage \
77
// RUN: -### -- %s 2>&1 \
@@ -22,6 +22,7 @@
2222
// CHECK: "/Oy"
2323
// CHECK: "/GF"
2424
// CHECK: "/GR-"
25+
// CHECK: "/GS-"
2526
// CHECK: "/Gy-"
2627
// CHECK: "/Gw-"
2728
// CHECK: "/Z7"
@@ -41,6 +42,10 @@
4142
// GR: cl.exe
4243
// GR: "/GR-"
4344

45+
// RUN: %clang_cl /fallback /GS- -### -- %s 2>&1 | FileCheck -check-prefix=GS %s
46+
// GS: cl.exe
47+
// GS: "/GS-"
48+
4449
// RUN: %clang_cl /fallback /Od -### -- %s 2>&1 | FileCheck -check-prefix=O0 %s
4550
// O0: cl.exe
4651
// O0: "/Od"

‎clang/test/Driver/cl-options.c

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
// RUN: %clang_cl /GR- -### -- %s 2>&1 | FileCheck -check-prefix=GR_ %s
6060
// GR_: -fno-rtti
6161

62+
// Security Buffer Check is on by default.
63+
// RUN: %clang_cl -### -- %s 2>&1 | FileCheck -check-prefix=GS-default %s
64+
// GS-default: "-stack-protector" "2"
65+
66+
// RUN: %clang_cl /GS -### -- %s 2>&1 | FileCheck -check-prefix=GS %s
67+
// GS: "-stack-protector" "2"
68+
69+
// RUN: %clang_cl /GS- -### -- %s 2>&1 | FileCheck -check-prefix=GS_ %s
70+
// GS_-NOT: -stack-protector
71+
6272
// RUN: %clang_cl /Gy -### -- %s 2>&1 | FileCheck -check-prefix=Gy %s
6373
// Gy: -ffunction-sections
6474

0 commit comments

Comments
 (0)
Please sign in to comment.