Skip to content

Commit 87b00ef

Browse files
authored
[libc][bazel] Prevent LIBC_NAMESPACE leakeage (#70455)
Right now `LIBC_NAMESPACE` definition is added to all rules that depend on any libc function. This have far-reaching effects which prevents llvm-libc development when the installed libc is itself llvm-libc. This PR removes the `:libc_root` target entirely and consistently uses the provided libc BUILD actions for compilation and testing. This makes sure that we don't leak `LIBC_NAMESPACE` to our clients.
1 parent e4044a7 commit 87b00ef

File tree

9 files changed

+109
-276
lines changed

9 files changed

+109
-276
lines changed

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 0 additions & 90 deletions
Large diffs are not rendered by default.

utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,56 @@
44

55
"""LLVM libc starlark rules for building individual functions."""
66

7+
load("@bazel_skylib//lib:paths.bzl", "paths")
78
load("@bazel_skylib//lib:selects.bzl", "selects")
9+
load(":libc_namespace.bzl", "LIBC_NAMESPACE")
810
load(":platforms.bzl", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_X86_64")
911

10-
LIBC_ROOT_TARGET = ":libc_root"
11-
INTERNAL_SUFFIX = ".__internal__"
12+
def libc_internal_target(name):
13+
return name + ".__internal__"
1214

13-
def _libc_library(name, copts = None, **kwargs):
15+
def libc_common_copts():
16+
root_label = Label(":libc")
17+
libc_include_path = paths.join(root_label.workspace_root, root_label.package)
18+
return [
19+
"-I" + libc_include_path,
20+
"-DLIBC_NAMESPACE=" + LIBC_NAMESPACE,
21+
]
22+
23+
def _libc_library(name, hidden, copts = [], deps = [], **kwargs):
1424
"""Internal macro to serve as a base for all other libc library rules.
1525
1626
Args:
1727
name: Target name.
1828
copts: The special compiler options for the target.
29+
deps: The list of target dependencies if any.
30+
hidden: Whether the symbols should be explicitly hidden or not.
1931
**kwargs: All other attributes relevant for the cc_library rule.
2032
"""
21-
copts = copts or []
2233

2334
# We want all libc sources to be compiled with "hidden" visibility.
2435
# The public symbols will be given "default" visibility explicitly.
2536
# See src/__support/common.h for more information.
26-
copts = copts + ["-fvisibility=hidden"]
37+
if hidden:
38+
copts = copts + ["-fvisibility=hidden"]
2739
native.cc_library(
2840
name = name,
29-
copts = copts,
41+
copts = copts + libc_common_copts(),
42+
deps = deps,
3043
linkstatic = 1,
3144
**kwargs
3245
)
3346

34-
# A convenience var which should be used to list all libc support libraries.
47+
# A convenience function which should be used to list all libc support libraries.
3548
# Any library which does not define a public function should be listed with
3649
# libc_support_library.
37-
libc_support_library = _libc_library
50+
def libc_support_library(name, **kwargs):
51+
_libc_library(name = name, hidden = False, **kwargs)
3852

3953
def libc_function(
4054
name,
4155
srcs,
4256
weak = False,
43-
deps = None,
4457
copts = None,
4558
local_defines = None,
4659
**kwargs):
@@ -64,37 +77,33 @@ def libc_function(
6477
its deps.
6578
**kwargs: Other attributes relevant for a cc_library. For example, deps.
6679
"""
67-
deps = deps or []
6880

6981
# We use the explicit equals pattern here because append and += mutate the
7082
# original list, where this creates a new list and stores it in deps.
71-
deps = deps + [LIBC_ROOT_TARGET]
7283
copts = copts or []
7384
copts = copts + ["-O3", "-fno-builtin", "-fno-lax-vector-conversions"]
7485

7586
# We compile the code twice, the first target is suffixed with ".__internal__" and contains the
76-
# C++ functions in the "__llvm_libc" namespace. This allows us to test the function in the
87+
# C++ functions in the "LIBC_NAMESPACE" namespace. This allows us to test the function in the
7788
# presence of another libc.
78-
native.cc_library(
79-
name = name + INTERNAL_SUFFIX,
89+
libc_support_library(
90+
name = libc_internal_target(name),
8091
srcs = srcs,
81-
deps = deps,
8292
copts = copts,
83-
linkstatic = 1,
8493
**kwargs
8594
)
8695

87-
# This second target is the llvm libc C function.
88-
96+
# This second target is the llvm libc C function with either a default or hidden visibility.
97+
# All other functions are hidden.
8998
func_attrs = ["__attribute__((visibility(\"default\")))"]
9099
if weak:
91100
func_attrs = func_attrs + ["__attribute__((weak))"]
92101
local_defines = local_defines or ["LIBC_COPT_PUBLIC_PACKAGING"]
93102
local_defines = local_defines + ["LLVM_LIBC_FUNCTION_ATTR='%s'" % " ".join(func_attrs)]
94103
_libc_library(
95104
name = name,
105+
hidden = True,
96106
srcs = srcs,
97-
deps = deps,
98107
copts = copts,
99108
local_defines = local_defines,
100109
**kwargs

utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
# LLVM libc unittest library.
66

7+
load("//libc:libc_build_rules.bzl", "libc_support_library")
8+
79
package(default_visibility = ["//visibility:public"])
810

911
licenses(["notice"])
1012

11-
cc_library(
13+
libc_support_library(
1214
name = "test_logger",
1315
srcs = ["TestLogger.cpp"],
1416
hdrs = ["TestLogger.h"],
@@ -17,11 +19,10 @@ cc_library(
1719
"//libc:__support_cpp_string_view",
1820
"//libc:__support_osutil_io",
1921
"//libc:__support_uint128",
20-
"//libc:libc_root",
2122
],
2223
)
2324

24-
cc_library(
25+
libc_support_library(
2526
name = "LibcUnitTest",
2627
srcs = [
2728
"BazelFilePath.cpp",
@@ -52,12 +53,11 @@ cc_library(
5253
"//libc:__support_stringutil",
5354
"//libc:__support_uint128",
5455
"//libc:errno",
55-
"//libc:libc_root",
5656
"//llvm:Support",
5757
],
5858
)
5959

60-
cc_library(
60+
libc_support_library(
6161
name = "fp_test_helpers",
6262
srcs = [
6363
"FPExceptMatcher.cpp",
@@ -79,11 +79,10 @@ cc_library(
7979
"//libc:__support_fputil_fp_bits",
8080
"//libc:__support_fputil_fpbits_str",
8181
"//libc:__support_fputil_rounding_mode",
82-
"//libc:libc_root",
8382
],
8483
)
8584

86-
cc_library(
85+
libc_support_library(
8786
name = "memory_matcher",
8887
srcs = [
8988
"MemoryMatcher.cpp",
@@ -100,7 +99,7 @@ cc_library(
10099
],
101100
)
102101

103-
cc_library(
102+
libc_support_library(
104103
name = "printf_matcher",
105104
srcs = [
106105
"PrintfMatcher.cpp",
@@ -116,7 +115,7 @@ cc_library(
116115
],
117116
)
118117

119-
cc_library(
118+
libc_support_library(
120119
name = "string_utils",
121120
hdrs = [
122121
"StringUtils.h",

utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,34 @@
66
77
libc functions are created though the libc_build_rules.bzl:libc_function.
88
They come in two flavors:
9-
- the internal one that is scoped into the `__llvm_libc` namespace.
9+
- the internal one that is scoped into the `LIBC_NAMESPACE` namespace.
1010
- the libc one that is the regular C function.
1111
1212
When performing tests we make sure to always use the internal version.
1313
"""
1414

15-
load("//libc:libc_build_rules.bzl", "INTERNAL_SUFFIX")
15+
load("//libc:libc_build_rules.bzl", "libc_common_copts", "libc_internal_target")
1616

17-
def libc_test(name, srcs, libc_function_deps, deps = [], **kwargs):
17+
def libc_test(name, srcs, libc_function_deps = [], copts = [], deps = [], **kwargs):
1818
"""Add target for a libc test.
1919
2020
Args:
2121
name: Test target name
2222
srcs: List of sources for the test.
2323
libc_function_deps: List of libc_function targets used by this test.
24+
copts: The list of options to add to the C++ compilation command.
2425
deps: The list of other libraries to be linked in to the test target.
25-
**kwargs: Attributes relevant for a cc_test. For example, name, srcs.
26+
**kwargs: Attributes relevant for a libc_test. For example, name, srcs.
2627
"""
2728
all_function_deps = libc_function_deps + ["//libc:errno"]
2829
native.cc_test(
2930
name = name,
3031
srcs = srcs,
31-
deps = [d + INTERNAL_SUFFIX for d in all_function_deps] + [
32-
"//libc:libc_root",
32+
deps = [libc_internal_target(d) for d in all_function_deps] + [
3333
"//libc/test/UnitTest:LibcUnitTest",
3434
] + deps,
3535
features = ["-link_llvmlibc"], # Do not link libllvmlibc.a
36+
copts = copts + libc_common_copts(),
3637
linkstatic = 1,
3738
**kwargs
3839
)

utils/bazel/llvm-project-overlay/libc/test/src/__support/CPP/BUILD.bazel

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,119 +4,83 @@
44

55
# Tests for LLVM libc CPP functions.
66

7+
load("//libc/test:libc_test_rules.bzl", "libc_test")
8+
79
package(default_visibility = ["//visibility:public"])
810

911
licenses(["notice"])
1012

11-
cc_test(
13+
libc_test(
1214
name = "atomic_test",
1315
srcs = ["atomic_test.cpp"],
14-
deps = [
15-
"//libc:__support_cpp_atomic",
16-
"//libc:libc_root",
17-
"//libc/test/UnitTest:LibcUnitTest",
18-
],
16+
deps = ["//libc:__support_cpp_atomic"],
1917
)
2018

21-
cc_test(
19+
libc_test(
2220
name = "bitset_test",
2321
srcs = ["bitset_test.cpp"],
24-
deps = [
25-
"//libc:__support_cpp_bitset",
26-
"//libc:libc_root",
27-
"//libc/test/UnitTest:LibcUnitTest",
28-
],
22+
deps = ["//libc:__support_cpp_bitset"],
2923
)
3024

31-
cc_test(
25+
libc_test(
3226
name = "cstddef_test",
3327
srcs = ["cstddef_test.cpp"],
34-
deps = [
35-
"//libc:__support_cpp_cstddef",
36-
"//libc:libc_root",
37-
"//libc/test/UnitTest:LibcUnitTest",
38-
],
28+
deps = ["//libc:__support_cpp_cstddef"],
3929
)
4030

41-
cc_test(
31+
libc_test(
4232
name = "integer_sequence_test",
4333
srcs = ["integer_sequence_test.cpp"],
44-
deps = [
45-
"//libc:__support_cpp_utility",
46-
"//libc:libc_root",
47-
"//libc/test/UnitTest:LibcUnitTest",
48-
],
34+
deps = ["//libc:__support_cpp_utility"],
4935
)
5036

51-
cc_test(
37+
libc_test(
5238
name = "limits_test",
5339
srcs = ["limits_test.cpp"],
5440
deps = [
5541
"//libc:__support_cpp_limits",
5642
"//libc:__support_uint",
57-
"//libc:libc_root",
58-
"//libc/test/UnitTest:LibcUnitTest",
5943
],
6044
)
6145

62-
cc_test(
46+
libc_test(
6347
name = "optional_test",
6448
srcs = ["optional_test.cpp"],
65-
deps = [
66-
"//libc:__support_cpp_optional",
67-
"//libc:libc_root",
68-
"//libc/test/UnitTest:LibcUnitTest",
69-
],
49+
deps = ["//libc:__support_cpp_optional"],
7050
)
7151

72-
cc_test(
52+
libc_test(
7353
name = "span_test",
7454
srcs = ["span_test.cpp"],
7555
deps = [
7656
"//libc:__support_cpp_array",
7757
"//libc:__support_cpp_span",
78-
"//libc:libc_root",
79-
"//libc/test/UnitTest:LibcUnitTest",
8058
],
8159
)
8260

83-
cc_test(
61+
libc_test(
8462
name = "stringstream_test",
8563
srcs = ["stringstream_test.cpp"],
8664
deps = [
8765
"//libc:__support_cpp_span",
8866
"//libc:__support_cpp_stringstream",
89-
"//libc:libc_root",
90-
"//libc/test/UnitTest:LibcUnitTest",
9167
],
9268
)
9369

94-
cc_test(
70+
libc_test(
9571
name = "string_test",
9672
srcs = ["string_test.cpp"],
97-
deps = [
98-
"//libc:__support_cpp_string",
99-
"//libc:libc_root",
100-
"//libc/test/UnitTest:LibcUnitTest",
101-
],
73+
deps = ["//libc:__support_cpp_string"],
10274
)
10375

104-
cc_test(
76+
libc_test(
10577
name = "stringview_test",
10678
srcs = ["stringview_test.cpp"],
107-
deps = [
108-
"//libc:__support_cpp_string_view",
109-
"//libc:libc_root",
110-
"//libc/test/UnitTest:LibcUnitTest",
111-
],
79+
deps = ["//libc:__support_cpp_string_view"],
11280
)
11381

114-
cc_test(
82+
libc_test(
11583
name = "type_traits_test",
11684
srcs = ["type_traits_test.cpp"],
117-
deps = [
118-
"//libc:__support_cpp_type_traits",
119-
"//libc:libc_root",
120-
"//libc/test/UnitTest:LibcUnitTest",
121-
],
85+
deps = ["//libc:__support_cpp_type_traits"],
12286
)

0 commit comments

Comments
 (0)