Git fork

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>

authored by

Matthias Aßhauer and committed by
Junio C Hamano
4a9b2049 e9d7761b

+15 -10
+4 -2
compat/mingw.c
··· 8 8 #include "win32/lazyload.h" 9 9 #include "../config.h" 10 10 #include "dir.h" 11 + #define SECURITY_WIN32 12 + #include <sspi.h> 11 13 12 14 #define HCAST(type, handle) ((type)(intptr_t)handle) 13 15 ··· 1008 1010 /* a pointer to the original strftime in case we can't find the UCRT version */ 1009 1011 static size_t (*fallback)(char *, size_t, const char *, const struct tm *) = strftime; 1010 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, 1012 1014 const char *, const struct tm *); 1013 1015 1014 1016 if (INIT_PROC_ADDR(strftime)) ··· 2183 2185 2184 2186 static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type) 2185 2187 { 2186 - DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW, 2188 + DECLARE_PROC_ADDR(secur32.dll, BOOL, SEC_ENTRY, GetUserNameExW, 2187 2189 enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG); 2188 2190 static wchar_t wbuffer[1024]; 2189 2191 DWORD len;
+3 -3
compat/win32/lazyload.h
··· 4 4 /* 5 5 * A pair of macros to simplify loading of DLL functions. Example: 6 6 * 7 - * DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW, 7 + * DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI, CreateHardLinkW, 8 8 * LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES); 9 9 * 10 10 * if (!INIT_PROC_ADDR(CreateHardLinkW)) ··· 25 25 }; 26 26 27 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 29 static struct proc_addr proc_addr_##function = \ 30 30 { #dll, #function, NULL, 0 }; \ 31 - typedef rettype (WINAPI *proc_type_##function)(__VA_ARGS__); \ 31 + typedef rettype (convention *proc_type_##function)(__VA_ARGS__); \ 32 32 static proc_type_##function function 33 33 34 34 /*
+2 -2
compat/win32/trace2_win32_process_info.c
··· 143 143 */ 144 144 static void get_peak_memory_info(void) 145 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 148 149 149 if (INIT_PROC_ADDR(GetProcessMemoryInfo)) { 150 150 PROCESS_MEMORY_COUNTERS pmc;
+3 -2
compat/winansi.c
··· 45 45 static void warn_if_raster_font(void) 46 46 { 47 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); 50 51 51 52 /* don't bother if output was ascii only */ 52 53 if (!non_ascii_used)
+3 -1
t/helper/test-drop-caches.c
··· 3 3 4 4 #if defined(GIT_WINDOWS_NATIVE) 5 5 #include "lazyload.h" 6 + #include <winnt.h> 6 7 7 8 static int cmd_sync(void) 8 9 { ··· 86 87 { 87 88 HANDLE hProcess = GetCurrentProcess(); 88 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); 90 92 SYSTEM_MEMORY_LIST_COMMAND command; 91 93 int status; 92 94