ClickHouse/tests/ci/libfuzzer_test_check.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

301 lines
8.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import argparse
import logging
import os
2024-10-19 04:28:48 +00:00
import re
import sys
2023-09-12 16:30:52 +00:00
import zipfile
from pathlib import Path
from typing import List
2024-10-17 01:11:45 +00:00
from botocore.exceptions import ClientError
from build_download_helper import download_fuzzers
2024-02-26 17:46:15 +00:00
from clickhouse_helper import CiLogsCredentials
from docker_images_helper import DockerImage, get_docker_image, pull_image
2024-10-17 01:00:27 +00:00
from env_helper import REPO_COPY, REPORT_PATH, S3_BUILDS_BUCKET, TEMP_PATH
from pr_info import PRInfo
2024-10-19 06:36:10 +00:00
from report import FAILURE, SUCCESS, JobReport, TestResult
2024-10-17 01:00:27 +00:00
from s3_helper import S3Helper
from stopwatch import Stopwatch
2023-09-12 20:56:42 +00:00
from tee_popen import TeePopen
NO_CHANGES_MSG = "Nothing to run"
2024-10-17 01:00:27 +00:00
s3 = S3Helper()
2024-10-17 12:42:51 +00:00
def zipdir(path, ziph):
# ziph is zipfile handle
2024-10-17 13:35:41 +00:00
for root, _, files in os.walk(path):
2024-10-17 12:42:51 +00:00
for file in files:
2024-10-17 13:00:31 +00:00
ziph.write(
2024-10-17 13:23:23 +00:00
os.path.join(root, file),
2024-10-17 13:12:04 +00:00
os.path.relpath(os.path.join(root, file), os.path.join(path, "..")),
2024-10-17 13:00:31 +00:00
)
2024-10-17 12:42:51 +00:00
def get_additional_envs(check_name, run_by_hash_num, run_by_hash_total):
result = []
if "DatabaseReplicated" in check_name:
result.append("USE_DATABASE_REPLICATED=1")
if "DatabaseOrdinary" in check_name:
result.append("USE_DATABASE_ORDINARY=1")
if "wide parts enabled" in check_name:
result.append("USE_POLYMORPHIC_PARTS=1")
if "ParallelReplicas" in check_name:
result.append("USE_PARALLEL_REPLICAS=1")
if "s3 storage" in check_name:
result.append("USE_S3_STORAGE_FOR_MERGE_TREE=1")
result.append("RANDOMIZE_OBJECT_KEY_TYPE=1")
if "analyzer" in check_name:
2024-03-20 12:35:30 +00:00
result.append("USE_OLD_ANALYZER=1")
if run_by_hash_total != 0:
result.append(f"RUN_BY_HASH_NUM={run_by_hash_num}")
result.append(f"RUN_BY_HASH_TOTAL={run_by_hash_total}")
return result
def get_run_command(
fuzzers_path: Path,
repo_path: Path,
result_path: Path,
additional_envs: List[str],
ci_logs_args: str,
image: DockerImage,
) -> str:
additional_options = ["--hung-check"]
additional_options.append("--print-time")
additional_options_str = (
'-e ADDITIONAL_OPTIONS="' + " ".join(additional_options) + '"'
)
envs = [
# a static link, don't use S3_URL or S3_DOWNLOAD
2024-10-03 17:32:05 +00:00
'-e S3_URL="https://s3.amazonaws.com"',
]
envs += [f"-e {e}" for e in additional_envs]
env_str = " ".join(envs)
return (
2023-09-12 20:56:42 +00:00
f"docker run "
f"{ci_logs_args} "
f"--workdir=/fuzzers "
f"--volume={fuzzers_path}:/fuzzers "
f"--volume={repo_path}/tests:/usr/share/clickhouse-test "
f"--volume={result_path}:/test_output "
2024-02-25 22:49:25 +00:00
"--security-opt seccomp=unconfined " # required to issue io_uring sys-calls
2024-07-15 19:27:54 +00:00
f"--cap-add=SYS_PTRACE {env_str} {additional_options_str} {image} "
2024-07-16 13:37:59 +00:00
"python3 /usr/share/clickhouse-test/fuzz/runner.py"
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("check_name")
return parser.parse_args()
2024-10-18 00:42:37 +00:00
def download_corpus(path: str):
logging.info("Download corpus...")
2024-10-17 01:00:27 +00:00
try:
2024-10-18 00:42:37 +00:00
s3.download_file(
2024-10-17 01:00:27 +00:00
bucket=S3_BUILDS_BUCKET,
2024-10-18 00:54:20 +00:00
s3_path="fuzzer/corpus.zip",
2024-10-18 00:42:37 +00:00
local_file_path=path,
2024-10-17 01:00:27 +00:00
)
except ClientError as e:
if e.response["Error"]["Code"] == "NoSuchKey":
2024-10-18 00:42:37 +00:00
logging.debug("No active corpus exists")
2024-10-17 01:00:27 +00:00
else:
raise
2024-10-18 00:42:37 +00:00
with zipfile.ZipFile(f"{path}/corpus.zip", "r") as zipf:
zipf.extractall(path)
os.remove(f"{path}/corpus.zip")
units = 0
for _, _, files in os.walk(path):
units += len(files)
logging.info("...downloaded %d units", units)
2024-10-17 01:00:27 +00:00
2024-10-17 16:29:19 +00:00
def upload_corpus(path: str):
2024-10-17 16:35:58 +00:00
with zipfile.ZipFile(f"{path}/corpus.zip", "w", zipfile.ZIP_DEFLATED) as zipf:
2024-10-17 16:29:19 +00:00
zipdir(f"{path}/corpus/", zipf)
2024-10-17 12:42:51 +00:00
s3.upload_file(
2024-10-17 13:00:31 +00:00
bucket=S3_BUILDS_BUCKET,
2024-10-17 16:29:19 +00:00
file_path=f"{path}/corpus.zip",
2024-10-17 13:00:31 +00:00
s3_path="fuzzer/corpus.zip",
2024-10-17 12:42:51 +00:00
)
2024-10-17 01:00:27 +00:00
2024-10-19 04:28:48 +00:00
def process_error(path: Path) -> list:
ERROR = r"^==\d+==\s?ERROR: (\S+): (.*)"
# error_source = ""
# error_reason = ""
# test_unit = ""
2024-10-19 04:41:36 +00:00
# TEST_UNIT_LINE = r"artifact_prefix='.*\/'; Test unit written to (.*)"
2024-10-19 04:28:48 +00:00
error_info = []
is_error = False
2024-10-19 04:41:36 +00:00
with open(path, "r", encoding="utf-8") as file:
2024-10-19 04:28:48 +00:00
for line in file:
2024-10-19 18:09:12 +00:00
line = line.rstrip("\n")
2024-10-19 04:28:48 +00:00
if is_error:
error_info.append(line)
# match = re.search(TEST_UNIT_LINE, line)
# if match:
# test_unit = match.group(1)
continue
match = re.search(ERROR, line)
if match:
error_info.append(line)
# error_source = match.group(1)
# error_reason = match.group(2)
is_error = True
return error_info
def read_status(status_path: Path):
result = []
2024-10-19 04:41:36 +00:00
with open(status_path, "r", encoding="utf-8") as file:
2024-10-19 04:28:48 +00:00
for line in file:
2024-10-19 18:09:12 +00:00
result.append(line.rstrip("\n"))
2024-10-19 04:28:48 +00:00
return result
def process_results(result_path: Path):
test_results = []
oks = 0
2024-10-22 12:25:15 +00:00
errors = 0
2024-10-19 04:28:48 +00:00
fails = 0
for file in result_path.glob("*.status"):
fuzzer = file.stem
2024-10-19 17:38:50 +00:00
file_path = file.parent / fuzzer
2024-10-19 04:28:48 +00:00
file_path_unit = file_path.with_suffix(".unit")
file_path_out = file_path.with_suffix(".out")
status = read_status(file)
2024-10-19 21:51:59 +00:00
result = TestResult(fuzzer, status[0], float(status[2]))
2024-10-19 04:28:48 +00:00
if status[0] == "OK":
oks += 1
2024-10-22 12:25:15 +00:00
elif status[0] == "ERROR":
errors += 1
2024-10-19 21:51:59 +00:00
if file_path_out.exists():
2024-10-20 14:05:18 +00:00
result.set_log_files(f"['{file_path_out}']")
2024-10-19 04:28:48 +00:00
else:
fails += 1
2024-10-19 21:51:59 +00:00
if file_path_out.exists():
result.set_raw_logs("\n".join(process_error(file_path_out)))
2024-10-20 15:19:22 +00:00
if file_path_unit.exists():
2024-10-20 12:38:28 +00:00
result.set_log_files(f"['{file_path_unit}']")
2024-10-20 18:23:34 +00:00
elif file_path_out.exists():
result.set_log_files(f"['{file_path_out}']")
2024-10-19 04:28:48 +00:00
test_results.append(result)
2024-10-22 12:25:15 +00:00
return [oks, errors, fails, test_results]
2024-10-19 04:28:48 +00:00
def main():
logging.basicConfig(level=logging.INFO)
stopwatch = Stopwatch()
2023-09-16 04:41:13 +00:00
temp_path = Path(TEMP_PATH)
reports_path = Path(REPORT_PATH)
temp_path.mkdir(parents=True, exist_ok=True)
2023-09-16 04:41:13 +00:00
repo_path = Path(REPO_COPY)
args = parse_args()
check_name = args.check_name
2023-09-16 04:41:13 +00:00
pr_info = PRInfo()
2023-09-16 04:41:13 +00:00
temp_path.mkdir(parents=True, exist_ok=True)
if "RUN_BY_HASH_NUM" in os.environ:
run_by_hash_num = int(os.getenv("RUN_BY_HASH_NUM", "0"))
run_by_hash_total = int(os.getenv("RUN_BY_HASH_TOTAL", "0"))
else:
run_by_hash_num = 0
run_by_hash_total = 0
docker_image = pull_image(get_docker_image("clickhouse/libfuzzer"))
2023-09-19 15:32:58 +00:00
fuzzers_path = temp_path / "fuzzers"
fuzzers_path.mkdir(parents=True, exist_ok=True)
2024-10-18 00:42:37 +00:00
download_corpus(fuzzers_path)
2023-09-11 19:06:00 +00:00
download_fuzzers(check_name, reports_path, fuzzers_path)
2023-09-12 16:30:52 +00:00
for file in os.listdir(fuzzers_path):
2023-09-12 20:56:42 +00:00
if file.endswith("_fuzzer"):
os.chmod(fuzzers_path / file, 0o777)
2023-09-12 20:56:42 +00:00
elif file.endswith("_seed_corpus.zip"):
2024-10-17 20:56:26 +00:00
seed_corpus_path = fuzzers_path / (
file.removesuffix("_seed_corpus.zip") + ".in"
)
2024-02-26 17:46:15 +00:00
with zipfile.ZipFile(fuzzers_path / file, "r") as zfd:
2024-10-17 20:50:21 +00:00
zfd.extractall(seed_corpus_path)
2023-09-19 15:32:58 +00:00
result_path = temp_path / "result_path"
result_path.mkdir(parents=True, exist_ok=True)
run_log_path = result_path / "run.log"
additional_envs = get_additional_envs(
check_name, run_by_hash_num, run_by_hash_total
)
ci_logs_credentials = CiLogsCredentials(Path(temp_path) / "export-logs-config.sh")
ci_logs_args = ci_logs_credentials.get_docker_arguments(
pr_info, stopwatch.start_time_str, check_name
)
run_command = get_run_command(
fuzzers_path,
2023-09-16 04:41:13 +00:00
repo_path,
result_path,
additional_envs,
ci_logs_args,
docker_image,
)
2023-09-12 16:30:52 +00:00
logging.info("Going to run libFuzzer tests: %s", run_command)
2023-09-12 20:56:42 +00:00
with TeePopen(run_command, run_log_path) as process:
retcode = process.wait()
if retcode == 0:
logging.info("Run successfully")
2024-10-17 16:29:19 +00:00
upload_corpus(fuzzers_path)
2023-09-12 20:56:42 +00:00
else:
logging.info("Run failed")
2024-10-19 17:13:47 +00:00
results = process_results(result_path)
2024-10-19 04:28:48 +00:00
success = results[1] == 0 and results[2] == 0
JobReport(
2024-10-22 12:25:15 +00:00
description=f"OK: {results[0]}, ERROR: {results[1]}, FAIL: {results[2]}",
2024-10-19 04:28:48 +00:00
test_results=results[3],
2024-10-19 06:36:10 +00:00
status=SUCCESS if success else FAILURE,
2024-10-19 04:28:48 +00:00
start_time=stopwatch.start_time_str,
duration=stopwatch.duration_seconds,
additional_files=[],
).dump()
if not success:
sys.exit(1)
if __name__ == "__main__":
main()