Commit Graph

3150 Commits

Author SHA1 Message Date
Jonathan Grynspan
0664c3f333 Add a distinct hook for swift_willThrowTypedImpl(). (#71651)
Add a distinct hook for `swift_willThrowTypedImpl()`.

This PR adds a hook for `swift_willThrowTypedImpl()` that is distinct from
`swift_willThrow()`. The current implementation of `swift_willThrowTypedImpl()` creates a
temporary existential box for the thrown error and calls the `_swift_willThrow` hook that
is set by XCTest and swift-testing. Unfortunately, this temporary existential box isn't
very useful because its address is not expected to be stable.

Instead, expose a separate hook, `_swift_willThrowTypedImpl`, whose signature matches
that of the new ABI function. This hook can then be used by XCTest and swift-testing to
distinguish between typed throws and untyped throws and avoids creating a temporary
existential box when it won't be useful.

As implemented, if `_swift_willThrowTypedImpl` is not set but `_swift_willThrow` is, the
`swift_willThrowTypedImpl()` will fall back to calling `_swift_willThrow` with a temporary
existential box. We might decide this is unnecessary, but I figured it would allow us more
time to stage adoption of the new hook in the testing libraries.

Resolves rdar://122824443.
2024-02-16 17:30:44 -05:00
Mishal Shah
81dbc534d6 Merge pull request #71678 from al45tair/eng/PR-122995672 2024-02-16 10:04:46 -08:00
Dario Rexin
7a4fcc6841 [Runtime] Mask out reserved bits for SwiftError in BytecodeLayouts.cpp (#71669)
rdar://122911427

These bits can be used for storing multi payload enum tags and not masking them out can cause crashes an other unexpected behavior.
2024-02-16 08:59:22 -08:00
Konrad `ktoso` Malawski
e9c7f3c382 [Distributed] Target identifiers for protocol calls (#70928) 2024-02-16 07:19:20 -08:00
Alastair Houghton
88aa111677 [Runtime] Don't try to call __cxa_begin_catch if -fno-exceptions.
We shouldn't call `__cxa_begin_catch` if built with `-fno-exceptions`,
because it's possible that the environment we're in won't include
the `__cxa_begin_catch` routine.

rdar://122995672
2024-02-16 09:35:03 +00:00
Mike Ash
f66b57c7fb Merge pull request #71666 from mikeash/libprespecialize-no-packs
[Runtime] Skip types with pack parameters in LibPrespecialized.
2024-02-15 23:30:06 -05:00
David Smith
de50b0a387 Eliminate pointer auth overhead in interposable refcount support (#71448)
Fixes rdar://122595662
2024-02-15 18:21:34 -08:00
Mike Ash
923bf1fdea Merge pull request #71664 from mikeash/build-metadata-demangling-null-checks
[Runtime] Add some missing NULL checks to _swift_buildDemanglingForMetadata calls.
2024-02-15 20:11:48 -05:00
Mike Ash
dba0859b5c [Runtime] Skip types with pack parameters in LibPrespecialized.
We don't yet support pack parameters anyway, and `_buildDemanglingForGenericType` has trouble with them, so skip them altogether.
2024-02-15 17:37:31 -05:00
Mike Ash
8dc4bc13b1 [Runtime] Add some missing NULL checks to _swift_buildDemanglingForMetadata calls.
The call can fail but many callers assumed it always succeeds.
2024-02-15 16:30:09 -05:00
Mike Ash
5f7421b7a7 [Runtime] Check for demangling failure when looking up prespecialized metadata.
_buildDemanglingForGenericType can fail, so we need to check for NULL.
2024-02-15 10:25:46 -05:00
Tim Kientzle
0308b167a8 Merge pull request #71620 from tbkka/tbkka-bincompat-objc-hash-isequal-interop
Bincompat hooks for revised hash/isEqual interop
2024-02-15 07:11:02 -08:00
Mike Ash
752f0fb2f8 [ExternalGenericMetadataBuilder] Fix pointer targets, exports, and recursive metadata.
Use exports, not symbols, when emitting a pointer target. Exports are what the linker can actually work with.

When searching for a nearby export to use for a pointer target, accept anything within the same segment, not just the same section. Only segments can be rearranged relative to each other, not sections within a segment, so this is safe and allows for more possible targets.

Disallow pointer targets with no export within the same segment. We attempted to emit a target that's relative to the section starting point in this case, but that didn't work. We'll revisit if it looks useful to do so.

In order to make this work, we resolve the export when writing a pointer instead of when emitting JSON, and make the writePointer functions failable. If writePointer fails, we'll fail to build the metadata and skip it.

Correctly handle the case where the names JSON contains a metadata we already constructed as part of a prior name. Previously we'd emit it twice, now it checks to see if it's already been built and do nothing in that case. Also save errors when a metadata can't be built, so subsequent attempts to build it can fail immediately.

When emitting fixups with ptrauth attributes, use the correct target kind "arm64_auth_ptr".

Fix the VerifyExternalMetadata.swift test not to load an arm64e runtime slice when testing arm64. That's normally fine, but we depend on loading the exact same dylib that we built prespecializations for.

rdar://122968337
2024-02-14 16:57:18 -05:00
Tim Kientzle
b3ed7aee24 Bincompat hooks for revised hash/isEqual interop
This adds in hooks so that the new hash/isEqual interop
(which bridges Obj-C hash/isEqual: calls to the corresponding
Swift Hashable/Equatable conformances) can be selectively
disabled based on the OS and/or client.

For now, enable the new semantics everywhere except Apple platforms
(which have legacy apps that may be relying on the old semantics).
2024-02-14 12:13:07 -08:00
Mike Ash
89e5a80847 [ExternalGenericMetadataBuilder] Support ARM64e fixups.
Add ptrauth attributes to the witness tables and descriptor pointers, and have the test support ARM64e.

rdar://122660111
2024-02-14 09:52:58 -05:00
Alastair Houghton
2aba85f12f Merge pull request #71056 from al45tair/eng/PR-120952971
[Runtime][IRGen] Trap C++ exceptions on *throw*, not catch.
2024-02-08 15:00:35 +00:00
Alastair Houghton
9d2dcb5c4e Merge pull request #71386 from al45tair/eng/PR-122302117
[Runtime] Add an option to produce non-symbolicated backtraces.
2024-02-08 11:14:26 +00:00
Alastair Houghton
dd0b826235 [Runtime] Improved symbolication option for backtracing.
Rather than just on or off, I've changed it to allow "off", "fast",
or "full".  "fast" means that we'll do symbol lookup, but we won't
try to find inline frames and we won't run line number programs
(those are the things that are taking considerable time in some
cases).

rdar://122302117
2024-02-06 17:38:20 +00:00
Alastair Houghton
25e9ca02b0 [Runtime][Test] Add a leading underscore to swift_exceptionPersonality.
The other new runtime functions appear to have a leading underscore.
It makes sense in this case because we don't expect anything to call
this directly (other than the unwinder).

rdar://120952971
2024-02-06 16:21:09 +00:00
Alastair Houghton
143a473aa4 [Runtime][IRGen] Trap C++ exceptions on *throw*, not catch.
The previous approach was effectively to catch the exception and then
run a trap instruction.  That has the unfortunate feature that we end
up with a crash at the catch site, not at the throw site, which leaves
us with very little information about which exception was thrown or
where from.

(Strictly we do have the exception pointer and could obtain exception
information, but it still won't tell us what threw it.)

Instead of that, set a personality function for Swift functions that
call potentially throwing code, and have that personality function
trap the exception during phase 1 (i.e. *before* the original stack
has been unwound).

rdar://120952971
2024-02-06 16:19:17 +00:00
Doug Gregor
e4d6bee153 Merge pull request #71400 from DougGregor/will-throw-typed
Implement `swift_willThrow` variant for typed throws.
2024-02-06 03:22:22 -08:00
Doug Gregor
3fa07a0e7a Implement swift_willThrow variant for typed throws.
`swift_willThrow` is called with an error right before it is thrown.
This existing entrypoint requires an already-boxed error existential;
with typed errors, we don't have the error existential on hand, so we
would need to allocate the box to throw a typed error. That's not okay.

Introduce a new `swift_willThrowTypedImpl` entry point into the runtime
that will first check for the presence of an error handler and, if one
is present, box the error to provide to the error handler. This
maintains the no-allocations path for typed errors while still
allowing existing error handlers to work.

This new entrypoint isn't available on older Swift runtimes, so create
a back-deployable shim called by the compiler. On new-enough platforms,
this will call through to `swift_willThrowTypedImpl`. On older
platforms, we drop the error and don't call the registered will-throw
handler at all. This is a compromise that avoids boxing when throwing
typed errors, at the cost of a slightly different experience for this
new feature on older runtimes.

Fixes rdar://119828459.
2024-02-05 15:06:55 -08:00
Alastair Houghton
8269bf72eb [Runtime] Add an option to produce non-symbolicated backtraces.
Symbolication can take some time, depending on the binaries involved.
In certain contexts it's better for the backtrace to finish quickly,
and then symbolication could be done offline.

rdar://122302117
2024-02-05 12:56:47 +00:00
Tim Kientzle
ccc6ef8e68 Merge pull request #70978 from tbkka/tbkka-revert-PR68952
Revert #68952 [Casting] Make more casts look inside __SwiftValue
2024-02-02 11:03:04 -08:00
Guillaume Lessard
114f235d17 Merge pull request #71167 from vanvoorden/vanvoorden/inclusive-language
[Inclusive Language][Comments][Documentation] migrate "sanity" checks to "soundness" checks
2024-02-02 10:27:34 -08:00
Dario Rexin
55e565d838 [Runtime] Handle different types for src and dest in existentialAssignWithCopy (#71323)
rdar://117083470

An existential can contain a different type than the one that is being assigned, so we have to check the types and handle the values accordingly.
2024-02-02 08:34:37 -08:00
Mike Ash
b8b59c4bac [ExternalGenericMetadataBuilder] Add a test that validates built metadata against the runtime.
We run the builder, then use a small program that converts the JSON output into C code that generates the data. Compile that into a bundle, then load it as the prespecializations library. Then scan all the entries in the table and compare them with what the runtime builds dynamically.
2024-01-31 17:03:27 -05:00
Mike Ash
46ddceb7d4 Merge pull request #71184 from mikeash/libprespecialize-metadata-name-lookup
[Runtime] Do a proper hash table lookup in the prespecializations library.
2024-01-29 12:10:35 -05:00
Mike Ash
ade4b8e989 [Runtime] Fix dyld call for Swift prespecialized data.
Make our variables const to match the call signature, and do a weak check of the symbol before calling it.
2024-01-27 12:53:23 -05:00
Mike Ash
edb292dda5 [Runtime] Do a proper hash table lookup in the prespecializations library.
We were doing a linear scan of the table contents as a stopgap. Stop doing that, and compute the proper key for the lookup, matching the one used in the builder.
2024-01-26 12:04:02 -05:00
Mike Ash
4bc7726d43 Merge pull request #70900 from mikeash/swift-generic-metadata-builder-out-of-process
[Tools] Add a library to build specialized generic metadata out of process.
2024-01-26 10:50:34 -05:00
John McCall
b0fb03d8c7 Create a uniform representation for function type isolation.
Not quite NFC because apparently the representation bleeds into what's
accepted in some situations where we're supposed to be warning about
conflicts and then making an arbitrary choice.  But what we're doing
is nonsense, so we definitely need to break behavior here.

This is setting up for isolated(any) and isolated(caller).  I tried
to keep that out of the patch as much as possible, though.
2024-01-25 22:11:01 -05:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Mike Ash
4341102c92 [Tools] Add a library to build specialized generic metadata out of process.
This library uses GenericMetadataBuilder with a ReaderWriter that can read data and resolve pointers from MachO files, and emit a JSON representation of a dylib containing the built metadata.

We use LLVM's binary file readers to parse the MachO files and resolve fixups so we can follow pointers. This code is somewhat MachO specific, but could be generalized to other formats that LLVM supports.

rdar://116592577
2024-01-24 20:45:50 -05:00
Tim Kientzle
62f8780bb0 Revert "Obj-C class metatypes should never satisfy Obj-C existentials"
This reverts commit aed05b9a36.

This needs some additional work.  Reverting out until it can be fixed.
2024-01-22 11:27:42 -08:00
Tim Kientzle
4ea5ed1309 Revert #68952 [Casting] Make more casts look inside __SwiftValue
Turns out that our Obj-C bridging relies on this inconsistent behavior,
so it can quite likely never be actually fixed.
2024-01-17 18:14:57 -08:00
Allan Shortlidge
79d0ecafaa NFC: Ignore deprecation of asl_log in the runtime and demangling library.
ASL is deprecated in macOS 10.12. It may be time to transition to os_log now
that deployment targets have been raised to 10.12, but until that project
starts these warnings are just pollution.

Filed rdar://121066531 to track adoption of `os_log()` if appropriate.
2024-01-16 13:27:55 -08:00
Allan Shortlidge
d3305af80a NFC: Ignore -Wreceiver-forward-class warning in SwiftObject.mm. 2024-01-16 13:27:55 -08:00
Allan Shortlidge
5add794b82 NFC: Ignore -Wobjc-root-class warning in SwiftObject.h.
SwiftObject intentionally does not inherit from `NSObject`.
2024-01-16 13:27:55 -08:00
Allan Shortlidge
3a3a2460df NFC: Resolve -Wformat warning in ProtocolConformance.cpp. 2024-01-12 17:41:50 -08:00
Allan Shortlidge
272a1a4b18 NFC: Resolve -Wauto-var-id warning in ErrorObject.mm. 2024-01-12 17:40:46 -08:00
Allan Shortlidge
333b7b6a46 NFC: Resolve unused variable warnings in BytecodeLayouts.cpp. 2024-01-12 17:39:00 -08:00
Mike Ash
29c350e813 [Runtime] Create an external generic metadata builder.
Create a version of the metadata specialization code which is abstracted so that it can work in different contexts, such as building specialized metadata from dylibs on disk rather than from inside a running process.

The GenericMetadataBuilder class is templatized on a ReaderWriter. The ReaderWriter abstracts out everything that's different between in-process and external construction of this data. Instead of reading and writing pointers directly, the builder calls the ReaderWriter to resolve and write pointers. The ReaderWriter also handles symbol lookups and looking up other Swift types by name.

This is accompanied by a simple implementation of the ReaderWriter which works in-process. The abstracted calls to resolve and write pointers are implemented using standard pointer dereferencing.

A new SWIFT_DEBUG_VALIDATE_EXTERNAL_GENERIC_METADATA_BUILDER environment variable uses the in-process ReaderWriter to validate the builder by running it in parallel with the existing metadata builder code in the runtime. When enabled, the GenericMetadataBuilder is used to build a second copy of metadata built by the runtime, and the two are compared to ensure that they match. When this environment variable is not set, the new builder code is inactive.

The builder is incomplete, and this initial version only works on structs. Any unsupported type produces an error, and skips the validation.

rdar://116592420
2024-01-11 09:15:02 -05:00
Rose
873a423da6 Some methods swap the number of elements and the size arguments
For calloc, the variable denoting the of elements comes first,
then the variable denoting the size of each element. However, both
arguments are swapped when calling this function in many places in this codebase.
2023-12-23 11:00:31 -05:00
Alastair Houghton
7689c29e08 [Runtime] Fix call site for malloc_type_posix_memalign.
Seems I missed off the type ID.  Oops.

rdar://119137861
2023-12-13 20:52:51 +00:00
Alastair Houghton
9fc61ddb8b [Runtime] Use malloc_type_posix_memalign().
We can't use `malloc_type_aligned_alloc()` because `aligned_alloc()` requires
that `size` be a multiple of `alignment`, which isn't something we expect here.

rdar://119137861
2023-12-11 12:21:02 +00:00
Alastair Houghton
c9abb0bc61 [Backtracing] Improve warning message about thread suspension failure.
In the event that the backtracer fails to suspend a thread, it emits a
warning message.  Unfortunately the exact meaning of this message isn't
obvious and it doesn't make clear that it's a secondary problem (that is,
you've already crashed at this point).

Update the message to make things clearer.

rdar://116593541
2023-12-08 13:53:38 +00:00
Mike Ash
3716df4ca5 [Runtime] Fix findBridgeWitness when building for the shared cache.
The static bridgeableProtocol inherits the ptrauth_struct attribute which uses the B key, and that's not allowed on global data in the shared cache. Pass the value directly as a parameter instead.
2023-12-01 09:40:25 -05:00
nate-chandler
1c0971e719 Merge pull request #70099 from nate-chandler/gardening/20231129/1
[Gardening] Fix typo.
2023-11-29 16:59:09 -08:00
Nate Chandler
3817af3067 [Gardening] Fix typo.
Replaced homophone of presence ("presents", i.e. 🎁🎁, 🖥️, etc.) with
the word itself.
2023-11-29 12:52:35 -08:00