1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023–2025 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 Swift project authors
//
#include "Discovery.h"
#if !defined(SWT_NO_LEGACY_TEST_DISCOVERY)
#include <cstdint>
#include <cstring>
#include <type_traits>
#pragma mark - Swift ABI
#if defined(__PTRAUTH_INTRINSICS__)
#include <ptrauth.h>
#define SWT_PTRAUTH_SWIFT_TYPE_DESCRIPTOR __ptrauth(ptrauth_key_process_independent_data, 1, 0xae86)
#else
#define SWT_PTRAUTH_SWIFT_TYPE_DESCRIPTOR
#endif
/// A type representing a pointer relative to itself.
///
/// This type is derived from `RelativeDirectPointerIntPair` in the Swift
/// repository.
template <typename T, int32_t maskValue = 0>
struct SWTRelativePointer {
private:
int32_t _offset;
public:
SWTRelativePointer(const SWTRelativePointer&) = delete;
SWTRelativePointer(const SWTRelativePointer&&) = delete;
SWTRelativePointer& operator =(const SWTRelativePointer&) = delete;
SWTRelativePointer& operator =(const SWTRelativePointer&&) = delete;
int32_t getRawValue(void) const {
return _offset;
}
const T *_Nullable get(void) const& {
int32_t maskedOffset = getRawValue() & ~maskValue;
if (maskedOffset == 0) {
return nullptr;
}
auto offset = static_cast<uintptr_t>(static_cast<intptr_t>(maskedOffset));
auto result = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(this) + offset);
#if defined(__PTRAUTH_INTRINSICS__)
if (std::is_function_v<T> && result) {
result = ptrauth_strip(result, ptrauth_key_function_pointer);
result = ptrauth_sign_unauthenticated(result, ptrauth_key_function_pointer, 0);
}
#endif
return reinterpret_cast<const T *>(result);
}
};
/// A type representing a 32-bit absolute function pointer, usually used on platforms
/// where relative function pointers are not supported.
///
/// This type is derived from `AbsoluteFunctionPointer` in the Swift repository.
template <typename T>
struct SWTAbsoluteFunctionPointer {
private:
T *_pointer;
static_assert(sizeof(T *) == sizeof(int32_t), "Function pointer must be 32-bit when using compact absolute pointer");
public:
const T *_Nullable get(void) const & {
return _pointer;
}
};
/// A type representing a pointer relative to itself with low bits reserved for
/// use as flags.
///
/// This type is derived from `RelativeDirectPointerIntPair` in the Swift
/// repository.
template <typename T, typename I, int32_t maskValue = (alignof(int32_t) - 1)>
struct SWTRelativePointerIntPair: public SWTRelativePointer<T, maskValue> {
I getInt() const & {
return I(this->getRawValue() & maskValue);
}
};
template <typename T>
#if defined(__wasm32__)
using SWTCompactFunctionPointer = SWTAbsoluteFunctionPointer<T>;
#else
using SWTCompactFunctionPointer = SWTRelativePointer<T>;
#endif
/// A type representing a metatype as constructed during compilation of a Swift
/// module.
///
/// This type is derived from `TargetTypeContextDescriptor` in the Swift
/// repository.
struct SWTTypeContextDescriptor {
private:
uint32_t _flags;
SWTRelativePointer<void> _parent;
SWTRelativePointer<char> _name;
struct MetadataAccessResponse {
void *value;
size_t state;
};
using MetadataAccessFunction = __attribute__((swiftcall)) MetadataAccessResponse(size_t);
SWTCompactFunctionPointer<MetadataAccessFunction> _metadataAccessFunction;
public:
const char *_Nullable getName(void) const& {
return _name.get();
}
void *_Nullable getMetadata(void) const& {
if (auto fp = _metadataAccessFunction.get()) {
return (* fp)(0xFF).value;
}
return nullptr;
}
bool isGeneric(void) const& {
return (_flags & 0x80u) != 0;
}
};
/// A type representing a relative pointer to a type descriptor.
///
/// This type is derived from `TargetTypeMetadataRecord` in the Swift
/// repository.
struct SWTTypeMetadataRecord {
private:
SWTRelativePointerIntPair<void, unsigned int> _pointer;
public:
const SWTTypeContextDescriptor *_Nullable getContextDescriptor(void) const {
switch (_pointer.getInt()) {
case 0: // Direct pointer.
return reinterpret_cast<const SWTTypeContextDescriptor *>(_pointer.get());
case 1: // Indirect pointer (pointer to a pointer.)
// The inner pointer is signed when pointer authentication
// instructions are available.
if (auto contextDescriptor = reinterpret_cast<SWTTypeContextDescriptor *const SWT_PTRAUTH_SWIFT_TYPE_DESCRIPTOR *>(_pointer.get())) {
return *contextDescriptor;
}
[[fallthrough]];
default: // Unsupported or invalid.
return nullptr;
}
}
};
#pragma mark - Legacy test discovery
const size_t SWTTypeMetadataRecordByteCount = sizeof(SWTTypeMetadataRecord);
const void *swt_getTypeFromTypeMetadataRecord(const void *recordAddress, const char *nameSubstring) {
auto record = reinterpret_cast<const SWTTypeMetadataRecord *>(recordAddress);
auto contextDescriptor = record->getContextDescriptor();
if (!contextDescriptor) {
// This type metadata record is invalid (or we don't understand how to
// get its context descriptor), so skip it.
return nullptr;
} else if (contextDescriptor->isGeneric()) {
// Generic types cannot be fully instantiated without generic
// parameters, which is not something we can know abstractly.
return nullptr;
}
// Check that the type's name passes. This will be more expensive than the
// checks above, but should be cheaper than realizing the metadata.
const char *typeName = contextDescriptor->getName();
bool nameOK = typeName && nullptr != std::strstr(typeName, nameSubstring);
if (!nameOK) {
return nullptr;
}
if (void *typeMetadata = contextDescriptor->getMetadata()) {
return typeMetadata;
}
return nullptr;
}
#endif
|