Skip to content

Commit 46f8a7b

Browse files
authored
gh-127076: Ignore memory mmap in FileIO testing (#127088)
`mmap`, `munmap`, and `mprotect` are used by CPython for memory management, which may occur in the middle of the FileIO tests. The system calls can also be used with files, so `strace` includes them in its `%file` and `%desc` filters. Filter out the `mmap` system calls related to memory allocation for the file tests. Currently FileIO doesn't do `mmap` at all, so didn't add code to track from `mmap` through `munmap` since it wouldn't be used. For now if an `mmap` on a fd happens, the call will be included (which may cause test to fail), and at that time support for tracking the address throug `munmap` could be added.
1 parent 0a1944c commit 46f8a7b

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

Lib/test/support/strace_helper.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ def sections(self):
7171

7272
return sections
7373

74+
def _filter_memory_call(call):
75+
# mmap can operate on a fd or "MAP_ANONYMOUS" which gives a block of memory.
76+
# Ignore "MAP_ANONYMOUS + the "MAP_ANON" alias.
77+
if call.syscall == "mmap" and "MAP_ANON" in call.args[3]:
78+
return True
79+
80+
if call.syscall in ("munmap", "mprotect"):
81+
return True
82+
83+
return False
84+
85+
86+
def filter_memory(syscalls):
87+
"""Filter out memory allocation calls from File I/O calls.
88+
89+
Some calls (mmap, munmap, etc) can be used on files or to just get a block
90+
of memory. Use this function to filter out the memory related calls from
91+
other calls."""
92+
93+
return [call for call in syscalls if not _filter_memory_call(call)]
94+
7495

7596
@support.requires_subprocess()
7697
def strace_python(code, strace_flags, check=True):
@@ -93,8 +114,6 @@ def _make_error(reason, details):
93114
"-c",
94115
textwrap.dedent(code),
95116
__run_using_command=[_strace_binary] + strace_flags,
96-
# Don't want to trace our JIT's own mmap and mprotect calls:
97-
PYTHON_JIT="0",
98117
)
99118
except OSError as err:
100119
return _make_error("Caught OSError", err)
@@ -145,9 +164,14 @@ def get_events(code, strace_flags, prelude, cleanup):
145164
return all_sections['code']
146165

147166

148-
def get_syscalls(code, strace_flags, prelude="", cleanup=""):
167+
def get_syscalls(code, strace_flags, prelude="", cleanup="",
168+
ignore_memory=True):
149169
"""Get the syscalls which a given chunk of python code generates"""
150170
events = get_events(code, strace_flags, prelude=prelude, cleanup=cleanup)
171+
172+
if ignore_memory:
173+
events = filter_memory(events)
174+
151175
return [ev.syscall for ev in events]
152176

153177

@@ -177,5 +201,5 @@ def requires_strace():
177201
return unittest.skipUnless(_can_strace(), "Requires working strace")
178202

179203

180-
__all__ = ["get_events", "get_syscalls", "requires_strace", "strace_python",
181-
"StraceEvent", "StraceResult"]
204+
__all__ = ["filter_memory", "get_events", "get_syscalls", "requires_strace",
205+
"strace_python", "StraceEvent", "StraceResult"]

Lib/test/test_fileio.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ def testErrnoOnClosedReadinto(self, f):
364364

365365
@strace_helper.requires_strace()
366366
def test_syscalls_read(self):
367-
"""Check that the set of system calls produced by the I/O stack is what
368-
is expected for various read cases.
367+
"""Check set of system calls during common I/O patterns
369368
370369
It's expected as bits of the I/O implementation change, this will need
371370
to change. The goal is to catch changes that unintentionally add
@@ -383,6 +382,11 @@ def check_readall(name, code, prelude="", cleanup="",
383382
prelude=prelude,
384383
cleanup=cleanup)
385384

385+
# Some system calls (ex. mmap) can be used for both File I/O and
386+
# memory allocation. Filter out the ones used for memory
387+
# allocation.
388+
syscalls = strace_helper.filter_memory(syscalls)
389+
386390
# The first call should be an open that returns a
387391
# file descriptor (fd). Afer that calls may vary. Once the file
388392
# is opened, check calls refer to it by fd as the filename
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Filter out memory-related ``mmap``, ``munmap``, and ``mprotect`` calls from
2+
file-related ones when testing :mod:`io` behavior using strace.

0 commit comments

Comments
 (0)