cc_toolchain_sanitizer_features.bzl 4.0 KB

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