Commit Graph

210 Commits

Author SHA1 Message Date
Max Desiatov 5b97cb7c1f Fix KeyPath with 16-byte-aligned subscript traps on 32-bit targets (#88725)
- **Explanation**: Fixes a crash with key paths on 32-bit platforms reproducible for types that have 16-byte alignment.
The intended bit layout of `ComputedArgumentSize` in `KeyPath` on 32-bit is:
```
 ┌───────┬───────────┐
 │ bits  │   field   │
 ├───────┼───────────┤
 │ 0–27  │ size      │
 ├───────┼───────────┤
 │ 28–29 │ padding   │
 ├───────┼───────────┤
 │ 30–31 │ alignment │
 └───────┴───────────┘
```
Currently, `alignmentMask = 0x6000_0000`, i.e. bits 29–30, not 30–31. It overlaps paddingMask (bits 28–29) at bit 29, meaning that alignment and padding unintentionally share a bit. With `alignmentShift = 30`, storing `shift = 2 << 30` places 1 at bit 31, which the mask doesn't cover.

Correct value is `0xC000_0000` covers bits 30–31, which matches `alignmentShift = 30` so both `shift = 1` and `shift = 2` round-trip, and it does not overlap with `paddingMask = 0x3000_0000` (bits 28–29). It also mirrors the 64-bit layout (top bits of the word reserved for alignment, just 2 of them instead of 1).
- **Scope**: Limited to 32-bit platforms.
- **Issues**: rdar://175799967
- **Risk**: Low due to increased test coverage.
- **Testing**: Previously crashing on 32-bit platforms sample code is now added to the test suite.
2026-04-29 20:25:16 -07:00
indextrown 2276f4e466 [stdlib] Fix documentation typo in KeyPath.swift 2026-04-30 00:53:42 +09:00
Doug Gregor e4512739a8 Fix indentation 2026-04-17 16:04:07 -07:00
Doug Gregor 453277eb74 Mark the various with* functions as @safe
Functions like withUnsafeBufferPointer are, by themselves, safe to
call. It's only the operations on the unsafe pointers passed into the
closure that are the safety issue.

This was the intent spelled out in SE-0458 but was not fully realized
in the library.

Fixes rdar://174519372.
2026-04-16 22:37:54 -07:00
Alejandro Alonso 714fd26830 Add the intermediate result for every leaf component 2025-10-22 13:00:55 -07:00
Alejandro Alonso ac376e97f2 Don't use SIMD because it may not exist 2025-09-24 09:55:12 -07:00
Alejandro Alonso aed77472e1 Update KeyPath.swift 2025-09-22 17:27:48 -07:00
Alejandro Alonso 465ed1d36d Update KeyPath.swift 2025-09-18 13:33:20 -07:00
Alejandro Alonso 4d622abf39 Always set the alignment bit, and accurately calculate appended leaf component sizes 2025-09-18 11:38:44 -07:00
Alejandro Alonso c4ab943d55 Prevent KeyPathBuffer.next from being inlined 2025-09-16 13:41:31 -07:00
Alejandro Alonso 1071451844 Force keypath objects to be 16 byte aligned 2025-09-16 10:04:25 -07:00
Alejandro Alonso 90fe494825 Update KeyPath.swift 2025-09-13 14:59:18 -07:00
Alejandro Alonso 13b5a44d95 Update KeyPath.swift 2025-09-13 14:59:18 -07:00
Alejandro Alonso b6b98b281f Update KeyPath.swift 2025-09-13 14:59:18 -07:00
Alejandro Alonso b7f4bb7918 Make ComputedArgumentSize unavailable in embedded 2025-09-13 14:59:18 -07:00
Alejandro Alonso 52a7051b77 Update KeyPath.swift 2025-09-13 14:59:17 -07:00
Alejandro Alonso f95e9e4ee4 Correctly handle alignment and appending keypaths
Add test for overaligned keypath arguments
2025-09-13 14:59:17 -07:00
Alejandro Alonso 1ac8e2c291 Handle arguments with alignment larger than a word in KeyPath 2025-09-13 14:59:15 -07:00
Gabor Horvath 402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00
Stephen Canon 337b20e8ed Remove some redundant unsafe markings (#83192)
We were getting warnings about these; remove them to cut down on noise.
2025-07-21 11:22:13 -04:00
Allan Shortlidge 008efc432f stdlib: Fix missing unsafe operators in more places.
Add `unsafe` where it is still missing. I missed these in previous passes due
to conditional compilation.
2025-06-22 18:46:57 -07:00
Saleem Abdulrasool c17e67e1a9 stdlib: handle unsafe annotations in additional places
This applies more annotations in the `INTERNAL_CHECKS_ENABLED` disabled
paths, Windows, 32-bit, and non-ObjC paths. Interestingly enough, there
are a couple of compiler intrinsics which are also uncovered.
2025-06-18 09:46:17 -07:00
Doug Gregor 050a514588 [Strict memory safety] Update standard library for unsafe treated as a call effect 2025-04-25 21:54:23 -07:00
Doug Gregor 457eb4cc64 [Strict memory safety] Update standard library for nested safe/unsafe types
Use this to mark a few internal types @safe now that it works properly.
2025-04-19 19:54:32 -07:00
Allan Shortlidge dbd3d40c07 stdlib: Address StrictMemorySafety warnings in KeyPath related code. 2025-03-31 16:45:08 -07:00
Evan Wilde ddaf003c56 Get stdlib building again
PR 79186 (https://github.com/swiftlang/swift/pull/79186) moved one of
the mandatory passes from the C++ implementation to the Swift
implementation resulting in a compiler that is unable to build the
standard library. The pass used to ensure that inaccessible control-flow
positions after an infinite loop was marked with `unreachable` in SIL.
Since the pass is no longer running, any function that returns a value
that also has an infinite loop internally must place a fatalError after
the infinite loop or it will fail to compile as the compiler will
determine that the function does not return from all control flow paths
even though some of the paths are unreachable.
2025-03-06 13:32:54 -08:00
Guillaume Lessard fab65890fb [stdlib] remove now-unnecessary unsafe annotations 2025-03-01 20:29:11 -08:00
Doug Gregor 22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Alejandro Alonso f38e84cfe1 Pass buffer to _projectReadOnly and fix appends 2025-02-05 14:33:30 -08:00
Alejandro Alonso 90bd2a008e Add maxSize helper to buffer and fix reroot 2025-02-04 16:21:09 -08:00
Alejandro Alonso edee13a612 Take the leaf type into account for max size 2025-02-04 15:16:40 -08:00
Alejandro Alonso 59c53b72f0 Don't do the kvc stored offset optimization on 16 bit platforms 2025-02-04 15:16:40 -08:00
Alejandro Alonso 02401d4bb0 Remove some workarounds 2025-02-04 15:16:39 -08:00
Alejandro Alonso 581376e86e Move variable into closure 2025-02-04 15:16:39 -08:00
Alejandro Alonso 25a4bbe549 Check maxSize on finish 2025-02-04 15:16:39 -08:00
Alejandro Alonso 6fdb684fd2 Optimization for single component and fix tests 2025-02-04 15:16:39 -08:00
Alejandro Alonso 00ace6695f Round up sizeWithMaxSize 2025-02-04 15:16:38 -08:00
Alejandro Alonso 4733da2744 Performance improvements for reading keypaths 2025-02-04 15:16:36 -08:00
Amritpan Kaur 1c494c4d89 [StdLib] Handle nil property descriptors. 2024-09-25 12:54:52 -07:00
nate-chandler bcd08c0c9a Merge pull request #73235 from nate-chandler/bitwise-copyable/enable
[BitwiseCopyable] Promote to feature.
2024-05-04 10:16:40 -07:00
Kuba Mracek 1d48f28d84 [KeyPath] Fix regression in == on keypaths 2024-04-30 12:50:03 -07:00
Nate Chandler b1fbe4ea91 [BitwiseCopyable] Remove underscore. 2024-04-25 11:44:15 -07:00
Joe Groff 02e1f2ea15 IRGen: Fix key path generic environment marshalling for external property descriptors.
The layout of a computed key path component carries an argument buffer for captures, which has
the following layout:

```
---
captured values (subscript indices)
---
generic arguments
---
```

When we reference an externally-defined public property or subscript from a key path, and the
external declaration has a property descriptor, then the generic arguments for the external
declaration get appended to the end of this buffer, giving:

```
---
captured values (subscript indices)
---
generic arguments
---
external property's generic arguments
---
```

The convention for key path accessors to bind their generic environment is thus to unpack them
from the end of the argument buffer, so that the external keypath's accessors can find the
arguments to bind the external generic environment while still allowing the enclosing key path
to save the original captured generic environment (which may be necessary to capture the
appropriate conditional and/or retroactive `Equatable` and `Hashable` conformances for
subscript indices).

However, our code generation for binding the generic arguments out of the argument buffer
contained a flawed optimization: for a property, we know there are never any captured values,
so I had assumed that the generic parameters could always be bound from the beginning of the
argument buffer, assuming that the generic parameters make up the totality of the buffer. This
falls over for external property descriptor references when the key path itself captures a
generic environment, since the external property's expected generic environment appears after
the key path's original generic environment. We can fix this by removing the conditional
entirely, and always adjusting the offset we load the generic environment from to look at the
end of the buffer. Fixes rdar://125886333.
2024-04-25 10:36:13 -07:00
Alejandro Alonso e71a55eb19 Print a special message when we fail getting a keypath value 2024-04-03 15:35:55 -07:00
Kuba Mracek b642d771be [embedded] Compile-time (literal) KeyPaths for Embedded Swift
Enable KeyPath/AnyKeyPath/PartialKeyPath/WritableKeyPath in Embedded Swift, but
for compile-time use only:

- Add keypath optimizations into the mandatory optimizations pipeline
- Allow keypath optimizations to look through begin_borrow, to make them work
  even in OSSA.
- If a use of a KeyPath doesn't optimize away, diagnose in PerformanceDiagnostics
- Make UnsafePointer.pointer(to:) transparent to allow the keypath optimization
  to happen in the callers of UnsafePointer.pointer(to:).
2024-03-20 15:35:46 -07:00
Nate Chandler 5a2d1621ec [KeyPath] Restrict push and pop to BitwiseCopyable
Promotes the runtime isPOD check to a compile time constraint.
2024-02-14 13:02:09 -08:00
Guillaume Lessard 0c916f507d [stdlib] replace uses of String(validatingUTF8:)
- use the new name `String(validatingCString:)`
2023-09-11 14:17:05 -07:00
Alejandro Alonso 141fa804b1 Make _createOffsetBasedKeyPath and _rerootKeyPath only available with reflection 2023-06-26 23:09:39 -07:00
Alejandro Alonso d9d26eec9d Add support for classes in _createOffsetBasedKeyPath 2023-06-22 14:07:41 -07:00
Alejandro Alonso 3360e41c9c Add SPI to reroot a keypath for a given superclass 2023-04-24 17:15:28 -07:00