-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Avoid malloc_size on OpenBSD. #30560
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
[stdlib] Avoid malloc_size on OpenBSD. #30560
Conversation
cc @milseman who was working in this area recently |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a comment here explaining what you wrote in the PR's summary?
Array also calls |
3886ca2
to
538e2d3
Compare
Done. I tweaked the language in the code comment to be a little more generic; who knows if there might be a porting effort by someone to another platform without malloc introspection.
There are three uses of malloc_size in Swift that are problematic:
I'm working on an OpenBSD port recently. It doesn't have malloc introspection, and introspection like this is something we can't kludge around, because knowing the allocation extents depends on introspecting the system allocator, so the only way around this is to track the size of the allocation in parallel with every allocation. |
It's going to be easy to forget about platforms like OpenBSD as we work on the standard library and refactor code, which might add new
|
I'm happy to try and make that refactor here, that sounds simple and straightforward enough in itself, but I still don't know how to apply it for ManagedBufferPointer. That doesn't mean we can't at least apply that scheme where it works for the moment. |
@Catfish-Man or @milseman would be more familiar with the specific code at hand, but in general, you ought to be able to replace existing direct calls to |
Sure, but the problem with ManagedBufferPointer is that the malloc_size call is divorced from the allocation (see _capacityInBytes), and ManagedBufferPointer asserts on "illegal stored properties". Happy to hear further advice on this, though. |
09262ff
to
f594ddb
Compare
Adopted @jckarter's suggestion to introduce a new shim. Still would like to hear if @Catfish-Man or @milseman have advice. Points of note:
|
Thanks for this, I think that the shim'ed approach is definitely nicer. |
@swift-ci please test |
Build failed |
Build failed |
@swift-ci please test macOS platform |
Build failed |
Looks like the OS X build failure is because swiftpm is being built with a platform Swift SDK for SDKROOT but with the Swift stdlib from HEAD + this PR, is that right? Is there something this PR is missing? |
Lost the build failure @swift-ci test macOS platform |
Build failed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions:
-
use
__swift_bool
if possible? -
and update the copyright year in all files.
-// Copyright (c) 2014 - 20** Apple Inc. and the Swift project authors +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
f594ddb
to
4b70859
Compare
stdlib/public/SwiftShims/LibcShims.h
Outdated
#define HAS_MALLOC_SIZE 1 | ||
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) { | ||
#if defined(__ANDROID__) | ||
#define HAS_MALLOC_SIZE 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this #define HAS_MALLOC_SIZE
twice for Android?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, should HAS_MALLOC_SIZE
be renamed to _SWIFT_STDLIB_HAS_MALLOC_SIZE
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Removed the Android #define.
I'm not sure a rename is necessary; giving it a full prefix suggests this is something with stronger scope than it actually has. The underscore prefix seems rather uncommon for stdlib as well, so I'm inclined to leave it as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: replace each #define HAS_MALLOC_SIZE
with a function?
-#define HAS_MALLOC_SIZE 1
+static inline __swift_bool _swift_stdlib_has_malloc_size() { return true; }
-#define HAS_MALLOC_SIZE 0
+static inline __swift_bool _swift_stdlib_has_malloc_size() { return false; }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a lot more to copy and paste for the next new port, though.This suggests it would be far better to push the preprocessor conditionals into one definition of each function at the top level, but that would probably be better suited to do in a separate PR.
4b70859
to
03ffde0
Compare
@swift-ci Please test |
Build failed |
Build failed |
@swift-ci please test |
Build failed |
@swift-ci please test macOS platform |
Build failed |
The macOS failure was:
Does this mean that the system dylib is expected to contain the new |
Yes, the tests currently run against the system version of the core library. This is due to a system issue that’s been there for a while. Fixing that is blocked by the other build system work that exposes the issue in an addressable manner. CC: @drexin |
Running tests against the OS runtime seems like a feature, not a bug, because we want to know if we accidentally add symbols to the standard library, since they'll be burdened by deployment target limitations because they aren't available in older OSes. In this case, we need to mark that new function with the |
stdlib/public/core/Shims.swift
Outdated
@@ -36,3 +36,8 @@ internal var _fastEnumerationStorageMutationsTarget: CUnsignedLong = 0 | |||
internal let _fastEnumerationStorageMutationsPtr = | |||
UnsafeMutablePointer<CUnsignedLong>(Builtin.addressof(&_fastEnumerationStorageMutationsTarget)) | |||
#endif | |||
|
|||
@usableFromInline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to being @usableFromInline
, this should also be @_alwaysEmitIntoClient
, so that user code doesn't try to link against a nonexistent symbol in older Swift runtime dylibs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Thanks, that's the piece of knowledge I was missing here.
malloc introspection is a platform feature that is unavailable on OpenBSD. There is no workaround for the feature, so we have to assume that allocations succeed in allocating exactly the amount of memory requested, and nothing more. Here a new mallocSize shim is introduced so the feature check for malloc introspection is pushed to the shims, rather than using os checks directly from Swift. Not every use of malloc_size has been converted yet; ManagedBuffer.swift still remains. However, this module requires special care to fix, which will be done separately.
03ffde0
to
7830028
Compare
Can someone please kick-off swift-ci on Mac OS for me? |
@swift-ci Please test |
Build failed |
Build failed |
@CodaFi This look good before I merge? |
Thanks @3405691582! |
malloc introspection is a platform feature that is unavailable on
OpenBSD. There is no workaround for the feature, so simply avoid calling
it in this case. It is assumed on OpenBSD that the tail allocator will
allocate the amount of memory requested of it, and nothing more.