Commit Graph

3163 Commits

Author SHA1 Message Date
Tim Kientzle
2be0f047f8 Missing include 2024-02-28 10:42:52 -08:00
Mike Ash
a71eaf15db [Runtime] Don't shrink class InstanceStart when initing field offset vectors.
When ObjC interop is enabled, we emit what we think will be the class's InstanceStart and InstanceSize based on what we know about the superclass. We then fix up those values at runtime if they don't match. The compiler will emit this data into read-only memory if it knows they will always match, and then the runtime avoids writing to these fields if they already contain the correct value.

However, the compiler aligns the InstanceStart, but instance size is not aligned. For example:

class Super<T> { var bool = true }
class Sub: Super<Int> { var obj: AnyObject? }

Super's InstanceSize is 17 (on 64-bit) but Sub's InstanceStart is 24. The compiler sees a fixed layout and emits Sub's rodata into constant memory. The runtime sees that 24 does not equal 17 and tries to update it, but we don't want it to.

Instead, only update InstanceStart if it's too small to accommodate the superclass's InstanceSize. If it's overlay large then we'll just leave it alone. The compiler underestimates InstanceStart when it doesn't know the superclass's size so this should only happen due to alignment.

rdar://123695998
2024-02-28 10:49:42 -05:00
Tim Kientzle
5337c8b208 Preserve legacy behavior on Apple OSes in all environments 2024-02-27 17:46:49 -08:00
Tim Kientzle
fac88221c5 Fix hash/isEqual interop conditionals, update tests
Github PR #71620 mixed up one of the bincompat conditionals.
It also had some errors in the tests for ObjC interop.

For now, this leaves the legacy behavior in place for
all Apple platforms.
2024-02-27 17:46:49 -08:00
Mike Ash
20d60406cb [Runtime] Harden SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH.
Have SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH use dlopen with RTLD_NOLOAD. We don't want it to allow loading arbitrary dylibs. Instead, a user can use something like DYLD_INSERT_LIBRARIES to load the dylib, which we will then pick up in the runtime, and processes that deny DYLD_INSERT_LIBRARIES will not be able to work around it with SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH.

rdar://123643585
2024-02-26 14:38:09 -05:00
Konrad `ktoso` Malawski
1d44e2e8e0 [Distributed] Undo new record and mangling scheme for dist.p.witnesses (#71801) 2024-02-22 23:02:29 +09:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Yuta Saito
d3d4bd203c Merge pull request #71768 from kateinoigakukun/pr-5b0947b86ee45b937e9b6fedd0963644227b754d 2024-02-22 04:07:42 +09:00
Yuta Saito
f3f13a2637 [wasm] Enable --gc-sections for WebAssembly by default
This change enables the `--gc-sections` linker flag for WebAssembly
targets by default. This flag has been disabled because it did not
respect `llvm.used` and it caused stripping `swift5` metadata sections
even though they were used through encapsulation symbols (a.k.a `__start`/`__stop`).

The issue has been fixed in the LLVM side (ba3c1f9ce3)
by adding new segment flags to the WebAssembly object file format.
2024-02-21 02:13:01 +00:00
Joe Groff
b9f91144d1 Fix ABI breakage caused by ValueOwnership order change.
The `ABI` headers had accidentally grown an `#include` into compiler headers,
allowing the enum constant values of the `ValueOwnership` enum to leak into
the runtime ABI. Sever this inappropriate relationship by declaring a separate
`ParameterOwnership` enum with ABI-stable values in the ABI headers, and
explicitly converting between the AST and ABI representation where needed.
Fixes rdar://122435628.
2024-02-20 07:55:16 -08:00
Michael Gottesman
f4efcec55c [transferring] Add mangling support for transferring.
This includes runtime support for instantiating transferring param/result in
function types. This is especially important since that is how we instantiate
function types like: typealias Fn = (transferring X) -> ().

rdar://123118061
2024-02-19 12:11:57 -08:00
Jonathan Grynspan
d35dcc8f9c Add swift5_tests to MetadataSections. (#71509) 2024-02-19 13:47:40 -05:00
Mike Ash
d6f9401c28 Merge pull request #71627 from mikeash/externalgenericmetadatabuilder-fix-targets-exports-recursion
[ExternalGenericMetadataBuilder] Fix pointer targets, exports, and recursive metadata.
2024-02-17 18:28:01 -05:00
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