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

View File

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

View File

@ -14,12 +14,24 @@
# define _L(x) __L(x)
#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)
#define MAYBE_WOW64
# define MAYBE_WOW64 1
#else
# define MAYBE_WOW64 0
#endif
#if defined(_M_AMD64) || defined(_M_ARM64) || defined(_DEBUG)
#define ACCEPT_WOW64
#if defined(_M_AMD64) || defined(_M_ARM64)
# define ACCEPT_WOW64 1
#else
# define ACCEPT_WOW64 0
#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 HANDLE ModuleHeap;

View File

@ -3,6 +3,10 @@
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
*/
#include "entry.h"
#if ACCEPT_WOW64 == 1
#include "adapter.h"
#include "logger.h"
#include "wintun.h"
@ -14,8 +18,6 @@
#define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
#if defined(ACCEPT_WOW64) || defined(_DEBUG)
static DWORD
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());
Done();
}
#endif