Commit Graph

110 Commits

Author SHA1 Message Date
Cory Benfield
9cb3641ce8 Remove checks in UR[M]BP.Iterator.next() (#62965)
Swift tends to emit unnecessary checks and traps when iterating unsafe
raw buffer pointers. These traps are confirming that the position
pointer isn't nil, but this check is redundant with the bounds check
that is already present. We can safely remove it.
2023-01-12 11:04:42 -05:00
Guillaume Lessard
2689b6044b Merge pull request #41608 from glessard/se-pointer-family-initialization 2022-09-13 22:00:59 -06:00
Guillaume Lessard
1152347699 [stdlib] remove primary associated type from copyBytes
- this caused an unexpected test failure, will change separately.
2022-09-12 14:18:47 -06:00
Guillaume Lessard
ffa7b0e78c [se-0370] add notes regarding overlapping memory regions 2022-09-08 18:23:45 -06:00
Erik Eckstein
9c1e7cac0f stdlib: fix compilation error when compiling with an old compiler
Fix wrong/missing check for `$BuiltinUnprotectedAddressOf` around the new "unprotectedAddressOf" builtins.

rdar://99713099
2022-09-08 23:05:17 +02:00
Erik Eckstein
0f8dd3a551 stdlib: opt out of stack protection in places where there can't be buffer overflows.
We trust the internal implementation of the stdlib to not cause any unintentional buffer overflows.
In such cases we can use the "unprotected" address-to-pointer conversions.
This avoids inserting stack protections where it's not needed.
2022-09-08 08:42:25 +02:00
Erik Eckstein
ba135dc493 stdlib: add underscored "withUnprotected..Pointer" functions
Those functions work the same way as their "unprotected" counterparts, except that they don't trigger stack protection for the pointers.

* `_withUnprotectedUnsafeMutablePointer`
* `_withUnprotectedUnsafePointer`
* `_withUnprotectedUnsafeBytes` (two times)
2022-09-08 08:42:25 +02:00
Guillaume Lessard
44a349f8e0 [se-0370] edit doc-comments for consistency 2022-09-07 15:22:49 -06:00
Guillaume Lessard
88225a2584 [se-0370] some consistency tweaks 2022-09-07 10:07:10 -06:00
Guillaume Lessard
6512840469 [stdlib] update copyBytes with primary associated type 2022-09-07 10:07:04 -06:00
Guillaume Lessard
c2c432c9d2 [se-0370] re-implement in accordance with updated proposal 2022-08-30 11:05:41 -06:00
Guillaume Lessard
f5ffe09c53 [se-0370] fix editing errors 2022-08-26 17:36:40 -06:00
Guillaume Lessard
26e5c43aa3 [se-0370] update documentation to track proposal
for the functions involving `fromContentsOf:`.
2022-08-26 17:36:40 -06:00
Guillaume Lessard
00aaa07daf [stdlib] improve doc-comments 2022-08-26 17:36:40 -06:00
Guillaume Lessard
831a87fd36 [stdlib] make additions non-abi 2022-08-26 17:36:40 -06:00
Guillaume Lessard
2e3b5e6655 [stdlib] more memory initialization functions for UMRBP 2022-08-26 17:36:40 -06:00
Guillaume Lessard
92087a8f62 [stdlib] add explanations to _debugPrecondition calls 2022-08-18 15:37:25 -06:00
Guillaume Lessard
a74f22dadf [docs] fix a repeated typo 2022-06-29 18:19:34 -06:00
Guillaume Lessard
e353e2f568 [stdlib] document compatibility entry point silgen names better 2022-06-29 16:21:55 -06:00
Guillaume Lessard
6d36a36dd9 [stdlib] improve silgen names and better document intention 2022-06-29 16:20:34 -06:00
Karoy Lorentey
35fc9ea772 Merge pull request #59117 from glessard/se0349-doctweak
[doc] mention `loadUnaligned` in the description of `UnsafeRawBufferPointer`
2022-06-10 11:00:59 -07:00
Guillaume Lessard
0895934303 [docs] fix a parameter description for storeBytes 2022-06-01 09:04:43 -06:00
Guillaume Lessard
11138424a7 [docs] clarify docs for load, loadUnaligned and storeBytes
- outdated and incorrect information removed
2022-05-31 15:55:09 -06:00
Guillaume Lessard
d6165cd83d [doc] mention loadUnaligned in URBP description 2022-05-26 14:09:00 -06:00
Guillaume Lessard
a368e59564 [stdlib] incorporate proposal number in silgen name 2022-04-18 12:50:19 -06:00
Guillaume Lessard
1828aca9f0 [stdlib] update UnsafeMutableRawBufferPointer.storeBytes
- preserve previous version for ABI and source stability.
- add new version without alignment restriction.
- add explicit POD type enforcement in new version.
2022-04-14 17:23:53 -06:00
Guillaume Lessard
4b3e7fa41f [stdlib] add unaligned loads to Unsafe{Mutable}RawBufferPointer 2022-04-14 17:23:53 -06:00
Alex Martini
395a4b29be Add missing parameter.
Here, as in UnsafeMutableRawPointer.storeBytes(of:toByteOffset:as:)
"as" is an argument label and "type" is the parameter.

Because the function body doesn't use this -- it's just for type
information -- changing its name from "as" to "type" doesn't have
any impact there.
2022-04-11 16:50:01 -07:00
Guillaume Lessard
b17b1a9d04 Merge pull request #41836 from glessard/sr15994
[stdlib] tolerate empty source buffers in `UMRBP.copyMemory`
2022-03-16 16:24:44 -06:00
Guillaume Lessard
e40e0f7580 [stdlib] improve UnsafeMutableRawBufferPointer.copyMemory
- The previous implementation trapped when the source buffer was empty. That behaviour is both not documented and unnecessary. If the source buffer is empty, its length is necessarily shorter or equal than the length of the destination.
- The updated version simply returns when the source buffer is empty.
2022-03-16 10:05:21 -06:00
Alex Martini
fa04ef570d Add missing parameter in doc comment.
This text matches what's in the doc comment for
UnsafeMutableRawPointer.storeBytes(of:toByteOffset:as:)

Fixes rdar://88982081
2022-02-15 16:49:07 -08:00
Guillaume Lessard
441e3ce4e3 [stdlib] add withContiguousStorageIfAvailable for raw buffers 2022-02-08 18:05:02 -07:00
Guillaume Lessard
1161f92bd7 Merge pull request #39529 from glessard/se-withMemoryRebound 2022-02-05 09:01:05 -07:00
Karoy Lorentey
8533b4295b Merge pull request #37424 from Lukasa/cb-fewer-checks-in-ubp
Remove as much checked math as possible from buffer pointers.
2022-02-04 10:40:24 -08:00
Guillaume Lessard
245426493c [stdlib] documentation clarifications 2022-02-03 20:02:33 -07:00
Guillaume Lessard
c24832677d [stdlib] add U[M]RBP.assumingMemoryBound 2022-02-03 20:02:33 -07:00
Guillaume Lessard
5403129588 [stdlib] add U[M]RBP.withMemoryRebound 2022-02-03 20:02:33 -07:00
Andrew Trick
47cc279960 Unsafe*Pointer types should not be Sendable.
To send them across actors, they need to be wrapped in an '@unchecked
Sendable' type. Typically such a wrapper type would be be responsible
for ensuring its uniqueness or immutability.

Inferring Sendability for arbitrary types that contain Unsafe*Pointers
would introduce race conditions without warning or any explicit
acknoledgement from the programmer that the pointer is in fact unique.
2021-12-06 15:52:50 -08:00
Guillaume Lessard
8eea64271a Merge pull request #39824 from glessard/urbp-copycontents-optimization
[stdlib] a minor optimization
2021-10-21 11:39:43 -06:00
Guillaume Lessard
4d05b759b2 [stdlib] a minor optimization
We have already done the hard work of unwrapping the optionals
that represent the first and last pointer, and calling `self.count` does it all over again. Use the distance between the unwrapped pointers instead.
2021-10-15 11:46:58 -06:00
Karoy Lorentey
dd40ff2929 [stdlib][NFC] Put stored properties & primary initializers before other members in struct declarations 2021-10-05 22:01:35 -07:00
Guillaume Lessard
af036db7bd [stdlib] implement _copyContents for UnsafeRawPointer 2021-08-10 15:35:07 -06:00
Guillaume Lessard
9457406d0b Merge pull request #38463 from wongzigii/SR-14886
Check withContiguousStorageIfAvailable before copying byte by byte
2021-07-30 09:40:09 -06:00
ZigiiWong
26b3c55c23 [SR-14886] Check wCSIA before copying byte by byte. 2021-07-30 11:04:15 +08:00
Guillaume Lessard
ae10c297a9 make documentation of UMRBP.allocate() more complete. 2021-07-16 12:33:24 -06:00
Guillaume Lessard
7f68d6a4f5 document the requirement that allocation alignments must be powers of two. 2021-07-15 19:19:15 -06:00
Cory Benfield
b1683c89be Remove as much checked math as possible from buffer pointers.
This patch removes as much checked math as I was able to find from the
unsafe buffer pointers types. In particular, I removed two kinds of
checked math:

1. Checked math that was provably unable to trap due to prior operations
   done in the same function, or in a function that must have been
   called before the current one. For example, in
   index(_:offsetBy:limitedBy:), when we do the final index addition we
   have already validated that it must be in-bounds. Therefore, it
   cannot overflow.
2. Checked math that can only fail if the user is misusing the indices.
   As previously discussed with Karoy and Andrew, the unsafe raw buffer
   types are not obligated to crash if you misuse their indices, they
   are free to invoke undefined behaviour. In these cases, I added
   defensive overflow checks in debug mode.

The result of this change should be reductions in code size in almost
all usage sites of the raw buffer types.
2021-05-21 09:25:32 +01:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07:00
Doug Gregor
1a1f79c0de Introduce safety checkin for ConcurrentValue conformance.
Introduce checking of ConcurrentValue conformances:
- For structs, check that each stored property conforms to ConcurrentValue
- For enums, check that each associated value conforms to ConcurrentValue
- For classes, check that each stored property is immutable and conforms
  to ConcurrentValue

Because all of the stored properties / associated values need to be
visible for this check to work, limit ConcurrentValue conformances to
be in the same source file as the type definition.

This checking can be disabled by conforming to a new marker protocol,
UnsafeConcurrentValue, that refines ConcurrentValue.
UnsafeConcurrentValue otherwise his no specific meaning. This allows
both "I know what I'm doing" for types that manage concurrent access
themselves as well as enabling retroactive conformance, both of which
are fundamentally unsafe but also quite necessary.

The bulk of this change ended up being to the standard library, because
all conformances of standard library types to the ConcurrentValue
protocol needed to be sunk down into the standard library so they
would benefit from the checking above. There were numerous little
mistakes in the initial pass through the stsandard library types that
have now been corrected.
2021-02-04 03:45:09 -08:00
Karoy Lorentey
282a1408de [stdlib] U[R]BP: Restore compile-time condition lost with removed precondition 2020-12-08 22:25:16 -08:00