2017-11-21 17:47:51 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2017 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import argparse
|
2017-11-27 21:24:43 +00:00
|
|
|
import imp
|
2017-11-21 17:47:51 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
|
2017-11-27 21:24:43 +00:00
|
|
|
from packages import package
|
|
|
|
|
2017-11-22 21:13:30 +00:00
|
|
|
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
2017-11-27 21:24:43 +00:00
|
|
|
PACKAGES_DIR = os.path.join(SCRIPT_DIR, 'packages')
|
2017-11-22 21:13:30 +00:00
|
|
|
INJECTED_ARGS = [
|
|
|
|
'-fsanitize=memory',
|
|
|
|
'-fsanitize-memory-track-origins=2',
|
|
|
|
'-fsanitize-recover=memory',
|
|
|
|
'-fPIC',
|
|
|
|
'-fno-omit-frame-pointer',
|
|
|
|
]
|
|
|
|
|
2017-11-21 17:47:51 +00:00
|
|
|
|
|
|
|
class MSanBuildException(Exception):
|
|
|
|
"""Base exception."""
|
|
|
|
|
|
|
|
|
|
|
|
def SetUpEnvironment(work_dir):
|
|
|
|
"""Set up build environment."""
|
|
|
|
env = {}
|
|
|
|
env['REAL_CLANG_PATH'] = subprocess.check_output(['which', 'clang']).strip()
|
|
|
|
print('Real clang at', env['REAL_CLANG_PATH'])
|
|
|
|
compiler_wrapper_path = os.path.join(SCRIPT_DIR, 'compiler_wrapper.py')
|
|
|
|
|
|
|
|
# Symlink binaries into TMP/bin
|
|
|
|
bin_dir = os.path.join(work_dir, 'bin')
|
|
|
|
os.mkdir(bin_dir)
|
|
|
|
|
|
|
|
os.symlink(compiler_wrapper_path,
|
|
|
|
os.path.join(bin_dir, 'clang'))
|
|
|
|
|
|
|
|
os.symlink(compiler_wrapper_path,
|
|
|
|
os.path.join(bin_dir, 'clang++'))
|
|
|
|
|
|
|
|
env['CC'] = os.path.join(bin_dir, 'clang')
|
|
|
|
env['CXX'] = os.path.join(bin_dir, 'clang++')
|
|
|
|
|
|
|
|
# Not all build rules respect $CC/$CXX, so make additional symlinks.
|
|
|
|
dpkg_host_architecture = subprocess.check_output(
|
|
|
|
['dpkg-architecture', '-qDEB_HOST_GNU_TYPE']).strip()
|
|
|
|
os.symlink(compiler_wrapper_path,
|
|
|
|
os.path.join(bin_dir, dpkg_host_architecture + '-gcc'))
|
|
|
|
os.symlink(compiler_wrapper_path,
|
|
|
|
os.path.join(bin_dir, dpkg_host_architecture + '-g++'))
|
|
|
|
|
|
|
|
os.symlink(compiler_wrapper_path, os.path.join(bin_dir, 'gcc'))
|
|
|
|
os.symlink(compiler_wrapper_path, os.path.join(bin_dir, 'cc'))
|
|
|
|
os.symlink(compiler_wrapper_path, os.path.join(bin_dir, 'g++'))
|
|
|
|
os.symlink(compiler_wrapper_path, os.path.join(bin_dir, 'c++'))
|
|
|
|
|
2017-11-22 21:13:30 +00:00
|
|
|
MSAN_OPTIONS = ' '.join(INJECTED_ARGS)
|
2017-11-21 17:47:51 +00:00
|
|
|
|
2017-11-22 22:26:44 +00:00
|
|
|
env['DEB_BUILD_OPTIONS'] = 'nocheck nostrip'
|
2017-11-21 17:47:51 +00:00
|
|
|
env['DEB_CFLAGS_APPEND'] = MSAN_OPTIONS
|
|
|
|
env['DEB_CXXFLAGS_APPEND'] = MSAN_OPTIONS + ' -stdlib=libc++'
|
|
|
|
env['DEB_CPPFLAGS_APPEND'] = env['DEB_CXXFLAGS_APPEND']
|
|
|
|
env['DEB_LDFLAGS_APPEND'] = MSAN_OPTIONS
|
|
|
|
env['DPKG_GENSYMBOLS_CHECK_LEVEL'] = '0'
|
|
|
|
|
2017-11-22 22:01:06 +00:00
|
|
|
# debian/rules can set DPKG_GENSYMBOLS_CHECK_LEVEL explicitly, so override it.
|
|
|
|
dpkg_gensymbols_path = os.path.join(bin_dir, 'dpkg-gensymbols')
|
|
|
|
with open(dpkg_gensymbols_path, 'w') as f:
|
|
|
|
f.write(
|
|
|
|
'#!/bin/sh\n'
|
|
|
|
'export DPKG_GENSYMBOLS_CHECK_LEVEL=0\n'
|
|
|
|
'/usr/bin/dpkg-gensymbols "$@"\n')
|
|
|
|
|
|
|
|
os.chmod(dpkg_gensymbols_path, 0755)
|
|
|
|
|
2017-11-21 17:47:51 +00:00
|
|
|
env['PATH'] = bin_dir + ':' + os.environ['PATH']
|
|
|
|
|
|
|
|
# Prevent entire build from failing because of bugs/uninstrumented in tools
|
|
|
|
# that are part of the build.
|
|
|
|
# TODO(ochang): Figure out some way to suppress reports since they can still
|
|
|
|
# be very noisy.
|
|
|
|
env['MSAN_OPTIONS'] = 'halt_on_error=0:exitcode=0'
|
|
|
|
return env
|
|
|
|
|
|
|
|
|
2017-12-04 20:35:43 +00:00
|
|
|
def FindPackageDebs(package_name, work_directory):
|
|
|
|
"""Find package debs."""
|
|
|
|
deb_paths = []
|
2017-11-21 17:47:51 +00:00
|
|
|
|
|
|
|
for filename in os.listdir(work_directory):
|
|
|
|
file_path = os.path.join(work_directory, filename)
|
|
|
|
if not file_path.endswith('.deb'):
|
|
|
|
continue
|
|
|
|
|
2017-12-04 20:35:43 +00:00
|
|
|
if filename.startswith(package_name + '_'):
|
|
|
|
deb_paths.append(file_path)
|
|
|
|
|
|
|
|
return deb_paths
|
|
|
|
|
|
|
|
|
|
|
|
def ExtractSharedLibraries(deb_paths, work_directory, output_directory):
|
|
|
|
"""Extract shared libraries from .deb packages."""
|
|
|
|
extract_directory = os.path.join(work_directory, 'extracted')
|
|
|
|
if os.path.exists(extract_directory):
|
|
|
|
shutil.rmtree(extract_directory, ignore_errors=True)
|
|
|
|
|
|
|
|
os.mkdir(extract_directory)
|
|
|
|
|
|
|
|
for deb_path in deb_paths:
|
|
|
|
subprocess.check_call(['dpkg-deb', '-x', deb_path, extract_directory])
|
2017-11-21 17:47:51 +00:00
|
|
|
|
2017-11-27 23:03:20 +00:00
|
|
|
extracted = []
|
2017-11-21 17:47:51 +00:00
|
|
|
for root, _, filenames in os.walk(extract_directory):
|
2017-11-27 23:03:20 +00:00
|
|
|
if 'libx32' in root or 'lib32' in root:
|
|
|
|
continue
|
|
|
|
|
2017-11-21 17:47:51 +00:00
|
|
|
for filename in filenames:
|
2017-11-27 23:03:20 +00:00
|
|
|
if not filename.endswith('.so') and '.so.' not in filename:
|
|
|
|
continue
|
|
|
|
|
2017-11-21 17:47:51 +00:00
|
|
|
file_path = os.path.join(root, filename)
|
2017-11-27 23:03:20 +00:00
|
|
|
rel_file_path = os.path.relpath(file_path, extract_directory)
|
|
|
|
rel_directory = os.path.dirname(rel_file_path)
|
|
|
|
|
|
|
|
target_dir = os.path.join(output_directory, rel_directory)
|
|
|
|
if not os.path.exists(target_dir):
|
|
|
|
os.makedirs(target_dir)
|
|
|
|
|
|
|
|
target_file_path = os.path.join(output_directory, rel_file_path)
|
|
|
|
extracted.append(target_file_path)
|
|
|
|
|
|
|
|
if os.path.islink(file_path):
|
|
|
|
link_path = os.readlink(file_path)
|
|
|
|
if os.path.isabs(link_path):
|
|
|
|
# Make absolute links relative.
|
|
|
|
link_path = os.path.relpath(
|
|
|
|
link_path, os.path.join('/', rel_directory))
|
|
|
|
|
|
|
|
os.symlink(link_path, target_file_path)
|
|
|
|
else:
|
|
|
|
shutil.copy2(file_path, target_file_path)
|
|
|
|
|
|
|
|
return extracted
|
2017-11-21 17:47:51 +00:00
|
|
|
|
|
|
|
|
2017-11-27 21:24:43 +00:00
|
|
|
def GetPackage(package_name):
|
|
|
|
custom_package_path = os.path.join(PACKAGES_DIR, package_name) + '.py'
|
|
|
|
if not os.path.exists(custom_package_path):
|
|
|
|
print('Using default package build steps.')
|
|
|
|
return package.Package(package_name)
|
|
|
|
|
|
|
|
print('Using custom package build steps.')
|
|
|
|
module = imp.load_source('packages.' + package_name, custom_package_path)
|
|
|
|
return module.Package()
|
|
|
|
|
|
|
|
|
2017-11-27 23:03:20 +00:00
|
|
|
def PatchRpath(path, output_directory):
|
|
|
|
"""Patch rpath to be relative to $ORIGIN."""
|
|
|
|
try:
|
|
|
|
rpaths = subprocess.check_output(
|
|
|
|
['patchelf', '--print-rpath', path]).strip()
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not rpaths:
|
|
|
|
return
|
|
|
|
|
|
|
|
processed_rpath = []
|
|
|
|
rel_directory = os.path.join(
|
|
|
|
'/', os.path.dirname(os.path.relpath(path, output_directory)))
|
|
|
|
|
|
|
|
for rpath in rpaths.split(':'):
|
|
|
|
if '$ORIGIN' in rpath:
|
|
|
|
# Already relative.
|
|
|
|
processed_rpath.append(rpath)
|
|
|
|
continue
|
|
|
|
|
|
|
|
processed_rpath.append(os.path.join(
|
|
|
|
'$ORIGIN',
|
|
|
|
os.path.relpath(rpath, rel_directory)))
|
|
|
|
|
|
|
|
processed_rpath = ':'.join(processed_rpath)
|
|
|
|
print('Patching rpath for', path, 'to', processed_rpath)
|
|
|
|
subprocess.check_call(
|
|
|
|
['patchelf', '--force-rpath', '--set-rpath',
|
|
|
|
processed_rpath, path])
|
|
|
|
|
|
|
|
|
2017-11-21 17:47:51 +00:00
|
|
|
class MSanBuilder(object):
|
|
|
|
"""MSan builder."""
|
|
|
|
|
2017-11-27 21:24:43 +00:00
|
|
|
def __init__(self, debug=False, log_path=None, work_dir=None):
|
2017-11-22 18:14:47 +00:00
|
|
|
self.debug = debug
|
2017-11-22 21:13:30 +00:00
|
|
|
self.log_path = log_path
|
2017-11-27 21:24:43 +00:00
|
|
|
self.work_dir = work_dir
|
2017-11-21 17:47:51 +00:00
|
|
|
self.env = None
|
|
|
|
|
|
|
|
def __enter__(self):
|
2017-11-27 21:24:43 +00:00
|
|
|
if not self.work_dir:
|
|
|
|
self.work_dir = tempfile.mkdtemp(dir=self.work_dir)
|
|
|
|
|
2017-12-04 20:35:43 +00:00
|
|
|
if os.path.exists(self.work_dir):
|
|
|
|
shutil.rmtree(self.work_dir, ignore_errors=True)
|
|
|
|
|
|
|
|
os.makedirs(self.work_dir)
|
2017-11-21 17:47:51 +00:00
|
|
|
self.env = SetUpEnvironment(self.work_dir)
|
2017-11-22 21:13:30 +00:00
|
|
|
|
|
|
|
if self.debug and self.log_path:
|
|
|
|
self.env['WRAPPER_DEBUG_LOG_PATH'] = self.log_path
|
|
|
|
|
2017-11-21 17:47:51 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2017-11-22 21:13:30 +00:00
|
|
|
if not self.debug:
|
2017-11-22 18:14:47 +00:00
|
|
|
shutil.rmtree(self.work_dir, ignore_errors=True)
|
2017-11-21 17:47:51 +00:00
|
|
|
|
2017-11-27 21:24:43 +00:00
|
|
|
def Build(self, package_name, output_directory):
|
2017-11-21 17:47:51 +00:00
|
|
|
"""Build the package and write results into the output directory."""
|
2017-12-04 20:35:43 +00:00
|
|
|
deb_paths = FindPackageDebs(package_name, self.work_dir)
|
|
|
|
if deb_paths:
|
|
|
|
print('Source package already built for', package_name)
|
|
|
|
else:
|
|
|
|
pkg = GetPackage(package_name)
|
|
|
|
|
|
|
|
pkg.InstallBuildDeps()
|
|
|
|
source_directory = pkg.DownloadSource(self.work_dir)
|
|
|
|
print('Source downloaded to', source_directory)
|
|
|
|
|
|
|
|
pkg.Build(source_directory, self.env)
|
|
|
|
deb_paths = FindPackageDebs(package_name, self.work_dir)
|
2017-11-27 21:24:43 +00:00
|
|
|
|
2017-12-04 20:35:43 +00:00
|
|
|
if not deb_paths:
|
|
|
|
raise MSanBuildException('Failed to find .deb packages.')
|
2017-11-21 17:47:51 +00:00
|
|
|
|
2017-12-04 20:35:43 +00:00
|
|
|
extracted_paths = ExtractSharedLibraries(deb_paths, self.work_dir,
|
|
|
|
output_directory)
|
2017-11-27 23:03:20 +00:00
|
|
|
for extracted_path in extracted_paths:
|
|
|
|
if not os.path.islink(extracted_path):
|
|
|
|
PatchRpath(extracted_path, output_directory)
|
2017-11-21 17:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser('msan_build.py', description='MSan builder.')
|
2017-12-04 20:35:43 +00:00
|
|
|
parser.add_argument('package_names', nargs='+', help='Name of the packages.')
|
2017-11-21 17:47:51 +00:00
|
|
|
parser.add_argument('output_dir', help='Output directory.')
|
2017-11-22 18:14:47 +00:00
|
|
|
parser.add_argument('--debug', action='store_true', help='Enable debug mode.')
|
2017-11-22 21:13:30 +00:00
|
|
|
parser.add_argument('--log-path', help='Log path for debugging.')
|
2017-11-27 21:24:43 +00:00
|
|
|
parser.add_argument('--work-dir', help='Work directory.')
|
2017-11-21 17:47:51 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if not os.path.exists(args.output_dir):
|
|
|
|
os.makedirs(args.output_dir)
|
|
|
|
|
2017-11-27 21:24:43 +00:00
|
|
|
with MSanBuilder(debug=args.debug, log_path=args.log_path,
|
|
|
|
work_dir=args.work_dir) as builder:
|
2017-12-04 20:35:43 +00:00
|
|
|
for package_name in args.package_names:
|
|
|
|
builder.Build(package_name, args.output_dir)
|
2017-11-21 17:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|