Skip to content

Commit eb83023

Browse files
committed
[RemoteMirror] Add support for QueryDataLayoutFunction to the interop header.
Remote Mirror added a new callback, QueryDataLayoutFunction, which provides a nicer and more extensible way to provide things like the target pointer size. Add support for that in the interop header. Search for and call the new creation function when it exists, and shim in support for older libraries that don't have it. rdar://problem/52849572
1 parent 44cacfb commit eb83023

File tree

1 file changed

+101
-9
lines changed

1 file changed

+101
-9
lines changed

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorLegacyInterop.h

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ swift_reflection_interop_createReflectionContext(
4444
GetStringLengthFunction GetStringLength,
4545
GetSymbolAddressFunction GetSymbolAddress);
4646

47+
static inline SwiftReflectionInteropContextRef
48+
swift_reflection_interop_createReflectionContextWithDataLayout(
49+
void *ReaderContext,
50+
QueryDataLayoutFunction DataLayout,
51+
FreeBytesFunction FreeBytes,
52+
ReadBytesFunction ReadBytes,
53+
GetStringLengthFunction GetStringLength,
54+
GetSymbolAddressFunction GetSymbolAddress);
55+
4756
/// Add a library handle to the interop context. Returns 1 if the
4857
/// library was added successfully, 0 if a symbol couldn't be looked up
4958
/// or the reported metadata version is too old.
@@ -193,6 +202,15 @@ struct SwiftReflectionFunctions {
193202
ReadBytesFunction ReadBytes,
194203
GetStringLengthFunction GetStringLength,
195204
GetSymbolAddressFunction GetSymbolAddress);
205+
206+
// Optional creation function that takes a data layout query function.
207+
SwiftReflectionContextRef (*createReflectionContextWithDataLayout)(
208+
void *ReaderContext,
209+
QueryDataLayoutFunction DataLayout,
210+
FreeBytesFunction FreeBytes,
211+
ReadBytesFunction ReadBytes,
212+
GetStringLengthFunction GetStringLength,
213+
GetSymbolAddressFunction GetSymbolAddress);
196214

