Skip to content

[CF] Avoid using malloc introspection. #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2020

Conversation

3405691582
Copy link
Member

malloc introspection, as afforded by malloc_size, malloc_usable_size, or _msize, is nonportable (and thus not guaranteed to be present on all platforms), not guaranteed to be accurate (though certainly an upper bound), and is generally recommended for debugging.

For those reasons, the following commits are introduced in this PR:

  • CFBasicHash.c: only expose CFBasicHashGetSize's total branch when one or both of the debugging symbols ENABLE_MEMORY_COUNTERS or ENABLE_DTRACE_PROBES are true
  • CFStorage.c: remove apparently unused function __CFStorageEstimateTotalAllocatedSize. If this is necessary for something, please let me know what constraints are required to avoid exposing the malloc_sizecall.

CFBasicHashGetSize has an argument, total, to enable whether to
presumably calculate the size its dependent data structures.
CFBasicHashGetSize does not appear outside CFBasicHash.c, and this total
argument is true only when ENABLE_MEMORY_COUNTERS is true or
transitively when ENABLE_DTRACE_PROBES is true. These preprocessor
symbols appear to be to control debugging and other informational
purposes.

malloc introspection, as afforded by malloc_size, malloc_usable_size, or
msize, is nonportable (and thus not guaranteed to be present on all
platforms), not guaranteed to be accurate (though certainly an upper
bound), and is generally recommended for debugging.

For those reasons, only expose the call to malloc_size when either of
ENABLE_MEMORY_COUNTERS or ENABLE_DTRACE_PROBES is true. This avoids an
undefined symbol issue when malloc introspection is unavailable, but
does mean these counters and other information is not available on
platforms when malloc_size is not present.
@spevans
Copy link
Contributor

spevans commented Jan 23, 2020

It seems that the standard library uses wraps malloc_size(), malloc_usable_size(), _msize() so isnt it assumed that at least one of these is present? Can we not wrap them the same way here?

https://github.com/apple/swift/blob/f1ef08af7cafbec1a98ac21dd89a54764950fcc8/stdlib/public/SwiftShims/LibcShims.h#L103

@3405691582
Copy link
Member Author

Yes, the Swift stdlib also uses malloc_size, but that is something of a separate issue[1], because CF ought to be able to be built standalone outside of Swift.

[1] This is really unfortunate, since it's basically used at the heart of ManagedBuffer.capacity (and somewhere in ContiguousArray also, from memory): it's exposed as part of a public API and thus isn't something that is only exposed during a debugging rebuild like here in this PR. But trying to deal with that in the right way is a PR for another day.

@compnerd
Copy link
Member

CC: @parkera @millenomi

I believe that __CFStorageEstimateTotalAllocatedSize is a debugging aid, meant to be invoked from the debugger by a developer. The user-visible aspect of this is that the description of the CFBasicHash will now show the same values for size and total_size, which I don't believe to be something that they should be parsing.

@compnerd
Copy link
Member

@swift-ci please test

@compnerd
Copy link
Member

@3405691582 would you mind updating the commit message for the second change to remove the statement that the function is vestigial, as I believe it to be a debugger-only debugging aid function.

@compnerd
Copy link
Member

@swift-ci please test macOS platform

This function is tagged with __attribute__((unused)) and certainly
appears to be unused. It may be a function intended for use in a
debugger context. As it is a static function, the reference to this
function and malloc_size will not necessarily be present when built,
_but_ this does cause a compiler warning when building on systems where
malloc_size or alternatives are not available.

malloc introspection, as afforded by malloc_size, malloc_usable_size, or
msize, is nonportable (and thus not guaranteed to be present on all
platforms), not guaranteed to be accurate (though certainly an upper
bound), and is generally recommended for debugging.

For those reasons, remove this function.
@3405691582 3405691582 force-pushed the PleaseAvoidMallocIntrospection branch from cd2eb7a to 74cd0cf Compare January 24, 2020 22:28
@3405691582
Copy link
Member Author

3405691582 commented Jan 24, 2020

Actually, after looking closer at __CFStorageEstimateTotalAllocatedSize, this may be more complicated: as it's a static function, this function's code is not necessarily emitted; perhaps some concoction of compiler flags (that I'm not even confident exists) may be necessary to get that function to be emitted in order to be invoked by a debugger.

On the one hand, that's good, because the symbol reference isn't actually emitted here, but it does cause a compiler warning when malloc_size isn't present, so let's clean up and remove this function anyway.

I've therefore reworded that commit accordingly. If this analysis is somehow off base, please let me know and I'll update the commit description further.

@compnerd
Copy link
Member

It should be preserved at -O0, its meant for debugging :) I believe that the commit message is reasonable now. Thanks for fixing that.

@compnerd
Copy link
Member

@swift-ci please test

@compnerd compnerd merged commit a5aa8a0 into swiftlang:master Jan 27, 2020
3405691582 added a commit to 3405691582/swift-corelibs-foundation that referenced this pull request May 11, 2020
This appears to have gotten lost in the recent Catalina merge.
millenomi added a commit that referenced this pull request May 30, 2020
[CF] Reapply #2626 (do not call malloc_size).
@3405691582 3405691582 mentioned this pull request Aug 26, 2021
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.

3 participants