Skip to content

[build-script] Add sanitizer flags #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 65 additions & 23 deletions Utilities/build-script-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def get_swiftpm_options(args):
if args.verbose:
swiftpm_args += ['--verbose']

if args.sanitize:
for san in args.sanitize:
swiftpm_args += ['--sanitize=%s' % san]

if platform.system() == 'Darwin':
swiftpm_args += [
# Relative library rpath for swift; will only be used when /usr/lib/swift
Expand Down Expand Up @@ -81,6 +85,44 @@ def delete_rpath(rpath, binary):
print(' '.join(cmd))
subprocess.check_call(cmd)


def handle_invocation(swift_exec, args):
swiftpm_args = get_swiftpm_options(args)

env = os.environ
# Set the toolchain used in tests at runtime
env['SOURCEKIT_TOOLCHAIN_PATH'] = args.toolchain
# Use local dependencies (i.e. checked out next sourcekit-lsp).
if not args.no_local_deps:
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"

if args.ninja_bin:
env['NINJA_BIN'] = args.ninja_bin

if args.sanitize and 'address' in args.sanitize:
# Workaround reports in Foundation: https://bugs.swift.org/browse/SR-12551
env['ASAN_OPTIONS'] = 'detect_leaks=false'
if args.sanitize and 'undefined' in args.sanitize:
supp = os.path.join(args.package_path, 'Utilities', 'ubsan_supressions.supp')
env['UBSAN_OPTIONS'] = 'halt_on_error=true,suppressions=%s' % supp


if args.action == 'build':
swiftpm('build', swift_exec, swiftpm_args, env)
elif args.action == 'test':
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
tests = os.path.join(bin_path, 'sk-tests')
print('Cleaning ' + tests)
shutil.rmtree(tests, ignore_errors=True)
swiftpm('test', swift_exec, swiftpm_args + ['--parallel'], env)
elif args.action == 'install':
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
swiftpm('build', swift_exec, swiftpm_args, env)
install(bin_path, args.toolchain)
else:
assert False, 'unknown action \'{}\''.format(args.action)


def main():
parser = argparse.ArgumentParser(description='Build along with the Swift build-script.')
def add_common_args(parser):
Expand All @@ -90,6 +132,8 @@ def add_common_args(parser):
parser.add_argument('--build-path', metavar='PATH', default='.build', help='build in the given path')
parser.add_argument('--configuration', '-c', default='debug', help='build using configuration (release|debug)')
parser.add_argument('--no-local-deps', action='store_true', help='use normal remote dependencies when building')
parser.add_argument('--sanitize', action='append', help='build using the given sanitizer(s) (address|thread|undefined)')
parser.add_argument('--sanitize-all', action='store_true', help='build using every available sanitizer in sub-directories of build path')
parser.add_argument('--verbose', '-v', action='store_true', help='enable verbose output')

subparsers = parser.add_subparsers(title='subcommands', dest='action', metavar='action')
Expand All @@ -104,6 +148,9 @@ def add_common_args(parser):

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

if args.sanitize and args.sanitize_all:
assert False, 'cannot combine --sanitize with --sanitize-all'

# Canonicalize paths
args.package_path = os.path.abspath(args.package_path)
args.build_path = os.path.abspath(args.build_path)
Expand All @@ -114,32 +161,27 @@ def add_common_args(parser):
else:
swift_exec = 'swift'

swiftpm_args = get_swiftpm_options(args)
handle_invocation(swift_exec, args)
if args.sanitize_all:
base = args.build_path

env = os.environ
# Set the toolchain used in tests at runtime
env['SOURCEKIT_TOOLCHAIN_PATH'] = args.toolchain
# Use local dependencies (i.e. checked out next sourcekit-lsp).
if not args.no_local_deps:
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"
print('=== %s sourcekit-lsp with asan ===' % args.action)
args.sanitize = ['address']
args.build_path = os.path.join(base, 'test-asan')
handle_invocation(swift_exec, args)

if args.ninja_bin:
env['NINJA_BIN'] = args.ninja_bin
print('=== %s sourcekit-lsp with tsan ===' % args.action)
args.sanitize = ['thread']
args.build_path = os.path.join(base, 'test-tsan')
handle_invocation(swift_exec, args)

# Linux ubsan disabled: https://bugs.swift.org/browse/SR-12550
if platform.system() != 'Linux':
print('=== %s sourcekit-lsp with ubsan ===' % args.action)
args.sanitize = ['undefined']
args.build_path = os.path.join(base, 'test-ubsan')
handle_invocation(swift_exec, args)

if args.action == 'build':
swiftpm('build', swift_exec, swiftpm_args, env)
elif args.action == 'test':
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
tests = os.path.join(bin_path, 'sk-tests')
print('Cleaning ' + tests)
shutil.rmtree(tests, ignore_errors=True)
swiftpm('test', swift_exec, swiftpm_args + ['--parallel'], env)
elif args.action == 'install':
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
swiftpm('build', swift_exec, swiftpm_args, env)
install(bin_path, args.toolchain)
else:
assert False, 'unknown action \'{}\''.format(args.action)

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions Utilities/ubsan_supressions.supp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alignment:mdb.c