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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 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"
#include <atomic>
#include <cstring>
#include <iterator>
#include <type_traits>
#include <vector>
#if defined(SWT_NO_DYNAMIC_LINKING)
#include <algorithm>
#elif defined(__APPLE__)
#include <dispatch/dispatch.h>
#include <mach-o/dyld.h>
#include <mach-o/getsect.h>
#include <objc/runtime.h>
#include <os/lock.h>
#endif
/// Enumerate over all Swift type metadata sections in the current process.
///
/// - Parameters:
/// - body: A function to call once for every section in the current process.
/// A pointer to the first type metadata record and the number of records
/// are passed to this function.
template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body);
#pragma mark - Swift ABI
#if defined(__PTRAUTH_INTRINSICS__)
#include <ptrauth.h>
#define SWT_PTRAUTH __ptrauth
#else
#define SWT_PTRAUTH(...)
#endif
#define SWT_PTRAUTH_SWIFT_TYPE_DESCRIPTOR SWT_PTRAUTH(ptrauth_key_process_independent_data, 1, 0xae86)
/// 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);
}
const T *_Nullable operator ->(void) const& {
return get();
}
};
/// 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;
}
const T *_Nullable operator ->(void) const & {
return get();
}
};
/// 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;
}
}
};
#if defined(SWT_NO_DYNAMIC_LINKING)
#pragma mark - Statically-linked implementation
// This environment does not have a dynamic linker/loader. Therefore, there is
// only one image (this one) with Swift code in it.
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp
extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");
template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
auto size = std::distance(§ionBegin, §ionEnd);
body(§ionBegin, size);
}
#elif defined(__APPLE__)
#pragma mark - Apple implementation
/// A type that acts as a C++ [Allocator](https://en.cppreference.com/w/cpp/named_req/Allocator)
/// without using global `operator new` or `operator delete`.
///
/// This type is necessary because global `operator new` and `operator delete`
/// can be overridden in developer-supplied code and cause deadlocks or crashes
/// when subsequently used while holding a dyld- or libobjc-owned lock. Using
/// `std::malloc()` and `std::free()` allows the use of C++ container types
/// without this risk.
template<typename T>
struct SWTHeapAllocator {
using value_type = T;
T *allocate(size_t count) {
return reinterpret_cast<T *>(std::calloc(count, sizeof(T)));
}
void deallocate(T *ptr, size_t count) {
std::free(ptr);
}
};
/// A type that acts as a C++ [Container](https://en.cppreference.com/w/cpp/named_req/Container)
/// and which contains a sequence of Mach headers.
#if __LP64__
using SWTMachHeaderList = std::vector<const mach_header_64 *, SWTHeapAllocator<const mach_header_64 *>>;
#else
using SWTMachHeaderList = std::vector<const mach_header *, SWTHeapAllocator<const mach_header *>>;
#endif
/// Get a copy of the currently-loaded Mach headers list.
///
/// - Returns: A list of Mach headers loaded into the current process. The order
/// of the resulting list is unspecified.
///
/// On non-Apple platforms, the `swift_enumerateAllMetadataSections()` function
/// exported by the runtime serves the same purpose as this function.
static SWTMachHeaderList getMachHeaders(void) {
/// This list is necessarily mutated while a global libobjc- or dyld-owned
/// lock is held. Hence, code using this list must avoid potentially
/// re-entering either library (otherwise it could potentially deadlock.)
///
/// To see how the Swift runtime accomplishes the above goal, see
/// `ConcurrentReadableArray` in that project's Concurrent.h header. Since the
/// testing library is not tasked with the same performance constraints as
/// Swift's runtime library, we just use a `std::vector` guarded by an unfair
/// lock.
static constinit SWTMachHeaderList *machHeaders = nullptr;
static constinit os_unfair_lock lock = OS_UNFAIR_LOCK_INIT;
static constinit dispatch_once_t once = 0;
dispatch_once_f(&once, nullptr, [] (void *) {
machHeaders = reinterpret_cast<SWTMachHeaderList *>(std::malloc(sizeof(SWTMachHeaderList)));
::new (machHeaders) SWTMachHeaderList();
machHeaders->reserve(_dyld_image_count());
objc_addLoadImageFunc([] (const mach_header *mh) {
auto mhn = reinterpret_cast<SWTMachHeaderList::value_type>(mh);
// Ignore this Mach header if it is in the shared cache. On platforms that
// support it (Darwin), most system images are contained in this range.
// System images can be expected not to contain test declarations, so we
// don't need to walk them.
if (mhn->flags & MH_DYLIB_IN_CACHE) {
return;
}
// Only store the mach header address if the image contains Swift data.
// Swift does not support unloading images, but images that do not contain
// Swift code may be unloaded at runtime and later crash
// the testing library when it calls enumerateTypeMetadataSections().
unsigned long size = 0;
if (getsectiondata(mhn, SEG_TEXT, "__swift5_types", &size)) {
os_unfair_lock_lock(&lock); {
machHeaders->push_back(mhn);
} os_unfair_lock_unlock(&lock);
}
});
});
// After the first call sets up the loader hook, all calls take the lock and
// make a copy of whatever has been loaded so far.
SWTMachHeaderList result;
result.reserve(_dyld_image_count());
os_unfair_lock_lock(&lock); {
result = *machHeaders;
} os_unfair_lock_unlock(&lock);
return result;
}
template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
SWTMachHeaderList machHeaders = getMachHeaders();
for (auto mh : machHeaders) {
unsigned long size = 0;
const void *section = getsectiondata(mh, SEG_TEXT, "__swift5_types", &size);
if (section && size > 0) {
body(section, size);
}
}
}
#elif defined(__linux__) || defined(_WIN32) || defined(__wasi__)
#pragma mark - Linux/Windows implementation
/// Specifies the address range corresponding to a section.
struct MetadataSectionRange {
uintptr_t start;
size_t length;
};
/// Identifies the address space ranges for the Swift metadata required by the
/// Swift runtime.
struct MetadataSections {
uintptr_t version;
std::atomic<const void *> baseAddress;
void *unused0;
void *unused1;
MetadataSectionRange swift5_protocols;
MetadataSectionRange swift5_protocol_conformances;
MetadataSectionRange swift5_type_metadata;
MetadataSectionRange swift5_typeref;
MetadataSectionRange swift5_reflstr;
MetadataSectionRange swift5_fieldmd;
MetadataSectionRange swift5_assocty;
MetadataSectionRange swift5_replace;
MetadataSectionRange swift5_replac2;
MetadataSectionRange swift5_builtin;
MetadataSectionRange swift5_capture;
MetadataSectionRange swift5_mpenum;
MetadataSectionRange swift5_accessible_functions;
};
/// A function exported by the Swift runtime that enumerates all metadata
/// sections loaded into the current process.
SWT_IMPORT_FROM_STDLIB void swift_enumerateAllMetadataSections(
bool (* body)(const MetadataSections *sections, void *context),
void *context
);
template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
swift_enumerateAllMetadataSections([] (const MetadataSections *sections, void *context) {
const auto& body = *reinterpret_cast<const SectionEnumerator *>(context);
MetadataSectionRange section = sections->swift5_type_metadata;
if (section.start && section.length > 0) {
body(reinterpret_cast<const void *>(section.start), section.length);
}
return true;
}, const_cast<SectionEnumerator *>(&body));
}
#else
#warning Platform-specific implementation missing: Runtime test discovery unavailable
template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {}
#endif
#pragma mark -
void swt_enumerateTypesWithNamesContaining(const char *nameSubstring, void *context, SWTTypeEnumerator body) {
enumerateTypeMetadataSections([=] (const void *section, size_t size) {
auto records = reinterpret_cast<const SWTTypeMetadataRecord *>(section);
size_t recordCount = size / sizeof(SWTTypeMetadataRecord);
bool stop = false;
for (size_t i = 0; i < recordCount && !stop; i++) {
const auto& record = records[i];
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.
continue;
} else if (contextDescriptor->isGeneric()) {
// Generic types cannot be fully instantiated without generic
// parameters, which is not something we can know abstractly.
continue;
}
// 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) {
continue;
}
if (void *typeMetadata = contextDescriptor->getMetadata()) {
body(typeMetadata, &stop, context);
}
}
});
}
|