Skip to content

Commit

Permalink
Revert "[clang, llvm] Add __declspec(safebuffers), support it in Code…
Browse files Browse the repository at this point in the history
…View"

Causing:
#57709

This reverts commit ab56719.
  • Loading branch information
sylvestre committed Sep 13, 2022
1 parent 2694234 commit cd20a18
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 105 deletions.
9 changes: 1 addition & 8 deletions clang/include/clang/Basic/Attr.td
Expand Up @@ -2080,19 +2080,12 @@ def NotTailCalled : InheritableAttr {
def : MutualExclusions<[AlwaysInline, NotTailCalled]>;

def NoStackProtector : InheritableAttr {
let Spellings = [Clang<"no_stack_protector">, Declspec<"safebuffers">];
let Spellings = [Clang<"no_stack_protector">];
let Subjects = SubjectList<[Function]>;
let Documentation = [NoStackProtectorDocs];
let SimpleHandler = 1;
}

def StrictGuardStackCheck : InheritableAttr {
let Spellings = [Declspec<"strict_gs_check">];
let Subjects = SubjectList<[Function]>;
let Documentation = [StrictGuardStackCheckDocs];
let SimpleHandler = 1;
}

def NoThrow : InheritableAttr {
let Spellings = [GCC<"nothrow">, Declspec<"nothrow">];
let Subjects = SubjectList<[FunctionLike]>;
Expand Down
24 changes: 1 addition & 23 deletions clang/include/clang/Basic/AttrDocs.td
Expand Up @@ -4453,8 +4453,7 @@ spelled "XYZ" in the `OpenMP 5.1 Standard`_).
def NoStackProtectorDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
Clang supports the ``__attribute__((no_stack_protector))`` and Microsoft style
``__declspec(safebuffers)`` attribute which disables
Clang supports the ``__attribute__((no_stack_protector))`` attribute which disables
the stack protector on the specified function. This attribute is useful for
selectively disabling the stack protector on some functions when building with
``-fstack-protector`` compiler option.
Expand All @@ -4473,27 +4472,6 @@ option.
}];
}

def StrictGuardStackCheckDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
Clang supports the Microsoft style ``__declspec((strict_gs_check))`` attribute
which upgrades the stack protector check from ``-fstack-protector`` to
``-fstack-protector-strong``.

For example, it upgrades the stack protector for the function ``foo`` to
``-fstack-protector-strong`` but function ``bar`` will still be built with the
stack protector with the ``-fstack-protector`` option.

.. code-block:: c

__declspec((strict_gs_check))
int foo(int x); // stack protection will be upgraded for foo.

int bar(int y); // bar can be built with the standard stack protector checks.

}];
}

def NotTailCalledDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
Expand Down
19 changes: 8 additions & 11 deletions clang/lib/CodeGen/CodeGenModule.cpp
Expand Up @@ -1970,17 +1970,14 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
if (!hasUnwindExceptions(LangOpts))
B.addAttribute(llvm::Attribute::NoUnwind);

if (D && D->hasAttr<NoStackProtectorAttr>())
; // Do nothing.
else if (D && D->hasAttr<StrictGuardStackCheckAttr>() &&
LangOpts.getStackProtector() == LangOptions::SSPOn)
B.addAttribute(llvm::Attribute::StackProtectStrong);
else 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);
if (!D || !D->hasAttr<NoStackProtectorAttr>()) {
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);
}

