forked from apple/swift
/
ImageInspection.swift
175 lines (140 loc) · 5.18 KB
/
ImageInspection.swift
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import Swift
import _SwiftRuntimeShims
#if !(os(macOS) || os(iOS) || os(tvOS) || os(watchOS))
import SwiftShims
#endif
@available(SwiftStdlib 5.9, *)
@frozen
public enum ImageInspection {}
//===----------------------------------------------------------------------===//
// Mach-O Image Lookup
//===----------------------------------------------------------------------===//
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
@_silgen_name("initializeDyldLookup")
func initializeDyldLookup()
#endif
//===----------------------------------------------------------------------===//
// ELF and COFF Image Lookup
//===----------------------------------------------------------------------===//
#if !(os(macOS) || os(iOS) || os(tvOS) || os(watchOS))
func enumerateSections<K: Hashable, V>(
_ body: (MetadataSections) -> (
MetadataSectionRange,
(UnsafeRawPointer, Int) -> ()
)
) {
swift_enumerateAllMetadataSections({ sections, _ in
guard let sections = sections else {
return true
}
let metadataSections = sections.assumingMemoryBound(
to: MetadataSections.self
)
guard metadataSections.version >= 3 else {
return false
}
let (range, register) = body(metadataSections)
register(range.start, range.length)
return true
}, nil)
}
#endif
//===----------------------------------------------------------------------===//
// Runtime Attributes
//===----------------------------------------------------------------------===//
@available(SwiftStdlib 5.9, *)
struct AttributeCache {
#if !(os(macOS) || os(iOS) || os(tvOS) || os(watchOS))
var imageCount = 0
#endif
var map: [ContextDescriptor: [UnsafeRawPointer]] = [:]
}
@available(SwiftStdlib 5.9, *)
var attributeCache: Lock<AttributeCache> = .create(with: .init())
@available(SwiftStdlib 5.9, *)
extension ImageInspection {
@available(SwiftStdlib 5.9, *)
public static func withAttributeCache<T>(
_ body: @Sendable ([ContextDescriptor: [UnsafeRawPointer]]) throws -> T
) rethrows -> T {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
initializeDyldLookup()
return try attributeCache.withLock {
try body($0.map)
}
#else
return try attributeCache.withLock {
// If our cached image count is less than what's being reported by the
// Swift runtime, then we need to reset our cache and enumerate all of
// the images again for updated attributes.
let currentImageCount = swift_getMetadataSectionCount()
if $0.imageCount < currentImageCount {
$0.map.removeAll(keepingCapacity: true)
// We enumerate inside the lock because we need to ensure that the
// thread who gets this lock first is able to at least initialize the
// cache before other threads.
enumerateSections {
($0.swift5_runtime_attributes, registerAttributes(_:_:))
}
$0.imageCount = currentImageCount
}
return try body($0.map)
}
#endif
}
}
@available(SwiftStdlib 5.9, *)
@_cdecl("registerAttributes")
func registerAttributes(_ section: UnsafeRawPointer, _ size: Int) {
var address = section
let end = address + size
while address < end {
// Flags (always 0 for right now)
address += MemoryLayout<Int32>.size
let attributeAddr = address.relativeIndirectableAddress(
as: ContextDescriptor.self
)
let attributeDescriptor = ContextDescriptor(attributeAddr)
// Attribute Context Descriptor
address += MemoryLayout<Int32>.size
let numberOfInstances = address.loadUnaligned(as: UInt32.self)
// Number of records this attribute has
address += MemoryLayout<Int32>.size
var fnPtrs: [UnsafeRawPointer] = []
fnPtrs.reserveCapacity(Int(numberOfInstances))
for _ in 0 ..< numberOfInstances {
// The type this attribute was on (not always an actual type)
address += MemoryLayout<Int32>.size
var fnRecord = address.relativeDirectAddress(as: UnsafeRawPointer.self)
fnRecord += MemoryLayout<RelativeDirectPointer<Void>>.size * 3
let fnPtr = fnRecord.relativeDirectAddress(as: UnsafeRawPointer.self)
fnPtrs.append(fnPtr)
// Function pointer to attribute initializer
address += MemoryLayout<Int32>.size
}
let copyFnPtrs = fnPtrs
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
attributeCache.withLock {
$0.map[attributeDescriptor, default: []].append(contentsOf: copyFnPtrs)
}
#else
// For non-Darwin platforms, we have to iterate the images and register
// the attributes inside of the attribute cache. So, we're already in the
// lock by the time this function gets called, so it is safe to access the
// value in our lock without locking.
attributeCache.withUnsafeValue {
$0.map[attributeDescriptor, default: []].append(contentsOf: copyFnPtrs)
}
#endif
}
}