Skip to content

Commit ddc81e1

Browse files
committed
Reflection: Some more refactoring for swift-reflection-test, NFC
1 parent 9627aa9 commit ddc81e1

File tree

2 files changed

+56
-99
lines changed

2 files changed

+56
-99
lines changed

include/swift/SwiftRemoteMirror/MemoryReaderInterface.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ extern "C" {
2929
typedef uint64_t addr_t;
3030

3131
typedef uint8_t (*PointerSizeFunction)(void *reader_context);
32-
typedef uint8_t (*SizeSizeFunction)(void * reader_context);
33-
typedef int (*ReadBytesFunction)(void * reader_context, addr_t address,
34-
uint8_t *dest, uint64_t size);
35-
typedef uint64_t (*GetStringLengthFunction)(void * reader_context,
32+
typedef uint8_t (*SizeSizeFunction)(void *reader_context);
33+
typedef int (*ReadBytesFunction)(void *reader_context, addr_t address,
34+
void *dest, uint64_t size);
35+
typedef uint64_t (*GetStringLengthFunction)(void *reader_context,
3636
addr_t address);
37-
typedef addr_t (*GetSymbolAddressFunction)(void * reader_context,
37+
typedef addr_t (*GetSymbolAddressFunction)(void *reader_context,
3838
const char *name,
3939
uint64_t name_length);
4040

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

Lines changed: 51 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,14 @@ static void errorAndExit(const char *message) {
5454
abort();
5555
}
5656

57-
RemoteSection makeRemoteSection(uintptr_t StartAddress, uintptr_t Size) {
58-
RemoteSection RS = {
59-
StartAddress,
60-
Size,
61-
StartAddress + Size
62-
};
63-
return RS;
64-
}
65-
6657
swift_reflection_section_t
67-
makeLocalSection(uintptr_t StartAddress, size_t Size) {
58+
makeLocalSection(void *Buffer, RemoteSection Section,
59+
RemoteReflectionInfo Info) {
60+
uintptr_t Base
61+
= (uintptr_t)Buffer + Section.StartAddress - Info.StartAddress;
6862
swift_reflection_section_t LS = {
69-
(void *)StartAddress,
70-
(void *)(StartAddress + Size)
63+
(void *)Base,
64+
(void *)(Base + Section.Size)
7165
};
7266
return LS;
7367
}
@@ -157,7 +151,7 @@ uint8_t PipeMemoryReader_getSizeSize(void *Context) {
157151
}
158152

159153
void PipeMemoryReader_collectBytesFromPipe(const PipeMemoryReader *Reader,
160-
uint8_t *Dest, size_t Size) {
154+
void *Dest, size_t Size) {
161155
int ReadFD = PipeMemoryReader_getParentReadFD(Reader);
162156
while (Size) {
163157
int bytesRead = read(ReadFD, Dest, Size);
@@ -171,7 +165,7 @@ void PipeMemoryReader_collectBytesFromPipe(const PipeMemoryReader *Reader,
171165
}
172166

173167
int PipeMemoryReader_readBytes(void *Context,
174-
addr_t Address, uint8_t *Dest, uint64_t Size) {
168+
addr_t Address, void *Dest, uint64_t Size) {
175169
const PipeMemoryReader *Reader = (const PipeMemoryReader *)Context;
176170
uintptr_t TargetAddress = Address;
177171
int WriteFD = PipeMemoryReader_getParentWriteFD(Reader);
@@ -228,99 +222,68 @@ PipeMemoryReader createPipeMemoryReader() {
228222
return Reader;
229223
}
230224

231-
const swift_reflection_info_t *
232-
PipeMemoryReader_receiveReflectionInfo(const PipeMemoryReader *Reader,
233-
size_t *NumReflectionInfos) {
225+
RemoteSection makeRemoteSection(const PipeMemoryReader *Reader) {
226+
uintptr_t Start;
227+
size_t Size;
228+
229+
PipeMemoryReader_collectBytesFromPipe(Reader, &Start, sizeof(Start));
230+
PipeMemoryReader_collectBytesFromPipe(Reader, &Size, sizeof(Size));
231+
232+
RemoteSection RS = {Start, Size, Start + Size};
233+
return RS;
234+
}
235+
236+
void
237+
PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC,
238+
const PipeMemoryReader *Reader) {
234239
int WriteFD = PipeMemoryReader_getParentWriteFD(Reader);
235240
write(WriteFD, REQUEST_REFLECTION_INFO, 2);
236-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)NumReflectionInfos,
241+
size_t NumReflectionInfos;
242+
PipeMemoryReader_collectBytesFromPipe(Reader, &NumReflectionInfos,
237243
sizeof(NumReflectionInfos));
238244

239-
if (*NumReflectionInfos == 0)
240-
return NULL;
241-
242-
size_t Size = sizeof(RemoteReflectionInfo) * *NumReflectionInfos;
243-
RemoteReflectionInfo *RemoteInfos = (RemoteReflectionInfo*)malloc(Size);
244-
245-
for (size_t i = 0; i < *NumReflectionInfos; ++i) {
246-
uintptr_t fieldmd_start;
247-
size_t fieldmd_size;
248-
uintptr_t assocty_start;
249-
size_t assocty_size;
250-
uintptr_t builtin_start;
251-
size_t builtin_size;
252-
uintptr_t typeref_start;
253-
size_t typeref_size;
254-
uintptr_t reflstr_start;
255-
size_t reflstr_size;
256-
257-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&fieldmd_start,
258-
sizeof(fieldmd_start));
259-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&fieldmd_size,
260-
sizeof(fieldmd_size));
261-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&assocty_start,
262-
sizeof(assocty_start));
263-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&assocty_size,
264-
sizeof(assocty_size));
265-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&builtin_start,
266-
sizeof(builtin_start));
267-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&builtin_size,
268-
sizeof(builtin_size));
269-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&typeref_start,
270-
sizeof(typeref_start));
271-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&typeref_size,
272-
sizeof(typeref_size));
273-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&reflstr_start,
274-
sizeof(reflstr_start));
275-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&reflstr_size,
276-
sizeof(reflstr_size));
245+
if (NumReflectionInfos == 0)
246+
return;
277247

