Skip to content

[Runtime] Look up Error bridging symbols indirectly through special symbols that won't be stripped in static builds. #16677

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
May 18, 2018
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
1 change: 1 addition & 0 deletions stdlib/public/SDK/Foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_swift_library(swiftFoundation ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SD
NSCoder.swift
NSDate.swift
NSDictionary.swift
NSError.c
NSError.swift
NSExpression.swift
NSFastEnumeration.swift
Expand Down
34 changes: 34 additions & 0 deletions stdlib/public/SDK/Foundation/NSError.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "swift/Runtime/Config.h"

#if SWIFT_OBJC_INTEROP
#include "swift/Demangling/ManglingMacros.h"

// Declare dynamic lookup points for ErrorObject.mm. It uses dlsym to
// find these symbols to locate NS/CFError Error conformance tables and
// related items. The .desc asm directive ensures that they are
// preserved even when statically linked into an executable and
// stripped, so that the dlsym lookup still works.
#define ERROROBJECT_DYNAMIC_LOOKUP_POINT(symbol) \
extern void *MANGLE_SYM(symbol); \
void **MANGLING_CONCAT2(ErrorObjectLookup_, MANGLE_SYM(symbol)) = &MANGLE_SYM(symbol); \
asm(".desc _ErrorObjectLookup_" MANGLE_AS_STRING(MANGLE_SYM(symbol)) ", 0x10");

ERROROBJECT_DYNAMIC_LOOKUP_POINT(So10CFErrorRefas5Error10FoundationWa)
ERROROBJECT_DYNAMIC_LOOKUP_POINT(So8NSObjectCs8Hashable10ObjectiveCWa)
ERROROBJECT_DYNAMIC_LOOKUP_POINT(10Foundation24_getErrorDefaultUserInfoyyXlSgxs0C0RzlF)
ERROROBJECT_DYNAMIC_LOOKUP_POINT(10Foundation21_bridgeNSErrorToError_3outSbSo0C0C_SpyxGtAA021_ObjectiveCBridgeableE0RzlF)
ERROROBJECT_DYNAMIC_LOOKUP_POINT(10Foundation26_ObjectiveCBridgeableErrorMp)

#endif
30 changes: 20 additions & 10 deletions stdlib/public/runtime/ErrorObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ static Class getSwiftNativeNSErrorClass() {
object_dispose((id)error);
}

/// Look up a symbol that points to something else. Treats the symbol as
/// a void** and dereferences it if it's non-NULL. Returns NULL if the
/// symbol can't be found or if the value is NULL.
static void *dynamicLookupSymbol(const char *name) {
void **ptr = reinterpret_cast<void **>(dlsym(RTLD_DEFAULT, name));
if (!ptr) return nullptr;
return *ptr;
}

/// Look up an indirect pointer to a mangled Swift symbol, automatically
/// prepending the ErrorObjectLookup_ prefix. Used to find the various
/// Foundation overlay symbols for Error bridging.
#define DYNAMIC_LOOKUP_SYMBOL(symbol) \
dynamicLookupSymbol("ErrorObjectLookup_" MANGLE_AS_STRING(MANGLE_SYM(symbol)))

static const WitnessTable *getNSErrorConformanceToError() {
// CFError and NSError are toll-free-bridged, so we can use either type's
// witness table interchangeably. CFError's is potentially slightly more
Expand All @@ -228,8 +243,7 @@ static Class getSwiftNativeNSErrorClass() {
// to assume that that's been linked in if a user is using NSError in their
// Swift source.

auto TheWitnessTable = SWIFT_LAZY_CONSTANT(dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(So10CFErrorRefas5Error10FoundationWa))));
auto TheWitnessTable = SWIFT_LAZY_CONSTANT(DYNAMIC_LOOKUP_SYMBOL(So10CFErrorRefas5Error10FoundationWa));
assert(TheWitnessTable &&
"Foundation overlay not loaded, or 'CFError : Error' conformance "
"not available");
Expand All @@ -238,8 +252,7 @@ static Class getSwiftNativeNSErrorClass() {
}

static const HashableWitnessTable *getNSErrorConformanceToHashable() {
auto TheWitnessTable = SWIFT_LAZY_CONSTANT(dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(So8NSObjectCs8Hashable10ObjectiveCWa))));
auto TheWitnessTable = SWIFT_LAZY_CONSTANT(DYNAMIC_LOOKUP_SYMBOL(So8NSObjectCs8Hashable10ObjectiveCWa));
assert(TheWitnessTable &&
"ObjectiveC overlay not loaded, or 'NSObject : Hashable' conformance "
"not available");
Expand Down Expand Up @@ -379,8 +392,7 @@ typedef SWIFT_CC(swift)
// public func Foundation._getErrorDefaultUserInfo<T: Error>(_ error: T)
// -> AnyObject?
auto foundationGetDefaultUserInfo = SWIFT_LAZY_CONSTANT(
reinterpret_cast<GetDefaultFn*> (dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(10Foundation24_getErrorDefaultUserInfoyyXlSgxs0C0RzlF)))));
reinterpret_cast<GetDefaultFn*> (DYNAMIC_LOOKUP_SYMBOL(10Foundation24_getErrorDefaultUserInfoyyXlSgxs0C0RzlF)));
if (!foundationGetDefaultUserInfo) {
SWIFT_CC_PLUSONE_GUARD(T->vw_destroy(error));
return nullptr;
Expand Down Expand Up @@ -520,12 +532,10 @@ typedef SWIFT_CC(swift)
bool BridgeFn(NSError *, OpaqueValue*, const Metadata *,
const WitnessTable *);
auto bridgeNSErrorToError = SWIFT_LAZY_CONSTANT(
reinterpret_cast<BridgeFn*>(dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(10Foundation21_bridgeNSErrorToError_3outSbSo0C0C_SpyxGtAA021_ObjectiveCBridgeableE0RzlF)))));
reinterpret_cast<BridgeFn*>(DYNAMIC_LOOKUP_SYMBOL(10Foundation21_bridgeNSErrorToError_3outSbSo0C0C_SpyxGtAA021_ObjectiveCBridgeableE0RzlF)));
// protocol _ObjectiveCBridgeableError
auto TheObjectiveCBridgeableError = SWIFT_LAZY_CONSTANT(
reinterpret_cast<const ProtocolDescriptor *>(dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(10Foundation26_ObjectiveCBridgeableErrorMp)))));
reinterpret_cast<const ProtocolDescriptor *>(DYNAMIC_LOOKUP_SYMBOL(10Foundation26_ObjectiveCBridgeableErrorMp)));

