api: ensure more code compiles by using dead code elimination

It'd be nicer to do this via

if (is_defined(HAVE_WHATEVER))

But MSVC won't work with the linux kernel macros for this. Ongoing
research.

Nevertheless, this makes most of the program always pass through the
compiler's type checker, only to have dead code removed later.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-11-03 02:09:00 +01:00
parent 353cfa562e
commit 1201c9f346
4 changed files with 33 additions and 29 deletions

View File

@ -340,7 +340,8 @@ EnableAllOurAdapters(_In_ HDEVINFO DevInfo, _In_ SP_DEVINFO_DATA_LIST *AdaptersT
void void
AdapterInit(void) AdapterInit(void)
{ {
#ifdef MAYBE_WOW64 if (!MAYBE_WOW64)
return;
typedef BOOL(WINAPI * IsWow64Process2_t)( typedef BOOL(WINAPI * IsWow64Process2_t)(
_In_ HANDLE hProcess, _Out_ USHORT * pProcessMachine, _Out_opt_ USHORT * pNativeMachine); _In_ HANDLE hProcess, _Out_ USHORT * pProcessMachine, _Out_opt_ USHORT * pNativeMachine);
HANDLE Kernel32; HANDLE Kernel32;
@ -354,7 +355,6 @@ AdapterInit(void)
NativeMachine = NativeMachine =
IsWow64Process(GetCurrentProcess(), &IsWoW64) && IsWoW64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_PROCESS; IsWow64Process(GetCurrentProcess(), &IsWoW64) && IsWoW64 ? IMAGE_FILE_MACHINE_AMD64 : IMAGE_FILE_PROCESS;
} }
#endif
} }
static BOOL static BOOL
@ -801,13 +801,13 @@ RtlGetNtVersionNumbers(_Out_opt_ DWORD *MajorVersion, _Out_opt_ DWORD *MinorVers
static BOOL static BOOL
HaveWHQL(void) HaveWHQL(void)
{ {
#if defined(HAVE_WHQL) if (HAVE_WHQL)
DWORD MajorVersion; {
RtlGetNtVersionNumbers(&MajorVersion, NULL, NULL); DWORD MajorVersion;
return MajorVersion >= 10; RtlGetNtVersionNumbers(&MajorVersion, NULL, NULL);
#else return MajorVersion >= 10;
}
return FALSE; return FALSE;
#endif
} }
static WINTUN_STATUS static WINTUN_STATUS
@ -1682,15 +1682,10 @@ WintunCreateAdapter(
RebootRequired = &DummyRebootRequired; RebootRequired = &DummyRebootRequired;
*RebootRequired = FALSE; *RebootRequired = FALSE;
DWORD Result; DWORD Result;
#ifdef MAYBE_WOW64 if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS)
if (NativeMachine != IMAGE_FILE_PROCESS)
{
Result = CreateAdapterViaRundll32(Pool, Name, RequestedGUID, Adapter, RebootRequired); Result = CreateAdapterViaRundll32(Pool, Name, RequestedGUID, Adapter, RebootRequired);
RevertToSelf(); else
return Result; Result = CreateAdapter(Pool, Name, RequestedGUID, Adapter, RebootRequired);
}
#endif
Result = CreateAdapter(Pool, Name, RequestedGUID, Adapter, RebootRequired);
RevertToSelf(); RevertToSelf();
return Result; return Result;
} }
@ -1706,14 +1701,12 @@ WintunDeleteAdapter(_In_ const WINTUN_ADAPTER *Adapter, _In_ BOOL ForceCloseSess
RebootRequired = &DummyRebootRequired; RebootRequired = &DummyRebootRequired;
*RebootRequired = FALSE; *RebootRequired = FALSE;
DWORD Result; DWORD Result;
#ifdef MAYBE_WOW64 if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS)
if (NativeMachine != IMAGE_FILE_PROCESS)
{ {
Result = DeleteAdapterViaRundll32(Adapter, ForceCloseSessions, RebootRequired); Result = DeleteAdapterViaRundll32(Adapter, ForceCloseSessions, RebootRequired);
RevertToSelf(); RevertToSelf();
return Result; return Result;
} }
#endif
HDEVINFO DevInfo; HDEVINFO DevInfo;
SP_DEVINFO_DATA DevInfoData; SP_DEVINFO_DATA DevInfoData;
@ -1796,14 +1789,12 @@ WintunDeleteDriver(void)
DWORD Result = ERROR_SUCCESS; DWORD Result = ERROR_SUCCESS;
#ifdef MAYBE_WOW64 if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS)
if (NativeMachine != IMAGE_FILE_PROCESS)
{ {
Result = DeleteDriverViaRundll32(); Result = DeleteDriverViaRundll32();
RevertToSelf(); RevertToSelf();
return Result; return Result;
} }
#endif
/* DeleteAllOurAdapters(); */ /* DeleteAllOurAdapters(); */
HANDLE DriverInstallationLock = NamespaceTakeDriverInstallationMutex(); HANDLE DriverInstallationLock = NamespaceTakeDriverInstallationMutex();

View File

@ -33,7 +33,7 @@ static FARPROC WINAPI DelayedLoadLibraryHook(unsigned dliNotify, PDelayLoadInfo
} }
const PfnDliHook __pfnDliNotifyHook2 = DelayedLoadLibraryHook; const PfnDliHook __pfnDliNotifyHook2 = DelayedLoadLibraryHook;
#define NOT 1
BOOL APIENTRY BOOL APIENTRY
DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
{ {

View File

@ -14,12 +14,24 @@
# define _L(x) __L(x) # define _L(x) __L(x)
#endif #endif
/* TODO: Replace with is_defined. MSVC has issues with the linux kernel varadic macro trick for this. */
#if defined(_M_IX86) || defined(_M_ARM) #if defined(_M_IX86) || defined(_M_ARM)
#define MAYBE_WOW64 # define MAYBE_WOW64 1
#else
# define MAYBE_WOW64 0
#endif #endif
#if defined(_M_AMD64) || defined(_M_ARM64) || defined(_DEBUG) #if defined(_M_AMD64) || defined(_M_ARM64)
#define ACCEPT_WOW64 # define ACCEPT_WOW64 1
#else
# define ACCEPT_WOW64 0
#endif #endif
#ifdef HAVE_WHQL
# undef HAVE_WHQL
# define HAVE_WHQL 1
#else
# define HAVE_WHQL 0
#endif
#pragma warning(disable : 4127) /* conditional expression is constant */
extern HINSTANCE ResourceModule; extern HINSTANCE ResourceModule;
extern HANDLE ModuleHeap; extern HANDLE ModuleHeap;

View File

@ -3,6 +3,10 @@
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
*/ */
#include "entry.h"
#if ACCEPT_WOW64 == 1
#include "adapter.h" #include "adapter.h"
#include "logger.h" #include "logger.h"
#include "wintun.h" #include "wintun.h"
@ -14,8 +18,6 @@
#define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__) #define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
#if defined(ACCEPT_WOW64) || defined(_DEBUG)
static DWORD static DWORD
WriteFormatted(_In_ DWORD StdHandle, _In_z_ const WCHAR *Template, ...) WriteFormatted(_In_ DWORD StdHandle, _In_z_ const WCHAR *Template, ...)
{ {
@ -147,5 +149,4 @@ VOID __stdcall DeleteDriver(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int n
WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X!", WintunDeleteDriver()); WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X!", WintunDeleteDriver());
Done(); Done();
} }
#endif #endif