1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMON_COMPONENTS_BASE_ARK_SANITIZER_H 17 #define COMMON_COMPONENTS_BASE_ARK_SANITIZER_H 18 19 #if (defined(__has_feature) && __has_feature(address_sanitizer)) || defined(__SANITIZE_ADDRESS__) 20 #define HAS_SANITIZER 21 #endif 22 23 #if defined(HAS_SANITIZER) && defined(RUN_WITH_ASAN) 24 #define ARK_ASAN_ON 25 #endif 26 27 #if defined(USE_ASAN) && defined(ARK_ASAN_ON) 28 extern "C" { 29 // NOLINTNEXTLINE(readability-identifier-naming) 30 void __asan_poison_memory_region(void const volatile *addr, size_t size) __attribute__((visibility("default"))); 31 // NOLINTNEXTLINE(readability-identifier-naming) 32 void __asan_unpoison_memory_region(void const volatile *addr, size_t size) __attribute__((visibility("default"))); 33 } 34 35 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 36 #ifndef ASAN_POISON_MEMORY_REGION 37 #define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region((addr), (size)) 38 #endif 39 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 40 #ifndef ASAN_UNPOISON_MEMORY_REGION 41 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region((addr), (size)) 42 #endif 43 #elif defined(USE_HWASAN) 44 extern "C" { 45 // NOLINTNEXTLINE(readability-identifier-naming) 46 void __hwasan_tag_memory(void const volatile *p, unsigned char tag, size_t size) __attribute__((visibility("default"))); 47 // NOLINTNEXTLINE(readability-identifier-naming) 48 void* __hwasan_tag_pointer(void const volatile *addr, unsigned char tag) __attribute__((visibility("default"))); 49 } 50 51 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 52 #ifndef ASAN_POISON_MEMORY_REGION 53 #define ASAN_POISON_MEMORY_REGION(addr, size) \ 54 do { \ 55 __hwasan_tag_memory(static_cast<const volatile void*>(addr), 0xFF, (size)); \ 56 } while (0) 57 #endif 58 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 59 #ifndef ASAN_UNPOISON_MEMORY_REGION 60 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ 61 do { \ 62 __hwasan_tag_memory(static_cast<const volatile void*>(addr), 0, (size)); \ 63 } while (0) 64 #endif 65 66 #else 67 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 68 #ifdef ASAN_POISON_MEMORY_REGION 69 #undef ASAN_POISON_MEMORY_REGION 70 #endif 71 #define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) 72 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 73 #ifdef ASAN_UNPOISON_MEMORY_REGION 74 #undef ASAN_UNPOISON_MEMORY_REGION 75 #endif 76 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) 77 #endif 78 79 #endif // COMMON_COMPONENTS_BASE_ARK_SANITIZER_H 80