1b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
3b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
4b1994897Sopenharmony_ci# You may obtain a copy of the License at
5b1994897Sopenharmony_ci#
6b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
7b1994897Sopenharmony_ci#
8b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
9b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
10b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
12b1994897Sopenharmony_ci# limitations under the License.
13b1994897Sopenharmony_ci
14b1994897Sopenharmony_cioption(PANDA_ENABLE_SANITIZE_TRAP "Enable sanitizer trap generation" false)
15b1994897Sopenharmony_ciif (CMAKE_BUILD_TYPE MATCHES Release)
16b1994897Sopenharmony_ci    set(PANDA_ENABLE_SANITIZE_TRAP true)
17b1994897Sopenharmony_ciendif ()
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_cioption(PANDA_ENABLE_ADDRESS_SANITIZER "Address sanitizer enable" false)
20b1994897Sopenharmony_cioption(PANDA_ENABLE_UNDEFINED_BEHAVIOR_SANITIZER "Undefined behavior sanitizer enable" false)
21b1994897Sopenharmony_cioption(PANDA_ENABLE_THREAD_SANITIZER "Thread sanitizer enable" false)
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_ciset(PANDA_SANITIZERS_LIST)
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ciif (PANDA_ENABLE_ADDRESS_SANITIZER)
26b1994897Sopenharmony_ci    message(STATUS "Enabled ASAN")
27b1994897Sopenharmony_ci    list(APPEND PANDA_SANITIZERS_LIST "address")
28b1994897Sopenharmony_ci    add_definitions(-D__SANITIZE_ADDRESS__)
29b1994897Sopenharmony_ciendif()
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_ciif (PANDA_ENABLE_UNDEFINED_BEHAVIOR_SANITIZER)
32b1994897Sopenharmony_ci    message(STATUS "Enabled UBSAN")
33b1994897Sopenharmony_ci    list(APPEND PANDA_SANITIZERS_LIST "undefined")
34b1994897Sopenharmony_ci    # GCC doesn't have built-in macro for UBSAN, bypass by making our own definition.
35b1994897Sopenharmony_ci    add_definitions(-D__SANITIZE_UNDEFINED__)
36b1994897Sopenharmony_ciendif()
37b1994897Sopenharmony_ci
38b1994897Sopenharmony_ciif (PANDA_ENABLE_THREAD_SANITIZER)
39b1994897Sopenharmony_ci    message(STATUS "Enabled TSAN")
40b1994897Sopenharmony_ci    list(APPEND PANDA_SANITIZERS_LIST "thread")
41b1994897Sopenharmony_ci    add_definitions(-D__SANITIZE_THREAD__)
42b1994897Sopenharmony_ciendif()
43b1994897Sopenharmony_ci
44b1994897Sopenharmony_ci# Workaround for http://rnd-gitlab-msc.huawei.com/rus-os-team/virtual-machines-and-tools/panda/-/issues/529
45b1994897Sopenharmony_ci# As of beginning of May 2020 we have checked: gcc 7.5.0, gcc 8.4.0, gcc 9.3.0 and 10.0-rc versions, all have
46b1994897Sopenharmony_ci# some false-positive or another issues when compiling with ASAN or UBSAN in release mode. So, cover this with early-bailout.
47b1994897Sopenharmony_ciif ((PANDA_ENABLE_ADDRESS_SANITIZER OR PANDA_ENABLE_UNDEFINED_BEHAVIOR_SANITIZER) AND
48b1994897Sopenharmony_ci    "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND (CMAKE_BUILD_TYPE MATCHES Release) AND
49b1994897Sopenharmony_ci    (NOT CROSS_VALUES_CONFIG))
50b1994897Sopenharmony_ci        message(FATAL_ERROR "GCC gives false positives in release builds with ASAN or UBSAN, please use clang or another compiler.")
51b1994897Sopenharmony_ci    unset(build_type)
52b1994897Sopenharmony_ciendif()
53b1994897Sopenharmony_ci
54b1994897Sopenharmony_ci# Suppress warning for line `target_link_libraries(${ARG_TARGET} log)`:
55b1994897Sopenharmony_ci# Attempt to add link library "log" to target * which is not built in this directory.
56b1994897Sopenharmony_ciif(POLICY CMP0079)
57b1994897Sopenharmony_ci    cmake_policy(SET CMP0079 NEW)
58b1994897Sopenharmony_ciendif()
59b1994897Sopenharmony_ci
60b1994897Sopenharmony_ci# Add options to build with sanitizers to specified target
61b1994897Sopenharmony_ci#
62b1994897Sopenharmony_ci# Example usage:
63b1994897Sopenharmony_ci#   panda_add_sanitizers(TARGET <name> SANITIZERS <sanitizer1>,<sanitizer2>)
64b1994897Sopenharmony_ci#
65b1994897Sopenharmony_ci# Adds options to build target <name> with <sanitizer1> and <sanitizer2>
66b1994897Sopenharmony_ci#
67b1994897Sopenharmony_ci# Available sanitizers: address, thread, undefined
68b1994897Sopenharmony_ci
69b1994897Sopenharmony_cifunction(panda_add_sanitizers)
70b1994897Sopenharmony_ci    set(prefix ARG)
71b1994897Sopenharmony_ci    set(noValues)
72b1994897Sopenharmony_ci    set(singleValues TARGET)
73b1994897Sopenharmony_ci    set(multiValues SOURCES SANITIZERS)
74b1994897Sopenharmony_ci
75b1994897Sopenharmony_ci    cmake_parse_arguments(${prefix}
76b1994897Sopenharmony_ci                          "${noValues}"
77b1994897Sopenharmony_ci                          "${singleValues}"
78b1994897Sopenharmony_ci                          "${multiValues}"
79b1994897Sopenharmony_ci                          ${ARGN})
80b1994897Sopenharmony_ci
81b1994897Sopenharmony_ci    if(ARG_SANITIZERS)
82b1994897Sopenharmony_ci        set(AVAILABLE_SANITIZERS "address" "undefined" "thread")
83b1994897Sopenharmony_ci        foreach(sanitizer ${ARG_SANITIZERS})
84b1994897Sopenharmony_ci            if(NOT ${sanitizer} IN_LIST AVAILABLE_SANITIZERS)
85b1994897Sopenharmony_ci                message(FATAL_ERROR "Unknown sanitizer: ${sanitizer}")
86b1994897Sopenharmony_ci            endif()
87b1994897Sopenharmony_ci        endforeach()
88b1994897Sopenharmony_ci        string(REPLACE ";" "," ARG_SANITIZERS "${ARG_SANITIZERS}")
89b1994897Sopenharmony_ci        target_compile_options(${ARG_TARGET} PUBLIC "-fsanitize=${ARG_SANITIZERS}" -g)
90b1994897Sopenharmony_ci        if (PANDA_ENABLE_SANITIZE_TRAP)
91b1994897Sopenharmony_ci            target_compile_options(${ARG_TARGET} PUBLIC "-fsanitize-undefined-trap-on-error")
92b1994897Sopenharmony_ci        endif()
93b1994897Sopenharmony_ci        set_target_properties(${ARG_TARGET} PROPERTIES LINK_FLAGS "-fsanitize=${ARG_SANITIZERS}")
94b1994897Sopenharmony_ci
95b1994897Sopenharmony_ci        if (PANDA_TARGET_MOBILE)
96b1994897Sopenharmony_ci            target_link_libraries(${ARG_TARGET} log)
97b1994897Sopenharmony_ci        endif()
98b1994897Sopenharmony_ci    endif()
99b1994897Sopenharmony_ciendfunction()
100