fix(relinker): escape control characters in call registry JSON

This commit is contained in:
Aarav
2026-10-07 16:05:40 +05:30
parent 0d3c1eec89
commit 8ab22b0954
3 changed files with 52 additions and 1 deletions
+2
View File
@@ -234,6 +234,8 @@ if(BUILD_TESTING)
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_input_magic.py $<TARGET_FILE:relinker>)
add_test(NAME string_table_bounds COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_string_table_bounds.py $<TARGET_FILE:relinker>)
add_test(NAME call_registry COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_call_registry.py $<TARGET_FILE:relinker>)
else()
message(STATUS "Python3 interpreter not found; skipping Python relinker tests")
endif()
@@ -13,7 +13,13 @@ static std::string _jsonString(const std::string& s) {
else if (c == '\n') out += "\\n";
else if (c == '\r') out += "\\r";
else if (c == '\t') out += "\\t";
else out.push_back(c);
else if (static_cast<unsigned char>(c) < 0x20) {
constexpr char digits[] = "0123456789abcdef";
const auto value = static_cast<unsigned char>(c);
out += "\\u00";
out.push_back(digits[value >> 4]);
out.push_back(digits[value & 0x0f]);
} else out.push_back(c);
}
out.push_back('"');
return out;
@@ -0,0 +1,43 @@
import json
from pathlib import Path
import struct
import subprocess
import sys
import tempfile
from test_string_table_bounds import fixture
def main():
relinker = Path(sys.argv[1]).resolve()
names = ["symbol", 'quote"slash\\', "caf\u00e9-\u007f"]
names.extend("symbol" + chr(value) for value in range(1, 32))
with tempfile.TemporaryDirectory(prefix="anyps5-call-registry-") as directory:
work = Path(directory)
for index, name in enumerate(names):
strings = b"\0lib.so\0" + name.encode("utf-8") + b"\0"
image = fixture(str_size=len(strings), table_bytes=strings)
struct.pack_into("<H", image, 56, 5)
for slot in (3, 4):
struct.pack_into("<IIQQQQQQ", image, 64 + slot * 56,
0x6fffff01, 0, 0, 0, 0, 0, 0, 1)
source = work / (str(index) + ".elf")
source.write_bytes(image)
for options in ([], ["--windows"]):
output = work / (str(index) + ("-windows.exe" if options else "-linux.out"))
result = subprocess.run([str(relinker), "--skip-sce-module", "--registry", *options,
str(source), str(output)], capture_output=True, text=True, timeout=20)
if result.returncode != 0 or not output.exists():
raise AssertionError((name, options, result.returncode, result.stdout, result.stderr))
registry = output.with_suffix(".registry.json")
entries = json.loads(registry.read_text(encoding="utf-8"))
expected = [{"nid": name, "library": "", "relocationType": "R_X86_64_GLOB_DAT",
"relocationOffset": "0x700", "targetSection": ".got", "targetOffset": "0x300",
"callSites": ["0x200"], "callSitesResolved": True}]
if entries != expected:
raise AssertionError((name, options, entries))
print("Call registry JSON tests passed")
if __name__ == "__main__":
main()