| 1 |
|
| 2 |
"""Run a test repeatedly with mallocfail. |
| 3 |
|
| 4 |
Usage: run_mallocfail [--ctest=<cost>] [<exe>...] |
| 5 |
|
| 6 |
This runs the programs with mallocfail until there are no more additional |
| 7 |
stack hashes for mallocfail to try out. |
| 8 |
|
| 9 |
Passing "--ctest" additionally runs all the tests with a cost lower than the |
| 10 |
given flag value. Cost is measured in seconds of runtime. |
| 11 |
|
| 12 |
You need to build mallocfail (https://github.com/ralight/mallocfail) and install |
| 13 |
it to /usr/local/lib/mallocfail. Change _MALLOCFAIL_SO below if you want it |
| 14 |
elsewhere. |
| 15 |
""" |
| 16 |
import glob |
| 17 |
import multiprocessing |
| 18 |
import os |
| 19 |
import shutil |
| 20 |
import subprocess |
| 21 |
import sys |
| 22 |
import tempfile |
| 23 |
import time |
| 24 |
from typing import Dict |
| 25 |
from typing import List |
| 26 |
from typing import NoReturn |
| 27 |
from typing import Optional |
| 28 |
from typing import Tuple |
| 29 |
|
| 30 |
_PRIMER = "auto_tests/auto_version_test" |
| 31 |
_MALLOCFAIL_SO = "/usr/local/lib/mallocfail.so" |
| 32 |
_HASHES = "mallocfail_hashes" |
| 33 |
_HASHES_PREV = "mallocfail_hashes.prev" |
| 34 |
_TIMEOUT = 3.0 |
| 35 |
_BUILD_DIR = os.getcwd() |
| 36 |
|
| 37 |
_ENV = { |
| 38 |
"LD_PRELOAD": _MALLOCFAIL_SO, |
| 39 |
"UBSAN_OPTIONS": "color=always,print_stacktrace=1,exitcode=11", |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
def run_mallocfail(tmpdir: str, timeout: float, exe: str, iteration: int, |
| 44 |
keep_going: bool) -> bool: |
| 45 |
"""Run a program with mallocfail.""" |
| 46 |
print(f"\x1b[1;33mmallocfail '{exe}' run #{iteration}\x1b[0m") |
| 47 |
hashes = os.path.join(tmpdir, _HASHES) |
| 48 |
hashes_prev = os.path.join(tmpdir, _HASHES_PREV) |
| 49 |
profraw = os.path.join(_BUILD_DIR, "mallocfail.out", exe, "%p.profraw") |
| 50 |
if os.path.exists(hashes): |
| 51 |
shutil.copy(hashes, hashes_prev) |
| 52 |
try: |
| 53 |
proc = subprocess.run( |
| 54 |
[exe], |
| 55 |
timeout=timeout, |
| 56 |
env={ |
| 57 |
"LLVM_PROFILE_FILE": profraw, |
| 58 |
**_ENV, |
| 59 |
}, |
| 60 |
cwd=tmpdir, |
| 61 |
) |
| 62 |
except subprocess.TimeoutExpired: |
| 63 |
print(f"\x1b[1;34mProgram {exe} timed out\x1b[0m") |
| 64 |
return True |
| 65 |
finally: |
| 66 |
assert os.path.exists(hashes) |
| 67 |
if os.path.exists(hashes_prev): |
| 68 |
with open(hashes_prev, "r") as prev: |
| 69 |
with open(hashes, "r") as cur: |
| 70 |
if prev.read() == cur.read(): |
| 71 |
|
| 72 |
return False |
| 73 |
|
| 74 |
if proc.returncode in (0, 1): |
| 75 |
|
| 76 |
pass |
| 77 |
elif proc.returncode == -6: |
| 78 |
|
| 79 |
pass |
| 80 |
elif proc.returncode == 7: |
| 81 |
|
| 82 |
pass |
| 83 |
elif proc.returncode == -14: |
| 84 |
print(f"\x1b[0;34mProgram '{exe}' timed out\x1b[0m") |
| 85 |
else: |
| 86 |
print( |
| 87 |
f"\x1b[1;32mProgram '{exe}' failed to handle OOM situation cleanly (code {proc.returncode})\x1b[0m" |
| 88 |
) |
| 89 |
if not keep_going: |
| 90 |
raise Exception("Aborting test") |
| 91 |
|
| 92 |
return True |
| 93 |
|
| 94 |
|
| 95 |
def ctest_costs() -> Dict[str, float]: |
| 96 |
with open("Testing/Temporary/CTestCostData.txt", "r") as fh: |
| 97 |
costs = {} |
| 98 |
for line in fh.readlines(): |
| 99 |
if line.startswith("---"): |
| 100 |
break |
| 101 |
prog, _, cost = line.rstrip().split(" ") |
| 102 |
costs[prog] = float(cost) |
| 103 |
return costs |
| 104 |
|
| 105 |
|
| 106 |
def find_prog(name: str) -> Tuple[Optional[str], ...]: |
| 107 |
def attempt(path: str) -> Optional[str]: |
| 108 |
if os.path.exists(path): |
| 109 |
return path |
| 110 |
return None |
| 111 |
|
| 112 |
return (attempt(f"auto_tests/auto_{name}_test"), |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
def parse_flags(args: List[str]) -> Tuple[Dict[str, str], List[str]]: |
| 117 |
flags: Dict[str, str] = {} |
| 118 |
exes: List[str] = [] |
| 119 |
for arg in args: |
| 120 |
if arg.startswith("--"): |
| 121 |
flag, value = arg.split("=", 1) |
| 122 |
flags[flag] = value |
| 123 |
else: |
| 124 |
exes.append(arg) |
| 125 |
return flags, exes |
| 126 |
|
| 127 |
|
| 128 |
def loop_mallocfail(tmpdir: str, |
| 129 |
timeout: float, |
| 130 |
exe: str, |
| 131 |
keep_going: bool = False) -> None: |
| 132 |
i = 1 |
| 133 |
while run_mallocfail(tmpdir, timeout, exe, i, keep_going): |
| 134 |
i += 1 |
| 135 |
|
| 136 |
|
| 137 |
def isolated_mallocfail(timeout: int, exe: str) -> None: |
| 138 |
with tempfile.TemporaryDirectory(prefix="mallocfail") as tmpdir: |
| 139 |
print(f"\x1b[1;33mRunning for {exe} in isolated path {tmpdir}\x1b[0m") |
| 140 |
os.mkdir(os.path.join(tmpdir, "auto_tests")) |
| 141 |
shutil.copy(exe, os.path.join(tmpdir, exe)) |
| 142 |
shutil.copy(_HASHES, os.path.join(tmpdir, _HASHES)) |
| 143 |
loop_mallocfail(tmpdir, timeout, exe) |
| 144 |
profraw = os.path.join(tmpdir, "default.profraw") |
| 145 |
if os.path.exists(profraw): |
| 146 |
shutil.copy(profraw, exe + ".mallocfail.profraw") |
| 147 |
|
| 148 |
|
| 149 |
def main(args: List[str]) -> None: |
| 150 |
"""Run a program repeatedly under mallocfail.""" |
| 151 |
if len(args) == 1: |
| 152 |
print(f"Usage: {args[0]} <exe>") |
| 153 |
sys.exit(1) |
| 154 |
|
| 155 |
flags, exes = parse_flags(args[1:]) |
| 156 |
|
| 157 |
timeout = _TIMEOUT |
| 158 |
if "--ctest" in flags: |
| 159 |
costs = ctest_costs() |
| 160 |
max_cost = float(flags["--ctest"]) |
| 161 |
timeout = max(max_cost + 1, timeout) |
| 162 |
exes.extend(prog for test in costs.keys() for prog in find_prog(test) |
| 163 |
if costs[test] <= max_cost and prog) |
| 164 |
if "--jobs" in flags: |
| 165 |
jobs = int(flags["--jobs"]) |
| 166 |
else: |
| 167 |
jobs = 1 |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if os.path.exists(_PRIMER): |
| 173 |
print(f"\x1b[1;33mPriming hashes with {_PRIMER}\x1b[0m") |
| 174 |
loop_mallocfail(os.getcwd(), timeout, _PRIMER, keep_going=True) |
| 175 |
|
| 176 |
print(f"\x1b[1;33m--------------------------------\x1b[0m") |
| 177 |
print(f"\x1b[1;33mStarting mallocfail for {len(exes)} programs:\x1b[0m") |
| 178 |
print(f"\x1b[1;33m{exes}\x1b[0m") |
| 179 |
print(f"\x1b[1;33m--------------------------------\x1b[0m") |
| 180 |
time.sleep(1) |
| 181 |
with multiprocessing.Pool(jobs) as p: |
| 182 |
done = tuple( |
| 183 |
p.starmap(isolated_mallocfail, ((timeout, exe) for exe in exes))) |
| 184 |
print(f"\x1b[1;32mCompleted {len(done)} programs\x1b[0m") |
| 185 |
|
| 186 |
|
| 187 |
if __name__ == "__main__": |
| 188 |
main(sys.argv) |