Skip to content

Commit 76ad941

Browse files
committed
add QNX set_api() support
1 parent bace0ea commit 76ad941

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
import sys
7+
sys.path.append("..")
8+
9+
from qiling import Qiling
10+
from qiling.const import QL_INTERCEPT, QL_CALL_BLOCK, QL_VERBOSE
11+
from qiling.os.const import STRING
12+
13+
def my_puts_onenter(ql: Qiling):
14+
params = ql.os.resolve_fcall_params({'s': STRING})
15+
16+
print(f'puts("{params["s"]}")')
17+
return QL_CALL_BLOCK
18+
19+
def my_printf_onenter(ql: Qiling):
20+
params = ql.os.resolve_fcall_params({'s': STRING})
21+
22+
print(f'printf("{params["s"]}")')
23+
return QL_CALL_BLOCK
24+
25+
def my_puts_onexit(ql: Qiling):
26+
print(f'after puts')
27+
return QL_CALL_BLOCK
28+
29+
if __name__ == "__main__":
30+
ql = Qiling(["rootfs/arm_qnx/bin/hello_static"], "rootfs/arm_qnx")
31+
ql.set_api('puts', my_puts_onenter, QL_INTERCEPT.ENTER)
32+
ql.set_api('printf', my_printf_onenter, QL_INTERCEPT.ENTER)
33+
ql.set_api('puts', my_puts_onexit, QL_INTERCEPT.EXIT)
34+
ql.run()

