Commit Graph

323 Commits

Author SHA1 Message Date
Slava Pestov
32caa17b11 Runtime: Finish removing the 'extra argument' notion 2023-03-03 02:21:08 -05:00
Augusto Noronha
b1c763d954 Allow TypeInfoProviders to identify themselves
TypeInfoProvider's address was used as part of the key to cache type
infos. This can cause the unnecessary recomputation of type infos, as
different instances of TypeInfoProviders may provide the exact same type
infos. Allow TypeInfoProviders to provide an id which can be used for
caching purposes.

rdar://80001304
2023-03-02 21:54:10 -08:00
Slava Pestov
d5b75fe7d2 Runtime: Add GenericRequirementKind::SameShape and stub out cases 2023-02-28 17:33:06 -05:00
Augusto Noronha
893d83ba0a Fix wrong calculation of generic params per level when building decl
Fix computation of generic params per level when a generic type is
nested in  a non-generic one. For example, for the following code:

```
class A {
  class B<T, U> {
  }
}
```
Fix the array of generic params per level from [2] to [0, 2].

rdar://103457629
2023-01-06 10:41:41 -08:00
Mike Ash
ba3744f648 [Reflection] Fix premature deallocation of string memory in buildContextDescriptor.
This function demangles a std::string, but the demangler can create interior pointers into the string being demangled. Solve this by copying the string into the Demangler first.

readMangledName does the same thing. Consolidate the string copying code into a method on NodeFactory, then make both functions use it.

rdar://102275748
2022-11-14 14:05:54 -05:00
Nuri Amari
66f993194a Fix issues with runtime reporting incorrect types for nested generics
Fixes: https://github.com/apple/swift/issues/60565
2022-11-04 09:43:26 -07:00
Mike Ash
c3f15cb65a Merge pull request #61703 from mikeash/readtypefrommetadata-recursion-limit
[Reflection] Put a recursion limit on readTypeFromMetadata.
2022-10-25 16:10:01 -04:00
Mike Ash
4945a56344 [Reflection] Put a recursion limit on readTypeFromMetadata.
We can end up with a stack overflow if we encounter a very deeply nested type, or bad data that looks like one. Fail gracefully for types that are nested beyond a limit. By default, the limit is 50.

rdar://100847548
2022-10-25 10:35:51 -04:00
Mike Ash
494d570e87 [Reflection] Fix premature deallocation of string memory in readMangledName.
This function creates a demangled tree from a std::string, but the demangle tree can include pointers into the interior of the passed-in string, which become invalid on return. Copy the string into the demangler's own memory first, so that the lifetimes are correct.

rdar://101438017
2022-10-24 17:00:40 -04:00
Alastair Houghton
fb092d9eb2 Add workaround for implicit copy constructor generation problem.
Some versions of Clang seem to generate a non-working implicit copy constructor
for `RemoteRef<BuiltinTypeDescriptor>`, which results in all the reflection tests
failing.  Fix by declaring it explicitly.

rdar://101240198
2022-10-17 11:58:45 +01:00
Augusto Noronha
63a2c7640c Introduce ExternalTypeRefCache
LLDB would like to cache typeref information to accelerate finding type
information. This patch adds an optional interface that allows of
typeref to register and provider field descriptor information for faster
lookups.
2022-08-17 14:07:15 -07:00
Artem Chikin
f38f3ff1ac Merge pull request #59791 from artemcm/GatherOpaqueAssociatedTypeConformanceReqs
Gather opaque type conformance requirements when scanning associated type infos from a binary
2022-07-05 11:47:28 -07:00
John McCall
175f74d38f Implement symbolic demangling for extended existential metadata
Fixes rdar://96268090.
2022-07-01 11:21:53 -04:00
Artem Chikin
cfad246963 [Metadata Reader] Do not assume OpaqueType context descriptor's parent context is always anonymous 2022-06-29 11:09:07 -07:00
Augusto Noronha
6a82edd366 Fix nested generic typerefs applying generic params at the wrong level
Generic params of typerefs are supposed to be "attached" on the level
they belong, not as a flat list, unlike other parts of the system. Fix
the application of bound generic params by checking how many were
already applied in the hierarchy and ignoring those already attached.
2022-06-22 16:33:38 -07:00
zoecarver
f972f664d3 [cxx-interop] Runtime support for foreign reference types. 2022-06-14 12:18:05 -07:00
Robert Widmann
9918050dfa [Remote Mirrors] Support Extended Existential Type Metadata
Teach Remote Mirrors to read extended existential type metadata.
2022-04-27 13:18:50 -07:00
Robert Widmann
61e5eee909 Read Runtime Generic Signatures 2022-04-25 18:24:13 -07:00
Robert Widmann
fe6e280b30 Add a Utility to Read Extended Existential Type Shapes 2022-04-25 18:24:13 -07:00
Robert Widmann
d710b52b66 Add Substitution Machinery to Builders
Add requirements to the Builder concept to construct generic signatures and substitution maps. Then introduce a `subst` requirement that uses the substitution map to call through to the builder's notion of type (ref) substitution.

