Commit Graph

3149 Commits

Author SHA1 Message Date
Dario Rexin
011fce8212 [Runtime] Fix leak of bridge objects in swift_generic_initWithTake (#69977)
rdar://118606044

The initWithTakeTable accidentally referenced bridgeRetain instead of copyingInitWithTake, which caused a leak when an object containing a bridge reference was also not bitwise takable.
2023-11-27 08:04:10 -08:00
Dario Rexin
506fbb3c91 [Runtime] Fix alignment of tuples in runtime layout string instantiation (#69975)
rdar://118366415

If a tuple was not already aligned, this would cause a wrong offset to be used in the layout string.
2023-11-27 08:03:38 -08:00
Finagolfin
d9d6356f3b [Backtracing][Android] Fix armv7 build 2023-11-16 20:26:09 +05:30
Alastair Houghton
b693cce3b8 Merge pull request #69859 from al45tair/eng/PR-118402226
[Backtracing] Fix ARM64e build failure.
2023-11-15 11:14:13 +00:00
Alastair Houghton
5c427124ae [Backtracing] Fix ARM64e build failure.
Accessing the thread context structures is complicated because their
member variables change name depending on various macros.  This caused
a build failure for ARM64e.

rdar://118402226
2023-11-14 17:37:00 +00:00
Alastair Houghton
1667378e73 [Backtracing] Make sure we print the program counter on ARM64 linux.
On Linux, the right macro for detecting ARM64 is `__aarch64__`, not
`__arm64__`.

rdar://118379429
2023-11-14 08:27:24 +00:00
Alastair Houghton
2c8bd79bf0 Merge pull request #69697 from al45tair/eng/PR-118055527
[Backtracing] Add an indication that we're working on a backtrace.
2023-11-13 17:54:43 +00:00
Tim Kientzle
4e821f65ec Merge pull request #69464 from tbkka/tbkka-objc-equatable-hashable
Use Swift Equatable and Hashable conformances from ObjC
2023-11-10 13:46:19 -08:00
Dario Rexin
36f3367275 [Runtime] Use threaded code in compact value witness runtime (#69756)
* [Runtime] Use threaded code in compact value witness runtime

These changed reduce branching and yield performance improvements of up to 10% for some cases.

* Fix offset in handleRefCountsInitWithTake
2023-11-09 19:12:29 -08:00
Alastair Houghton
9ca81c63b1 [Backtracing] Massage the output a little in various cases.
We shouldn't try to erase the message when in non-color mode; mostly
that's intended for redirected output scenarios anyway, and in that case
we don't really want garbage in the output.

Also, improve the failure messages when the backtracer itself goes
wrong or can't be executed.

Finally, change the behaviour slightly so that *if* we're explicitly
enabled, *and* the backtracer path wasn't explicitly specified, *and*
we can't find the backtracer, we print a warning on start-up.  We
don't do that in any other case because we don't want spurious warnings
everywhere.

rdar://118055527
2023-11-09 18:12:18 +00:00
Alastair Houghton
9d462a7b69 [Backtracing] Add an indication that we're working on a backtrace.
When we crash, emit a message straight away that says we're working
on a backtrace.  If starting the backtracer fails, report that also.

Finally, add a duration to the messages output by the backtracer, so
that we can see how long it took.

rdar://118055527
2023-11-09 18:12:18 +00:00
Alastair Houghton
3d9533de0d Merge pull request #69570 from al45tair/eng/PR-117470489
[Linux] Disable fatalError() backtraces when the runtime backtracer is active.
2023-11-09 17:53:53 +00:00
Mike Ash
967c5c6a75 Merge pull request #69542 from mikeash/retain-release-override-optional
[Runtime] Add option to remove override point for retain/release.
2023-11-08 19:51:29 -05:00
Mike Ash
5e0125b53d Merge pull request #69334 from mikeash/metadata-cache-key-equality-memcmp
[Runtime] Have MetadataCacheKey::operator== try memcmp before deeper comparison.
2023-11-08 19:36:48 -05:00
Tim Kientzle
f85cb1cd15 Merge pull request #69684 from tbkka/tbkka-better-unexpected-nullable-message
Clarify the error we emit when we see an unexpected null pointer
2023-11-08 16:00:31 -08:00
Tim Kientzle
079c83ba9e Reword message a bit more based on review feedback 2023-11-08 11:04:43 -08:00
Alastair Houghton
003b4556b3 [Backtracing] Fix a Windows build issue.
When backtracing is disabled, don't try to refer to
`_swift_backtraceSettings`.

rdar://117470489
2023-11-08 16:06:16 +00:00
Tim Kientzle
7a920cdcbf Merge pull request #69618 from tbkka/tbkka-SwiftValue-equatable-redux
Make SwiftValue hash/equality work the same as SwiftObject

For Equatable types that are not Hashable, this proxies ObjC `-isEqual:` to Swift `Equatable` and returns a constant for ObjC `-hash`
2023-11-07 13:03:13 -08:00
Alejandro Alonso
8ac7f0b10f Merge pull request #69138 from Azoy/generics-are-hard
[Runtime] Refactor _gatherWrittenGenericParameters
2023-11-06 19:27:54 -08:00
Tim Kientzle
d16e493856 Clarify the error we emit when we see an unexpected null pointer 2023-11-06 16:39:14 -08:00
Alejandro Alonso
d73c812a2d Simplify _gatherWrittenGenericParameters 2023-11-06 12:51:06 -08:00
Alastair Houghton
199dcc2a12 Merge pull request #69635 from al45tair/eng/PR-117900760
[Backtracing][Linux] Handle inaccessible memory properly in Docker.
2023-11-06 18:36:03 +00:00
Alejandro Alonso
3503ab35d8 Update assert to fix packs 2023-11-06 10:31:21 -08:00
Alejandro Alonso
a0c8465f0c Refactor _gatherWrittenGenericParameters
Fix some bugs
2023-11-06 10:31:20 -08:00
Dario Rexin
ed25ad30ed Merge pull request #69632 from drexin/wip-117755666
[Runtime] Fix missing memcpy in handleSingleRefCountInitWithCopy
2023-11-03 11:19:55 -07:00
Alastair Houghton
7c71d0b98c [Backtracing][Linux] Handle inaccessible memory properly in Docker.
Thanks to a missing `-` sign, we were returning garbage rather than
indicating that the memory region in question was inaccessible.  This
mainly affects register dumps (since that's the time we expect to have
to cope with out-of-bounds reads).

rdar://117900760
2023-11-03 11:13:23 +00:00
Dario Rexin
40b8079018 [Runtime] Fix missing memcpy in handleSingleRefCountInitWithCopy
rdar://117755666

Missing memcpy in handleSingleRefCountInitWithCopy caused wrong enum cases, crashes etc.
2023-11-03 00:27:45 -07:00
Tim Kientzle
2cbd70e531 Make SwiftValue hash/equality work the same as SwiftObject
Update PR #68720 with lessons learned in reviewing #69464

Background:

* SwiftValue can expose Swift value types (structs/enums)
  to ObjC by wrapping them in an Obj-C object on the heap

* SwiftObject is the Obj-C type that Swift class objects all
  inherit from (when viewed from Obj-C).  This allows arbitrary
  Swift class objects to be passed into Obj-C.

History:

* PR #4124 made Obj-C `-hash` and `-isEqual:` work for SwiftValue for Hashable Swift types

* PR #68720 extended SwiftValue to also support Equatable Swift types

* PR #69464 added similar support to SwiftObject

In the process of working through #69464, we found a better way
to handle an ObjC request for `-hash` for a type that is Swift
Equatable but not Hashable.  This PR updates SwiftValue to use
the same approach.  The approach considers three cases:

1. A Hashable type can forward both `-hash` and `-isEqual:` to
   the Swift object.

2. A type that is neither Equatable nor Hashable can implement
   `-isEqual:` as the identity check and `-hash` as returning
   the address of the object in memory.

3. A type is that Equatable but not Hashable is more complex.

In this last case, we can easily forward `-isEqual:` to the
Equatable conformance but ObjC also requires us to
always provide a compatible `-hash` implementation.
The only way to do this is to have `-hash` return a constant,
but that is a very bad idea in general, so we're also including
a log message whenever we see a request for `-hash` on
a Swift value that is Equatable but not Hashable.
To limit performance problems from the logging itself, we
emit the log message only once for each type.
2023-11-02 11:24:04 -07:00
Tim Kientzle
d8d325fa04 Ooops! Missing includes 2023-11-01 17:25:10 -07:00
Tim Kientzle
33888255f3 Log a message when Obj-C -hash is invoked for a Swift object that is Equatable but not Hashable
This is worth telling people about:

Since we're being asked for the hash value, that probably means someone is
trying to put this object into some form of set or dictionary.  But we don't know
anything about the Equatable implementation, so the only valid hash value we can
possibly return is a constant.  And hash table implementations typically degrade
into unsorted lists when provided constant hash values.

Note: In order to avoid hammering the logs, we only emit the
warning once for each class that hits this path.
2023-11-01 14:19:12 -07:00
Tim Kientzle
3e49b5c684 Log a warning if Obj-C -hash is called on a Swift type that is Equatable but not Hashable
The only reasonable way to handle this can lead to severe
performance drop-offs.  We should make sure the
user is aware of this.
2023-11-01 11:53:40 -07:00
Konrad `ktoso` Malawski
5546f4da7b Rename ExecutorRef to SerialExecutorRef 2023-11-01 18:39:44 +09:00
Alastair Houghton
93e05cd1bb [Linux] Disable fatalError() backtraces when the runtime backtracer is active.
There's no need for fatalError() to try to generate its own backtraces
when the runtime's backtracer is enabled.  Not only is the code it uses
more fragile but it also doesn't support async or inline frames and it
can't look-up symbols properly either.

rdar://117470489
2023-11-01 09:18:21 +00:00
Tim Kientzle
716b58e956 Handle ObjC -hash differently for Equatable and non-Equatable types
For an Equatable type, we need a hash implementation that
is compatible with any possible definition of `==`.
Conservatively, that means `-hash` must return a constant.

For non-Equatable types, we know that `==` is identity based,
so we can get better hash behavior by using the object address.

Caveat:  This means that non-Equatable types will do two
protocol conformance checks on every call to `hash`.
2023-10-31 14:26:20 -07:00
Tim Kientzle
137e468916 Use a simpler strategy for finding the Equatable conformance to use 2023-10-31 12:44:28 -07:00
Mike Ash
6606850232 [Runtime] Add option to remove override point for retain/release.
Add a `SWIFT_STDLIB_OVERRIDABLE_RETAIN_RELEASE` CMake option. When set to true, swift_retain/release and the other functions in InstrumentsSupport.h can be overridden by setting the appropriate global function pointer, as is already the case. When set to false, those function pointers are removed and the functions always go into the default implementation.

Set `SWIFT_STDLIB_OVERRIDABLE_RETAIN_RELEASE` to false when building the minimal stdlib, and set it to true otherwise by default.

rdar://115987924
2023-10-31 15:26:01 -04:00
Tim Kientzle
6fd39741d6 Fix a number of issues (review feedback)
* Fix how we were looking for common parent class
* Use a constant hashValue for types that are Equatable but not Hashable
2023-10-30 15:57:47 -07:00
Doug Gregor
1b42ca3673 Handle any Error and Never thrown error types during runtime uniquing.
When forming runtime metadata for a function type that has either `any
Error` or `Never` as the specified thrown error type, drop the thrown
error type and normalize down to (untyped) `throws` or non-throwing,
as appropriate.
2023-10-29 19:29:09 -07:00
Doug Gregor
40e07cf900 [Typed throws] IR generation and runtime support for function type metadata
Extend function type metadata with an entry for the thrown error type,
so that thrown error types are represented at runtime as well. Note
that this required the introduction of "extended" function type
flags into function type metadata, because we would have used the last
bit. Do so, and define one extended flag bit as representing typed
throws.

Add `swift_getExtendedFunctionTypeMetadata` to the runtime to build
function types that have the extended flags and a thrown error type.
Teach IR generation to call this function to form the metadata, when
appropriate.

Introduce all of the runtime mangling/demangling support needed for
thrown error types.
2023-10-29 09:12:32 -07:00
Doug Gregor
e82854d860 Reimplement TargetFunctionTypeMetadata using TrailingObjects
The layout is the same, but this eliminates the handwritten address
computations.
2023-10-29 09:12:32 -07:00
Doug Gregor
4da1032f93 Add name mangling support for functions with a thrown error type 2023-10-29 09:12:32 -07:00
Tim Kientzle
a840c69392 Use Swift Equatable and Hashable conformances from ObjC
If a Swift type implements Equatable and/or Hashable and
we then pass that object into ObjC, we want ObjC
`isEqual:` and `hashValue` to use that.  This allows
ObjC code to build ObjC collections of Swift objects.

* Support for Hashable struct/enum types was implemented in #4124
* Support for Equatable struct/enum types was implemented in #68720
* This implements support for Hashable and Equatable _class_ types

Caveats:
1. This does a lot of dynamic lookup work for each operation, so is
   inherently rather slow.  Unlike the struct/enum case, there is no convenient
   place to cache the conformance information, so it's not clear that there is a
   viable way to make it significantly faster.
2. This is a behavioral change to low-level support code.  There is a
   risk of breaking code that may be relying on the old behavior.
2023-10-27 12:01:44 -07:00
Tim Kientzle
aed05b9a36 Obj-C class metatypes should never satisfy Obj-C existentials
Given this
```
@objc protocol P { func f() }
class C: P { func f() {} }
```
the casts `C.self is any P` and `C.self as? any P` should always fail,
because the metatype of `C.self` does not have an `f()` implementation.

These casts previously succeeded because the runtime implementation
deferred to Obj-C's `[o conformsToProtocol:p]`, and that behaves
differently depending on whether `o` is a class instance or
a Class itself.

Since these casts should never succeed, I've just modified the
Swift runtime logic to fail whenever the source of the cast
is an Obj-C Class and the target is a protocol existential.

Resolves: rdar://106973771
2023-10-26 11:57:56 -07:00
Tim Kientzle
3bdbbb5048 Merge pull request #68952 from tbkka/tbkka-rdar111422725-more-SwiftValue-casting
[Casting] Make more casts look inside `__SwiftValue`
2023-10-24 07:15:57 -07:00
Mike Ash
fc6cfbbf32 [Runtime] Have MetadataCacheKey::operator== try memcmp before deeper comparison.
Experiment shows that the vast majority (~99.99%) of keys with equal hashes have equal bytes. Fast-path that case by doing a memcmp before doing the deeper comparison.

rdar://110478978
2023-10-23 13:04:28 -04:00
Dario Rexin
0c36e44f2e [Runtime] Cleanup function names in BytecodeLayouts.cpp 2023-10-13 10:22:02 -07:00
Alejandro Alonso
de8233a5d6 Revert "Merge pull request #68844 from Azoy/gather-no-parameters"
This reverts commit ccaf427fc3, reversing
changes made to bf45c55530.
2023-10-10 19:36:20 -07:00
Alejandro Alonso
ccaf427fc3 Merge pull request #68844 from Azoy/gather-no-parameters
[Runtime] Wrap _checkGenericRequirements for _instantiateCheckedGenericMetadata
2023-10-10 11:08:33 -07:00
Alejandro Alonso
3ddec4fd83 Wrap _checkGenericRequirements instead for _instantiateCheckedGenericMetadata 2023-10-09 15:02:29 -07:00
Arnold Schwaighofer
b0424759d7 Add support for objective c protocol symbolic references
Using symbolic references instead of a text based mangling avoids the
expensive type descriptor scan when objective c protocols are requested.

rdar://111536582
2023-10-05 13:11:32 -07:00