Skip to content

Commit 484a923

Browse files
authored
Add test for C library functions in iOS (#61)
1 parent 539dba4 commit 484a923

File tree

6 files changed

+98
-53
lines changed

6 files changed

+98
-53
lines changed

src/chomper/os/ios/hooks.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class IosFuncHooks(FuncHooks):
10-
"""System function hooks for iOS."""
10+
"""System function hooks for the iOS."""
1111

1212
@classmethod
1313
def register(cls, emu):
@@ -29,12 +29,12 @@ def register(cls, emu):
2929
"_dispatch_async": 0,
3030
# libdyld.dylib
3131
"_dyld_program_sdk_at_least": 1,
32-
"__CFBundleCreateInfoDictFromMainExecutable": 0,
32+
# libobjc.A.dylib
3333
"__ZN11objc_object16rootAutorelease2Ev": None,
34+
# CoreFoundation
35+
"__CFBundleCreateInfoDictFromMainExecutable": 0,
3436
# Foundation
3537
"_NSLog": 0,
36-
# Other modules
37-
"___cficu_udat_open": 0,
3838
}
3939

4040
hooks = {
@@ -61,12 +61,10 @@ def register(cls, emu):
6161
"_srandom": cls.hook_srandom,
6262
"_random": cls.hook_random,
6363
"_localtime_r": cls.hook_localtime_r,
64-
"_fopen": cls.hook_fopen,
6564
"___srefill": cls.hook_srefill,
6665
# libsystem_kernel.dylib
6766
"_stat": cls.hook_stat,
6867
"_lstat": cls.hook_lstat,
69-
"___sysctl": cls.hook_sysctl,
7068
"___sysctlbyname": cls.hook_sysctlbyname,
7169
# libmacho.dylib
7270
"_getsectiondata": cls.hook_getsectiondata,
@@ -293,10 +291,6 @@ def hook_lstat(uc, address, size, user_data):
293291

294292
return 0
295293

296-
@staticmethod
297-
def hook_sysctl(uc, address, size, user_data):
298-
return 0
299-
300294
@staticmethod
301295
def hook_sysctlbyname(uc, address, size, user_data):
302296
emu = user_data["emu"]

src/chomper/os/ios/options.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class IosOptions(Options):
5-
"""Options for iOS.
5+
"""Options of the iOS environment.
66
77
Args:
88
enable_objc: Enable Objective-C support. Defaults to True.

src/chomper/os/ios/os.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ def find_system_module(self, module_name: str) -> str:
5959
raise FileNotFoundError("Module '%s' not found" % module_name)
6060

6161
def check_module(self, module_name: str) -> bool:
62-
"""Check if the module is loaded."""
62+
"""Check if the module exist."""
6363
return module_name in (t.name for t in self.emu.modules)
6464

65+
def resolve_modules(self, module_classes: List):
66+
"""Load modules if don't exist."""
67+
for module_class in module_classes:
68+
if self.check_module(module_class.MODULE_NAME):
69+
continue
70+
71+
module = module_class(self.emu)
72+
module.load()
73+
6574
def init_objc(self, module: Module):
6675
"""Initialize Objective-C."""
6776
if not self.check_module(modules.Objc.MODULE_NAME):
@@ -95,11 +104,6 @@ def init_objc(self, module: Module):
95104
self.emu.logger.error("Initialize Objective-C failed.")
96105
self.emu.logger.exception(e)
97106

98-
def load_modules(self, module_classes: List):
99-
"""Load modules."""
100-
for module_cls in module_classes:
101-
module_cls(self.emu).load()
102-
103107
def enable_objc(self):
104108
"""Enable Objective-C support."""
105109
dependent_modules = [
@@ -127,7 +131,7 @@ def enable_objc(self):
127131
modules.Security,
128132
]
129133

130-
self.load_modules(dependent_modules)
134+
self.resolve_modules(dependent_modules)
131135

132136
def enable_ui_kit(self):
133137
"""Enable UIKit support.
@@ -147,7 +151,7 @@ def enable_ui_kit(self):
147151
modules.UIKitCore,
148152
]
149153

150-
self.load_modules(dependent_modules)
154+
self.resolve_modules(dependent_modules)
151155

152156
def initialize(self):
153157
"""Initialize the environment."""

src/chomper/os/ios/syscall.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class IosSyscallNo:
1515
ACCESS = 0x21
1616
GETEGID = 0x2B
1717
GETTIMEOFDAY = 0x74
18+
SYSCTL = 0xCA
1819
SHM_OPEN = 0x10A
20+
SYSCTLBYNAME = 0x112
1921
GETTID = 0x11E
2022
ISSETUGID = 0x147
2123
STAT64 = 0x152
@@ -31,7 +33,7 @@ class IosSyscallNo:
3133

3234

3335
class IosSyscallHandler(SyscallHandler):
34-
"""System call handler for iOS."""
36+
"""System call handler for the iOS."""
3537

3638
@classmethod
3739
def register(cls, emu):
@@ -42,7 +44,9 @@ def register(cls, emu):
4244
IosSyscallNo.ACCESS: cls.access,
4345
IosSyscallNo.GETEGID: cls.getegid,
4446
IosSyscallNo.GETTIMEOFDAY: cls.gettimeofday,
47+
IosSyscallNo.SYSCTL: cls.sysctl,
4548
IosSyscallNo.SHM_OPEN: cls.shm_open,
49+
IosSyscallNo.SYSCTLBYNAME: cls.sysctlbyname,
4650
IosSyscallNo.GETTID: cls.gettid,
4751
IosSyscallNo.ISSETUGID: cls.issetugid,
4852
IosSyscallNo.STAT64: cls.stat64,
@@ -71,8 +75,7 @@ def geteuid(emu):
7175

7276
@staticmethod
7377
def access(emu):
74-
# pathname = emu.read_string(emu.get_arg(0))
75-
78+
# path = emu.read_string(emu.get_arg(0))
7679
emu.set_retval(0)
7780

7881
@staticmethod
@@ -88,10 +91,41 @@ def gettimeofday(emu):
8891

8992
emu.set_retval(0)
9093

94+
@staticmethod
95+
def sysctl(emu):
96+
emu.set_retval(0)
97+
9198
@staticmethod
9299
def shm_open(emu):
93100
emu.set_retval(0x80000000)
94101

102+
@staticmethod
103+
def sysctlbyname(emu):
104+
name = emu.read_string(emu.get_arg(0))
105+
oldp = emu.get_arg(2)
106+
oldlenp = emu.get_arg(3)
107+
108+
if not oldp or not oldlenp:
109+
emu.set_retval(0)
110+
return
111+
112+
if name == "kern.boottime":
113+
emu.write_u64(oldp, int(time.time()) - 3600 * 24)
114+
emu.write_u64(oldp + 8, 0)
115+
116+
elif name == "kern.osvariant_status":
117+
# internal_release_type = 3
118+
emu.write_u64(oldp, 0x30)
119+
120+
elif name == "hw.memsize":
121+
emu.write_u64(oldp, 4 * 1024 * 1024 * 1024)
122+
123+
else:
124+
emu.logger.warning("Unhandled sysctlbyname: %s" % name)
125+
# raise RuntimeError("Unhandled sysctl command: %s" % name)
126+
127+
emu.set_retval(0)
128+
95129
@staticmethod
96130
def gettid(emu):
97131
tid = threading.current_thread().ident

tests/conftest.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
android_lib_path = os.path.join(base_path, "../examples/rootfs/android/system/lib")
1111
android_lib64_path = os.path.join(base_path, "../examples/rootfs/android/system/lib64")
12-
ios_lib_path = os.path.join(base_path, "../examples/rootfs/ios/usr/lib/system")
12+
13+
ios_rootfs_path = os.path.join(base_path, "../examples/rootfs/ios")
1314

1415
com_shizhuang_duapp_path = os.path.join(
1516
base_path, "../examples/apps/android/com.shizhuang.duapp"
@@ -46,7 +47,7 @@ def libz_arm(emu_arm):
4647
def libdusanwa_v4856_arm(emu_arm):
4748
"""From com.shizhuang.duapp 4.85.6"""
4849
yield emu_arm.load_module(
49-
os.path.join(com_shizhuang_duapp_path, "libdusanwa.so"),
50+
module_file=os.path.join(com_shizhuang_duapp_path, "libdusanwa.so"),
5051
exec_init_array=True,
5152
)
5253

@@ -70,7 +71,7 @@ def libz_arm64(emu_arm64):
7071
def libszstone_v4945_arm64(emu_arm64):
7172
"""From com.shizhuang.duapp 4.94.5"""
7273
yield emu_arm64.load_module(
73-
os.path.join(com_shizhuang_duapp_path, "libszstone.so"),
74+
module_file=os.path.join(com_shizhuang_duapp_path, "libszstone.so"),
7475
exec_init_array=True,
7576
)
7677

@@ -83,10 +84,8 @@ def libtiny_v73021_arm64(emu_arm64):
8384

8485
@pytest.fixture(scope="module")
8586
def emu_ios():
86-
emu = Chomper(arch=ARCH_ARM64, os_type=OS_IOS)
87-
88-
emu.load_module(os.path.join(ios_lib_path, "libsystem_platform.dylib"))
89-
emu.load_module(os.path.join(ios_lib_path, "libsystem_c.dylib"))
90-
emu.load_module(os.path.join(ios_lib_path, "libsystem_kernel.dylib"))
91-
92-
yield emu
87+
yield Chomper(
88+
arch=ARCH_ARM64,
89+
os_type=OS_IOS,
90+
rootfs_path=ios_rootfs_path,
91+
)

0 commit comments

Comments
 (0)