-
Notifications
You must be signed in to change notification settings - Fork 2
/
python3-asan
executable file
·52 lines (45 loc) · 1.18 KB
/
python3-asan
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
#!/usr/bin/env python3
from argparse import ArgumentParser
import os
import subprocess
def main():
parser = ArgumentParser()
parser.add_argument("-m")
args, rest = parser.parse_known_args()
script = """\
from os import environ
del environ["LD_PRELOAD"]
"""
if args.m:
script += f"""\
import {args.m}.__main__
"""
else:
script += """\
import sys
assert sys.argv[0] == "-c"
del sys.argv[0]
with open(sys.argv[0]) as fp:
exec(compile(fp.read(), sys.argv[0], "exec"))
"""
cc = os.environ.get("CC", "cc")
# https://github.com/google/sanitizers/issues/934
libstdcxx = (
subprocess.check_output([cc, "-print-file-name=libstdc++.so"]).strip().decode()
)
libasan = (
subprocess.check_output([cc, "-print-file-name=libasan.so"]).strip().decode()
)
# https://stackoverflow.com/questions/77894856
argv = ["setarch", "--addr-no-randomize", "python3", "-c", script, *rest]
os.execvpe(
argv[0],
argv,
{
**os.environ,
"ASAN_OPTIONS": "detect_leaks=0",
"LD_PRELOAD": os.pathsep.join((libasan, libstdcxx)),
},
)
if __name__ == "__main__":
main()