Chromium Code Reviews| Index: pydir/sz_driver.py |
| diff --git a/pydir/sz_driver.py b/pydir/sz_driver.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7475b3bb070b56fa04fe744712281726552d2db0 |
| --- /dev/null |
| +++ b/pydir/sz_driver.py |
| @@ -0,0 +1,88 @@ |
| +import os |
| +import shutil |
| +import subprocess |
| +import sys |
| +import tempfile |
| + |
| +from utils import FindBaseNaCl, shellcmd |
| + |
| +def subsToMacros(subs, src): |
| + 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
|
| + '#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.
|
| + '#ifdef __cplusplus\n' |
| + 'extern "C" {\n' |
| + '#endif\n') |
| + for func in subs: |
| + args = [('{atype} a{num}').format(atype=atype, num=i) for |
| + i, atype in enumerate(subs[func]['sig'][1:])] |
| + macros += ( |
| + '{ftype} {name}({args});\n' |
| + ).format(ftype=subs[func]['sig'][0], |
| + name=subs[func]['sub'], |
| + args=', '.join(args)) |
| + macros += ( |
| + '#define {func}(args...) ({sub}(args))\n' |
| + ).format(func=func, sub=subs[func]['sub']) |
| + macros += ( |
| + '#ifdef __cplusplus\n' |
| + '}} // extern "C"\n' |
| + '#endif\n' |
| + '#line 1 "{src}"\n').format(src=src) |
| + return macros |
| + |
| +def run(is_cpp): |
| + """Passes its arguments directly to pnacl-clang. |
| + |
| + If -fsanitize-address is specified, extra information is passed to |
| + pnacl-clang to ensure that later instrumentation in pnacl-sz can be |
| + performed. For example, clang automatically inlines many memory allocation |
| + functions, so this script will redefine them at compile time to make sure |
| + they can be correctly instrumented by pnacl-sz. |
| + """ |
| + pnacl_root = FindBaseNaCl() |
| + dummy_subs = {'calloc': {'sig': ['void *', 'size_t', 'size_t'], |
| + 'sub': '__asan_dummy_calloc'}, |
| + '_calloc': {'sig': ['void *', 'size_t', 'size_t'], |
| + 'sub': '__asan_dummy_calloc'}} |
| + subs_src = ( |
| + '{root}/toolchain_build/src/subzero/pydir/sz_clang_dummies.c' |
| + ).format(root=pnacl_root) |
| + clang = ( |
| + '{root}/toolchain/linux_x86/pnacl_newlib_raw/bin/pnacl-clang{pp}' |
| + ).format(root=pnacl_root, pp='++' if is_cpp else '') |
| + args = sys.argv |
| + args[0] = clang |
| + tmp_dir = '' |
| + if '-fsanitize-address' in args: |
| + args.remove('-fsanitize-address') |
| + include_dirs = set() |
| + tmp_dir = tempfile.mkdtemp() |
| + for i, arg in enumerate(args[1:], 1): |
| + if not os.path.isfile(arg): |
| + continue |
| + src = os.path.basename(arg) |
| + ext = os.path.splitext(arg)[1] |
| + if ext in ['.c', '.cc', '.cpp']: |
| + include_dirs |= {os.path.dirname(arg)} |
| + dest_name = os.path.join(tmp_dir, src) |
| + with open(dest_name, 'w') as dest: |
| + dest.write(subsToMacros(dummy_subs, arg)) |
| + with open(arg) as src: |
| + for line in src: |
| + dest.write(line) |
| + args[i] = dest_name |
| + # If linking (not single file compilation) then add dummy definitions |
| + if not ('-o' in args and |
| + ('-c' in args or '-S' in args or '-E' in args)): |
| + args.append(subs_src) |
| + for d in include_dirs: |
| + args.append('-iquote {d}'.format(d=d)) |
| + err_code = 0 |
| + try: |
| + shellcmd(args, echo=True) |
| + except subprocess.CalledProcessError as e: |
| + print e.output |
| + err_code = e.returncode |
| + if tmp_dir != '': |
| + shutil.rmtree(tmp_dir) |
| + exit(err_code) |