FindUBSan.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #
  2. # The MIT License (MIT)
  3. #
  4. # Copyright (c) 2013 Matthew Arsenault
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. #
  24. # Check if the compiler supports a working ubsan. Provides a UBSan
  25. # build type, which is essentially Debug + ubsan. The flag can be used
  26. # independently to compose it with other build types or sanitizers.
  27. #
  28. # Sets these variables:
  29. #
  30. # HAVE_UNDEFINED_BEHAVIOR_SANITIZER - True or false if the UBSan is available
  31. # UNDEFINED_BEHAVIOR_SANITIZER_FLAG - Flag to add to compiler to use ubsan if supported
  32. #
  33. # CMAKE_C_FLAGS_UBSAN - Flags to use for C with ubsan
  34. # CMAKE_CXX_FLAGS_UBSAN - Flags to use for C++ with ubsan
  35. ##
  36. #
  37. include(CheckCXXCompilerFlag)
  38. include(CheckCXXSourceRuns)
  39. # Set -Werror to catch "argument unused during compilation" warnings
  40. set(CMAKE_REQUIRED_FLAGS "-Werror")
  41. check_cxx_compiler_flag("-fsanitize=undefined" HAVE_FLAG_SANITIZE_UNDEFINED)
  42. if(HAVE_FLAG_SANITIZE_UNDEFINED)
  43. set(UNDEFINED_BEHAVIOR_SANITIZER_FLAG "-fsanitize=undefined")
  44. else()
  45. set(HAVE_UNDEFINED_BEHAVIOR_SANITIZER FALSE)
  46. return()
  47. endif()
  48. unset(CMAKE_REQUIRED_FLAGS)
  49. # It isn't sufficient to check if the flag works since the
  50. # check_c_compiler_flag test doesn't link the output.
  51. #
  52. # Most clang packages ship broken packages (the autotools build
  53. # produces a broken package which doesn't include the ubsan
  54. # compiler-rt, so check that it actually works with a linked program
  55. # before trying to use it
  56. set(CMAKE_REQUIRED_FLAGS "${UNDEFINED_BEHAVIOR_SANITIZER_FLAG} -Wno-error=delete-non-virtual-dtor")
  57. check_cxx_source_runs(
  58. "
  59. #include <cstdio>
  60. #include <cstdlib>
  61. #include <iostream>
  62. class BarB
  63. {
  64. public:
  65. float y;
  66. /* Include something that uses a virtual function. The symbols
  67. that are broken on current OS X libc++ involve this */
  68. virtual int arst(int o)
  69. {
  70. return 4 + o;
  71. }
  72. };
  73. /* Just include something that ubsan will need to check */
  74. int main(int argc, const char* argv[])
  75. {
  76. BarB* b = new BarB();
  77. if (argc > 1)
  78. {
  79. fputs(argv[atoi(argv[1])], stdout);
  80. std::cout << b->arst(atoi(argv[1]));
  81. }
  82. delete b;
  83. return 0;
  84. }
  85. "
  86. HAVE_UNDEFINED_BEHAVIOR_SANITIZER)
  87. unset(CMAKE_REQUIRED_FLAGS)
  88. if(NOT HAVE_UNDEFINED_BEHAVIOR_SANITIZER)
  89. return()
  90. endif()
  91. set(CMAKE_C_FLAGS_UBSAN "-O0 -g ${UNDEFINED_BEHAVIOR_SANITIZER_FLAG} -fno-omit-frame-pointer"
  92. CACHE STRING "Flags used by the C compiler during UBSan builds."
  93. FORCE)
  94. set(CMAKE_CXX_FLAGS_UBSAN "-O0 -g ${UNDEFINED_BEHAVIOR_SANITIZER_FLAG} -fno-omit-frame-pointer"
  95. CACHE STRING "Flags used by the C++ compiler during UBSan builds."
  96. FORCE)
  97. set(CMAKE_EXE_LINKER_FLAGS_UBSAN "${UNDEFINED_BEHAVIOR_SANITIZER_FLAG}"
  98. CACHE STRING "Flags used for linking binaries during UBSan builds."
  99. FORCE)
  100. set(CMAKE_SHARED_LINKER_FLAGS_UBSAN "${UNDEFINED_BEHAVIOR_SANITIZER_FLAG}"
  101. CACHE STRING "Flags used by the shared libraries linker during UBSan builds."
  102. FORCE)
  103. mark_as_advanced(CMAKE_C_FLAGS_UBSAN
  104. CMAKE_CXX_FLAGS_UBSAN
  105. CMAKE_EXE_LINKER_FLAGS_UBSAN
  106. CMAKE_SHARED_LINKER_FLAGS_UBSAN)