Commit Graph

73 Commits

Author SHA1 Message Date
Anthony Latsis
311ff73a7f runtime: Silence a -Wglobal-constructors error than appears with upstream clang on Windows
Landing this on main to minimize rebranch-specific changes.
2025-08-13 00:32:23 +01:00
Saleem Abdulrasool
2349a4fb9d Update LibcShims.cpp
Add missing include for `atexit`.
2023-09-06 19:37:50 -07:00
YR Chen
e39babe1c9 Fix building LibcShims.cpp with MSVC v14.37
`_swift_stdlib_configure_console_mode` reverts console codepage on Windows using `atexit`, which is declared in `stdlib.h`.

Add the missing `#include <stdlib.h>` to unblock building Swift `stdlib` with MSVC v14.37 (Visual Studio 17.7).
2023-08-09 17:32:07 +08:00
Egor Zhdan
84a1ffcb33 [Shims] Include SwiftShims headers without ../
This replaces a number of `#include`-s like this:
```
#include "../../../stdlib/public/SwiftShims/Visibility.h"
```
with this:
```
#include "swift/shims/Visibility.h"
```

This is needed to allow SwiftCompilerSources to use C++ headers which include SwiftShims headers. Currently trying to do that results in errors:
```
swift/swift/include/swift/Demangling/../../../stdlib/public/SwiftShims/module.modulemap:1:8: error: redefinition of module 'SwiftShims'
module SwiftShims {
       ^
Builds.noindex/swift/swift/bootstrapping0/lib/swift/shims/module.modulemap:1:8: note: previously defined here
module SwiftShims {
       ^
```
This happens because the headers in both the source dir and the build dir refer to SwiftShims headers by relative path, and both the source root and the build root contain SwiftShims headers (which are equivalent, but since they are located in different dirs, Clang treats them as different modules).
2022-09-14 11:14:50 +01:00
Kuba (Brecka) Mracek
6fe3ccc47d Drop read/write/close LibcShims and use the platform APIs directly from the SwiftPrivate module (#40211) 2022-02-08 15:32:49 -08:00
Saleem Abdulrasool
ca39bb5b10 stubs: avoid reaching up-tree, prefer inclusion options
This removes the explicit tree structure reference in the stubs to
locate the shims.  Instead, it expects that the `SwiftShims` directory
will be added to the header search path.
2022-01-14 09:34:56 -08:00
Saleem Abdulrasool
9847cab8d3 stdlib: change the console to UTF-8 on start
This adjusts the Windows console to switch the codepage to UTF-8.  This
is important as the default codepage (CP437) does not allow for UTF-8
output, but expects ASCII.  However, strings in Swift are assumed to be
UTF-8, which means that there is now a conversion mismatch.

Because the console mode persists beyond the duration of the application
as it is state local to the console and not the C runtime, we should
restore the state of the console before termination.  We do this by
registering a termination handler via `atexit`.  This means that an
abnormal termination (e.g. via `fatalError`) will irrevocably alter the
state of the console (interestingly enough, `chcp` will still report the
original console codepage even though the console will internally be set
to UTF-8).

Fixes: SR-13807
2021-12-20 07:14:01 -08:00
Kuba (Brecka) Mracek
500c643dcb Remove _swift_stdlib_putchar_unlocked (#39383) 2021-09-21 18:00:03 -07:00
zoecarver
3e4ddb13d6 [NFC] Remove __swift_mode_t.
Removes the "__swift_mode_t" type from SwiftShims. This type is no longer used anywhere.
2020-07-10 08:38:10 -07:00
Zoe Carver
7eff49c1b6 [cxx-interop] [nfc] Remove swift namespace from SwiftShims in C++ mode. (#32715)
Most SwiftShims were put in the swift namespace in C++ mode which broke certain things when importing them in a swift file in C++ mode. This was OK when they were only imported as part of the swift runtime but, now they are used in C++ mode both in the swift runtime and when C++ interop is enabled.

This broke when C++ interop was enabled because the `Swift` module contains references to symbols in the SwiftShims headers which are built without C++ interop enabled (no "swift" namespace). But, when C++ interop is enabled, the SwiftShims headers would put everything in the swift namespace meaning the symbols couldn't be found in the global namespace. Then, the compiler would error when trying to deserialize the Swift module.
2020-07-08 08:43:26 -07:00
Max Desiatov
44a6475566 [WebAssembly] enable shims and stubs for WASI (#29505)
* [WebAssembly] enable shims and stubs for WASI

* Implement _swift_stdlib_getUnsafeArgvArgc for WASI

* Make include unconditional, add JIRA links
2020-01-28 18:29:52 -08:00
Saleem Abdulrasool
8b4c14b71e stdlib: conditionalise unistd.h
unistd.h is the standard Unix header.  The standard library may be built
for Windows, which is not a Unix.  Guard the inclusion.
2018-11-07 11:10:59 -08:00
Mike Ash
45693049bf [Stdlib] Remove SwiftPrivate's dependency on Darwin.
Revert #20194, which seems to be more trouble than it's worth. Instead, move the functions that SwiftPrivate needs back into LibcShims.h/cpp as SPI.

rdar://problem/45817565
2018-11-06 17:41:56 -05:00
Saleem Abdulrasool
92ef7e46dc stdlib: use some clever C11 to define ssize_t
`ssize_t` is not universally available as it is not a standard type.
However, we require clang to build the runtime due to custom calling
convention support.  As a result, we know that the compiler will support
the `_Generic` keyword from the C11 standard.  Use this with an
expansion to map the type appropriately for all targets.  This avoids
having to have a sanity check in the runtime that the type definition
matches the underlying system.
2018-10-08 11:49:02 -07:00
Mike Ash
5391abbf4b [Stdlib] Clean up includes in LibcShims and Random. 2018-10-03 11:16:37 -04:00
Mike Ash
5f17b450c3 [Stdlib] Make all the functions in LibcShims.h either INTERNAL or inline. Move LibcShimsInline.h to LibcOverlayShims.h for more consistent naming. Fix up several tests that needed the mock Darwin overlay built. Fix one SourceKit test that no longer produces is_system: 1 on an import Darwin line. 2018-10-03 09:55:34 -04:00
Mike Ash
ec25f928ff [Stdlib] Move _swift_stdlib_read/write/close into LibcShimsInline.h. 2018-10-03 09:55:34 -04:00
Mike Ash
b2675280c7 [Stdlib] Move the shims used by Platform.swift into a separate header and make them static inline. This avoids exposing them as exported symbols in libswiftCore. 2018-10-03 09:55:34 -04:00
Mike Ash
6926fbd363 [Stdlib] Pull _swift_stdlib_random into a separate file. 2018-10-03 09:55:34 -04:00
Mike Ash
e18e03171f [Stdlib] Change SWIFT_RUNTIME_STDLIB_INTERNAL to not export the symbol.
The functions in LibcShims are used externally, some directly and some through @inlineable functions. These are changed to SWIFT_RUNTIME_STDLIB_SPI to better match their actual usage. Their names are also changed to add "_swift" to the front to match our naming conventions.

Three functions from SwiftObject.mm are changed to SPI and get a _swift prefix.

A few other support functions are also changed to SPI. They already had a prefix and look like they were meant to be SPI anyway. It was just hard to notice any mixup when they were #defined to the same thing.

rdar://problem/35863717
2018-10-03 09:55:33 -04:00
Alejandro
054adb3b3e [Stubs] _stdlib_random: Try to use direct syscall to getrandom (#19309)
Instead of only requiring Glibc's implementation of getrandom, try to use a direct syscall to getrandom. This should allow more versions of linux to utilize getrandom instead of always having to fallback to reading /dev/urandom.
2018-09-25 09:55:19 -04:00
Mike Ash
29ff3de4ab [Runtime][Stdlib] Use dedicated thread-local storage keys when possible.
rdar://problem/32275323 rdar://problem/44104305
2018-09-13 14:36:57 -04:00
Ben Rimmington
2f326bcc88 [stdlib] Remove theGlobalMT19937
SwiftPrivate/PRNG.swift:

- currently uses `theGlobalMT19937`;
- previously used `arc4random` (see #1939);
- is obsoleted by SE-0202: Random Unification.
2018-08-01 13:00:16 +01:00
Susan Cheng
9b61888046 getrandom introduced in Android API 28 2018-07-31 10:42:10 +08:00
Azoy
b8fc8b333c Remove _interface 2018-07-29 10:41:22 -04:00
Nate Cook
7a4e0a32f6 [stdlib] Revise documentation
This includes various revisions to the APIs landing in Swift 4.2, including:
- Random and other randomness APIs
- Hashable changes
- MemoryLayout.offset(of:)
2018-05-18 11:31:54 -05:00
Mike Ash
0a6ea9733c Merge pull request #16281 from mikeash/warning-fix-libcshims-metadata
[Runtime] Fix warnings for inconsistent nullability annotation on _stdlib_getEnviron.
2018-05-10 16:13:02 -04:00
Mike Ash
022253e1e5 [Runtime] Fix warnings for inconsistent nullability annotation on _stdlib_getEnviron.
rdar://problem/39739801
2018-05-09 13:41:58 -04:00
Ben Rimmington
b65d0c1d11 Consolidate _stdlib_random functions (#2)
* Use the `__has_include` and `GRND_RANDOM` macros

* Use `getentropy` instead of `getrandom`

* Use `std::min` from the <algorithm> header

* Move `#if` out of the `_stdlib_random` function

* Use `getrandom` with "/dev/urandom" fallback

* Use `#pragma comment` to import "Bcrypt.lib"

* <https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp>
* <https://clang.llvm.org/docs/UsersManual.html#microsoft-extensions>

* Use "/dev/urandom" instead of `SecRandomCopyBytes`

* Use `swift::StaticMutex` for shared "/dev/urandom"

* Add `getrandom_available`; use `O_CLOEXEC` flag

Add platform impl docs

Update copyrights

Fix docs

Add _stdlib_random test

Update _stdlib_random test

Add missing &

Notice about _stdlib_random

Fix docs

Guard on upperBound = 0

Test full range of 8 bit integers

Remove some gyb

Clean up integerRangeTest

Remove FixedWidthInteger constraint

Use arc4random universally

Fix randomElement

Constrain shuffle to RandomAccessCollection

warning instead of error

Move Apple's implementation

Fix failing test on 32 bit systems
2018-05-09 00:21:44 -05:00
Ben Rimmington
a5df0ef83d Add _stdlib_random for more platforms (#1)
* Remove refs to Countable ranges

* Add `_stdlib_random` for more platforms

* Use `getrandom` (if available) for Android, Cygwin

* Reorder the `_stdlib_random` functions

* Also include <features.h> on Linux

* Add `#error TODO` in `_stdlib_random` for Windows

* Colon after Fatal Error

Performance improvement for Random

gybify ranges

Fix typo in 'basic random numbers'
Add _stdlib_random as a testable method

Switch to generic constraints

Hopefully link against bcrypt

Fix some implementation details

1. Uniform distribution is now uniform
2. Apply Jens' method for uniform floats

Fix a lineable attribute
2018-05-04 21:03:38 -05:00
Azoy
d23d219e95 Add shims for stdlib random
Initial random api

Use C syscall for I/O

1. Fixed an issue where integers would would result in an infinite loop if they were unsigned, or signed integers always returning negative numbers.
2. Fixed an issue with Bool initialization

Add shuffle functions

Add documentation to Random API

Fix a few typos within the documentation

Fixes more typos

Also states that the range for floating points is from 0 to 1 inclusive

Update API to reflect mailing list discussions

Remove unnecessary import

Make sure not to return upperBound on Range

Use SecRandomCopyBytes on older macOS

Update API to match mailing list discussion, add tests

Added pick(_:) to collection
Added random(in:using:) to Randomizable
Added tests

Fix typo in Randomizable documentation

Rename pick to sampling

Move sampling below random

Update docs

Use new Libc naming

Fix Random.swift with new Libc naming

Remove sampling

gybify signed integer creation

Make FloatingPoint.random exclusive

Refactor {Closed}Range.random

Fix FloatingPoint initialization

Precondition getting a random number from range

Fix some doc typos

Make .random a function

Update API to reflect discussion

Make .random a function
Remove .random() in favor of .random(in:) for all numeric types

Fix compile errors

Clean up _stdlib_random

Cleanup around API

Remove `.random()` requirement from `Collection`

Use generators

Optimize shuffle()

Thread safety for /dev/urandom

Remove {Closed}Range<BinaryFloatingPoint>.random()

Add Collection random requirement

Refactor _stdlib_random

Remove whitespace changes

Clean linux shim

Add shuffle and more tests

Provide finishing tests and suggestions

Remove refs to Countable ranges

Revert to checking if T is > UInt64
2018-05-04 21:02:57 -05:00
Stephen Canon
ddf70fa199 Cleanup tgmath wrappers. (#15304)
* Cleanup tgmath wrappers.

- Remove special-case gyb logic for lgamma on Darwin; the symbols we need are always present, even if not visible in the headers, so we only need a prototype.
- Add some deprecations for symbols that have direct stdlib analogues.
- Make some operations generic on [Binary]FloatingPoint, where they can map to the protocols instead of calling libm.
- Mark ldexp(Float/Double) renamed to scalbn; for binary formats these are identical functions, and we don't really want to use these names for hypothetical future Decimal support, as they're not Swifty.
2018-03-17 10:32:13 -04:00
Thomas Roughton
b4391f35de Fix comment on #endif for defined(_Win32) 2017-12-04 11:39:22 +13:00
troughton
a1518031ec Fix Windows errno() and _stdlib_open 2017-12-04 11:12:11 +13:00
Greg Parker
da14cd79a6 [runtime] Clean up symbol exports in libc functions. (#13202) 2017-12-01 17:49:11 -08:00
Saleem Abdulrasool
13213efe6a stubs: fix architecture check for Windows
Fix a typo in the architecture check.  The missing `I` in the x86 check
prevented the x86 build from succeeding.
2017-09-26 16:11:34 -07:00
Calvin Hill
aee81d272f Add Initial platform support for Haiku. (#11583) 2017-09-22 21:06:56 -04:00
Saleem Abdulrasool
42c98e63d0 stubs: fix Windows x86 TLS callback CC violation
Windows x86 requires that the TLS destructor callback uses the "stdcall"
calling convention.  Provide a shim which will adjust the calling
convention and call back into the swift portion of the code code with
the expected calling convention.
2017-09-21 11:29:56 -07:00
Saleem Abdulrasool
ba8cbe137c stdlib: generalise TLS to support Windows
Rename the explicit `pthread` to `thread` to indicate that this is not
`pthread` specifically.  This allows us to implement the TLS support for
Windows using Fibers.
2017-09-15 10:56:50 -07:00
Michael Ilseman
ebe391cf5d [stdlib stubs] Fix up Linux build to know about pthread types.
Adds in Linux platform support for our pthread TLS. Replace usage of
PTHREAD_KEYS_MAX with a sentinel value, as it's tricky to define
cross-platform and was only lightly used inside sanity checks.
2017-05-16 20:30:02 -07:00
Michael Ilseman
5eb5e34897 [stdlib] Shims for UBreakIterator and thread local storage.
Introduce shims for using UBreakIterators from ICU. Also introduce
shims for using thread local storage via pthreads.

We will be relying on ICU and UBreakIterators for grapheme
breaking. But, UBreakIterators are very expensive to create,
especially for the way we do grapheme breaking, which is relatively
stateless. Thus, we will stash one or more into thread local storage
and reset it as needed.

Note: Currently, pthread_key_t is hard coded for a single platform
(Darwin), but I have a static_assert alongside directions on how to
adapt it to any future platforms who differ in key type.
2017-05-16 20:28:31 -07:00
Michael Ilseman
f0abff5539 Revert "Merge pull request #9265 from milseman/tls_ftw"
This reverts commit 26f7659efe, reversing
changes made to 7b927e55e8.
2017-05-11 10:39:58 -07:00
Michael Ilseman
75741a9dde [stdlib stubs] Fix up Linux build to know about pthread types.
Adds in Linux platform support for our pthread TLS. Replace usage of
PTHREAD_KEYS_MAX with a sentinel value, as it's tricky to define
cross-platform and was only lightly used inside sanity checks.
2017-05-10 15:23:12 -07:00
Michael Ilseman
4a17449d02 [stdlib] Shims for UBreakIterator and thread local storage.
Introduce shims for using UBreakIterators from ICU. Also introduce
shims for using thread local storage via pthreads.

We will be relying on ICU and UBreakIterators for grapheme
breaking. But, UBreakIterators are very expensive to create,
especially for the way we do grapheme breaking, which is relatively
stateless. Thus, we will stash one or more into thread local storage
and reset it as needed.

Note: Currently, pthread_key_t is hard coded for a single platform
(Darwin), but I have a static_assert alongside directions on how to
adapt it to any future platforms who differ in key type.
2017-05-10 15:21:07 -07:00
Stephen Canon
d89f8f423e Go back to using static inline stubs for sqrt and remainder (#6769)
* Go back to using static inline implementations of sqrt and remainder now that SR-2089 is resolved.

* Fix typo: sqrt -> squareRoot.

* Added test for constant-folding sqrt with -O.

* Added test case requested by jrose.
2017-01-13 13:26:46 -05:00
Max Moiseev
195691523f [stdlib] Eliminating some 'unsafeBitcast' related warnings 2017-01-10 12:36:22 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Hugh Bellamy
eca5b3da34 Change _MSC_VER conditions to _WIN32 conditions in stubs 2016-12-13 14:15:39 +00:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Saleem Abdulrasool
7f088d3fe0 stdlib: mark shims with the visibility attribute
Ensure that the header and the implementation matches the ABI interface
annotations.
2016-10-18 11:36:15 -07:00