Skip to content

[Wasm][KeyPath] Resolve absolute function pointer as identity #42096

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
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
2 changes: 2 additions & 0 deletions include/swift/ABI/KeyPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class KeyPathComponentHeader {

enum ComputedPropertyIDResolution {
Resolved,
ResolvedAbsolute,
IndirectPointer,
FunctionCall,
};
Expand All @@ -214,6 +215,7 @@ class KeyPathComponentHeader {
? _SwiftKeyPathComponentHeader_ComputedIDByVTableOffsetFlag : 0)
| (hasArguments ? _SwiftKeyPathComponentHeader_ComputedHasArgumentsFlag : 0)
| (resolution == Resolved ? _SwiftKeyPathComponentHeader_ComputedIDResolved
: resolution == ResolvedAbsolute ? _SwiftKeyPathComponentHeader_ComputedIDResolvedAbsolute
: resolution == IndirectPointer ? _SwiftKeyPathComponentHeader_ComputedIDUnresolvedIndirectPointer
: resolution == FunctionCall ? _SwiftKeyPathComponentHeader_ComputedIDUnresolvedFunctionCall
: (assert(false && "invalid resolution"), 0)));
Expand Down
14 changes: 11 additions & 3 deletions lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,11 @@ emitKeyPathComponent(IRGenModule &IGM,
// instantiation time.
idResolution = idRef.isIndirect()
? KeyPathComponentHeader::IndirectPointer
: KeyPathComponentHeader::Resolved;
// Compute absolute reference from relative reference if target supports it.
// Otherwise, embed absolute reference directly.
: (IGM.getOptions().CompactAbsoluteFunctionPointer
? KeyPathComponentHeader::ResolvedAbsolute
: KeyPathComponentHeader::Resolved);
break;
}
case KeyPathPatternComponent::ComputedPropertyId::DeclRef: {
Expand Down Expand Up @@ -1089,8 +1093,12 @@ emitKeyPathComponent(IRGenModule &IGM,
fields.addInt32(header.getData());
switch (idKind) {
case KeyPathComponentHeader::Pointer:
// Use a relative offset to the referent.
fields.addRelativeAddress(idValue);
// Use a relative offset to the referent if value is not absolute.
if (idResolution == KeyPathComponentHeader::ResolvedAbsolute) {
fields.add(idValue);
} else {
fields.addRelativeAddress(idValue);
}
break;

case KeyPathComponentHeader::VTableOffset:
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/SwiftShims/KeyPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static const __swift_uint32_t _SwiftKeyPathComponentHeader_ComputedIDResolutionM
= 0x0000000FU;
static const __swift_uint32_t _SwiftKeyPathComponentHeader_ComputedIDResolved
= 0x00000000U;
static const __swift_uint32_t _SwiftKeyPathComponentHeader_ComputedIDResolvedAbsolute
= 0x00000003U;
static const __swift_uint32_t _SwiftKeyPathComponentHeader_ComputedIDUnresolvedIndirectPointer
= 0x00000002U;
static const __swift_uint32_t _SwiftKeyPathComponentHeader_ComputedIDUnresolvedFunctionCall
Expand Down
24 changes: 17 additions & 7 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ internal enum KeyPathComputedIDKind {

internal enum KeyPathComputedIDResolution {
case resolved
case resolvedAbsolute
case indirectPointer
case functionCall
}
Expand Down Expand Up @@ -1108,6 +1109,9 @@ internal struct RawKeyPathComponent {
internal static var computedIDResolved: UInt32 {
return _SwiftKeyPathComponentHeader_ComputedIDResolved
}
internal static var computedIDResolvedAbsolute: UInt32 {
return _SwiftKeyPathComponentHeader_ComputedIDResolvedAbsolute
}
internal static var computedIDUnresolvedIndirectPointer: UInt32 {
return _SwiftKeyPathComponentHeader_ComputedIDUnresolvedIndirectPointer
}
Expand All @@ -1118,6 +1122,8 @@ internal struct RawKeyPathComponent {
switch payload & Header.computedIDResolutionMask {
case Header.computedIDResolved:
return .resolved
case Header.computedIDResolvedAbsolute:
return .resolvedAbsolute
case Header.computedIDUnresolvedIndirectPointer:
return .indirectPointer
case Header.computedIDUnresolvedFunctionCall:
Expand Down Expand Up @@ -3381,35 +3387,39 @@ internal struct InstantiateKeyPathBuffer: KeyPathPatternVisitor {
resolvedID = UnsafeRawPointer(bitPattern: value)

case .pointer:
// Resolve the sign-extended relative reference.
var absoluteID: UnsafeRawPointer? = _resolveRelativeAddress(idValueBase, idValue)

// If the pointer ID is unresolved, then it needs work to get to
// the final value.
switch idResolution {
case .resolved:
resolvedID = _resolveRelativeAddress(idValueBase, idValue)
break

case .resolvedAbsolute:
let value = UInt(UInt32(bitPattern: idValue))
resolvedID = UnsafeRawPointer(bitPattern: value)
break

case .indirectPointer:
// The pointer in the pattern is an indirect pointer to the real
// identifier pointer.
absoluteID = absoluteID.unsafelyUnwrapped
let absoluteID = _resolveRelativeAddress(idValueBase, idValue)
resolvedID = absoluteID
.load(as: UnsafeRawPointer?.self)

case .functionCall:
// The pointer in the pattern is to a function that generates the
// identifier pointer.
typealias Resolver = @convention(c) (UnsafeRawPointer?) -> UnsafeRawPointer?
let absoluteID = _resolveCompactFunctionPointer(idValueBase, idValue)
let resolverSigned = _PtrAuth.sign(
pointer: absoluteID.unsafelyUnwrapped,
pointer: absoluteID,
key: .processIndependentCode,
discriminator: _PtrAuth.discriminator(for: Resolver.self))
let resolverFn = unsafeBitCast(resolverSigned,
to: Resolver.self)

absoluteID = resolverFn(patternArgs)
resolvedID = resolverFn(patternArgs)
}
resolvedID = absoluteID
}

// Bring over the header, getter, and setter.
Expand Down