Skip to content

Commit 2bd8c93

Browse files
committed
Deduplicate Linux timespec struct
1 parent 3f03eb4 commit 2bd8c93

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

qiling/os/linux/syscall.py

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@
1111
from math import floor
1212
import ctypes
1313

14-
class timespec(ctypes.Structure):
15-
_fields_ = [
16-
("tv_sec", ctypes.c_uint64),
17-
("tv_nsec", ctypes.c_int64)
18-
]
14+
def __get_timespec_struct(archbits: int):
15+
long = getattr(ctypes, f'c_int{archbits}')
16+
ulong = getattr(ctypes, f'c_uint{archbits}')
1917

20-
_pack_ = 8
18+
class timespec(ctypes.Structure):
19+
_pack_ = archbits // 8
2120

21+
_fields_ = (
22+
('tv_sec', ulong),
23+
('tv_nsec', long)
24+
)
2225

23-
# Temporary dirty fix.
24-
# TODO: Pack ctypes.Structure according to ql.arch.type and ql.ostype?
25-
class timespec32(ctypes.Structure):
26-
_fields_ = [
27-
("tv_sec", ctypes.c_uint32),
28-
("tv_nsec", ctypes.c_int32)
29-
]
26+
return timespec
27+
28+
def __get_timespec_obj(archbits: int):
29+
now = datetime.now().timestamp()
30+
31+
tv_sec = floor(now)
32+
tv_nsec = floor((now - floor(now)) * 1e6)
33+
ts_cls = __get_timespec_struct(archbits)
34+
35+
return ts_cls(tv_sec=tv_sec, tv_nsec=tv_nsec)
3036

31-
_pack_ = 4
3237

3338
def ql_syscall_set_thread_area(ql: Qiling, u_info_addr: int):
3439
if ql.arch.type == QL_ARCH.X86:
@@ -62,39 +67,25 @@ def ql_syscall_set_thread_area(ql: Qiling, u_info_addr: int):
6267
return 0
6368

6469

65-
def ql_syscall_set_tls(ql, address, *args, **kw):
70+
def ql_syscall_set_tls(ql: Qiling, address: int):
6671
if ql.arch.type == QL_ARCH.ARM:
6772
ql.arch.regs.c13_c0_3 = address
6873
ql.mem.write_ptr(ql.arch.arm_get_tls_addr + 16, address, 4)
6974
ql.arch.regs.r0 = address
7075
ql.log.debug("settls(0x%x)" % address)
7176

72-
def ql_syscall_clock_gettime(ql, clock_gettime_clock_id, clock_gettime_timespec, *args, **kw):
73-
now = datetime.now().timestamp()
74-
tv_sec = floor(now)
75-
tv_nsec = floor((now - floor(now)) * 1e6)
76-
if ql.arch.type == QL_ARCH.X8664:
77-
tp = timespec(tv_sec= tv_sec, tv_nsec=tv_nsec)
78-
else:
79-
tp = timespec32(tv_sec= tv_sec, tv_nsec=tv_nsec)
80-
ql.mem.write(clock_gettime_timespec, bytes(tp))
81-
82-
ql.log.debug("clock_gettime(clock_id = %d, timespec = 0x%x)" % (clock_gettime_clock_id, clock_gettime_timespec))
83-
77+
def ql_syscall_clock_gettime(ql: Qiling, clock_id: int, tp: int):
78+
ts_obj = __get_timespec_obj(ql.arch.bits)
79+
ql.mem.write(tp, bytes(ts_obj))
80+
8481
return 0
8582

86-
def ql_syscall_gettimeofday(ql, gettimeofday_tv, gettimeofday_tz, *args, **kw):
87-
now = datetime.now().timestamp()
88-
tv_sec = floor(now)
89-
tv_nsec = floor((now - floor(now)) * 1e6)
90-
if ql.arch.type == QL_ARCH.X8664:
91-
tp = timespec(tv_sec= tv_sec, tv_nsec=tv_nsec)
92-
else:
93-
tp = timespec32(tv_sec= tv_sec, tv_nsec=tv_nsec)
94-
95-
if gettimeofday_tv != 0:
96-
ql.mem.write(gettimeofday_tv, bytes(tp))
97-
if gettimeofday_tz != 0:
98-
ql.mem.write(gettimeofday_tz, b'\x00' * 8)
99-
regreturn = 0
100-
return regreturn
83+
def ql_syscall_gettimeofday(ql: Qiling, tv: int, tz: int):
84+
if tv:
85+
ts_obj = __get_timespec_obj(ql.arch.bits)
86+
ql.mem.write(tv, bytes(ts_obj))
87+
88+
if tz:
89+
ql.mem.write(tz, b'\x00' * 8)
90+
91+
return 0

0 commit comments

Comments
 (0)