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
|
//===---- AccessibleFunction.cpp - Swift protocol conformance checking ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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
//
//===----------------------------------------------------------------------===//
//
// Checking and caching of Swift accessible functions.
//
//===----------------------------------------------------------------------===//
#include "ImageInspection.h"
#include "Private.h"
#include "swift/Basic/Lazy.h"
#include "swift/Demangling/Demangler.h"
#include "swift/Runtime/AccessibleFunction.h"
#include "swift/Runtime/Concurrent.h"
#include "swift/Runtime/Metadata.h"
#include "Tracing.h"
#include <cstdint>
#include <new>
using namespace swift;
#pragma mark Accessible function cache
namespace {
struct AccessibleFunctionsSection {
const AccessibleFunctionRecord *__ptrauth_swift_accessible_function_record
Begin;
const AccessibleFunctionRecord *__ptrauth_swift_accessible_function_record
End;
AccessibleFunctionsSection(const AccessibleFunctionRecord *begin,
const AccessibleFunctionRecord *end)
: Begin(begin), End(end) {}
AccessibleFunctionsSection(const void *ptr, uintptr_t size) {
auto bytes = reinterpret_cast<const char *>(ptr);
Begin = reinterpret_cast<const AccessibleFunctionRecord *>(ptr);
End = reinterpret_cast<const AccessibleFunctionRecord *>(bytes + size);
}
const AccessibleFunctionRecord *begin() const { return Begin; }
const AccessibleFunctionRecord *end() const { return End; }
};
struct AccessibleFunctionCacheEntry {
private:
const char *Name;
size_t NameLength;
const AccessibleFunctionRecord *__ptrauth_swift_accessible_function_record R;
public:
AccessibleFunctionCacheEntry(llvm::StringRef name,
const AccessibleFunctionRecord *record)
: R(record) {
char *Name = reinterpret_cast<char *>(malloc(name.size()));
memcpy(Name, name.data(), name.size());
this->Name = Name;
this->NameLength = name.size();
}
const AccessibleFunctionRecord *getRecord() const { return R; }
bool matchesKey(llvm::StringRef name) {
return name == llvm::StringRef{Name, NameLength};
}
friend llvm::hash_code hash_value(const AccessibleFunctionCacheEntry &value) {
return hash_value(llvm::StringRef{value.Name, value.NameLength});
}
template <class... T>
static size_t getExtraAllocationSize(T &&...ignored) {
return 0;
}
};
struct AccessibleFunctionsState {
ConcurrentReadableHashMap<AccessibleFunctionCacheEntry> Cache;
ConcurrentReadableArray<AccessibleFunctionsSection> SectionsToScan;
AccessibleFunctionsState() {
initializeAccessibleFunctionsLookup();
}
};
static Lazy<AccessibleFunctionsState> Functions;
} // end anonymous namespace
static void _registerAccessibleFunctions(AccessibleFunctionsState &C,
AccessibleFunctionsSection section) {
C.SectionsToScan.push_back(section);
}
void swift::addImageAccessibleFunctionsBlockCallbackUnsafe(
const void *baseAddress, const void *functions, uintptr_t size) {
assert(
size % sizeof(AccessibleFunctionRecord) == 0 &&
"accessible function section not a multiple of AccessibleFunctionRecord");
auto &C = Functions.unsafeGetAlreadyInitialized();
_registerAccessibleFunctions(C, AccessibleFunctionsSection{functions, size});
}
void swift::addImageAccessibleFunctionsBlockCallback(
const void *baseAddress, const void *functions, uintptr_t size) {
Functions.get();
addImageAccessibleFunctionsBlockCallbackUnsafe(baseAddress, functions, size);
}
// TODO(distributed): expose dumping records via a flag
LLVM_ATTRIBUTE_UNUSED
static void _dumpAccessibleFunctionRecords() {
auto &S = Functions.get();
fprintf(stderr, "==== Accessible Function Records ====\n");
int count = 0;
for (const auto §ion : S.SectionsToScan.snapshot()) {
for (auto &record : section) {
auto recordName =
swift::Demangle::makeSymbolicMangledNameStringRef(record.Name.get());
fprintf(stderr, "Record name: %s\n", recordName.data());
fprintf(stderr, " Function Ptr: %p\n", record.Function.get());
fprintf(stderr, " Flags.IsDistributed: %d\n", record.Flags.isDistributed());
++count;
}
}
fprintf(stderr, "Record count: %d\n", count);
fprintf(stderr, "==== End of Accessible Function Records ====\n");
}
static const AccessibleFunctionRecord *
_searchForFunctionRecord(AccessibleFunctionsState &S, llvm::StringRef name) {
auto traceState = runtime::trace::accessible_function_scan_begin(name);
for (const auto §ion : S.SectionsToScan.snapshot()) {
for (auto &record : section) {
auto recordName =
swift::Demangle::makeSymbolicMangledNameStringRef(record.Name.get());
if (recordName == name)
return traceState.end(&record);
}
}
return nullptr;
}
SWIFT_RUNTIME_STDLIB_SPI
const AccessibleFunctionRecord *
swift::runtime::swift_findAccessibleFunction(const char *targetNameStart,
size_t targetNameLength) {
auto &S = Functions.get();
llvm::StringRef name{targetNameStart, targetNameLength};
// Look for an existing entry.
{
auto snapshot = S.Cache.snapshot();
if (auto E = snapshot.find(name))
return E->getRecord();
}
// If entry doesn't exist (either record doesn't exist, hasn't been loaded, or
// requested yet), let's try to find it and add to the cache.
auto *record = _searchForFunctionRecord(S, name);
if (record) {
S.Cache.getOrInsert(
name, [&](AccessibleFunctionCacheEntry *entry, bool created) {
if (created)
::new (entry) AccessibleFunctionCacheEntry{name, record};
return true;
});
}
return record;
}
|