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,5 +1,9 @@
This file details the changelog of Qiling Framework.

------------------------------------
[Unreleased]
- Report the guest architecture in Linux RV64 uname().machine.

------------------------------------
[Version 1.4.6]: August 4th, 2023

Expand Down
9 changes: 8 additions & 1 deletion qiling/os/posix/syscall/utsname.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
#

from qiling import Qiling
from qiling.const import QL_ARCH, QL_OS

def ql_syscall_uname(ql: Qiling, buf: int):
UTSLEN = 65

# Linux reports RV64 guests as "riscv64" in utsname.machine.
if ql.os.type == QL_OS.LINUX and ql.arch.type == QL_ARCH.RISCV64:
machine = b'riscv64'
else:
machine = b'ql_processor'

fields = (
b'QilingOS', # sysname
b'ql_vm', # nodename
b'99.0-RELEASE', # release
b'QilingOS 99.0-RELEASE r1', # version
b'ql_processor', # machine
machine, # machine
b'' # domainname
)

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_uname_machine import *

if __name__ == "__main__":
unittest.main()
54 changes: 54 additions & 0 deletions tests/test_uname_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
#
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
#

import unittest
from types import SimpleNamespace
from unittest.mock import Mock

from qiling.const import QL_ARCH, QL_ENDIAN, QL_OS
from qiling.os.posix.syscall.utsname import ql_syscall_uname


class UnameMachineTest(unittest.TestCase):
def invoke(self, os_type, arch_type, endian=QL_ENDIAN.EL):
ql = SimpleNamespace(
arch=SimpleNamespace(type=arch_type, endian=endian),
os=SimpleNamespace(type=os_type),
mem=Mock(),
)
self.assertEqual(0, ql_syscall_uname(ql, 0x1000))
writes = [call.args for call in ql.mem.write.call_args_list]
self.assertEqual([0x1000 + i * 65 for i in range(6)], [addr for addr, _ in writes])
self.assertEqual([65] * 6, [len(data) for _, data in writes])
return [data for _, data in writes]

def test_linux_rv64_reports_riscv64(self):
fields = self.invoke(QL_OS.LINUX, QL_ARCH.RISCV64)
self.assertEqual(b'riscv64'.ljust(65, b'\x00'), fields[4])

def test_other_linux_profiles_keep_existing_machine_value(self):
for os_type, arch_type in (
(QL_OS.LINUX, QL_ARCH.RISCV),
(QL_OS.LINUX, QL_ARCH.X8664),
):
with self.subTest(os=os_type.name, arch=arch_type.name):
fields = self.invoke(os_type, arch_type)
self.assertEqual(b'ql_processor'.ljust(65, b'\x00'), fields[4])

def test_non_linux_machine_keeps_profile_value(self):
fields = self.invoke(QL_OS.FREEBSD, QL_ARCH.RISCV64)
self.assertEqual(b'ql_processor' + bytes(53), fields[4])

def test_linux_riscv_update_preserves_other_uts_fields(self):
fields = self.invoke(QL_OS.LINUX, QL_ARCH.RISCV64)
expected = tuple(
value.ljust(65, b'\x00')
for value in (b'QilingOS', b'ql_vm', b'99.0-RELEASE', b'QilingOS 99.0-RELEASE r1', b'')
)
self.assertEqual(expected, tuple(fields[:4] + fields[5:]))


if __name__ == '__main__':
unittest.main()