git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] lazyload: use correct calling conventions
@ 2022-01-08 16:02 Matthias Aßhauer via GitGitGadget
  2022-01-10 22:09 ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Aßhauer via GitGitGadget @ 2022-01-08 16:02 UTC (permalink / raw)
  To: git; +Cc: reiter.christoph, Matthias Aßhauer, Matthias Aßhauer

From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= <mha1993@live.de>

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>
---
    lazyload: use correct calling conventions
    
    I wanted to get this out a lot earlier, but got distracted by a lot of
    work at $DAYJOB. I'm sorry about sending this this late in the 2.35.0
    cycle.
    
    This applies cleanly on master and maint, but CI produces some weird
    build failures that seem unrelated in both of those situations.
    
    On master the FreeBSD fails with
    
    > archive.c:337:35: error: '_Generic' is a C11 extension
    > [-Werror,-Wc11-extensions] strbuf_addstr(&path_in_archive,
    > basename(path)); ^ /usr/include/libgen.h:61:21: note: expanded from
    > macro 'basename' #define basename(x) __generic(x, const char *,
    > __old_basename, basename)(x) ^ /usr/include/sys/cdefs.h:329:2: note:
    > expanded from macro '__generic' _Generic(expr, t: yes, default: no)
    
    While on maint the CI / linux32 (daald/ubuntu32:xenial) job just
    straight up can't find make. I have no clue what's up with that, but I'm
    fairly certain I'm not the direct cause of either of those failures.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1181%2Frimrul%2Fwin-lazyload-calling-convention-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1181/rimrul/win-lazyload-calling-convention-v1
Pull-Request: https://github.com/git/git/pull/1181

 compat/mingw.c                           | 6 ++++--
 compat/win32/lazyload.h                  | 6 +++---
 compat/win32/trace2_win32_process_info.c | 4 ++--
 compat/winansi.c                         | 5 +++--
 t/helper/test-drop-caches.c              | 4 +++-
 5 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index 9e0cd1e097f..f38c4381123 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -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;
