Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

fix: exclude uninitialized executable memory from static code

Sennett Lau3c6a2386e402
diff --git a/recomp/elf.cpp b/recomp/elf.cpp
index c751142..62fc076 100644
--- a/recomp/elf.cpp
+++ b/recomp/elf.cpp
@@ -44,7 +44,7 @@ Image Image::load(const std::string &path) {
// game rewrites it at runtime, so it holds no code to translate here
// and any call into it walks to the images the game puts there.
if (!filesz && (flags & 1u)) continue;
- Segment segment{vaddr, std::vector<std::uint8_t>(memsz), (flags & 1u) != 0};
+ Segment segment{vaddr, std::vector<std::uint8_t>(memsz), (flags & 1u) != 0, filesz};
std::memcpy(segment.bytes.data(), data.data() + offset, filesz);
image.segments_.push_back(std::move(segment));
}
@@ -74,7 +74,8 @@ Image Image::load(const std::string &path) {
Image Image::raw(std::uint32_t address, std::vector<std::uint8_t> bytes, std::pair<std::uint32_t, std::uint32_t> text) {
Image image;
- image.segments_.push_back(Segment{address, std::move(bytes), true});
+ const auto initialized_size = static_cast<std::uint32_t>(bytes.size());
+ image.segments_.push_back(Segment{address, std::move(bytes), true, initialized_size});
image.text_ = text;
return image;
}
@@ -91,8 +92,10 @@ std::optional<std::uint32_t> Image::word(std::uint32_t address) const {
}
bool Image::executable(std::uint32_t address) const {
+ // Zero-filled memory is data until the game loads code there. Translating its
+ // initial zeroes would mask a fixed-address overlay loaded into that space.
for (const auto &segment : segments_)
- if (segment.executable && address >= segment.address && address - segment.address < segment.bytes.size())
+ if (segment.executable && address >= segment.address && address - segment.address < segment.initialized_size)
return true;
return false;
}
diff --git a/recomp/elf.hpp b/recomp/elf.hpp
index 23c398f..c18e342 100644
--- a/recomp/elf.hpp
+++ b/recomp/elf.hpp
@@ -13,6 +13,7 @@ struct Segment {
std::uint32_t address{};
std::vector<std::uint8_t> bytes;
bool executable{};
+ std::uint32_t initialized_size{};
};
class Image {
diff --git a/tests/elf_bss.py b/tests/elf_bss.py
new file mode 100644
index 0000000..68982ee
--- /dev/null
+++ b/tests/elf_bss.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+"""Do not translate the zero-filled tail of an executable ELF load segment."""
+import argparse
+from pathlib import Path
+import struct
+import subprocess
+import tempfile
+
+root = Path(__file__).resolve().parents[1]
+parser = argparse.ArgumentParser(description=__doc__)
+parser.add_argument('--recompiler', type=Path, default=root / 'build/tools/eerecomp')
+args = parser.parse_args()
+
+with tempfile.TemporaryDirectory(prefix='ps2-bss-test-') as temp:
+ work = Path(temp)
+ data = bytearray(0x108)
+ data[:7] = b'\x7fELF\x01\x01\x01'
+ struct.pack_into('<HHIIIIIHHHHHH', data, 16, 2, 8, 1, 0x100000, 52, 0, 0, 52, 32, 1, 40, 0, 0)
+ # One executable segment containing jr ra; nop, followed by zero-filled BSS.
+ struct.pack_into('<8I', data, 52, 1, 0x100, 0x100000, 0, 8, 0x1000, 5, 4)
+ struct.pack_into('<2I', data, 0x100, 0x03e00008, 0)
+ (work / 'input.elf').write_bytes(data)
+ # A runtime-discovered overlay entry inside BSS must not register the
+ # executable's initial zeroes as the implementation of that address.
+ (work / 'entries.txt').write_text('100100\n')
+ result = subprocess.run([str(args.recompiler.resolve()), str(work / 'input.elf'),
+ '--entries', str(work / 'entries.txt'), '--out', str(work / 'generated')],
+ check=True, capture_output=True, text=True)
+ assert 'code 2 instructions' in result.stdout, result.stdout
+print('Executable BSS regression passed')