static PipeMemoryReader createPipeMemoryReader() { PipeMemoryReader Reader; if (pipe(Reader.to_child)) errnoAndExit("Couldn't create pipes to child process"); if (pipe(Reader.from_child)) errnoAndExit("Couldn't create pipes from child process"); return Reader; }
static void PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC, const PipeMemoryReader *Reader) { int WriteFD = PipeMemoryReader_getParentWriteFD(Reader); write(WriteFD, REQUEST_REFLECTION_INFO, 2); size_t NumReflectionInfos; PipeMemoryReader_collectBytesFromPipe(Reader, &NumReflectionInfos, sizeof(NumReflectionInfos)); if (NumReflectionInfos == 0) return; RemoteReflectionInfo *RemoteInfos = calloc(NumReflectionInfos, sizeof(RemoteReflectionInfo)); if (RemoteInfos == NULL) errnoAndExit("malloc failed"); for (size_t i = 0; i < NumReflectionInfos; ++i) { RemoteInfos[i] = makeRemoteReflectionInfo( makeRemoteSection(Reader), makeRemoteSection(Reader), makeRemoteSection(Reader), makeRemoteSection(Reader), makeRemoteSection(Reader), makeRemoteSection(Reader)); } // Now pull in the remote sections into our address space. for (size_t i = 0; i < NumReflectionInfos; ++i) { RemoteReflectionInfo RemoteInfo = RemoteInfos[i]; void *Buffer = malloc(RemoteInfo.TotalSize); int Success = PipeMemoryReader_readBytes((void *)Reader, RemoteInfo.StartAddress, Buffer, RemoteInfo.TotalSize); if (!Success) errorAndExit("Couldn't read reflection information"); swift_reflection_info_t Info = { {makeLocalSection(Buffer, RemoteInfo.fieldmd, RemoteInfo), 0}, {makeLocalSection(Buffer, RemoteInfo.assocty, RemoteInfo), 0}, {makeLocalSection(Buffer, RemoteInfo.builtin, RemoteInfo), 0}, {makeLocalSection(Buffer, RemoteInfo.capture, RemoteInfo), 0}, {makeLocalSection(Buffer, RemoteInfo.typeref, RemoteInfo), 0}, {makeLocalSection(Buffer, RemoteInfo.reflstr, RemoteInfo), 0}, /*LocalStartAddress*/ (uintptr_t) Buffer, /*RemoteStartAddress*/ RemoteInfo.StartAddress, }; swift_reflection_addReflectionInfo(RC, Info); } free(RemoteInfos); }
static void PipeMemoryReader_collectBytesFromPipe(const PipeMemoryReader *Reader, void *Dest, size_t Size) { int ReadFD = PipeMemoryReader_getParentReadFD(Reader); while (Size) { int bytesRead = read(ReadFD, Dest, Size); if (bytesRead < 0) if (errno == EINTR) continue; else errnoAndExit("collectBytesFromPipe"); else if (bytesRead == 0) errorAndExit("collectBytesFromPipe: Unexpected end of file"); Size -= bytesRead; Dest += bytesRead; } }
static void PipeMemoryReader_collectBytesFromPipe(const PipeMemoryReader *Reader, void *Dest, size_t Size) { int ReadFD = PipeMemoryReader_getParentReadFD(Reader); while (Size) { int bytesRead = read(ReadFD, Dest, Size); if (bytesRead < 0) if (errno == EINTR) continue; else errnoAndExit("collectBytesFromPipe"); else if (bytesRead == 0) errorAndExit("collectBytesFromPipe: Unexpected end of file"); Size -= bytesRead; // Arithmetic on a void pointer is a GNU extension. Dest = (char*)(Dest) + bytesRead; } }
int doDumpHeapInstance(const char *BinaryFilename) { PipeMemoryReader Pipe = createPipeMemoryReader(); pid_t pid = _fork(); switch (pid) { case -1: errnoAndExit("Couldn't fork child process"); case 0: { // Child: close(PipeMemoryReader_getParentWriteFD(&Pipe)); close(PipeMemoryReader_getParentReadFD(&Pipe)); dup2(PipeMemoryReader_getChildReadFD(&Pipe), STDIN_FILENO); dup2(PipeMemoryReader_getChildWriteFD(&Pipe), STDOUT_FILENO); _execv(BinaryFilename, NULL); exit(EXIT_SUCCESS); } default: { // Parent close(PipeMemoryReader_getChildReadFD(&Pipe)); close(PipeMemoryReader_getChildWriteFD(&Pipe)); SwiftReflectionContextRef RC = swift_reflection_createReflectionContextWithDataLayout( (void *)&Pipe, PipeMemoryReader_queryDataLayout, PipeMemoryReader_freeBytes, PipeMemoryReader_readBytes, PipeMemoryReader_getStringLength, PipeMemoryReader_getSymbolAddress); uint8_t PointerSize = PipeMemoryReader_getPointerSize((void*)&Pipe); if (PointerSize != sizeof(uintptr_t)) errorAndExit("Child process had unexpected architecture"); #if defined(__APPLE__) && defined(__MACH__) PipeMemoryReader_receiveImages(RC, &Pipe); #else PipeMemoryReader_receiveReflectionInfo(RC, &Pipe); #endif while (1) { InstanceKind Kind = PipeMemoryReader_receiveInstanceKind(&Pipe); switch (Kind) { case Object: printf("Reflecting an object.\n"); if (!reflectHeapObject(RC, Pipe)) return EXIT_SUCCESS; break; case Existential: { static const char Name[] = MANGLING_PREFIX_STR "ypD"; swift_typeref_t AnyTR = swift_reflection_typeRefForMangledTypeName(RC, Name, sizeof(Name)-1); printf("Reflecting an existential.\n"); if (!reflectExistential(RC, Pipe, AnyTR)) return EXIT_SUCCESS; break; } case ErrorExistential: { static const char ErrorName[] = MANGLING_PREFIX_STR "s5Error_pD"; swift_typeref_t ErrorTR = swift_reflection_typeRefForMangledTypeName(RC, ErrorName, sizeof(ErrorName)-1); printf("Reflecting an error existential.\n"); if (!reflectExistential(RC, Pipe, ErrorTR)) return EXIT_SUCCESS; break; } case Closure: printf("Reflecting a closure.\n"); if (!reflectHeapObject(RC, Pipe)) return EXIT_SUCCESS; break; case None: swift_reflection_destroyReflectionContext(RC); printf("Done.\n"); return EXIT_SUCCESS; } } } } return EXIT_SUCCESS; }