Bug 1335411 - Fix --enable-address-sanitizer for Mac cross-compilation and adapt Linux ASan configs for Mac. r=froydnj
authorJesse Schwartzentruber <jschwartzentruber@mozilla.com>
Fri, 10 Feb 2017 11:10:23 -0500
changeset 484288 1e88c7e7676c43568615214631547587b944c823
parent 484287 12a0181ef7db2b46ef95607e0c6d9f718d5213d8
child 484289 feb1d8e5b192765cc9ad6fad93464df1e35150df
push id45444
push userbmo:gasolin@mozilla.com
push dateWed, 15 Feb 2017 05:18:43 +0000
reviewersfroydnj
bugs1335411
milestone54.0a1
Bug 1335411 - Fix --enable-address-sanitizer for Mac cross-compilation and adapt Linux ASan configs for Mac. r=froydnj
browser/app/macbuild/Contents/MacOS-files.in
browser/config/mozconfigs/macosx64/debug-asan
browser/config/mozconfigs/macosx64/nightly-asan
build/autoconf/toolchain.m4
build/macosx/cross-mozconfig.common
build/unix/rewrite_asan_dylib.py
--- a/browser/app/macbuild/Contents/MacOS-files.in
+++ b/browser/app/macbuild/Contents/MacOS-files.in
@@ -1,10 +1,13 @@
 /*.app/***
 /*.dylib
 /certutil
 /firefox-bin
 /gtest/***
+#if defined(MOZ_ASAN) || defined(MOZ_TSAN)
+/llvm-symbolizer
+#endif
 /pingsender
 /pk12util
 /ssltunnel
 /xpcshell
 /XUL
--- a/browser/config/mozconfigs/macosx64/debug-asan
+++ b/browser/config/mozconfigs/macosx64/debug-asan
@@ -1,20 +1,24 @@
-. $topsrcdir/build/unix/mozconfig.asan
-
+# Use at least -O1 for optimization to avoid stack space
+# exhaustions caused by Clang function inlining.
 ac_add_options --enable-application=browser
 ac_add_options --enable-debug
 ac_add_options --enable-optimize="-O1"
 
+. $topsrcdir/build/unix/mozconfig.asan
+
 # Enable Telemetry
 export MOZ_TELEMETRY_REPORTING=1
 
-# Package js shell.
-export MOZ_PACKAGE_JSSHELL=1
-
 if test "${MOZ_UPDATE_CHANNEL}" = "nightly"; then
 ac_add_options --with-macbundlename-prefix=Firefox
 fi
 
+# Package js shell.
+export MOZ_PACKAGE_JSSHELL=1
+
 # Need this to prevent name conflicts with the normal nightly build packages
+# Before mozconfig.common so we can test for asan builds there
 export MOZ_PKG_SPECIAL=asan
 
+. "$topsrcdir/build/macosx/mozconfig.common"
 . "$topsrcdir/build/mozconfig.common.override"
new file mode 100644
--- /dev/null
+++ b/browser/config/mozconfigs/macosx64/nightly-asan
@@ -0,0 +1,23 @@
+ac_add_options --enable-application=browser
+# We still need to build with debug symbols
+ac_add_options --disable-debug
+ac_add_options --enable-optimize="-O2"
+
+. $topsrcdir/build/unix/mozconfig.asan
+
+# Enable Telemetry
+export MOZ_TELEMETRY_REPORTING=1
+
+if test "${MOZ_UPDATE_CHANNEL}" = "nightly"; then
+ac_add_options --with-macbundlename-prefix=Firefox
+fi
+
+# Package js shell.
+export MOZ_PACKAGE_JSSHELL=1
+
+# Need this to prevent name conflicts with the normal nightly build packages
+# Before mozconfig.common so we can test for asan builds there
+export MOZ_PKG_SPECIAL=asan
+
+. "$topsrcdir/build/macosx/mozconfig.common"
+. "$topsrcdir/build/mozconfig.common.override"
--- a/build/autoconf/toolchain.m4
+++ b/build/autoconf/toolchain.m4
@@ -77,16 +77,17 @@ AC_PROG_CXX
 
 AC_CHECK_PROGS(RANLIB, "${TOOLCHAIN_PREFIX}ranlib", :)
 AC_CHECK_PROGS(AR, "${TOOLCHAIN_PREFIX}ar", :)
 AC_CHECK_PROGS(AS, "${TOOLCHAIN_PREFIX}as", :)
 AC_CHECK_PROGS(LIPO, "${TOOLCHAIN_PREFIX}lipo", :)
 AC_CHECK_PROGS(STRIP, "${TOOLCHAIN_PREFIX}strip", :)
 AC_CHECK_PROGS(WINDRES, "${TOOLCHAIN_PREFIX}windres", :)
 AC_CHECK_PROGS(OTOOL, "${TOOLCHAIN_PREFIX}otool", :)
+AC_CHECK_PROGS(INSTALL_NAME_TOOL, "${TOOLCHAIN_PREFIX}install_name_tool", :)
 AC_CHECK_PROGS(OBJCOPY, "${TOOLCHAIN_PREFIX}objcopy", :)
 PATH=$_SAVE_PATH
 ])
 
 AC_DEFUN([MOZ_CXX11],
 [
 dnl Updates to the test below should be duplicated further below for the
 dnl cross-compiling case.
--- a/build/macosx/cross-mozconfig.common
+++ b/build/macosx/cross-mozconfig.common
@@ -40,14 +40,17 @@ export HOST_CXX="$topsrcdir/clang/bin/cl
 export HOST_CPP="$topsrcdir/clang/bin/clang -E"
 export HOST_CFLAGS="-g"
 export HOST_CXXFLAGS="-g"
 export HOST_LDFLAGS="-g"
 
 ac_add_options --target=x86_64-apple-darwin
 ac_add_options --with-macos-private-frameworks=$CROSS_PRIVATE_FRAMEWORKS
 
-# Enable static analysis checks by default on OSX cross builds.
-ac_add_options --enable-clang-plugin
+if [ "x$MOZ_PKG_SPECIAL" != "xasan" ]; then
+  # Enable static analysis checks by default on OSX cross builds.
+  # Exception is ASan, where this breaks.
+  ac_add_options --enable-clang-plugin
+fi
 
 . "$topsrcdir/build/mozconfig.cache"
 
 export SOCORRO_SYMBOL_UPLOAD_TOKEN_FILE=/builds/crash-stats-api.token
--- a/build/unix/rewrite_asan_dylib.py
+++ b/build/unix/rewrite_asan_dylib.py
@@ -1,27 +1,57 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+import re
 import sys
 import os
 import subprocess
 import shutil
 from buildconfig import substs
 
 '''
 Scans the given directories for binaries referencing the AddressSanitizer
 runtime library, copies it to the main directory and rewrites binaries to not
 reference it with absolute paths but with @executable_path instead.
 '''
 
 # This is the dylib we're looking for
 DYLIB_NAME='libclang_rt.asan_osx_dynamic.dylib'
 
+def resolve_rpath(filename):
+    otoolOut = subprocess.check_output([substs['OTOOL'], '-l', filename])
+    currentCmd = None
+
+    # The lines we need to find look like this:
+    # ...
+    # Load command 22
+    #           cmd LC_RPATH
+    #       cmdsize 80
+    #          path /home/build/src/clang/bin/../lib/clang/3.8.0/lib/darwin (offset 12)
+    # Load command 23
+    # ...
+    # Other load command types have a varying number of fields.
+    for line in otoolOut.splitlines():
+        cmdMatch = re.match(r'^\s+cmd ([A-Z_]+)', line)
+        if cmdMatch is not None:
+            currentCmd = cmdMatch.group(1)
+            continue
+
+        if currentCmd == 'LC_RPATH':
+            pathMatch = re.match(r'^\s+path (.*) \(offset \d+\)', line)
+            if pathMatch is not None:
+                path = pathMatch.group(1)
+                if os.path.isdir(path):
+                    return path
+
+    sys.stderr.write('@rpath could not be resolved from %s\n' % filename)
+    exit(1)
+
 def scan_directory(path):
     dylibCopied = False
 
     for root, subdirs, files in os.walk(path):
         for filename in files:
             filename = os.path.join(root, filename)
 
             # Skip all files that aren't either dylibs or executable
@@ -38,23 +68,29 @@ def scan_directory(path):
                 if line.find(DYLIB_NAME) != -1:
                     absDylibPath = line.split()[0]
 
                     # Don't try to rewrite binaries twice
                     if absDylibPath.find('@executable_path/') == 0:
                         continue
 
                     if not dylibCopied:
+                        if absDylibPath.find('@rpath/') == 0:
+                            rpath = resolve_rpath(filename)
+                            copyDylibPath = absDylibPath.replace('@rpath', rpath)
+                        else:
+                            copyDylibPath = absDylibPath
+
                         # Copy the runtime once to the main directory, which is passed
                         # as the argument to this function.
-                        shutil.copy(absDylibPath, path)
+                        shutil.copy(copyDylibPath, path)
 
                         # Now rewrite the library itself
-                        subprocess.check_call(['install_name_tool', '-id', '@executable_path/' + DYLIB_NAME, os.path.join(path, DYLIB_NAME)])
+                        subprocess.check_call([substs['INSTALL_NAME_TOOL'], '-id', '@executable_path/' + DYLIB_NAME, os.path.join(path, DYLIB_NAME)])
                         dylibCopied = True
 
                     # Now use install_name_tool to rewrite the path in our binary
-                    subprocess.check_call(['install_name_tool', '-change', absDylibPath, '@executable_path/' + DYLIB_NAME, filename])
+                    subprocess.check_call([substs['INSTALL_NAME_TOOL'], '-change', absDylibPath, '@executable_path/' + DYLIB_NAME, filename])
                     break
 
 if __name__ == '__main__':
     for d in sys.argv[1:]:
         scan_directory(d)