Hi Martin,

This patch looks right to me. If it never really worked, backward compatibility is not an issue. If someone wants those features, using UCRT seems to be the right answer.

Thanks,
Jacek

On 22.04.2020 23:00, Martin Storsjö wrote:
This was a partial (but incomplete!) support for the MSVC stack
cookie handling (under the MSVC option /GS, similar to GCC and
Clang/MinGW's -fstack-protector-strong). This contained the
stack cookie itself and the init function, and the error reporting
function, but not the checking function (__security_check_cookie).
And these functions would only end up being called by code generated
by MSVC anyway.

Signed-off-by: Martin Storsjö <mar...@martin.st>
---
This is one out of two alternative ways of handling the potentially
problematic function __report_gsfailure, which calls a number of
functions that aren't available for UWP apps - the other alternative
is to keep the functions but move __report_gsfailure to the msvcrt
import library, as more modern msvcrt versions, and ucrt, does provide
their own versions of the function - and in normal cases, nothing
references the symbol so it won't end up linked in (unless it's in
the same object file as __security_init_cookie).
---
  mingw-w64-crt/Makefile.am       |   2 +-
  mingw-w64-crt/crt/crtdll.c      |   1 -
  mingw-w64-crt/crt/crtexe.c      |   2 -
  mingw-w64-crt/crt/gs_support.c  | 154 --------------------------------
  mingw-w64-headers/crt/process.h |  10 ---
  5 files changed, 1 insertion(+), 168 deletions(-)
  delete mode 100644 mingw-w64-crt/crt/gs_support.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 0f882ae2a..86b0b0d73 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -115,7 +115,7 @@ src_libntoskrnl=libsrc/memcmp.c
src_libmingw32=include/oscalls.h include/internal.h include/sect_attribs.h \
    crt/crt0_c.c        crt/dll_argv.c  crt/gccmain.c     crt/natstart.c  
crt/pseudo-reloc-list.c  crt/wildcard.c \
-  crt/charmax.c       crt/crt0_w.c         crt/dllargv.c   crt/gs_support.c  
crt/_newmode.c  crt/tlssup.c             crt/xncommod.c \
+  crt/charmax.c       crt/crt0_w.c         crt/dllargv.c   crt/_newmode.c  
crt/tlssup.c             crt/xncommod.c \
    crt/cinitexe.c      crt/crt0_w.c         crt/merr.c      crt/pesect.c      
crt/udllargc.c  crt/xthdloc.c            crt/CRT_fp10.c \
    crt/mingw_custom.c  crt/mingw_helpers.c  \
    crt/pseudo-reloc.c  crt/udll_argv.c      \
diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index 6187f10d2..797c1beeb 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -156,7 +156,6 @@ DllMainCRTStartup (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
    mingw_app_type = 0;
    if (dwReason == DLL_PROCESS_ATTACH)
      {
-      __security_init_cookie ();
  #ifdef __x86_64__
        __mingw_init_ehandler ();
  #endif
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index b730dc479..f521b54cd 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -189,7 +189,6 @@ int WinMainCRTStartup (void)
      );
  #endif
    mingw_app_type = 1;
-  __security_init_cookie ();
    ret = __tmainCRTStartup ();
  #ifdef SEH_INLINE_ASM
    asm ("\tnop\n"
@@ -217,7 +216,6 @@ int mainCRTStartup (void)
      );
  #endif
    mingw_app_type = 0;
-  __security_init_cookie ();
    ret = __tmainCRTStartup ();
  #ifdef SEH_INLINE_ASM
    asm ("\tnop\n"
diff --git a/mingw-w64-crt/crt/gs_support.c b/mingw-w64-crt/crt/gs_support.c
deleted file mode 100644
index 0c6ac68cd..000000000
--- a/mingw-w64-crt/crt/gs_support.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-
-#ifndef WIN32_LEAN_AND_MEAN
-#define WIN32_LEAN_AND_MEAN
-#endif
-#define WIN32_NO_STATUS
-#include <stdlib.h>      /* abort () */
-#include <windows.h>
-#undef  WIN32_NO_STATUS
-#include <ntstatus.h>    /* STATUS macros */
-#ifdef _WIN64
-#include <intrin.h>
-#endif
-
-#ifdef _WIN64
-#define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
-#else
-#define DEFAULT_SECURITY_COOKIE 0xBB40E64E
-#endif
-
-/* Externals.  */
-
-typedef LONG NTSTATUS; /* same as in ntdef.h / winternl.h */
-
-#define UNW_FLAG_NHANDLER   0x0
-
-typedef union
-{
-  unsigned __int64 ft_scalar;
-  FILETIME ft_struct;
-} FT;
-
-static EXCEPTION_RECORD GS_ExceptionRecord;
-static CONTEXT GS_ContextRecord;
-
-static const EXCEPTION_POINTERS GS_ExceptionPointers = {
-  &GS_ExceptionRecord,&GS_ContextRecord
-};
-
-DECLSPEC_SELECTANY UINT_PTR __security_cookie = DEFAULT_SECURITY_COOKIE;
-DECLSPEC_SELECTANY UINT_PTR __security_cookie_complement = 
~(DEFAULT_SECURITY_COOKIE);
-
-void __cdecl __security_init_cookie (void);
-
-void __cdecl
-__security_init_cookie (void)
-{
-  UINT_PTR cookie;
-  FT systime = { 0, };
-  LARGE_INTEGER perfctr;
-
-  if (__security_cookie != DEFAULT_SECURITY_COOKIE)
-    {
-      __security_cookie_complement = ~__security_cookie;
-      return;
-    }
-
-  GetSystemTimeAsFileTime (&systime.ft_struct);
-#ifdef _WIN64
-  cookie = systime.ft_scalar;
-#else
-  cookie = systime.ft_struct.dwLowDateTime;
-  cookie ^= systime.ft_struct.dwHighDateTime;
-#endif
-
-  cookie ^= GetCurrentProcessId ();
-  cookie ^= GetCurrentThreadId ();
-  cookie ^= GetTickCount ();
-
-  QueryPerformanceCounter (&perfctr);
-#ifdef _WIN64
-  cookie ^= perfctr.QuadPart;
-#else
-  cookie ^= perfctr.LowPart;
-  cookie ^= perfctr.HighPart;
-#endif
-
-#ifdef _WIN64
-  cookie &= 0x0000ffffffffffffll;
-#endif
-
-  if (cookie == DEFAULT_SECURITY_COOKIE)
-    cookie = DEFAULT_SECURITY_COOKIE + 1;
-  __security_cookie = cookie;
-  __security_cookie_complement = ~cookie;
-}
-
-
-#if defined(__GNUC__) /* wrap msvc intrinsics onto gcc builtins */
-#undef  _ReturnAddress
-#undef  _AddressOfReturnAddress
-#define _ReturnAddress()               __builtin_return_address(0)
-#define _AddressOfReturnAddress()      __builtin_frame_address (0)
-#endif /* __GNUC__ */
-
-__declspec(noreturn) void __cdecl __report_gsfailure (ULONG_PTR);
-
-#define UNUSED_PARAM(x) { x = x; }
-__declspec(noreturn) void __cdecl
-__report_gsfailure (ULONG_PTR StackCookie)
-{
-  volatile UINT_PTR cookie[2] __MINGW_ATTRIB_UNUSED;
-#if defined(_WIN64) && !defined(__aarch64__)
-  ULONG64 controlPC, imgBase, establisherFrame;
-  PRUNTIME_FUNCTION fctEntry;
-  PVOID hndData;
-
-  RtlCaptureContext (&GS_ContextRecord);
-  controlPC = GS_ContextRecord.Rip;
-  fctEntry = RtlLookupFunctionEntry (controlPC, &imgBase, NULL);
-  if (fctEntry != NULL)
-    {
-      RtlVirtualUnwind (UNW_FLAG_NHANDLER, imgBase, controlPC, fctEntry,
-                       &GS_ContextRecord, &hndData, &establisherFrame, NULL);
-    }
-  else
-#endif /* _WIN64 */
-    {
-#if defined(__x86_64__) || defined(_AMD64_)
-      GS_ContextRecord.Rip = (ULONGLONG) _ReturnAddress();
-      GS_ContextRecord.Rsp = (ULONGLONG) _AddressOfReturnAddress() + 8;
-#elif defined(__i386__) || defined(_X86_)
-      GS_ContextRecord.Eip = (DWORD) _ReturnAddress();
-      GS_ContextRecord.Esp = (DWORD) _AddressOfReturnAddress() + 4;
-#elif defined(__arm__) || defined(_ARM_)
-      GS_ContextRecord.Pc = (DWORD) _ReturnAddress();
-      GS_ContextRecord.Sp = (DWORD) _AddressOfReturnAddress() + 4;
-#endif /* _WIN64 */
-    }
-
-#if defined(__x86_64__) || defined(_AMD64_)
-  GS_ExceptionRecord.ExceptionAddress = (PVOID) GS_ContextRecord.Rip;
-  GS_ContextRecord.Rcx = StackCookie;
-#elif defined(__i386__) || defined(_X86_)
-  GS_ExceptionRecord.ExceptionAddress = (PVOID) GS_ContextRecord.Eip;
-  GS_ContextRecord.Ecx = StackCookie;
-#elif defined(__arm__) || defined(_ARM_)
-  GS_ExceptionRecord.ExceptionAddress = (PVOID) GS_ContextRecord.Pc;
-  UNUSED_PARAM(StackCookie);
-#endif /* _WIN64 */
-  GS_ExceptionRecord.ExceptionCode = STATUS_STACK_BUFFER_OVERRUN;
-  GS_ExceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
-  cookie[0] = __security_cookie;
-  cookie[1] = __security_cookie_complement;
-  SetUnhandledExceptionFilter (NULL);
-  UnhandledExceptionFilter ((EXCEPTION_POINTERS *) &GS_ExceptionPointers);
-  TerminateProcess (GetCurrentProcess (), STATUS_STACK_BUFFER_OVERRUN);
-  abort();
-}
-
diff --git a/mingw-w64-headers/crt/process.h b/mingw-w64-headers/crt/process.h
index a22aa237b..d047568dd 100644
--- a/mingw-w64-headers/crt/process.h
+++ b/mingw-w64-headers/crt/process.h
@@ -122,16 +122,6 @@ extern "C" {
  #endif
  #endif /* _CRT_USE_WINAPI_FAMILY_DESKTOP_APP */
- void __cdecl __security_init_cookie(void);
-#if (defined(_X86_) && !defined(__x86_64))
-  void __fastcall __security_check_cookie(uintptr_t _StackCookie);
-  __MINGW_ATTRIB_NORETURN void __cdecl __report_gsfailure(void);
-#else
-  void __cdecl __security_check_cookie(uintptr_t _StackCookie);
-  __MINGW_ATTRIB_NORETURN void __cdecl __report_gsfailure(uintptr_t 
_StackCookie);
-#endif
-  extern uintptr_t __security_cookie;
-
  #ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP
    intptr_t __cdecl _loaddll(char *_Filename);
    int __cdecl _unloaddll(intptr_t _Handle);




_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to