Development discussion of WireGuard
 help / color / mirror / Atom feed
From: roy azachi <royaza1998@gmail.com>
To: wireguard@lists.zx2c4.com
Subject: [PATCH] Fix Wireguard-Apple Logger Memory Leak (Fix Wireguard-Apple Logger Memory Leak)
Date: Sun, 24 Apr 2022 12:19:50 +0300	[thread overview]
Message-ID: <CAHskJYgbLezQMWkC=+LaZ0vhKMz2Qg1fSmigNGGsDj0yxRVHQA@mail.gmail.com> (raw)

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)

                 reply	other threads:[~2022-04-24 20:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHskJYgbLezQMWkC=+LaZ0vhKMz2Qg1fSmigNGGsDj0yxRVHQA@mail.gmail.com' \
    --to=royaza1998@gmail.com \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).