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
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ jobs:
pip install -r requirements_py/dev/requirements.txt
make test

- name: Run memory-safety-reporter tests
working-directory: tools/memory-safety-reporter
run: |
pip install -r requirements_py/dev/requirements.txt
make package
make test

- name: Run report-converter tests
working-directory: tools/report-converter
run: |
Expand Down
23 changes: 23 additions & 0 deletions tools/memory-safety-reporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# CodeChecker Memory Safety Reporter

A tool to collect all important information about a given analyzation.
The tool collects the reports from the run in sarif format, the metadata,
all the settings for all the checkers set at the time of the run and
the passed setting to CodeChecker then it creates an archive in either
zip or tar.gz format.

## Usage

The tool needs only the report directory to run but other optional arguments can also be used.

Running the tool: `memory-safety-reporter -o MemorySafetyReport -r /path/to/report/directory`

For the extensive list of argument, see the help of the script!

## Requirements

- Python >= 3.9

## Authors

CodeChecker Team (Ericsson)
204 changes: 204 additions & 0 deletions tools/memory-safety-reporter/memory_safety_reporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#!/usr/bin/env python3
Comment thread
gulyasgergely902 marked this conversation as resolved.
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------

"""Memory Safety Report Generator"""


import argparse
import hashlib
import logging
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path

LOG = logging.getLogger('memory-safety-reporter')

log_handler = logging.StreamHandler(sys.stdout)
msg_formatter = logging.Formatter('[%(levelname)s] - %(message)s')
log_handler.setFormatter(msg_formatter)
LOG.setLevel(logging.INFO)
LOG.addHandler(log_handler)


