OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ | |
6 #define BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/atomicops.h" | |
11 #include "base/base_export.h" | |
12 #include "base/macros.h" | |
13 | |
14 namespace base { | |
15 | |
16 // Simple allocator for pieces of a memory block that may be shared across | |
17 // multiple processes. | |
18 // | |
19 // This class provides for thread-secure (i.e. safe against other threads | |
20 // or processes that may be compromised and thus have malicious intent) | |
21 // allocation of memory within a designated block and also a mechanism by | |
22 // which other threads can learn of the allocations with any additional | |
23 // shared information. | |
24 // | |
25 // There is (currently) no way to release an allocated block of data because | |
26 // doing so would risk invalidating pointers held by other processes and | |
27 // greatly complicate the allocation algorithm. | |
28 // | |
29 // Construction of this object can accept new, clean (i.e. zeroed) memory | |
30 // or previously initialized memory. In the first case, construction must | |
31 // be allowed to complete before letting other allocators attach to the same | |
32 // segment. In other words, don't share the segment until at least one | |
33 // allocator has been attached to it. | |
34 // | |
35 // It should be noted that memory doesn't need to actually have zeros written | |
36 // throughout; it just needs to read as zero until something diffferent is | |
37 // written to a location. This is an important distinction as it supports the | |
38 // use-case of non-pinned memory, such as from a demand-allocated region by | |
39 // the OS or a memory-mapped file that auto-grows from a starting size of zero. | |
40 class BASE_EXPORT SharedMemoryAllocator { | |
41 public: | |
42 // Internal state information when iterating over memory allocations. | |
43 struct Iterator { | |
44 int32_t last; | |
45 uint32_t niter; | |
46 }; | |
47 | |
48 // Returned information about the internal state of the heap. | |
49 struct MemoryInfo { | |
50 int32_t total; | |
51 int32_t free; | |
52 }; | |
53 | |
54 enum { | |
chrisha
2015/11/03 21:25:25
Use a typed enum:
enum : int32_t { kTypeIdAny = 0
bcwhite
2015/11/03 22:32:17
Done.
| |
55 kTypeIdAny = 0 // Match any type-id inside GetAsObject(). | |
56 }; | |
57 | |
58 // The allocator operates on any arbitrary block of memory. Creation and | |
59 // sharing of that block with another process is the responsibility of the | |
60 // caller. The allocator needs to know only the block's |base| address, the | |
61 // total |size| of the block, and any internal |page| size (zero if not | |
62 // paged) across which allocations should not span. | |
63 // | |
64 // SharedMemoryAllocator does NOT take ownership of this memory block. The | |
65 // caller must manage it and ensure it stays available throughout the lifetime | |
66 // of this object. | |
67 // | |
68 // Memory segments for sharing must have had an allocator attached to them | |
69 // before actually being shared. If the memory segment was just created, it | |
70 // should be zeroed. If it was an existing segment, the values here will | |
71 // be compared to copies stored in the shared segment as a guard against | |
72 // corruption. | |
73 SharedMemoryAllocator(void* base, int32_t size, int32_t page_size); | |
74 ~SharedMemoryAllocator(); | |
75 | |
76 // Get an object referenced by an |offset|. For safety reasons, the |type_id| | |
77 // code and size-of(|T|) are compared to ensure the reference is valid | |
78 // and cannot return an object outside of the memory segment. A |type_id| of | |
79 // zero will match any though the size is still checked. NULL is returned | |
80 // if any problem is detected, such as corrupted storage or incorrect | |
81 // parameters. Callers MUST check that the returned value is not-null EVERY | |
82 // TIME before accessing it or risk crashing! Once dereferenced, the pointer | |
83 // is safe to reuse forever. | |
84 // | |
85 // NOTE: Though this method will guarantee that an object of the specified | |
86 // type can be accessed without going outside the bounds of the memory | |
87 // segment, it makes not guarantees of the validity of the data within the | |
88 // object itself. If it is expected that the contents of the segment could | |
89 // be compromised with malicious intent, the object must be hardened as well. | |
90 template <typename T> | |
91 T* GetAsObject(int32_t offset, int32_t type_id) { | |
92 return static_cast<T*>(GetBlockData(offset, type_id, sizeof(T), false)); | |
93 } | |
94 | |
95 // Reserve space in the memory segment of the desired |size| and |type_id|. | |
96 // A return value of zero indicates the allocation failed, otherwise the | |
97 // returned offset can be used by any process to get a real pointer via | |
98 // the GetAsObject() call. | |
99 int32_t Allocate(int32_t size, int32_t type_id); | |
100 | |
101 // Allocated objects can be added to an internal list that can then be | |
chrisha
2015/11/03 21:25:24
Shouldn't this be "should" rather than "can"? Is t
bcwhite
2015/11/03 22:32:17
Sure. In the case of a Histogram, it has it's met
| |
102 // iterated over by other processes. If an allocated object can be found | |
103 // another way, such as by having its offset within a different object | |
104 // that will be made iterable, then this call is not necessary. This always | |
105 // succeeds unless corruption is detected; check IsCorrupted() to find out. | |
106 void MakeIterable(int32_t offset); | |
107 | |
108 // Get the information about the amount of free space in the allocator. The | |
109 // amount of free space should be treated as approximate due to extras from | |
110 // alignment and metadata. Concurrent allocations from other threads will | |
111 // also make the true amount less than what is reported. It will never | |
112 // return _less_ than could actually be allocated. | |
113 void GetMemoryInfo(MemoryInfo* meminfo); | |
114 | |
115 // Iterating uses a |state| structure (initialized by CreateIterator) and | |
116 // returns both the offset reference to the object as well as the |type_id| | |
117 // of that object. A zero return value indicates there are currently no more | |
118 // objects to be found but future attempts can be made without having to | |
119 // reset the iterator to "first". | |
120 void CreateIterator(Iterator* state); | |
121 int32_t GetNextIterable(Iterator* state, int32_t* type_id); | |
122 | |
123 // If there is some indication that the shared memory has become corrupted, | |
124 // calling this will attempt to prevent further damage by indicating to | |
125 // all processes that something is not as expected. | |
126 void SetCorrupted(); | |
127 | |
128 // This can be called to determine if corruption has been detected in the | |
129 // shared segment, possibly my a malicious actor. Once detected, future | |
130 // allocations will fail and iteration may not locate all objects. | |
131 bool IsCorrupted(); | |
132 | |
133 // Flag set if an allocation has failed because memory was full. | |
134 bool IsFull(); | |
135 | |
136 private: | |
137 struct SharedMetadata; | |
138 struct BlockHeader; | |
139 | |
140 BlockHeader* GetBlock(int32_t offset, int32_t type_id, int32_t size, | |
141 bool special); | |
142 void* GetBlockData(int32_t offset, int32_t type_id, int32_t size, | |
143 bool special); | |
144 | |
145 SharedMetadata* shared_meta_; // Pointer to start of memory segment. | |
146 char* mem_base_; // Same. (char because sizeof guaranteed 1) | |
chrisha
2015/11/03 21:25:24
ubernit: Do we even need to store this? Or maybe j
bcwhite
2015/11/03 22:32:17
Correct on all counts. It's just so much simpler
| |
147 int32_t mem_size_; // Size of entire memory segment. | |
148 int32_t mem_page_; // Page size allocations shouldn't cross. | |
149 subtle::Atomic32 corrupted_; // TODO(bcwhite): Use std::atomic<char> when ok. | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator); | |
152 }; | |
153 | |
154 } // namespace base | |
155 | |
156 #endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ | |
OLD | NEW |