RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
AddressSanitizer.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2018 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
23#include <cstddef>
24
25// see http://clang.llvm.org/docs/LanguageExtensions.html
26#ifndef __has_feature // Optional of course.
27#define __has_feature(x) 0 // Compatibility with non-clang compilers.
28#endif
29#ifndef __has_extension
30#define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
31#endif
32
33#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
34#include <sanitizer/asan_interface.h>
35#endif
36
37namespace rawspeed {
38
39struct ASan final {
40 // Do not instantiate.
41 ASan() = delete;
42 ASan(const ASan&) = delete;
43 ASan(ASan&&) = delete;
44 ASan& operator=(const ASan&) = delete;
45 ASan& operator=(ASan&&) = delete;
46 ~ASan() = delete;
47
48 // Marks memory region [addr, addr+size) as unaddressable.
49 static void PoisonMemoryRegion(const volatile void* addr, size_t size);
50 // Marks memory region [addr, addr+size) as addressable.
51 static void UnPoisonMemoryRegion(const volatile void* addr, size_t size);
52
53 // If at least one byte in [beg, beg+size) is poisoned, return true
54 // Otherwise return 0.
55 static bool RegionIsPoisoned(const volatile void* addr, size_t size);
56};
57
58#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
59inline void ASan::PoisonMemoryRegion(const volatile void* addr, size_t size) {
60 __asan_poison_memory_region(addr, size);
61}
62inline void ASan::UnPoisonMemoryRegion(const volatile void* addr, size_t size) {
63 __asan_unpoison_memory_region(addr, size);
64}
65inline bool ASan::RegionIsPoisoned(const volatile void* addr, size_t size) {
66 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
67 auto* beg = const_cast<void*>(addr);
68 return nullptr != __asan_region_is_poisoned(beg, size);
69}
70#else
71inline void ASan::PoisonMemoryRegion([[maybe_unused]] const volatile void* addr,
72 [[maybe_unused]] size_t size) {
73 // If we are building without ASan, then there is no way to have a non-empty
74 // body of this function. It's better than to have a macros, or to use
75 // preprocessor in every place it is called.
76}
77inline void
78ASan::UnPoisonMemoryRegion([[maybe_unused]] const volatile void* addr,
79 [[maybe_unused]] size_t size) {
80 // If we are building without ASan, then there is no way to have a non-empty
81 // body of this function. It's better than to have a macros, or to use
82 // preprocessor in every place it is called.
83}
84inline bool ASan::RegionIsPoisoned([[maybe_unused]] const volatile void* addr,
85 [[maybe_unused]] size_t size) {
86 // If we are building without ASan, then there is no way to have a poisoned
87 // memory region.
88 return false;
89}
90#endif
91
92} // namespace rawspeed
static bool RegionIsPoisoned(const volatile void *addr, size_t size)
ASan(const ASan &)=delete
ASan(ASan &&)=delete
~ASan()=delete
ASan & operator=(const ASan &)=delete
static void PoisonMemoryRegion(const volatile void *addr, size_t size)
static void UnPoisonMemoryRegion(const volatile void *addr, size_t size)
ASan & operator=(ASan &&)=delete