def generate_checker_details(to_dir: Path):
Comment thread
barnabasdomozi marked this conversation as resolved.
"""Generate checker_details.json file using `CodeChecker checkers`"""
with subprocess.Popen(
[
"CodeChecker",
"checkers",
"--guideline",
"memory-safety",
"-o",
"json",
"--details"
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
) as process:
while process.poll() is None:
pass
if process.returncode != 0:
raise RuntimeError("Failed to get checker details")
out, _ = process.communicate()
with open(os.path.join(to_dir, "checker_details.json"), "wb") as file:
file.write(out)


def generate_reports_sarif(reports_dir: Path, to_dir: Path):
"""Generate reports.sarif file using the reports directory"""
result = subprocess.run(
[
"CodeChecker",
"parse",
reports_dir,
"-e",
"sarif",
"-o",
os.path.join(to_dir, "reports.sarif")
],
check=False
)

if result.returncode not in (2, 0):
raise RuntimeError("Failed to parse reports directory")


def collect_argsfiles(reports_dir: Path, to_dir: Path):
"""Collect all args files (e.g. saargs, tidyargs)"""
shutil.copytree(os.path.join(reports_dir, "conf"), to_dir)


def generate_checksum(path: Path):
"""Generate checksum file recursively for all files
originated from `path`"""
checksum_file = os.path.join(path, "CHECKSUMS.sha256")
with open(checksum_file, "w", encoding="utf-8") as file:
for root, _, files in os.walk(path):
for filename in files:
if filename == "CHECKSUMS.sha256":
continue
filepath = os.path.join(root, filename)
sha256 = hashlib.sha256()
with open(filepath, "rb") as file_handle:
Comment thread
barnabasdomozi marked this conversation as resolved.
while chunk := file_handle.read(8192):
sha256.update(chunk)
rel_path = os.path.relpath(filepath, path)
file.write(f"{sha256.hexdigest()}\t{rel_path}\n")


def create_package(parsed_args):
"""Assemble and generate package"""
set_arguments = [parsed_args.output_file]
for arg_val in [
parsed_args.product_name,
parsed_args.revision,
parsed_args.binary_name,
parsed_args.build_id,
parsed_args.timestamp
]:
if arg_val is not None:
set_arguments.append(arg_val)
base_file_name = "_".join(set_arguments)

if not os.path.isdir(parsed_args.report_directory):
LOG.error(
"Report directory '%s' does not exist!",
parsed_args.report_directory
)
sys.exit(1)

with tempfile.TemporaryDirectory() as tmpdir:
temp_dir = Path(tmpdir)
base_dir = temp_dir / base_file_name
base_dir.mkdir(parents=True, exist_ok=True)

generate_reports_sarif(parsed_args.report_directory, base_dir)

shutil.copy(
os.path.join(parsed_args.report_directory, "metadata.json"),
base_dir
)

generate_checker_details(base_dir)

config_dir = base_dir / "config"
collect_argsfiles(parsed_args.report_directory, config_dir)

generate_checksum(base_dir)

print("Creating package with name: ", base_file_name)
shutil.make_archive(
base_name=base_file_name,
format=parsed_args.extension,
root_dir=temp_dir,
base_dir=base_file_name
)


def main():
DESCRIPTION = "Memory safety analysis report creator tool."

parser = argparse.ArgumentParser(
description=DESCRIPTION)

parser.add_argument('-o',
type=str,
dest="output_file",
required=False,
help="The name of the output archive",
default="memory_safety_report")
parser.add_argument('-r',
type=str,
dest="report_directory",
required=True,
help="The path of the report directory")
parser.add_argument('-p',
type=str,
dest="product_name",
required=False,
help="Product or system identifier (e.g. CXC123456)")
parser.add_argument('-v',
type=str,
dest="revision",
required=False,
help="RState of the product (e.g. R10S25)")
parser.add_argument('-b',
type=str,
dest="binary_name",
required=False,
help="Name of the binary (e.g. tinyxml)")
parser.add_argument('-d',
type=str,
dest="build_id",
required=False,
help="""Build or pipeline identifier (e.g. CI job id,
LMC, branch+change, commit hash)""")
parser.add_argument('-t',
type=str,
dest="timestamp",
required=False,
help="Timestamp at the end of the analysis, in UTC")
parser.add_argument('-x',
choices=['gztar', 'zip'],
type=str,
dest="extension",
required=False,
help="The extension of the archive (e.g. tar.gz, zip)",
default="zip")

args = parser.parse_args()

create_package(args)


if __name__ == '__main__':
main()
8 changes: 8 additions & 0 deletions tools/memory-safety-reporter/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[pytest]

addopts =
# increase verbosity level
--verbose

# do not capture stdout
--capture=sys
2 changes: 2 additions & 0 deletions tools/memory-safety-reporter/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest<9
codechecker
14 changes: 14 additions & 0 deletions tools/memory-safety-reporter/test-report/compile_cmd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"directory": "/local/test_projects/tinyxml2/build",
"command": "/bin/c++ -D_FILE_OFFSET_BITS=64 -I/local/test_projects/tinyxml2 -fvisibility=hidden -fvisibility-inlines-hidden -o CMakeFiles/tinyxml2.dir/tinyxml2.cpp.o -c /local/test_projects/tinyxml2/tinyxml2.cpp",
"file": "/local/test_projects/tinyxml2/tinyxml2.cpp",
"output": "CMakeFiles/tinyxml2.dir/tinyxml2.cpp.o"
},
{
"directory": "/local/test_projects/tinyxml2/build",
"command": "/bin/c++ -D_FILE_OFFSET_BITS=64 -I/local/test_projects/tinyxml2 -fvisibility=hidden -fvisibility-inlines-hidden -o CMakeFiles/xmltest.dir/xmltest.cpp.o -c /local/test_projects/tinyxml2/xmltest.cpp",
"file": "/local/test_projects/tinyxml2/xmltest.cpp",
"output": "CMakeFiles/xmltest.dir/xmltest.cpp.o"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"[\"/bin/c++\", \"c++\", []]": {"compiler_includes": ["/usr/include/c++/13", "/usr/include/x86_64-linux-gnu/c++/13", "/usr/include/c++/13/backward", "/usr/lib/gcc/x86_64-linux-gnu/13/include", "/usr/local/include", "/usr/include/x86_64-linux-gnu", "/usr/include"], "compiler_standard": "-std=gnu++17", "target": "x86_64-linux-gnu"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
1 change: 1 addition & 0 deletions tools/memory-safety-reporter/test-report/conf/saargs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
1 change: 1 addition & 0 deletions tools/memory-safety-reporter/test-report/conf/skip_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
Loading
Loading