relinker --to-rosetta: signed integer overflow in Amd64OnlyConverter island placement for a code segment mapped across 2^63 #11198

Closed
opened 2026-10-25 21:41:24 +00:00 by zaryabzip · 0 comments
zaryabzip commented 2026-10-25 21:41:24 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-25T21:41:24Z.


What happens

In --to-rosetta 256-bit store lowering, _islandAt and _buildIsland subtract addresses as std::int64_t. In a code segment that crosses 2^63 this overflows: Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775799 cannot be represented in type 'std::int64_t'.

Title, test or build step

The island case of converterWideStoreWindows (core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp:853-865), run with the code segment mapped at 0x8000000000000000 - 0x10 instead of 0x1000. No retail title.

AnyPS5 commit

main 9c6d153dec, with no PRs on top.

System

macOS 27.0 arm64, Apple clang 21.0.0, -fsanitize=undefined -fno-sanitize-recover=undefined. The converter runs as plain C++ here, so no x86-64 host was needed.

Steps to reproduce

  1. Save this driver as IslandRepro.cpp. It uses the same bytes as the existing island case and converts them at both base addresses:
    #include <codegen/IAmd64OnlyConverter.hpp>
    #include <algorithm>
    #include <cstdint>
    #include <cstdio>
    #include <vector>
    
    using Bytes = std::vector<std::uint8_t>;
    
    int main() {
        const auto converter = Codegen::MakeAmd64OnlyConverter(Codegen::Amd64OnlyTarget::Rosetta);
        Bytes text = {0xE8, 0x00, 0x00, 0x00, 0x00, 0xC5, 0xFC, 0x11, 0x07, 0xC3, 0xEB, 0xFD};
        for (int index = 0; index < 4; ++index)
            text.insert(text.end(), {0x48, 0x83, 0xC1, 0x01});
        for (const std::uint64_t base : {std::uint64_t{0x1000}, std::uint64_t{0x8000000000000000} - 0x10}) {
            Bytes file(0x300 + text.size(), 0xCC);
            std::copy(text.begin(), text.end(), file.begin() + 0x200);
            const auto result = converter->Convert(file, {{1, 5, 0x200, base, 0, text.size(), text.size(), 16}});
            std::printf("base 0x%llx: %zu trampolines, short jump %02X %02X\n", static_cast<unsigned long long>(base), result.Trampolines.size(), result.Bytes[0x205], result.Bytes[0x206]);
        }
    }
    
  2. From core/relinker, compile it with the sources of the amd64_only_converter_tests target (core/relinker/CMakeLists.txt) in place of the test file, then run it:
    clang++ -std=c++20 -O1 -g -fsanitize=undefined -fno-sanitize-recover=undefined \
      -Idomain/include -Icodegen/include -Ielfpatcher/include -Iio/include -I../libs/nid/include \
      <the target's .cpp files except Amd64OnlyConverterTests.cpp> IslandRepro.cpp -o island_repro
    ./island_repro
    
    It stops at once.

Log

codegen/src/Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775799 cannot be represented in type 'std::int64_t'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior codegen/src/Amd64OnlyConverter.cpp:169:66
base 0x1000: 2 trampolines, short jump EB 0D

Built with -fsanitize=signed-integer-overflow, so that it carries on after each report, the same run also reports the other three subtractions in the island path:

codegen/src/Amd64OnlyConverter.cpp:203:55: runtime error: signed integer overflow: 9223372036854775797 - -9223372036854775804 cannot be represented in type 'std::int64_t'
codegen/src/Amd64OnlyConverter.cpp:205:63: runtime error: signed integer overflow: 9223372036854775801 - -9223372036854775804 cannot be represented in type 'std::int64_t'
codegen/src/Amd64OnlyConverter.cpp:213:113: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775799 cannot be represented in type 'std::int64_t'