// If the Foundation overlay isn't loaded, then arbitrary NSErrors can't be
// bridged.
Expand Down
14 changes: 14 additions & 0 deletions test/stdlib/ErrorBridgedStatic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Bar: Foo {
override func foo(_ x: Int32) throws {
try super.foo(5)
}

override func foothrows(_ x: Int32) throws {
try super.foothrows(5)
}
}

var ErrorBridgingStaticTests = TestSuite("ErrorBridging with static libs")
Expand All @@ -24,4 +28,14 @@ ErrorBridgingStaticTests.test("round-trip Swift override of ObjC method") {
} catch { }
}

ErrorBridgingStaticTests.test("round-trip Swift override of throwing ObjC method") {
do {
try (Bar() as Foo).foothrows(5)
} catch {
print(error)
expectEqual(error._domain, "abcd")
expectEqual(error._code, 1234)
}
}

runAllTests()
1 change: 1 addition & 0 deletions test/stdlib/Inputs/ErrorBridgedStaticImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
@interface Foo: NSObject

- (BOOL)foo:(int)x error:(NSError**)error;
- (BOOL)foothrows:(int)x error:(NSError**)error;

@end
5 changes: 5 additions & 0 deletions test/stdlib/Inputs/ErrorBridgedStaticImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ - (BOOL)foo:(int)x error:(NSError**)error {
return NO;
}

- (BOOL)foothrows:(int)x error:(NSError**)error {
*error = [NSError errorWithDomain: @"abcd" code: 1234 userInfo: nil];
return NO;
}

@end