api: use proper iso atomic semantics

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-10-30 16:58:50 +01:00 committed by Simon Rozman
parent b4a1494fb2
commit f947205cee
5 changed files with 6 additions and 36 deletions

View File

@ -146,6 +146,7 @@
<PreprocessorDefinitions Condition="Exists('$(OutDir)whql\')">HAVE_WHQL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalOptions>/volatile:iso %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>..\$(Configuration)\$(WintunPlatform);..\$(Configuration);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
@ -194,7 +195,6 @@
<ItemGroup>
<ClInclude Include="api.h" />
<ClInclude Include="adapter.h" />
<ClInclude Include="atomic.h" />
<ClInclude Include="elevate.h" />
<ClInclude Include="logger.h" />
<ClInclude Include="namespace.h" />

View File

@ -52,9 +52,6 @@
<ClInclude Include="wintun.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="atomic.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="elevate.h">
<Filter>Header Files</Filter>
</ClInclude>

View File

@ -1,26 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
*/
#pragma once
#include <Windows.h>
static __forceinline VOID
InterlockedSetU(_Inout_ _Interlocked_operand_ ULONG volatile *Target, _In_ ULONG Value)
{
*Target = Value;
}
static __forceinline LONG
InterlockedGet(_In_ _Interlocked_operand_ LONG volatile *Value)
{
return *Value;
}
static __forceinline ULONG
InterlockedGetU(_In_ _Interlocked_operand_ ULONG volatile *Value)
{
return *Value;
}

View File

@ -6,7 +6,6 @@
#pragma once
#include "adapter.h"
#include "atomic.h"
#include "api.h"
#include "elevate.h"
#include "logger.h"

View File

@ -172,7 +172,7 @@ WintunReceivePacket(_In_ TUN_SESSION *Session, _Out_bytecapcount_(*PacketSize) B
Result = ERROR_HANDLE_EOF;
goto cleanup;
}
const ULONG BuffTail = InterlockedGetU(&Session->Descriptor.Send.Ring->Tail);
const ULONG BuffTail = ReadULongAcquire(&Session->Descriptor.Send.Ring->Tail);
if (BuffTail >= Session->Capacity)
{
Result = ERROR_HANDLE_EOF;
@ -226,7 +226,7 @@ WintunReceiveRelease(_In_ TUN_SESSION *Session, _In_ const BYTE *Packet)
Session->Send.HeadRelease = TUN_RING_WRAP(Session->Send.HeadRelease + AlignedPacketSize, Session->Capacity);
Session->Send.PacketsToRelease--;
}
InterlockedSetU(&Session->Descriptor.Send.Ring->Head, Session->Send.HeadRelease);
WriteULongRelease(&Session->Descriptor.Send.Ring->Head, Session->Send.HeadRelease);
LeaveCriticalSection(&Session->Send.Lock);
}
@ -241,7 +241,7 @@ WintunAllocateSendPacket(_In_ TUN_SESSION *Session, _In_ DWORD PacketSize, _Out_
goto cleanup;
}
const ULONG AlignedPacketSize = TUN_ALIGN(sizeof(TUN_PACKET) + PacketSize);
const ULONG BuffHead = InterlockedGetU(&Session->Descriptor.Receive.Ring->Head);
const ULONG BuffHead = ReadULongAcquire(&Session->Descriptor.Receive.Ring->Head);
if (BuffHead >= Session->Capacity)
{
Result = ERROR_HANDLE_EOF;
@ -281,8 +281,8 @@ WintunSendPacket(_In_ TUN_SESSION *Session, _In_ const BYTE *Packet)
TUN_RING_WRAP(Session->Receive.TailRelease + AlignedPacketSize, Session->Capacity);
Session->Receive.PacketsToRelease--;
}
InterlockedSetU(&Session->Descriptor.Receive.Ring->Tail, Session->Receive.TailRelease);
if (InterlockedGet(&Session->Descriptor.Receive.Ring->Alertable))
WriteULongRelease(&Session->Descriptor.Receive.Ring->Tail, Session->Receive.TailRelease);
if (ReadAcquire(&Session->Descriptor.Receive.Ring->Alertable))
SetEvent(Session->Descriptor.Receive.TailMoved);
LeaveCriticalSection(&Session->Receive.Lock);
}