1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#!/usr/bin/env python3
import argparse
import json
import subprocess
from pathlib import Path
SANITIZERS = {
"asan-ubsan": {
"flag": "-fsanitize=address,undefined",
"symbols": ("__asan_", "__ubsan_"),
"runtimes": ("libasan.so", "libubsan.so"),
},
"tsan": {
"flag": "-fsanitize=thread",
"symbols": ("__tsan_",),
"runtimes": ("libtsan.so",),
},
}
LANGUAGE_SUFFIXES = {
"C": {".c"},
"C++": {".cc", ".cpp", ".cxx"},
"Fortran": {".f", ".f03", ".f08", ".f90", ".f95", ".for", ".ftn"},
}
def fail(message):
raise SystemExit(f"ERROR: {message}")
def command_text(entry):
if "command" in entry:
return entry["command"]
return " ".join(entry.get("arguments", []))
def require_file(path):
if not path.is_file():
fail(f"expected build artifact is missing: {path}")
def tool_output(*arguments):
result = subprocess.run(
arguments,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
return result.stdout
def verify_compile_commands(build_dir, sanitizer_flag):
commands_path = build_dir / "compile_commands.json"
require_file(commands_path)
commands = json.loads(commands_path.read_text(encoding="utf-8"))
language_counts = {language: 0 for language in LANGUAGE_SUFFIXES}
uninstrumented = []
for entry in commands:
suffix = Path(entry.get("file", "")).suffix.lower()
for language, suffixes in LANGUAGE_SUFFIXES.items():
if suffix not in suffixes:
continue
language_counts[language] += 1
if sanitizer_flag not in command_text(entry):
uninstrumented.append(entry.get("file", "<unknown>"))
missing_languages = [
language for language, count in language_counts.items() if count == 0
]
if missing_languages:
fail(
"no compile commands were found for: " + ", ".join(missing_languages)
)
if uninstrumented:
examples = ", ".join(uninstrumented[:10])
suffix = "" if len(uninstrumented) <= 10 else ", ..."
fail(f"compile commands without sanitizer instrumentation: {examples}{suffix}")
return language_counts
def verify_archive(path, symbol):
require_file(path)
symbols = tool_output("nm", "-A", "--undefined-only", str(path))
if symbol not in symbols:
fail(f"{path.name} has no reference to {symbol}")
def verify_link(build_dir, target, sanitizer_flag, runtimes):
link_command_path = build_dir / "CMakeFiles" / f"{target}.dir" / "link.txt"
executable_path = build_dir / target
require_file(link_command_path)
require_file(executable_path)
if sanitizer_flag not in link_command_path.read_text(encoding="utf-8"):
fail(f"{target} final link command does not contain {sanitizer_flag}")
dynamic_section = tool_output("readelf", "-d", str(executable_path))
for runtime in runtimes:
if runtime not in dynamic_section:
fail(f"{target} does not declare a dependency on {runtime}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("build_dir", type=Path)
parser.add_argument(
"--sanitizer", choices=SANITIZERS, default="asan-ubsan"
)
args = parser.parse_args()
build_dir = args.build_dir.resolve()
if not build_dir.is_dir():
fail(f"build directory does not exist: {build_dir}")
sanitizer = SANITIZERS[args.sanitizer]
language_counts = verify_compile_commands(build_dir, sanitizer["flag"])
for symbol in sanitizer["symbols"]:
verify_archive(build_dir / "libwsjt_cxx.a", symbol)
verify_archive(build_dir / "libwsjt_fort_omp.a", symbol)
verify_link(build_dir, "jt9", sanitizer["flag"], sanitizer["runtimes"])
verify_link(build_dir, "wsjtx", sanitizer["flag"], sanitizer["runtimes"])
print("Sanitizer instrumentation verified")
compile_summary = ", ".join(
f"{language}={count}" for language, count in language_counts.items()
)
print(f" compile commands: {compile_summary}")
print(" static archives: libwsjt_cxx.a, libwsjt_fort_omp.a")
print(" final executables: jt9, wsjtx")
if __name__ == "__main__":
main()
|