OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #ifndef VM_VERIFIED_MEMORY_H_ |
| 6 #define VM_VERIFIED_MEMORY_H_ |
| 7 |
| 8 #include "vm/allocation.h" |
| 9 #include "vm/flags.h" |
| 10 #include "vm/virtual_memory.h" |
| 11 |
| 12 namespace dart { |
| 13 |
| 14 #if defined(DEBUG) |
| 15 DECLARE_FLAG(bool, verified_mem); |
| 16 DECLARE_FLAG(int, verified_mem_max_reserve_mb); |
| 17 #endif |
| 18 |
| 19 |
| 20 // A wrapper around VirtualMemory for verifying that a particular class of |
| 21 // memory writes are only performed through a particular interface. |
| 22 // |
| 23 // The main use case is verifying that storing pointers into objects is only |
| 24 // performed by code aware of the GC write barrier. |
| 25 // |
| 26 // NOTE: Verification is enabled only if 'verified_mem' is true, and this flag |
| 27 // only exists in DEBUG builds. |
| 28 class VerifiedMemory : public AllStatic { |
| 29 public: |
| 30 // Reserves a block of memory for which all methods in this class may |
| 31 // be called. Returns NULL if out of memory. |
| 32 static VirtualMemory* Reserve(intptr_t size) { |
| 33 return enabled() ? ReserveInternal(size) : VirtualMemory::Reserve(size); |
| 34 } |
| 35 |
| 36 // Verifies that [start, start + size) has only been mutated through |
| 37 // methods in this class (or explicitly accepted by calling Accept). |
| 38 static void Verify(uword start, intptr_t size) { |
| 39 if (!enabled()) return; |
| 40 ASSERT(size <= offset()); |
| 41 ASSERT(memcmp(reinterpret_cast<void*>(start + offset()), |
| 42 reinterpret_cast<void*>(start), |
| 43 size) == 0); |
| 44 } |
| 45 |
| 46 // Assigns value to *ptr after verifying previous content at that location. |
| 47 template<typename T> |
| 48 static void Write(T* ptr, const T& value) { |
| 49 if (enabled()) { |
| 50 uword addr = reinterpret_cast<uword>(ptr); |
| 51 Verify(addr, sizeof(T)); |
| 52 T* offset_ptr = reinterpret_cast<T*>(addr + offset()); |
| 53 *offset_ptr = value; |
| 54 } |
| 55 *ptr = value; |
| 56 } |
| 57 |
| 58 // Accepts the current state of [start, start + size), even if it has been |
| 59 // mutated by other means. |
| 60 static void Accept(uword start, intptr_t size) { |
| 61 if (!enabled()) return; |
| 62 ASSERT(size <= offset()); |
| 63 memmove(reinterpret_cast<void*>(start + offset()), |
| 64 reinterpret_cast<void*>(start), |
| 65 size); |
| 66 } |
| 67 |
| 68 private: |
| 69 #if defined(DEBUG) |
| 70 static bool enabled() { return FLAG_verified_mem; } |
| 71 static intptr_t offset() { return FLAG_verified_mem_max_reserve_mb * MB; } |
| 72 static VirtualMemory* ReserveInternal(intptr_t size); |
| 73 #else |
| 74 // In release mode, most code in this class is optimized away. |
| 75 static bool enabled() { return false; } |
| 76 static intptr_t offset() { UNREACHABLE(); return -1; } |
| 77 static VirtualMemory* ReserveInternal(intptr_t size) { |
| 78 UNREACHABLE(); |
| 79 return NULL; |
| 80 } |
| 81 #endif |
| 82 |
| 83 friend class Assembler; // To use enabled/offset when generating code. |
| 84 }; |
| 85 |
| 86 } // namespace dart |
| 87 |
| 88 #endif // VM_VERIFIED_MEMORY_H_ |
OLD | NEW |