This infrastructure is sufficient to model the notion of a RuntimeGenericSignature.
2022-04-25 18:24:13 -07:00
Robert Widmann
a410d1a6be [NFC] Consolidate Builder Result Types 2022-04-25 16:49:34 -07:00
Josh Soref
871634b9f3 Spelling include (#42616)
* spelling: accessible

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: are

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: assume

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: attempt

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: children

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: convenience

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: creation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: default

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: dereference

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: deserialization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: embedded

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: enriched

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: excluding

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: for

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: global

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: guarantee

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: initialization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: initialize

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: label

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: lifting

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: mangled

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: metadata

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: minimum

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: offset

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: only

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: otherwise

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: output

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overall

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: parameter

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: passed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: performance

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: referenced

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: standard

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: syntax

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: that

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: trivia

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: truncate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: undesirable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: uniformly

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: uninitialized

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: value

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: verification

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-25 09:00:59 -07:00
John McCall
cb3818ea16 [NFC] Split the basic target-layout logic into its own file 2022-03-22 21:58:27 -04:00
Dave Lee
cfe0b9774d [Reflection] Update MemoryReader to distinguish related operations (#41530)
Until recently, `MemoryReader` had a single function `resovlePointer` which did two things, and has a somewhat vague name. The two things were:

1. Tool-specific mapping between real addresses and tagged addresses (first implemented in `swift-reflection-dump` and then later in lldb)
2. Finding a "symbol" for a given address

Recently, `resolvePointerAsSymbol` was added, which overloaded the term "resolve" and it added another way to deal with symbols for addresses. Symbols themselves were a bit muddled, as `swift-reflection-dump` was dealing with dynamic symbols aka bindings, while lldb was dealing in regular (static) symbols.

This change separates these two parts of functionality, and also divides symbol lookup into two cases. The API surface will now be:

1. `resolvePointer` for mapping/tagging addresses
3. `getSymbol` for looking up a symbol for an address
4. `getDynamicSymbol` for looking up a dyld binding for an address

Note: each of these names could be improved. Some alternative terms: `lookup` instead of `get`, `Binding` or `BindingName` instead of `DynamicSymbol`. Maybe even another term instead of "resolve". Suggestions welcome!

Currently, `swift-reflection-dump` supports `getDynamicSymbol` but not `getSymbol`. For lldb it's the reverse, `getSymbol` is supported but `getDynamicSymbol` needs to be implemented.

For everything but lldb, this change is NFC. For lldb it fixes a bug where `LLDBMemoryReader` returns regular symbols where we should instead be returning dynamic symbols.
2022-03-11 22:36:20 -08:00
Ben Langmuir
5a5dd13ca1 Include what you use: Optional and Hashing
Add a few includes of Optional.h and Hashing.h. These files are failing
ot build in the "next" branch due to changes in llvm's own includes, but
it's general goodness to include them on main as well.
2022-02-24 13:47:04 -08:00
Dave Lee
5a5f4b0f81 Merge pull request #41196 from apple/Reflection-Fix-failing-arm64e-test
[Reflection] Fix failing arm64e test
2022-02-04 09:49:41 -08:00
Mike Ash
bd434d4a56 Merge pull request #41160 from mikeash/swift-inspect-concurrency
[RemoteMirror][swift-inspect] Add a command to inspect the state of the concurrency runtime.
2022-02-04 12:41:20 -05:00
Mike Ash
a82ea120a4 [RemoteMirror][swift-inspect] Add a command to inspect the state of the concurrency runtime.
Most of the new inspection logic is in Remote Mirror. New code in swift-inspect calls the new Remote Mirror functions and formats the resulting information for display.

Specific Remote Mirror changes:

* Add a call to check if a given metadata is an actor.
* Add calls to get information about actors and tasks.
* Add a `readObj` call to MemoryReader that combines the read and the cast, greatly simplifying code chasing pointers in the remote process.
* Add a generalized facility to the C shims that can allocate a temporary object that remains valid until at least the next call, which is used to return various temporary arrays from the new calls. Remove the existing `lastString` and `lastChunks` member variables in favor of this new facility.

Swift-inspect changes:

* Add a new dump-concurrency command.
* Add a new `ConcurrencyDumper.swift` file with the implementation. The dumper needs to do some additional work with the results from Remote Mirror to build up the task tree and this keeps it all organized.
* Extend `Inspector` to query the target's threads and fetch each thread's current task.

Concurrency runtime changes:

* Add `_swift_concurrency_debug` variables pointing to the various future adapters. Remote Mirror uses these to provide a better view of a tasks's resume pointer.

rdar://85231338
2022-02-04 09:28:32 -05:00
Dave Lee
f60873145a Add braces 2022-02-04 06:26:39 -08:00
Dave Lee
5836dbd18f [Reflection] Fix failing arm64e test
rdar://88403538
2022-02-03 15:03:18 -08:00
Augusto Noronha
2baed0e025 Merge pull request #41072 from augusto2112/resolvePointerAsSymbol
Add a new virtual function to MemoryReader: resolvePointerAsSymbol
2022-02-02 10:31:45 -03:00
Dave Lee
1d1acf644f fallback to directly constructing RemoteAbsolutePointer 2022-01-30 22:44:16 -08:00
Dave Lee
c348ad9a27 [Reflection] Resolve direct symbolic reference pointers 2022-01-28 19:34:57 -08:00
Augusto Noronha
f30b313610 Add a new virtual function to MemoryReader: resolvePointerAsSymbol
This new function allows subclasses to attempt to resolve a pointer to
a symbol before any memory is read, which can be potentially expensive
(for example, in LLDB).
2022-01-28 19:23:30 -03:00
Adrian Prantl
fede775269 Make Objective-C interoperability configurable in the runtime
In order to be able to debug, for example, a Linux process from a macOS host, we
need to be able to initialize a ReflectionContext without Objective-C
interoperability. This patch turns ObjCInterOp into another template trait, so
it's possible to instantiate a non-ObjC MetadataReader on a system built with
ObjC-interop (but not vice versa).

This patch changes the class hierarchy to

                          TargetMetadata<Runtime>
                                    |
                          TargetHeapMetadata<Runtime>
                                    |
                          TargetAnyClassMetadata<Runtime>
                                   /                \
                                  /               TargetAnyClassMetadataObjCInterop<Runtime>
                                 /                              \
TargetClassMetadata<Runtime, TargetAnyClassMetadata<Runtime>>    \
                                                                  \
                    TargetClassMetadata<Runtime, TargetAnyClassMetadataObjCInterop<Runtime>>

TargetAnyClassMetadataObjCInterop inherits from TargetAnyClassMetadata because
most of the implementation is the same. This choice makes TargetClassMetadata a
bit tricky. In this patch I went with templating the parent class.

rdar://87179578
2022-01-20 18:28:18 -08:00
Mike Ash
cf3c131e7c [Reflection] Add API for inspecting async task allocation slabs.
We remove the existing `swift_reflection_iterateAsyncTaskAllocations` API that attempts to provide all necessary information about a tasks's allocations starting from the task. Instead, we split it into two pieces: `swift_reflection_asyncTaskSlabPointer` to get the first slab for a task, and `+swift_reflection_asyncTaskSlabAllocations` to get the allocations in a slab, and a pointer to the next slab.

We also add a dummy metadata pointer to the beginning of each slab. This allows tools to identify slab allocations on the heap without needing to locate every single async task object. They can then use `swift_reflection_asyncTaskSlabAllocations` on such allocations to find out about the contents.

rdar://82549631
2021-11-18 14:15:25 -05:00
Saleem Abdulrasool
3c9c564eba Revert "[Reflection] Add API for inspecting async task allocation slabs." 2021-11-17 18:47:13 -08:00
Mike Ash
7c7dc5d5b3 [Reflection] Add API for inspecting async task allocation slabs.
We remove the existing `swift_reflection_iterateAsyncTaskAllocations` API that attempts to provide all necessary information about a tasks's allocations starting from the task. Instead, we split it into two pieces: `swift_reflection_asyncTaskSlabPointer` to get the first slab for a task, and `+swift_reflection_asyncTaskSlabAllocations` to get the allocations in a slab, and a pointer to the next slab.

We also add a dummy metadata pointer to the beginning of each slab. This allows tools to identify slab allocations on the heap without needing to locate every single async task object. They can then use `swift_reflection_asyncTaskSlabAllocations` on such allocations to find out about the contents.

rdar://82549631
2021-11-11 16:58:14 -05:00
Augusto Noronha
1336d2bc74 Implement IsValueInlinedInExistentialContainer 2021-11-05 16:05:03 -03:00
Alastair Houghton
3f01f853a6 [Demangling] Add error handling to the remangler.
Mangling can fail, usually because the Node structure has been built
incorrectly or because something isn't supported with the old remangler.
We shouldn't just terminate the program when that happens, particularly
if it happens because someone has passed bad data to the demangler.

rdar://79725187
2021-09-06 17:49:09 +01:00
Mike Ash
ebd9c215ec [Reflection] Sanity-check metadata sizes in MetadataReader before reading.
MetadataReader can be given corrupt or garbage data and we need to be able to handle it gracefully. When reading metadata, the full size to read is calculated from partial data. When we're given bad data, these calculated sizes can be enormous, up to 4GB. Trying to read that much data can cause address space exhaustion which leads to unpleasantness.

To avoid this, fix a limit of 1MB on metadata sizes, and fail early if the size is larger. No real-world metadata should ever be that large.

We also switch these potentially large calls to use the readBytes variant that returns a unique_ptr, rather than allocating a buffer and reading into it. Our clients typically implement that as the primitive, so this avoids an unnecessary extra data copy and extra address space usage for them. Clients that implement reading into a provided buffer as the primitive should see the same performance as before.

rdar://78621784
2021-06-11 13:20:10 -04:00
Doug Gregor
b814e225dd Implement (de-)mangling and type metadata for global actor function types.
Implement name mangling, type metadata, runtime demangling, etc. for
global-actor qualified function types. Ensure that the manglings
round-trip through the various subsystems.

Implements rdar://78269642.
2021-06-02 23:34:22 -07:00
Mike Ash
1aa30366e5 [RemoteMirror] Add NULL checks to Remote Mirror malloc calls.
These calls can fail when passed absurd sizes, which can happen when we try to read data that's corrupt or doesn't contain what we think it should. Fail gracefully instead of crashing.

rdar://78210820
2021-05-19 13:25:09 -04:00
Adrian Prantl
a50c048aa0 Add a missing check for the number of children before accessing them.
parentDemangling is going to be added as the first child of the return node, so
we can't construct one unless we have both children.

rdar://77530286
2021-05-07 16:49:31 -07:00
Tim Kientzle
142b4f4230 Don't crash if malloc fails
Resolves rdar://61159451
2021-05-03 14:00:19 -07:00
Mike Ash
3da4d20081 [Runtime] Use ptrauth qualifiers on metadata and superclass fields. 2021-04-12 12:59:49 -04:00
Richard Wei
d997526948 Fix function differentiability kind metadata and mangling. (#36601)
* Move differentiability kinds from target function type metadata to trailing objects so that we don't exhaust all remaining bits of function type metadata.
  * Differentiability kind is now stored in a tail-allocated word when function type flags say it's differentiable, located immediately after the normal function type metadata's contents (with proper alignment in between).
  * Add new runtime function `swift_getFunctionTypeMetadataDifferentiable` which handles differentiable function types.
* Fix mangling of different differentiability kinds in function types. Mangle it like `ConcurrentFunctionType` so that we can drop special cases for escaping functions.
    ```
    function-signature ::= params-type params-type async? sendable? throws? differentiable? // results and parameters
    ...
    differentiable ::= 'jf'                    // @differentiable(_forward) on function type
    differentiable ::= 'jr'                    // @differentiable(reverse) on function type
    differentiable ::= 'jd'                    // @differentiable on function type
    differentiable ::= 'jl'                    // @differentiable(_linear) on function type
    ```

Resolves rdar://75240064.
2021-03-30 09:59:06 -07:00
tbkka
04b21d0481 Limit the recursion depth when trying to get the mangling for a context descriptor (#35314)
* Limit the recursion depth when trying to get the mangling for a context descriptor

This should help guard against corrupted data in the target app when debugging.

* Only decrement the recursion limit once for each parent visit
2021-02-09 11:07:40 -08:00
Adrian Prantl
6fb61785c1 Support SILBoxWithLayout in TypeDecoder and factor out decodeRequirement() from
ASTBuilder.
2021-02-03 14:23:54 -08:00
Alejandro Alonso
424802fb34 Revert SE-0283 (#34492)
Reverted despite build failures.
2020-10-29 17:32:06 -07:00