Skip to content

Conversation

aschwaighofer
Copy link
Contributor

String's hashValue function is implemented in terms of Foundation's hash
function in a runtime function on darwin platforms. For non-ASCII strings we
will call str.decomposedStringWithCanonicalMapping inside this runtime function
which will allocate a new NSString and return the result in the current
autorelease pool. We implemented this function in a file compiled without ARC.
This meant that we would leak said NSString into the current active autorelease
pool.
This patch moves the implementation to a file compiled with ARC. ARC will insert
objc_retainAutoreleasedReturnValue call and on platforms that require it an
marker for the hand-off of the autoreleased return value optimization.

SR-4889
rdar://32199117

  • Explanation: The standard library’s hashValue, uppercased, and loweredCased functions “leak” a temporary object into the current autorelease pool. This autorelease pool can be scope such that this means unbounded memory growth.
    The reason is that we implement the hashing function by a runtime stub that calls str.decomposedStringWithCanonicalMapping.hash. The decomposedStringWithCanonicalMapping call puts its result as a temporary object into the current autorelease pool. We did not compile the file that implemented this method with ARC and so the compiler did not insert code to remove the temporary object from the autorelease pool.
    The fix is to move these runtime stubs into a file that is compiled with ARC such that the temporary object is released at the end of the runtime stub call.

  • Scope: Calls to hashValue, uppercased, and lowercased leak an object into the current active autorelease pool whose scope is to large effectively causing a memory leak or a least unexpected memory growth. This code has been around since several releases.

  • Risk: Low. The runtime stubs where moved into a file that is compiled with ARC.

  • Testing: Runtime and assembly tests where added to the swift regression tests

…to-rel…

…eased return value optimization to an ARC compiled file

String's hashValue function is implemented in terms of Foundation's hash
function in a runtime function on darwin platforms. For non-ASCII strings we
will call str.decomposedStringWithCanonicalMapping inside this runtime function
which will allocate a new NSString and return the result in the current
autorelease pool. We implemented this function in a file compiled without ARC.
This meant that we would leak said NSString into the current active autorelease
pool.
This patch moves the implementation to a file compiled with ARC. ARC will insert
objc_retainAutoreleasedReturnValue call and on platforms that require it an
marker for the hand-off of the autoreleased return value optimization.

SR-4889
rdar://32199117
@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test

@tkremenek tkremenek merged commit b878d3b into swiftlang:swift-4.0-branch Oct 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants