Repository navigation
feat(libc): export the num_get and time_get locale objects as data #8880
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46c36c3
feat(libc): export the num_get and time_get locale objects as data
ycbuzi 2657a81
test(relinker): resolve a libSceAmpr-qualified import of the locale o…
ycbuzi 7157ebb
test(relinker): cover libc fallback for Ampr imports
MarcZX10 0c8fb06
test(relinker): register windows_import_identity only when the librar…
ycbuzi e8196fc
Merge branch 'boykopovar:main' into feat/libc-num-get-objects
ycbuzi a946c46
Merge upstream main into feat/libc-num-get-objects
ycbuzi 428e9cb
fix(libc): size the num_get<char> vtable to the console's 17 slots
ycbuzi 6287af9
docs(libc): name the num_get<char> vtable's slots from the console
ycbuzi 2a796cb
Merge branch 'boykopovar:main' into feat/libc-num-get-objects
ycbuzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
core/relinker/elfpatcher/tests/WindowsImportIdentityTests.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #define WIN32_LEAN_AND_MEAN | ||
| #define NOMINMAX | ||
| #include <windows.h> | ||
| #include <elfpatcher/windows/WindowsEntryStubBuilder.hpp> | ||
| #include <elfpatcher/windows/WindowsImportBuilder.hpp> | ||
| #include <elfpatcher/windows/WindowsPeWriter.hpp> | ||
| #include <array> | ||
| #include <cstdint> | ||
| #include <filesystem> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <random> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
|
|
||
| namespace Fs = std::filesystem; | ||
| using namespace Elfpatcher::Windows; | ||
|
|
||
| constexpr std::uint32_t GotRva = LoadRva + 0x1000; | ||
| constexpr std::array<const char*, 3> LocaleObjects{"-mLzBSk-VGs", "a54t8+k7KpY", "KfcTPbeaOqg"}; | ||
|
|
||
| void require(const bool condition, const std::string& message) { | ||
| if (!condition) throw std::runtime_error(message); | ||
| } | ||
|
|
||
| Fs::path createFixtureDirectory(const Fs::path& parent) { | ||
| std::random_device random; | ||
| for (int attempt = 0; attempt < 100; ++attempt) { | ||
| auto name = Fs::path("windows-import-identity-"); | ||
| name += std::to_string(GetCurrentProcessId()) + "-" + std::to_string(random()); | ||
| const auto directory = parent / name; | ||
| if (Fs::create_directory(directory)) return directory; | ||
| } | ||
| throw std::runtime_error("Cannot create a unique import identity fixture directory"); | ||
| } | ||
|
|
||
| void appendDisplacement(std::vector<std::uint8_t>& code, const std::int32_t displacement) { | ||
| for (int i = 0; i < 4; ++i) code.push_back(static_cast<std::uint8_t>((static_cast<std::uint32_t>(displacement) >> (8 * i)) & 0xffu)); | ||
| } | ||
|
|
||
| // For each pair of GOT slots: mov rax,[a]; cmp rax,[b]; jne fail. Then xor eax,eax; ret. fail: mov eax,1; ret. | ||
| std::vector<std::uint8_t> compareCode(const std::vector<std::pair<int, int>>& pairs) { | ||
| std::vector<std::uint8_t> code; | ||
| const auto failOffset = static_cast<std::int32_t>(pairs.size() * 16 + 3); | ||
| for (std::size_t i = 0; i < pairs.size(); ++i) { | ||
| const auto base = static_cast<std::uint32_t>(i * 16); | ||
| code.insert(code.end(), {0x48, 0x8b, 0x05}); | ||
| appendDisplacement(code, static_cast<std::int32_t>(GotRva + 8u * static_cast<std::uint32_t>(pairs[i].first)) - static_cast<std::int32_t>(LoadRva + base + 7)); | ||
| code.insert(code.end(), {0x48, 0x3b, 0x05}); | ||
| appendDisplacement(code, static_cast<std::int32_t>(GotRva + 8u * static_cast<std::uint32_t>(pairs[i].second)) - static_cast<std::int32_t>(LoadRva + base + 14)); | ||
| code.push_back(0x75); | ||
| code.push_back(static_cast<std::uint8_t>(failOffset - static_cast<std::int32_t>(base + 16))); | ||
| } | ||
| code.insert(code.end(), {0x31, 0xc0, 0xc3}); | ||
| code.insert(code.end(), {0xb8, 0x01, 0x00, 0x00, 0x00, 0xc3}); | ||
| return code; | ||
| } | ||
|
|
||
| std::uint32_t slot(const int index) { | ||
| return GotRva + 8u * static_cast<std::uint32_t>(index); | ||
| } | ||
|
|
||
| void generateProbe(const Fs::path& file, const std::string& libraries, const std::vector<PeImport>& peImports, const std::vector<std::pair<int, int>>& pairs) { | ||
| const std::vector<std::string> loaded{"libSceAmpr.prx", "libkernel.prx", "libc.prx"}; | ||
| PeSection entry{".guest", LoadRva, SectionRead | SectionExecute | 0x20u, compareCode(pairs)}; | ||
| PeSection got{".got", GotRva, SectionRead | SectionWrite | 0x40u, std::vector<std::uint8_t>(8 * 8)}; | ||
| const auto imports = WindowsImportBuilder().Build(LoadRva + 0x2000); | ||
| const auto startup = WindowsEntryStubBuilder().Build(LoadRva + 0x3000, entry.Rva, imports, loaded, peImports, libraries, false, false); | ||
| std::array<PeDirectory, 16> directories{}; | ||
| directories[1] = imports.Directory; | ||
| directories[3] = startup.ExceptionDirectory; | ||
| directories[12] = imports.AddressTable; | ||
| const auto image = WindowsPeWriter().Write({entry, got, imports.Section, startup.Data, startup.Code}, startup.Code.Rva, directories, false, ImageBase, false); | ||
| std::ofstream stream(file, std::ios::binary); | ||
| stream.write(reinterpret_cast<const char*>(image.data()), static_cast<std::streamsize>(image.size())); | ||
| require(static_cast<bool>(stream), "Cannot write " + file.string()); | ||
| } | ||
|
|
||
| DWORD runProbe(const Fs::path& probe, std::string& output) { | ||
| SECURITY_ATTRIBUTES security{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE}; | ||
| HANDLE input = nullptr; | ||
| HANDLE written = nullptr; | ||
| require(CreatePipe(&input, &written, &security, 0) && SetHandleInformation(input, HANDLE_FLAG_INHERIT, 0), "Cannot create the probe's pipe"); | ||
| STARTUPINFOW startup{}; | ||
| startup.cb = sizeof(startup); | ||
| startup.dwFlags = STARTF_USESTDHANDLES; | ||
| startup.hStdOutput = written; | ||
| startup.hStdError = written; | ||
| startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | ||
| PROCESS_INFORMATION process{}; | ||
| const auto filename = probe.wstring(); | ||
| require(CreateProcessW(filename.c_str(), nullptr, nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, probe.parent_path().wstring().c_str(), &startup, &process) != 0, | ||
| "Cannot start " + probe.filename().string() + ": " + std::to_string(GetLastError())); | ||
| CloseHandle(written); | ||
| if (WaitForSingleObject(process.hProcess, 120000) != WAIT_OBJECT_0) { | ||
| TerminateProcess(process.hProcess, 1); | ||
| throw std::runtime_error(probe.filename().string() + " timed out"); | ||
| } | ||
| DWORD status = 0; | ||
| require(GetExitCodeProcess(process.hProcess, &status) != 0, "Cannot read the probe's exit code"); | ||
| char buffer[4096]; | ||
| DWORD size = 0; | ||
| while (ReadFile(input, buffer, sizeof(buffer), &size, nullptr) && size != 0) output.append(buffer, size); | ||
| CloseHandle(input); | ||
| CloseHandle(process.hThread); | ||
| CloseHandle(process.hProcess); | ||
| return status; | ||
| } | ||
|
|
||
| // The generated probes load libc.prx next to the other libraries; its compiler runtime DLLs sit next to this test. | ||
| void copyRuntime(const Fs::path& directory) { | ||
| wchar_t self[MAX_PATH]; | ||
| const auto length = GetModuleFileNameW(nullptr, self, MAX_PATH); | ||
| if (length == 0 || length >= MAX_PATH) return; | ||
| const auto own = Fs::path(self).parent_path(); | ||
| for (const char* name : {"libgcc_s_seh-1.dll", "libstdc++-6.dll", "libwinpthread-1.dll"}) { | ||
| std::error_code error; | ||
| if (Fs::exists(own / name, error)) Fs::copy_file(own / name, directory / name, Fs::copy_options::overwrite_existing, error); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| if (argc != 2) { | ||
| std::cerr << "usage: windows_import_identity_tests <patched libraries directory>\n"; | ||
| return 2; | ||
| } | ||
| try { | ||
| const auto libraries = Fs::absolute(argv[1]).string(); | ||
| const auto directory = createFixtureDirectory(Fs::temp_directory_path()); | ||
| copyRuntime(directory); | ||
| std::vector<PeImport> same; | ||
| for (int i = 0; i < 3; ++i) { | ||
| same.push_back({.Name = LocaleObjects[i], .TargetRva = slot(2 * i), .Addend = 0, .Library = "libSceAmpr.prx"}); | ||
| same.push_back({.Name = LocaleObjects[i], .TargetRva = slot(2 * i + 1), .Addend = 0, .Library = "libc.prx"}); | ||
| } | ||
| generateProbe(directory / "same.exe", libraries, same, {{0, 1}, {2, 3}, {4, 5}}); | ||
| const std::vector<PeImport> different{ | ||
| {.Name = LocaleObjects[0], .TargetRva = slot(0), .Addend = 0, .Library = "libSceAmpr.prx"}, | ||
| {.Name = "k+AXqu2-eBc", .TargetRva = slot(6), .Addend = 0, .Library = "libkernel.prx"}, | ||
| }; | ||
| generateProbe(directory / "different.exe", libraries, different, {{0, 6}}); | ||
| std::string output; | ||
| const auto sameStatus = runProbe(directory / "same.exe", output); | ||
| require(sameStatus == 0, "A libSceAmpr.prx-qualified import of the locale objects did not resolve to libc's object (exit " + std::to_string(sameStatus) + ")\n" + output); | ||
| output.clear(); | ||
| const auto differentStatus = runProbe(directory / "different.exe", output); | ||
| require(differentStatus == 1, "The probe did not tell two different addresses apart (exit " + std::to_string(differentStatus) + ")\n" + output); | ||
| std::error_code error; | ||
| Fs::remove_all(directory, error); | ||
| std::cout << "Windows import identity tests passed\n"; | ||
| } catch (const std::exception& error) { | ||
| std::cerr << error.what() << '\n'; | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The PR description relies on
libSceAmpr.prx-qualified imports falling back to the same libc object, but these tests only check the objects through the direct libc-linked test and import_audit provider inventory. Could you add a resolver/entry-stub regression with a scopedlibSceAmpr.prximport that verifies its search order reaches libc (or a Windows runtime probe showing both qualified imports resolve to the same address)? That would pin the cross-module identity claim independently of the current resolver implementation.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.
I checked the current fallback path: on Windows a host import qualified to
libSceAmpr.prxsearches that module and thenlibc.prx, so the intended shared provider looks plausible by inspection. A narrow regression could useWindowsEntryStubBuilderwithlibraries = {"libSceAmpr.prx", "libc.prx"}and one of these NIDs qualified to Ampr, then assert the search table contains both indexes in that order; the libc-qualified case should resolve directly to the same libc index. That would exercise the resolver plan without needing a proprietary title.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.
I added the resolver-plan regression on my fork at
MarcZX10:codex/pr-8880-ampr-libc-resolver-test(commitbabde2b5bd). It asserts that an Ampr-qualifiednum_get::idimport searches Ampr then libc, while the libc-qualified import searches libc directly. On this PR head,entry_stub_testsbuilds and passes on Linux;python tools/check_conventions.py --base upstream/main --head HEADandgit diff --checkalso pass. This verifies the generated search order, not a runtime address comparison; the branch is available to cherry-pick since direct push to your PR branch is unavailable.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.
Both are in now. Your search-order regression is cherry-picked as 7157ebb with your authorship; on this head
entry_stub_testspasses on Windows as well. The runtime side is 2657a81:windows_import_identity(core/relinker/elfpatcher/tests/WindowsImportIdentityTests.cpp, registered next towindows_dependency_diagnostics). It generates two probes withWindowsEntryStubBuilder,WindowsImportBuilderandWindowsPeWriter, the way #8853's generator and the diagnostics test do, and runs them beside the patched libraries (core/libs/libs, so it depends onlibs):same.exeimports the three NIDs twice each, qualified tolibSceAmpr.prxand tolibc.prx, into six GOT slots, and its entry compares each pair (mov rax,[a]; cmp rax,[b]; jne fail), exiting 0 only if all three resolved to one address.different.execompares the first NID fromlibSceAmpr.prxwithgetpagesizefromlibkernel.prxand must exit 1, so a comparison that could not tell addresses apart would fail the test.A failed probe's stub output lands in the test's message (an empty library folder gives
Failed to load module: ...libSceAmpr.prx,GetLastError: 126). Qualifying the second import tolibSceLibcInternal.prxis not possible at this layer, the stub builder only accepts modules from its load list, and the relinker addslibc.prxfor that name before this stage (GuestModuleBuilder.cpp), solibc.prxis the direct reference here.Result on this PC (Windows 11, MinGW GCC 15.2.0, Release):
windows_import_identitypasses (ctest -R windows_import_identity, 0.2 s) afterninja windows_import_identity_tests, which buildslibsfirst. The description's Tested section lists both.