Commit Graph

29 Commits

Author SHA1 Message Date
Konrad `ktoso` Malawski
85fcd69833 [Concurrency] Implement detecting isIsolatingCurrentContext user impls (#79946)
* [Concurrency] Detect non-default impls of isIsolatingCurrentContext

* [Concurrency] No need for trailing info about isIsolating... in conformance

* Apply changes from review
2025-03-18 09:39:11 +09:00
Konrad `ktoso` Malawski
e056c63c89 [Concurrency] Implement isIsolatingCurrentContext requirement and mode (#79788) 2025-03-11 09:48:38 +09:00
Konrad `ktoso` Malawski
76f3729644 The SWIFT_DUMP_ACCESSIBLE_FUNCTIONS flag must be a bool, not a string 2025-02-05 10:30:53 +09:00
Konrad `ktoso` Malawski
6e9224cb5d [Distributed] Introduce SWIFT_DUMP_ACCESSIBLE_FUNCTIONS 2024-12-03 14:59:15 +09:00
Mike Ash
4b3a197dc2 [Runtime] Support type descriptor map in LibPrespecialized.
The descriptor map is keyed by a simplified mangling that canonicalizes the differences that we accept in _contextDescriptorMatchesMangling, such as the ability to specify any kind of type with an OtherNominalType node.

This simplified mangling is not necessarily unique, but we use _contextDescriptorMatchesMangling for the final equality checking when looking up entries in the map, so occasional collisions are acceptable and get resolved when probing the table.

The table is meant to be comprehensive, so it includes all descriptors that can be looked up by name, and a negative result means the descriptor does not exist in the shared cache. We add a flag to the options that can mark it as non-definitive in case we ever need to degrade this, and fall back to a full search after a negative result.

The map encompasses the entire shared cache but we need to reject lookups for types in images that aren't loaded. The map includes an image index which allows us to cheaply query whether a given descriptor is in a loaded image or not, so we can ignore ones which are not.

TypeMetadataPrivateState now has a separate sections array for sections within the shared cache. _searchTypeMetadataRecords consults the map first. If no result is found in the map and the map is marked as comprehensive, then only the sections outside the shared cache need to be scanned.

Replace the SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED environment variable with one specifically for metadata and one for descriptor lookup so they can be controlled independently. Also add SWIFT_DEBUG_VALIDATE_LIB_PRESPECIALIZED_DESCRIPTOR_LOOKUP which consults the map and does the full scan, and ensures they produce the same result, for debugging purposes.

Enhance the environment variable code to track whether a variable was set at all. This allows SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED to override the default in either direction.

Remove the disablePrespecializedMetadata global and instead modify the mapConfiguration to disable prespecialized metadata when an image is loaded that overrides one in the shared cache.

rdar://113059233
2024-08-01 18:43:15 -04:00
Mike Ash
7427aa5f48 [Concurrency] Remove SWIFT_ENABLE_ASYNC_JOB_DISPATCH_INTEGRATION environment variable.
This option serves no purpose anymore and this dispatch integration should always be enabled.
2024-05-30 12:43:19 -04:00
Konrad `ktoso` Malawski
7790609fc8 [Concurrency] Futher prevent crashes in legacy mode of isCurrentExecutor 2024-05-27 17:02:26 +09:00
Konrad `ktoso` Malawski
b2bd17635f [Concurrency] Fix too eager early return in checkIsolation mode detecting (#73495) 2024-05-09 03:12:37 -07:00
Mike Ash
b8f29cfda0 [Runtime] Allow disabling/enabling prespecializations library per-process.
Read a list of disabled process names from the prespecializations library, and turn the feature off if the current process matches. Also allow passing process names in environment variables. Processes can be disabled by name using SWIFT_DEBUG_LIB_PRESPECIALIZED_DISABLED_PROCESSES, and a disable can be overridden with SWIFT_DEBUG_LIB_PRESPECIALIZED_ENABLED_PROCESSES.

rdar://126216786
2024-04-10 14:29:19 -04:00
Mike Ash
0491192337 [Runtime] Remove ExternalGenericMetadataBuilder. 2024-03-21 17:56:17 -04: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
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
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
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
Alastair Houghton
8a29657f4c [Runtime] [Darwin] Add code to trigger the external backtracer.
When a Swift program crashes, we should catch the crash and execute the
external backtracer.

rdar://105391747
2023-03-04 07:56:02 +00:00
Alastair Houghton
f02f62f1e3 [Backtracing] Add support for looking up paths for auxiliary executables.
We need to be able to locate `swift-backtrace` relative to the current
location of the runtime library.

This needs to work:

* In a Swift build directory.
* On Darwin, where we're installed in /usr/lib/swift and /usr/libexec/swift.
* On Linux, where we're in /usr/lib/swift/linux and /usr/libexec/swift/linux.
* On Windows, where we may be in a flat directory layout (because of limitations
  of Windows DLL lookups).

rdar://103071801
2023-03-01 13:43:15 +00:00
Alastair Houghton
f0200bc0d8 Conditionalise the warnings on an environment variable.
We'll only generate warnings if SWIFT_DEBUG_FAILED_TYPE_LOOKUP is
enabled.

rdar://103950409
2023-01-06 14:42:27 +00:00
Mike Ash
96e965a359 [Concurrency] Use dispatch cooperative queues when available.
These queues are better suited to the concurrency runtime.
2022-09-29 15:46:35 -04:00
Mike Ash
afc5116ef0 [Concurrency] Add an environment variable to validate unchecked continuation usage.
When enabled, we track all active unchecked continuations in a global set, and fatal error if one is called twice.

rdar://97390481
2022-09-16 13:50:59 -04:00
Karoy Lorentey
50c2399a94 [stdlib] Work around binary compatibility issues with String index validation fixes in 5.7
Swift 5.7 added stronger index validation for `String`, so some illegal cases that previously triggered inconsistently diagnosed out of bounds accesses now result in reliable runtime errors. Similarly, attempts at applying an index originally vended by a UTF-8 string on a UTF-16 string now result in a reliable runtime error.

As is usually the case, new traps to the stdlib exposes code that contains previously undiagnosed / unreliably diagnosed coding issues.

Allow invalid code in binaries built with earlier versions of the stdlib to continue running with the 5.7 library by disabling some of the new traps based on the version of Swift the binary was built with.

In the case of an index encoding mismatch, allow transcoding of string storage regardless of the direction of the mismatch. (Previously we only allowed transcoding a UTF-8 string to UTF-16.)

rdar://93379333
2022-05-17 19:25:10 -07:00
Michael Gottesman
f79108caf7 [exclusivity] Introduce an env variable to in asserts builds cause the exclusivity part of the runtime to emit logging information.
The specific environment variable is SWIFT_DEBUG_RUNTIME_EXCLUSIVITY_LOGGING. We only
check the environment the first time we try to lookup the TLSContext, so it
shouldn't impact exclusivity performance.

My intention is to use this to write some FileCheck tests so that we can really
be sure that the runtime is doing what we think it is. As such I also changed
the small amount of logging routines meant for use in the debugger to use stdout.
2021-07-22 13:25:10 -07:00
Mike Ash
0989524338 [Concurrency] Make Job objects work as Dispatch objects.
Fill out the metadata for Job to have a Dispatch-compatible vtable. When available, use the dispatch_enqueue_onto_queue_4Swift to enqueue Jobs directly onto queues. Otherwise, keep using dispatch_async_f as we have been.

rdar://75227953
2021-03-26 18:31:00 -04:00
Mike Ash
5193d76c74 [Runtime] Fixed shared cache protocol conformance lookup with subclasses.
Match the logic used in the non-shared-cache case, where we walk up the class hierarchy to find the class where the conformance actually applies. This is important in cases like:

  class Super<T> : Proto {}
  class Sub: Super<Int> {}

Looking up the conformance `Sub: Proto` without this logic causes it to attempt to find the witness table for `Sub<Int>`, which fails. The correct logic looks up the witness table for `Super<Int>`.

While we're in there, fix a bug in the shared cache validation code (std::find wasn't checked for failure correctly) and add a `SWIFT_DEBUG_ENABLE_SHARED_CACHE_PROTOCOL_CONFORMANCES` environment variable to allow us to turn the shared cache integration off at runtime.

rdar://75431771
2021-03-17 11:46:11 -04:00
Mike Ash
f35b110cf7 [Runtime] Support precomputed protocol conformances in the shared cache.
When available, take advantage of precomputed protocol conformances in the shared cache on Darwin platforms to accelerate conformance lookups and cut down on memory usage.

We consult the precomputed conformances before doing the runtime's standard conformance lookup. When a conformance is precomputed, this avoids the slow linear scan of all loaded conformances for the first access, and it avoids the memory cost of storing the conformance in the cache.

When the shared cache has no images overridden (the normal case), then we can also skip scanning conformances in the shared cache in all circumstances, because the precomputed conformances will always cover those results. This greatly speeds up the slow linear scan for the initial lookup of anything that doesn't have a conformance in the shared cache, including lookups with conformances in apps or app frameworks, and negative lookups.

A validation mode is available by setting the SWIFT_DEBUG_VALIDATE_SHARED_CACHE_PROTOCOL_CONFORMANCES environment variable. When enabled, results from the precomputed conformances are compared with the results from a slow scan of the conformances in the shared cache. This extremely slow, but good at catching bugs in the system.

When the calls for precomputed conformances are unavailable, the new code is omitted and we remain in the same situation as before, with the runtime performing all lookups by scanning conformance sections in all loaded Swift images.

rdar://71128983
2021-02-11 12:34:23 -05:00
Erik Eckstein
66d62ff144 stdlib: fix a back-deployment issue for array COW checks with a stdlib assert-build
And rename COWSanityChecks -> COWChecks.

rdar://problem/68181747
2020-09-21 12:21:11 +02:00
Erik Eckstein
666c5e087c stdlib: enable COW sanity checks with an environment variable
Instead of doing the sanity checks by default (with an assert-build of the stdlib), only do the checks if the environment variable SWIFT_DEBUG_ENABLE_COW_SANITY_CHECKS is set to true.

The checks can give false alarms in case a binary is built against a no-assert stdlib but run with an assert-stdlib.
Therefore only do the checks if it's explicitly enabled at runtime.

rdar://problem/65475776
2020-07-20 11:23:42 +02:00
Nate Chandler
03659813ca [Runtime] Scribble on metadata allocations.
Previously, when NDEBUG was not defined, the allocations made for value
metadata records were filled with 0xAA bytes.  Here, that behavior is
both expanded to all metadata records and enabled in release builds when
the environment variable SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE is set.
2020-06-16 12:46:43 -07:00
Mike Ash
cd624bf380 [Runtime][Reflection][swift-inspect] Add facilities for tracking and examining backtraces for metadata allocations.
rdar://problem/63674755
2020-06-11 14:49:52 -04:00
Mike Ash
f2fb53967c [Runtime] Unify debug variable parsing from the environment and avoid getenv when possible.
There are a few environment variables used to enable debugging options in the
runtime, and we'll likely add more over time. These are implemented with
scattered getenv() calls at the point of use. This is inefficient, as most/all
OSes have to do a linear scan of the environment for each call. It's also not
discoverable, since the only way to find these variables is to inspect the
source.

This commit places all of these variables in a central location.
stdlib/public/runtime/EnvironmentVariables.def defines all of the debug
variables including their name, type, default value, and a help string. On OSes
which make an `environ` array available, the entire array is scanned in a single
pass the first time any debug variable is requested. By quickly rejecting
variables that do not start with `SWIFT_`, we optimize for the common case where
no debug variables are set. We also have a fallback to repeated `getenv()` calls
when a full scan is not possible.

Setting `SWIFT_HELP=YES` will print out all available debug variables along with
a brief description of what they do.
2020-06-04 10:00:06 -04:00