lazyload: use correct calling conventions

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>

Matthias Aßhauer committed Jan 8, 2022 at 16:02 UTC 4a9b204920152c668228a9d43a63be39b0c32f45
5 files changed +15 -10
compat/mingw.c
+4 -2
@@ -8,6 +8,8 @@
8 #include "win32/lazyload.h"
9 #include "../config.h"
10 #include "dir.h"
11 +#define SECURITY_WIN32
12 +#include <sspi.h>
13
14 #define HCAST(type, handle) ((type)(intptr_t)handle)
15
@@ -1008,7 +1010,7 @@ size_t mingw_strftime(char *s, size_t max,
1010 /* a pointer to the original strftime in case we can't find the UCRT version */
1011 static size_t (*fallback)(char *, size_t, const char *, const struct tm *) = strftime;
1012 size_t ret;
1011 - DECLARE_PROC_ADDR(ucrtbase.dll, size_t, strftime, char *, size_t,
1013 + DECLARE_PROC_ADDR(ucrtbase.dll, size_t, __cdecl, strftime, char *, size_t,
1014 const char *, const struct tm *);
1015
1016 if (INIT_PROC_ADDR(strftime))
@@ -2183,7 +2185,7 @@ enum EXTENDED_NAME_FORMAT {
2185
2186 static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
2187 {
2186 - DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
2188 + DECLARE_PROC_ADDR(secur32.dll, BOOL, SEC_ENTRY, GetUserNameExW,
2189 enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
2190 static wchar_t wbuffer[1024];
2191 DWORD len;
compat/win32/lazyload.h
+3 -3
@@ -4,7 +4,7 @@
4 /*
5 * A pair of macros to simplify loading of DLL functions. Example:
6 *
7 - * DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
7 + * DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI, CreateHardLinkW,
8 * LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
9 *
10 * if (!INIT_PROC_ADDR(CreateHardLinkW))
@@ -25,10 +25,10 @@ struct proc_addr {
25 };
26
27 /* Declares a function to be loaded dynamically from a DLL. */
28 -#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \
28 +#define DECLARE_PROC_ADDR(dll, rettype, convention, function, ...) \
29 static struct proc_addr proc_addr_##function = \
30 { #dll, #function, NULL, 0 }; \
31 - typedef rettype (WINAPI *proc_type_##function)(__VA_ARGS__); \
31 + typedef rettype (convention *proc_type_##function)(__VA_ARGS__); \
32 static proc_type_##function function
33
34 /*
compat/win32/trace2_win32_process_info.c
+2 -2
@@ -143,8 +143,8 @@ static void get_is_being_debugged(void)
143 */
144 static void get_peak_memory_info(void)
145 {
146 - DECLARE_PROC_ADDR(psapi.dll, BOOL, GetProcessMemoryInfo, HANDLE,
147 - PPROCESS_MEMORY_COUNTERS, DWORD);
146 + DECLARE_PROC_ADDR(psapi.dll, BOOL, WINAPI, GetProcessMemoryInfo,
147 + HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
148
149 if (INIT_PROC_ADDR(GetProcessMemoryInfo)) {
150 PROCESS_MEMORY_COUNTERS pmc;
compat/winansi.c
+3 -2
@@ -45,8 +45,9 @@ typedef struct _CONSOLE_FONT_INFOEX {
45 static void warn_if_raster_font(void)
46 {
47 DWORD fontFamily = 0;
48 - DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx,
49 - HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
48 + DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI,
49 + GetCurrentConsoleFontEx, HANDLE, BOOL,
50 + PCONSOLE_FONT_INFOEX);
51
52 /* don't bother if output was ascii only */
53 if (!non_ascii_used)
t/helper/test-drop-caches.c
+3 -1
@@ -3,6 +3,7 @@
3
4 #if defined(GIT_WINDOWS_NATIVE)
5 #include "lazyload.h"
6 +#include <winnt.h>
7
8 static int cmd_sync(void)
9 {
@@ -86,7 +87,8 @@ static int cmd_dropcaches(void)
87 {
88 HANDLE hProcess = GetCurrentProcess();
89 HANDLE hToken;
89 - DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
90 + DECLARE_PROC_ADDR(ntdll.dll, DWORD, NTAPI, NtSetSystemInformation, INT, PVOID,
91 + ULONG);
92 SYSTEM_MEMORY_LIST_COMMAND command;
93 int status;
94