8000 GH-120754: Add a strace helper and test set of syscalls for open().read() by cmaloney · Pull Request #121143 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-120754: Add a strace helper and test set of syscalls for open().read() #121143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a44faf3
Add strace helper for tracing system calls made by python running spe…
cmaloney Jun 28, 2024
a462039
Update test_subprocess to use strace_helper
cmaloney Jun 28, 2024
2ab832d
Add test to FileIO that validates set of syscalls
cmaloney Jun 29, 2024
ef298f2
Move from assert to .assertEqual
cmaloney Jun 29, 2024
283a077
Allow libc to use different fstat variants
cmaloney Jun 29, 2024
3b6c094
Exit early if strace exited non-zero
cmaloney Jun 29, 2024
97b294f
Add myself to ACKS
cmaloney Jun 29, 2024
e5bdc6b
Add tests around pathilb read_*() behavior
cmaloney Jun 29, 2024
397cd9e
Remove subprocess._USE_VFORK strace test
cmaloney Jun 29, 2024
e88d414
Merge remote-tracking branch 'origin/main' into cmaloney/systrace_hel…
cmaloney Jul 4, 2024
d99157f
Update call sequence after gh-120755
cmaloney Jul 4, 2024
5664558
Add specific python bug links
cmaloney Jul 9, 2024
736d5bc
Reduce annotations, stay bytes longer, make raw_events non-private
cmaloney Jul 9, 2024
47ed7fe
Move _strace_working checks to all be in requires_strace
cmaloney Jul 9, 2024
55d1cec
formatting fixes, reduce annotations further
cmaloney Jul 10, 2024
6fe0961
Small cleanups from self review
cmaloney Jul 10, 2024
943b07d
Merge branch 'main' into cmaloney/systrace_helper_wip
cmaloney Jul 10, 2024
2ea2bc8
Adjust test cases to match more general system call shape
cmaloney Jul 10, 2024
cdf449a
raw_events -> event_bytes
cmaloney Jul 10, 2024
c44bca6
Add bits I forgot to commit
cmaloney Jul 10, 2024
0210d16
Merge remote-tracking branch 'main' into cmaloney/systrace_helper_wip
cmaloney Aug 1, 2024
a1b4028
Merge branch 'main' into cmaloney/systrace_helper_wip
cmaloney Aug 18, 2024
0c6ebe6
Switch to functools.cache, simplifying the code
cmaloney Aug 18, 2024
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
Prev Previous commit
Next Next commit
Reduce annotations, stay bytes longer, make raw_events non-private
  • Loading branch information
cmaloney committed Jul 9, 2024
commit 736d5bc0139ed19d9c4c3592eedf722378c01164
58 changes: 28 additions & 30 deletions Lib/test/support/strace_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from dataclasses import dataclass
from test import support
from test.support.script_helper import run_python_until_end
from typing import Dict, List

_strace_binary = "/usr/bin/strace"
_syscall_regex = re.compile(
r"(?P<syscall>[^(]*)\((?P<args>[^)]*)\)\s*[=]\s*(?P<returncode>.+)")
_returncode_regex = re.compile(
r"\+\+\+ exited with (?P<returncode>\d+) \+\+\+")
br"\+\+\+ exited with (?P<returncode>\d+) \+\+\+")

# Cached value of whether or not there is a compatible strace binary
_strace_working: bool | None = None
Expand All @@ -20,36 +19,43 @@
@dataclass
class StraceEvent:
syscall: str
args: List[str]
args: list[str]
returncode: str


@dataclass
class StraceResult:
strace_returncode: int
python_returncode: int
_raw_events: str
stdout: str
stderr: str

def events(self) -> List[StraceEvent]:
"""Extract the call list information from _raw_events"""
"""The event messages generated by strace. This is very similar to the
stderr strace produces with returncode marker section removed."""
raw_events: bytes
stdout: bytes
stderr: bytes

