Skip to content

[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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions stdlib/public/SwiftShims/LibcShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -19,6 +19,7 @@
#ifndef SWIFT_STDLIB_SHIMS_LIBCSHIMS_H
#define SWIFT_STDLIB_SHIMS_LIBCSHIMS_H

#include "SwiftStdbool.h"
#include "SwiftStdint.h"
#include "SwiftStddef.h"
#include "Visibility.h"
Expand Down Expand Up @@ -104,12 +105,14 @@ static inline int _swift_stdlib_memcmp(const void *s1, const void *s2,

// Non-standard extensions
#if defined(__APPLE__)
#define HAS_MALLOC_SIZE 1
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
extern __swift_size_t malloc_size(const void *);
return malloc_size(ptr);
}
#elif defined(__linux__) || defined(__CYGWIN__) || defined(__ANDROID__) \
|| defined(__HAIKU__) || defined(__FreeBSD__) || defined(__wasi__)
#define HAS_MALLOC_SIZE 1
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
#if defined(__ANDROID__)
#if !defined(__ANDROID_API__) || __ANDROID_API__ >= 17
Expand All @@ -121,14 +124,23 @@ static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
return malloc_usable_size(CONST_CAST(void *, ptr));
}
#elif defined(_WIN32)
#define HAS_MALLOC_SIZE 1
static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
extern __swift_size_t _msize(void *ptr);
return _msize(CONST_CAST(void *, ptr));
}
#else
#error No malloc_size analog known for this platform/libc.
#define HAS_MALLOC_SIZE 0

static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
return 0;
}
#endif

static inline __swift_bool _swift_stdlib_has_malloc_size() {
return HAS_MALLOC_SIZE != 0;
}

// Math library functions
static inline SWIFT_ALWAYS_INLINE
float _stdlib_remainderf(float _self, float _other) {
Expand Down
16 changes: 10 additions & 6 deletions stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -268,11 +268,15 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
realMinimumCapacity._builtinWordValue, Element.self)

let storageAddr = UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_storage))
let endAddr = storageAddr + _swift_stdlib_malloc_size(storageAddr)
let realCapacity = endAddr.assumingMemoryBound(to: Element.self) - firstElementAddress

_initStorageHeader(
count: uninitializedCount, capacity: realCapacity)
if let allocSize = _mallocSize(ofAllocation: storageAddr) {
let endAddr = storageAddr + allocSize
let realCapacity = endAddr.assumingMemoryBound(to: Element.self) - firstElementAddress
_initStorageHeader(
count: uninitializedCount, capacity: realCapacity)
} else {
_initStorageHeader(
count: uninitializedCount, capacity: realMinimumCapacity)
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/core/Shims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -14,9 +14,9 @@
///
//===----------------------------------------------------------------------===//

#if _runtime(_ObjC)
import SwiftShims

#if _runtime(_ObjC)
@inlinable
internal func _makeSwiftNSFastEnumerationState()
-> _SwiftNSFastEnumerationState {
Expand All @@ -36,3 +36,8 @@ internal var _fastEnumerationStorageMutationsTarget: CUnsignedLong = 0
internal let _fastEnumerationStorageMutationsPtr =
UnsafeMutablePointer<CUnsignedLong>(Builtin.addressof(&_fastEnumerationStorageMutationsTarget))
#endif

@usableFromInline @_alwaysEmitIntoClient
internal func _mallocSize(ofAllocation ptr: UnsafeRawPointer) -> Int? {
return _swift_stdlib_has_malloc_size() ? _swift_stdlib_malloc_size(ptr) : nil
}
18 changes: 11 additions & 7 deletions stdlib/public/core/StringStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -206,13 +206,17 @@ fileprivate func _allocate<T: AnyObject>(
let totalTailBytes = total - numHeaderBytes

let object = tailAllocator(totalTailBytes)
let mallocSize = _swift_stdlib_malloc_size(
UnsafeRawPointer(Builtin.bridgeToRawPointer(object)))
_internalInvariant(mallocSize % MemoryLayout<Int>.stride == 0)
if let allocSize = _mallocSize(ofAllocation:
UnsafeRawPointer(Builtin.bridgeToRawPointer(object))) {
_internalInvariant(allocSize % MemoryLayout<Int>.stride == 0)

let realNumTailBytes = mallocSize - numHeaderBytes
_internalInvariant(realNumTailBytes >= numTailBytes)
return (object, realNumTailBytes)
let realNumTailBytes = allocSize - numHeaderBytes
_internalInvariant(realNumTailBytes >= numTailBytes)

return (object, realNumTailBytes)
} else {
return (object, totalTailBytes)
}
}

fileprivate func _allocateStringStorage(
Expand Down