What was checked

  • The four sites:

    • 169 is reach in _islandAt.
    • 203 is the Seek back to the store in _buildIsland.
    • 205 is the JumpTo after the store.
    • 213 is the short-jump displacement written over the store.

    In every case the store is just below 2^63 and the island is just above it. The true distance is a few bytes, but the signed operands have opposite signs, so the difference cannot be represented.

  • Without sanitizers (-O2), both bases print 2 trampolines, short jump EB 0D, the same bytes as the existing test expects at 0x1000. The wrapped difference is correct in practice, so this is undefined behaviour in a malformed-address case, with no wrong output seen.

  • The fixture's E8 and EB FD branches stay below 2^63, so _collectBranchTargets line 282 (#10653, fixed in #10680 and #11033) is not reached first. #10680 changes only lines 277-290, so this path is separate.

  • I reproduced this through Convert directly, not through a crafted ELF on the relinker command line.

  • Same result every run.

  • One possible fix is to subtract the addresses as std::uint64_t and check the range afterwards, as the displacement helper in elfpatcher/src/linux/LinuxGuestModuleWriter.cpp:255-266 does.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6029660199 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/11198). Originally posted by [zaryabzip](https://github.com/zaryabzip) on 2026-10-25T21:41:24Z. --- ### What happens In `--to-rosetta` 256-bit store lowering, `_islandAt` and `_buildIsland` subtract addresses as `std::int64_t`. In a code segment that crosses 2^63 this overflows: `Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775799 cannot be represented in type 'std::int64_t'`. ### Title, test or build step The island case of `converterWideStoreWindows` (`core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp:853-865`), run with the code segment mapped at `0x8000000000000000 - 0x10` instead of `0x1000`. No retail title. ### AnyPS5 commit `main` 9c6d153dec, with no PRs on top. ### System macOS 27.0 arm64, Apple clang 21.0.0, `-fsanitize=undefined -fno-sanitize-recover=undefined`. The converter runs as plain C++ here, so no x86-64 host was needed. ### Steps to reproduce 1. Save this driver as `IslandRepro.cpp`. It uses the same bytes as the existing island case and converts them at both base addresses: ```cpp #include <codegen/IAmd64OnlyConverter.hpp> #include <algorithm> #include <cstdint> #include <cstdio> #include <vector> using Bytes = std::vector<std::uint8_t>; int main() { const auto converter = Codegen::MakeAmd64OnlyConverter(Codegen::Amd64OnlyTarget::Rosetta); Bytes text = {0xE8, 0x00, 0x00, 0x00, 0x00, 0xC5, 0xFC, 0x11, 0x07, 0xC3, 0xEB, 0xFD}; for (int index = 0; index < 4; ++index) text.insert(text.end(), {0x48, 0x83, 0xC1, 0x01}); for (const std::uint64_t base : {std::uint64_t{0x1000}, std::uint64_t{0x8000000000000000} - 0x10}) { Bytes file(0x300 + text.size(), 0xCC); std::copy(text.begin(), text.end(), file.begin() + 0x200); const auto result = converter->Convert(file, {{1, 5, 0x200, base, 0, text.size(), text.size(), 16}}); std::printf("base 0x%llx: %zu trampolines, short jump %02X %02X\n", static_cast<unsigned long long>(base), result.Trampolines.size(), result.Bytes[0x205], result.Bytes[0x206]); } } ``` 2. From `core/relinker`, compile it with the sources of the `amd64_only_converter_tests` target (`core/relinker/CMakeLists.txt`) in place of the test file, then run it: ``` clang++ -std=c++20 -O1 -g -fsanitize=undefined -fno-sanitize-recover=undefined \ -Idomain/include -Icodegen/include -Ielfpatcher/include -Iio/include -I../libs/nid/include \ <the target's .cpp files except Amd64OnlyConverterTests.cpp> IslandRepro.cpp -o island_repro ./island_repro ``` It stops at once. ### Log ``` codegen/src/Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775799 cannot be represented in type 'std::int64_t' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior codegen/src/Amd64OnlyConverter.cpp:169:66 base 0x1000: 2 trampolines, short jump EB 0D ``` Built with `-fsanitize=signed-integer-overflow`, so that it carries on after each report, the same run also reports the other three subtractions in the island path: ``` codegen/src/Amd64OnlyConverter.cpp:203:55: runtime error: signed integer overflow: 9223372036854775797 - -9223372036854775804 cannot be represented in type 'std::int64_t' codegen/src/Amd64OnlyConverter.cpp:205:63: runtime error: signed integer overflow: 9223372036854775801 - -9223372036854775804 cannot be represented in type 'std::int64_t' codegen/src/Amd64OnlyConverter.cpp:213:113: runtime error: signed integer overflow: -9223372036854775804 - 9223372036854775799 cannot be represented in type 'std::int64_t' ``` ### What was checked - The four sites: - 169 is `reach` in `_islandAt`. - 203 is the `Seek` back to the store in `_buildIsland`. - 205 is the `JumpTo` after the store. - 213 is the short-jump displacement written over the store. In every case the store is just below 2^63 and the island is just above it. The true distance is a few bytes, but the signed operands have opposite signs, so the difference cannot be represented. - Without sanitizers (`-O2`), both bases print `2 trampolines, short jump EB 0D`, the same bytes as the existing test expects at 0x1000. The wrapped difference is correct in practice, so this is undefined behaviour in a malformed-address case, with no wrong output seen. - The fixture's `E8` and `EB FD` branches stay below 2^63, so `_collectBranchTargets` line 282 (#10653, fixed in #10680 and #11033) is not reached first. #10680 changes only lines 277-290, so this path is separate. - I reproduced this through `Convert` directly, not through a crafted ELF on the `relinker` command line. - Same result every run. - One possible fix is to subtract the addresses as `std::uint64_t` and check the range afterwards, as the `displacement` helper in `elfpatcher/src/linux/LinuxGuestModuleWriter.cpp:255-266` does.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#11198
No description provided.