Commit Graph

3219 Commits

Author SHA1 Message Date
Alastair Houghton
e38c0652b6 Merge pull request #84906 from al45tair/eng/PR-101623384
[Backtracing] Add initial support for Windows.
2026-02-06 14:53:16 +00:00
Kavon Farvardin
ae77ba81e3 Merge pull request #87013 from kavon/revert-84789
Back out #84789 after additional testing
2026-02-06 02:58:00 -08:00
Alastair Houghton
ceaba82d6d [Backtracing] Updates after review.
Tweaked the comment in `Runtime/Config.h`.

Fixed a couple of incorrect ARM64 instruction mnemonics.  This still needs
testing on ARM64 Windows.

Fixed an out-of-date comment in `swift-backtrace`.

Use a macro in `Backtrace.cpp` to guarantee we don't overrun the buffer,
and in the process simplify the code slightly.

rdar://101623384
2026-02-06 08:53:16 +00:00
Alastair Houghton
d9b0031f09 [Backtracing] Build and test fixes.
Tweak things to build on Linux and macOS again.

Also fix a few things in the tests.

rdar://101623384
2026-02-06 08:53:15 +00:00
Jonathan Grynspan
83d82a5af5 swift_slowAlloc() assumes malloc(0) returns non-NULL. (#86922)
`swift_slowAlloc()` and related functions assume that `malloc(0)` and `aligned_alloc(0)` return non-`NULL` pointers. The C standards allow implementations to return `NULL` when the allocation size is `0`.

This PR implements a check for `0` that instead allocates `1`. The cost of the check is negligible next to the cost of actually allocating, but we'll mark it `SWIFT_UNLIKELY` just in case.

Resolves rdar://169304909.
2026-02-05 21:44:47 -08:00
Kavon Farvardin
d5f9a1eeeb Revert "Merge pull request #84789 from nickolas-pohilets/mpokhylets/fix-82618"
This reverts commit b633bd37ac, reversing
changes made to b27bb64b03.
2026-02-05 11:34:59 -08:00
Alastair Houghton
0682923f45 [Backtracing] Make the debug option work on Windows.
Hitting `D` when in the backtracer should do the same on Windows
that it does on macOS, namely launching `lldb` and attaching it to
the crashed program.

rdar://101623384
2026-02-03 18:23:30 +00:00
Alastair Houghton
a677131c96 [Backtracing] Tweak a few things after rebasing.
Fix a couple of issues with the rebase.

Also, add code to handle the debug exceptions (which we don't want
to crash the process).

rdar://101623384
2026-02-03 18:23:29 +00:00
Alastair Houghton
6d930d39ec [Backtracing] Make tests work, plus various fixes.
Made the tests run, then fixed various issues with them.

rdar://101623384
2026-02-03 18:23:29 +00:00
Alastair Houghton
e0845e1483 [Backtracing] Made on-crash backtraces work for 64-bit.
On-crash backtracing is basically there for 64-bit Windows.  It
won't work on 32-bit because of a Swift compiler issue, and there
is a little more work to do yet, but it is now working!

rdar://101623384
2026-02-03 18:23:29 +00:00
Alastair Houghton
d13988bd5f [Backtracing][Windows] Initial Windows support.
This doesn't have a working symbolicator yet, but it does build and
it can obtain a basic backtrace.

It also doesn't include a working `swift-backtrace` program yet.

rdar://101623384
2026-02-03 18:23:29 +00:00
Joe Groff
8f14c35630 Runtime: Propagate addressable-for-dependencies bit in generic and resilient type layout.
Now that the bit controls `Borrow`'s layout, it must be set properly for runtime-instantiated types.
2026-02-02 18:43:46 -08:00
Joe Groff
0f3ddfbcc8 Merge pull request #86545 from jckarter/builtin-borrow
`Builtin.Borrow` implementation
2026-01-26 07:32:31 -08:00
Alejandro Alonso
02d0bdfbcd Implement extra inhabitant counts for borrows 2026-01-23 08:02:10 -08:00
Alejandro Alonso
f31597d185 Fill out some more generic value witness table entries 2026-01-23 08:02:10 -08:00
Alejandro Alonso
632d6f4257 WIP: Dependent borrow layout 2026-01-23 08:02:10 -08:00
Joe Groff
bc166d5a8c Add a Builtin.Borrow type.
This will represent the layout of a borrow of a value.
2026-01-23 07:46:50 -08:00
Mike Ash
816edbde13 [Runtime] Remove clang:: from clang::no_unique_address.
Clang only recognizes [[no_unique_address]] or [[msvc::no_unique_address]] depending on the target.
2026-01-22 15:35:19 -05:00
Alastair Houghton
e8077b9a0e Merge pull request #86618 from AZero13/swift
Use strict class check for _SwiftValue box
2026-01-22 15:37:29 +00:00
Mykola Pokhylets
e9ddb3f945 Fixed overly optimistic optimization of checked cast into unchecked one for function types 2026-01-20 14:49:56 +01:00
AZero13
8ffaa0b47c Use strict class check for _SwiftValue box
Replaces isKindOfClass with object_getClass to ensure 'other' is exactly a _SwiftValue box, not a subclass. This works because other has no subclasses/proxies.
2026-01-17 11:52:08 -05:00
Mike Ash
d0017555c7 [Runtime] Faster dynamic exclusivity checking implemented in Swift.
Replace C++ implementation of swift_beginAccess and swift_endAccess with (almost) pure Swift implementation. Helpers remain in C++ for TLS, getting return addresses, and raising a fatal error on violations.

This change also moves the exclusivity access set head from the shared SwiftTLSContext structure to a dedicated TLS key. This improves performance, which is important for exclusivity checking. This is particularly the case where we can inline TLS access with a constant key, as on Darwin ARM64.

The code that bridges exclusivity tracking into Concurrency remains in C++. The new Swift implementation exposes a few helpers for it to use as a replacement for directly manipulating the C++ implementation.

rdar://161122309
2026-01-14 12:23:55 -05:00
Konrad `ktoso` Malawski
40dabefdb4 Reapply: Runtime: expose demangle() in Runtime module (#84788) (#86510) 2026-01-14 21:44:28 +09:00
eeckstein
4766678f3f Revert "Runtime: expose demangle() in Runtime module (#84788)"
This reverts commit ab69fc0d2c.
2026-01-12 10:26:14 +01:00
Konrad `ktoso` Malawski
ab69fc0d2c Runtime: expose demangle() in Runtime module (#84788)
We should expose the demangle functionality; It's been widely used by
calling into internal _swift_demangle and instead we should offer a real
API. It's also already used in the Runtime module already when forming
backtraces.

[Previous
discussions](https://forums.swift.org/t/demangle-function/25416/15)
happened between 2019 and 2024, and just never materialized in a
complete implementation and proposal.

Right now, even more tools are in need of this API, as we are building
continious profiling solutions etc, so it is a good time to revisit this
topic.

This PR is roughly based off @Azoy's

https://github.com/swiftlang/swift/pull/25314/files#diff-fd379a721cc9a1c9ef6486eae713e945da842b42170d4d069029a57334371eba
from 2019, however it brings it over to the new Runtime module which is
a great place to put this functionality - even Backtrace had to recently
reinvent calling the demangling infra in this module.

Pending SE review, [proposal
here](https://github.com/swiftlang/swift-evolution/pull/2989).

cc @azoy @al45tair

---------

Co-authored-by: Alastair Houghton <alastair@alastairs-place.net>
2026-01-09 09:46:19 -08:00
Saleem Abdulrasool
5dc5ecd6c9 runtime: silence a warning on clang
`[[no_unique_address]]` is a C++20 feature and we currently use C++17.
Guard this with `__clang__` and use the clang namespaced attribute which
is available. Additionally, since this feature is supported by MSVC as
well as an extension in earlier versions, use the MSVC scoped attribute
on MSVC.
2026-01-07 10:43:38 -08:00
Alastair Houghton
7f91815fb8 Merge pull request #86249 from AZero13/patch-23
[Runtime][Backtracing][Linux] Read and write from `ptr`, not `buf`.
2026-01-06 17:30:06 +00:00
AZero13
93d545dd25 Read and write from ptr, not buffer
This causes both functions to repeatedly read/write the same bytes from the beginning of the buffer instead of advancing through it.
2026-01-01 12:58:38 -05:00
AZero13
e895af4d1a Runtime: Fix error message concatenation using null pointer
Error history is lost because we used current, which is set to nullptr, so it would just print (null)
2025-12-28 12:43:52 -05:00
Carl Peto
590f3d702b [Backtracing] Fix threads=all in SWIFT_BACKTRACE parameters.
If you added threads=all to the SWIFT_BACKTRACE settings, it actually prevented all threads from being shown. This fixes that.

Also, the multiple thread test should test for presence of multiple threads.

rdar://166532079
2025-12-15 15:27:35 +00:00
AZero13
bef81c9ee5 swift_nonatomic_unknownObjectRelease should use swift_nonatomic_release
This appears to be a typo. Thankfully, it is not a correctness issue, as the atomic version does the same thing just slower.
2025-12-05 20:56:06 -05:00
Carl Peto
95d6d4384c [Backtracing] Fix color=tty to check the actual output.
The `color=tty` setting was checking `stdout` to see if it was a TTY, but
we may be using `stderr`, in which case it's easy to end up with coloured
output in a file we're redirecting to.  Fix it to check the selected
output instead.

rdar://164624364
2025-11-21 11:50:32 +00:00
Alastair Houghton
40cee72e5a Merge pull request #85350 from aeu/fix-readlink-null-termination
Fix missing null terminator in runtime path resolution on Linux
2025-11-17 10:25:39 +00:00
Mike Ash
662f8d4613 Merge pull request #85346 from mikeash/fix-extended-existential-copying
[Runtime] Fix copying in extended existential value witnesses.
2025-11-10 12:40:26 -05:00
Mike Ash
cc8e6fd877 Merge pull request #85260 from mikeash/client-rr-library-rename
[Runtime] Rename ClientRetainRelease library to SwiftDirectRuntime.
2025-11-08 12:40:27 -05:00
Mike Ash
7bb9579434 [Runtime] Fix copying in extended existential value witnesses.
Instead of copying the data and the type and witnesses separately, use the size in the value witness table and copy everything at once.

copyTypeInto assumed the type was an ordinary existential. When it was actually an extended existential, it would do an incorrect cast and read part of a pointer as the number of witness tables to copy. This would typically result in a large buffer overflow and crash. At this point we already know the type's size, so we can use that info directly rather than essentially recomputing it.

rdar://163980446
2025-11-07 23:38:09 -05:00
Mike Ash
a62f08e050 [Concurrency] Add environment variable for disabling async stack slab allocator.
Add SWIFT_DEBUG_ENABLE_TASK_SLAB_ALLOCATOR, which is on by default. When turned off, async stack allocations call through to malloc/free. This allows memory debugging tools to be used on async stack allocations.
2025-11-07 22:48:41 -05:00
Mike Ash
1898b01ce6 [Runtime] Rename ClientRetainRelease library to SwiftDirectRuntime.
This library will likely become home to other fast-path-in-client functions, so give it a more general name.
2025-11-07 16:36:29 -05:00
Alfonso Urdaneta
cfbe70db5d Fix missing null terminator in runtime path resolution on Linux 2025-11-05 23:39:26 +00:00
Mike Ash
fd6edb86e0 Merge pull request #85208 from mikeash/remove-weak-def-placeholder
[Runtime] Temporarily remove swift_release_preservemost_weak_placeholder.
2025-10-30 14:23:00 -04:00
Mike Ash
e1d20db8a2 [Runtime] Temporarily remove swift_release_preservemost_weak_placeholder.
This is breaking embedded due to its use of weak definitions for embedded versions of the runtime functions. The presence of a weak export in libswiftCore allows any strong symbol in libswiftCore to override a weak symbol elsewhere. Remove while we work out how to reconcile the two.

rdar://163578646
2025-10-29 22:32:43 -04:00
Mike Ash
a9ee814345 [Runtime] Fix the no-retain/release-overrides build.
We need a stub version of CALL_IMPL_SWIFT_REFCOUNT_CC for the !SWIFT_STDLIB_OVERRIDABLE_RETAIN_RELEASE case.
2025-10-29 12:22:10 -04:00
Mike Ash
3a0b3924df Merge pull request #85044 from mikeash/emit-into-client-retain-release
[IRGen][Runtime] Add emit-into-client retain/release calls for Darwin ARM64.
2025-10-28 12:09:01 -04:00
Mike Ash
93fae78e04 [IRGen][Runtime] Add emit-into-client retain/release calls for Darwin ARM64.
This is currently disabled by default. Building the client library can be enabled with the CMake option SWIFT_BUILD_CLIENT_RETAIN_RELEASE, and using the library can be enabled with the flags -Xfrontend -enable-client-retain-release.

To improve retain/release performance, we build a static library containing optimized implementations of the fast paths of swift_retain, swift_release, and the corresponding bridgeObject functions. This avoids going through a stub to make a cross-library call.

IRGen gains awareness of these new functions and emits calls to them when the functionality is enabled and the target supports them. Two options are added to force use of them on or off: -enable-client-retain-release and -disable-client-retain-release. When enabled, the compiler auto-links the static library containing the implementations.

The new calls also use LLVM's preserve_most calling convention. Since retain/release doesn't need a large number of scratch registers, this is mostly harmless for the implementation, while allowing callers to improve code size and performance by spilling fewer registers around refcounting calls. (Experiments with an even more aggressive calling convention preserving x2 and up showed an insignificant savings in code size, so preserve_most seems to be a good middle ground.)

Since the implementations are embedded into client binaries, any change in the runtime's refcounting implementation needs to stay compatible with this new fast path implementation. This is ensured by having the implementation use a runtime-provided mask to check whether it can proceed into its fast path. The mask is provided as the address of the absolute symbol _swift_retainRelease_slowpath_mask_v1. If that mask ANDed with the object's current refcount field is non-zero, then we take the slow path. A future runtime that changes the refcounting implementation can adjust this mask to match, or set the mask to all 1s to disable the old embedded fast path entirely (as long as the new representation never uses 0 as a valid refcount field value).

As part of this work, the overall approach for bridgeObjectRetain is changed slightly. Previously, it would mask off the spare bits from the native pointer and then call through to swift_retain. This either lost the spare bits in the return value (when tail calling swift_retain) which is problematic since it's supposed to return its parameter, or it required pushing a stack frame which is inefficient. Now, swift_retain takes on the responsibility of masking off spare bits from the parameter and preserving them in the return value. This is a trivial addition to the fast path (just a quick mask and an extra register for saving the original value) and makes bridgeObjectRetain quite a bit more efficient when implemented correctly to return the exact value it was passed.

The runtime's implementations of swift_retain/release are now also marked as preserve_most so that they can be tail called from the client library. preserve_most is compatible with callers expecting the standard calling convention so this doesn't break any existing clients. Some ugly tricks were needed to prevent the compiler from creating unnecessary stack frames with the new calling convention. Avert your eyes.

To allow back deployment, the runtime now has aliases for these functions called swift_retain_preservemost and swift_release_preservemost. The client library brings weak definitions of these functions that save the extra registers and call through to swift_retain/release. This allows them to work correctly on older runtimes, with a small performance penalty, while still running at full speed on runtimes that have the new preservemost symbols.

Although this is only supported on Darwin at the moment, it shouldn't be too much work to adapt it to other ARM64 targets. We need to ensure the assembly plays nice with the other platforms' assemblers, and make sure the implementation is correct for the non-ObjC-interop case.

rdar://122595871
2025-10-27 12:00:28 -04:00
Jonathan Grynspan
8feba8d584 Use __ehdr_start on ELF instead of calling dladdr(). (#85012)
This PR replaces our use of `__dso_handle` and (indirectly) `dladdr()`
when figuring out the base address of loaded Swift images on ELF-based
platforms. Instead, we use `__ehdr_start` which refers to the start of
the image's ELF header (i.e. the start of the file) and is provided by
the compiler/linker for this purpose.

This pointer is consumed by `swift_addNewDSOImage()` and, later, by
Swift Testing when running tests, but is otherwise unused, so the less
work we do on it at runtime, the better.

A fallback path is present that, for images that do not contain
`__ehdr_start`, derives the address by calling `dladdr()` on the section
structure's address.

Resolves #84997.
2025-10-24 12:01:09 -07:00
Alejandro Alonso
bc04d6dcf7 Don't check suppressed protocols when the extended existential is metatype constrained 2025-10-15 10:57:52 -07:00
Tim Kientzle
f0679b363e Re-apply PR #82750: Reimplement floating-point description implementation in Swift.
This reverts PR #84576, which was a revert of PR #82750
It reverts commit 4ac18aa32e, reversing
changes made to b46eddbabd.
2025-10-07 07:11:57 -07:00
Tim Kientzle
7d85d75f78 Revert "Merge pull request #82750 from tbkka/tbkka-swift-floatingpointtostring"
This reverts commit 54627fb49b, reversing
changes made to dda4608a84.
2025-09-29 15:13:19 -07:00
Tim Kientzle
54627fb49b Merge pull request #82750 from tbkka/tbkka-swift-floatingpointtostring
Reimplement floating-point description implementation in Swift.
2025-09-29 07:55:25 -07:00
Mike Ash
3f610e94a3 Merge pull request #84446 from mikeash/swift-inspect-scan-search
[swift-inspect] Add a mode to search for and dump generic metadata without metadata iteration enabled.
2025-09-26 19:46:00 -04:00