Skip to content
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

Add extra_compiler_files attribute to make ignorelist files available to sanitizers during compilation #245

Merged
merged 4 commits into from
Mar 13, 2024
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
49 changes: 40 additions & 9 deletions toolchain/cc_wrapper.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,55 @@

# shellcheck disable=SC1083

set -eu
set -euo pipefail

# See note in toolchain/internal/configure.bzl where we define
# `wrapper_bin_prefix` for why this wrapper is needed.

# Call the C++ compiler.
if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
exec %{toolchain_path_prefix}bin/clang "$@"
execroot_path=""
elif [[ ${BASH_SOURCE[0]} == "/"* ]]; then
# Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
# change CWD and call $CC (this script) with its absolute path.
# the execroot (i.e. `cmake` from `rules_foreign_cc`) and call CC . For cases like this,
# we'll try to find `clang` relative to this script.
# This script is at _execroot_/external/_repo_name_/bin/clang_wrapper.sh
execroot_path="${BASH_SOURCE[0]%/*/*/*/*}"
clang="${execroot_path}/%{toolchain_path_prefix}bin/clang"
exec "${clang}" "${@}"
# For cases like this, we'll try to find `clang` through an absolute path.
# This script is at _execroot_/external/_repo_name_/bin/cc_wrapper.sh
execroot_path="${BASH_SOURCE[0]%/*/*/*/*}/"
else
echo >&2 "ERROR: could not find clang; PWD=\"${PWD}\"; PATH=\"${PATH}\"."
exit 5
fi

function sanitize_option() {
local -r opt=$1
if [[ ${opt} == */cc_wrapper.sh ]]; then
printf "%s" "${execroot_path}%{toolchain_path_prefix}bin/clang"
elif [[ ${opt} =~ ^-fsanitize-(ignore|black)list=[^/] ]]; then
# shellcheck disable=SC2206
parts=(${opt/=/ }) # Split flag name and value into array.
printf "%s" "${parts[0]}=${execroot_path}${parts[1]}"
else
printf "%s" "${opt}"
fi
}

cmd=()
for ((i = 0; i <= $#; i++)); do
if [[ ${!i} == @* ]]; then
while IFS= read -r opt; do
opt="$(
set -e
sanitize_option "${opt}"
)"
cmd+=("${opt}")
done <"${!i:1}"
else
opt="$(
set -e
sanitize_option "${!i}"
)"
cmd+=("${opt}")
fi
done

# Call the C++ compiler.
exec "${cmd[@]}"
8 changes: 7 additions & 1 deletion toolchain/internal/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def llvm_config_impl(rctx):
coverage_link_flags_dict = rctx.attr.coverage_link_flags,
unfiltered_compile_flags_dict = rctx.attr.unfiltered_compile_flags,
llvm_version = llvm_version,
extra_compiler_files = rctx.attr.extra_compiler_files,
)
host_dl_ext = "dylib" if os == "darwin" else "so"
host_tools_info = dict([
Expand Down Expand Up @@ -412,7 +413,10 @@ filegroup(
template = template + """
filegroup(
name = "compiler-components-{suffix}",
srcs = [":sysroot-components-{suffix}"],
srcs = [
":sysroot-components-{suffix}",
{extra_compiler_files}
],
)

filegroup(
Expand Down Expand Up @@ -445,6 +449,7 @@ filegroup(
"{llvm_dist_label_prefix}clang",
"{llvm_dist_label_prefix}include",
":sysroot-components-{suffix}",
{extra_compiler_files}
],
)

Expand Down Expand Up @@ -541,6 +546,7 @@ cc_toolchain(
extra_files_str = extra_files_str,
host_tools_info = host_tools_info,
cxx_builtin_include_directories = _list_to_string(cxx_builtin_include_directories),
extra_compiler_files = ("\"%s\"," % str(toolchain_info.extra_compiler_files)) if toolchain_info.extra_compiler_files else "",
)

def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_dist_label_prefix, host_dl_ext):
Expand Down
6 changes: 6 additions & 0 deletions toolchain/internal/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ _compiler_configuration_attrs = {
mandatory = False,
doc = ("Override the toolchain's `target_settings` attribute."),
),
"extra_compiler_files": attr.label(
mandatory = False,
doc = ("Files to be made available in the sandbox for compile actions. " +
"Mostly useful for providing files containing lists of flags, e.g. " +
"sanitizer ignorelists."),
),
}

llvm_config_attrs = dict(common_attrs)
Expand Down
106 changes: 65 additions & 41 deletions toolchain/osx_cc_wrapper.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac
# on how to set those paths for Mach-O binaries.

# shellcheck disable=all
# shellcheck disable=SC1083

set -eu
set -euo pipefail

INSTALL_NAME_TOOL="/usr/bin/install_name_tool"

Expand All @@ -39,29 +39,65 @@ OUTPUT=
function parse_option() {
local -r opt="$1"
if [[ ${OUTPUT} == "1" ]]; then
OUTPUT=$opt
elif [[ $opt =~ ^-l(.*)$ ]]; then
LIBS="${BASH_REMATCH[1]} $LIBS"
elif [[ $opt =~ ^-L(.*)$ ]]; then
LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
elif [[ $opt =~ ^\@loader_path/(.*)$ ]]; then
OUTPUT=${opt}
elif [[ ${opt} =~ ^-l(.*)$ ]]; then
LIBS="${BASH_REMATCH[1]} ${LIBS}"
elif [[ ${opt} =~ ^-L(.*)$ ]]; then
LIB_DIRS="${BASH_REMATCH[1]} ${LIB_DIRS}"
elif [[ ${opt} =~ ^\@loader_path/(.*)$ ]]; then
RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
elif [[ $opt =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
elif [[ ${opt} =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
elif [[ $opt == "-o" ]]; then
elif [[ ${opt} == "-o" ]]; then
# output is coming
OUTPUT=1
fi
}

# let parse the option list
for i in "$@"; do
if [[ $i == @* ]]; then
if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
execroot_path=""
elif [[ ${BASH_SOURCE[0]} == "/"* ]]; then
# Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
# change CWD and call $CC (this script) with its absolute path.
# For cases like this, we'll try to find `clang` through an absolute path.
# This script is at _execroot_/external/_repo_name_/bin/cc_wrapper.sh
execroot_path="${BASH_SOURCE[0]%/*/*/*/*}/"
else
echo >&2 "ERROR: could not find clang; PWD=\"${PWD}\"; PATH=\"${PATH}\"."
exit 5
fi

function sanitize_option() {
local -r opt=$1
if [[ ${opt} == */cc_wrapper.sh ]]; then
printf "%s" "${execroot_path}%{toolchain_path_prefix}bin/clang"
elif [[ ${opt} =~ ^-fsanitize-(ignore|black)list=[^/] ]]; then
# shellcheck disable=SC2206
parts=(${opt/=/ }) # Split flag name and value into array.
printf "%s" "${parts[0]}=${execroot_path}${parts[1]}"
else
printf "%s" "${opt}"
fi
}

cmd=()
for ((i = 0; i <= $#; i++)); do
if [[ ${!i} == @* ]]; then
while IFS= read -r opt; do
parse_option "$opt"
done <"${i:1}" || exit 1
opt="$(
set -e
sanitize_option "${opt}"
)"
parse_option "${opt}"
cmd+=("${opt}")
done <"${!i:1}"
else
parse_option "$i"
opt="$(
set -e
sanitize_option "${!i}"
)"
parse_option "${opt}"
cmd+=("${opt}")
fi
done

Expand All @@ -86,27 +122,13 @@ if [[ ":${PATH}:" != *":/usr/bin:"* ]]; then
fi

# Call the C++ compiler.
if [[ -f %{toolchain_path_prefix}bin/clang ]]; then
%{toolchain_path_prefix}bin/clang "$@"
elif [[ ${BASH_SOURCE[0]} == "/"* ]]; then
# Some consumers of `CcToolchainConfigInfo` (e.g. `cmake` from rules_foreign_cc)
# change CWD and call $CC (this script) with its absolute path.
# the execroot (i.e. `cmake` from `rules_foreign_cc`) and call CC . For cases like this,
# we'll try to find `clang` relative to this script.
# This script is at _execroot_/external/_repo_name_/bin/cc_wrapper.sh
execroot_path="${BASH_SOURCE[0]%/*/*/*/*}"
clang="${execroot_path}/%{toolchain_path_prefix}bin/clang"
"${clang}" "${@}"
else
echo >&2 "ERROR: could not find clang; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
exit 5
fi
"${cmd[@]}"

function get_library_path() {
for libdir in ${LIB_DIRS}; do
if [ -f ${libdir}/lib$1.so ]; then
if [[ -f "${libdir}/lib$1".so ]]; then
echo "${libdir}/lib$1.so"
elif [ -f ${libdir}/lib$1.dylib ]; then
elif [[ -f "${libdir}"/lib"$1".dylib ]]; then
echo "${libdir}/lib$1.dylib"
fi
done
Expand All @@ -116,8 +138,9 @@ function get_library_path() {
# and multi-level symlinks.
function get_realpath() {
local previous="$1"
local next=$(readlink "${previous}")
while [ -n "${next}" ]; do
local next
next="$(readlink "${previous}")"
while [[ -n ${next} ]]; do
previous="${next}"
next=$(readlink "${previous}")
done
Expand All @@ -127,24 +150,25 @@ function get_realpath() {
# Get the path of a lib inside a tool
function get_otool_path() {
# the lib path is the path of the original lib relative to the workspace
get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|'
get_realpath "$1" | sed 's|^.*/bazel-out/|bazel-out/|'
}

# Do replacements in the output
for rpath in ${RPATHS}; do
for lib in ${LIBS}; do
unset libname
if [ -f "$(dirname ${OUTPUT})/${rpath}/lib${lib}.so" ]; then
if [[ -f "$(dirname "${OUTPUT}")/${rpath}/lib${lib}.so" ]]; then
libname="lib${lib}.so"
elif [ -f "$(dirname ${OUTPUT})/${rpath}/lib${lib}.dylib" ]; then
elif [[ -f "$(dirname "${OUTPUT}")/${rpath}/lib${lib}.dylib" ]]; then
libname="lib${lib}.dylib"
fi
# ${libname-} --> return $libname if defined, or undefined otherwise. This is to make
# this set -e friendly
if [[ -n ${libname-} ]]; then
libpath=$(get_library_path ${lib})
if [ -n "${libpath}" ]; then
${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") \
libpath="$(get_library_path "${lib}")"
if [[ -n ${libpath} ]]; then
otool_path="$(get_otool_path "${libpath}")"
"${INSTALL_NAME_TOOL}" -change "${otool_path}" \
"@loader_path/${rpath}/${libname}" "${OUTPUT}"
fi
fi
Expand Down
Loading