def events(self):
"""Parse raw_events data into system calls for easier processing.

This assumes the program under inspection doesn't print any non-utf8
strings which would mix into the strace output."""
decoded_events = self.raw_events.decode('utf-8')
matches = [
_syscall_regex.match(event)
for event in self._raw_events.splitlines()
for event in decoded_events.splitlines()
]
return [
StraceEvent(match["syscall"],
[arg.strip() for arg in (match["args"].split(","))],
match["returncode"]) for match in matches if match
]

def sections(self) -> Dict[str:List[StraceEvent]]:
def sections(self):
"""Find all "MARK <X>" writes and use them to make groups of events.

This is useful to avoid variable / overhead strace events, like that
at interpreter startup, so a test can just check does the small case
under study work."""
This is useful to avoid variable / overhead events, like those at
interpreter startup or when opening a file so a test can verify just
the small case under study."""
current_section = "__startup"
sections = {current_section: []}
for event in self.events():
Expand All @@ -68,20 +74,17 @@ def sections(self) -> Dict[str:List[StraceEvent]]:


@support.requires_subprocess()
def strace_python(code: str,
strace_flags: List[str],
check: bool = True) -> StraceResult:
def strace_python(code, strace_flags, check = True):
"""Run strace and return the trace.

Sets strace_returncode and python_returncode to `-1` on error
"""
Sets strace_returncode and python_returncode to `-1` on error."""
res = None

def _make_error(reason, details):
return StraceResult(
strace_returncode=-1,
python_returncode=-1,
_raw_events=f"error({reason},details={details}) = -1",
raw_events=f"error({reason},details={details}) = -1".encode('utf-8'),
stdout=res.out if res else "",
stderr=res.err if res else "")

Expand All @@ -98,12 +101,11 @@ def _make_error(reason, details):
res.fail(cmd_line)

# Get out program returncode
decoded = res.err.decode().strip()

output = decoded.rsplit("\n", 1)
stripped = res.err.strip()
output = stripped.rsplit(b"\n", 1)
if len(output) != 2:
return _make_error("Expected strace events and exit code line",
decoded[-50:])
stripped[-50:])

returncode_match = _returncode_regex.match(output[1])
if not returncode_match:
Expand All @@ -116,13 +118,12 @@ def _make_error(reason, details):

return StraceResult(strace_returncode=res.rc,
python_returncode=python_returncode,
_raw_events=output[0],
raw_events=output[0],
stdout=res.out,
stderr=res.err)


def _get_events(code: str, strace_flags: List[str], prelude: str,
cleanup: str) -> List[StraceEvent]:
def _get_events(code, strace_flags, prelude, cleanup):
# NOTE: The flush is currently required to prevent the prints from getting
# buffered and done all at once at exit
prelude = textwrap.dedent(prelude)
Expand All @@ -142,10 +143,7 @@ def _get_events(code: str, strace_flags: List[str], prelude: str,
return all_sections['code']


def get_syscalls(code: str,
strace_flags: List[str],
prelude: str = "",
cleanup: str = "") -> List[str]:
def get_syscalls(code, strace_flags, prelude = "", cleanup = ""):
"""Get the syscalls which a given chunk of python code generates"""
events = _get_events(code, strace_flags, prelude=prelude, cleanup=cleanup)
return [ev.syscall for ev in events]
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3461,7 +3461,7 @@ def test_vfork_used_when_expected(self):
strace_args
)
# Match both vfork() and clone(..., flags=...|CLONE_VFORK|...)
self.assertRegex(vfork_result._raw_events, r"(?i)vfork")
self.assertRegex(vfork_result.raw_events, rb"(?i)vfork")
# Do NOT check that fork() or other clones did not happen.
# If the OS denys the vfork it'll fallback to plain fork().

Expand All @@ -3487,7 +3487,7 @@ def test_vfork_used_when_expected(self):
strace_args
)
# Ensure neither vfork() or clone(..., flags=...|CLONE_VFORK|...).
self.assertNotRegex(non_vfork_result._raw_events, r"(?i)vfork")
self.assertNotRegex(non_vfork_result.raw_events, rb"(?i)vfork")


@unittest.skipUnless(mswindows, "Windows specific tests")
Expand Down
0