OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python2 | |
2 | |
3 import os | |
4 import shutil | |
5 import subprocess | |
6 import sys | |
7 import tempfile | |
8 | |
9 from utils import FindBaseNaCl, shellcmd | |
10 | |
11 def subsToMacros(subs): | |
12 macros = '#include <stddef.h>\n' | |
13 for func in subs: | |
14 args = [('{atype} a{num}').format(atype=atype, num=i) for | |
15 i, atype in enumerate(subs[func]['sig'][1:])] | |
16 macros += ( | |
17 '{ftype} {name}({args});\n' | |
18 ).format(ftype=subs[func]['sig'][0], | |
19 name=subs[func]['sub'], | |
20 args=', '.join(args)) | |
21 macros += ( | |
22 '#define {func}(args...) ({sub}(args))\n' | |
23 ).format(func=func, sub=subs[func]['sub']) | |
24 return macros | |
25 | |
26 def main(): | |
27 """Passes its arguments directly to pnacl-clang. | |
28 | |
29 If -fsanitize-address is specified, extra information is passed to | |
30 pnacl-clang to ensure that later instrumentation in pnacl-sz can be | |
31 performed. For example, clang automatically inlines many memory allocation | |
32 functions, so this script will redefine them at compile time to make sure | |
33 they can be correctly instrumented by pnacl-sz. | |
34 """ | |
35 pnacl_root = FindBaseNaCl() | |
36 dummy_subs = {'calloc': {'sig': ['void *', 'size_t', 'size_t'], | |
37 'sub': '__asan_dummy_calloc'}, | |
38 '_calloc': {'sig': ['void *', 'size_t', 'size_t'], | |
39 'sub': '__asan_dummy_calloc'}} | |
40 subs_src = ( | |
41 '{root}/toolchain_build/src/subzero/pydir/sz_clang_dummies.c' | |
42 ).format(root=pnacl_root) | |
43 clang = ( | |
44 '{root}/toolchain/linux_x86/pnacl_newlib_raw/bin/pnacl-clang' | |
45 ).format(root=pnacl_root) | |
46 args = sys.argv | |
47 args[0] = clang | |
48 tmp_dir = '' | |
49 if '-fsanitize-address' in args: | |
50 args.remove('-fsanitize-address') | |
51 include_dirs = set() | |
52 tmp_dir = tempfile.mkdtemp() | |
53 for i, arg in enumerate(args[1:], 1): | |
54 if not os.path.isfile(arg): | |
55 continue | |
56 src = os.path.basename(arg) | |
57 ext = os.path.splitext(arg)[1] | |
58 if ext in ['.c', '.cc', '.cpp']: | |
59 include_dirs |= {os.path.dirname(arg)} | |
60 dest_name = os.path.join(tmp_dir, src) | |
61 with open(dest_name, 'w') as dest: | |
62 dest.write(subsToMacros(dummy_subs)) | |
63 dest.write('#line 1 "{file}"\n'.format(file=arg)) | |
64 with open(arg) as src: | |
65 for line in src: | |
66 dest.write(line) | |
67 args[i] = dest_name | |
Karl
2016/07/14 17:33:50
Ok. I now understand the following code. I suggest
tlively
2016/07/14 20:26:14
Done.
| |
68 if not ('-o' in args and | |
69 ('-c' in args or '-S' in args or '-E' in args)): | |
70 args.append(subs_src) | |
71 for d in include_dirs: | |
72 args.append('-iquote {d}'.format(d=d)) | |
73 err_code = 0 | |
74 try: | |
75 shellcmd(args, echo=True) | |
76 except subprocess.CalledProcessError as e: | |
77 print e.output | |
78 err_code = e.returncode | |
79 if tmp_dir != '': | |
80 shutil.rmtree(tmp_dir) | |
81 exit(err_code) | |
82 | |
83 if __name__ == '__main__': | |
84 main() | |
OLD | NEW |