Skip to content

Commit 76fba8d

Browse files
authored
Fix #15283 (donate-cpu: the assertion failure message is not saved when Cppcheck crashes) (#9086)
1 parent 4095c5e commit 76fba8d

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import sys
20+
21+
import pytest
22+
1923
from donate_cpu_lib import *
2024

2125
def _test_library_includes(tmpdir, libs, content, libinc_obj=None):
@@ -66,3 +70,59 @@ def test_match_multiple_time(tmpdir):
6670
# there was a bug that we would only match each library once successfully
6771
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
6872
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
73+
74+
def test_get_assertion_failures():
75+
assert get_assertion_failures('') == []
76+
assert get_assertion_failures('Checking file.cpp ...\n') == []
77+
glibc_msg = "cppcheck: build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed."
78+
# the program name is removed
79+
msg = "build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed."
80+
assert get_assertion_failures('Checking file.cpp ...\n' + glibc_msg + '\n') == [msg]
81+
# same message twice => reported once
82+
assert get_assertion_failures(glibc_msg + '\n' + glibc_msg + '\n') == [msg]
83+
# not an assertion failure message
84+
assert get_assertion_failures("Assertion `x' failed.\n") == []
85+
assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == []
86+
87+
def _write_script(path, content):
88+
with open(path, 'wt') as f:
89+
f.write('#!/usr/bin/env python3\nimport sys\n' + content)
90+
os.chmod(path, 0o755)
91+
92+
def _scan_package_assertion_failures(tmpdir, monkeypatch, gdb_stdout):
93+
# -j2 run: two child processes crash with an assertion failure
94+
_write_script(os.path.join(str(tmpdir), 'cppcheck'),
95+
'sys.stderr.write("cppcheck: lib/checkb.cpp:20: void checkB(): Assertion `y\' failed.\\n"\n'
96+
' "cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n"\n'
97+
' "src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n"\n'
98+
' "src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n")\n'
99+
'sys.exit(1)\n')
100+
# gdb run on src/a.c
101+
_write_script(os.path.join(str(tmpdir), 'gdb'),
102+
'sys.stdout.write({!r})\n'.format(gdb_stdout) +
103+
'sys.stderr.write("cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n")\n')
104+
monkeypatch.setenv('PATH', str(tmpdir) + os.pathsep + os.environ['PATH'])
105+
monkeypatch.setattr('donate_cpu_lib.__jobs', '-j2')
106+
returncode, stacktrace, _, _, _, _ = scan_package(str(tmpdir), str(tmpdir), [])
107+
assert returncode == -6
108+
return stacktrace
109+
110+
@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb')
111+
def test_scan_package_assertion_failure(tmpdir, monkeypatch):
112+
# only the assertion failure from the gdb run is put in the stack trace
113+
stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch,
114+
'Checking src/a.c ...\n\nProgram received signal SIGABRT, Aborted.\n'
115+
'#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n')
116+
assert stacktrace == 'Checking src/a.c ...\n\n' \
117+
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
118+
'Program received signal SIGABRT, Aborted.\n' \
119+
'#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n'
120+
121+
@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb')
122+
def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch):
123+
# no stack trace from gdb => the assertion failures from the first run are used
124+
stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, '')
125+
assert stacktrace == "lib/checkb.cpp:20: void checkB(): Assertion `y' failed.\n" \
126+
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
127+
'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \
128+
'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n'

‎tools/donate_cpu_lib.py‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
1717
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
1818
# changes)
19-
CLIENT_VERSION = "1.4.1"
19+
CLIENT_VERSION = "1.4.2"
2020

2121
# Timeout for analysis with Cppcheck in seconds
2222
CPPCHECK_TIMEOUT = 30 * 60
@@ -437,6 +437,22 @@ def __run_command(cmd, print_cmd=True):
437437
return return_code, stdout, stderr, elapsed_time
438438

439439

440+
# glibc assertion failure message: "<program>: <file>:<line>: <function>: Assertion `<expression>' failed."
441+
# Example: cppcheck: lib/token.cpp:123: void Token::foo(): Assertion `x' failed.
442+
__assertion_failure_re = re.compile(r"^\S+: (?P<message>[^:\s]+:\d+: .+: Assertion `.+' failed\.)$")
443+
444+
445+
def get_assertion_failures(text):
446+
# The program name is removed from the messages. The servers treat lines that start with "cppcheck: " as
447+
# the version line.
448+
messages = []
449+
for line in text.split('\n'):
450+
m = __assertion_failure_re.match(line.strip())
451+
if m and m.group('message') not in messages:
452+
messages.append(m.group('message'))
453+
return messages
454+
455+
440456
def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, enable='style,information', debug_warnings=True, check_level=None, extra_args=None):
441457
print('Analyze..')
442458
libs = ''
@@ -536,6 +552,8 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
536552
# make sure we have the actual error code set
537553
if has_sig:
538554
returncode = -sig_num
555+
# assertion failures from the first run, used if there is no stack trace from gdb
556+
assertion_failures = get_assertion_failures(stderr)
539557
stacktrace = ''
540558
if capture_callstack:
541559
# re-run within gdb to get a stacktrace
@@ -544,20 +562,31 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
544562
cmd += sig_file
545563
else:
546564
cmd += dir_to_scan
547-
_, st_stdout, _, _ = __run_command(cmd)
565+
_, st_stdout, st_stderr, _ = __run_command(cmd)
548566
gdb_pos = st_stdout.find(" received signal")
549567
if gdb_pos != -1:
568+
# with -jN the first run can have assertion failures from several crashes, gdb only re-runs one file
569+
# => only use the assertion failures from the gdb run
570+
assertion_failures = get_assertion_failures(st_stderr)
550571
last_check_pos = st_stdout.rfind('Checking ', 0, gdb_pos)
551572
if last_check_pos == -1:
552573
stacktrace = st_stdout[gdb_pos:]
553574
else:
554-
stacktrace = st_stdout[last_check_pos:]
575+
# put the assertion failure messages before the ' received signal ' line, the servers parse the stack trace after that line
576+
signal_line_pos = max(st_stdout.rfind('\n', 0, gdb_pos) + 1, last_check_pos)
577+
stacktrace = st_stdout[last_check_pos:signal_line_pos] + \
578+
''.join(msg + '\n' for msg in assertion_failures) + \
579+
st_stdout[signal_line_pos:]
580+
assertion_failures = []
555581
# if no stacktrace was generated return the original stdout or internal errors list
556582
if not stacktrace:
557583
if has_sig:
558584
stacktrace = ''.join(internal_error_messages_list)
559585
else:
560586
stacktrace = stdout
587+
# assertion failure messages that were not put in the gdb output
588+
if assertion_failures:
589+
stacktrace = ''.join(msg + '\n' for msg in assertion_failures) + stacktrace
561590
return returncode, stacktrace, '', returncode, options_j, ''
562591

563592
if returncode != 0:

0 commit comments

Comments
 (0)