cc_toolchain_sanitizer_features.bzl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """Definitions of sanitizer-related `cc_toolchain_config` features."""
  5. load(
  6. "@rules_cc//cc:cc_toolchain_config_lib.bzl",
  7. "feature",
  8. "feature_set",
  9. "flag_group",
  10. "flag_set",
  11. "with_feature_set",
  12. )
  13. load(
  14. ":cc_toolchain_actions.bzl",
  15. "all_compile_actions",
  16. "all_link_actions",
  17. )
  18. sanitizer_common_flags = feature(
  19. name = "sanitizer_common_flags",
  20. implies = ["minimal_debug_info_flags", "preserve_call_stacks"],
  21. flag_sets = [flag_set(
  22. actions = all_link_actions,
  23. flag_groups = [flag_group(flags = ["-static-libsan"])],
  24. with_features = [
  25. with_feature_set(["linux_target"]),
  26. with_feature_set(["freebsd_target"]),
  27. ],
  28. )],
  29. )
  30. asan = feature(
  31. name = "asan",
  32. implies = ["sanitizer_common_flags"],
  33. flag_sets = [flag_set(
  34. actions = all_compile_actions + all_link_actions,
  35. flag_groups = [flag_group(flags = [
  36. "-fsanitize=address,undefined,nullability",
  37. "-fsanitize-address-use-after-scope",
  38. # Outlining is almost always the right tradeoff for our
  39. # sanitizer usage where we're more pressured on generated code
  40. # size than runtime performance.
  41. "-fsanitize-address-outline-instrumentation",
  42. # We don't need the recovery behavior of UBSan as we expect
  43. # builds to be clean. Not recovering is a bit cheaper.
  44. "-fno-sanitize-recover=undefined,nullability",
  45. # Don't embed the full path name for files. This limits the size
  46. # and combined with line numbers is unlikely to result in many
  47. # ambiguities.
  48. "-fsanitize-undefined-strip-path-components=-1",
  49. # Needed due to clang AST issues, such as in
  50. # clang/AST/Redeclarable.h line 199.
  51. "-fno-sanitize=vptr",
  52. ])],
  53. )],
  54. )
  55. # A feature that further reduces the generated code size of our the ASan
  56. # feature, but at the cost of lower quality diagnostics. This is enabled
  57. # along with ASan in our fastbuild configuration, but can be disabled
  58. # explicitly to get better error messages.
  59. asan_min_size = feature(
  60. name = "asan_min_size",
  61. requires = [feature_set(["asan"])],
  62. flag_sets = [flag_set(
  63. actions = all_compile_actions + all_link_actions,
  64. flag_groups = [flag_group(flags = [
  65. # Force two UBSan checks that have especially large code size
  66. # cost to use the minimal branch to a trapping instruction model
  67. # instead of the full diagnostic.
  68. "-fsanitize-trap=alignment,null",
  69. ])],
  70. )],
  71. )
  72. fuzzer = feature(
  73. name = "fuzzer",
  74. flag_sets = [flag_set(
  75. actions = all_compile_actions + all_link_actions,
  76. flag_groups = [flag_group(flags = [
  77. "-fsanitize=fuzzer-no-link",
  78. ])],
  79. )],
  80. )
  81. sanitizer_workarounds = feature(
  82. name = "sanitizer_workarounds",
  83. enabled = True,
  84. requires = [feature_set(["asan"])],
  85. flag_sets = [flag_set(
  86. actions = all_compile_actions + all_link_actions,
  87. flag_groups = [flag_group(flags = [
  88. # Likely due to being unable to use the static-linked and up-to-date
  89. # sanitizer runtimes, we have to disable this sanitizer on macOS.
  90. "-fno-sanitize=function",
  91. ])],
  92. with_features = [with_feature_set(["macos_target"])],
  93. )],
  94. )
  95. # Note that the order of features is significant in this list and determines the
  96. # relative order of flags from the features listed.
  97. sanitizer_features = [
  98. sanitizer_common_flags,
  99. asan,
  100. asan_min_size,
  101. fuzzer,
  102. # Note that the workarounds must come last here to override earlier flags.
  103. sanitizer_workarounds,
  104. ]