Skip to content

Commit 4b4adbc

Browse files
committed
Fix ARM stat64 timestamp units
1 parent 23ad4fd commit 4b4adbc

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎qiling/os/posix/syscall/stat.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,15 @@ def __common_pack_stat_struct(stat, info) -> bytes:
11581158
if isinstance(val, ctypes.Array):
11591159
stat.__setattr__(field, (0,) * len(val))
11601160
else:
1161-
stat.__setattr__(field, int(info[field]))
1161+
value = int(info[field])
1162+
if isinstance(
1163+
stat, (LinuxARMStat, LinuxARMEBStat, LinuxARMStat64)
1164+
) and field in (
1165+
"st_atime_ns", "st_mtime_ns", "st_ctime_ns"
1166+
):
1167+
# ARM Linux stat structures store nanoseconds within each second.
1168+
value %= 1_000_000_000
1169+
stat.__setattr__(field, value)
11621170

11631171
return bytes(stat)
11641172

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import ctypes
2+
import struct
3+
import unittest
4+
5+
from qiling.os.posix.syscall.stat import (
6+
LinuxARMEBStat,
7+
LinuxARMStat,
8+
LinuxARMStat64,
9+
__common_pack_stat_struct as common_pack_stat_struct,
10+
)
11+
12+
13+
class ARMStatTimestampUnitTest(unittest.TestCase):
14+
def _check_struct(self, stat_type, byteorder):
15+
timestamps = {
16+
"st_atime_ns": (1_791_143_806_588_454_438, 588_454_438),
17+
"st_mtime_ns": (1_791_143_806_592_454_430, 592_454_430),
18+
"st_ctime_ns": (1_791_143_806_592_454_430, 592_454_430),
19+
}
20+
seconds = {
21+
"st_atime": 1_791_143_806,
22+
"st_mtime": 1_791_143_806,
23+
"st_ctime": 1_791_143_806,
24+
}
25+
info = {field: 0 for field, _ in stat_type._fields_}
26+
info.update(seconds)
27+
info["st_size"] = 29
28+
for field, (epoch_ns, _) in timestamps.items():
29+
info[field] = epoch_ns
30+
31+
stat = stat_type()
32+
packed = common_pack_stat_struct(stat, info)
33+
34+
self.assertEqual(len(packed), ctypes.sizeof(stat_type))
35+
self.assertEqual(stat.st_size, 29)
36+
for field, expected in seconds.items():
37+
self.assertEqual(getattr(stat, field), expected)
38+
for field, (_, expected) in timestamps.items():
39+
with self.subTest(field=field):
40+
self.assertEqual(getattr(stat, field), expected)
41+
offset = getattr(stat_type, field).offset
42+
actual = struct.unpack_from(byteorder + "I", packed, offset)[0]
43+
self.assertEqual(actual, expected)
44+
45+
def test_little_endian_arm_stat_timestamps_use_subsecond_units(self):
46+
self._check_struct(LinuxARMStat, "<")
47+
48+
def test_big_endian_arm_stat_timestamps_use_subsecond_units(self):
49+
self._check_struct(LinuxARMEBStat, ">")
50+
51+
def test_little_endian_arm_stat64_timestamps_use_subsecond_units(self):
52+
self._check_struct(LinuxARMStat64, "<")

‎tests/test_posix.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from test_elf import *
99
from test_riscv import *
1010
from test_qltool import *
11+
from test_d203_stat64_timestamps import *
1112

1213
if __name__ == "__main__":
1314
unittest.main()

0 commit comments

Comments
 (0)