Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
60 changes: 60 additions & 0 deletions test/tools/donate_cpu_lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys

import pytest

from donate_cpu_lib import *

def _test_library_includes(tmpdir, libs, content, libinc_obj=None):
Expand Down Expand Up @@ -66,3 +70,59 @@ def test_match_multiple_time(tmpdir):
# there was a bug that we would only match each library once successfully
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)

def test_get_assertion_failures():
assert get_assertion_failures('') == []
assert get_assertion_failures('Checking file.cpp ...\n') == []
glibc_msg = "cppcheck: build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed."
# the program name is removed
msg = "build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed."
assert get_assertion_failures('Checking file.cpp ...\n' + glibc_msg + '\n') == [msg]
# same message twice => reported once
assert get_assertion_failures(glibc_msg + '\n' + glibc_msg + '\n') == [msg]
# not an assertion failure message
assert get_assertion_failures("Assertion `x' failed.\n") == []
assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == []

def _write_script(path, content):
with open(path, 'wt') as f:
f.write('#!/usr/bin/env python3\nimport sys\n' + content)
os.chmod(path, 0o755)

def _scan_package_assertion_failures(tmpdir, monkeypatch, gdb_stdout):
# -j2 run: two child processes crash with an assertion failure
_write_script(os.path.join(str(tmpdir), 'cppcheck'),
'sys.stderr.write("cppcheck: lib/checkb.cpp:20: void checkB(): Assertion `y\' failed.\\n"\n'
' "cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n"\n'
' "src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n"\n'
' "src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n")\n'
'sys.exit(1)\n')
# gdb run on src/a.c
_write_script(os.path.join(str(tmpdir), 'gdb'),
'sys.stdout.write({!r})\n'.format(gdb_stdout) +
'sys.stderr.write("cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n")\n')
monkeypatch.setenv('PATH', str(tmpdir) + os.pathsep + os.environ['PATH'])
monkeypatch.setattr('donate_cpu_lib.__jobs', '-j2')
returncode, stacktrace, _, _, _, _ = scan_package(str(tmpdir), str(tmpdir), [])
assert returncode == -6
return stacktrace

@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb')
def test_scan_package_assertion_failure(tmpdir, monkeypatch):
# only the assertion failure from the gdb run is put in the stack trace
stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch,
'Checking src/a.c ...\n\nProgram received signal SIGABRT, Aborted.\n'
'#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n')
assert stacktrace == 'Checking src/a.c ...\n\n' \
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
'Program received signal SIGABRT, Aborted.\n' \
'#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n'

@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb')
def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch):
# no stack trace from gdb => the assertion failures from the first run are used
stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, '')
assert stacktrace == "lib/checkb.cpp:20: void checkB(): Assertion `y' failed.\n" \
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \
'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n'
35 changes: 32 additions & 3 deletions tools/donate_cpu_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
# changes)
CLIENT_VERSION = "1.4.1"
CLIENT_VERSION = "1.4.2"

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


# glibc assertion failure message: "<program>: <file>:<line>: <function>: Assertion `<expression>' failed."
# Example: cppcheck: lib/token.cpp:123: void Token::foo(): Assertion `x' failed.
__assertion_failure_re = re.compile(r"^\S+: (?P<message>[^:\s]+:\d+: .+: Assertion `.+' failed\.)$")


def get_assertion_failures(text):
# The program name is removed from the messages. The servers treat lines that start with "cppcheck: " as
# the version line.
messages = []
for line in text.split('\n'):
m = __assertion_failure_re.match(line.strip())
if m and m.group('message') not in messages:
messages.append(m.group('message'))
return messages


def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, enable='style,information', debug_warnings=True, check_level=None, extra_args=None):
print('Analyze..')
libs = ''
Expand Down Expand Up @@ -536,6 +552,8 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
# make sure we have the actual error code set
if has_sig:
returncode = -sig_num
# assertion failures from the first run, used if there is no stack trace from gdb
assertion_failures = get_assertion_failures(stderr)
stacktrace = ''
if capture_callstack:
# re-run within gdb to get a stacktrace
Expand All @@ -544,20 +562,31 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
cmd += sig_file
else:
cmd += dir_to_scan
_, st_stdout, _, _ = __run_command(cmd)
_, st_stdout, st_stderr, _ = __run_command(cmd)
gdb_pos = st_stdout.find(" received signal")
if gdb_pos != -1:
# with -jN the first run can have assertion failures from several crashes, gdb only re-runs one file
# => only use the assertion failures from the gdb run
assertion_failures = get_assertion_failures(st_stderr)
last_check_pos = st_stdout.rfind('Checking ', 0, gdb_pos)
if last_check_pos == -1:
stacktrace = st_stdout[gdb_pos:]
else:
stacktrace = st_stdout[last_check_pos:]
# put the assertion failure messages before the ' received signal ' line, the servers parse the stack trace after that line
signal_line_pos = max(st_stdout.rfind('\n', 0, gdb_pos) + 1, last_check_pos)
stacktrace = st_stdout[last_check_pos:signal_line_pos] + \
Comment thread
danmar marked this conversation as resolved.
Resolved
''.join(msg + '\n' for msg in assertion_failures) + \
st_stdout[signal_line_pos:]
assertion_failures = []
# if no stacktrace was generated return the original stdout or internal errors list
if not stacktrace:
if has_sig:
stacktrace = ''.join(internal_error_messages_list)
else:
stacktrace = stdout
# assertion failure messages that were not put in the gdb output
if assertion_failures:
stacktrace = ''.join(msg + '\n' for msg in assertion_failures) + stacktrace
return returncode, stacktrace, '', returncode, options_j, ''

if returncode != 0:
Expand Down
Loading