248+
RemoteReflectionInfo *RemoteInfos = calloc(NumReflectionInfos,
249+
sizeof(RemoteReflectionInfo));
250+
if (RemoteInfos == NULL)
251+
errorAndExit("malloc failed");
252+
253+
for (size_t i = 0; i < NumReflectionInfos; ++i) {
278254
RemoteInfos[i] = makeRemoteReflectionInfo(
279-
makeRemoteSection(fieldmd_start, fieldmd_size),
280-
makeRemoteSection(assocty_start, assocty_size),
281-
makeRemoteSection(builtin_start, builtin_size),
282-
makeRemoteSection(typeref_start, typeref_size),
283-
makeRemoteSection(reflstr_start, reflstr_size));
255+
makeRemoteSection(Reader),
256+
makeRemoteSection(Reader),
257+
makeRemoteSection(Reader),
258+
makeRemoteSection(Reader),
259+
makeRemoteSection(Reader));
284260
}
285261

286262
// Now pull in the remote sections into our address space.
287263

288-
swift_reflection_info_t *Infos
289-
= malloc(sizeof(swift_reflection_info_t) * *NumReflectionInfos);
290-
291-
for (size_t i = 0; i < *NumReflectionInfos; ++i) {
264+
for (size_t i = 0; i < NumReflectionInfos; ++i) {
292265
RemoteReflectionInfo RemoteInfo = RemoteInfos[i];
293266

294-
uintptr_t buffer = (uintptr_t)malloc(RemoteInfo.TotalSize);
267+
void *Buffer = malloc(RemoteInfo.TotalSize);
295268

296-
int Success = PipeMemoryReader_readBytes((void*)Reader,
269+
int Success = PipeMemoryReader_readBytes((void *)Reader,
297270
RemoteInfo.StartAddress,
298-
(uint8_t*)buffer,
271+
Buffer,
299272
RemoteInfo.TotalSize);
300273
if (!Success)
301274
errorAndExit("Couldn't read reflection information");
302275

303-
uintptr_t fieldmd_base
304-
= buffer + RemoteInfo.fieldmd.StartAddress - RemoteInfo.StartAddress;
305-
uintptr_t assocty_base
306-
= buffer + RemoteInfo.assocty.StartAddress - RemoteInfo.StartAddress;
307-
uintptr_t builtin_base
308-
= buffer + RemoteInfo.builtin.StartAddress - RemoteInfo.StartAddress;
309-
uintptr_t typeref_base
310-
= buffer + RemoteInfo.typeref.StartAddress - RemoteInfo.StartAddress;
311-
uintptr_t reflstr_base
312-
= buffer + RemoteInfo.reflstr.StartAddress - RemoteInfo.StartAddress;
313-
314276
swift_reflection_info_t Info = {
315-
makeLocalSection(fieldmd_base, RemoteInfo.fieldmd.Size),
316-
makeLocalSection(assocty_base, RemoteInfo.assocty.Size),
317-
makeLocalSection(builtin_base, RemoteInfo.builtin.Size),
318-
makeLocalSection(typeref_base, RemoteInfo.typeref.Size),
319-
makeLocalSection(reflstr_base, RemoteInfo.reflstr.Size)
277+
makeLocalSection(Buffer, RemoteInfo.fieldmd, RemoteInfo),
278+
makeLocalSection(Buffer, RemoteInfo.assocty, RemoteInfo),
279+
makeLocalSection(Buffer, RemoteInfo.builtin, RemoteInfo),
280+
makeLocalSection(Buffer, RemoteInfo.typeref, RemoteInfo),
281+
makeLocalSection(Buffer, RemoteInfo.reflstr, RemoteInfo)
320282
};
321-
Infos[i] = Info;
283+
swift_reflection_addReflectionInfo(RC, Info);
322284
}
323-
return Infos;
285+
286+
free(RemoteInfos);
324287
}
325288

326289
uint64_t PipeMemoryReader_getStringLength(void *Context, addr_t Address) {
@@ -329,8 +292,7 @@ uint64_t PipeMemoryReader_getStringLength(void *Context, addr_t Address) {
329292
write(WriteFD, REQUEST_STRING_LENGTH, 2);
330293
write(WriteFD, &Address, sizeof(Address));
331294
uintptr_t Length = 0;
332-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&Length,
333-
sizeof(Length));
295+
PipeMemoryReader_collectBytesFromPipe(Reader, &Length, sizeof(Length));
334296
return Length;
335297
}
336298

@@ -371,12 +333,7 @@ int doDumpHeapInstance(const char *BinaryFilename) {
371333
printf("Instance pointer in child address space: 0x%lx\n",
372334
instance);
373335

374-
size_t NumReflectionInfos = 0;
375-
const swift_reflection_info_t *Infos
376-
= PipeMemoryReader_receiveReflectionInfo(&Pipe, &NumReflectionInfos);
377-
378-
for (size_t i = 0; i < NumReflectionInfos; ++i)
379-
swift_reflection_addReflectionInfo(RC, Infos[i]);
336+
PipeMemoryReader_receiveReflectionInfo(RC, &Pipe);
380337

381338
printf("Type reference:\n");
382339

0 commit comments

Comments
 (0)