1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBARTBASE_BASE_MEMORY_TOOL_H_
18 #define ART_LIBARTBASE_BASE_MEMORY_TOOL_H_
19 
20 #include <stddef.h>
21 
22 namespace art {
23 
24 #if !defined(__has_feature)
25 # define __has_feature(x) 0
26 #endif
27 
28 #if __has_feature(address_sanitizer)
29 
30 # include <sanitizer/asan_interface.h>
31 # define ADDRESS_SANITIZER
32 
33 # ifdef ART_ENABLE_ADDRESS_SANITIZER
34 #  define MEMORY_TOOL_MAKE_NOACCESS(p, s) __asan_poison_memory_region(p, s)
35 #  define MEMORY_TOOL_MAKE_UNDEFINED(p, s) __asan_unpoison_memory_region(p, s)
36 #  define MEMORY_TOOL_MAKE_DEFINED(p, s) __asan_unpoison_memory_region(p, s)
37 constexpr bool kMemoryToolIsAvailable = true;
38 # else
39 #  define MEMORY_TOOL_MAKE_NOACCESS(p, s) do { (void)(p); (void)(s); } while (0)
40 #  define MEMORY_TOOL_MAKE_UNDEFINED(p, s) do { (void)(p); (void)(s); } while (0)
41 #  define MEMORY_TOOL_MAKE_DEFINED(p, s) do { (void)(p); (void)(s); } while (0)
42 constexpr bool kMemoryToolIsAvailable = false;
43 # endif
44 
45 extern "C" void __asan_handle_no_return();
46 
47 # define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address, noinline))
48 # define MEMORY_TOOL_HANDLE_NO_RETURN __asan_handle_no_return()
49 constexpr bool kRunningOnMemoryTool = true;
50 constexpr bool kMemoryToolDetectsLeaks = true;
51 constexpr bool kMemoryToolAddsRedzones = true;
52 constexpr size_t kMemoryToolStackGuardSizeScale = 2;
53 
54 #else
55 
56 # define MEMORY_TOOL_MAKE_NOACCESS(p, s) do { (void)(p); (void)(s); } while (0)
57 # define MEMORY_TOOL_MAKE_UNDEFINED(p, s) do { (void)(p); (void)(s); } while (0)
58 # define MEMORY_TOOL_MAKE_DEFINED(p, s) do { (void)(p); (void)(s); } while (0)
59 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
60 # define MEMORY_TOOL_HANDLE_NO_RETURN do { } while (0)
61 constexpr bool kRunningOnMemoryTool = false;
62 constexpr bool kMemoryToolIsAvailable = false;
63 constexpr bool kMemoryToolDetectsLeaks = false;
64 constexpr bool kMemoryToolAddsRedzones = false;
65 constexpr size_t kMemoryToolStackGuardSizeScale = 1;
66 
67 #endif
68 
69 #if __has_feature(hwaddress_sanitizer)
70 # define HWADDRESS_SANITIZER
71 // NB: The attribute also implies NO_INLINE. If inlined, the hwasan attribute would be lost.
72 //     If method is also separately marked as ALWAYS_INLINE, the NO_INLINE takes precedence.
73 # define ATTRIBUTE_NO_SANITIZE_HWADDRESS __attribute__((no_sanitize("hwaddress"), noinline))
74 #else
75 # define ATTRIBUTE_NO_SANITIZE_HWADDRESS
76 #endif
77 
78 // Removes the hwasan tag from the pointer (the top eight bits).
79 // Those bits are used for verification by hwasan and they are ignored by normal ARM memory ops.
80 template<typename PtrType>
81 static inline PtrType* HWASanUntag(PtrType* p) {
82 #if __has_feature(hwaddress_sanitizer) && defined(__aarch64__)
83   return reinterpret_cast<PtrType*>(reinterpret_cast<uintptr_t>(p) & ((1ULL << 56) - 1));
84 #else
85   return p;
86 #endif
87 }
88 
89 }  // namespace art
90 
91 #endif  // ART_LIBARTBASE_BASE_MEMORY_TOOL_H_
92