2023-07-25 11:53:09 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Run a test repeatedly with mallocfail.
|
|
|
|
|
|
|
|
Usage: run_mallocfail [--ctest=<cost>] [<exe>...]
|
|
|
|
|
|
|
|
This runs the programs with mallocfail until there are no more additional
|
|
|
|
stack hashes for mallocfail to try out.
|
|
|
|
|
|
|
|
Passing "--ctest" additionally runs all the tests with a cost lower than the
|
|
|
|
given flag value. Cost is measured in seconds of runtime.
|
|
|
|
|
|
|
|
You need to build mallocfail (https://github.com/ralight/mallocfail) and install
|
|
|
|
it to /usr/local/lib/mallocfail. Change _MALLOCFAIL_SO below if you want it
|
|
|
|
elsewhere.
|
|
|
|
"""
|
|
|
|
import glob
|
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
from typing import Dict
|
|
|
|
from typing import List
|
|
|
|
from typing import NoReturn
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Tuple
|
|
|
|
|
2024-01-12 21:30:48 +01:00
|
|
|
_PRIMER = "auto_tests/auto_version_test"
|
2023-07-25 11:53:09 +02:00
|
|
|
_MALLOCFAIL_SO = "/usr/local/lib/mallocfail.so"
|
|
|
|
_HASHES = "mallocfail_hashes"
|
|
|
|
_HASHES_PREV = "mallocfail_hashes.prev"
|
|
|
|
_TIMEOUT = 3.0
|
2024-01-12 21:30:48 +01:00
|
|
|
_BUILD_DIR = os.getcwd()
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
_ENV = {
|
|
|
|
"LD_PRELOAD": _MALLOCFAIL_SO,
|
|
|
|
"UBSAN_OPTIONS": "color=always,print_stacktrace=1,exitcode=11",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
def run_mallocfail(tmpdir: str, timeout: float, exe: str, iteration: int,
|
|
|
|
keep_going: bool) -> bool:
|
2023-07-25 11:53:09 +02:00
|
|
|
"""Run a program with mallocfail."""
|
|
|
|
print(f"\x1b[1;33mmallocfail '{exe}' run #{iteration}\x1b[0m")
|
|
|
|
hashes = os.path.join(tmpdir, _HASHES)
|
|
|
|
hashes_prev = os.path.join(tmpdir, _HASHES_PREV)
|
2024-01-12 21:30:48 +01:00
|
|
|
profraw = os.path.join(_BUILD_DIR, "mallocfail.out", exe, "%p.profraw")
|
2023-07-25 11:53:09 +02:00
|
|
|
if os.path.exists(hashes):
|
|
|
|
shutil.copy(hashes, hashes_prev)
|
|
|
|
try:
|
2024-01-12 21:30:48 +01:00
|
|
|
proc = subprocess.run(
|
|
|
|
[exe],
|
|
|
|
timeout=timeout,
|
|
|
|
env={
|
|
|
|
"LLVM_PROFILE_FILE": profraw,
|
|
|
|
**_ENV,
|
|
|
|
},
|
|
|
|
cwd=tmpdir,
|
|
|
|
)
|
2023-07-25 11:53:09 +02:00
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
print(f"\x1b[1;34mProgram {exe} timed out\x1b[0m")
|
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
assert os.path.exists(hashes)
|
|
|
|
if os.path.exists(hashes_prev):
|
|
|
|
with open(hashes_prev, "r") as prev:
|
|
|
|
with open(hashes, "r") as cur:
|
|
|
|
if prev.read() == cur.read():
|
|
|
|
# Done: no new stack hashes.
|
|
|
|
return False
|
|
|
|
|
|
|
|
if proc.returncode in (0, 1):
|
|
|
|
# Process exited cleanly (success or failure).
|
|
|
|
pass
|
|
|
|
elif proc.returncode == -6:
|
2024-01-12 21:30:48 +01:00
|
|
|
# abort(), we allow it.
|
|
|
|
pass
|
|
|
|
elif proc.returncode == 7:
|
|
|
|
# ck_assert failed, also fine for us.
|
2023-07-25 11:53:09 +02:00
|
|
|
pass
|
|
|
|
elif proc.returncode == -14:
|
|
|
|
print(f"\x1b[0;34mProgram '{exe}' timed out\x1b[0m")
|
|
|
|
else:
|
|
|
|
print(
|
2024-01-12 21:30:48 +01:00
|
|
|
f"\x1b[1;32mProgram '{exe}' failed to handle OOM situation cleanly (code {proc.returncode})\x1b[0m"
|
2023-07-25 11:53:09 +02:00
|
|
|
)
|
2023-12-27 12:37:22 +01:00
|
|
|
if not keep_going:
|
|
|
|
raise Exception("Aborting test")
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def ctest_costs() -> Dict[str, float]:
|
|
|
|
with open("Testing/Temporary/CTestCostData.txt", "r") as fh:
|
|
|
|
costs = {}
|
|
|
|
for line in fh.readlines():
|
|
|
|
if line.startswith("---"):
|
|
|
|
break
|
|
|
|
prog, _, cost = line.rstrip().split(" ")
|
|
|
|
costs[prog] = float(cost)
|
|
|
|
return costs
|
|
|
|
|
|
|
|
|
|
|
|
def find_prog(name: str) -> Tuple[Optional[str], ...]:
|
|
|
|
def attempt(path: str) -> Optional[str]:
|
|
|
|
if os.path.exists(path):
|
|
|
|
return path
|
|
|
|
return None
|
|
|
|
|
2024-01-12 21:30:48 +01:00
|
|
|
return (attempt(f"auto_tests/auto_{name}_test"),
|
|
|
|
) # attempt(f"./unit_{name}_test"),
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_flags(args: List[str]) -> Tuple[Dict[str, str], List[str]]:
|
|
|
|
flags: Dict[str, str] = {}
|
|
|
|
exes: List[str] = []
|
|
|
|
for arg in args:
|
|
|
|
if arg.startswith("--"):
|
|
|
|
flag, value = arg.split("=", 1)
|
|
|
|
flags[flag] = value
|
|
|
|
else:
|
|
|
|
exes.append(arg)
|
|
|
|
return flags, exes
|
|
|
|
|
|
|
|
|
2023-12-27 12:37:22 +01:00
|
|
|
def loop_mallocfail(tmpdir: str,
|
|
|
|
timeout: float,
|
|
|
|
exe: str,
|
|
|
|
keep_going: bool = False) -> None:
|
2023-07-25 11:53:09 +02:00
|
|
|
i = 1
|
2023-12-27 12:37:22 +01:00
|
|
|
while run_mallocfail(tmpdir, timeout, exe, i, keep_going):
|
2023-07-25 11:53:09 +02:00
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
def isolated_mallocfail(timeout: int, exe: str) -> None:
|
|
|
|
with tempfile.TemporaryDirectory(prefix="mallocfail") as tmpdir:
|
|
|
|
print(f"\x1b[1;33mRunning for {exe} in isolated path {tmpdir}\x1b[0m")
|
2023-12-27 12:37:22 +01:00
|
|
|
os.mkdir(os.path.join(tmpdir, "auto_tests"))
|
2023-07-25 11:53:09 +02:00
|
|
|
shutil.copy(exe, os.path.join(tmpdir, exe))
|
|
|
|
shutil.copy(_HASHES, os.path.join(tmpdir, _HASHES))
|
|
|
|
loop_mallocfail(tmpdir, timeout, exe)
|
2024-01-12 21:30:48 +01:00
|
|
|
profraw = os.path.join(tmpdir, "default.profraw")
|
|
|
|
if os.path.exists(profraw):
|
|
|
|
shutil.copy(profraw, exe + ".mallocfail.profraw")
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main(args: List[str]) -> None:
|
|
|
|
"""Run a program repeatedly under mallocfail."""
|
|
|
|
if len(args) == 1:
|
|
|
|
print(f"Usage: {args[0]} <exe>")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
flags, exes = parse_flags(args[1:])
|
|
|
|
|
|
|
|
timeout = _TIMEOUT
|
|
|
|
if "--ctest" in flags:
|
|
|
|
costs = ctest_costs()
|
|
|
|
max_cost = float(flags["--ctest"])
|
|
|
|
timeout = max(max_cost + 1, timeout)
|
|
|
|
exes.extend(prog for test in costs.keys() for prog in find_prog(test)
|
|
|
|
if costs[test] <= max_cost and prog)
|
|
|
|
if "--jobs" in flags:
|
|
|
|
jobs = int(flags["--jobs"])
|
|
|
|
else:
|
|
|
|
jobs = 1
|
|
|
|
|
2024-01-12 21:30:48 +01:00
|
|
|
# Start by running version_test, which allocates no memory of its own, just
|
2023-07-25 11:53:09 +02:00
|
|
|
# to prime the mallocfail hashes so it doesn't make global initialisers
|
|
|
|
# such as llvm_gcov_init fail.
|
|
|
|
if os.path.exists(_PRIMER):
|
2024-01-12 21:30:48 +01:00
|
|
|
print(f"\x1b[1;33mPriming hashes with {_PRIMER}\x1b[0m")
|
|
|
|
loop_mallocfail(os.getcwd(), timeout, _PRIMER, keep_going=True)
|
2023-07-25 11:53:09 +02:00
|
|
|
|
|
|
|
print(f"\x1b[1;33m--------------------------------\x1b[0m")
|
|
|
|
print(f"\x1b[1;33mStarting mallocfail for {len(exes)} programs:\x1b[0m")
|
|
|
|
print(f"\x1b[1;33m{exes}\x1b[0m")
|
|
|
|
print(f"\x1b[1;33m--------------------------------\x1b[0m")
|
|
|
|
time.sleep(1)
|
|
|
|
with multiprocessing.Pool(jobs) as p:
|
|
|
|
done = tuple(
|
|
|
|
p.starmap(isolated_mallocfail, ((timeout, exe) for exe in exes)))
|
|
|
|
print(f"\x1b[1;32mCompleted {len(done)} programs\x1b[0m")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|