Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
[clang] Make guard(nocf) attribute available only for Windows
Browse files Browse the repository at this point in the history
Control Flow Guard is only supported on Windows target, therefore there
is no point to make it an accepted attribute for other targets.

Reviewed By: rnk, aaron.ballman

Differential Revision: https://reviews.llvm.org/D132661
  • Loading branch information
alvinhochun authored and mstorsjo committed Aug 29, 2022
1 parent a845d8f commit 00d648b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Attribute Changes in Clang
``[[clang::guard(nocf)]]``, which is equivalent to ``__declspec(guard(nocf))``
when using the MSVC environment. This is to support enabling Windows Control
Flow Guard checks with the ability to disable them for specific functions when
using the MinGW environment.
using the MinGW environment. This attribute is only available for Windows
targets.

Windows Support
---------------
Expand Down
5 changes: 4 additions & 1 deletion clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
def TargetX86 : TargetArch<["x86"]>;
def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
def TargetWindows : TargetSpec {
let OSes = ["Win32"];
}
def TargetHasDLLImportExport : TargetSpec {
let CustomCode = [{ Target.getTriple().hasDLLImportExport() }];
}
Expand Down Expand Up @@ -3494,7 +3497,7 @@ def MSAllocator : InheritableAttr {
let Documentation = [MSAllocatorDocs];
}

def CFGuard : InheritableAttr {
def CFGuard : InheritableAttr, TargetSpecificAttr<TargetWindows> {
// Currently only the __declspec(guard(nocf)) modifier is supported. In future
// we might also want to support __declspec(guard(suppress)).
let Spellings = [Declspec<"guard">, Clang<"guard">];
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Sema/attr-guard_nocf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// RUN: %clang_cc1 -triple %ms_abi_triple -fms-extensions -verify -std=c++11 -fsyntax-only -x c++ %s
// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -verify -fsyntax-only %s
// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -verify -std=c++11 -fsyntax-only -x c++ %s
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -verify -fsyntax-only %s
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -verify -std=c++11 -fsyntax-only -x c++ %s

// The x86_64-w64-windows-gnu version tests mingw target, which relies on
// __declspec(...) being defined as __attribute__((...)) by compiler built-in.

#if defined(_WIN32)

// Function definition.
__declspec(guard(nocf)) void testGuardNoCF(void) { // no warning
}
Expand Down Expand Up @@ -35,3 +39,9 @@ __declspec(guard(nocf, nocf)) void testGuardNoCFTooManyParams(void) { // expecte
// 'guard' Attribute argument must be a supported identifier.
__declspec(guard(cf)) void testGuardNoCFInvalidParam(void) { // expected-warning {{'guard' attribute argument not supported: 'cf'}}
}

#else

__attribute((guard(nocf))) void testGNUStyleGuardNoCF(void) {} // expected-warning {{unknown attribute 'guard' ignored}}

#endif

0 comments on commit 00d648b

Please sign in to comment.