Skip to content

[interop][SwiftToCxx] dispatch swift class methods using signed isa a… #63599

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 1 commit into from
Feb 12, 2023
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
21 changes: 17 additions & 4 deletions include/swift/IRGen/IRABIDetailsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,19 @@ class IRABIDetailsProvider {
/// The call should be made via the provided thunk function.
Thunk
};
struct PointerAuthDiscriminator {
/// The value of the other discriminator
uint64_t value;
};

static MethodDispatchInfo direct() {
return MethodDispatchInfo(Kind::Direct, 0);
}

static MethodDispatchInfo indirectVTableStaticOffset(size_t bitOffset) {
return MethodDispatchInfo(Kind::IndirectVTableStaticOffset, bitOffset);
static MethodDispatchInfo indirectVTableStaticOffset(
size_t bitOffset, Optional<PointerAuthDiscriminator> discriminator) {
return MethodDispatchInfo(Kind::IndirectVTableStaticOffset, bitOffset, "",
discriminator);
}

static MethodDispatchInfo thunk(std::string thunkName) {
Expand All @@ -276,18 +282,25 @@ class IRABIDetailsProvider {
assert(kind == Kind::IndirectVTableStaticOffset);
return bitOffset;
}
Optional<PointerAuthDiscriminator> getPointerAuthDiscriminator() const {
assert(kind == Kind::IndirectVTableStaticOffset);
return discriminator;
}
StringRef getThunkSymbolName() const {
assert(kind == Kind::Thunk);
return thunkName;
}

private:
MethodDispatchInfo(Kind kind, size_t bitOffset, std::string thunkName = "")
: kind(kind), bitOffset(bitOffset), thunkName(thunkName) {}
MethodDispatchInfo(Kind kind, size_t bitOffset, std::string thunkName = "",
Optional<PointerAuthDiscriminator> discriminator = None)
: kind(kind), bitOffset(bitOffset), thunkName(thunkName),
discriminator(discriminator) {}

Kind kind;
size_t bitOffset;
std::string thunkName;
Optional<PointerAuthDiscriminator> discriminator;
};

Optional<MethodDispatchInfo>
Expand Down
23 changes: 20 additions & 3 deletions lib/IRGen/IRABIDetailsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Callee.h"
#include "FixedTypeInfo.h"
#include "GenEnum.h"
#include "GenPointerAuth.h"
#include "GenType.h"
#include "GenericRequirement.h"
#include "IRGen.h"
Expand Down Expand Up @@ -206,6 +207,21 @@ class IRABIDetailsProviderImpl {

using MethodDispatchInfo = IRABIDetailsProvider::MethodDispatchInfo;

Optional<MethodDispatchInfo::PointerAuthDiscriminator>
getMethodPointerAuthInfo(const AbstractFunctionDecl *funcDecl,
SILDeclRef method) {
// FIXME: Async support.
if (funcDecl->hasAsync())
return None;
const auto &schema = IGM.getOptions().PointerAuth.SwiftClassMethods;
if (!schema)
return None;
auto discriminator =
PointerAuthInfo::getOtherDiscriminator(IGM, schema, method);
return MethodDispatchInfo::PointerAuthDiscriminator{
discriminator->getZExtValue()};
}

Optional<MethodDispatchInfo>
getMethodDispatchInfo(const AbstractFunctionDecl *funcDecl) {
if (funcDecl->isSemanticallyFinal())
Expand All @@ -227,8 +243,8 @@ class IRABIDetailsProviderImpl {
if (!isa<ClassMetadataLayout>(layout))
return {};
auto &classLayout = cast<ClassMetadataLayout>(layout);
auto *mi = classLayout.getStoredMethodInfoIfPresent(
SILDeclRef(const_cast<AbstractFunctionDecl *>(funcDecl)));
auto silDecl = SILDeclRef(const_cast<AbstractFunctionDecl *>(funcDecl));
auto *mi = classLayout.getStoredMethodInfoIfPresent(silDecl);
if (!mi)
return {};
switch (mi->TheKind) {
Expand All @@ -237,7 +253,8 @@ class IRABIDetailsProviderImpl {
case ClassMetadataLayout::MethodInfo::Kind::Offset:
if (mi->TheOffset.isStatic()) {
return MethodDispatchInfo::indirectVTableStaticOffset(
/*bitOffset=*/mi->TheOffset.getStaticOffset().getValue());
/*bitOffset=*/mi->TheOffset.getStaticOffset().getValue(),
getMethodPointerAuthInfo(funcDecl, silDecl));
}
return {};
}
Expand Down
27 changes: 21 additions & 6 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "PrintClangClassType.h"
#include "PrintClangValueType.h"
#include "SwiftToClangInteropContext.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/Module.h"
Expand Down Expand Up @@ -1107,13 +1108,27 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
os << "void ***selfPtr_ = reinterpret_cast<void ***>( "
"::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));"
"\n";

os << "#ifdef __arm64e__\n";
os << "void **vtable_ = ptrauth_auth_data(*selfPtr_, "
"ptrauth_key_process_independent_data, "
"ptrauth_blend_discriminator(selfPtr_,"
<< SpecialPointerAuthDiscriminators::ObjCISA << "));\n";
os << "#else\n";
os << "void **vtable_ = *selfPtr_;\n";
os << "using FType = decltype("
<< cxx_synthesis::getCxxImplNamespaceName() << "::" << swiftSymbolName
<< ");\n";
os << "FType *fptr_ = reinterpret_cast<FType *>(*(vtable_ + "
<< (dispatchInfo->getStaticBitOffset() / 8) << "));\n";
indirectFunctionVar = StringRef("fptr_");
os << "#endif\n";
os << "struct FTypeAddress {\n";
os << "decltype(" << cxx_synthesis::getCxxImplNamespaceName()
<< "::" << swiftSymbolName << ") *";
if (auto ptrAuthDisc = dispatchInfo->getPointerAuthDiscriminator())
os << " __ptrauth_swift_class_method_pointer(" << ptrAuthDisc->value
<< ')';
os << " func;\n";
os << "};\n";
os << "FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ "
"+ "
<< (dispatchInfo->getStaticBitOffset() / 8) << ");\n";
indirectFunctionVar = StringRef("fptrptr_->func");
break;
case DispatchKindTy::Thunk:
swiftSymbolName = dispatchInfo->getThunkSymbolName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %S/swift-class-virtual-method-dispatch.swift -target arm64e-apple-ios12.0 -typecheck -module-name Class -clang-header-expose-decls=all-public -emit-clang-header-path %t/class.h
// RUN: %FileCheck %s < %t/class.h

// RUN: %check-interop-cxx-header-in-clang(%t/class.h)

// REQUIRES: CPU=arm64e
// REQUIRES: OS=ios

// note: uses swift-class-virtual-method-dispatch.swift

// CHECK: void BaseClass::virtualMethod() {
// CHECK-NEXT: void ***selfPtr_ = reinterpret_cast<void ***>( ::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
// CHECK-NEXT: #ifdef __arm64e__
// CHECK-NEXT: void **vtable_ = ptrauth_auth_data(*selfPtr_, ptrauth_key_process_independent_data, ptrauth_blend_discriminator(selfPtr_,27361));
// CHECK-NEXT: #else
// CHECK-NEXT: void **vtable_ = *selfPtr_;
// CHECK-NEXT: #endif
// CHECK-NEXT: struct FTypeAddress {
// CHECK-NEXT: decltype(_impl::$s5Class04BaseA0C13virtualMethodyyF) * __ptrauth_swift_class_method_pointer([[#AUTH:]]) func;
// CHECK-NEXT: };
// CHECK-NEXT: FTypeAddress *fptrptr_ = reinterpret_cast<FTypeAddress *>(vtable_ + [[#VM1:]]);
// CHECK-NEXT: return (* fptrptr_->func)(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(*this));
// CHECK-NEXT: }
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
// RUN: %target-codesign %t/swift-class-execution
// RUN: %target-run %t/swift-class-execution | %FileCheck %s

// FIXME: pointer signing support.
// UNSUPPORTED: CPU=arm64e

// REQUIRES: executable_test

#include "class.h"
Expand Down
Loading