Commit Graph

41 Commits

Author SHA1 Message Date
Mike Ash
4341102c92 [Tools] Add a library to build specialized generic metadata out of process.
This library uses GenericMetadataBuilder with a ReaderWriter that can read data and resolve pointers from MachO files, and emit a JSON representation of a dylib containing the built metadata.

We use LLVM's binary file readers to parse the MachO files and resolve fixups so we can follow pointers. This code is somewhat MachO specific, but could be generalized to other formats that LLVM supports.

rdar://116592577
2024-01-24 20:45:50 -05:00
Erik Eckstein
2ed2cea2b6 Use std::invoke_result instead of the deprecated std::result_of 2022-11-15 17:23:08 +01:00
Artem Chikin
92011f2f5a Gather opaque type conformance requirements when scanning associated type infos from a binary
When detecting that an associated type's substituted type is an opaque type, read out its opaque type descriptor to collect the names of protocols it must conform to.
2022-06-29 11:24:22 -07:00
Yuta Saito
8c598e98f7 [Wasm][Runtime] Interpret absolute function pointer in runtime structures
When SWIFT_COMPACT_ABSOLUTE_FUNCTION_POINTER is enabled, relative direct
pointers whose pointees are functions will be turned into absolute
pointer at compile-time.
2022-04-02 04:32:41 +00:00
Kuba (Brecka) Mracek
ec615b9ee0 Fix ptrauth in RelativeDirectPointer to sign *then* cast (#41355) 2022-02-12 06:49:22 -08:00
Artem Chikin
e934fe201a Add extraction protocol conformance descriptor extraction, as read from an object file image, to TypeRefBuilder. 2022-01-26 13:54:02 -08:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Michael Gottesman
32fe5eba89 [gardening] Fix a file level comment to be a doxygen comment. 2019-06-19 22:46:51 -07:00
Slava Pestov
c71f1e422b RelativePointer::operator() should be const 2018-08-20 16:23:07 -07:00
Slava Pestov
fd6bfcab3f Runtime: New RelativeIndirectPointer template
We have RelativeDirectPointer and RelativeIndirectablePointer. The latter
cannot be used with function pointers because it assumes the payload is
2-byte aligned and uses the least significant bit to dynamically
distinguish a direct pointer from an indirect pointer.

For resilient conformances, we want to use protocol dispatch thunks as
keys in the witness table template. They're not 2 byte aligned, and
they're always defined in a different image, so we need an unconditional
indirect relative pointer template.
2018-03-27 16:24:18 -07:00
Joe Groff
a7a3b17597 Replace nominal type descriptors with a hierarchy of context descriptors.
This new format more efficiently represents existing information, while
more accurately encoding important information about nested generic
contexts with same-type and layout constraints that need to be evaluated
at runtime. It's also designed with an eye to forward- and
backward-compatible expansion for ABI stability with future Swift
versions.
2018-01-29 16:19:25 -08:00
Doug Gregor
aee0c681e4 [Runtime] Pack TypeMetadataRecordKind into spare bits of protocol conformances
Use the spare bits within the type reference field to describe the kinds
of type metadata records, so that we no longer need to rely on a
separate "flags" field.
2018-01-03 09:23:31 -08:00
Doug Gregor
7e6f52308d [Runtime] Move conformance kind into low bits of witness table reference.
Move the 2-bit conformance reference kind from the conformance flags (which
is meant to go away) into the lower two bits of the witness table offset.
2018-01-02 12:34:06 -08:00
John McCall
fa9de8068f Remove operator* from RelativePointer so we can use T=void. 2017-08-25 20:02:57 -04:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01: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
Roman Levenstein
40b8fd58fe Remove copy constructors and assignment operators from RelativeDirectPointerImpl.
There were places were RelativeDirectPointers were copied using bitwise copies, which is semantically wrong. This patch makes sure it cannot happen anymore.
2016-06-29 17:20:12 -07:00
Chris Lattner
821c2cb98f Merge pull request #1796 from danra/swift_basic_sanitize
Canonicalize swift header files headers and footers
2016-03-29 17:31:53 -07:00
practicalswift
ce62c46207 [gardening] Fix recently introduced typo: "desireable" → "desirable" 2016-03-28 21:52:02 +02:00
John McCall
0ffb7278bc Only use metadata patterns for generic types; perform other
initialization in-place on demand.  Initialize parent metadata
references correctly on struct and enum metadata.

Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.

Includes a fix since the earlier commit to make enum metadata
writable if they have an unfilled payload size.  This didn't show
up on Darwin because "constant" is currently unenforced there in
global data containing relocations.

This patch requires an associated LLDB change which is being
submitted in parallel.
2016-03-24 15:10:31 -07:00
John McCall
abba7f0c8b Revert "Only use metadata patterns for generic types; perform other"
This reverts commit 41efb3d4d3.
LLDB has too many tendrils into our metadata.
2016-03-23 20:26:43 -07:00
John McCall
41efb3d4d3 Only use metadata patterns for generic types; perform other
initialization in-place on demand.  Initialize parent metadata
references correctly on struct and enum metadata.

Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.
2016-03-23 17:04:04 -07:00
Dan Raviv
29d76f3b68 Canonize swift header files headers and footers
- Added missing ifdef guard in PointerIntEnum header
- Consistent naming convention for ifdef guards
- Consistent 'end namespace swift'
- Consistent single EOL at end of header files
2016-03-23 09:04:12 +02:00
David Farler
eabf6eee0c Add header guard to RelativePointer.h 2016-02-23 13:17:47 -08:00
Joe Groff
32872cb74a IRGen/Runtime: Relative-reference the nominal type descriptor and parent type from metadata.
Save a couple relocations per concrete value type, leaving only the value witness table as an absolute symbol.
2016-02-09 15:17:03 -08:00
Slava Pestov
3624b1fc6b Runtime: Support for resiliently adding protocol requirements with default implementations
This is the first patch in a series that will allow new protocol
requirements to be added resiliently, with the runtime filling in
default implementations in witness tables.

First, this adds a new flag to the protocol descriptor indicating
that the protocol is resilient. In this case, there are two
additional fields, MinimumWitnessTableSizeInWords and
DefaultWitnessTableSizeInWords, followed by tail-allocated
default witnesses.

The swift_getGenericWitnessTable() entry point now fills in the
default witnesses from the protocol if the given witness table
template is smaller than the expected witness table size.

This also changes the layout of instantiated witness tables to move
the address point to the end of private data. Previously the private
data came after the requirements, but this meant that adding new
requirements would require sliding the private data at runtime and
accessing it indirectly. It is much simpler to access it from
negative offsets instead.

I updated IRGen to emit the new metadata, but currently all protocols
are flagged as not resilient, and default witnesses are not emitted;
this will come in a subsequent patch once some more plumbing is
in place.

To avoid generating GOT entries for references to protocols defined
in the current module, I had to add some hacks to the existing hack
for this. I'll hopefully clean this up in a principled manner later.
2016-02-04 17:34:55 -08:00
Joe Groff
9f95f66f6e Avoid UB in RelativePointer on 32-bit platforms.
We really want to apply offsets using wrapping (unsigned) arithmetic, albeit with sign extension. This is significant on 32-bit platforms, where "far" addresses could be more than 2GB apart, but still relative-referenced using 32-bit signed values, and offset addition could end up wrapping around. Factor the logic to add an offset to a pointer out into a function that performs the sacred casting dance to appease the UB gods.
2016-01-21 10:58:07 -08:00
Joe Groff
e5227a3ed6 Templatize Relative*Pointer on offset type.
We usually want to use 32-bit offsets, since we use them to reference other objects within the same small-code-model image. However, for data structures that are both compiler-generated and runtime-allocated, we may want to save the relocation in compile time, but need a full-width offset to be able to relatively reference things from the heap. NFC yet.
2016-01-20 20:00:24 -08:00
Joe Groff
638e4b0984 IRGen/Runtime: Use relative addresses in nominal type descriptors.
Decrease the size of nominal type descriptors and make them true-const by relative-addressing the other metadata they need to reference, which should all be included in the same image as the descriptor itself. Relative-referencing string constants exposes a bug in the Apple linker, which crashes when resolving relative relocations to coalesceable symbols (rdar://problem/22674524); work around this for now by revoking the `unnamed_addr`-ness of string constants that we take relative references to. (I haven't tested whether GNU ld or gold also have this problem on Linux; it may be possible to conditionalize the workaround to only apply to Darwin targets for now.)
2016-01-20 14:41:53 -08:00
practicalswift
1339b5403b Consistent use of header comment format.
Correct format:
//===--- Name of file - Description ----------------------------*- Lang -*-===//
2016-01-04 13:26:31 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
John McCall
8f30faa4c1 Include access functions for the metadata and witness tables
of associated types in protocol witness tables.

We use the global access functions when the result isn't
dependent, and a simple accessor when the result can be cheaply
recovered from the conforming metadata.  Otherwise, we add a
cache slot to a private section of the witness table, forcing
an instantiation per conformance.  Like generic type metadata,
concrete instantiations of generic conformances are memoized.

There's a fair amount of code in this patch that can't be
dynamically tested at the moment because of the widespread
reliance on recursive expansion of archetypes / dependent
types.  That's something we're now theoretically in a position
to change, and as we do so, we'll test more of this code.

This speculatively re-applies 7576a91009,
i.e. reverts commit 11ab3d537f.
We have not been able to duplicate the build failure in
independent testing; it might have been spurious or unrelated.
2015-12-29 12:14:40 -08:00
Dmitri Gribenko
11ab3d537f Revert "Include access functions for the metadata and witness tables"
This reverts commit 7576a91009.
It broke the testsuite for swift-corelibs-foundation.
2015-12-25 19:17:50 +02:00
John McCall
7576a91009 Include access functions for the metadata and witness tables
of associated types in protocol witness tables.

We use the global access functions when the result isn't
dependent, and a simple accessor when the result can be cheaply
recovered from the conforming metadata.  Otherwise, we add a
cache slot to a private section of the witness table, forcing
an instantiation per conformance.  Like generic type metadata,
concrete instantiations of generic conformances are memoized.

There's a fair amount of code in this patch that can't be
dynamically tested at the moment because of the widespread
reliance on recursive expansion of archetypes / dependent
types.  That's something we're now theoretically in a position
to change, and as we do so, we'll test more of this code.

This reverts commit 6528ec2887, i.e.
it reapplies b1e3120a28, with a fix
to unbreak release builds.
2015-12-24 20:21:17 -08:00
Sean Callanan
6528ec2887 Revert "Include access functions for the metadata and witness tables"
This reverts commit b1e3120a28.

Reverting because this patch uses WitnessTableBuilder::PI in NDEBUG code.
That field only exists when NDEBUG is not defined, but now NextCacheIndex, a
field that exists regardless, is being updated based on information from PI.

This problem means that Release builds do not work.
2015-12-23 15:42:10 -08:00
John McCall
b1e3120a28 Include access functions for the metadata and witness tables
of associated types in protocol witness tables.

We use the global access functions when the result isn't
dependent, and a simple accessor when the result can be cheaply
recovered from the conforming metadata.  Otherwise, we add a
cache slot to a private section of the witness table, forcing
an instantiation per conformance.  Like generic type metadata,
concrete instantiations of generic conformances are memoized.

There's a fair amount of code in this patch that can't be
dynamically tested at the moment because of the widespread
reliance on recursive expansion of archetypes / dependent
types.  That's something we're now theoretically in a position
to change, and as we do so, we'll test more of this code.
2015-12-23 00:37:24 -08:00
Joe Groff
fd457fb343 Revert "IRGen/Runtime: Use relative addresses in nominal type descriptors."
This reverts commit fbb832665a. It causes LLVM to complain with
"Cannot represent a subtraction with a weak symbol" when targeting Linux.
2015-12-11 15:41:11 -08:00
Joe Groff
fbb832665a IRGen/Runtime: Use relative addresses in nominal type descriptors.
Decrease the size of nominal type descriptors and make them true-const by relative-addressing the other metadata they need to reference, which should all be included in the same image as the descriptor itself. Relative-referencing string constants exposes a bug in the Apple linker, which crashes when resolving relative relocations to coalesceable symbols (rdar://problem/22674524); work around this for now by revoking the `unnamed_addr`-ness of string constants that we take relative references to. (I haven't tested whether GNU ld or gold also have this problem on Linux; it may be possible to conditionalize the workaround to only apply to Darwin targets for now.)
2015-12-11 15:21:12 -08:00
Joe Groff
12e0e0271f Runtime: Mixed-sign arithmetic is hard
Consistently use signed arithmetic in RelativePointer to avoid incorrectly zero-extending negative offsets.

Swift SVN r31903
2015-09-11 19:30:54 +00:00
Joe Groff
686505c498 Runtime: Rename RelativePointer types to be more descriptive.
Not every relative address makes sense to be conditionally indirect, so separate RelativeIndirectablePointer (which takes a tag bit to indicate whether the reference is directly to the object in question or to memory containing its address, such as its GOT entry) and RelativeDirectPointer. NFC.

Swift SVN r31839
2015-09-10 00:51:08 +00:00
Joe Groff
773eadb9f2 IRGen/Runtime: Populate the runtime protocol conformance table with relative references.
By using relative references, either directly to symbols internal to the current TU, or to the GOT entry for external symbols, we avoid unnecessary runtime relocations, and we save space on 64-bit platforms, since a single image is still <2GB in size. For the 64-bit standard library, this trades 26KB of fake-const data in __DATA,__swift1_proto for 13KB of true-const data in __TEXT,__swift2_proto. Implements rdar://problem/22334380.

Swift SVN r31555
2015-08-28 18:07:22 +00:00