if (!D) {
// If we don't have a declaration to control inlining, the function isn't
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Parser/MicrosoftExtensions.c
Expand Up @@ -5,7 +5,7 @@ typedef int (__cdecl *tptr)(void);
void (*__fastcall fastpfunc)(void);
extern __declspec(dllimport) void __stdcall VarR4FromDec(void);
__declspec(deprecated) __declspec(deprecated) char * __cdecl ltoa( long _Val, char * _DstBuf, int _Radix);
__declspec(safebuffers) __declspec(noalias) __declspec(restrict) void * __cdecl xxx(void *_Memory);
__declspec(safebuffers) __declspec(noalias) __declspec(restrict) void * __cdecl xxx(void *_Memory); /* expected-warning{{__declspec attribute 'safebuffers' is not supported}} */
typedef __w64 unsigned long ULONG_PTR, *PULONG_PTR;

void * __ptr64 PtrToPtr64(const void *p) {
Expand Down
17 changes: 0 additions & 17 deletions llvm/include/llvm/BinaryFormat/COFF.h
Expand Up @@ -772,23 +772,6 @@ enum CodeViewIdentifiers {
DEBUG_HASHES_SECTION_MAGIC = 0x133C9C5
};

// These flags show up in the @feat.00 symbol. They appear to be some kind of
// compiler features bitfield read by link.exe.
enum Feat00Flags : uint32_t {
// Object is compatible with /safeseh.
SafeSEH = 0x1,
// Object was compiled with /GS.
GuardStack = 0x100,
// Object was compiled with /sdl.
SDL = 0x200,
// Object was compiled with /guard:cf.
GuardCF = 0x800,
// Object was compiled with /guard:ehcont.
GuardEHCont = 0x4000,
// Object was compiled with /kernel.
Kernel = 0x40000000,
};

inline bool isReservedSectionNumber(int32_t SectionNumber) {
return SectionNumber <= 0;
}
Expand Down
10 changes: 1 addition & 9 deletions llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
Expand Up @@ -1498,16 +1498,8 @@ void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
FPO |= FrameProcedureOptions::MarkedInline;
if (GV.hasFnAttribute(Attribute::Naked))
FPO |= FrameProcedureOptions::Naked;
if (MFI.hasStackProtectorIndex()) {
if (MFI.hasStackProtectorIndex())
FPO |= FrameProcedureOptions::SecurityChecks;
if (GV.hasFnAttribute(Attribute::StackProtectStrong) ||
GV.hasFnAttribute(Attribute::StackProtectReq)) {
FPO |= FrameProcedureOptions::StrictSecurityChecks;
}
} else if (!GV.hasStackProtectorFnAttr()) {
// __declspec(safebuffers) disables stack guards.
FPO |= FrameProcedureOptions::SafeBuffers;
}
FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);
FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);
if (Asm->TM.getOptLevel() != CodeGenOpt::None &&
Expand Down
16 changes: 7 additions & 9 deletions llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
Expand Up @@ -201,32 +201,30 @@ void AArch64AsmPrinter::emitStartOfAsmFile(Module &M) {
const Triple &TT = TM.getTargetTriple();

if (TT.isOSBinFormatCOFF()) {
// Emit an absolute @feat.00 symbol
// Emit an absolute @feat.00 symbol. This appears to be some kind of
// compiler features bitfield read by link.exe.
MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00"));
OutStreamer->beginCOFFSymbolDef(S);
OutStreamer->emitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
OutStreamer->emitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
OutStreamer->endCOFFSymbolDef();
int64_t Feat00Value = 0;
int64_t Feat00Flags = 0;

if (M.getModuleFlag("cfguard")) {
// Object is CFG-aware.
Feat00Value |= COFF::Feat00Flags::GuardCF;
Feat00Flags |= 0x800; // Object is CFG-aware.
}

if (M.getModuleFlag("ehcontguard")) {
// Object also has EHCont.
Feat00Value |= COFF::Feat00Flags::GuardEHCont;
Feat00Flags |= 0x4000; // Object also has EHCont.
}

if (M.getModuleFlag("ms-kernel")) {
// Object is compiled with /kernel.
Feat00Value |= COFF::Feat00Flags::Kernel;
Feat00Flags |= 0x40000000; // Object is compiled with /kernel.
}

OutStreamer->emitSymbolAttribute(S, MCSA_Global);
OutStreamer->emitAssignment(
S, MCConstantExpr::create(Feat00Value, MMI->getContext()));
S, MCConstantExpr::create(Feat00Flags, MMI->getContext()));
}

if (!TT.isOSBinFormatELF())
Expand Down
18 changes: 8 additions & 10 deletions llvm/lib/Target/X86/X86AsmPrinter.cpp
Expand Up @@ -792,41 +792,39 @@ void X86AsmPrinter::emitStartOfAsmFile(Module &M) {
OutStreamer->switchSection(getObjFileLowering().getTextSection());

if (TT.isOSBinFormatCOFF()) {
// Emit an absolute @feat.00 symbol.
// Emit an absolute @feat.00 symbol. This appears to be some kind of
// compiler features bitfield read by link.exe.
MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00"));
OutStreamer->beginCOFFSymbolDef(S);
OutStreamer->emitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
OutStreamer->emitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
OutStreamer->endCOFFSymbolDef();
int64_t Feat00Value = 0;
int64_t Feat00Flags = 0;

if (TT.getArch() == Triple::x86) {
// According to the PE-COFF spec, the LSB of this value marks the object
// for "registered SEH". This means that all SEH handler entry points
// must be registered in .sxdata. Use of any unregistered handlers will
// cause the process to terminate immediately. LLVM does not know how to
// register any SEH handlers, so its object files should be safe.
Feat00Value |= COFF::Feat00Flags::SafeSEH;
Feat00Flags |= 1;
}

if (M.getModuleFlag("cfguard")) {
// Object is CFG-aware.
Feat00Value |= COFF::Feat00Flags::GuardCF;
Feat00Flags |= 0x800; // Object is CFG-aware.
}

if (M.getModuleFlag("ehcontguard")) {
// Object also has EHCont.
Feat00Value |= COFF::Feat00Flags::GuardEHCont;
Feat00Flags |= 0x4000; // Object also has EHCont.
}

if (M.getModuleFlag("ms-kernel")) {
// Object is compiled with /kernel.
Feat00Value |= COFF::Feat00Flags::Kernel;
Feat00Flags |= 0x40000000; // Object is compiled with /kernel.
}

OutStreamer->emitSymbolAttribute(S, MCSA_Global);
OutStreamer->emitAssignment(
S, MCConstantExpr::create(Feat00Value, MMI->getContext()));
S, MCConstantExpr::create(Feat00Flags, MMI->getContext()));
}
OutStreamer->emitSyntaxDirective();

Expand Down
14 changes: 7 additions & 7 deletions llvm/test/DebugInfo/COFF/frameproc-flags.ll
Expand Up @@ -65,43 +65,43 @@
; CHECK-LABEL: S_GPROC32_ID [size = 52] `use_alloca`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = VFRAME, param fp reg = EBP
; CHECK: flags = has alloca | secure checks | strict secure checks | opt speed
; CHECK: flags = has alloca | secure checks | opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 52] `call_setjmp`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = NONE, param fp reg = NONE
; CHECK: flags = has setjmp | opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 56] `use_inlineasm`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = NONE, param fp reg = NONE
; CHECK: flags = has inline asm | safe buffers | opt speed
; CHECK: flags = has inline asm | opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 48] `cpp_eh`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = EBP, param fp reg = EBP
; CHECK: flags = has eh | opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 52] `use_inline`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = NONE, param fp reg = NONE
; CHECK: flags = safe buffers | opt speed
; CHECK: flags = opt speed
; CHECK-LABEL: S_LPROC32_ID [size = 56] `is_marked_inline`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = NONE, param fp reg = NONE
; CHECK: flags = marked inline | safe buffers | opt speed
; CHECK: flags = marked inline | opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 44] `seh`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = EBP, param fp reg = EBP
; CHECK: flags = has seh | opt speed
; CHECK-LABEL: S_LPROC32_ID [size = 56] `?filt$0@0@seh@@`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = EBP, param fp reg = EBP
; CHECK: flags = safe buffers | opt speed
; CHECK: flags = opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 52] `use_naked`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = NONE, param fp reg = NONE
; CHECK: flags = has inline asm | naked | safe buffers | opt speed
; CHECK: flags = has inline asm | naked | opt speed
; CHECK-LABEL: S_GPROC32_ID [size = 52] `stack_guard`
; CHECK: S_FRAMEPROC [size = 32]
; CHECK: local fp reg = VFRAME, param fp reg = EBP
; CHECK: flags = secure checks | strict secure checks | opt speed
; CHECK: flags = secure checks | opt speed

