fix(llvm): keep Mach-O relocation addends out of indirect tables

This commit is contained in:
shenlei
2026-11-24 17:33:22 +09:00
parent 47a7acc741
commit d4626203e1
4 changed files with 145 additions and 103 deletions
+70
View File
@@ -2,6 +2,7 @@ import hashlib
import json
from pathlib import Path
import re
import struct
import subprocess
import sys
import tempfile
@@ -12,6 +13,7 @@ TOOLS_LLVM_OBF = ROOT / 'tools' / 'llvm_obf'
sys.path.insert(0, str(TOOLS_LLVM_OBF))
from ig_toolchain_resolver import detect_stock_tool, resolve_opt # noqa: E402
from verify_macho_indirect_calls import parse_macho_sections # noqa: E402
def require_host_opt() -> str:
@@ -27,6 +29,74 @@ def macos_sdk_path() -> str:
class TestIndirectCallPass(unittest.TestCase):
def test_ios_linker_preserves_indirect_call_destinations(self):
opt_path = require_host_opt()
clang = detect_stock_tool('clang')
sdk = subprocess.check_output(
['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'], text=True,
).strip()
target = ['-target', 'arm64-apple-ios15.0', '-isysroot', sdk]
with tempfile.TemporaryDirectory(prefix='ig-icall-ios-link-') as td:
p = Path(td)
source = p / 'ios_call.c'
source.write_text(
'__attribute__((noinline)) int ios_add(int x) { return x + 7; }\n'
'__attribute__((noinline)) int ios_sub(int x) { return x - 9; }\n'
'int main(int argc, char **argv) { return ios_add(argc) + ios_sub(argc); }\n',
encoding='utf-8',
)
bc = p / 'ios_call.bc'
subprocess.run([
clang, *target, '-O2', '-emit-llvm', '-c', str(source), '-o', str(bc),
], check=True)
for dual in (False, True):
with self.subTest(dual=dual):
policy = p / 'policy.json'
policy.write_text(json.dumps({
'indirect_call_targets': [{'path_contains': source.name}],
'indirect_call_dual_table': dual,
}), encoding='utf-8')
obf = p / 'obf.bc'
obj = p / 'obf.o'
binary = p / 'app'
subprocess.run([
opt_path, '-passes=ig-obfuscate', f'--ig-policy={policy}',
'--ig-mode=full', '--ig-pass-filter=icall',
'--ig-seed=ios_call_relocation_regression',
f'--ig-source-path={source}', str(bc), '-o', str(obf),
], check=True)
subprocess.run([
clang, *target, '-O2', '-Xclang', '-disable-llvm-passes',
'-x', 'ir', '-c', str(obf), '-o', str(obj),
], check=True)
subprocess.run([
clang, *target, '-Wl,-dead_strip', str(obj), '-o', str(binary),
], check=True)
nm = subprocess.check_output(['xcrun', 'nm', '-n', str(binary)], text=True)
symbols = {}
for line in nm.splitlines():
fields = line.split()
if len(fields) == 3:
address, _, name = fields
symbols[name] = int(address, 16)
anchor, = [addr for name, addr in symbols.items() if name.startswith('___ig_ca_')]
prefix = '___ig_cs1_' if dual else '___ig_ct_'
table, = [addr for name, addr in symbols.items() if name.startswith(prefix)]
section = next(
section for name, section in parse_macho_sections(binary).items()
if not name.endswith(',*')
and section['addr'] <= table
and table + 16 <= section['addr'] + section['size']
)
offset = section['offset'] + table - section['addr']
deltas = struct.unpack_from('<2q', binary.read_bytes(), offset)
self.assertCountEqual(
[anchor + delta for delta in deltas],
[symbols['_ios_add'], symbols['_ios_sub']],
)
def test_c_same_tu_cross_tu_dead_strip_and_dylib_safety(self):
opt_path = require_host_opt()
clang = detect_stock_tool('clang')