Commit Graph

32 Commits

Author SHA1 Message Date
finagolfin da0de8aa96 Revert "Pack env vars into a struct and initialize them all at once, … (#89709)
…rather than lazily populating the _isSet flags on access. In most cases
this should go from O(n) writes to 0, since none of them will be set
(#89651)"

This reverts commit c5f72c8f19.

This optimization pull broke the Android CI, #89708, so reverting until
we can figure out why.
2026-06-05 12:14:29 -07:00
David Smith c5f72c8f19 Pack env vars into a struct and initialize them all at once, rather than lazily populating the _isSet flags on access. In most cases this should go from O(n) writes to 0, since none of them will be set (#89651)
Fixes rdar://178564603
2026-06-03 10:12:55 -07:00
Mike Ash cc2ee62dab [Runtime] Fix spurious "cannot parse value" environment variable warning.
When using the getenv path, parse_uint8 would print the "cannot parse value" when the value was NULL, but that should only be printed when a value is provided but cannot be parsed.
2026-06-01 16:38:25 -04:00
Mike Ash 7b19e8b87d [Runtime] Replace SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE with a configurable byte.
Replace the boolean SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE knob with
SWIFT_DEBUG_RUNTIME_ALLOC_SCRIBBLE_BYTE, an optional_uint8 that lets
the user pick the actual byte to scribble. An unset value disables
scribbling. Default is 0xaa in NDEBUG-off builds and unset in release.

Add an optional_uint8 environment variable type and a parse_optional_uint8
parser so the .def entry can express the new behavior.
2026-05-19 12:04:32 -04:00
Mike Ash f7745f2094 [Runtime] Group environment variable types into a types namespace.
Rather than attempt to use standard C++ type names, create a namespace specifically for environment variable types and require vars to use a type from that namespace. This avoids the annoying collision between this `string` and `std::string`, and makes everything a bit more consistent. Rename the types a bit to be more appropriate for this context: boolean, uint8, and uint32, instead of bool, uint8_t, and uint32_t.
2026-05-14 15:16:17 -04:00
Mike Ash db4692534e [Concurrency] Add more info to tracing calls.
* Have job_run include the actor, executor, and task name.
* Make task_status_changed only trace if one of the tracked values actually changed.
* Add a job_enqueue_executor that covers enqueueing jobs on serial executors (default actors, custom executors, main actor, etc.).
* Actually end the actor lifetime signpost interval.
* Include the actor metadata and context descriptor pointers in actor enqueue/dequeue so the type can be identified.
* Add the task pointer to all task-related signpost messages.
* Emit job_enqueue_main_executor from the swift_task_enqueueImpl path so main actor enqueues appear in traces.
* Add TracingExecutorKind enum and log executorKind in job_enqueue_executor and job_run to distinguish global, default actor, main actor, custom serial executor, and task executor contexts.

While we're here, finally add a test for Concurrency signposts. This is difficult because the signposts are disabled by default and there isn't a good way to turn them on in an automated fashion. Resolve this by adding an environment variable, SWIFT_CONCURRENCY_TRACING_SUBSYSTEM, which overrides the subsystem used by the Concurrency signpost code. The test can use this to set a subsystem that isn't disabled by default, which then allows `log stream` to capture the signposts.
2026-03-26 15:10:48 -04: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
Saleem Abdulrasool db160d5e81 runtime: tweak the environment handling
We would previously enable "environment variables" on Android via global
properties. Re-order the macros a bit to make it more explicit that the
environment handling is required for that and remove it when environment
variables are unsupported.
2025-02-07 09:26:49 -08:00
Andrew Rogers 63dd1997c0 Add comment explaining debug. propname prefix 2024-12-18 10:07:23 -08:00
Andrew Rogers b2fb2f465c PR feedback: rename propValueString to buffer 2024-12-18 10:02:56 -08:00
Andrew Rogers feea6aaefe PR feedback 2024-12-17 11:54:02 -08:00
Andrew Rogers 0192aa4728 [android] also load debug env vars from system properties 2024-12-17 10:24:57 -08:00
Saleem Abdulrasool 6716c0428e runtime: re-order environment variable lookup
Hoist the no environment case over the case with the environment. This
ensures that if no environment configuration is selected, the code still
builds. If no environment configuration is selected, `ENVIRON` may still
be defined and that results in calls to elided functions.
2024-12-12 10:39:01 -08:00
Yuta Saito 650e90a2b0 [Runtime] Repair build of getenv mode in EnvironmentVariables.cpp
Follow-up fix to 4b3a197dc2
2024-08-03 08:36:36 +00: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 b2bd17635f [Concurrency] Fix too eager early return in checkIsolation mode detecting (#73495) 2024-05-09 03:12:37 -07: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
Valeriy Van 3e85cc4862 [runtime] Fix incorrect format specifier 2023-02-02 14:04:25 +02: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
Alastair Houghton f5bdb858e0 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen 4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alastair Houghton 63a09007a1 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-05-24 14:57:39 +01: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
Kuba (Brecka) Mracek c2c1bfb053 Add SWIFT_STDLIB_HAS_ENVIRON to remove usage of getenv/environ from stdlib (#39599) 2021-10-12 07:16:36 -07:00
Saleem Abdulrasool f0f81f75df runtime: annotate _environ DLL storage, exclude in WinRT
`_environ` is meant to be DLL imported when linking against the C
runtime dynamically (`/MD` or `/MDd`).  However, when building for
the Windows Runtime environment, we cannot support the use of `_environ`
and thus disable the support for that.  `_WINRT_DLL` identifies the
combination of `/ZW` and `/LD` or `/LDd`.  Windows Runtime builds cannot
be executables (and obviously not static libraries as they are not
executable), and thus we properly disable `ENVIRON` in all Windows
Runtime builds.
2021-06-06 19:40:02 -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
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
3405691582 a9007790b1 [stdlib][test] Fix message test expectation.
In #32137 direct environment variable parsing was introduced, the
availability of which is branched by a preprocessor symbol (`ENVIRON`)
if the environment is available directly on the platform, or if getenv
must be used.

The message expectation in the unit test in the however diverged on the
non-`ENVIRON` branch, causing a unit test failure. This PR fixes the
expectation, but also, while we're here, OpenBSD supports the `ENVIRON`
branch anyway.
2020-06-05 00:12:17 -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