Development discussion of WireGuard
 help / color / mirror / Atom feed
* [PATCH] Fix Wireguard-Apple Logger Memory Leak (Fix Wireguard-Apple Logger Memory Leak)
@ 2022-04-24  9:19 roy azachi
  0 siblings, 0 replies; only message in thread
From: roy azachi @ 2022-04-24  9:19 UTC (permalink / raw)
  To: wireguard

Using "Instruments" tool shows every log message from the
SystemExtension creates a persistent allocation. Although it seems
there is no retain cycle, the allocated resources are not freed, which
might be caused by the way the function is invoked using golang
instead of the usual swift invocation freeing allocations with zero
retain counter . Using the "autoreleasepool" seems to solve it.
the call tree not freed:
  12 WireGuardNetworkExtension     runtime.asmcgocall.abi0
  11 WireGuardNetworkExtension     _cgo_f1100e4bfc6f_Cfunc_callLogger
  10 WireGuardNetworkExtension     callLogger
   9 WireGuardNetworkExtension     @objc closure #1 in
WireGuardAdapter.setupLogHandler()
<reducted>/wireguard-apple/<compiler-generated>:0
   8 WireGuardNetworkExtension     closure #1 in
WireGuardAdapter.setupLogHandler()
<reducted>/wireguard-apple/Sources/WireGuardKit/WireGuardAdapter.swift:299
   7 libswiftCore.dylib      specialized static String._fromUTF8Repairing(_:)
   6 libswiftCore.dylib      specialized static
String._uncheckedFromUTF8(_:asciiPreScanResult:)
   5 libswiftCore.dylib      specialized static
__StringStorage.create(initializingFrom:isASCII:)
   4 libswiftCore.dylib      _allocateStringStorage(codeUnitCapacity:)
   3 libswiftCore.dylib      _swift_allocObject_
   2 libswiftCore.dylib      _swift_allocObject_
   1 libswiftCore.dylib      swift_slowAlloc
   0 libsystem_malloc.dylib      _malloc_zone_malloc

Signed-off-by: Roy Azachi <royaza1998@gmail.com>
---
 Sources/WireGuardKit/WireGuardAdapter.swift | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/Sources/WireGuardKit/WireGuardAdapter.swift
b/Sources/WireGuardKit/WireGuardAdapter.swift
index 4cb2e2e..3828951 100644
--- a/Sources/WireGuardKit/WireGuardAdapter.swift
+++ b/Sources/WireGuardKit/WireGuardAdapter.swift
@@ -295,11 +295,12 @@ public class WireGuardAdapter {

             let unretainedSelf =
Unmanaged<WireGuardAdapter>.fromOpaque(context)
                 .takeUnretainedValue()
+            autoreleasepool {
+                let swiftString = String(cString:
message).trimmingCharacters(in: .newlines)
+                let tunnelLogLevel = WireGuardLogLevel(rawValue:
logLevel) ?? .verbose

-            let swiftString = String(cString:
message).trimmingCharacters(in: .newlines)
-            let tunnelLogLevel = WireGuardLogLevel(rawValue:
logLevel) ?? .verbose
-
-            unretainedSelf.logHandler(tunnelLogLevel, swiftString)
+                unretainedSelf.logHandler(tunnelLogLevel, swiftString)
+            }
         }
     }

--
2.30.1 (Apple Git-130)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-24 20:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-24  9:19 [PATCH] Fix Wireguard-Apple Logger Memory Leak (Fix Wireguard-Apple Logger Memory Leak) roy azachi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).