Skip to content

Commit 71f1c39

Browse files
committed
[RemoteMirrors] Move FreeBytesFunction from an out-parameter on ReadBytesFunction to a function pointer passed in when creating a reflection context.
1 parent 7d01c79 commit 71f1c39

File tree

7 files changed

+56
-50
lines changed

7 files changed

+56
-50
lines changed

include/swift/Remote/CMemoryReader.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ class CMemoryReader final : public MemoryReader {
7676

7777
std::tuple<const void *, std::function<void()>>
7878
readBytes(RemoteAddress address, uint64_t size) override {
79-
FreeBytesFunction freeFunc;
80-
void *freeContext;
81-
auto ptr = Impl.readBytes(Impl.reader_context, address.getAddressData(), size,
82-
&freeFunc, &freeContext);
83-
auto freeLambda = [=]{ freeFunc(ptr, freeContext); };
79+
void *FreeContext;
80+
auto Ptr = Impl.readBytes(Impl.reader_context, address.getAddressData(), size,
81+
&FreeContext);
82+
auto ReaderContext = Impl.reader_context;
83+
auto Free = Impl.free;
84+
auto freeLambda = [=]{ Free(ReaderContext, Ptr, FreeContext); };
8485

85-
return std::make_tuple(ptr, freeLambda);
86+
return std::make_tuple(Ptr, freeLambda);
8687
}
8788
};
8889

