Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions qiling/os/posix/syscall/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
58 changes: 58 additions & 0 deletions tests/test_syscall_clock_nanosleep.py
Original file line number Diff line number Diff line change
@@ -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()