Skip to content

Commit 69af621

Browse files
committed
proposal for exposing demangling
1 parent 99c18ea commit 69af621

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

proposals/NNNN-runtime-demangle.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Expose demangle function in Runtime module
2+
3+
* Proposal: [SE-NNNN](0496-runtime-demangle.md)
4+
* Authors: [Konrad'ktoso'Malawski](https://github.com/ktoso), [Alejandro Alonso](https://github.com/Azoy)
5+
* Review Manager: TODO
6+
* Status: TODO
7+
* Implementation:
8+
* Review:
9+
* Previous [pitch](https://forums.swift.org/t/demangle-function/25416/16)
10+
11+
## Introduction
12+
13+
Swift symbols are subject to name mangling. These mangled names then show up in backtraces and other profiling tools. Mangled names may look something like this `$sSS7cStringSSSPys4Int8VG_tcfC` and often end up visible to developers, unless they are demangled before displaying.
14+
15+
In manu situations, it is much preferable to demangle the identifiers before displaying them. For example, the previously shown identifier would can be demangled as `Swift.String.init(cString: Swift.UnsafePointer<Swift.Int8>) -> Swift.String`, which is a nice human-readable format, that a Swift developer can easily understand.
16+
17+
This proposal introduces a new API that allows calling out to the Swift runtime's demangler, without leaving the process.
18+
19+
## Motivation
20+
21+
Currently, many tools that need to display symbol names to developers are forced to create a process and execute the `swift-demangle` tool, or use unofficial runtime APIs to invoke the runtime's demangler.
22+
23+
Neither of these approaches are satisfactionary, because either we are paying a high cost for creating processes, or we're relying on unofficial APIs.
24+
25+
This proposal introduces an official `demangle(:String) -> String?` function that offers a maintained and safe way to call the Swift demangled from a running Swift application.
26+
27+
## Proposed solution
28+
29+
We propose to introduce two `demangle` functions in the `Runtime` module:
30+
31+
A simple demangle method, returning an optional `String`:
32+
33+
```swift
34+
public func demangle(_ mangledName: String) -> String?
35+
```
36+
37+
And an overload which accepts a pre-allocated buffer into which the demangled string can be written:
38+
39+
```swift
40+
@discardableResult
41+
public func demangle(
42+
_ input: String,
43+
into buffer: UnsafeMutableBufferPointer<Int8>
44+
) -> DemanglingResult
45+
46+
public enum DemanglingResult: Equatable {
47+
case success
48+
case failed
49+
case truncated(Int)
50+
}
51+
```
52+
53+
The buffer accepting API is necessary for performance sensitive use-cases, which attempt to demangle symbols in process, before displaying or sending them for further processing. In those use-cases it is common to have a known maximum buffer size into which we are willing to write the demangled representation.
54+
55+
If the demangled representation does not fit the preallocated buffer, the demangle method will return `truncated(actualSize)` such that developers can determine by how much the buffer might need to be increased to handle the complete demangling.
56+
57+
### Demangling format
58+
59+
While the mangled strings are part of Swift ABI and can therefore not really change on platforms with stable ABI, the demangled representation returned by the `demangle` functions is _not guaranteed to be stable in any way_.
60+
61+
The demangled representation may change without any warning, during even patch releases of Swift. The returned strings should be treated mostly as nicer to present to developers human readable representations, and it is not a goal to provide any form of guarantee about the exact shape of these.
62+
63+
## Source compatibility
64+
65+
This proposal is purely additive.
66+
67+
## ABI compatibility
68+
69+
This proposal is purely additive.
70+
71+
## Implications on adoption
72+
73+
The runtime demangling func becoming an official entry point will help prevent libraries call swift internals.
74+
75+
## Future directions
76+
77+
## Alternatives considered
78+
79+
### Do nothing
80+
81+
Not exposing this demangling capabilities officially, would result in tools authors continuing to use
82+
unofficial ways to get to this API.
83+
84+
It also means that further locking down access to `swift_` APIs may be difficult,
85+
as they are crucial and load bearing for some tools (such as *continuous profiling*).
86+
E.g. recent versions of Swift issue the following warning when accessing some of its `swift_` namespaced runtime functions:
87+
88+
> symbol name 'swift_...' is reserved for the Swift runtime and cannot be directly referenced without causing unpredictable behavior; this will become an error
89+
90+
So if we wanted to further lock down such uses, including `swift_demangle`, it would be preferable to offer an official solution instead.
91+
92+
### Expose from Swift module
93+
94+
Previously, this was considered to expose from just the `Swift` module, however the `Runtime`
95+
module is much more aligned with the use and utility of this function.
96+
97+
Demangling is already used by the `Backtrace` type which is located in the Runtime module,
98+
so the demangling functions should be exposed from the same place.
99+
100+
## Acknowledgments
101+
102+
Thanks to [Alejandro Alonso](https://github.com/Azoy), who did an initial version of this pitch many years ago, and this proposal is heavily based on his initial pitch.

0 commit comments

Comments
 (0)