include/swift/SwiftRemoteMirror/MemoryReaderInterface.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ extern "C" {
3232
// in the system library, so we use 'swift_addr_t'.
3333
typedef uint64_t swift_addr_t;
3434

35-
typedef void (*FreeBytesFunction)(const void *bytes, void *context);
35+
typedef void (*FreeBytesFunction)(void *reader_context, const void *bytes, void *context);
3636

3737
typedef uint8_t (*PointerSizeFunction)(void *reader_context);
3838
typedef uint8_t (*SizeSizeFunction)(void *reader_context);
3939
typedef const void *(*ReadBytesFunction)(void *reader_context, swift_addr_t address,
4040
uint64_t size,
41-
FreeBytesFunction *outFreeFunction,
4241
void **outFreeContext);
4342
typedef uint64_t (*GetStringLengthFunction)(void *reader_context,
4443
swift_addr_t address);
@@ -57,6 +56,9 @@ typedef struct MemoryReaderImpl {
5756
/// Get the size in bytes of the target's size type.
5857
SizeSizeFunction getSizeSize;
5958

59+
/// Free memory returned from readBytes.
60+
FreeBytesFunction free;
61+
6062
// FIXME: -Wdocumentation complains about \param and \returns on function pointers.
6163
#pragma clang diagnostic push
6264
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -65,14 +67,11 @@ typedef struct MemoryReaderImpl {
6567
///
6668
/// \param address the address in the target address space
6769
/// \param size the number of bytes to read
68-
/// \param outFreeFunction on return, a function that the caller will invoke to free
69-
/// the returned memory, or NULL if no action needs to be taken
70-
/// to free the memory
7170
/// \param outFreeContext on return, an arbitrary context pointer that the caller will
7271
/// pass to the free function
7372
/// \returns A pointer to the requested memory, or NULL if the memory could not be read.
74-
/// If outFreeFunction is non-NULL, the caller must invoke it on the returned
75-
/// pointer once it's done using it.
73+
/// The caller must invoke the free function on the returned pointer once it's
74+
/// done using the memory.
7675
ReadBytesFunction readBytes;
7776

7877
/// Get the string length at the given address.

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ SwiftReflectionContextRef
4545
swift_reflection_createReflectionContext(
4646
void *ReaderContext,
4747
uint8_t PointerSize,
48-
ReadBytesFunction readBytes,
49-
GetStringLengthFunction getStringLength,
50-
GetSymbolAddressFunction getSymbolAddress);
48+
FreeBytesFunction Free,
49+
ReadBytesFunction ReadBytes,
50+
GetStringLengthFunction GetStringLength,
51+
GetSymbolAddressFunction GetSymbolAddress);
5152

5253
/// Destroys an opaque reflection context.
5354
void

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorLegacyInterop.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ struct SwiftReflectionFunctions {
5959
SwiftReflectionContextRef (*createReflectionContext)(
6060
void *ReaderContext,
6161
uint8_t PointerSize,
62-
ReadBytesFunction readBytes,
63-
GetStringLengthFunction getStringLength,
64-
GetSymbolAddressFunction getSymbolAddress);
62+
FreeBytesFunction FreeBytes,
63+
ReadBytesFunction ReadBytes,
64+
GetStringLengthFunction GetStringLength,
65+
GetSymbolAddressFunction GetSymbolAddress);
6566

6667
SwiftReflectionContextRef (*createReflectionContextLegacy)(
6768
void *ReaderContext,
@@ -152,6 +153,7 @@ struct SwiftReflectionInteropContextLibrary {
152153

153154
struct SwiftReflectionInteropContext {
154155
void *ReaderContext;
156+
FreeBytesFunction FreeBytes;
155157
ReadBytesFunction ReadBytes;
156158
uint64_t (*GetStringLength)(void *reader_context,
157159
swift_addr_t address);
@@ -251,15 +253,14 @@ static int swift_reflection_interop_readBytesAdapter(void *reader_context,
251253
SwiftReflectionInteropContextRef Context =
252254
(SwiftReflectionInteropContextRef)reader_context;
253255

254-
FreeBytesFunction FreeFunction;
255256
void *FreeContext;
256257
const void *ptr = Context->ReadBytes(Context->ReaderContext, address, size,
257-
&FreeFunction, &FreeContext);
258+
&FreeContext);
258259
if (ptr == NULL)
259260
return 0;
260261

261262
memcpy(dest, ptr, size);
262-
FreeFunction(ptr, FreeContext);
263+
Context->FreeBytes(Context->ReaderContext, ptr, FreeContext);
263264
return 1;
264265
}
265266

@@ -288,9 +289,10 @@ swift_reflection_interop_createReflectionContext(
288289
void *LibraryHandle,
289290
void *LegacyLibraryHandle,
290291
uint8_t PointerSize,
291-
ReadBytesFunction readBytes,
292-
GetStringLengthFunction getStringLength,
293-
GetSymbolAddressFunction getSymbolAddress) {
292+
FreeBytesFunction FreeBytes,
293+
ReadBytesFunction ReadBytes,
294+
GetStringLengthFunction GetStringLength,
295+
GetSymbolAddressFunction GetSymbolAddress) {
294296

295297
SwiftReflectionInteropContextRef ContextRef =
296298
(SwiftReflectionInteropContextRef)calloc(sizeof(*ContextRef), 1);
@@ -313,14 +315,15 @@ swift_reflection_interop_createReflectionContext(
313315
swift_reflection_interop_GetSymbolAddressAdapter);
314316
} else {
315317
Library->Context = Library->Functions.createReflectionContext(ReaderContext,
316-
PointerSize, readBytes, getStringLength, getSymbolAddress);
318+
PointerSize, FreeBytes, ReadBytes, GetStringLength, GetSymbolAddress);
317319
}
318320
}
319321

320322
ContextRef->ReaderContext = ReaderContext;
321-
ContextRef->ReadBytes = readBytes;
322-
ContextRef->GetStringLength = getStringLength;
323-
ContextRef->GetSymbolAddress = getSymbolAddress;
323+
ContextRef->FreeBytes = FreeBytes;
324+
ContextRef->ReadBytes = ReadBytes;
325+
ContextRef->GetStringLength = GetStringLength;
326+
ContextRef->GetSymbolAddress = GetSymbolAddress;
324327

325328
return ContextRef;
326329
}
@@ -358,31 +361,28 @@ swift_reflection_interop_addImageLegacy(
358361
SwiftReflectionInteropContextRef ContextRef,
359362
struct SwiftReflectionInteropContextLibrary *Library,
360363
swift_addr_t imageStart) {
361-
FreeBytesFunction FreeFunction;
362364
void *FreeContext;
363365
const void *Buf;
364366
Buf = ContextRef->ReadBytes(ContextRef->ReaderContext,
365367
imageStart,
366368
sizeof(MachHeader),
367-
&FreeFunction,
368369
&FreeContext);
369370
if (Buf == NULL)
370371
return 0;
371372

372373
MachHeader *Header = (MachHeader *)Buf;
373374

374375
if (Header->magic != MH_MAGIC && Header->magic != MH_MAGIC_64) {
375-
FreeFunction(Buf, ContextRef->ReaderContext);
376+
ContextRef->FreeBytes(ContextRef->ReaderContext, Buf, FreeContext);
376377
return 0;
377378
}
378379

379380
uint32_t Length = Header->sizeofcmds;
380-
FreeFunction(Buf, ContextRef->ReaderContext);
381+
ContextRef->FreeBytes(ContextRef->ReaderContext, Buf, FreeContext);
381382

382383
Buf = ContextRef->ReadBytes(ContextRef->ReaderContext,
383384
imageStart,
384385
Length,
385-
&FreeFunction,
386386
&FreeContext);
387387
Header = (MachHeader *)Buf;
388388
// TODO: free the stuff when we're done

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,22 @@ swift_reflection_getSupportedMetadataVersion() {
5454
SwiftReflectionContextRef
5555
swift_reflection_createReflectionContext(void *ReaderContext,
5656
uint8_t PointerSize,
57-
ReadBytesFunction readBytes,
58-
GetStringLengthFunction getStringLength,
59-
GetSymbolAddressFunction getSymbolAddress) {
57+
FreeBytesFunction Free,
58+
ReadBytesFunction ReadBytes,
59+
GetStringLengthFunction GetStringLength,
60+
GetSymbolAddressFunction GetSymbolAddress) {
6061
assert((PointerSize == 4 || PointerSize == 8) && "We only support 32-bit and 64-bit.");
61-
auto getSize = PointerSize == 4
62+
auto GetSize = PointerSize == 4
6263
? [](void *){ return (uint8_t)4; }
6364
: [](void *){ return (uint8_t)8; };
6465
MemoryReaderImpl ReaderImpl {
6566
ReaderContext,
66-
getSize,
67-
getSize,
68-
readBytes,
69-
getStringLength,
70-
getSymbolAddress
67+
GetSize,
68+
GetSize,
69+
Free,
70+
ReadBytes,
71+
GetStringLength,
72+
GetSymbolAddress
7173
};
7274

7375
return new SwiftReflectionContext(ReaderImpl);

tools/swift-reflection-test/swift-reflection-test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ void PipeMemoryReader_collectBytesFromPipe(const PipeMemoryReader *Reader,
9595
}
9696
}
9797

98-
static void PipeMemoryReader_freeBytes(const void *bytes, void *context) {
98+
static void PipeMemoryReader_freeBytes(void *reader_context, const void *bytes,
99+
void *context) {
99100
free((void *)bytes);
100101
}
101102

102103
static
103104
const void *PipeMemoryReader_readBytes(void *Context, swift_addr_t Address,
104105
uint64_t Size,
105-
FreeBytesFunction *outFreeFunction,
106106
void **outFreeContext) {
107107
const PipeMemoryReader *Reader = (const PipeMemoryReader *)Context;
108108
uintptr_t TargetAddress = Address;
@@ -115,7 +115,6 @@ const void *PipeMemoryReader_readBytes(void *Context, swift_addr_t Address,
115115
void *Buf = malloc(Size);
116116
PipeMemoryReader_collectBytesFromPipe(Reader, Buf, Size);
117117

118-
*outFreeFunction = PipeMemoryReader_freeBytes;
119118
*outFreeContext = NULL;
120119

121120
return Buf;
@@ -288,6 +287,7 @@ int doDumpHeapInstance(const char *BinaryFilename) {
288287
SwiftReflectionContextRef RC = swift_reflection_createReflectionContext(
289288
(void*)&Pipe,
290289
sizeof(void *),
290+
PipeMemoryReader_freeBytes,
291291
PipeMemoryReader_readBytes,
292292
PipeMemoryReader_getStringLength,
293293
PipeMemoryReader_getSymbolAddress);

unittests/Reflection/RemoteMirrorInterop/test.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
return Handle;
1515
}
1616

17-
void NopFree(const void *bytes, void *context) {}
17+
void NopFree(void *reader_context, const void *bytes, void *context) {
18+
assert(reader_context == (void *)0xdeadbeef);
19+
assert(context == (void *)0xfeedface);
20+
}
1821

1922
const void *ReadBytes(void *context, swift_addr_t address, uint64_t size,
20-
FreeBytesFunction *outFreeFunction, void **outFreeContext) {
23+
void **outFreeContext) {
2124
assert(context == (void *)0xdeadbeef);
22-
*outFreeFunction = NopFree;
23-
*outFreeContext = NULL;
25+
*outFreeContext = (void *)0xfeedface;
2426
return (void *)address;
2527
}
2628

@@ -59,6 +61,7 @@ int main(int argc, char **argv) {
5961
Mirror5Handle,
6062
Mirror4Handle,
6163
sizeof(void *),
64+
NopFree,
6265
ReadBytes,
6366
GetStringLength,
6467
GetSymbolAddress);

0 commit comments

Comments
 (0)