25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.2 KiB

  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. check_cxx_compiler_flag("-fcatch-undefined-behavior" HAVE_FLAG_CATCH_UNDEFINED_BEHAVIOR)
  43. if(HAVE_FLAG_SANITIZE_UNDEFINED)
  44. set(UNDEFINED_BEHAVIOR_SANITIZER_FLAG "-fsanitize=undefined")
  45. elseif(HAVE_FLAG_CATCH_UNDEFINED_BEHAVIOR)
  46. set(UNDEFINED_BEHAVIOR_SANITIZER_FLAG "-fcatch-undefined-behavior")
  47. else()
  48. set(HAVE_UNDEFINED_BEHAVIOR_SANITIZER FALSE)
  49. return()
  50. endif()
  51. unset(CMAKE_REQUIRED_FLAGS)
  52. # It isn't sufficient to check if the flag works since the
  53. # check_c_compiler_flag test doesn't link the output.
  54. #
  55. # Most clang packages ship broken packages (the autotools build
  56. # produces a broken package which doesn't include the ubsan
  57. # compiler-rt, so check that it actually works with a linked program
  58. # before trying to use it
  59. set(CMAKE_REQUIRED_FLAGS "${UNDEFINED_BEHAVIOR_SANITIZER_FLAG} -Wno-error=delete-non-virtual-dtor")
  60. check_cxx_source_runs(
  61. "
  62. #include <cstdio>
  63. #include <cstdlib>
  64. #include <iostream>
  65. class BarB
  66. {
  67. public:
  68. float y;
  69. /* Include something that uses a virtual function. The symbols
  70. that are broken on current OS X libc++ involve this */
  71. virtual int arst(int o)
  72. {
  73. return 4 + o;
  74. }
  75. };
  76. /* Just include something that ubsan will need to check */
  77. int main(int argc, const char* argv[])
  78. {
  79. BarB* b = new BarB();
  80. if (argc > 1)
  81. {
  82. fputs(argv[atoi(argv[1])], stdout);
  83. std::cout << b->arst(atoi(argv[1]));
  84. }
  85. delete b;
  86. return 0;
  87. }
  88. "
  89. HAVE_UNDEFINED_BEHAVIOR_SANITIZER)
  90. unset(CMAKE_REQUIRED_FLAGS)
  91. if(NOT HAVE_UNDEFINED_BEHAVIOR_SANITIZER)
  92. return()
  93. endif()
  94. set(CMAKE_C_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_CXX_FLAGS_UBSAN "-O0 -g ${UNDEFINED_BEHAVIOR_SANITIZER_FLAG} -fno-omit-frame-pointer"
  98. CACHE STRING "Flags used by the C++ compiler during UBSan builds."
  99. FORCE)
  100. set(CMAKE_EXE_LINKER_FLAGS_UBSAN "${UNDEFINED_BEHAVIOR_SANITIZER_FLAG}"
  101. CACHE STRING "Flags used for linking binaries during UBSan builds."
  102. FORCE)
  103. set(CMAKE_SHARED_LINKER_FLAGS_UBSAN "${UNDEFINED_BEHAVIOR_SANITIZER_FLAG}"
  104. CACHE STRING "Flags used by the shared libraries linker during UBSan builds."
  105. FORCE)
  106. mark_as_advanced(CMAKE_C_FLAGS_UBSAN
  107. CMAKE_CXX_FLAGS_UBSAN
  108. CMAKE_EXE_LINKER_FLAGS_UBSAN
  109. CMAKE_SHARED_LINKER_FLAGS_UBSAN)