Skip to content

Commit 3f4c06e

Browse files
committed
[Distributed] Runtime: Add skeleton support for accessible function sections
1 parent be08a8b commit 3f4c06e

File tree

9 files changed

+135
-2
lines changed

9 files changed

+135
-2
lines changed

include/swift/ABI/Metadata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,6 +5059,8 @@ struct TargetAccessibleFunctionRecord final {
50595059
AccessibleFunctionFlags Flags;
50605060
};
50615061

5062+
using AccessibleFunctionRecord = TargetAccessibleFunctionRecord<InProcess>;
5063+
50625064
} // end namespace swift
50635065

50645066
#pragma clang diagnostic pop

stdlib/public/SwiftShims/MetadataSections.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct MetadataSections {
6464
MetadataSectionRange swift5_replac2;
6565
MetadataSectionRange swift5_builtin;
6666
MetadataSectionRange swift5_capture;
67+
MetadataSectionRange swift5_accessible_functions;
6768
};
6869

6970
#ifdef __cplusplus
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

stdlib/public/runtime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ set(swift_runtime_sources
6868
ReflectionMirror.cpp
6969
RuntimeInvocationsTracking.cpp
7070
SwiftDtoa.cpp
71-
SwiftTLSContext.cpp)
71+
SwiftTLSContext.cpp
72+
AccessibleFunction.cpp)
7273

7374
# Acknowledge that the following sources are known.
7475
set(LLVM_OPTIONAL_SOURCES

stdlib/public/runtime/ImageInspection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ void initializeTypeMetadataRecordLookup();
7373
/// Load the metadata from the image necessary to perform dynamic replacements.
7474
void initializeDynamicReplacementLookup();
7575

76+
/// Load the metadata from the image necessary to find functions by name.
77+
void initializeAccessibleFunctionsLookup();
78+
7679
// Callbacks to register metadata from an image to the runtime.
7780
void addImageProtocolsBlockCallback(const void *start, uintptr_t size);
7881
void addImageProtocolsBlockCallbackUnsafe(const void *start, uintptr_t size);
@@ -87,6 +90,10 @@ void addImageTypeMetadataRecordBlockCallbackUnsafe(const void *start,
8790
void addImageDynamicReplacementBlockCallback(const void *start, uintptr_t size,
8891
const void *start2,
8992
uintptr_t size2);
93+
void addImageAccessibleFunctionsBlockCallback(const void *start,
94+
uintptr_t size);
95+
void addImageAccessibleFunctionsBlockCallbackUnsafe(const void *start,
96+
uintptr_t size);
9097

9198
int lookupSymbol(const void *address, SymbolInfo *info);
9299

stdlib/public/runtime/ImageInspectionCommon.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ void swift_addNewDSOImage(const void *addr) {
8181
replacements, dynamic_replacements.length, replacements_some,
8282
dynamic_replacements_some.length);
8383
}
84+
85+
const auto &accessible_funcs_section = sections->swift5_accessible_functions;
86+
const void *functions =
87+
reinterpret_cast<void *>(accessible_funcs_section.start);
88+
if (accessible_funcs_section.length)
89+
swift::addImageAccessibleFunctionsBlockCallback(
90+
functions, accessible_funcs_section.length);
8491
}
8592

8693
void swift::initializeProtocolLookup() {
@@ -131,6 +138,21 @@ void swift::initializeTypeMetadataRecordLookup() {
131138
void swift::initializeDynamicReplacementLookup() {
132139
}
133140

141+
void swift::initializeAccessibleFunctionsLookup() {
142+
const swift::MetadataSections *sections = registered;
143+
while (true) {
144+
const swift::MetadataSectionRange &functions =
145+
sections->swift5_accessible_functions;
146+
if (functions.length)
147+
addImageAccessibleFunctionsBlockCallbackUnsafe(
148+
reinterpret_cast<void *>(functions.start), functions.length);
149+
150+
if (sections->next == registered)
151+
break;
152+
sections = sections->next;
153+
}
154+
}
155+
134156
#ifndef NDEBUG
135157

136158
SWIFT_RUNTIME_EXPORT
@@ -177,4 +199,4 @@ size_t swift_getMetadataSectionCount() {
177199

178200
#endif // !defined(__MACH__)
179201

180-
#endif // SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H
202+
#endif // SWIFT_RUNTIME_IMAGEINSPECTIONCOMMON_H

stdlib/public/runtime/ImageInspectionCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
/// This lives within SEG_TEXT.
3535
#define MachODynamicReplacementSection "__swift5_replace"
3636
#define MachODynamicReplacementSomeSection "__swift5_replac2"
37+
/// The Mach-O section name for the section containing accessible functions.
38+
/// This lives within SEG_TEXT.
39+
#define MachOAccessibleFunctionsSection "__swift5_acfuncs"
3740

3841
#define MachOTextSegment "__TEXT"
3942

stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ constexpr const char DynamicReplacementSection[] =
4343
MachODynamicReplacementSection;
4444
constexpr const char DynamicReplacementSomeSection[] =
4545
MachODynamicReplacementSomeSection;
46+
constexpr const char AccessibleFunctionsSection[] =
47+
MachOAccessibleFunctionsSection;
4648
constexpr const char TextSegment[] = MachOTextSegment;
4749

4850
#if __POINTER_WIDTH__ == 64
@@ -159,6 +161,12 @@ void swift::initializeDynamicReplacementLookup() {
159161
addImageDynamicReplacementBlockCallback>);
160162
}
161163

164+
void swift::initializeAccessibleFunctionsLookup() {
165+
REGISTER_FUNC(
166+
addImageCallback<TextSegment, AccessibleFunctionsSection,
167+
addImageAccessibleFunctionsBlockCallbackUnsafe>);
168+
}
169+
162170
#if SWIFT_STDLIB_HAS_DLADDR
163171
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
164172
Dl_info dlinfo;

stdlib/public/runtime/ImageInspectionStatic.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,14 @@ void swift::initializeDynamicReplacementLookup() {
7474
return;
7575
addImageDynamicReplacementBlockCallback(start1, size1, start2, size2);
7676
}
77+
void swift::initializeAccessibleFunctionsLookup() {
78+
void *start;
79+
uintptr_t size;
80+
GET_SECTION_START_AND_SIZE(start, size, MachOTextSegment,
81+
MachOAccessibleFunctionsSection);
82+
if (start == nullptr || size == 0)
83+
return;
84+
addImageAccessibleFunctionBlockCallbackUnsafe(start, size);
85+
}
7786

7887
#endif // defined(__MACH__) && defined(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION)

0 commit comments

Comments
 (0)