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