qiling/os/qnx/message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def ql_qnx_msg_io_connect(ql:Qiling, coid, smsg, sparts, rmsg, rparts, *args, **
9898
else:
9999
# TODO: Can we throw this exception here?
100100
# raise NotImplementedError(f'msg_io_connect(_IO_CONNECT_COMBINE) for type 0x{x_type:x} not implemented')
101-
ql.log.warn(f'msg_io_connect(_IO_CONNECT_COMBINE) for type 0x{x_type:x} not implemented')
101+
ql.log.warning(f'msg_io_connect(_IO_CONNECT_COMBINE) for type 0x{x_type:x} not implemented')
102102
elif subtype == 2: # == _IO_CONNECT_OPEN
103103
ql.log.debug(f'open(path = {path}, openflags = 0x{ioflag:x}, openmode = 0x{real_mode:x})')
104104
ql.os.connections[coid].fd = ql_syscall_open(ql, ql.unpack32(ql.mem.read(smsg + 8, 4)), ioflag, real_mode)
@@ -127,7 +127,7 @@ def ql_qnx_msg_io_connect(ql:Qiling, coid, smsg, sparts, rmsg, rparts, *args, **
127127
elif os.path.isfile(real_path):
128128
umask = file_stats['_S_IFREG']
129129
else:
130-
ql.log.warn("msg_io_connect(): type of {real_path} not handled properly?")
130+
ql.log.warning("msg_io_connect(): type of {real_path} not handled properly?")
131131
umask = 0
132132
# struct _io_connect_link_reply in lib/c/public/sys/iomsg.h
133133
ql.mem.write(iov_base, pack("<IIBBHIHH", 0, file_type, eflag, 0, 0, umask, 0, 0))
@@ -190,7 +190,7 @@ def ql_qnx_msg_mem_ctrl(ql:Qiling, coid, smsg, sparts, rmsg, rparts, *args, **kw
190190
if not subtype in mem_ctrl_subtypes:
191191
raise NotImplementedError(f'MEM_CTRL subtype {subtype} not implemented')
192192

193-
ql.log.warn(f'msg_mem_ctrl(subtype = {mem_ctrl_subtypes[subtype]}, flags = 0x{flags:x}, addr = 0x{addr:x}, len = 0x{len:x}) not implemented')
193+
ql.log.warning(f'msg_mem_ctrl(subtype = {mem_ctrl_subtypes[subtype]}, flags = 0x{flags:x}, addr = 0x{addr:x}, len = 0x{len:x}) not implemented')
194194
# TODO: implement mem_ctrl
195195
return -1
196196

qiling/os/qnx/qnx.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@
33
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44
#
55

6+
from typing import Callable
67
import os
78
from unicorn import UcError
89

910
from qiling.os.posix.posix import QlOsPosix
1011
from qiling.os.qnx.const import NTO_SIDE_CHANNEL, SYSMGR_PID, SYSMGR_CHID, SYSMGR_COID
1112
from qiling.os.qnx.helpers import QnxConn
1213
from qiling.os.qnx.structs import _thread_local_storage
13-
from qiling.const import QL_ARCH
14+
from qiling.os.fcall import QlFunctionCall
15+
from qiling.cc import QlCC, intel, arm, mips, riscv
16+
from qiling.const import QL_ARCH, QL_INTERCEPT
1417

1518
class QlOsQnx(QlOsPosix):
1619
def __init__(self, ql):
1720
super(QlOsQnx, self).__init__(ql)
1821
self.load()
22+
23+
cc: QlCC = {
24+
QL_ARCH.X86 : intel.cdecl,
25+
QL_ARCH.X8664 : intel.amd64,
26+
QL_ARCH.ARM : arm.aarch32,
27+
QL_ARCH.ARM64 : arm.aarch64,
28+
QL_ARCH.MIPS : mips.mipso32,
29+
QL_ARCH.RISCV : riscv.riscv,
30+
QL_ARCH.RISCV64: riscv.riscv,
31+
}[ql.archtype](ql)
32+
33+
self.fcall = QlFunctionCall(ql, cc)
1934

2035
# use counters to get free Ids
2136
self.channel_id = 1
@@ -44,6 +59,10 @@ def hook_syscall(self, intno= None, int = None):
4459
return self.load_syscall()
4560

4661

62+
def add_function_hook(self, fn: str, cb: Callable, intercept: QL_INTERCEPT):
63+
self.ql.os.function_hook.add_function_hook(fn, cb, intercept)
64+
65+
4766
def hook_sigtrap(self, intno= None, int = None):
4867
self.ql.log.info("Trap Found")
4968
self.emu_error()

tests/test_qnx.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from qiling import *
1010
from qiling.exception import *
1111
from qiling.const import QL_VERBOSE
12+
from qiling.const import QL_INTERCEPT, QL_CALL_BLOCK, QL_VERBOSE
13+
from qiling.os.const import STRING
1214

1315
class QNXTest(unittest.TestCase):
1416
def test_arm_qnx_static(self):
@@ -20,7 +22,37 @@ def test_arm_qnx_static(self):
2022

2123
def test_arm_qnx_sqrt(self):
2224
ql = Qiling(["../examples/rootfs/arm_qnx/bin/hello_sqrt"], "../examples/rootfs/arm_qnx", verbose=QL_VERBOSE.DEBUG)
23-
ql.run()
25+
ql.run()
26+
27+
def test_set_api_arm_qnx_sqrt(self):
28+
def my_puts_onenter(ql: Qiling):
29+
params = ql.os.resolve_fcall_params({'s': STRING})
30+
31+
print(f'puts("{params["s"]}")')
32+
return QL_CALL_BLOCK
33+
34+
def my_puts_onexit(ql: Qiling):
35+
print(f'after puts')
36+
return QL_CALL_BLOCK
37+
38+
def my_printf_onenter(ql: Qiling):
39+
params = ql.os.resolve_fcall_params({'s': STRING})
40+
41+
print(f'printf("{params["s"]}")')
42+
return QL_CALL_BLOCK
43+
44+
def my_printf_onexit(ql: Qiling):
45+
print(f'after printf')
46+
return QL_CALL_BLOCK
47+
48+
ql = Qiling(["../examples/rootfs/arm_qnx/bin/hello_sqrt"], "../examples/rootfs/arm_qnx", verbose=QL_VERBOSE.DEBUG)
49+
ql.set_api('puts', my_puts_onenter, QL_INTERCEPT.ENTER)
50+
ql.set_api('printf', my_printf_onenter, QL_INTERCEPT.ENTER)
51+
52+
ql.set_api('puts', my_puts_onexit, QL_INTERCEPT.EXIT)
53+
ql.set_api('printf', my_printf_onexit, QL_INTERCEPT.EXIT)
54+
55+
ql.run()
2456

2557
if __name__ == "__main__":
2658
unittest.main()

0 commit comments

Comments
 (0)