blob: 793d7989f10175012c8d20817316c2a134736e10 [file] [log] [blame]
Ian Rogers6fac4472014-02-25 17:01:10 -08001/*
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 Gamped7576322014-10-24 22:13:45 -070024#include "valgrind_settings.h"
25
Ian Rogers6fac4472014-02-25 17:01:10 -080026namespace art {
27namespace gc {
28namespace space {
29
Andreas Gamped7576322014-10-24 22:13:45 -070030namespace valgrind_details {
Ian Rogers6fac4472014-02-25 17:01:10 -080031
Andreas Gamped7576322014-10-24 22:13:45 -070032template <size_t kValgrindRedZoneBytes, bool kUseObjSizeForUsable>
33inline 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
70inline size_t GetObjSizeNoThreadSafety(mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
71 return obj->SizeOf<kVerifyNone>();
72}
73
74} // namespace valgrind_details
75
76template <typename S,
77 size_t kValgrindRedZoneBytes,
78 bool kAdjustForRedzoneInAllocSize,
79 bool kUseObjSizeForUsable>
80mirror::Object*
81ValgrindMallocSpace<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 Rogers6fac4472014-02-25 17:01:10 -080088 void* obj_with_rdz = S::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes,
Andreas Gamped7576322014-10-24 22:13:45 -070089 &bytes_allocated, &usable_size);
Ian Rogers6fac4472014-02-25 17:01:10 -080090 if (obj_with_rdz == nullptr) {
91 return nullptr;
92 }
Andreas Gamped7576322014-10-24 22:13:45 -070093
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 Rogers6fac4472014-02-25 17:01:10 -080099}
100
Andreas Gamped7576322014-10-24 22:13:45 -0700101template <typename S,
102 size_t kValgrindRedZoneBytes,
103 bool kAdjustForRedzoneInAllocSize,
104 bool kUseObjSizeForUsable>
105mirror::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 Rogers6fac4472014-02-25 17:01:10 -0800114 if (obj_with_rdz == nullptr) {
115 return nullptr;
116 }
Andreas Gamped7576322014-10-24 22:13:45 -0700117
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 Rogers6fac4472014-02-25 17:01:10 -0800123}
124
Andreas Gamped7576322014-10-24 22:13:45 -0700125template <typename S,
126 size_t kValgrindRedZoneBytes,
127 bool kAdjustForRedzoneInAllocSize,
128 bool kUseObjSizeForUsable>
129size_t ValgrindMallocSpace<S,
130 kValgrindRedZoneBytes,
131 kAdjustForRedzoneInAllocSize,
132 kUseObjSizeForUsable>::AllocationSize(
133 mirror::Object* obj, size_t* usable_size) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800134 size_t result = S::AllocationSize(reinterpret_cast<mirror::Object*>(
Andreas Gamped7576322014-10-24 22:13:45 -0700135 reinterpret_cast<uint8_t*>(obj) - (kAdjustForRedzoneInAllocSize ? kValgrindRedZoneBytes : 0)),
136 usable_size);
137 if (usable_size != nullptr) {
138 if (kUseObjSizeForUsable) {
139 *usable_size = valgrind_details::GetObjSizeNoThreadSafety(obj);
140 } else {
141 *usable_size = *usable_size - 2 * kValgrindRedZoneBytes;
142 }
143 }
Mathieu Chartier661974a2014-01-09 11:23:53 -0800144 return result;
Ian Rogers6fac4472014-02-25 17:01:10 -0800145}
146
Andreas Gamped7576322014-10-24 22:13:45 -0700147template <typename S,
148 size_t kValgrindRedZoneBytes,
149 bool kAdjustForRedzoneInAllocSize,
150 bool kUseObjSizeForUsable>
151size_t ValgrindMallocSpace<S,
152 kValgrindRedZoneBytes,
153 kAdjustForRedzoneInAllocSize,
154 kUseObjSizeForUsable>::Free(
155 Thread* self, mirror::Object* ptr) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800156 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
Andreas Gamped7576322014-10-24 22:13:45 -0700157 uint8_t* obj_with_rdz = reinterpret_cast<uint8_t*>(obj_after_rdz) - kValgrindRedZoneBytes;
Ian Rogers6fac4472014-02-25 17:01:10 -0800158 // Make redzones undefined.
Andreas Gamped7576322014-10-24 22:13:45 -0700159 size_t usable_size;
160 size_t allocation_size = AllocationSize(ptr, &usable_size);
161
162 // Unprotect the allocation.
163 // Use the obj-size-for-usable flag to determine whether usable_size is the more important one,
164 // e.g., whether there's data in the allocation_size (and usable_size can't be trusted).
165 if (kUseObjSizeForUsable) {
166 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size);
167 } else {
168 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, usable_size + 2 * kValgrindRedZoneBytes);
169 }
170
Mathieu Chartier661974a2014-01-09 11:23:53 -0800171 return S::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
Ian Rogers6fac4472014-02-25 17:01:10 -0800172}
173
Andreas Gamped7576322014-10-24 22:13:45 -0700174template <typename S,
175 size_t kValgrindRedZoneBytes,
176 bool kAdjustForRedzoneInAllocSize,
177 bool kUseObjSizeForUsable>
178size_t ValgrindMallocSpace<S,
179 kValgrindRedZoneBytes,
180 kAdjustForRedzoneInAllocSize,
181 kUseObjSizeForUsable>::FreeList(
182 Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Ian Rogers6fac4472014-02-25 17:01:10 -0800183 size_t freed = 0;
184 for (size_t i = 0; i < num_ptrs; i++) {
185 freed += Free(self, ptrs[i]);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800186 ptrs[i] = nullptr;
Ian Rogers6fac4472014-02-25 17:01:10 -0800187 }
188 return freed;
189}
190
Andreas Gamped7576322014-10-24 22:13:45 -0700191template <typename S,
192 size_t kValgrindRedZoneBytes,
193 bool kAdjustForRedzoneInAllocSize,
194 bool kUseObjSizeForUsable>
195template <typename... Params>
196ValgrindMallocSpace<S,
197 kValgrindRedZoneBytes,
198 kAdjustForRedzoneInAllocSize,
199 kUseObjSizeForUsable>::ValgrindMallocSpace(
200 MemMap* mem_map, size_t initial_size, Params... params) : S(mem_map, initial_size, params...) {
201 VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size,
202 mem_map->Size() - initial_size);
Ian Rogers6fac4472014-02-25 17:01:10 -0800203}
204
205} // namespace space
206} // namespace gc
207} // namespace art
208
209#endif // ART_RUNTIME_GC_SPACE_VALGRIND_MALLOC_SPACE_INL_H_