From b59910ef5bdc3c95415ada2beddec9f1eb1e687a Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Fri, 21 Aug 2020 18:27:10 -0400 Subject: [PATCH 1/2] Add OpenBSD to PlatformKinds.def. In #31686 changes were introduced to ensure that capacity was stored in the ManagedBuffer allocation, and @lorentey sugested that as a stopgap measure for addressing the lack of platform malloc introspection on OpenBSD, we use Swift availability attributes instead on the relevant parts of ManagedBuffer and friends. Since platform availability symbols must be specifically set up to be used, this commit does so in advance of the above change. --- include/swift/AST/PlatformKinds.def | 1 + lib/AST/PlatformKind.cpp | 2 ++ lib/ClangImporter/ClangImporter.cpp | 11 +++++++++++ lib/PrintAsObjC/DeclAndTypePrinter.cpp | 3 +++ lib/SymbolGraphGen/AvailabilityMixin.cpp | 2 ++ lib/TBDGen/TBDGen.cpp | 2 ++ test/IDE/complete_decl_attribute.swift | 1 + tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp | 3 +++ 8 files changed, 25 insertions(+) diff --git a/include/swift/AST/PlatformKinds.def b/include/swift/AST/PlatformKinds.def index 29ae59270d6ab..55d2f2e874cdb 100644 --- a/include/swift/AST/PlatformKinds.def +++ b/include/swift/AST/PlatformKinds.def @@ -32,5 +32,6 @@ AVAILABILITY_PLATFORM(watchOSApplicationExtension, "application extensions for w AVAILABILITY_PLATFORM(macOSApplicationExtension, "application extensions for macOS") AVAILABILITY_PLATFORM(macCatalyst, "Mac Catalyst") AVAILABILITY_PLATFORM(macCatalystApplicationExtension, "application extensions for Mac Catalyst") +AVAILABILITY_PLATFORM(OpenBSD, "OpenBSD") #undef AVAILABILITY_PLATFORM diff --git a/lib/AST/PlatformKind.cpp b/lib/AST/PlatformKind.cpp index ff6a00319b1aa..f0f72ab2675c2 100644 --- a/lib/AST/PlatformKind.cpp +++ b/lib/AST/PlatformKind.cpp @@ -87,6 +87,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform, case PlatformKind::watchOS: case PlatformKind::watchOSApplicationExtension: return Target.isWatchOS(); + case PlatformKind::OpenBSD: + return Target.isOSOpenBSD(); case PlatformKind::none: llvm_unreachable("handled above"); } diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 697e900d69182..0e4978a71d522 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -1930,6 +1930,10 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts) "APIs deprecated as of macOS 10.9 and earlier are unavailable in Swift"; break; + case PlatformKind::OpenBSD: + deprecatedAsUnavailableMessage = ""; + break; + case PlatformKind::none: break; } @@ -1962,6 +1966,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const { case PlatformKind::watchOSApplicationExtension: return name == "watchos" || name == "watchos_app_extension"; + case PlatformKind::OpenBSD: + return name == "openbsd"; + case PlatformKind::none: return false; } @@ -2001,6 +2008,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable( case PlatformKind::watchOSApplicationExtension: // No deprecation filter on watchOS return false; + + case PlatformKind::OpenBSD: + // No deprecation filter on OpenBSD + return false; } llvm_unreachable("Unexpected platform"); diff --git a/lib/PrintAsObjC/DeclAndTypePrinter.cpp b/lib/PrintAsObjC/DeclAndTypePrinter.cpp index 3a84f5cc82bcc..19cfe27eebf37 100644 --- a/lib/PrintAsObjC/DeclAndTypePrinter.cpp +++ b/lib/PrintAsObjC/DeclAndTypePrinter.cpp @@ -852,6 +852,9 @@ class DeclAndTypePrinter::Implementation case PlatformKind::watchOSApplicationExtension: plat = "watchos_app_extension"; break; + case PlatformKind::OpenBSD: + plat = "openbsd"; + break; case PlatformKind::none: llvm_unreachable("handled above"); } diff --git a/lib/SymbolGraphGen/AvailabilityMixin.cpp b/lib/SymbolGraphGen/AvailabilityMixin.cpp index aa446f412a83c..4dc66f0b9ed62 100644 --- a/lib/SymbolGraphGen/AvailabilityMixin.cpp +++ b/lib/SymbolGraphGen/AvailabilityMixin.cpp @@ -56,6 +56,8 @@ StringRef getDomain(const AvailableAttr &AvAttr) { return { "tvOSAppExtension" }; case swift::PlatformKind::watchOSApplicationExtension: return { "watchOSAppExtension" }; + case swift::PlatformKind::OpenBSD: + return { "OpenBSD" }; case swift::PlatformKind::none: return { "*" }; } diff --git a/lib/TBDGen/TBDGen.cpp b/lib/TBDGen/TBDGen.cpp index 623e3d02ffb3e..e3523a683456a 100644 --- a/lib/TBDGen/TBDGen.cpp +++ b/lib/TBDGen/TBDGen.cpp @@ -249,6 +249,8 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver) { switch(Ver.Platform) { case swift::PlatformKind::none: llvm_unreachable("cannot find platform kind"); + case swift::PlatformKind::OpenBSD: + llvm_unreachable("not used for this platform"); case swift::PlatformKind::iOS: case swift::PlatformKind::iOSApplicationExtension: return Ver.IsSimulator ? LinkerPlatformId::iOS_sim: diff --git a/test/IDE/complete_decl_attribute.swift b/test/IDE/complete_decl_attribute.swift index 1861cd5944abf..23606dacfa21e 100644 --- a/test/IDE/complete_decl_attribute.swift +++ b/test/IDE/complete_decl_attribute.swift @@ -39,6 +39,7 @@ struct MyStruct {} // AVAILABILITY1-NEXT: Keyword/None: macOSApplicationExtension[#Platform#]; name=macOSApplicationExtension{{$}} // AVAILABILITY1-NEXT: Keyword/None: macCatalyst[#Platform#]; name=macCatalyst // AVAILABILITY1-NEXT: Keyword/None: macCatalystApplicationExtension[#Platform#]; name=macCatalystApplicationExtension +// AVAILABILITY1-NEXT: Keyword/None: OpenBSD[#Platform#]; name=OpenBSD{{$}} // AVAILABILITY1-NEXT: End completions @available(*, #^AVAILABILITY2^#) diff --git a/tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp b/tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp index f59c9d0564bd1..0cb600d01d4de 100644 --- a/tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp +++ b/tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp @@ -674,6 +674,7 @@ static void reportAttributes(ASTContext &Ctx, static UIdent PlatformOSXAppExt("source.availability.platform.osx_app_extension"); static UIdent PlatformtvOSAppExt("source.availability.platform.tvos_app_extension"); static UIdent PlatformWatchOSAppExt("source.availability.platform.watchos_app_extension"); + static UIdent PlatformOpenBSD("source.availability.platform.openbsd"); std::vector Scratch; for (auto Attr : getDeclAttributes(D, Scratch)) { @@ -702,6 +703,8 @@ static void reportAttributes(ASTContext &Ctx, PlatformUID = PlatformtvOSAppExt; break; case PlatformKind::watchOSApplicationExtension: PlatformUID = PlatformWatchOSAppExt; break; + case PlatformKind::OpenBSD: + PlatformUID = PlatformOpenBSD; break; } AvailableAttrInfo Info; From cd7570fdee9c68dcc93b0107041c6c9bc47f101c Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Fri, 21 Aug 2020 18:33:19 -0400 Subject: [PATCH 2/2] ManagedBuffer capacity is unavailable on OpenBSD. On OpenBSD, malloc introspection (e.g., malloc_usable_size or malloc_size) is not provided by the platform allocator. Since allocator introspection is currently a load-bearing piece of functionality for ManagedBuffer and ManagedBufferPointer, pending any API changes, as a stopgap measure, this commit marks methods in ManagedBuffer and ManagedBufferPointer calling _swift_stdlib_malloc_size and methods dependent thereon unavailable on OpenBSD. This may induce some compatibility issues for some files, but at least this change ensures that we can get stdlib to build on this platform until the evolution process addresses this problem more thoroughly. --- stdlib/public/core/BridgingBuffer.swift | 1 + stdlib/public/core/ManagedBuffer.swift | 4 ++++ test/Interpreter/generic_ref_counts.swift | 1 + test/stdlib/ManagedBuffer.swift | 1 + 4 files changed, 7 insertions(+) diff --git a/stdlib/public/core/BridgingBuffer.swift b/stdlib/public/core/BridgingBuffer.swift index c3d0613fdeb35..5e6e28a5a6e84 100644 --- a/stdlib/public/core/BridgingBuffer.swift +++ b/stdlib/public/core/BridgingBuffer.swift @@ -25,6 +25,7 @@ internal final class __BridgingBufferStorage internal typealias _BridgingBuffer = ManagedBufferPointer<_BridgingBufferHeader, AnyObject> +@available(OpenBSD, unavailable, message: "malloc_size is unavailable.") extension ManagedBufferPointer where Header == _BridgingBufferHeader, Element == AnyObject { internal init(_ count: Int) { diff --git a/stdlib/public/core/ManagedBuffer.swift b/stdlib/public/core/ManagedBuffer.swift index 62489940670a1..bb6a63204854e 100644 --- a/stdlib/public/core/ManagedBuffer.swift +++ b/stdlib/public/core/ManagedBuffer.swift @@ -83,6 +83,7 @@ extension ManagedBuffer { /// idea to store this information in the "header" area when /// an instance is created. @inlinable + @available(OpenBSD, unavailable, message: "malloc_size is unavailable.") public final var capacity: Int { let storageAddr = UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(self)) let endAddr = storageAddr + _swift_stdlib_malloc_size(storageAddr) @@ -197,6 +198,7 @@ public struct ManagedBufferPointer { /// properties. The `deinit` of `bufferClass` must destroy its /// stored `Header` and any constructed `Element`s. @inlinable + @available(OpenBSD, unavailable, message: "malloc_size is unavailable.") public init( bufferClass: AnyClass, minimumCapacity: Int, @@ -329,6 +331,7 @@ extension ManagedBufferPointer { /// idea to store this information in the "header" area when /// an instance is created. @inlinable + @available(OpenBSD, unavailable, message: "malloc_size is unavailable.") public var capacity: Int { return ( _capacityInBytes &- ManagedBufferPointer._elementOffset @@ -431,6 +434,7 @@ extension ManagedBufferPointer { /// The actual number of bytes allocated for this object. @inlinable + @available(OpenBSD, unavailable, message: "malloc_size is unavailable.") internal var _capacityInBytes: Int { return _swift_stdlib_malloc_size(_address) } diff --git a/test/Interpreter/generic_ref_counts.swift b/test/Interpreter/generic_ref_counts.swift index 44e6307fac6be..4f9a469bfd6ca 100644 --- a/test/Interpreter/generic_ref_counts.swift +++ b/test/Interpreter/generic_ref_counts.swift @@ -1,5 +1,6 @@ // RUN: %target-run-simple-swift | %FileCheck %s // REQUIRES: executable_test +// XFAIL: OS=openbsd import Swift diff --git a/test/stdlib/ManagedBuffer.swift b/test/stdlib/ManagedBuffer.swift index 24d288c510adb..0d69197c59f65 100644 --- a/test/stdlib/ManagedBuffer.swift +++ b/test/stdlib/ManagedBuffer.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// // RUN: %target-run-simple-swift // REQUIRES: executable_test +// XFAIL: OS=openbsd import StdlibUnittest