Skip to content

[WIP] Reenable PruneVTables and handle non-overridden method descriptors #36897

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
20 changes: 17 additions & 3 deletions include/swift/ABI/MetadataValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,18 @@ class MethodDescriptorFlags {
ReadCoroutine,
};

private:
enum : int_type {
KindMask = 0x0F, // 16 kinds should be enough for anybody
IsInstanceMask = 0x10,
IsDynamicMask = 0x20,
IsAsyncMask = 0x40,
IsAsyncMask = 0x40, // added in Swift 5.5 runtime
IsNonoverriddenMask = 0x80, // added in Swift 5.5 runtime
ExtraDiscriminatorShift = 16,
ExtraDiscriminatorMask = 0xFFFF0000,
};

int_type Value;

public:
MethodDescriptorFlags(Kind kind) : Value(unsigned(kind)) {}

MethodDescriptorFlags withIsInstance(bool isInstance) const {
Expand Down Expand Up @@ -355,6 +354,15 @@ class MethodDescriptorFlags {
return copy;
}

MethodDescriptorFlags withIsNonoverridden(bool isNonoverridden) const {
auto copy = *this;
if (isNonoverridden)
copy.Value |= IsNonoverriddenMask;
else
copy.Value &= ~IsNonoverriddenMask;
return copy;
}

MethodDescriptorFlags withExtraDiscriminator(uint16_t value) const {
auto copy = *this;
copy.Value = (copy.Value & ~ExtraDiscriminatorMask)
Expand All @@ -371,6 +379,12 @@ class MethodDescriptorFlags {
///
/// Note that 'init' is not considered an instance member.
bool isInstance() const { return Value & IsInstanceMask; }

/// Is the method known to have no overrides?
/// This indicates that the implementation reference from this method
/// descriptor can be used directly, without looking into the vtable
/// of the subclass for a potential override.
bool isNonoverridden() const { return Value & IsNonoverriddenMask; }

bool isAsync() const { return Value & IsAsyncMask; }

Expand Down
1 change: 0 additions & 1 deletion lib/AST/Availability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ AvailabilityContext ASTContext::getSwift55Availability() {
return getSwiftFutureAvailability();
}


AvailabilityContext ASTContext::getSwiftFutureAvailability() {
auto target = LangOpts.Target;

Expand Down
136 changes: 74 additions & 62 deletions lib/IRGen/GenThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,79 @@ void IRGenModule::emitMethodLookupFunction(ClassDecl *classDecl) {
return;
}

// In older Swift runtimes, swift_lookUpClassMethod did not implement the
// "non-overridden" bit for method descriptors, so given a descriptor for
// a method whose vtable entry has been pruned, the runtime would attempt
// to resolve a nonexistent vtable entry. We have to check for this flag
// ourselves when deploying to these older runtimes.
auto lookUpFn = getLookUpClassMethodFn();
if (!getAvailabilityContext()
.isContainedIn(Context.getSwift55Availability())) {
// Get or create a local helper function to check for nonoverridden
// method descriptors before calling into the runtime.
auto makeHelper = [&](IRGenFunction &subIGF) -> void {
subIGF.CurFn->setDoesNotAccessMemory();
setHasNoFramePointer(subIGF.CurFn);

auto params = subIGF.collectParameters();
auto subtype = params.claimNext();
auto method = params.claimNext();
auto superLookupContext = params.claimNext();

// Project the flags word from the method descriptor and check for
// the "not overridden" bit.
auto methodFlagsPtr = subIGF.Builder.CreateStructGEP(method, 0);
auto methodFlags = subIGF.Builder.CreateLoad(methodFlagsPtr, Alignment(4));
auto isOverriddenMask = subIGF.Builder.CreateAnd(methodFlags,
llvm::ConstantInt::get(Int32Ty,
MethodDescriptorFlags::IsNonoverriddenMask));
auto isNonoverridden = subIGF.Builder.CreateICmpNE(isOverriddenMask,
llvm::ConstantInt::get(Int32Ty, 0));

auto nonoverriddenBB = subIGF.createBasicBlock("nonoverridden");
auto overridableBB = subIGF.createBasicBlock("overridable");

subIGF.Builder.CreateCondBr(isNonoverridden, nonoverriddenBB, overridableBB);

subIGF.Builder.emitBlock(overridableBB);
// Pass it off to the Swift runtime to do the dynamic method lookup.
auto runtimeResult = subIGF.Builder.CreateCall(getLookUpClassMethodFn(),
{subtype, method, superLookupContext});
subIGF.Builder.CreateRet(runtimeResult);

subIGF.Builder.emitBlock(nonoverriddenBB);
// Resolve the relative reference to the impl from the descriptor itself.
auto methodImpRelPtr = subIGF.Builder.CreateStructGEP(method, 1);
auto methodImpRel = subIGF.Builder.CreateLoad(methodImpRelPtr, Alignment(4));
auto methodImpRelBase = subIGF.Builder.CreatePtrToInt(methodImpRelPtr, IntPtrTy);
auto methodImpRelOff = subIGF.Builder.CreateSExtOrBitCast(methodImpRel, IntPtrTy);
auto methodImpAddr = subIGF.Builder.CreateAdd(methodImpRelBase, methodImpRelOff);

// Sign the method pointer, if this platform does that.
if (auto &schema = getOptions().PointerAuth.SwiftClassMethods) {
auto discriminatorMask = subIGF.Builder.CreateAnd(methodFlags,
llvm::ConstantInt::get(Int32Ty,
MethodDescriptorFlags::ExtraDiscriminatorMask));
auto discriminatorShift = subIGF.Builder.CreateLShr(discriminatorMask,
llvm::ConstantInt::get(Int32Ty,
MethodDescriptorFlags::ExtraDiscriminatorShift));
auto discriminator = subIGF.Builder.CreateZExtOrBitCast(discriminatorShift,
IntPtrTy);
methodImpAddr = emitPointerAuthSign(subIGF, methodImpAddr,
PointerAuthInfo(schema.getKey(), discriminator));
}
auto methodImp = subIGF.Builder.CreateIntToPtr(methodImpAddr, Int8PtrTy);
subIGF.Builder.CreateRet(methodImp);
};
lookUpFn =
getOrCreateHelperFunction("__swift_lookUpPossiblyNonoverriddenClassMethod",
Int8PtrTy,
{TypeMetadataPtrTy,
MethodDescriptorStructTy->getPointerTo(),
TypeContextDescriptorPtrTy},
makeHelper);
}

IRGenFunction IGF(*this, f);

auto params = IGF.collectParameters();
Expand All @@ -434,69 +507,8 @@ void IRGenModule::emitMethodLookupFunction(ClassDecl *classDecl) {
auto *description = getAddrOfTypeContextDescriptor(classDecl,
RequireMetadata);

// Check for lookups of nonoverridden methods first.
class LookUpNonoverriddenMethods
: public ClassMetadataScanner<LookUpNonoverriddenMethods> {

IRGenFunction &IGF;
llvm::Value *methodArg;

public:
LookUpNonoverriddenMethods(IRGenFunction &IGF,
ClassDecl *classDecl,
llvm::Value *methodArg)
: ClassMetadataScanner(IGF.IGM, classDecl), IGF(IGF),
methodArg(methodArg) {}

void noteNonoverriddenMethod(SILDeclRef method) {
// The method lookup function would be used only for `super.` calls
// from other modules, so we only need to look at public-visibility
// methods here.
if (!hasPublicVisibility(method.getLinkage(NotForDefinition))) {
return;
}

auto methodDesc = IGM.getAddrOfMethodDescriptor(method, NotForDefinition);

auto isMethod = IGF.Builder.CreateICmpEQ(methodArg, methodDesc);

auto falseBB = IGF.createBasicBlock("");
auto trueBB = IGF.createBasicBlock("");

IGF.Builder.CreateCondBr(isMethod, trueBB, falseBB);

IGF.Builder.emitBlock(trueBB);
// Since this method is nonoverridden, we can produce a static result.
auto entry = VTable->getEntry(IGM.getSILModule(), method);
llvm::Value *impl = IGM.getAddrOfSILFunction(entry->getImplementation(),
NotForDefinition);
// Sign using the discriminator we would include in the method
// descriptor.
if (auto &schema =
entry->getImplementation()->getLoweredFunctionType()->isAsync()
? IGM.getOptions().PointerAuth.AsyncSwiftClassMethods
: IGM.getOptions().PointerAuth.SwiftClassMethods) {
auto discriminator =
PointerAuthInfo::getOtherDiscriminator(IGM, schema, method);

impl = emitPointerAuthSign(IGF, impl,
PointerAuthInfo(schema.getKey(), discriminator));
}
impl = IGF.Builder.CreateBitCast(impl, IGM.Int8PtrTy);
IGF.Builder.CreateRet(impl);

IGF.Builder.emitBlock(falseBB);
// Continue emission on the false branch.
}

void noteResilientSuperclass() {}
void noteStartOfImmediateMembers(ClassDecl *clas) {}
};

LookUpNonoverriddenMethods(IGF, classDecl, method).layout();

// Use the runtime to look up vtable entries.
auto *result = IGF.Builder.CreateCall(getLookUpClassMethodFn(),
auto *result = IGF.Builder.CreateCall(lookUpFn,
{metadata, method, description});
IGF.Builder.CreateRet(result);
}
19 changes: 19 additions & 0 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3273,6 +3273,25 @@ swift::swift_lookUpClassMethod(const ClassMetadata *metadata,

assert(isAncestorOf(metadata, description));

// If the method descriptor is non-overridden, we can just return the
// implementation pointer from the descriptor.
if (method->Flags.isNonoverridden()) {
auto impl = method->Impl.get();
#if SWIFT_PTRAUTH
if (method->Flags.isAsync()) {
return ptrauth_sign_unauthenticated(impl,
ptrauth_key_process_independent_data,
method->Flags.getExtraDiscriminator());
} else {
return ptrauth_sign_unauthenticated(impl,
ptrauth_key_function_pointer,
method->Flags.getExtraDiscriminator());
}
#else
return impl;
#endif
}

auto *vtable = description->getVTableDescriptor();
assert(vtable != nullptr);

Expand Down
8 changes: 4 additions & 4 deletions test/IRGen/class_resilience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public class ClassWithResilientThenEmpty {

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i8* @"$s16class_resilience26ClassWithResilientPropertyCMu"(%swift.type* %0, %swift.method_descriptor* %1)
// CHECK-NEXT: entry:
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @swift_lookUpClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience26ClassWithResilientPropertyCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @{{.*}}swift_lookUp{{.*}}ClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience26ClassWithResilientPropertyCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: ret i8* [[RESULT]]
// CHECK-NEXT: }

Expand Down Expand Up @@ -528,7 +528,7 @@ public class ClassWithResilientThenEmpty {

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i8* @"$s16class_resilience33ClassWithResilientlySizedPropertyCMu"(%swift.type* %0, %swift.method_descriptor* %1)
// CHECK-NEXT: entry:
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @swift_lookUpClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience33ClassWithResilientlySizedPropertyCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @{{.*}}swift_lookUp{{.*}}ClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience33ClassWithResilientlySizedPropertyCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: ret i8* [[RESULT]]
// CHECK-NEXT: }

Expand All @@ -547,7 +547,7 @@ public class ClassWithResilientThenEmpty {

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i8* @"$s16class_resilience14ResilientChildCMu"(%swift.type* %0, %swift.method_descriptor* %1)
// CHECK-NEXT: entry:
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @swift_lookUpClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience14ResilientChildCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @{{.*}}swift_lookUp{{.*}}ClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience14ResilientChildCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: ret i8* [[RESULT]]
// CHECK-NEXT: }

Expand Down Expand Up @@ -601,6 +601,6 @@ public class ClassWithResilientThenEmpty {

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc i8* @"$s16class_resilience21ResilientGenericChildCMu"(%swift.type* %0, %swift.method_descriptor* %1)
// CHECK-NEXT: entry:
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @swift_lookUpClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience21ResilientGenericChildCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: [[RESULT:%.*]] = call i8* @{{.*}}swift_lookUp{{.*}}ClassMethod(%swift.type* %0, %swift.method_descriptor* %1, %swift.type_descriptor* bitcast ({{.*}}* @"$s16class_resilience21ResilientGenericChildCMn{{(\.ptrauth.*)?}}" to %swift.type_descriptor*))
// CHECK-NEXT: ret i8* [[RESULT]]
// CHECK-NEXT: }
13 changes: 13 additions & 0 deletions test/Interpreter/prune-vtables.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -emit-module-path %t/PrunedBaseClass.swiftmodule -c -o %t/PrunedBaseClass.o

import PrunedBaseClass

public class PrunedSub: PrunedBase {
func exerciseNonoverriddenMethod() {
self.nonoverridden()
super.nonoverridden()
}
}

PrunedSub().exerciseNonoverriddenMethod()