diff --git a/Lib/test/test_gdb.py b/Lib/test/test_gdb.py index c45fd45735cdac..118dbbf0d9ef26 100644 --- a/Lib/test/test_gdb.py +++ b/Lib/test/test_gdb.py @@ -58,6 +58,23 @@ def get_gdb_version(): checkout_hook_path = os.path.join(os.path.dirname(sys.executable), 'python-gdb.py') + +def cet_protection(): + cflags = sysconfig.get_config_var('CFLAGS') + if not cflags: + return False + flags = cflags.split() + # True if "-mcet -fcf-protection" options are found, but false + # if "-fcf-protection=none" or "-fcf-protection=return" is found. + return (('-mcet' in flags) + and any((flag.startswith('-fcf-protection') + and not flag.endswith(("=none", "=return"))) + for flag in flags)) + +# Control-flow enforcement technology +CET_PROTECTION = cet_protection() + + def run_gdb(*args, **env_vars): """Runs gdb in batch mode with the additional arguments given by *args. @@ -171,6 +188,12 @@ def get_stack_trace(self, source=None, script=None, commands += ['set print entry-values no'] if cmds_after_breakpoint: + if CET_PROTECTION: + # bpo-32962: When Python is compiled with -mcet + # -fcf-protection, function arguments are unusable before + # running the first instruction of the function entry point. + # The 'next' command makes the required first step. + commands += ['next'] commands += cmds_after_breakpoint else: commands += ['backtrace'] @@ -241,6 +264,11 @@ def get_gdb_repr(self, source, # # For a nested structure, the first time we hit the breakpoint will # give us the top-level structure + + # NOTE: avoid decoding too much of the traceback as some + # undecodable characters may lurk there in optimized mode + # (issue #19743). + cmds_after_breakpoint = cmds_after_breakpoint or ["backtrace 1"] gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print', cmds_after_breakpoint=cmds_after_breakpoint, import_site=import_site) @@ -886,9 +914,17 @@ def __init__(self): print("first break point") l = MyList() ''') + cmds_after_breakpoint = ['break wrapper_call', 'continue'] + if CET_PROTECTION: + # bpo-32962: same case as in get_stack_trace(): + # we need an additional 'next' command in order to read + # arguments of the innermost function of the call stack. + cmds_after_breakpoint.append('next') + cmds_after_breakpoint.append('py-bt') + # Verify with "py-bt": gdb_output = self.get_stack_trace(cmd, - cmds_after_breakpoint=['break wrapper_call', 'continue', 'py-bt']) + cmds_after_breakpoint=cmds_after_breakpoint) self.assertRegexpMatches(gdb_output, r"