git-commit-vandalism/compat/win32/lazyload.h
Matthias Aßhauer 4a9b204920 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>
2022-01-09 10:34:53 -08:00

62 lines
1.7 KiB
C

#ifndef LAZYLOAD_H
#define LAZYLOAD_H
/*
* A pair of macros to simplify loading of DLL functions. Example:
*
* DECLARE_PROC_ADDR(kernel32.dll, BOOL, WINAPI, CreateHardLinkW,
* LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
*
* if (!INIT_PROC_ADDR(CreateHardLinkW))
* return error("Could not find CreateHardLinkW() function";
*
* if (!CreateHardLinkW(source, target, NULL))
* return error("could not create hardlink from %S to %S",
* source, target);
*/
typedef void (*FARVOIDPROC)(void);
struct proc_addr {
const char *const dll;
const char *const function;
FARVOIDPROC pfunction;
unsigned initialized : 1;
};
/* Declares a function to be loaded dynamically from a DLL. */
#define DECLARE_PROC_ADDR(dll, rettype, convention, function, ...) \
static struct proc_addr proc_addr_##function = \
{ #dll, #function, NULL, 0 }; \
typedef rettype (convention *proc_type_##function)(__VA_ARGS__); \
static proc_type_##function function
/*
* Loads a function from a DLL (once-only).
* Returns non-NULL function pointer on success.
* Returns NULL + errno == ENOSYS on failure.
* This function is not thread-safe.
*/
#define INIT_PROC_ADDR(function) \
(function = (proc_type_##function)get_proc_addr(&proc_addr_##function))
static inline FARVOIDPROC get_proc_addr(struct proc_addr *proc)
{
/* only do this once */
if (!proc->initialized) {
HANDLE hnd;
proc->initialized = 1;
hnd = LoadLibraryExA(proc->dll, NULL,
LOAD_LIBRARY_SEARCH_SYSTEM32);
if (hnd)
proc->pfunction = (FARVOIDPROC)GetProcAddress(hnd,
proc->function);
}
/* set ENOSYS if DLL or function was not found */
if (!proc->pfunction)
errno = ENOSYS;
return proc->pfunction;
}
#endif