From f947205cee76a1ae9ddf555d7e2a45675fe333fd Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 30 Oct 2020 16:58:50 +0100 Subject: [PATCH] api: use proper iso atomic semantics Signed-off-by: Jason A. Donenfeld --- api/api.vcxproj | 2 +- api/api.vcxproj.filters | 3 --- api/atomic.h | 26 -------------------------- api/pch.h | 1 - api/session.c | 10 +++++----- 5 files changed, 6 insertions(+), 36 deletions(-) delete mode 100644 api/atomic.h diff --git a/api/api.vcxproj b/api/api.vcxproj index 12ae6ca..cea689d 100644 --- a/api/api.vcxproj +++ b/api/api.vcxproj @@ -146,6 +146,7 @@ HAVE_WHQL;%(PreprocessorDefinitions) Use pch.h + /volatile:iso %(AdditionalOptions) ..\$(Configuration)\$(WintunPlatform);..\$(Configuration);%(AdditionalIncludeDirectories) @@ -194,7 +195,6 @@ - diff --git a/api/api.vcxproj.filters b/api/api.vcxproj.filters index 090a1ef..c585527 100644 --- a/api/api.vcxproj.filters +++ b/api/api.vcxproj.filters @@ -52,9 +52,6 @@ Header Files - - Header Files - Header Files diff --git a/api/atomic.h b/api/atomic.h deleted file mode 100644 index 011e53b..0000000 --- a/api/atomic.h +++ /dev/null @@ -1,26 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. - */ - -#pragma once - -#include - -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; -} diff --git a/api/pch.h b/api/pch.h index bb5b3a1..45bf3b1 100644 --- a/api/pch.h +++ b/api/pch.h @@ -6,7 +6,6 @@ #pragma once #include "adapter.h" -#include "atomic.h" #include "api.h" #include "elevate.h" #include "logger.h" diff --git a/api/session.c b/api/session.c index 71849c3..e39f21b 100644 --- a/api/session.c +++ b/api/session.c @@ -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); }