Skip to content

Commit bf60ae2

Browse files
dvyukovcopybara-github
authored andcommitted
Pass more precise defines to the clang tool
The immediate problem at hand is that TSan/MSan change libc++ ABI so that std namespace becomes std::__xsan, which in turn changes name mangling. As the result all names we extract don't match the actual names in the final build. Use the actual defines that would be used during C++ compilation for the clang tool. This fixes the name mangling problem. PiperOrigin-RevId: 792115834 Change-Id: Ia064dceb1d19d2b9291ec7bb1c1edf0807b9385e
1 parent eef91dc commit bf60ae2

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

sandboxed_api/bazel/sapi.bzl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,31 @@ def sort_deps(deps):
7373
other_deps = [x for x in deps if not x.startswith(":")]
7474
return sorted(colon_deps) + sorted(other_deps)
7575

76-
def _clang_generator_flags(cc_ctx, cpp_toolchain, input_files_paths):
76+
def _clang_generator_flags(ctx, cc_ctx, cpp_toolchain, input_files_paths):
7777
flags = []
7878

79+
feature_configuration = cc_common.configure_features(
80+
ctx = ctx,
81+
cc_toolchain = cpp_toolchain,
82+
requested_features = ctx.features,
83+
unsupported_features = ctx.disabled_features + ["module_maps"],
84+
)
85+
compile_variables = cc_common.create_compile_variables(
86+
cc_toolchain = cpp_toolchain,
87+
feature_configuration = feature_configuration,
88+
)
89+
default_copts = cc_common.get_memory_inefficient_command_line(
90+
feature_configuration = feature_configuration,
91+
action_name = "c++-compile",
92+
variables = compile_variables,
93+
)
94+
95+
# Use the defines that would be used by the toolchain normally for C++ compilation.
96+
# Add them first so that they are overridden by the user's copts.
97+
for copt in default_copts:
98+
if copt.startswith("-D"):
99+
flags.append("--extra-arg=" + copt)
100+
79101
# The gnu compiler is what frequently used in practice,
80102
# and it allows to use more langauge extensions (e.g. _Bool type in C).
81103
# However, compiling C code with -std=gnu++ does not work,
@@ -173,7 +195,7 @@ def _sapi_interface_impl(ctx):
173195

174196
if use_clang_generator:
175197
input_files += cpp_toolchain.all_files.to_list()
176-
extra_flags += _clang_generator_flags(cc_ctx, cpp_toolchain, input_files_paths)
198+
extra_flags += _clang_generator_flags(ctx, cc_ctx, cpp_toolchain, input_files_paths)
177199
else:
178200
append_all(extra_flags, "-D", cc_ctx.defines.to_list())
179201
append_all(extra_flags, "-isystem", cc_ctx.system_includes.to_list())
@@ -200,6 +222,7 @@ def _sapi_interface_impl(ctx):
200222
# Build rule that generates SAPI interface.
201223
sapi_interface = rule(
202224
implementation = _sapi_interface_impl,
225+
fragments = ["cpp"],
203226
attrs = {
204227
"out": attr.output(mandatory = True),
205228
"embed_dir": attr.string(),
@@ -619,7 +642,7 @@ def _sandboxed_library_gen_impl(ctx):
619642
args.append("--sapi_out={}".format(ctx.attr.sapi_hdr))
620643
args.append("--sapi_limit_scan_depth")
621644
input_files_paths = _lib_direct_headers(ctx.attr.lib, cc_ctx)
622-
args += _clang_generator_flags(cc_ctx, cpp_toolchain, input_files_paths)
645+
args += _clang_generator_flags(ctx, cc_ctx, cpp_toolchain, input_files_paths)
623646
args += input_files_paths
624647

625648
progress_msg = "Generating sandboxed library {}.".format(ctx.attr.lib_name)
@@ -635,6 +658,7 @@ def _sandboxed_library_gen_impl(ctx):
635658
# Build rule that generates SAPI interface.
636659
_sandboxed_library_gen = rule(
637660
implementation = _sandboxed_library_gen_impl,
661+
fragments = ["cpp"],
638662
attrs = {
639663
"lib": attr.label(providers = [CcInfo]),
640664
"lib_name": attr.string(),

sandboxed_api/testcases/BUILD

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,5 @@ cc_sandboxed_library_test(
3838
srcs = ["replaced_library_test.cc"],
3939
lib = ":replaced_library",
4040
sandboxed_lib = ":replacement_library",
41-
# TSan/MSan may change ABI and name mangling, but the clang tool is not invoked
42-
# correctly (the same way as the actual build) and produces wrong symbol names.
43-
tags = [
44-
"nomsan",
45-
"notsan",
46-
],
4741
deps = ["@googletest//:gtest_main"],
4842
)

0 commit comments

Comments
 (0)