Skip to contentSkip to conversation

test-drop-caches: simplify delay loading of NtSetSystemInformation

PATCH Superseded Ben Peart 7 participants 9 messages Apr 30, 2018 – May 2, 2018

lore
Share & tools
lore

Ben Peart

author
#0 lore raw

PATCH 1/1 test-drop-caches: simplify delay loading of NtSetSystemInformation

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

---

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(-)
t/helper/test-drop-caches.c +4 -12
index 838760898b..dd41da1a2c 100644
@@ -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

Signature · 1 line
2.17.0.windows.1
#1 lore raw
Quoted 19 lines · Ben Peart

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

---

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(-)
t/helper/test-drop-caches.c +0 -0
index 838760898b..dd41da1a2c 100644

t/helper/test-drop-caches.c line 3

Quoted 3 lines
t/helper/test-drop-caches.c +1 -0
@@ -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?

Quoted 44 lines
t/helper/test-drop-caches.c +3 -12
 #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

Signature · 1 line
Duy
#2 lore raw
Quoted 15 lines · Duy Nguyen
Quoted 14 lines · Ben Peart

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

---

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(-)
Quoted 12 lines

t/helper/test-drop-caches.c line 3

Quoted 8 lines
t/helper/test-drop-caches.c +1 -0
index 838760898b..dd41da1a2c 100644
@@ -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.

#4 lore raw
In reply to Ben Peart
Quoted 1 line · Ben Peart

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

#5 lore raw
Quoted 5 lines · Stefan Beller
Quoted 1 line · Ben Peart

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?

Quoted 8 lines

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)

#6 lore raw
In reply to Ben Peart

Am 30.04.2018 um 16:26 schrieb Ben Peart:

Quoted 12 lines
t/helper/test-drop-caches.c +0 -2
@@ -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);

t/helper/test-drop-caches.c static int cmd_dropcaches(void) · line 97

Quoted 10 lines
t/helper/test-drop-caches.c +1 -8
 
-	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.

t/helper/test-drop-caches.c static int cmd_dropcaches(void) · line 113

Quoted 13 lines
t/helper/test-drop-caches.c +2 -2
+	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

author
#7 lore raw

PATCH v2 1/1 test-drop-caches: simplify delay loading of NtSetSystemInformation

In reply to Ben Peart

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)

---

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(-)
t/helper/test-drop-caches.c +4 -12
index 838760898b..d6bcfddf13 100644
@@ -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

Signature · 1 line
2.17.0.windows.1
#8 lore raw

Hi Ben,

Quoted 7 lines · Ben Peart

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)

---

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