197215
SwiftReflectionContextRef (*createReflectionContextLegacy)(
198216
void *ReaderContext,
@@ -298,7 +316,7 @@ struct SwiftReflectionInteropContextLegacyImageRangeList {
298316

299317
struct SwiftReflectionInteropContext {
300318
void *ReaderContext;
301-
uint8_t PointerSize;
319+
QueryDataLayoutFunction DataLayout;
302320
FreeBytesFunction FreeBytes;
303321
ReadBytesFunction ReadBytes;
304322
uint64_t (*GetStringLength)(void *reader_context,
@@ -409,11 +427,12 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
409427
#ifndef __cplusplus
410428
#define decltype(x) void *
411429
#endif
412-
#define LOAD_NAMED(field, symbol) do { \
430+
#define LOAD_NAMED(field, symbol, required) do { \
413431
Functions->field = (decltype(Functions->field))dlsym(Handle, symbol); \
414-
if (Functions->field == NULL) return 0; \
432+
if (required && Functions->field == NULL) return 0; \
415433
} while (0)
416-
#define LOAD(name) LOAD_NAMED(name, "swift_reflection_" #name)
434+
#define LOAD(name) LOAD_NAMED(name, "swift_reflection_" #name, 1)
435+
#define LOAD_OPT(name) LOAD_NAMED(name, "swift_reflection_" #name, 0)
417436

418437
Functions->classIsSwiftMaskPtr =
419438
(unsigned long long *)dlsym(Handle, "swift_reflection_classIsSwiftMask");
@@ -425,15 +444,18 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
425444
int IsLegacy = dlsym(Handle, "swift_reflection_addImage") == NULL;
426445

427446
if (IsLegacy) {
428-
LOAD_NAMED(createReflectionContextLegacy, "swift_reflection_createReflectionContext");
429-
LOAD_NAMED(addReflectionInfoLegacy, "swift_reflection_addReflectionInfo");
447+
LOAD_NAMED(createReflectionContextLegacy, "swift_reflection_createReflectionContext", 1);
448+
LOAD_NAMED(addReflectionInfoLegacy, "swift_reflection_addReflectionInfo", 1);
430449
} else {
431450
LOAD(createReflectionContext);
432451
LOAD(addReflectionInfo);
433452
LOAD(addImage);
434453
LOAD(ownsObject);
435454
LOAD(ownsAddress);
436455
LOAD(metadataForObject);
456+
457+
// Optional creation function.
458+
LOAD_OPT(createReflectionContextWithDataLayout);
437459
}
438460

439461
LOAD(destroyReflectionContext);
@@ -508,6 +530,32 @@ swift_reflection_interop_GetSymbolAddressAdapter(
508530
return Context->GetSymbolAddress(Context->ReaderContext, name, name_length);
509531
}
510532

533+
static inline int
534+
swift_reflection_interop_minimalDataLayoutQueryFunction4(
535+
void *ReaderContext,
536+
DataLayoutQueryType type,
537+
void *inBuffer, void *outBuffer) {
538+
if (type == DLQ_GetPointerSize || type == DLQ_GetSizeSize) {
539+
uint8_t *result = (uint8_t *)outBuffer;
540+
*result = 4;
541+
return 1;
542+
}
543+
return 0;
544+
}
545+
546+
static inline int
547+
swift_reflection_interop_minimalDataLayoutQueryFunction8(
548+
void *ReaderContext,
549+
DataLayoutQueryType type,
550+
void *inBuffer, void *outBuffer) {
551+
if (type == DLQ_GetPointerSize || type == DLQ_GetSizeSize) {
552+
uint8_t *result = (uint8_t *)outBuffer;
553+
*result = 8;
554+
return 1;
555+
}
556+
return 0;
557+
}
558+
511559
static inline SwiftReflectionInteropContextRef
512560
swift_reflection_interop_createReflectionContext(
513561
void *ReaderContext,
@@ -516,12 +564,37 @@ swift_reflection_interop_createReflectionContext(
516564
ReadBytesFunction ReadBytes,
517565
GetStringLengthFunction GetStringLength,
518566
GetSymbolAddressFunction GetSymbolAddress) {
519-
567+
QueryDataLayoutFunction DataLayout;
568+
if (PointerSize == 4)
569+
DataLayout = swift_reflection_interop_minimalDataLayoutQueryFunction4;
570+
else if (PointerSize == 8)
571+
DataLayout = swift_reflection_interop_minimalDataLayoutQueryFunction8;
572+
else
573+
abort(); // Can't handle sizes other than 4 and 8.
574+
575+
return swift_reflection_interop_createReflectionContextWithDataLayout(
576+
ReaderContext,
577+
NULL,
578+
FreeBytes,
579+
ReadBytes,
580+
GetStringLength,
581+
GetSymbolAddress);
582+
}
583+
584+
static inline SwiftReflectionInteropContextRef
585+
swift_reflection_interop_createReflectionContextWithDataLayout(
586+
void *ReaderContext,
587+
QueryDataLayoutFunction DataLayout,
588+
FreeBytesFunction FreeBytes,
589+
ReadBytesFunction ReadBytes,
590+
GetStringLengthFunction GetStringLength,
591+
GetSymbolAddressFunction GetSymbolAddress) {
592+
520593
SwiftReflectionInteropContextRef ContextRef =
521594
(SwiftReflectionInteropContextRef)calloc(sizeof(*ContextRef), 1);
522595

523596
ContextRef->ReaderContext = ReaderContext;
524-
ContextRef->PointerSize = PointerSize;
597+
ContextRef->DataLayout = DataLayout;
525598
ContextRef->FreeBytes = FreeBytes;
526599
ContextRef->ReadBytes = ReadBytes;
527600
ContextRef->GetStringLength = GetStringLength;
@@ -548,10 +621,29 @@ swift_reflection_interop_addLibrary(
548621
swift_reflection_interop_readBytesAdapter,
549622
swift_reflection_interop_GetStringLengthAdapter,
550623
swift_reflection_interop_GetSymbolAddressAdapter);
624+
} else if (Library->Functions.createReflectionContextWithDataLayout) {
625+
Library->Context =
626+
Library->Functions.createReflectionContextWithDataLayout(
627+
ContextRef->ReaderContext,
628+
ContextRef->DataLayout,
629+
ContextRef->FreeBytes,
630+
ContextRef->ReadBytes,
631+
ContextRef->GetStringLength,
632+
ContextRef->GetSymbolAddress);
551633
} else {
634+
uint8_t PointerSize;
635+
int result = ContextRef->DataLayout(
636+
ContextRef->ReaderContext, DLQ_GetPointerSize, NULL, &PointerSize);
637+
if (!result)
638+
abort(); // We need the pointer size, can't proceed without it.
639+
552640
Library->Context = Library->Functions.createReflectionContext(
553641
ContextRef->ReaderContext,
554-
ContextRef->PointerSize, ContextRef->FreeBytes, ContextRef->ReadBytes, ContextRef->GetStringLength, ContextRef->GetSymbolAddress);
642+
PointerSize,
643+
ContextRef->FreeBytes,
644+
ContextRef->ReadBytes,
645+
ContextRef->GetStringLength,
646+
ContextRef->GetSymbolAddress);
555647
}
556648
}
557649
return Success;

0 commit comments

Comments
 (0)