Skip to content

Commit 26d0e49

Browse files
committed
[Backtracing] Updates following review.
Added some explanatory text to the Compact Backtrace Format documentation, and also to the `CMakeLists.txt` for the runtime. Tweaked the conversions for `Backtrace.Address` to truncate, which should result in reasonable behaviour for negative fixed-width integers. Use a constant for the ASCII values for `/` and `\` in the Compact Image Format encoder/decoder. Make the TSD key for `ElfImageCache` non-optional, and call `fatalError()` if we don't get one. rdar://124913332
1 parent c9c5dc0 commit 26d0e49

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

docs/CompactBacktraceFormat.md

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ The following instructions are currently defined
5757
| `1000xxxx` | `rep` | Repeat the previous frame |
5858
| `1xxxxxxx` | reserved | Reserved for future expansion |
5959

60+
If the bit labelled `a` is set, it means that the address computation
61+
is absolute rather than being relative to the previously computed
62+
address.
63+
6064
### `end`/`trunc`
6165

6266
#### Encoding

stdlib/public/RuntimeModule/Address.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ extension Backtrace.Address {
148148
return T(0)
149149
case let .sixteenBit(addr):
150150
guard T.bitWidth >= 16 else { return nil }
151-
return T(addr)
151+
return T(truncatingIfNeeded: addr)
152152
case let .thirtyTwoBit(addr):
153153
guard T.bitWidth >= 32 else { return nil }
154-
return T(addr)
154+
return T(truncatingIfNeeded: addr)
155155
case let .sixtyFourBit(addr):
156156
guard T.bitWidth >= 64 else { return nil }
157-
return T(addr)
157+
return T(truncatingIfNeeded: addr)
158158
}
159159
}
160160
}
@@ -204,11 +204,11 @@ extension Backtrace.Address {
204204
public init?<T: FixedWidthInteger>(_ value: T) {
205205
switch T.bitWidth {
206206
case 16:
207-
self.init(UInt16(value))
207+
self.init(UInt16(truncatingIfNeeded: value))
208208
case 32:
209-
self.init(UInt32(value))
209+
self.init(UInt32(truncatingIfNeeded: value))
210210
case 64:
211-
self.init(UInt64(value))
211+
self.init(UInt64(truncatingIfNeeded: value))
212212
default:
213213
return nil
214214
}

stdlib/public/RuntimeModule/CompactImageMap.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
import Swift
1818

19+
private let slash = UInt8(ascii: "/")
20+
private let backslash = UInt8(ascii: "\\")
21+
1922
@_spi(Internal)
2023
public enum CompactImageMapFormat {
2124

@@ -58,7 +61,7 @@ public enum CompactImageMapFormat {
5861
while pos < end {
5962
let ch = str[pos]
6063

61-
if pos > base && (ch == 0x2f || ch == 0x5c) {
64+
if pos > base && (ch == slash || ch == backslash) {
6265
let range = base..<pos
6366
let prefix = String(str[range])!
6467
body(prefix)
@@ -179,7 +182,8 @@ public enum CompactImageMapFormat {
179182
guard let char = iterator.next() else {
180183
return nil
181184
}
182-
if base + n > stringBase! && (char == 0x2f || char == 0x5c) {
185+
if base + n > stringBase! && (char == slash
186+
|| char == backslash) {
183187
let prefix = String(decoding: resultBytes[stringBase!..<base+n],
184188
as: UTF8.self)
185189
#if DEBUG_COMPACT_IMAGE_MAP
@@ -226,11 +230,11 @@ public enum CompactImageMapFormat {
226230
print("framewk version='\(versionChar)' name='\(name)'")
227231
#endif
228232

229-
resultBytes.append(0x2f) // '/'
233+
resultBytes.append(slash)
230234
resultBytes.append(contentsOf: nameBytes)
231235
resultBytes.append(contentsOf: ".framework/Versions/".utf8)
232236
resultBytes.append(version)
233-
resultBytes.append(0x2f)
237+
resultBytes.append(slash)
234238
resultBytes.append(contentsOf: nameBytes)
235239

236240
return String(decoding: resultBytes, as: UTF8.self)
@@ -383,7 +387,7 @@ public enum CompactImageMapFormat {
383387
// Extract the name from the path
384388
if let path = path {
385389
if let lastSlashNdx = path.utf8.lastIndex(
386-
where: { $0 == 0x2f || $0 == 0x5c }
390+
where: { $0 == slash || $0 == backslash }
387391
) {
388392
let nameNdx = path.index(after: lastSlashNdx)
389393

@@ -726,7 +730,7 @@ public enum CompactImageMapFormat {
726730
version = remainingPath[verNdx]
727731

728732
let slashNdx = remainingPath.index(after: verNdx)
729-
if remainingPath[slashNdx] == 0x2f {
733+
if remainingPath[slashNdx] == slash {
730734
let nameNdx = remainingPath.index(after: slashNdx)
731735
if remainingPath[nameNdx...].elementsEqual(name.utf8) {
732736
self.remainingPath = remainingPath[nameNdx...]

stdlib/public/RuntimeModule/ElfImageCache.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ final class ElfImageCache {
6666
return nil
6767
}
6868

69-
private static var key: pthread_key_t? = {
69+
private static var key: pthread_key_t = {
7070
var theKey = pthread_key_t()
7171
let err = pthread_key_create(
7272
&theKey,
@@ -78,15 +78,15 @@ final class ElfImageCache {
7878
}
7979
)
8080
if err != 0 {
81-
return nil
81+
fatalError("Unable to create TSD key for ElfImageCache")
8282
}
8383
return theKey
8484
}()
8585

8686
static var threadLocal: ElfImageCache {
87-
guard let rawPtr = pthread_getspecific(key!) else {
87+
guard let rawPtr = pthread_getspecific(key) else {
8888
let cache = Unmanaged<ElfImageCache>.passRetained(ElfImageCache())
89-
pthread_setspecific(key!, cache.toOpaque())
89+
pthread_setspecific(key, cache.toOpaque())
9090
return cache.takeUnretainedValue()
9191
}
9292
let cache = Unmanaged<ElfImageCache>.fromOpaque(rawPtr)

stdlib/public/runtime/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ foreach(sdk ${SWIFT_SDKS})
144144
endif()
145145
endforeach()
146146

147+
# In modern CMake, we would be able to use the previous name `swiftRuntime`
148+
# without clashing with the `Runtime` module, but the current build system
149+
# is architected in such a way that we had to rename this to `swiftRuntimeCore`
150+
# in order to avoid a clash with the new Swift module.
147151
add_swift_target_library(swiftRuntimeCore OBJECT_LIBRARY
148152
${swift_runtime_sources}
149153
${swift_runtime_objc_sources}
@@ -323,4 +327,3 @@ if(static_binary_lnk_file_list)
323327
add_dependencies(stdlib ${static_binary_lnk_file_list})
324328
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
325329
endif()
326-

0 commit comments

Comments
 (0)