|
| 1 | +//===---- AccessibleFunction.cpp - Swift protocol conformance checking ----===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Checking and caching of Swift accessible functions. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "ImageInspection.h" |
| 18 | +#include "Private.h" |
| 19 | +#include "swift/Basic/Lazy.h" |
| 20 | +#include "swift/Demangling/Demangler.h" |
| 21 | +#include "swift/Runtime/Concurrent.h" |
| 22 | +#include "swift/Runtime/Metadata.h" |
| 23 | +#include "Private.h" |
| 24 | + |
| 25 | +#include <cstdint> |
| 26 | + |
| 27 | +using namespace swift; |
| 28 | + |
| 29 | +#pragma mark Accessible function cache |
| 30 | +namespace { |
| 31 | + |
| 32 | +struct AccessibleFunctionsSection { |
| 33 | + const AccessibleFunctionRecord *Begin, *End; |
| 34 | + |
| 35 | + AccessibleFunctionsSection(const AccessibleFunctionRecord *begin, |
| 36 | + const AccessibleFunctionRecord *end) |
| 37 | + : Begin(begin), End(end) {} |
| 38 | + |
| 39 | + AccessibleFunctionsSection(const void *ptr, uintptr_t size) { |
| 40 | + auto bytes = reinterpret_cast<const char *>(ptr); |
| 41 | + Begin = reinterpret_cast<const AccessibleFunctionRecord *>(ptr); |
| 42 | + End = reinterpret_cast<const AccessibleFunctionRecord *>(bytes + size); |
| 43 | + } |
| 44 | + |
| 45 | + const AccessibleFunctionRecord *begin() const { return Begin; } |
| 46 | + const AccessibleFunctionRecord *end() const { return End; } |
| 47 | +}; |
| 48 | + |
| 49 | +struct AccessibleFunctionsState { |
| 50 | + ConcurrentReadableArray<AccessibleFunctionsSection> SectionsToScan; |
| 51 | + |
| 52 | + AccessibleFunctionsState() { |
| 53 | + initializeAccessibleFunctionsLookup(); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +static Lazy<AccessibleFunctionsState> Functions; |
| 58 | + |
| 59 | +} // end anonymous namespace |
| 60 | + |
| 61 | +static void _registerAccessibleFunctions(AccessibleFunctionsState &C, |
| 62 | + AccessibleFunctionsSection section) { |
| 63 | + C.SectionsToScan.push_back(section); |
| 64 | +} |
| 65 | + |
| 66 | +void swift::addImageAccessibleFunctionsBlockCallbackUnsafe(const void *functions, |
| 67 | + uintptr_t size) { |
| 68 | + assert( |
| 69 | + size % sizeof(AccessibleFunctionRecord) == 0 && |
| 70 | + "accessible function section not a multiple of AccessibleFunctionRecord"); |
| 71 | + |
| 72 | + auto &C = Functions.unsafeGetAlreadyInitialized(); |
| 73 | + _registerAccessibleFunctions(C, AccessibleFunctionsSection{functions, size}); |
| 74 | +} |
| 75 | + |
| 76 | +void swift::addImageAccessibleFunctionsBlockCallback(const void *functions, |
| 77 | + uintptr_t size) { |
| 78 | + Functions.get(); |
| 79 | + addImageAccessibleFunctionsBlockCallbackUnsafe(functions, size); |
| 80 | +} |
0 commit comments