1#
2# Copyright 2017 The Abseil Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
18load(
19    "//absl:copts/configure_copts.bzl",
20    "ABSL_DEFAULT_COPTS",
21    "ABSL_DEFAULT_LINKOPTS",
22    "ABSL_TEST_COPTS",
23)
24
25package(
26    default_visibility = ["//visibility:public"],
27)
28
29licenses(["notice"])  # Apache 2.0
30
31cc_library(
32    name = "stacktrace",
33    srcs = [
34        "internal/stacktrace_aarch64-inl.inc",
35        "internal/stacktrace_arm-inl.inc",
36        "internal/stacktrace_config.h",
37        "internal/stacktrace_generic-inl.inc",
38        "internal/stacktrace_powerpc-inl.inc",
39        "internal/stacktrace_unimplemented-inl.inc",
40        "internal/stacktrace_win32-inl.inc",
41        "internal/stacktrace_x86-inl.inc",
42        "stacktrace.cc",
43    ],
44    hdrs = ["stacktrace.h"],
45    copts = ABSL_DEFAULT_COPTS,
46    linkopts = ABSL_DEFAULT_LINKOPTS,
47    deps = [
48        ":debugging_internal",
49        "//absl/base:config",
50        "//absl/base:core_headers",
51    ],
52)
53
54cc_library(
55    name = "symbolize",
56    srcs = [
57        "symbolize.cc",
58        "symbolize_elf.inc",
59        "symbolize_unimplemented.inc",
60        "symbolize_win32.inc",
61    ],
62    hdrs = [
63        "internal/symbolize.h",
64        "symbolize.h",
65    ],
66    copts = ABSL_DEFAULT_COPTS,
67    linkopts = ABSL_DEFAULT_LINKOPTS + select({
68        "//absl:windows": ["-DEFAULTLIB:dbghelp.lib"],
69        "//conditions:default": [],
70    }),
71    deps = [
72        ":debugging_internal",
73        ":demangle_internal",
74        "//absl/base",
75        "//absl/base:config",
76        "//absl/base:core_headers",
77        "//absl/base:dynamic_annotations",
78        "//absl/base:malloc_internal",
79        "//absl/base:raw_logging_internal",
80    ],
81)
82
83cc_test(
84    name = "symbolize_test",
85    srcs = ["symbolize_test.cc"],
86    copts = ABSL_TEST_COPTS + select({
87        "//absl:windows": ["/Z7"],
88        "//conditions:default": [],
89    }),
90    linkopts = ABSL_DEFAULT_LINKOPTS + select({
91        "//absl:windows": ["/DEBUG"],
92        "//conditions:default": [],
93    }),
94    deps = [
95        ":stack_consumption",
96        ":symbolize",
97        "//absl/base",
98        "//absl/base:core_headers",
99        "//absl/base:raw_logging_internal",
100        "//absl/memory",
101        "@com_google_googletest//:gtest",
102    ],
103)
104
105cc_library(
106    name = "examine_stack",
107    srcs = [
108        "internal/examine_stack.cc",
109    ],
110    hdrs = [
111        "internal/examine_stack.h",
112    ],
113    copts = ABSL_DEFAULT_COPTS,
114    linkopts = ABSL_DEFAULT_LINKOPTS,
115    visibility = ["//visibility:private"],
116    deps = [
117        ":stacktrace",
118        ":symbolize",
119        "//absl/base:config",
120        "//absl/base:core_headers",
121        "//absl/base:raw_logging_internal",
122    ],
123)
124
125cc_library(
126    name = "failure_signal_handler",
127    srcs = ["failure_signal_handler.cc"],
128    hdrs = ["failure_signal_handler.h"],
129    copts = ABSL_DEFAULT_COPTS,
130    linkopts = ABSL_DEFAULT_LINKOPTS,
131    deps = [
132        ":examine_stack",
133        ":stacktrace",
134        "//absl/base",
135        "//absl/base:config",
136        "//absl/base:core_headers",
137        "//absl/base:errno_saver",
138        "//absl/base:raw_logging_internal",
139    ],
140)
141
142cc_test(
143    name = "failure_signal_handler_test",
144    srcs = ["failure_signal_handler_test.cc"],
145    copts = ABSL_TEST_COPTS,
146    linkopts = select({
147        "//absl:windows": [],
148        "//conditions:default": ["-pthread"],
149    }) + ABSL_DEFAULT_LINKOPTS,
150    deps = [
151        ":failure_signal_handler",
152        ":stacktrace",
153        ":symbolize",
154        "//absl/base:raw_logging_internal",
155        "//absl/strings",
156        "@com_google_googletest//:gtest",
157    ],
158)
159
160cc_library(
161    name = "debugging_internal",
162    srcs = [
163        "internal/address_is_readable.cc",
164        "internal/elf_mem_image.cc",
165        "internal/vdso_support.cc",
166    ],
167    hdrs = [
168        "internal/address_is_readable.h",
169        "internal/elf_mem_image.h",
170        "internal/vdso_support.h",
171    ],
172    copts = ABSL_DEFAULT_COPTS,
173    linkopts = ABSL_DEFAULT_LINKOPTS,
174    deps = [
175        "//absl/base:config",
176        "//absl/base:core_headers",
177        "//absl/base:dynamic_annotations",
178        "//absl/base:errno_saver",
179        "//absl/base:raw_logging_internal",
180    ],
181)
182
183cc_library(
184    name = "demangle_internal",
185    srcs = ["internal/demangle.cc"],
186    hdrs = ["internal/demangle.h"],
187    copts = ABSL_DEFAULT_COPTS,
188    deps = [
189        "//absl/base",
190        "//absl/base:config",
191        "//absl/base:core_headers",
192    ],
193)
194
195cc_test(
196    name = "demangle_test",
197    srcs = ["internal/demangle_test.cc"],
198    copts = ABSL_TEST_COPTS,
199    linkopts = ABSL_DEFAULT_LINKOPTS,
200    deps = [
201        ":demangle_internal",
202        ":stack_consumption",
203        "//absl/base:core_headers",
204        "//absl/base:raw_logging_internal",
205        "//absl/memory",
206        "@com_google_googletest//:gtest_main",
207    ],
208)
209
210cc_library(
211    name = "leak_check",
212    srcs = ["leak_check.cc"],
213    hdrs = ["leak_check.h"],
214    linkopts = ABSL_DEFAULT_LINKOPTS,
215    deps = [
216        "//absl/base:config",
217        "//absl/base:core_headers",
218    ],
219)
220
221# Adding a dependency to leak_check_disable will disable
222# sanitizer leak checking (asan/lsan) in a test without
223# the need to mess around with build features.
224cc_library(
225    name = "leak_check_disable",
226    srcs = ["leak_check_disable.cc"],
227    linkopts = ABSL_DEFAULT_LINKOPTS,
228    linkstatic = 1,
229    deps = ["//absl/base:config"],
230    alwayslink = 1,
231)
232
233# These targets exists for use in tests only, explicitly configuring the
234# LEAK_SANITIZER macro. It must be linked with -fsanitize=leak for lsan.
235ABSL_LSAN_LINKOPTS = select({
236    "//absl:llvm_compiler": ["-fsanitize=leak"],
237    "//conditions:default": [],
238})
239
240cc_library(
241    name = "leak_check_api_enabled_for_testing",
242    testonly = 1,
243    srcs = ["leak_check.cc"],
244    hdrs = ["leak_check.h"],
245    copts = select({
246        "//absl:llvm_compiler": ["-DLEAK_SANITIZER"],
247        "//conditions:default": [],
248    }),
249    linkopts = ABSL_DEFAULT_LINKOPTS,
250    visibility = ["//visibility:private"],
251    deps = [
252        "//absl/base:config",
253    ],
254)
255
256cc_library(
257    name = "leak_check_api_disabled_for_testing",
258    testonly = 1,
259    srcs = ["leak_check.cc"],
260    hdrs = ["leak_check.h"],
261    copts = ["-ULEAK_SANITIZER"],
262    linkopts = ABSL_DEFAULT_LINKOPTS,
263    visibility = ["//visibility:private"],
264    deps = [
265        "//absl/base:config",
266    ],
267)
268
269cc_test(
270    name = "leak_check_test",
271    srcs = ["leak_check_test.cc"],
272    copts = select({
273        "//absl:llvm_compiler": ["-DABSL_EXPECT_LEAK_SANITIZER"],
274        "//conditions:default": [],
275    }),
276    linkopts = ABSL_LSAN_LINKOPTS + ABSL_DEFAULT_LINKOPTS,
277    tags = ["notsan"],
278    deps = [
279        ":leak_check_api_enabled_for_testing",
280        "//absl/base",
281        "@com_google_googletest//:gtest_main",
282    ],
283)
284
285cc_test(
286    name = "leak_check_no_lsan_test",
287    srcs = ["leak_check_test.cc"],
288    copts = ["-UABSL_EXPECT_LEAK_SANITIZER"],
289    linkopts = ABSL_DEFAULT_LINKOPTS,
290    tags = ["noasan"],
291    deps = [
292        ":leak_check_api_disabled_for_testing",
293        "//absl/base",  # for raw_logging
294        "@com_google_googletest//:gtest_main",
295    ],
296)
297
298# Test that leak checking is skipped when lsan is enabled but
299# ":leak_check_disable" is linked in.
300#
301# This test should fail in the absence of a dependency on ":leak_check_disable"
302cc_test(
303    name = "disabled_leak_check_test",
304    srcs = ["leak_check_fail_test.cc"],
305    linkopts = ABSL_LSAN_LINKOPTS + ABSL_DEFAULT_LINKOPTS,
306    tags = ["notsan"],
307    deps = [
308        ":leak_check_api_enabled_for_testing",
309        ":leak_check_disable",
310        "//absl/base",
311        "@com_google_googletest//:gtest_main",
312    ],
313)
314
315cc_library(
316    name = "stack_consumption",
317    testonly = 1,
318    srcs = ["internal/stack_consumption.cc"],
319    hdrs = ["internal/stack_consumption.h"],
320    copts = ABSL_DEFAULT_COPTS,
321    linkopts = ABSL_DEFAULT_LINKOPTS,
322    visibility = ["//visibility:private"],
323    deps = [
324        "//absl/base:config",
325        "//absl/base:core_headers",
326        "//absl/base:raw_logging_internal",
327    ],
328)
329
330cc_test(
331    name = "stack_consumption_test",
332    srcs = ["internal/stack_consumption_test.cc"],
333    copts = ABSL_TEST_COPTS,
334    linkopts = ABSL_DEFAULT_LINKOPTS,
335    deps = [
336        ":stack_consumption",
337        "//absl/base:core_headers",
338        "//absl/base:raw_logging_internal",
339        "@com_google_googletest//:gtest_main",
340    ],
341)
342