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
+10 -2
View File
@@ -196,12 +196,12 @@ int main(void) {
# Cross-compile & link for arm64-apple-ios15.0 with -Wl,-dead_strip to validate ld64 PIC relocations
bc_ios = p / 'ibr_ios.bc'
obf_ios = p / 'ibr_ios.obf.bc'
obf_ios = p / 'ibr_ios.obf.ll'
obj_ios = p / 'ibr_ios.o'
bin_ios = p / 'ibr_ios.bin'
subprocess.run([clang, '-target', 'arm64-apple-ios15.0', '-isysroot', sdk_ios, '-Os', '-g', '-emit-llvm', '-c', str(src_c), '-o', str(bc_ios)], check=True)
subprocess.run([
opt_path, '-passes=ig-obfuscate',
opt_path, '-S', '-passes=ig-obfuscate',
f'--ig-policy={policy_path}',
'--ig-mode=full',
'--ig-pass-filter=all',
@@ -209,6 +209,14 @@ int main(void) {
f'--ig-source-path={src_c}',
str(bc_ios), '-o', str(obf_ios),
], check=True)
delta_tables = [
line for line in obf_ios.read_text(encoding='utf-8').splitlines()
if line.startswith('@__ig_ibt1_')
]
self.assertTrue(delta_tables)
for table in delta_tables:
self.assertIn('i64 sub (', table)
self.assertNotIn('i64 add (', table)
subprocess.run([
clang, '-target', 'arm64-apple-ios15.0', '-isysroot', sdk_ios, '-Os',
'-Xclang', '-disable-llvm-passes', '-fno-builtin', '-Wno-override-module', '-g',
+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')