# [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation

9 messages from 2018-04-30 to 2018-05-02. Participants: Ben Peart, Duy Nguyen, Eric Sunshine, Stefan Beller, Johannes Sixt, Johannes Schindelin.
Thread: https://gitlist.dev/t/48385

## Ben Peart, 2018-04-30 14:26

Subject: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <20180430142555.23260-1-benpeart@microsoft.com>
URL: https://gitlist.dev/e/20180430142555.23260-1-benpeart%40microsoft.com

```
Take advantage of the recent addition of support for lazy loading functions
on Windows to simplfy the loading of NtSetSystemInformation.

Signed-off-by: Ben Peart <benpeart@microsoft.com>
---

Notes:
    Base Ref: master
    Web-Diff: https://github.com/benpeart/git/commit/6e6ce4a788
    Checkout: git fetch https://github.com/benpeart/git test-drop-caches-v1 && git checkout 6e6ce4a788

 t/helper/test-drop-caches.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
index 838760898b..dd41da1a2c 100644
--- a/t/helper/test-drop-caches.c
+++ b/t/helper/test-drop-caches.c
@@ -1,5 +1,6 @@
 #include "test-tool.h"
 #include "git-compat-util.h"
+#include "lazyload.h"
 
 #if defined(GIT_WINDOWS_NATIVE)
 
@@ -82,8 +83,6 @@ static int cmd_dropcaches(void)
 {
 	HANDLE hProcess = GetCurrentProcess();
 	HANDLE hToken;
-	HMODULE ntdll;
-	DWORD(WINAPI *NtSetSystemInformation)(INT, PVOID, ULONG);
 	SYSTEM_MEMORY_LIST_COMMAND command;
 	int status;
 
@@ -95,14 +94,9 @@ static int cmd_dropcaches(void)
 
 	CloseHandle(hToken);
 
-	ntdll = LoadLibrary("ntdll.dll");
-	if (!ntdll)
-		return error("Can't load ntdll.dll, wrong Windows version?");
-
-	NtSetSystemInformation =
-		(DWORD(WINAPI *)(INT, PVOID, ULONG))GetProcAddress(ntdll, "NtSetSystemInformation");
-	if (!NtSetSystemInformation)
-		return error("Can't get function addresses, wrong Windows version?");
+	DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
+	if (!INIT_PROC_ADDR(NtSetSystemInformation))
+		return error("Could not find NtSetSystemInformation() function");
 
 	command = MemoryPurgeStandbyList;
 	status = NtSetSystemInformation(
@@ -115,8 +109,6 @@ static int cmd_dropcaches(void)
 	else if (status != STATUS_SUCCESS)
 		error("Unable to execute the memory list command %d", status);
 
-	FreeLibrary(ntdll);
-
 	return status;
 }
 

base-commit: 1f1cddd558b54bb0ce19c8ace353fd07b758510d
-- 
2.17.0.windows.1


```

## Duy Nguyen, 2018-04-30 14:57

Subject: Re: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <CACsJy8CY1fNY9OPq9nisDC3X93QXjntre-vFgQ0OeuO=B0YV=Q@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8CY1fNY9OPq9nisDC3X93QXjntre-vFgQ0OeuO%3DB0YV%3DQ%40mail.gmail.com
In-Reply-To: <20180430142555.23260-1-benpeart@microsoft.com>

