1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#ifndef LIBPANDABASE_UTILS_ASAN_INTERFACE_H
17b1994897Sopenharmony_ci#define LIBPANDABASE_UTILS_ASAN_INTERFACE_H
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci#if defined(__has_feature)
20b1994897Sopenharmony_ci// for clang
21b1994897Sopenharmony_ci#if __has_feature(address_sanitizer)
22b1994897Sopenharmony_ci#define PANDA_ASAN_ON
23b1994897Sopenharmony_ci#endif
24b1994897Sopenharmony_ci#else
25b1994897Sopenharmony_ci#if defined(__SANITIZE_ADDRESS__)
26b1994897Sopenharmony_ci// for gnu compiler
27b1994897Sopenharmony_ci#define PANDA_ASAN_ON
28b1994897Sopenharmony_ci#endif
29b1994897Sopenharmony_ci#endif
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_ci#ifdef PANDA_ASAN_ON
32b1994897Sopenharmony_ciextern "C" {
33b1994897Sopenharmony_ci// Marks memory region [addr, addr+size) as unaddressable.
34b1994897Sopenharmony_ci// NOLINTNEXTLINE(readability-identifier-naming)
35b1994897Sopenharmony_civoid __asan_poison_memory_region(void const volatile *addr, size_t size) __attribute__((visibility("default")));
36b1994897Sopenharmony_ci// Marks memory region [addr, addr+size) as addressable.
37b1994897Sopenharmony_ci// NOLINTNEXTLINE(readability-identifier-naming)
38b1994897Sopenharmony_civoid __asan_unpoison_memory_region(void const volatile *addr, size_t size) __attribute__((visibility("default")));
39b1994897Sopenharmony_ci}
40b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
41b1994897Sopenharmony_ci#define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region((addr), (size))
42b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
43b1994897Sopenharmony_ci#define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region((addr), (size))
44b1994897Sopenharmony_ci
45b1994897Sopenharmony_ci// Use attribute ATTRIBUTE_NO_SANITIZE_ADDRESS to fix an issue with concurrent POISON/UNPOISON
46b1994897Sopenharmony_ci// during accessing class fields from the class methods during MT ASAN runs.
47b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
48b1994897Sopenharmony_ci#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
49b1994897Sopenharmony_ci#else
50b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
51b1994897Sopenharmony_ci#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
52b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
53b1994897Sopenharmony_ci#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
54b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
55b1994897Sopenharmony_ci#define ATTRIBUTE_NO_SANITIZE_ADDRESS
56b1994897Sopenharmony_ci#endif
57b1994897Sopenharmony_ci
58b1994897Sopenharmony_ci#endif  // LIBPANDABASE_UTILS_ASAN_INTERFACE_H
59