| 1 |
import re |
| 2 |
import sys |
| 3 |
import textwrap |
| 4 |
import os |
| 5 |
import unittest |
| 6 |
from dataclasses import dataclass |
| 7 |
from functools import cache |
| 8 |
from test import support |
| 9 |
from test.support.script_helper import run_python_until_end |
| 10 |
|
| 11 |
_strace_binary = "/usr/bin/strace" |
| 12 |
_syscall_regex = re.compile( |
| 13 |
r"(?P<syscall>[^(]*)\((?P<args>[^)]*)\)\s*[=]\s*(?P<returncode>.+)") |
| 14 |
_returncode_regex = re.compile( |
| 15 |
br"\+\+\+ exited with (?P<returncode>\d+) \+\+\+") |
| 16 |
|
| 17 |
|
| 18 |
@dataclass |
| 19 |
class StraceEvent: |
| 20 |
syscall: str |
| 21 |
args: list[str] |
| 22 |
returncode: str |
| 23 |
|
| 24 |
|
| 25 |
@dataclass |
| 26 |
class StraceResult: |
| 27 |
strace_returncode: int |
| 28 |
python_returncode: int |
| 29 |
|
| 30 |
"""The event messages generated by strace. This is very similar to the |
| 31 |
stderr strace produces with returncode marker section removed.""" |
| 32 |
event_bytes: bytes |
| 33 |
stdout: bytes |
| 34 |
stderr: bytes |
| 35 |
|
| 36 |
def events(self): |
| 37 |
"""Parse event_bytes data into system calls for easier processing. |
| 38 |
|
| 39 |
This assumes the program under inspection doesn't print any non-utf8 |
| 40 |
strings which would mix into the strace output.""" |
| 41 |
decoded_events = self.event_bytes.decode('utf-8', 'surrogateescape') |
| 42 |
matches = [ |
| 43 |
_syscall_regex.match(event) |
| 44 |
for event in decoded_events.splitlines() |
| 45 |
] |
| 46 |
return [ |
| 47 |
StraceEvent(match["syscall"], |
| 48 |
[arg.strip() for arg in (match["args"].split(","))], |
| 49 |
match["returncode"]) for match in matches if match |
| 50 |
] |
| 51 |
|
| 52 |
def sections(self): |
| 53 |
"""Find all "MARK <X>" writes and use them to make groups of events. |
| 54 |
|
| 55 |
This is useful to avoid variable / overhead events, like those at |
| 56 |
interpreter startup or when opening a file so a test can verify just |
| 57 |
the small case under study.""" |
| 58 |
current_section = "__startup" |
| 59 |
sections = {current_section: []} |
| 60 |
for event in self.events(): |
| 61 |
if event.syscall == 'write' and len( |
| 62 |
event.args) > 2 and event.args[1].startswith("\"MARK "): |
| 63 |
# Found a new section, don't include the write in the section |
| 64 |
# but all events until next mark should be in that section |
| 65 |
current_section = event.args[1].split( |
| 66 |
" ", 1)[1].removesuffix('\\n"') |
| 67 |
if current_section not in sections: |
| 68 |
sections[current_section] = list() |
| 69 |
else: |
| 70 |
sections[current_section].append(event) |
| 71 |
|
| 72 |
return sections |
| 73 |
|
| 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 |
|
| 95 |
|
| 96 |
@support.requires_subprocess() |
| 97 |
def strace_python(code, strace_flags, check=True): |
| 98 |
"""Run strace and return the trace. |
| 99 |
|
| 100 |
Sets strace_returncode and python_returncode to `-1` on error.""" |
| 101 |
res = None |
| 102 |
|
| 103 |
def _make_error(reason, details): |
| 104 |
return StraceResult( |
| 105 |
strace_returncode=-1, |
| 106 |
python_returncode=-1, |
| 107 |
event_bytes= f"error({reason},details={details!r}) = -1".encode('utf-8'), |
| 108 |
stdout=res.out if res else b"", |
| 109 |
stderr=res.err if res else b"") |
| 110 |
|
| 111 |
# Run strace, and get out the raw text |
| 112 |
try: |
| 113 |
res, cmd_line = run_python_until_end( |
| 114 |
"-c", |
| 115 |
textwrap.dedent(code), |
| 116 |
__run_using_command=[_strace_binary] + strace_flags, |
| 117 |
) |
| 118 |
except OSError as err: |
| 119 |
return _make_error("Caught OSError", err) |
| 120 |
|
| 121 |
if check and res.rc: |
| 122 |
res.fail(cmd_line) |
| 123 |
|
| 124 |
# Get out program returncode |
| 125 |
stripped = res.err.strip() |
| 126 |
output = stripped.rsplit(b"\n", 1) |
| 127 |
if len(output) != 2: |
| 128 |
return _make_error("Expected strace events and exit code line", |
| 129 |
stripped[-50:]) |
| 130 |
|
| 131 |
returncode_match = _returncode_regex.match(output[1]) |
| 132 |
if not returncode_match: |
| 133 |
return _make_error("Expected to find returncode in last line.", |
| 134 |
output[1][:50]) |
| 135 |
|
| 136 |
python_returncode = int(returncode_match["returncode"]) |
| 137 |
if check and python_returncode: |
| 138 |
res.fail(cmd_line) |
| 139 |
|
| 140 |
return StraceResult(strace_returncode=res.rc, |
| 141 |
python_returncode=python_returncode, |
| 142 |
event_bytes=output[0], |
| 143 |
stdout=res.out, |
| 144 |
stderr=res.err) |
| 145 |
|
| 146 |
|
| 147 |
def get_events(code, strace_flags, prelude, cleanup): |
| 148 |
# NOTE: The flush is currently required to prevent the prints from getting |
| 149 |
# buffered and done all at once at exit |
| 150 |
prelude = textwrap.dedent(prelude) |
| 151 |
code = textwrap.dedent(code) |
| 152 |
cleanup = textwrap.dedent(cleanup) |
| 153 |
to_run = f""" |
| 154 |
print("MARK prelude", flush=True) |
| 155 |
{prelude} |
| 156 |
print("MARK code", flush=True) |
| 157 |
{code} |
| 158 |
print("MARK cleanup", flush=True) |
| 159 |
{cleanup} |
| 160 |
print("MARK __shutdown", flush=True) |
| 161 |
""" |
| 162 |
trace = strace_python(to_run, strace_flags) |
| 163 |
all_sections = trace.sections() |
| 164 |
return all_sections['code'] |
| 165 |
|
| 166 |
|
| 167 |
def get_syscalls(code, strace_flags, prelude="", cleanup="", |
| 168 |
ignore_memory=True): |
| 169 |
"""Get the syscalls which a given chunk of python code generates""" |
| 170 |
events = get_events(code, strace_flags, prelude=prelude, cleanup=cleanup) |
| 171 |
|
| 172 |
if ignore_memory: |
| 173 |
events = filter_memory(events) |
| 174 |
|
| 175 |
return [ev.syscall for ev in events] |
| 176 |
|
| 177 |
|
| 178 |
# Moderately expensive (spawns a subprocess), so share results when possible. |
| 179 |
@cache |
| 180 |
def _can_strace(): |
| 181 |
res = strace_python("import sys; sys.exit(0)", |
| 182 |
# --trace option needs strace 5.5 (gh-133741) |
| 183 |
["--trace=%process"], |
| 184 |
check=False) |
| 185 |
if res.strace_returncode == 0 and res.python_returncode == 0: |
| 186 |
assert res.events(), "Should have parsed multiple calls" |
| 187 |
return True |
| 188 |
return False |
| 189 |
|
| 190 |
|
| 191 |
def requires_strace(): |
| 192 |
if sys.platform != "linux": |
| 193 |
return unittest.skip("Linux only, requires strace.") |
| 194 |
|
| 195 |
if "LD_PRELOAD" in os.environ: |
| 196 |
# Distribution packaging (ex. Debian `fakeroot` and Gentoo `sandbox`) |
| 197 |
# use LD_PRELOAD to intercept system calls, which changes the overall |
| 198 |
# set of system calls which breaks tests expecting a specific set of |
| 199 |
# system calls). |
| 200 |
return unittest.skip("Not supported when LD_PRELOAD is intercepting system calls.") |
| 201 |
|
| 202 |
if support.check_sanitizer(address=True, memory=True): |
| 203 |
return unittest.skip("LeakSanitizer does not work under ptrace (strace, gdb, etc)") |
| 204 |
|
| 205 |
return unittest.skipUnless(_can_strace(), "Requires working strace") |
| 206 |
|
| 207 |
|
| 208 |
__all__ = ["filter_memory", "get_events", "get_syscalls", "requires_strace", |
| 209 |
"strace_python", "StraceEvent", "StraceResult"] |