From e5b9616a149a6a2287ff8560efa447b7c75c1efa Mon Sep 17 00:00:00 2001 From: carlosqwqqwq <102978772+carlosqwqqwq@users.noreply.github.com> Date: Tue, 24 Nov 2026 05:54:34 +0800 Subject: [PATCH] fix linux clock_nanosleep invalid clock ids --- ChangeLog | 4 ++ qiling/os/posix/syscall/time.py | 8 ++++ tests/test_posix.py | 1 + tests/test_syscall_clock_nanosleep.py | 58 +++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 tests/test_syscall_clock_nanosleep.py diff --git a/ChangeLog b/ChangeLog index 84070a4c9..c4ce50415 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,10 @@ This file details the changelog of Qiling Framework. ------------------------------------ +[Unreleased] + +- Fix invalid static clock IDs in Linux `clock_nanosleep`. + [Version 1.4.6]: August 4th, 2023 ## What's Changed diff --git a/qiling/os/posix/syscall/time.py b/qiling/os/posix/syscall/time.py index ad0004f04..b7bd5076e 100644 --- a/qiling/os/posix/syscall/time.py +++ b/qiling/os/posix/syscall/time.py @@ -8,6 +8,8 @@ import gevent from qiling import Qiling +from ....const import QL_OS +from ..const import EINVAL def ql_syscall_time(ql: Qiling): return int(time.time()) @@ -50,6 +52,12 @@ def ql_syscall_nanosleep(ql: Qiling, req: int, rem: int): return __sleep_common(ql, req, rem) def ql_syscall_clock_nanosleep(ql: Qiling, clockid: int, flags: int, req: int, rem: int): + if ql.os.type is QL_OS.LINUX: + # Linux clockid_t is signed 32-bit; the static table has a hole at 10. + static_clockid = clockid & 0xffffffff + if static_clockid < 0x80000000 and (static_clockid == 10 or static_clockid > 11): + return -EINVAL + return __sleep_common(ql, req, rem) def ql_syscall_setitimer(ql: Qiling, which: int, new_value: int, old_value: int): diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..b03563cf2 100644 --- a/tests/test_posix.py +++ b/tests/test_posix.py @@ -8,6 +8,7 @@ from test_elf import * from test_riscv import * from test_qltool import * +from test_syscall_clock_nanosleep import * if __name__ == "__main__": unittest.main() diff --git a/tests/test_syscall_clock_nanosleep.py b/tests/test_syscall_clock_nanosleep.py new file mode 100644 index 000000000..2cacfd9f6 --- /dev/null +++ b/tests/test_syscall_clock_nanosleep.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# +# Cross Platform and Multi Architecture Advanced Binary Emulation Framework +# + +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +from qiling.const import QL_OS +from qiling.os.posix.const import EINVAL +from qiling.os.posix.syscall.time import ql_syscall_clock_nanosleep + + +class TestClockNanosleepClockId(unittest.TestCase): + def make_ql(self, os_type=QL_OS.LINUX): + return SimpleNamespace(os=SimpleNamespace(type=os_type)) + + @patch('qiling.os.posix.syscall.time.__sleep_common', return_value=0) + def test_rejects_invalid_positive_static_clock_ids(self, sleep): + ql = self.make_ql() + + for clockid in (10, 12, 999, 0x1000003e7): + with self.subTest(clockid=clockid): + self.assertEqual(ql_syscall_clock_nanosleep(ql, clockid, 0, 0, 0), -EINVAL) + + sleep.assert_not_called() + + @patch('qiling.os.posix.syscall.time.__sleep_common', return_value=0) + def test_known_static_ids_keep_the_existing_sleep_path(self, sleep): + ql = self.make_ql() + + for clockid in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11): + with self.subTest(clockid=clockid): + self.assertEqual(ql_syscall_clock_nanosleep(ql, clockid, 0, 0, 0), 0) + + self.assertEqual(sleep.call_count, 11) + + @patch('qiling.os.posix.syscall.time.__sleep_common', return_value=0) + def test_signed_and_wide_arguments_are_normalized_as_clockid_t(self, sleep): + ql = self.make_ql() + + for clockid in (-1, 0xffffffff, 0xffffffffffffffff, 0x100000001): + with self.subTest(clockid=clockid): + self.assertEqual(ql_syscall_clock_nanosleep(ql, clockid, 0, 0, 0), 0) + + self.assertEqual(sleep.call_count, 4) + + @patch('qiling.os.posix.syscall.time.__sleep_common', return_value=0) + def test_other_posix_os_keeps_the_existing_sleep_path(self, sleep): + ql = self.make_ql(QL_OS.FREEBSD) + + self.assertEqual(ql_syscall_clock_nanosleep(ql, 999, 0, 0, 0), 0) + sleep.assert_called_once_with(ql, 0, 0) + + +if __name__ == '__main__': + unittest.main()