rustc_flags.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from enum import Enum
  10. """rustc flags."""
  11. # Flags to enable code coverage.
  12. #
  13. # Note that because clusterd gets terminated by a signal in most
  14. # cases, it needs to use LLVM's continuous profiling mode, and though
  15. # the documentation is contradictory about this, on Linux this
  16. # requires the additional -runtime-counter-relocation flag or you'll
  17. # get errors of the form "__llvm_profile_counter_bias is undefined"
  18. # and no profiles will be written.
  19. coverage = [
  20. "-Cinstrument-coverage",
  21. "-Cllvm-args=-runtime-counter-relocation",
  22. ]
  23. class Sanitizer(Enum):
  24. """What sanitizer to use"""
  25. address = "address"
  26. """The AddressSanitizer, see https://clang.llvm.org/docs/AddressSanitizer.html"""
  27. hwaddress = "hwaddress"
  28. """The HWAddressSanitizer, see https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html"""
  29. cfi = "cfi"
  30. """Control Flow Integrity, see https://clang.llvm.org/docs/ControlFlowIntegrity.html"""
  31. thread = "thread"
  32. """The ThreadSanitizer, see https://clang.llvm.org/docs/ThreadSanitizer.html"""
  33. leak = "leak"
  34. """The LeakSanitizer, see https://clang.llvm.org/docs/LeakSanitizer.html"""
  35. undefined = "undefined"
  36. """The UndefinedBehavior, see https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html"""
  37. none = "none"
  38. """No sanitizer"""
  39. def __str__(self) -> str:
  40. return self.value
  41. def bazel_flags(self) -> list[str]:
  42. """Return a set of Bazel flags to enable the sanitizer."""
  43. if self == Sanitizer.address:
  44. return ["--config=asan"]
  45. elif self == Sanitizer.hwaddress:
  46. return ["--config=hwasan"]
  47. elif self == Sanitizer.cfi:
  48. return ["--config=cfi"]
  49. elif self == Sanitizer.thread:
  50. return ["--config=tsan"]
  51. elif self == Sanitizer.leak:
  52. return ["--config=leak"]
  53. elif self == Sanitizer.undefined:
  54. return ["--config=undefined"]
  55. else:
  56. return []
  57. sanitizer = {
  58. Sanitizer.address: [
  59. "-Zsanitizer=address",
  60. "-Cllvm-args=-asan-use-after-scope",
  61. "-Cllvm-args=-asan-use-after-return=always",
  62. # "-Cllvm-args=-asan-stack=false", # Remove when database-issues#7468 is fixed
  63. "-Cdebug-assertions=on",
  64. "-Clink-arg=-fuse-ld=lld", # access beyond end of merged section
  65. "-Clinker=clang++",
  66. ],
  67. Sanitizer.hwaddress: [
  68. "-Zsanitizer=hwaddress",
  69. "-Ctarget-feature=+tagged-globals",
  70. "-Clink-arg=-fuse-ld=lld", # access beyond end of merged section
  71. "-Clinker=clang++",
  72. ],
  73. Sanitizer.cfi: [
  74. "-Zsanitizer=cfi",
  75. "-Clto", # error: `-Zsanitizer=cfi` requires `-Clto` or `-Clinker-plugin-lto`
  76. "-Clink-arg=-fuse-ld=lld", # access beyond end of merged section
  77. "-Clinker=clang++",
  78. ],
  79. Sanitizer.thread: [
  80. "-Zsanitizer=thread",
  81. "-Clink-arg=-fuse-ld=lld", # access beyond end of merged section
  82. "-Clinker=clang++",
  83. ],
  84. Sanitizer.leak: [
  85. "-Zsanitizer=leak",
  86. "-Clink-arg=-fuse-ld=lld", # access beyond end of merged section
  87. "-Clinker=clang++",
  88. ],
  89. Sanitizer.undefined: ["-Clink-arg=-fsanitize=undefined", "-Clinker=clang++"],
  90. }
  91. sanitizer_cflags = {
  92. Sanitizer.address: ["-fsanitize=address"],
  93. Sanitizer.hwaddress: ["-fsanitize=hwaddress"],
  94. Sanitizer.cfi: ["-fsanitize=cfi"],
  95. Sanitizer.thread: ["-fsanitize=thread"],
  96. Sanitizer.leak: ["-fsanitize=leak"],
  97. Sanitizer.undefined: ["-fsanitize=undefined"],
  98. }