```
On Mon, Apr 30, 2018 at 4:26 PM, Ben Peart <Ben.Peart@microsoft.com> wrote:
> Take advantage of the recent addition of support for lazy loading functions
> on Windows to simplfy the loading of NtSetSystemInformation.
>
> Signed-off-by: Ben Peart <benpeart@microsoft.com>
> ---
>
> Notes:
>     Base Ref: master
>     Web-Diff: https://github.com/benpeart/git/commit/6e6ce4a788
>     Checkout: git fetch https://github.com/benpeart/git test-drop-caches-v1 && git checkout 6e6ce4a788
>
>  t/helper/test-drop-caches.c | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)
>
> diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
> index 838760898b..dd41da1a2c 100644
> --- a/t/helper/test-drop-caches.c
> +++ b/t/helper/test-drop-caches.c
> @@ -1,5 +1,6 @@
>  #include "test-tool.h"
>  #include "git-compat-util.h"
> +#include "lazyload.h"

This is in compat/win32, should it be inside the "if defined
(GIT_WINDOWS_NATIVE)" block instead of here?

>
>  #if defined(GIT_WINDOWS_NATIVE)
>
> @@ -82,8 +83,6 @@ static int cmd_dropcaches(void)
>  {
>         HANDLE hProcess = GetCurrentProcess();
>         HANDLE hToken;
> -       HMODULE ntdll;
> -       DWORD(WINAPI *NtSetSystemInformation)(INT, PVOID, ULONG);
>         SYSTEM_MEMORY_LIST_COMMAND command;
>         int status;
>
> @@ -95,14 +94,9 @@ static int cmd_dropcaches(void)
>
>         CloseHandle(hToken);
>
> -       ntdll = LoadLibrary("ntdll.dll");
> -       if (!ntdll)
> -               return error("Can't load ntdll.dll, wrong Windows version?");
> -
> -       NtSetSystemInformation =
> -               (DWORD(WINAPI *)(INT, PVOID, ULONG))GetProcAddress(ntdll, "NtSetSystemInformation");
> -       if (!NtSetSystemInformation)
> -               return error("Can't get function addresses, wrong Windows version?");
> +       DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
> +       if (!INIT_PROC_ADDR(NtSetSystemInformation))
> +               return error("Could not find NtSetSystemInformation() function");
>
>         command = MemoryPurgeStandbyList;
>         status = NtSetSystemInformation(
> @@ -115,8 +109,6 @@ static int cmd_dropcaches(void)
>         else if (status != STATUS_SUCCESS)
>                 error("Unable to execute the memory list command %d", status);
>
> -       FreeLibrary(ntdll);
> -
>         return status;
>  }
>
>
> base-commit: 1f1cddd558b54bb0ce19c8ace353fd07b758510d
> --
> 2.17.0.windows.1
>



-- 
Duy

```

## Ben Peart, 2018-04-30 15:17

Subject: Re: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <c05d652d-3275-4b04-c350-452e1c409493@gmail.com>
URL: https://gitlist.dev/e/c05d652d-3275-4b04-c350-452e1c409493%40gmail.com
In-Reply-To: <CACsJy8CY1fNY9OPq9nisDC3X93QXjntre-vFgQ0OeuO=B0YV=Q@mail.gmail.com>

```


On 4/30/2018 10:57 AM, Duy Nguyen wrote:
> On Mon, Apr 30, 2018 at 4:26 PM, Ben Peart <Ben.Peart@microsoft.com> wrote:
>> Take advantage of the recent addition of support for lazy loading functions
>> on Windows to simplfy the loading of NtSetSystemInformation.
>>
>> Signed-off-by: Ben Peart <benpeart@microsoft.com>
>> ---
>>
>> Notes:
>>      Base Ref: master
>>      Web-Diff: https://github.com/benpeart/git/commit/6e6ce4a788
>>      Checkout: git fetch https://github.com/benpeart/git test-drop-caches-v1 && git checkout 6e6ce4a788
>>
>>   t/helper/test-drop-caches.c | 16 ++++------------
>>   1 file changed, 4 insertions(+), 12 deletions(-)
>>
>> diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
>> index 838760898b..dd41da1a2c 100644
>> --- a/t/helper/test-drop-caches.c
>> +++ b/t/helper/test-drop-caches.c
>> @@ -1,5 +1,6 @@
>>   #include "test-tool.h"
>>   #include "git-compat-util.h"
>> +#include "lazyload.h"
> 
> This is in compat/win32, should it be inside the "if defined
> (GIT_WINDOWS_NATIVE)" block instead of here?
> 

Yes, that does make sense.  No other platform will need/want it.  I'll 
wait to see if there is any other feedback and will submit an updated 
patch.  Thanks for the catch.

```

## Eric Sunshine, 2018-04-30 17:05

Subject: Re: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <CAPig+cQPk9hj2JqOqFiZmb=Xcdi9P4dy7WUeJaj4AaUUMs10HQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQPk9hj2JqOqFiZmb%3DXcdi9P4dy7WUeJaj4AaUUMs10HQ%40mail.gmail.com
In-Reply-To: <20180430142555.23260-1-benpeart@microsoft.com>

```
On Mon, Apr 30, 2018 at 10:26 AM, Ben Peart <Ben.Peart@microsoft.com> wrote:
> Take advantage of the recent addition of support for lazy loading functions
> on Windows to simplfy the loading of NtSetSystemInformation.

s/simplfy/simplify/

> Signed-off-by: Ben Peart <benpeart@microsoft.com>

```

## Stefan Beller, 2018-04-30 18:12

