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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
|
//===--- LibPrespecialized.cpp - Interface for prespecializations----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/LibPrespecialized.h"
#include "MetadataCache.h"
#include "Private.h"
#include "swift/Basic/Lazy.h"
#include "swift/Runtime/EnvironmentVariables.h"
#include "swift/Runtime/Metadata.h"
#include <atomic>
#if SWIFT_STDLIB_HAS_DLADDR && __has_include(<dlfcn.h>)
#include <dlfcn.h>
#define USE_DLOPEN 1
#endif
#if __has_include(<mach-o/dyld_priv.h>)
#include <mach-o/dyld_priv.h>
#endif
#if __has_include(<os/feature_private.h>)
#include <os/feature_private.h> // for os_feature_enabled_simple()
#define HAS_OS_FEATURE 1
#endif
using namespace swift;
static bool prespecializedLoggingEnabled = false;
#define LOG(fmt, ...) \
do { \
if (SWIFT_UNLIKELY(prespecializedLoggingEnabled)) \
fprintf(stderr, "Prespecializations library: " fmt "\n", __VA_ARGS__); \
} while (0)
#define LOG0(string) LOG("%s", string)
static bool environmentProcessListContainsProcess(const char *list,
const char *progname) {
auto prognameLen = strlen(progname);
const char *cursor = list;
while (true) {
const char *next = strchr(cursor, ':');
if (!next) {
// Last entry in the list. Compare with the entire rest of the string.
return strcmp(progname, cursor) == 0;
}
// Entry at beginning or middle of the list. Compare against this substring.
size_t len = next - cursor;
if (len == prognameLen && strncmp(cursor, progname, len) == 0)
return true;
cursor = next + 1;
}
}
static bool isThisProcessEnabled(const LibPrespecializedData<InProcess> *data) {
extern const char *__progname;
if (!__progname)
return true;
auto envEnabledProcesses =
runtime::environment::SWIFT_DEBUG_LIB_PRESPECIALIZED_ENABLED_PROCESSES();
if (envEnabledProcesses && *envEnabledProcesses) {
if (environmentProcessListContainsProcess(envEnabledProcesses,
__progname)) {
LOG("Found %s in SWIFT_DEBUG_LIB_PRESPECIALIZED_ENABLED_PROCESSES, "
"enabling",
__progname);
return true;
}
}
auto envDisabledProcesses =
runtime::environment::SWIFT_DEBUG_LIB_PRESPECIALIZED_DISABLED_PROCESSES();
if (envDisabledProcesses && *envDisabledProcesses) {
if (environmentProcessListContainsProcess(envDisabledProcesses,
__progname)) {
LOG("Found %s in SWIFT_DEBUG_LIB_PRESPECIALIZED_DISABLED_PROCESSES, "
"disabling",
__progname);
return false;
}
}
if (auto *disabledProcesses = data->getDisabledProcessesTable()) {
auto *cursor = disabledProcesses;
while (auto *name = *cursor) {
if (strcmp(name, __progname) == 0) {
LOG("Found %s in disabled processes list, disabling", name);
return false;
}
cursor++;
}
}
return true;
}
struct LibPrespecializedState {
struct AddressRange {
uintptr_t start, end;
bool contains(const void *ptr) const {
return start <= (uintptr_t)ptr && (uintptr_t)ptr < end;
}
};
enum class MapConfiguration {
Unset,
UseNameKeyedMap,
UsePointerKeyedMap,
UsePointerKeyedMapDebugMode,
Disabled,
};
const LibPrespecializedData<InProcess> *data;
std::atomic<MapConfiguration> mapConfiguration = MapConfiguration::Unset;
AddressRange sharedCacheRange{0, 0};
AddressRange metadataAllocatorInitialPoolRange{0, 0};
bool descriptorMapEnabled;
LibPrespecializedState() {
prespecializedLoggingEnabled =
runtime::environment::SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_LOGGING();
data = findLibPrespecialized();
#if DYLD_GET_SWIFT_PRESPECIALIZED_DATA_DEFINED
size_t sharedCacheLength;
sharedCacheRange.start =
(uintptr_t)_dyld_get_shared_cache_range(&sharedCacheLength);
sharedCacheRange.end = sharedCacheRange.start + sharedCacheLength;
auto [initialPoolStart, initialPoolLength] =
MetadataAllocator::InitialPoolLocation();
metadataAllocatorInitialPoolRange.start = (uintptr_t)initialPoolStart;
metadataAllocatorInitialPoolRange.end =
metadataAllocatorInitialPoolRange.start + initialPoolLength;
#endif
// Compute our map configuration if it hasn't already been set. We must do
// this after the shared cache range has been retrieved, because the map
// configuration can be different depending on whether the map is in the
// shared cache.
if (mapConfiguration.load(std::memory_order_relaxed) ==
MapConfiguration::Unset)
mapConfiguration.store(computeMapConfiguration(data),
std::memory_order_relaxed);
if (data) {
descriptorMapEnabled =
data->getOptionFlags() &
LibPrespecializedData<InProcess>::OptionFlagDescriptorMapDefaultOn;
LOG("Setting descriptorMapEnabled=%s from the option flags.",
descriptorMapEnabled ? "true" : "false");
}
if (runtime::environment::
SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_DESCRIPTOR_LOOKUP_isSet()) {
descriptorMapEnabled = runtime::environment::
SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_DESCRIPTOR_LOOKUP();
LOG("Setting descriptorMapEnabled=%s from "
"SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_DESCRIPTOR_LOOKUP.",
descriptorMapEnabled ? "true" : "false");
} else {
#if HAS_OS_FEATURE
if (os_feature_enabled_simple(Swift, togglePrespecializationDescriptorMap,
false)) {
descriptorMapEnabled = !descriptorMapEnabled;
LOG("Toggling descriptorMapEnabled to %s "
"togglePrespecializationDescriptorMap is set.",
descriptorMapEnabled ? "true" : "false");
}
#endif
}
}
MapConfiguration
computeMapConfiguration(const LibPrespecializedData<InProcess> *data) {
// If no data, we have to disable.
if (!data)
return MapConfiguration::Disabled;
if (!runtime::environment::
SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_METADATA()) {
LOG0("Disabling metadata, SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_METADATA "
"is false.");
return MapConfiguration::Disabled;
}
auto nameKeyedMap = data->getMetadataMap();
auto pointerKeyedMap = data->getPointerKeyedMetadataMap();
// If we don't have either map, then disable it completely.
if (!nameKeyedMap && !pointerKeyedMap) {
LOG("No prespecializations map available from data at %p, disabling.",
data);
return MapConfiguration::Disabled;
}
// If we don't have the pointer-keyed map, fall back to the name-keyed map.
if (!pointerKeyedMap) {
LOG("Data at %p only contains name-keyed map.", data);
return MapConfiguration::UseNameKeyedMap;
}
// If we don't have the name-keyed map, always use the pointer-keyed map.
if (!nameKeyedMap) {
LOG("Data at %p only contains pointer-keyed map.", data);
return MapConfiguration::UsePointerKeyedMap;
}
// We have both. Consult the option flag.
bool usePointerKeyedMap =
data->getOptionFlags() &
LibPrespecializedData<InProcess>::OptionFlagDefaultToPointerKeyedMap;
#if HAS_OS_FEATURE
if (os_feature_enabled_simple(Swift, useAlternatePrespecializationMap,
false))
usePointerKeyedMap = !usePointerKeyedMap;
#endif
LOG("Data at %p contains both maps. Using %s keyed map.", data,
usePointerKeyedMap ? "pointer" : "name");
if (usePointerKeyedMap) {
// If we're using a map outside the shared cache, then we're in debug mode
// and need to use our own slow lookup.
if (!sharedCacheRange.contains(pointerKeyedMap))
return MapConfiguration::UsePointerKeyedMapDebugMode;
return MapConfiguration::UsePointerKeyedMap;
}
return MapConfiguration::UseNameKeyedMap;
}
const LibPrespecializedData<InProcess> *findLibPrespecialized() {
const void *dataPtr = nullptr;
#if USE_DLOPEN
auto path = runtime::environment::SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH();
if (path && path[0]) {
// Use RTLD_NOLOAD to avoid actually loading the library. We just want to
// find it if it has already been loaded by other means, such as
// DYLD_INSERT_LIBRARIES.
void *handle = dlopen(path, RTLD_LAZY | RTLD_NOLOAD);
if (!handle) {
swift::warning(0, "Failed to load prespecializations library: %s\n",
dlerror());
return nullptr;
}
dataPtr = dlsym(handle, LIB_PRESPECIALIZED_TOP_LEVEL_SYMBOL_NAME);
LOG("Loaded custom library from %s, found dataPtr %p", path, dataPtr);
}
#if DYLD_GET_SWIFT_PRESPECIALIZED_DATA_DEFINED
else if (SWIFT_RUNTIME_WEAK_CHECK(_dyld_get_swift_prespecialized_data)) {
dataPtr = SWIFT_RUNTIME_WEAK_USE(_dyld_get_swift_prespecialized_data());
LOG("Got dataPtr %p from _dyld_get_swift_prespecialized_data", dataPtr);
// Disable the prespecialized metadata if anything in the shared cache is
// overridden. Eventually we want to be cleverer and only disable the
// prespecializations that have been invalidated, but we'll start with the
// simplest approach.
if (dyld_shared_cache_some_image_overridden()) {
mapConfiguration.store(MapConfiguration::Disabled,
std::memory_order_release);
LOG("Disabling prespecialized metadata, "
"dyld_shared_cache_some_image_overridden = %d",
dyld_shared_cache_some_image_overridden());
}
}
#endif
#endif
LOG("Returning data pointer %p", dataPtr);
if (!dataPtr)
return nullptr;
auto *data =
reinterpret_cast<const LibPrespecializedData<InProcess> *>(dataPtr);
if (data->majorVersion !=
LibPrespecializedData<InProcess>::currentMajorVersion) {
LOG("Unknown major version %" PRIu32 ", disabling", data->majorVersion);
return nullptr;
}
if (!isThisProcessEnabled(data))
return nullptr;
LOG("Returning data %p, major version %" PRIu32 " minor %" PRIu32, data,
data->majorVersion, data->minorVersion);
LOG(" optionFlags=%#zx", data->getOptionFlags());
LOG(" metadataMap=%p", data->getMetadataMap());
LOG(" disabledProcessTable=%p", data->getDisabledProcessesTable());
LOG(" pointerKeyedMetadataMap=%p", data->getPointerKeyedMetadataMap());
LOG(" descriptorMap=%p", data->getDescriptorMap());
return data;
}
};
static Lazy<LibPrespecializedState> LibPrespecialized;
// Returns true if the type has any arguments that aren't plain types (packs or
// unknown kinds).
static bool
hasNonTypeGenericArguments(const TargetGenericContext<InProcess> *generics) {
for (auto param : generics->getGenericParams())
if (param.getKind() != GenericParamKind::Type)
return true;
return false;
}
static bool
isPotentialPrespecializedPointer(const LibPrespecializedState &state,
const void *pointer) {
// Prespecialized metadata descriptors and arguments are always in the shared
// cache. They're either statically emitted metadata, or they're
// prespecialized metadata. Anything that's dynamically allocated, or
// statically allocated outside the shared cache, is not a possible candidate.
// If we're loading a debug libprespecialized, we can't do these checks, so
// just say everything is a potential argument. Performance is not so
// important in that case.
if (!state.sharedCacheRange.contains(state.data))
return true;
// Anything outside the shared cache isn't a potential argument.
if (!state.sharedCacheRange.contains(pointer))
return false;
// Dynamically allocated metadata could be within the shared cache, in the
// initial metadata allocation pool. Reject anything in that region.
if (state.metadataAllocatorInitialPoolRange.contains(pointer))
return false;
return true;
}
static bool isDescriptorLoaded(const void *descriptor, uint16_t imageIndex) {
#if DYLD_GET_SWIFT_PRESPECIALIZED_DATA_DEFINED
return _dyld_is_preoptimized_objc_image_loaded(imageIndex);
#else
// If we're not using the dyld SPI, then we're working with a test dylib, and
// a test dylib can't have pointers to unloaded dylibs.
return true;
#endif
}
void
swift::libPrespecializedImageLoaded() {
#if DYLD_GET_SWIFT_PRESPECIALIZED_DATA_DEFINED
// A newly loaded image might have caused us to load images that are
// overriding images in the shared cache. If we do that, turn off
// prespecialized metadata.
if (dyld_shared_cache_some_image_overridden())
LibPrespecialized.get().mapConfiguration.store(
LibPrespecializedState::MapConfiguration::Disabled,
std::memory_order_release);
#endif
}
static Metadata *
getMetadataFromNameKeyedMap(const LibPrespecializedState &state,
const TypeContextDescriptor *description,
const void *const *arguments) {
auto *generics = description->getGenericContext();
if (!generics)
return nullptr;
// We don't support types with pack parameters yet (and especially not types
// with unknown parameter kinds) so don't even try to look those up.
if (hasNonTypeGenericArguments(generics))
return nullptr;
if (!isPotentialPrespecializedPointer(state, description)) {
LOG("Rejecting descriptor %p, not in the shared cache",
(const void *)description);
return nullptr;
}
auto numKeyArguments = generics->getGenericContextHeader().NumKeyArguments;
for (unsigned i = 0; i < numKeyArguments; i++) {
if (!isPotentialPrespecializedPointer(state, arguments[i])) {
LOG("Rejecting argument %u %p to descriptor %p, not in the shared cache",
i, arguments[i], (const void *)description);
return nullptr;
}
}
StackAllocatedDemangler<4096> dem;
auto mangleNode = _buildDemanglingForGenericType(description, arguments, dem);
if (!mangleNode) {
LOG("failed to build demangling with descriptor %p.", description);
return nullptr;
}
if (mangleNode->getKind() != Node::Kind::Global) {
auto wrapper = dem.createNode(Node::Kind::Global);
wrapper->addChild(mangleNode, dem);
mangleNode = wrapper;
}
auto resolver = [](SymbolicReferenceKind kind,
const void *ref) -> NodePointer {
swift::fatalError(0,
"Unexpected symbolic reference %p in generated mangle "
"tree for generic type lookup.",
ref);
};
auto mangling = Demangle::mangleNode(mangleNode, resolver, dem);
if (!mangling.isSuccess()) {
swift::warning(0,
"Mangling for prespecialized metadata failed with code %d",
mangling.error().code);
return nullptr;
}
auto key = mangling.result();
auto *metadataMap = state.data->getMetadataMap();
auto *element = metadataMap->find(key.data(), key.size());
auto *result = element ? element->value : nullptr;
LOG("found %p for key '%.*s'.", result, (int)key.size(), key.data());
return result;
}
static Metadata *
getMetadataFromPointerKeyedMap(const LibPrespecializedState &state,
const TypeContextDescriptor *description,
const void *const *arguments) {
#if DYLD_FIND_POINTER_HASH_TABLE_ENTRY_DEFINED
if (SWIFT_RUNTIME_WEAK_CHECK(_dyld_find_pointer_hash_table_entry)) {
auto *generics = description->getGenericContext();
if (!generics)
return nullptr;
auto argumentCount = generics->getGenericContextHeader().NumKeyArguments;
auto *map = state.data->getPointerKeyedMetadataMap();
auto result = SWIFT_RUNTIME_WEAK_USE(_dyld_find_pointer_hash_table_entry(
map, description, argumentCount, const_cast<const void **>(arguments)));
LOG("Looking up description %p in dyld table, found %p.", description,
result);
return reinterpret_cast<Metadata *>(const_cast<void *>(result));
}
#else
LOG("Looking up description %p but dyld hash table call not available.",
description);
return nullptr;
#endif
}
// When we have a pointer-keyed map from a debug library, it's not built as a
// hash table. We just scan it linearly.
static Metadata *getMetadataFromPointerKeyedMapDebugMode(
const LibPrespecializedState &state,
const TypeContextDescriptor *description, const void *const *arguments) {
auto *generics = description->getGenericContext();
if (!generics)
return nullptr;
auto argumentCount = generics->getGenericContextHeader().NumKeyArguments;
auto *mapPtr = state.data->getPointerKeyedMetadataMap();
struct MapKey {
size_t count;
void *pointers[];
};
struct MapEntry {
const MapKey *key;
Metadata *value;
};
struct Map {
size_t count;
MapEntry entries[];
};
const Map *map = reinterpret_cast<const Map *>(mapPtr);
for (size_t i = 0; i < map->count; i++) {
auto &entry = map->entries[i];
// Keys are descriptor followed by arguments, so their count is 1 plus the
// argument count.
if (entry.key->count != argumentCount + 1)
continue;
// Check the descriptor.
if (description != entry.key->pointers[0])
continue;
// Check the rest. The pointers array is now offset by 1 since index 0 is
// the descriptor.
bool equal = true;
for (size_t j = 0; j < argumentCount; j++) {
if (entry.key->pointers[j + 1] != arguments[j]) {
equal = false;
break;
}
}
if (equal) {
LOG("Looking up description %p in debug table, found %p.", description,
entry.value);
return entry.value;
}
}
LOG("Looking up description %p in debug table, no entry found.", description);
return nullptr;
}
Metadata *
swift::getLibPrespecializedMetadata(const TypeContextDescriptor *description,
const void *const *arguments) {
auto &state = LibPrespecialized.get();
switch (state.mapConfiguration) {
case LibPrespecializedState::MapConfiguration::Unset:
assert(false &&
"Map configuration should never be unset after initialization.");
return nullptr;
case LibPrespecializedState::MapConfiguration::Disabled:
return nullptr;
case LibPrespecializedState::MapConfiguration::UseNameKeyedMap:
return getMetadataFromNameKeyedMap(state, description, arguments);
case LibPrespecializedState::MapConfiguration::UsePointerKeyedMap:
return getMetadataFromPointerKeyedMap(state, description, arguments);
case LibPrespecializedState::MapConfiguration::UsePointerKeyedMapDebugMode:
return getMetadataFromPointerKeyedMapDebugMode(state, description,
arguments);
}
}
std::pair<LibPrespecializedLookupResult, const TypeContextDescriptor *>
swift::getLibPrespecializedTypeDescriptor(Demangle::NodePointer node) {
auto &state = LibPrespecialized.get();
// Retrieve the map and return immediately if we don't have it.
auto *data = state.data;
if (!data)
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
if (!state.descriptorMapEnabled)
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
auto *descriptorMap = data->getDescriptorMap();
if (!descriptorMap)
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
// Demangler and resolver for subsequent mangling operations.
StackAllocatedDemangler<4096> dem;
ExpandResolvedSymbolicReferences resolver{dem};
if (SWIFT_UNLIKELY(prespecializedLoggingEnabled)) {
auto mangling = Demangle::mangleNode(node, resolver, dem);
if (!mangling.isSuccess()) {
LOG("Failed to build demangling for node %p.", node);
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
}
auto mangled = mangling.result();
LOG("Looking up descriptor named '%.*s'.", (int)mangled.size(),
mangled.data());
}
// Get the simplified mangling that we use as the map's key.
auto simplifiedNode = buildSimplifiedDescriptorDemangling(node, dem);
if (!simplifiedNode) {
LOG("Failed to build simplified mangling for node %p.", node);
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
}
auto simplifiedMangling = Demangle::mangleNode(simplifiedNode, resolver, dem);
if (!simplifiedMangling.isSuccess()) {
LOG("Failed to build demangling for simplified node %p.\n", node);
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
}
// The map key is the simplified mangled name.
auto key = simplifiedMangling.result();
// Track how many descriptors we checked and how many were actually loaded,
// for logging.
unsigned numDescriptorsChecked = 0;
unsigned numDescriptorsLoaded = 0;
// A descriptor is a match if it's actually loaded, and if it matches the node
// we're looking up.
auto isMatch = [&](auto pointers) {
auto *descriptor = *pointers.first;
uint16_t libraryIndex = *pointers.second;
numDescriptorsChecked++;
if (!isDescriptorLoaded(descriptor, libraryIndex))
return false;
numDescriptorsLoaded++;
return _contextDescriptorMatchesMangling(
(const TypeContextDescriptor *)descriptor, node);
};
// Perform the lookup.
auto isNull = [](auto pointers) { return *pointers.first == nullptr; };
auto found = descriptorMap->find(key.data(), key.size(), isMatch, isNull);
if (SWIFT_UNLIKELY(prespecializedLoggingEnabled)) {
auto startPointers = descriptorMap->find(key.data(), key.size(), [](auto ignore){ return true; }, [](auto ignore){ return true; });
LOG("Hash table lookup checked %u loaded entries, %u total entries, starting data pointer %p, starting auxiliary pointer %p.",
numDescriptorsLoaded, numDescriptorsChecked, startPointers.first, startPointers.second);
}
// The pointers in `found` are pointers to the map entries, and should always
// be non-NULL. The only condition that returns NULL is if the map has no
// entries where `isMatch` or `isNull` return true, and the map should always
// have at least one NULL entry.
assert(found.first);
if (!found.first) {
LOG("Descriptor table lookup of '%.*s' returned NULL pointer to descriptor "
"pointer.",
(int)key.size(), key.data());
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
}
auto *foundDescriptor = *found.first;
if (!foundDescriptor) {
LOG("Did not find descriptor for key '%.*s'.", (int)key.size(), key.data());
// This result is definitive if the descriptor map is comprehensive. If the
// map is not comprehensive, return NonDefinitiveNotFound to tell the caller
// that it needs to perform a full search.
if (data->getOptionFlags() &
LibPrespecializedData<
InProcess>::OptionFlagDescriptorMapNotComprehensive)
return {LibPrespecializedLookupResult::NonDefinitiveNotFound, nullptr};
return {LibPrespecializedLookupResult::DefinitiveNotFound, nullptr};
}
LOG("Found descriptor %p for key '%.*s'.", foundDescriptor, (int)key.size(),
key.data());
return {LibPrespecializedLookupResult::Found,
(const TypeContextDescriptor *)foundDescriptor};
}
void _swift_validatePrespecializedMetadata(unsigned *outValidated,
unsigned *outFailed) {
if (outValidated)
*outValidated = 0;
if (outFailed)
*outFailed = 0;
auto *data = LibPrespecialized.get().data;
if (!data) {
return;
}
LibPrespecialized.get().mapConfiguration.store(
LibPrespecializedState::MapConfiguration::Disabled,
std::memory_order_release);
auto *metadataMap = data->getMetadataMap();
auto metadataMapSize = metadataMap->arraySize;
auto *array = metadataMap->array();
for (uint64_t i = 0; i < metadataMapSize; i++) {
auto &element = array[i];
if (!element.key || !element.value)
continue;
if (outValidated)
(*outValidated)++;
const char *mangledName = element.key;
// Skip the leading $.
if (mangledName[0] == '$')
mangledName++;
auto result = swift_getTypeByMangledName(MetadataState::Complete,
mangledName, nullptr, {}, {});
if (auto *error = result.getError()) {
fprintf(stderr,
"Prespecializations library validation: unable to build metadata "
"for mangled name '%s'\n",
mangledName);
if (outFailed)
(*outFailed)++;
}
if (!compareGenericMetadata(result.getType().getMetadata(), element.value))
if (outFailed)
(*outFailed)++;
}
}
|