diff --git a/compat/win32/lazyload.h b/compat/win32/lazyload.h
index 2b3637135f6..f2bb96c89c7 100644
--- a/compat/win32/lazyload.h
+++ b/compat/win32/lazyload.h
@@ -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
 
 /*
diff --git a/compat/win32/trace2_win32_process_info.c b/compat/win32/trace2_win32_process_info.c
index 8ccbd1c2c6f..a53fd924340 100644
--- a/compat/win32/trace2_win32_process_info.c
+++ b/compat/win32/trace2_win32_process_info.c
@@ -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;
diff --git a/compat/winansi.c b/compat/winansi.c
index c27b20a79d9..4fceecf14ce 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -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)
diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
index 7b4278462bb..e37396dd9c2 100644
--- a/t/helper/test-drop-caches.c
+++ b/t/helper/test-drop-caches.c
@@ -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;
 

base-commit: e9d7761bb94f20acc98824275e317fa82436c25d
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lazyload: use correct calling conventions
  2022-01-08 16:02 [PATCH] lazyload: use correct calling conventions Matthias Aßhauer via GitGitGadget
@ 2022-01-10 22:09 ` Junio C Hamano
  2022-01-11 13:17   ` Johannes Schindelin
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2022-01-10 22:09 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, reiter.christoph, Matthias Aßhauer,
	Matthias Aßhauer via GitGitGadget

Dscho, how does this look?  My choices are

 - Waiting for review.
 - Will merge to 'next'.
 - Will merge to 'next' and then to 'master'.

The last one will make it as part of Git 2.35.0 final, unless there
is some unexpected breakage found later.

Thanks.

(no comment of my own appears after this line, but the original is
left as reference).

"Matthias Aßhauer via GitGitGadget"  <gitgitgadget@gmail.com>
writes:

> From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= <mha1993@live.de>
>
> 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>
> ---
>     lazyload: use correct calling conventions
>     
>     I wanted to get this out a lot earlier, but got distracted by a lot of
>     work at $DAYJOB. I'm sorry about sending this this late in the 2.35.0
>     cycle.
>     
>     This applies cleanly on master and maint, but CI produces some weird
>     build failures that seem unrelated in both of those situations.
>     
>     On master the FreeBSD fails with
>     
>     > archive.c:337:35: error: '_Generic' is a C11 extension
>     > [-Werror,-Wc11-extensions] strbuf_addstr(&path_in_archive,
>     > basename(path)); ^ /usr/include/libgen.h:61:21: note: expanded from
>     > macro 'basename' #define basename(x) __generic(x, const char *,
>     > __old_basename, basename)(x) ^ /usr/include/sys/cdefs.h:329:2: note:
>     > expanded from macro '__generic' _Generic(expr, t: yes, default: no)
>     
>     While on maint the CI / linux32 (daald/ubuntu32:xenial) job just
>     straight up can't find make. I have no clue what's up with that, but I'm
>     fairly certain I'm not the direct cause of either of those failures.
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1181%2Frimrul%2Fwin-lazyload-calling-convention-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1181/rimrul/win-lazyload-calling-convention-v1
> Pull-Request: https://github.com/git/git/pull/1181
>
>  compat/mingw.c                           | 6 ++++--
>  compat/win32/lazyload.h                  | 6 +++---
>  compat/win32/trace2_win32_process_info.c | 4 ++--
>  compat/winansi.c                         | 5 +++--
>  t/helper/test-drop-caches.c              | 4 +++-
>  5 files changed, 15 insertions(+), 10 deletions(-)
>
> diff --git a/compat/mingw.c b/compat/mingw.c
> index 9e0cd1e097f..f38c4381123 100644
> --- a/compat/mingw.c
> +++ b/compat/mingw.c
> @@ -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;
> diff --git a/compat/win32/lazyload.h b/compat/win32/lazyload.h
> index 2b3637135f6..f2bb96c89c7 100644
> --- a/compat/win32/lazyload.h
> +++ b/compat/win32/lazyload.h
> @@ -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
>  
>  /*
> diff --git a/compat/win32/trace2_win32_process_info.c b/compat/win32/trace2_win32_process_info.c
> index 8ccbd1c2c6f..a53fd924340 100644
> --- a/compat/win32/trace2_win32_process_info.c
> +++ b/compat/win32/trace2_win32_process_info.c
> @@ -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;
> diff --git a/compat/winansi.c b/compat/winansi.c
> index c27b20a79d9..4fceecf14ce 100644
> --- a/compat/winansi.c
> +++ b/compat/winansi.c
> @@ -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)
> diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
> index 7b4278462bb..e37396dd9c2 100644
> --- a/t/helper/test-drop-caches.c
> +++ b/t/helper/test-drop-caches.c
> @@ -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;
>  
>
> base-commit: e9d7761bb94f20acc98824275e317fa82436c25d

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lazyload: use correct calling conventions
  2022-01-10 22:09 ` Junio C Hamano
@ 2022-01-11 13:17   ` Johannes Schindelin
  2022-01-11 22:33     ` Johannes Schindelin
  2022-01-12 18:13     ` Junio C Hamano
  0 siblings, 2 replies; 5+ messages in thread
From: Johannes Schindelin @ 2022-01-11 13:17 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, reiter.christoph, Matthias Aßhauer,
	Matthias Aßhauer via GitGitGadget

Hi Junio,

On Mon, 10 Jan 2022, Junio C Hamano wrote:

> Dscho, how does this look?  My choices are
>
>  - Waiting for review.
>  - Will merge to 'next'.
>  - Will merge to 'next' and then to 'master'.

My preference is the last option, merge it down quickly.

> The last one will make it as part of Git 2.35.0 final, unless there
> is some unexpected breakage found later.

I do not expect anything to break. Just like Matthias, I actually expect
future breakages to be fended off by this.

Merging it into v2.35.0 final will require a tiny fixup in
`compat/win32/flush.c` (which we carry in Git for Windows already), but
that's a much smaller deal than the fall-out of the v2.34.0 refactorings
we had to deal with.

Thank you,
Dscho

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lazyload: use correct calling conventions
  2022-01-11 13:17   ` Johannes Schindelin
@ 2022-01-11 22:33     ` Johannes Schindelin
  2022-01-12 18:13     ` Junio C Hamano
  1 sibling, 0 replies; 5+ messages in thread
From: Johannes Schindelin @ 2022-01-11 22:33 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, reiter.christoph, Matthias Aßhauer,
	Matthias Aßhauer via GitGitGadget

Hi Junio,

On Tue, 11 Jan 2022, Johannes Schindelin wrote:

> On Mon, 10 Jan 2022, Junio C Hamano wrote:
>
> > Dscho, how does this look?  My choices are
> >
> >  - Waiting for review.
> >  - Will merge to 'next'.
> >  - Will merge to 'next' and then to 'master'.
>
> My preference is the last option, merge it down quickly.
>
> > The last one will make it as part of Git 2.35.0 final, unless there
> > is some unexpected breakage found later.
>
> I do not expect anything to break. Just like Matthias, I actually expect
> future breakages to be fended off by this.

And that future is here already. We just got a report of a problem that is
resolved by Matthias' patch:
https://github.com/git-for-windows/git/pull/3624

> Merging it into v2.35.0 final will require a tiny fixup in
> `compat/win32/flush.c` (which we carry in Git for Windows already),
> [...]

And I did that, and opened a PR already, which I plan on merging soon:
https://github.com/git-for-windows/git/pull/3626

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] lazyload: use correct calling conventions
  2022-01-11 13:17   ` Johannes Schindelin
  2022-01-11 22:33     ` Johannes Schindelin
@ 2022-01-12 18:13     ` Junio C Hamano
  1 sibling, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2022-01-12 18:13 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, reiter.christoph, Matthias Aßhauer,
	Matthias Aßhauer via GitGitGadget

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> Hi Junio,
>
> On Mon, 10 Jan 2022, Junio C Hamano wrote:
>
>> Dscho, how does this look?  My choices are
>>
>>  - Waiting for review.
>>  - Will merge to 'next'.
>>  - Will merge to 'next' and then to 'master'.
>
> My preference is the last option, merge it down quickly.

Good.  Will do.  Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-12 18:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-08 16:02 [PATCH] lazyload: use correct calling conventions Matthias Aßhauer via GitGitGadget
2022-01-10 22:09 ` Junio C Hamano
2022-01-11 13:17   ` Johannes Schindelin
2022-01-11 22:33     ` Johannes Schindelin
2022-01-12 18:13     ` Junio C Hamano

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).