Subject: Re: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <CAGZ79kZvyFTKadjY11Xu+KcV9OUe-4hbjgPzBDcD52X2nZKCzg@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kZvyFTKadjY11Xu%2BKcV9OUe-4hbjgPzBDcD52X2nZKCzg%40mail.gmail.com
In-Reply-To: <20180430142555.23260-1-benpeart@microsoft.com>

```
On Mon, Apr 30, 2018 at 7:26 AM, Ben Peart <Ben.Peart@microsoft.com> wrote:
> Take advantage of the recent addition of support for lazy loading functions

Care to specify "recent additions"? Are these in Git code or somewhere else?

I find this alias handy, as then I can describe commits
in commit messages via "git gcs <commitish>

  alias.gcs=show --date=short -s --pretty='format:%h (%s, %ad)'

Thanks,
Stefan

```

## Ben Peart, 2018-04-30 18:43

Subject: Re: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <92ff364a-b46c-2274-a3fd-0b68eaa4159e@gmail.com>
URL: https://gitlist.dev/e/92ff364a-b46c-2274-a3fd-0b68eaa4159e%40gmail.com
In-Reply-To: <CAGZ79kZvyFTKadjY11Xu+KcV9OUe-4hbjgPzBDcD52X2nZKCzg@mail.gmail.com>

```


On 4/30/2018 2:12 PM, Stefan Beller wrote:
> On Mon, Apr 30, 2018 at 7:26 AM, Ben Peart <Ben.Peart@microsoft.com> wrote:
>> Take advantage of the recent addition of support for lazy loading functions
> 
> Care to specify "recent additions"? Are these in Git code or somewhere else?
> 
> I find this alias handy, as then I can describe commits
> in commit messages via "git gcs <commitish>
> 
>    alias.gcs=show --date=short -s --pretty='format:%h (%s, %ad)'
> 
> Thanks,
> Stefan
> 

I guess "recent" is relative. :)

db2f7c48cb (Win32: simplify loading of DLL functions, 2017-09-25)

```

## Johannes Sixt, 2018-04-30 19:21

Subject: Re: [PATCH v1] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <2c785509-8e9f-816b-9ab0-3ff2f3e4a3f3@kdbg.org>
URL: https://gitlist.dev/e/2c785509-8e9f-816b-9ab0-3ff2f3e4a3f3%40kdbg.org
In-Reply-To: <20180430142555.23260-1-benpeart@microsoft.com>

```
Am 30.04.2018 um 16:26 schrieb Ben Peart:
> @@ -82,8 +83,6 @@ static int cmd_dropcaches(void)
>   {
>   	HANDLE hProcess = GetCurrentProcess();
>   	HANDLE hToken;
> -	HMODULE ntdll;
> -	DWORD(WINAPI *NtSetSystemInformation)(INT, PVOID, ULONG);
>   	SYSTEM_MEMORY_LIST_COMMAND command;
>   	int status;
>   
> @@ -95,14 +94,9 @@ static int cmd_dropcaches(void)
>   
>   	CloseHandle(hToken);
>   
> -	ntdll = LoadLibrary("ntdll.dll");
> -	if (!ntdll)
> -		return error("Can't load ntdll.dll, wrong Windows version?");
> -
> -	NtSetSystemInformation =
> -		(DWORD(WINAPI *)(INT, PVOID, ULONG))GetProcAddress(ntdll, "NtSetSystemInformation");
> -	if (!NtSetSystemInformation)
> -		return error("Can't get function addresses, wrong Windows version?");
> +	DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);

This is a declaration-after-statement. Even though this is not in 
generic code, it is all too easy to trigger a warning from GCC if a 
corresponding warning is turned on.

> +	if (!INIT_PROC_ADDR(NtSetSystemInformation))
> +		return error("Could not find NtSetSystemInformation() function");
>   
>   	command = MemoryPurgeStandbyList;
>   	status = NtSetSystemInformation(
> @@ -115,8 +109,6 @@ static int cmd_dropcaches(void)
>   	else if (status != STATUS_SUCCESS)
>   		error("Unable to execute the memory list command %d", status);
>   
> -	FreeLibrary(ntdll);
> -
>   	return status;
>   }

-- Hannes

```

## Ben Peart, 2018-05-01 12:46

Subject: [PATCH v2] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <20180501124603.21896-1-benpeart@microsoft.com>
URL: https://gitlist.dev/e/20180501124603.21896-1-benpeart%40microsoft.com
In-Reply-To: <20180430142555.23260-1-benpeart@microsoft.com>

