Repository navigation
Tuanjie Support #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,9 @@ class Dumper : public IDumper | |
| if ((x.Protection & ExecutableSection::kSectionProtectionRead) == 0) | ||
| return false; | ||
|
|
||
| if (x.Data.size() < sizeof(RuntimeTypeArray)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: without this check, sections that are less than the size of the runtime array will loop indefinitely. Linux has a bunch of tiny < 1KiB sections, maybe Android too under certain situations. |
||
| return false; | ||
|
|
||
| return true; | ||
| })) | ||
| { | ||
|
|
@@ -145,6 +148,7 @@ class Dumper : public IDumper | |
| } | ||
| } | ||
|
|
||
| PlatformImpl.DebugLog("Failed to find common string buffer"); | ||
| return nullptr; | ||
| } | ||
| public: | ||
|
|
@@ -169,14 +173,19 @@ class Dumper : public IDumper | |
| TypeTreeShareableData data(label); | ||
| TypeTree tree(&data, label); | ||
|
|
||
| Object * object = nullptr; | ||
| if (!pRTTI->isAbstract && pRTTI->factory) | ||
| { | ||
| Object *object = pRTTI->factory(label, kCreateObjectDefault); | ||
| GenerateTypeTreeTransfer transfer(tree, flags, object, pRTTI->size); | ||
| object->VirtualRedirectTransfer(transfer); | ||
| object = pRTTI->factory(label, kCreateObjectDefault); | ||
| // "THX_Proxy" (what is this) can't be created, so it returns nothing. | ||
| // but it also is not abstract, curious! | ||
| if (object) { | ||
| GenerateTypeTreeTransfer transfer(&tree, flags, object, pRTTI->size); | ||
| object->VirtualRedirectTransfer(transfer); | ||
| } | ||
| } | ||
|
|
||
| Writer.Add(pRTTI, tree, flags, pTable); | ||
| Writer.Add(pRTTI, tree, flags, pTable, object); | ||
| } | ||
|
|
||
| PlatformImpl.DebugLog("Dumped types, now writing to file"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,134 @@ | ||
| #include <cstdarg> | ||
| #include <cstring> | ||
| #include <filesystem> | ||
| #include <span> | ||
| #include <string_view> | ||
| #include <fstream> | ||
| #include <vector> | ||
|
|
||
| #include <link.h> | ||
| #include <dlfcn.h> | ||
| #include <iostream> | ||
| #include <signal.h> | ||
|
|
||
| #include "common.hpp" | ||
| #include "executable.hpp" | ||
| #include "dumper.hpp" | ||
|
|
||
| int main() | ||
| template<Revision R, Variant V> | ||
| class LinuxDumper | ||
| { | ||
| std::cout << "not implemented :(" << "\n"; | ||
| return -1; | ||
| public: | ||
| std::span<ExecutableSection> GetExecutableSections() | ||
| { | ||
| if (CachedSections.empty()) | ||
| { | ||
| struct ElfInfo { | ||
| std::span<const ElfW(Phdr)> Sections; | ||
| uintptr_t BaseAddress{}; | ||
| }; | ||
| ElfInfo libraryInfo{}; | ||
|
|
||
| const auto findResult = dl_iterate_phdr([](dl_phdr_info* info, size_t size, void* context) -> int { | ||
| if (info->dlpi_name[0] == 0 || | ||
| std::string_view(info->dlpi_name).ends_with("/UnityPlayer.so") || std::string_view(info->dlpi_name).ends_with("/TuanjiePlayer.so")) { | ||
| const auto result = static_cast<ElfInfo*>(context); | ||
| result->Sections = std::span(info->dlpi_phdr, info->dlpi_phnum); | ||
| result->BaseAddress = info->dlpi_addr; | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }, &libraryInfo); | ||
|
|
||
| if (!findResult) { | ||
| DebugLog("Failed to find unity :("); | ||
| return {}; | ||
| } | ||
|
|
||
| for (const auto& phdr : libraryInfo.Sections) { | ||
| const auto base = phdr.p_vaddr + libraryInfo.BaseAddress; | ||
| const auto size = phdr.p_memsz; | ||
|
|
||
| auto protection = 0; | ||
| if (phdr.p_flags & PF_R) | ||
| protection |= ExecutableSection::kSectionProtectionRead; | ||
|
|
||
| if (phdr.p_flags & PF_W) | ||
| protection |= ExecutableSection::kSectionProtectionWrite; | ||
|
|
||
| if (phdr.p_flags & PF_X) | ||
| protection |= ExecutableSection::kSectionProtectionExecute; | ||
|
|
||
| CachedSections.emplace_back(std::span(reinterpret_cast<char*>(base), size), protection); | ||
| } | ||
| } | ||
|
|
||
| return CachedSections; | ||
| } | ||
|
|
||
| static std::ofstream CreateOutputFile(char const *filename) | ||
| { | ||
| // Create the output file in the current directory. | ||
| // NOTE: Should this be made configurable? i.e. through an environment variable? | ||
| const auto path = std::filesystem::current_path() / filename; | ||
| dprintf(2, "[TypeTreeRipper] writing '%s'", path.c_str()); | ||
| return std::ofstream(path, std::ios::out | std::ios::binary); | ||
| } | ||
|
|
||
| static void DebugLog(char const *message) | ||
| { | ||
| dprintf(2, "[TypeTreeRipper] %s\n", message); | ||
| } | ||
| private: | ||
| std::vector<ExecutableSection> CachedSections; | ||
| }; | ||
|
|
||
| typedef int (*orig_vfprintf_type)(FILE *stream, const char *format, va_list ap); | ||
|
|
||
| static Revision CachedRevision = Revision::Count; | ||
|
|
||
| int vfprintf(FILE *stream, const char *format, va_list ap) { | ||
| orig_vfprintf_type orig_vfprintf = (orig_vfprintf_type)dlsym(RTLD_NEXT, "vfprintf"); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| char tmp_buffer[0x100]; | ||
| va_list cap; | ||
| va_copy(cap, ap); | ||
| vsnprintf(tmp_buffer, sizeof(tmp_buffer), format, cap); | ||
| va_end(cap); | ||
| tmp_buffer[0xff] = 0; | ||
| // "engine version:" -> Runtime: Version | ||
| // "Editor version:" -> Editor: version | ||
| // is there a better way?? | ||
| if (CachedRevision == Revision::Count && strstr(tmp_buffer, "engine version:")) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Count comparison is necessary because the process forks into a player runtime, making the editor dump from the editor module with player structs. Likely a better way to determine this. |
||
| const auto str = std::string(tmp_buffer); | ||
| if (const auto parsedRevision = VersionStringToRevision(str.substr(str.find(':') + 1)); parsedRevision.has_value()) | ||
| { | ||
| CachedRevision = parsedRevision.value(); | ||
| RunDumper<LinuxDumper>(CachedRevision, Variant::Runtime); | ||
| // this is likely too early. | ||
| dprintf(2, "[TypeTreeRipper] parsed runtime revision"); | ||
| raise(SIGKILL); | ||
| } else { | ||
| dprintf(2, "[TypeTreeRipper] failed to parse revision"); | ||
| } | ||
| } else if (CachedRevision == Revision::Count && strstr(tmp_buffer, "Editor version:")) { | ||
| const auto str = std::string(tmp_buffer); | ||
| if (const auto parsedRevision = VersionStringToRevision(str.substr(str.find(':') + 1)); parsedRevision.has_value()) | ||
| { | ||
| CachedRevision = parsedRevision.value(); | ||
| dprintf(2, "[TypeTreeRipper] parsed editor revision"); | ||
| } else { | ||
| dprintf(2, "[TypeTreeRipper] failed to parse revision"); | ||
| } | ||
| } | ||
|
|
||
| // note: this is late, it also re-initializes managers which can crash unity if it happens during mono reset | ||
| // luckily this needle is printed after that | ||
| if (CachedRevision != Revision::Count && strstr(tmp_buffer, "Finished resetting the current domain")) { | ||
| RunDumper<LinuxDumper>(CachedRevision, Variant::Editor); | ||
| raise(SIGKILL); | ||
| } | ||
|
|
||
| return orig_vfprintf(stream, format, ap); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MemLabelId::String