]> git.ipfire.org Git - thirdparty/git.git/commitdiff
lazyload: use correct calling conventions
authorMatthias Aßhauer <mha1993@live.de>
Sat, 8 Jan 2022 16:02:30 +0000 (16:02 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 9 Jan 2022 18:34:53 +0000 (10:34 -0800)
Christoph Reiter reported on the Git for Windows issue tracker[1], that
mingw_strftime() imports strftime() from ucrtbase.dll with the wrong
calling convention. It should be __cdecl instead of WINAPI, which we
always use in DECLARE_PROC_ADDR().

The MSYS2 project encountered cmake sefaults on x86 Windows caused by
the same issue in the cmake source. [2] There are no known git crashes
that where caused by this, yet, but we should try to prevent them.

We import two other non-WINAPI functions via DECLARE_PROC_ADDR(), too.

* NtSetSystemInformation() (NTAPI)
* GetUserNameExW()         (SEC_ENTRY)

NTAPI, SEC_ENTRY and WINAPI are all ususally defined as __stdcall,
but there are circumstances where they're defined differently.

Teach DECLARE_PROC_ADDR() about calling conventions and be explicit
about when we want to use which calling convention.

Import winnt.h for the definition of NTAPI and sspi.h for SEC_ENTRY
near their respective only users.

[1] https://github.com/git-for-windows/git/issues/3560
[2] https://github.com/msys2/MINGW-packages/issues/10152

Reported-By: Christoph Reiter <reiter.christoph@gmail.com>
Signed-off-by: Matthias Aßhauer <mha1993@live.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/mingw.c
compat/win32/lazyload.h
compat/win32/trace2_win32_process_info.c
compat/winansi.c
t/helper/test-drop-caches.c

index 9e0cd1e097f25f59ebc15fe583cdcd215c82bfcd..f38c43811230a1fb6aab504051d042a1fb576c24 100644 (file)
@@ -8,6 +8,8 @@
 #include "win32/lazyload.h"
 #include "../config.h"
 #include "dir.h"
+#define SECURITY_WIN32
+#include <sspi.h>
 
 #define HCAST(type, handle) ((type)(intptr_t)handle)
 
@@ -1008,7 +1010,7 @@ size_t mingw_strftime(char *s, size_t max,
        /* a pointer to the original strftime in case we can't find the UCRT version */
        static size_t (*fallback)(char *, size_t, const char *, const struct tm *) = strftime;
        size_t ret;
-       DECLARE_PROC_ADDR(ucrtbase.dll, size_t, strftime, char *, size_t,
+       DECLARE_PROC_ADDR(ucrtbase.dll, size_t, __cdecl, strftime, char *, size_t,
                const char *, const struct tm *);
 
        if (INIT_PROC_ADDR(strftime))
@@ -2183,7 +2185,7 @@ enum EXTENDED_NAME_FORMAT {
 
 static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
 {
-       DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
+       DECLARE_PROC_ADDR(secur32.dll, BOOL, SEC_ENTRY, GetUserNameExW,
                enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
        static wchar_t wbuffer[1024];
        DWORD len;
index 2b3637135f68a3e7c2acadf0971e143cf689db02..f2bb96c89c735c67ab9915e6ced1e9feabb043b4 100644 (file)
@@ -4,7 +4,7 @@
 /*
  * A pair of macros to simplify loading of DLL functions. Example:
  *
- *   DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
+ *   DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI, CreateHardLinkW,
  *                     LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
  *
  *   if (!INIT_PROC_ADDR(CreateHardLinkW))
@@ -25,10 +25,10 @@ struct proc_addr {
 };
 
 /* Declares a function to be loaded dynamically from a DLL. */
-#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \
+#define DECLARE_PROC_ADDR(dll, rettype, convention, function, ...) \
        static struct proc_addr proc_addr_##function = \
        { #dll, #function, NULL, 0 }; \
-       typedef rettype (WINAPI *proc_type_##function)(__VA_ARGS__); \
+       typedef rettype (convention *proc_type_##function)(__VA_ARGS__); \
        static proc_type_##function function
 
 /*
index 8ccbd1c2c6f82d68e0c39f8ec77aa2bd4a899f2e..a53fd924340f7d6da04e5197363fe831b9d19bf3 100644 (file)
@@ -143,8 +143,8 @@ static void get_is_being_debugged(void)
  */
 static void get_peak_memory_info(void)
 {
-       DECLARE_PROC_ADDR(psapi.dll, BOOL, GetProcessMemoryInfo, HANDLE,
-                         PPROCESS_MEMORY_COUNTERS, DWORD);
+       DECLARE_PROC_ADDR(psapi.dll, BOOL, WINAPI, GetProcessMemoryInfo,
+                         HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
 
        if (INIT_PROC_ADDR(GetProcessMemoryInfo)) {
                PROCESS_MEMORY_COUNTERS pmc;
index c27b20a79d91cf7877d795c96e64cbadafdad7fd..4fceecf14ce599b585801bcb1ebc0b4e2c22af06 100644 (file)
@@ -45,8 +45,9 @@ typedef struct _CONSOLE_FONT_INFOEX {
 static void warn_if_raster_font(void)
 {
        DWORD fontFamily = 0;
-       DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx,
-                       HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
+       DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI,
+                       GetCurrentConsoleFontEx, HANDLE, BOOL,
+                       PCONSOLE_FONT_INFOEX);
 
        /* don't bother if output was ascii only */
        if (!non_ascii_used)
index 7b4278462bb7166522369aa9d74147fa36c03835..e37396dd9c2c2fe01b9bf2acc8d6a2c6456869fc 100644 (file)
@@ -3,6 +3,7 @@
 
 #if defined(GIT_WINDOWS_NATIVE)
 #include "lazyload.h"
+#include <winnt.h>
 
 static int cmd_sync(void)
 {
@@ -86,7 +87,8 @@ static int cmd_dropcaches(void)
 {
        HANDLE hProcess = GetCurrentProcess();
        HANDLE hToken;
-       DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
+       DECLARE_PROC_ADDR(ntdll.dll, DWORD, NTAPI, NtSetSystemInformation, INT, PVOID,
+               ULONG);
        SYSTEM_MEMORY_LIST_COMMAND command;
        int status;