1 /* 2 * Copyright (c) 2021 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 PANDA_LIBPANDABASE_UTILS_ASAN_INTERFACE_H_ 17 #define PANDA_LIBPANDABASE_UTILS_ASAN_INTERFACE_H_ 18 19 #if defined(__has_feature) 20 // for clang 21 #if __has_feature(address_sanitizer) 22 #define PANDA_ASAN_ON 23 #endif 24 #else 25 #if defined(__SANITIZE_ADDRESS__) 26 // for gnu compiler 27 #define PANDA_ASAN_ON 28 #endif 29 #endif 30 31 #ifdef PANDA_ASAN_ON 32 extern "C" { 33 // Marks memory region [addr, addr+size) as unaddressable. 34 // NOLINTNEXTLINE(readability-identifier-naming) 35 void __asan_poison_memory_region(void const volatile *addr, size_t size) __attribute__((visibility("default"))); 36 // Marks memory region [addr, addr+size) as addressable. 37 // NOLINTNEXTLINE(readability-identifier-naming) 38 void __asan_unpoison_memory_region(void const volatile *addr, size_t size) __attribute__((visibility("default"))); 39 } 40 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 41 #define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region((addr), (size)) 42 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 43 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region((addr), (size)) 44 45 // Uses attribute ATTRIBUTE_NO_SANITIZE_ADDRESS to fix an issue with concurrent POISON/UNPOISON 46 // during accessing class fields from the class methods during MT ASAN runs. 47 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 48 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) 49 #else 50 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 51 #define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) 52 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 53 #define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) 54 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 55 #define ATTRIBUTE_NO_SANITIZE_ADDRESS 56 #endif 57 58 #endif // PANDA_LIBPANDABASE_UTILS_ASAN_INTERFACE_H_ 59