```
Take advantage of the recent addition of support for lazy loading functions[1]
on Windows to simplify the loading of NtSetSystemInformation.

[1] db2f7c48cb (Win32: simplify loading of DLL functions, 2017-09-25)

Signed-off-by: Ben Peart <benpeart@microsoft.com>
---

Notes:
    Base Ref: master
    Web-Diff: https://github.com/benpeart/git/commit/3c9974548a
    Checkout: git fetch https://github.com/benpeart/git test-drop-caches-v2 && git checkout 3c9974548a
    
    ### Interdiff (v1..v2):
    
    diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
    index dd41da1a2c..d6bcfddf13 100644
    --- a/t/helper/test-drop-caches.c
    +++ b/t/helper/test-drop-caches.c
    @@ -1,8 +1,8 @@
     #include "test-tool.h"
     #include "git-compat-util.h"
    -#include "lazyload.h"
    
     #if defined(GIT_WINDOWS_NATIVE)
    +#include "lazyload.h"
    
     static int cmd_sync(void)
     {
    @@ -83,6 +83,7 @@ static int cmd_dropcaches(void)
     {
     	HANDLE hProcess = GetCurrentProcess();
     	HANDLE hToken;
    +	DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
     	SYSTEM_MEMORY_LIST_COMMAND command;
     	int status;
    
    @@ -94,7 +95,6 @@ static int cmd_dropcaches(void)
    
     	CloseHandle(hToken);
    
    -	DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
     	if (!INIT_PROC_ADDR(NtSetSystemInformation))
     		return error("Could not find NtSetSystemInformation() function");
    
    ### Patches

 t/helper/test-drop-caches.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
index 838760898b..d6bcfddf13 100644
--- a/t/helper/test-drop-caches.c
+++ b/t/helper/test-drop-caches.c
@@ -2,6 +2,7 @@
 #include "git-compat-util.h"
 
 #if defined(GIT_WINDOWS_NATIVE)
+#include "lazyload.h"
 
 static int cmd_sync(void)
 {
@@ -82,8 +83,7 @@ static int cmd_dropcaches(void)
 {
 	HANDLE hProcess = GetCurrentProcess();
 	HANDLE hToken;
-	HMODULE ntdll;
-	DWORD(WINAPI *NtSetSystemInformation)(INT, PVOID, ULONG);
+	DECLARE_PROC_ADDR(ntdll.dll, DWORD, NtSetSystemInformation, INT, PVOID, ULONG);
 	SYSTEM_MEMORY_LIST_COMMAND command;
 	int status;
 
@@ -95,14 +95,8 @@ static int cmd_dropcaches(void)
 
 	CloseHandle(hToken);
 
-	ntdll = LoadLibrary("ntdll.dll");
-	if (!ntdll)
-		return error("Can't load ntdll.dll, wrong Windows version?");
-
-	NtSetSystemInformation =
-		(DWORD(WINAPI *)(INT, PVOID, ULONG))GetProcAddress(ntdll, "NtSetSystemInformation");
-	if (!NtSetSystemInformation)
-		return error("Can't get function addresses, wrong Windows version?");
+	if (!INIT_PROC_ADDR(NtSetSystemInformation))
+		return error("Could not find NtSetSystemInformation() function");
 
 	command = MemoryPurgeStandbyList;
 	status = NtSetSystemInformation(
@@ -115,8 +109,6 @@ static int cmd_dropcaches(void)
 	else if (status != STATUS_SUCCESS)
 		error("Unable to execute the memory list command %d", status);
 
-	FreeLibrary(ntdll);
-
 	return status;
 }
 

base-commit: 1f1cddd558b54bb0ce19c8ace353fd07b758510d
-- 
2.17.0.windows.1


```

## Johannes Schindelin, 2018-05-02 06:55

Subject: Re: [PATCH v2] test-drop-caches: simplify delay loading of NtSetSystemInformation
Message-ID: <nycvar.QRO.7.76.6.1805020854550.73@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.1805020854550.73%40tvgsbejvaqbjf.bet
In-Reply-To: <20180501124603.21896-1-benpeart@microsoft.com>

```
Hi Ben,

On Tue, 1 May 2018, Ben Peart wrote:

> Take advantage of the recent addition of support for lazy loading functions[1]
> on Windows to simplify the loading of NtSetSystemInformation.
> 
> [1] db2f7c48cb (Win32: simplify loading of DLL functions, 2017-09-25)
> 
> Signed-off-by: Ben Peart <benpeart@microsoft.com>
> ---

Thank you for your diligence. I read over the code, and it is still good,
so you can add an Acked-by: or Reviewed-by: (or Junio can).

Thanks,
Johannes

```
