Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_ |
| 19 | |
| 20 | #include "valgrind_malloc_space.h" |
| 21 | |
| 22 | #include <memcheck/memcheck.h> |
| 23 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 24 | #include "valgrind_settings.h" |
| 25 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 26 | namespace art { |
| 27 | namespace gc { |
| 28 | namespace space { |
| 29 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 30 | namespace valgrind_details { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 31 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 32 | template <size_t kValgrindRedZoneBytes, bool kUseObjSizeForUsable> |
| 33 | inline mirror::Object* AdjustForValgrind(void* obj_with_rdz, size_t num_bytes, |
| 34 | size_t bytes_allocated, size_t usable_size, |
| 35 | size_t* bytes_allocated_out, size_t* usable_size_out) { |
| 36 | if (bytes_allocated_out != nullptr) { |
| 37 | *bytes_allocated_out = bytes_allocated; |
| 38 | } |
| 39 | |
| 40 | // This cuts over-provision and is a trade-off between testing the over-provisioning code paths |
| 41 | // vs checking overflows in the regular paths. |
| 42 | if (usable_size_out != nullptr) { |
| 43 | if (kUseObjSizeForUsable) { |
| 44 | *usable_size_out = num_bytes; |
| 45 | } else { |
| 46 | *usable_size_out = usable_size - 2 * kValgrindRedZoneBytes; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Left redzone. |
| 51 | VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes); |
| 52 | |
| 53 | // Make requested memory readable. |
| 54 | // (If the allocator assumes memory is zeroed out, we might get UNDEFINED warnings, so make |
| 55 | // everything DEFINED initially.) |
| 56 | mirror::Object* result = reinterpret_cast<mirror::Object*>( |
| 57 | reinterpret_cast<uint8_t*>(obj_with_rdz) + kValgrindRedZoneBytes); |
| 58 | VALGRIND_MAKE_MEM_DEFINED(result, num_bytes); |
| 59 | |
| 60 | // Right redzone. Assumes that if bytes_allocated > usable_size, then the difference is |
| 61 | // management data at the upper end, and for simplicity we will not protect that. |
| 62 | // At the moment, this fits RosAlloc (no management data in a slot, usable_size == alloc_size) |
| 63 | // and DlMalloc (allocation_size = (usable_size == num_bytes) + 4, 4 is management) |
| 64 | VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<uint8_t*>(result) + num_bytes, |
| 65 | usable_size - (num_bytes + kValgrindRedZoneBytes)); |
| 66 | |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | inline size_t GetObjSizeNoThreadSafety(mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS { |
| 71 | return obj->SizeOf<kVerifyNone>(); |
| 72 | } |
| 73 | |
| 74 | } // namespace valgrind_details |
| 75 | |
| 76 | template <typename S, |
| 77 | size_t kValgrindRedZoneBytes, |
| 78 | bool kAdjustForRedzoneInAllocSize, |
| 79 | bool kUseObjSizeForUsable> |
| 80 | mirror::Object* |
| 81 | ValgrindMallocSpace<S, |
| 82 | kValgrindRedZoneBytes, |
| 83 | kAdjustForRedzoneInAllocSize, |
| 84 | kUseObjSizeForUsable>::AllocWithGrowth( |
| 85 | Thread* self, size_t num_bytes, size_t* bytes_allocated_out, size_t* usable_size_out) { |
| 86 | size_t bytes_allocated; |
| 87 | size_t usable_size; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 88 | void* obj_with_rdz = S::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes, |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 89 | &bytes_allocated, &usable_size); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 90 | if (obj_with_rdz == nullptr) { |
| 91 | return nullptr; |
| 92 | } |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 93 | |
| 94 | return valgrind_details::AdjustForValgrind<kValgrindRedZoneBytes, |
| 95 | kUseObjSizeForUsable>(obj_with_rdz, num_bytes, |
| 96 | bytes_allocated, usable_size, |
| 97 | bytes_allocated_out, |
| 98 | usable_size_out); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 101 | template <typename S, |
| 102 | size_t kValgrindRedZoneBytes, |
| 103 | bool kAdjustForRedzoneInAllocSize, |
| 104 | bool kUseObjSizeForUsable> |
| 105 | mirror::Object* ValgrindMallocSpace<S, |
| 106 | kValgrindRedZoneBytes, |
| 107 | kAdjustForRedzoneInAllocSize, |
| 108 | kUseObjSizeForUsable>::Alloc( |
| 109 | Thread* self, size_t num_bytes, size_t* bytes_allocated_out, size_t* usable_size_out) { |
| 110 | size_t bytes_allocated; |
| 111 | size_t usable_size; |
| 112 | void* obj_with_rdz = S::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes, |
| 113 | &bytes_allocated, &usable_size); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 114 | if (obj_with_rdz == nullptr) { |
| 115 | return nullptr; |
| 116 | } |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 117 | |
| 118 | return valgrind_details::AdjustForValgrind<kValgrindRedZoneBytes, |
| 119 | kUseObjSizeForUsable>(obj_with_rdz, num_bytes, |
| 120 | bytes_allocated, usable_size, |
| 121 | bytes_allocated_out, |
| 122 | usable_size_out); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 125 | template <typename S, |
| 126 | size_t kValgrindRedZoneBytes, |
| 127 | bool kAdjustForRedzoneInAllocSize, |
| 128 | bool kUseObjSizeForUsable> |
Andreas Gampe | 24a5a30 | 2014-11-21 19:45:53 -0800 | [diff] [blame] | 129 | mirror::Object* ValgrindMallocSpace<S, |
| 130 | kValgrindRedZoneBytes, |
| 131 | kAdjustForRedzoneInAllocSize, |
| 132 | kUseObjSizeForUsable>::AllocThreadUnsafe( |
| 133 | Thread* self, size_t num_bytes, size_t* bytes_allocated_out, size_t* usable_size_out) { |
| 134 | size_t bytes_allocated; |
| 135 | size_t usable_size; |
| 136 | void* obj_with_rdz = S::AllocThreadUnsafe(self, num_bytes + 2 * kValgrindRedZoneBytes, |
| 137 | &bytes_allocated, &usable_size); |
| 138 | if (obj_with_rdz == nullptr) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | return valgrind_details::AdjustForValgrind<kValgrindRedZoneBytes, |
| 143 | kUseObjSizeForUsable>(obj_with_rdz, num_bytes, |
| 144 | bytes_allocated, usable_size, |
| 145 | bytes_allocated_out, |
| 146 | usable_size_out); |
| 147 | } |
| 148 | |
| 149 | template <typename S, |
| 150 | size_t kValgrindRedZoneBytes, |
| 151 | bool kAdjustForRedzoneInAllocSize, |
| 152 | bool kUseObjSizeForUsable> |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 153 | size_t ValgrindMallocSpace<S, |
| 154 | kValgrindRedZoneBytes, |
| 155 | kAdjustForRedzoneInAllocSize, |
| 156 | kUseObjSizeForUsable>::AllocationSize( |
| 157 | mirror::Object* obj, size_t* usable_size) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 158 | size_t result = S::AllocationSize(reinterpret_cast<mirror::Object*>( |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 159 | reinterpret_cast<uint8_t*>(obj) - (kAdjustForRedzoneInAllocSize ? kValgrindRedZoneBytes : 0)), |
| 160 | usable_size); |
| 161 | if (usable_size != nullptr) { |
| 162 | if (kUseObjSizeForUsable) { |
| 163 | *usable_size = valgrind_details::GetObjSizeNoThreadSafety(obj); |
| 164 | } else { |
| 165 | *usable_size = *usable_size - 2 * kValgrindRedZoneBytes; |
| 166 | } |
| 167 | } |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 168 | return result; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 171 | template <typename S, |
| 172 | size_t kValgrindRedZoneBytes, |
| 173 | bool kAdjustForRedzoneInAllocSize, |
| 174 | bool kUseObjSizeForUsable> |
| 175 | size_t ValgrindMallocSpace<S, |
| 176 | kValgrindRedZoneBytes, |
| 177 | kAdjustForRedzoneInAllocSize, |
| 178 | kUseObjSizeForUsable>::Free( |
| 179 | Thread* self, mirror::Object* ptr) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 180 | void* obj_after_rdz = reinterpret_cast<void*>(ptr); |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 181 | uint8_t* obj_with_rdz = reinterpret_cast<uint8_t*>(obj_after_rdz) - kValgrindRedZoneBytes; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 182 | // Make redzones undefined. |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 183 | size_t usable_size; |
| 184 | size_t allocation_size = AllocationSize(ptr, &usable_size); |
| 185 | |
| 186 | // Unprotect the allocation. |
| 187 | // Use the obj-size-for-usable flag to determine whether usable_size is the more important one, |
| 188 | // e.g., whether there's data in the allocation_size (and usable_size can't be trusted). |
| 189 | if (kUseObjSizeForUsable) { |
| 190 | VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size); |
| 191 | } else { |
| 192 | VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, usable_size + 2 * kValgrindRedZoneBytes); |
| 193 | } |
| 194 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 195 | return S::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz)); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 198 | template <typename S, |
| 199 | size_t kValgrindRedZoneBytes, |
| 200 | bool kAdjustForRedzoneInAllocSize, |
| 201 | bool kUseObjSizeForUsable> |
| 202 | size_t ValgrindMallocSpace<S, |
| 203 | kValgrindRedZoneBytes, |
| 204 | kAdjustForRedzoneInAllocSize, |
| 205 | kUseObjSizeForUsable>::FreeList( |
| 206 | Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 207 | size_t freed = 0; |
| 208 | for (size_t i = 0; i < num_ptrs; i++) { |
| 209 | freed += Free(self, ptrs[i]); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 210 | ptrs[i] = nullptr; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 211 | } |
| 212 | return freed; |
| 213 | } |
| 214 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 215 | template <typename S, |
| 216 | size_t kValgrindRedZoneBytes, |
| 217 | bool kAdjustForRedzoneInAllocSize, |
| 218 | bool kUseObjSizeForUsable> |
| 219 | template <typename... Params> |
| 220 | ValgrindMallocSpace<S, |
| 221 | kValgrindRedZoneBytes, |
| 222 | kAdjustForRedzoneInAllocSize, |
| 223 | kUseObjSizeForUsable>::ValgrindMallocSpace( |
| 224 | MemMap* mem_map, size_t initial_size, Params... params) : S(mem_map, initial_size, params...) { |
| 225 | VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, |
| 226 | mem_map->Size() - initial_size); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | } // namespace space |
| 230 | } // namespace gc |
| 231 | } // namespace art |
| 232 | |
| 233 | #endif // ART_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_ |