From 58a80c300e4e7488ffe83fdf91df4b7145d5e933 Mon Sep 17 00:00:00 2001 From: Shekhar Rajak <60732970+shekhar-rajak@users.noreply.github.com> Date: Mon, 24 Feb 2020 22:31:16 +0530 Subject: [PATCH] Use UInt instead of ObjectIdentifier in promiseCreationStore (#1411) * Use UInt instead of ObjectIdentifier in promiseCreationStore * defined obfuscatePointerValue method --- Sources/NIO/SelectableEventLoop.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/NIO/SelectableEventLoop.swift b/Sources/NIO/SelectableEventLoop.swift index a6e3ae7cc..db83f1645 100644 --- a/Sources/NIO/SelectableEventLoop.swift +++ b/Sources/NIO/SelectableEventLoop.swift @@ -87,20 +87,20 @@ internal final class SelectableEventLoop: EventLoop { /// Creates a new `SelectableEventLoop` instance that is tied to the given `pthread_t`. private let promiseCreationStoreLock = Lock() - private var _promiseCreationStore: [ObjectIdentifier: (file: StaticString, line: UInt)] = [:] + private var _promiseCreationStore: [UInt: (file: StaticString, line: UInt)] = [:] @usableFromInline internal func promiseCreationStoreAdd(future: EventLoopFuture, file: StaticString, line: UInt) { precondition(_isDebugAssertConfiguration()) self.promiseCreationStoreLock.withLock { - self._promiseCreationStore[ObjectIdentifier(future)] = (file: file, line: line) + self._promiseCreationStore[self.obfuscatePointerValue(future)] = (file: file, line: line) } } internal func promiseCreationStoreRemove(future: EventLoopFuture) -> (file: StaticString, line: UInt) { precondition(_isDebugAssertConfiguration()) return self.promiseCreationStoreLock.withLock { - self._promiseCreationStore.removeValue(forKey: ObjectIdentifier(future))! + self._promiseCreationStore.removeValue(forKey: self.obfuscatePointerValue(future))! } } @@ -301,6 +301,14 @@ internal final class SelectableEventLoop: EventLoop { return .blockUntilTimeout(nextReady) } } + + private func obfuscatePointerValue(_ future: EventLoopFuture) -> UInt { + // Note: + // 1. 0xbf15ca5d is randomly picked such that it fits into both 32 and 64 bit address spaces + // 2. XOR with 0xbf15ca5d so that Memory Graph Debugger and other memory debugging tools + // won't see it as a reference. + return UInt(bitPattern: ObjectIdentifier(future)) ^ 0xbf15ca5d + } /// Start processing I/O and tasks for this `SelectableEventLoop`. This method will continue running (and so block) until the `SelectableEventLoop` is closed. internal func run() throws {