Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: scripts/slave/chromium/win_apply_asan.py

Issue 11379003: Add Windows ASAN bots. (Closed) Base URL: http://git.chromium.org/chromium/tools/build.git@neuter
Patch Set: Rebase, tweaks, lint Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import multiprocessing
7 import optparse
8 import os
9 import shutil
10 import subprocess
11 import sys
12
13
14 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
15 BLACKLIST = set((
16 'sql.dll',
17 'gpu.dll',
18 'crnss.dll',
19 'icuuc.dll'
20 ))
21
22
23 ### Multiprocessing functions
24 OPTIONS = None
25 STOPPED = None
26 def _InitializeASANitizer(options, stopped):
27 global OPTIONS, STOPPED
28 OPTIONS = options
29 STOPPED = stopped
30
31
32 def _ASANitize(job):
33 retval = 0
34 stdout = ''
35 pe_image, pdb = job
36
37 try:
38 if not STOPPED.is_set():
39 out_pe = AddExtensionComponent(pe_image, 'asan')
40 out_pdb = AddExtensionComponent(pdb, 'asan')
41
42 # Note that instrument.exe requires --foo=bar format (including the '=')
43 command = [OPTIONS.instrument_exe, '--mode=ASAN',
44 '--input-image=%s' % pe_image,
45 '--input-pdb=%s' % pdb,
Roger McFarlane (Chromium) 2012/11/21 03:59:30 If the input PDB file can be expected to reside in
iannucci 2012/11/21 06:32:06 Done. Yeah... though I'm relying on the naming co
46 '--output-image=%s' % out_pe,
47 '--output-pdb=%s' % out_pdb,
48 '2>&1' # Combine stderr+stdout so that they're in order
49 ]
50
51 for fname in filter(os.path.exists, (out_pe, out_pdb)):
52 os.remove(fname)
53
54 proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
55 stdout, _ = proc.communicate()
56 retval = proc.returncode
57
58 return (retval, stdout, pe_image)
59 except Exception:
60 import traceback
61 return (1, stdout+'\n'+traceback.format_exc(), pe_image)
62
63
64 ### Normal functions
65
66 def AddExtensionComponent(path, new_ext):
67 """
68 Prepends new_ext to the existing extension
69 >>> ChangeExtension('hello.foo.dll', 'asan')
70 'hello.asan.foo.dll'
71 """
72 # Don't use os.path.splitext, because it will split on the rightmost dot
73 # instead of the leftmost dot.
74 base, ext = path.split('.', 1)
75 return base+'.'+new_ext+'.'+ext
76
77
78 def UpdateAsanRuntime(options):
79 """Updates the ASAN runtime dll in the build directory, if it exists."""
80 runtime = os.path.join(options.full_directory,
81 os.path.basename(options.runtime_path))
82
83 if os.path.exists(runtime):
84 print('Removing', runtime)
85 os.remove(runtime)
86
87 print 'Copying %s -> %s' % (options.runtime_path, runtime)
88 shutil.copy2(options.runtime_path, runtime)
89
90 fname = os.path.basename(options.runtime_path)
91 print 'Blacklisting %s' % fname
92 BLACKLIST.add(fname)
93
94
95 def GetCompatiblePDB(pe_image):
96 """Returns <path to pdb> or None (if no good pdb exists)."""
97 # TODO(iannucci): Use PE header to look up pdb name.
98 # for now, assume that the pdb is always just PE.pdb
99 pdb_path = pe_image+'.pdb'
100 return pdb_path if os.path.exists(pdb_path) else None
101
102
103 def IsAlreadyASAN(pe_image, pdb): # pylint: disable=W0613
104 """Returns True iff pe_image already has ASAN applied to it."""
105 # TODO(iannucci): Implement me!
106 return False
107
108
109 def FindFilesToAsan(directory):
110 """Finds eligible PE images in given directory. A PE image is eligible if it
111 has a corresponding pdb and doesn't already have ASAN applied to it. Skips
112 files which have an extra extension (like foo.orig.exe)."""
113 ret = []
114
115 def GoodExeOrDll(fname):
116 return (
117 '.' in fname and
118 fname not in BLACKLIST and
119 fname.split('.', 1)[-1] in ('exe', 'dll'))
120
121 for root, _, files in os.walk(directory):
122 for pe_image in (os.path.join(root, f) for f in files if GoodExeOrDll(f)):
123 pdb = GetCompatiblePDB(pe_image)
124 if not pdb:
125 print >> sys.stderr, 'PDB for "%s" does not exist.' % pe_image
126 continue
127
128 if IsAlreadyASAN(pe_image, pdb):
Roger McFarlane (Chromium) 2012/11/21 03:59:30 With your naming convention (foo.asan.dll) excludi
iannucci 2012/11/21 06:32:06 Yeah... this block made more sense when I was rena
129 print >> sys.stderr, \
130 '"%s" is already ASANed and is possibly a stale build product.' \
131 % pe_image
132 continue
133
134 ret.append((pe_image, pdb))
135 return ret
136
137
138 def ApplyAsanToBuild(options):
139 """Applies ASAN to all exe's/dll's in the build directory."""
140 to_asan = FindFilesToAsan(options.full_directory)
141
142 if not to_asan:
143 print >> sys.stderr, 'No files to ASAN!'
144 return 1
145
146 stopped = multiprocessing.Event()
147 pool = multiprocessing.Pool(options.jobs, initializer=_InitializeASANitizer,
148 initargs=(options, stopped))
149
150 ret = 0
151 try:
152 generator = pool.imap_unordered(_ASANitize, to_asan)
153 for retval, stdout, failed_image in generator:
154 ostream = (sys.stderr if retval else sys.stdout)
155 print >> ostream, stdout
156 sys.stdout.flush()
157 sys.stderr.flush()
158 if retval:
159 print 'Failed to ASAN %s. Stopping remaining jobs.' % failed_image
160 ret = retval
161 stopped.set()
162 except KeyboardInterrupt:
163 stopped.set()
164 pool.close()
165 pool.join()
166
167 return ret
168
169
170 def main():
171 default_asan_dir = os.path.join(os.pardir,
172 'third_party', 'syzygy', 'binaries', 'exe')
173 default_instrument_exe = os.path.join(default_asan_dir, 'instrument.exe')
174 default_runtime_path = os.path.join(default_asan_dir, 'asan_rtl.dll')
175
176 parser = optparse.OptionParser()
177 parser.add_option('--build_directory',
178 help='Path to the build directory to asan (required).')
179 parser.add_option('--target',
180 help='The target in the build directory to asan (required).')
181 parser.add_option('--jobs', type='int', default=multiprocessing.cpu_count(),
182 help='Specify the number of sub-tasks to use (%default).')
183 parser.add_option('--instrument_exe', default=default_instrument_exe,
184 help='Specify the path to the ASAN instrument.exe relative to '
185 'build_directory (%default).')
186 parser.add_option('--runtime_path', default=default_runtime_path,
187 help='Specify the path to the ASAN runtime DLL relative to '
188 'build_directory (%default).')
189 options, args = parser.parse_args()
190
191 if options.build_directory is None:
192 parser.error('Must specify --build_directory')
193
194 if options.target is None:
195 parser.error('Must specify --target')
196
197 options.full_directory = os.path.join(options.build_directory, options.target)
198 if not os.path.exists(options.full_directory):
199 parser.error('Could not find directory: %s' % options.full_directory)
200
201 options.instrument_exe = os.path.abspath(
202 os.path.join(options.build_directory, options.instrument_exe))
203 if not os.path.exists(options.instrument_exe):
204 parser.error('Could not find instrument_exe: %s' % options.instrument_exe)
205
206 options.runtime_path = os.path.abspath(
207 os.path.join(options.build_directory, options.runtime_path))
208 if not os.path.exists(options.runtime_path):
209 parser.error('Could not find runtime_path: %s' % options.runtime_path)
210
211 if args:
212 parser.error('Not expecting additional arguments')
213
214 print 'Default BLACKLIST is: %r' % BLACKLIST
215
216 UpdateAsanRuntime(options)
217 return ApplyAsanToBuild(options)
218
219 if __name__ == '__main__':
220 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698