Skip to content

Commit f008618

Browse files
authored
Merge pull request #82 from benlangmuir/sanitize
[build-script] Add --sanitize option
2 parents 7fa2811 + d870914 commit f008618

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

Utilities/build-script-helper.py

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def get_swiftpm_options(args):
2929
if args.verbose:
3030
swiftpm_args += ['--verbose']
3131

32+
if args.sanitize:
33+
for san in args.sanitize:
34+
swiftpm_args += ['--sanitize=%s' % san]
35+
3236
if platform.system() != 'Darwin':
3337
swiftpm_args += [
3438
# Dispatch headers
@@ -41,6 +45,36 @@ def get_swiftpm_options(args):
4145

4246
return swiftpm_args
4347

48+
49+
def handle_invocation(swift_exec, args):
50+
swiftpm_args = get_swiftpm_options(args)
51+
52+
env = os.environ
53+
# Set the toolchain used in tests at runtime
54+
env['INDEXSTOREDB_TOOLCHAIN_PATH'] = args.toolchain
55+
56+
if args.ninja_bin:
57+
env['NINJA_BIN'] = args.ninja_bin
58+
59+
if args.sanitize and 'address' in args.sanitize:
60+
# Workaround reports in Foundation: https://bugs.swift.org/browse/SR-12551
61+
env['ASAN_OPTIONS'] = 'detect_leaks=false'
62+
if args.sanitize and 'undefined' in args.sanitize:
63+
supp = os.path.join(args.package_path, 'Utilities', 'ubsan_supressions.supp')
64+
env['UBSAN_OPTIONS'] = 'halt_on_error=true,suppressions=%s' % supp
65+
66+
if args.action == 'build':
67+
swiftpm('build', swift_exec, swiftpm_args, env)
68+
elif args.action == 'test':
69+
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
70+
tests = os.path.join(bin_path, 'isdb-tests')
71+
print('Cleaning ' + tests)
72+
shutil.rmtree(tests, ignore_errors=True)
73+
swiftpm('test', swift_exec, swiftpm_args + ['--parallel'], env)
74+
else:
75+
assert False, 'unknown action \'{}\''.format(args.action)
76+
77+
4478
def main():
4579
parser = argparse.ArgumentParser(description='Build along with the Swift build-script.')
4680
def add_common_args(parser):
@@ -49,6 +83,8 @@ def add_common_args(parser):
4983
parser.add_argument('--ninja-bin', metavar='PATH', help='ninja binary to use for testing')
5084
parser.add_argument('--build-path', metavar='PATH', default='.build', help='build in the given path')
5185
parser.add_argument('--configuration', '-c', default='debug', help='build using configuration (release|debug)')
86+
parser.add_argument('--sanitize', action='append', help='build using the given sanitizer(s) (address|thread|undefined)')
87+
parser.add_argument('--sanitize-all', action='store_true', help='build using every available sanitizer in sub-directories of build path')
5288
parser.add_argument('--verbose', '-v', action='store_true', help='enable verbose output')
5389

5490
subparsers = parser.add_subparsers(title='subcommands', dest='action', metavar='action')
@@ -60,6 +96,9 @@ def add_common_args(parser):
6096

6197
args = parser.parse_args(sys.argv[1:])
6298

99+
if args.sanitize and args.sanitize_all:
100+
assert False, 'cannot combine --sanitize with --sanitize-all'
101+
63102
# Canonicalize paths
64103
args.package_path = os.path.abspath(args.package_path)
65104
args.build_path = os.path.abspath(args.build_path)
@@ -70,25 +109,28 @@ def add_common_args(parser):
70109
else:
71110
swift_exec = 'swift'
72111

73-
swiftpm_args = get_swiftpm_options(args)
112+
handle_invocation(swift_exec, args)
74113

75-
env = os.environ
76-
# Set the toolchain used in tests at runtime
77-
env['INDEXSTOREDB_TOOLCHAIN_PATH'] = args.toolchain
114+
if args.sanitize_all:
115+
base = args.build_path
78116

79-
if args.ninja_bin:
80-
env['NINJA_BIN'] = args.ninja_bin
117+
print('=== %s indexstore-db with asan ===' % args.action)
118+
args.sanitize = ['address']
119+
args.build_path = os.path.join(base, 'test-asan')
120+
handle_invocation(swift_exec, args)
121+
122+
print('=== %s indexstore-db with tsan ===' % args.action)
123+
args.sanitize = ['thread']
124+
args.build_path = os.path.join(base, 'test-tsan')
125+
handle_invocation(swift_exec, args)
126+
127+
# Linux ubsan disabled: https://bugs.swift.org/browse/SR-12550
128+
if platform.system() != 'Linux':
129+
print('=== %s indexstore-db with ubsan ===' % args.action)
130+
args.sanitize = ['undefined']
131+
args.build_path = os.path.join(base, 'test-ubsan')
132+
handle_invocation(swift_exec, args)
81133

82-
if args.action == 'build':
83-
swiftpm('build', swift_exec, swiftpm_args, env)
84-
elif args.action == 'test':
85-
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
86-
tests = os.path.join(bin_path, 'isdb-tests')
87-
print('Cleaning ' + tests)
88-
shutil.rmtree(tests, ignore_errors=True)
89-
swiftpm('test', swift_exec, swiftpm_args + ['--parallel'], env)
90-
else:
91-
assert False, 'unknown action \'{}\''.format(args.action)
92134

93135
if __name__ == '__main__':
94136
main()

Utilities/ubsan_supressions.supp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alignment:mdb.c

0 commit comments

Comments
 (0)