-
Notifications
You must be signed in to change notification settings - Fork 472
[feat] Add memory safety reporter script #4962
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
Open
gulyasgergely902
wants to merge
1
commit into
Ericsson:master
Choose a base branch
from
gulyasgergely902:add-memory-safety-report-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| #!/usr/bin/env python3 | ||
| # ------------------------------------------------------------------------- | ||
| # | ||
| # 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): | ||
|
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: | ||
|
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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pytest<9 | ||
| codechecker |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"}} |
1 change: 1 addition & 0 deletions
1
tools/memory-safety-reporter/test-report/conf/codechecker_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test |
1 change: 1 addition & 0 deletions
1
tools/memory-safety-reporter/test-report/conf/review_status.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Test |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.