; ModuleID = 'frameproc-flags.cpp'
source_filename = "frameproc-flags.cpp"
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/DebugInfo/COFF/multifunction.ll
Expand Up @@ -91,7 +91,7 @@
; X86-NEXT: .long 0 # Bytes of callee saved registers
; X86-NEXT: .long 0 # Exception handler offset
; X86-NEXT: .short 0 # Exception handler section
; X86-NEXT: .long 8192 # Flags (defines frame register)
; X86-NEXT: .long 0 # Flags (defines frame register)
; X86-NEXT: .p2align 2
; X86-NEXT: [[FPROC_END]]:
; X86-NEXT: .short 2
Expand Down Expand Up @@ -130,7 +130,7 @@
; X86-NEXT: .long 0 # Bytes of callee saved registers
; X86-NEXT: .long 0 # Exception handler offset
; X86-NEXT: .short 0 # Exception handler section
; X86-NEXT: .long 8192 # Flags (defines frame register)
; X86-NEXT: .long 0 # Flags (defines frame register)
; X86-NEXT: .p2align 2
; X86-NEXT: [[FPROC_END]]:
; X86-NEXT: .short 2
Expand Down Expand Up @@ -169,7 +169,7 @@
; X86-NEXT: .long 0 # Bytes of callee saved registers
; X86-NEXT: .long 0 # Exception handler offset
; X86-NEXT: .short 0 # Exception handler section
; X86-NEXT: .long 8192 # Flags (defines frame register)
; X86-NEXT: .long 0 # Flags (defines frame register)
; X86-NEXT: .p2align 2
; X86-NEXT: [[FPROC_END]]:
; X86-NEXT: .short 2
Expand Down Expand Up @@ -403,7 +403,7 @@
; X64-NEXT: .long 0 # Bytes of callee saved registers
; X64-NEXT: .long 0 # Exception handler offset
; X64-NEXT: .short 0 # Exception handler section
; X64-NEXT: .long 90112 # Flags (defines frame register)
; X64-NEXT: .long 81920 # Flags (defines frame register)
; X64-NEXT: .p2align 2
; X64-NEXT: [[FPROC_END]]:
; X64-NEXT: .short 2
Expand Down Expand Up @@ -441,7 +441,7 @@
; X64-NEXT: .long 0 # Bytes of callee saved registers
; X64-NEXT: .long 0 # Exception handler offset
; X64-NEXT: .short 0 # Exception handler section
; X64-NEXT: .long 90112 # Flags (defines frame register)
; X64-NEXT: .long 81920 # Flags (defines frame register)
; X64-NEXT: .p2align 2
; X64-NEXT: [[FPROC_END]]:
; X64-NEXT: .short 2
Expand Down Expand Up @@ -479,7 +479,7 @@
; X64-NEXT: .long 0 # Bytes of callee saved registers
; X64-NEXT: .long 0 # Exception handler offset
; X64-NEXT: .short 0 # Exception handler section
; X64-NEXT: .long 90112 # Flags (defines frame register)
; X64-NEXT: .long 81920 # Flags (defines frame register)
; X64-NEXT: .p2align 2
; X64-NEXT: [[FPROC_END]]:
; X64-NEXT: .short 2
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/DebugInfo/COFF/simple.ll
Expand Up @@ -71,7 +71,7 @@
; X86-NEXT: .long 0 # Bytes of callee saved registers
; X86-NEXT: .long 0 # Exception handler offset
; X86-NEXT: .short 0 # Exception handler section
; X86-NEXT: .long 8192 # Flags (defines frame register)
; X86-NEXT: .long 0 # Flags (defines frame register)
; X86-NEXT: .p2align 2
; X86-NEXT: [[FPROC_END]]:
; X86-NEXT: .short 2
Expand Down Expand Up @@ -201,7 +201,7 @@
; X64-NEXT: .long 0 # Bytes of callee saved registers
; X64-NEXT: .long 0 # Exception handler offset
; X64-NEXT: .short 0 # Exception handler section
; X64-NEXT: .long 90112 # Flags (defines frame register)
; X64-NEXT: .long 81920 # Flags (defines frame register)
; X64-NEXT: .p2align 2
; X64-NEXT: [[FPROC_END]]:
; X64-NEXT: .short 2
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/DebugInfo/COFF/vframe-fpo.ll
Expand Up @@ -125,8 +125,7 @@
; CODEVIEW-NEXT: BytesOfCalleeSavedRegisters: 0xC
; CODEVIEW-NEXT: OffsetOfExceptionHandler: 0x0
; CODEVIEW-NEXT: SectionIdOfExceptionHandler: 0x0
; CODEVIEW-NEXT: Flags [ (0x16000)
; CODEVIEW-NEXT: SafeBuffers (0x2000)
; CODEVIEW-NEXT: Flags [ (0x14000)
; CODEVIEW-NEXT: ]
; CODEVIEW-NEXT: LocalFramePtrReg: VFRAME (0x7536)
; CODEVIEW-NEXT: ParamFramePtrReg: VFRAME (0x7536)
Expand Down

0 comments on commit cd20a18

Please sign in to comment.