Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: base/memory/shared_memory_allocator.h

Issue 1410213004: Create "persistent memory allocator" for persisting and sharing objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: good grief -- when will it end? Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
Alexander Potapenko 2015/11/09 18:03:46 Has this design been reviewed by the security team
bcwhite 2015/11/09 19:21:16 Yes, from the security team Justin Schuh gave some
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 typedef int32_t Reference;
43
44 // Internal state information when iterating over memory allocations.
45 struct Iterator {
46 Reference last;
47 uint32_t niter;
48 };
49
50 // Returned information about the internal state of the heap.
51 struct MemoryInfo {
52 size_t total;
53 size_t free;
54 };
55
56 enum : uint32_t {
57 kTypeIdAny = 0 // Match any type-id inside GetAsObject().
chrisha 2015/11/09 16:53:51 Also worth having a int32_t kReferenceInvalid = 0?
bcwhite 2015/11/09 18:02:05 I don't think it's necessary. Like with pointers,
58 };
59
60 // The allocator operates on any arbitrary block of memory. Creation and
61 // sharing of that block with another process is the responsibility of the
62 // caller. The allocator needs to know only the block's |base| address, the
63 // total |size| of the block, and any internal |page| size (zero if not
64 // paged) across which allocations should not span.
65 //
66 // SharedMemoryAllocator does NOT take ownership of this memory block. The
67 // caller must manage it and ensure it stays available throughout the lifetime
68 // of this object.
69 //
70 // Memory segments for sharing must have had an allocator attached to them
71 // before actually being shared. If the memory segment was just created, it
72 // should be zeroed. If it was an existing segment, the values here will
73 // be compared to copies stored in the shared segment as a guard against
74 // corruption.
75 SharedMemoryAllocator(void* base, size_t size, size_t page_size);
76 ~SharedMemoryAllocator();
77
78 // Get an object referenced by an |ref|. For safety reasons, the |type_id|
chrisha 2015/11/09 16:53:51 by a* |ref|
bcwhite 2015/11/09 18:02:05 Doh! I *knew* I was going to miss something in th
79 // code and size-of(|T|) are compared to ensure the reference is valid
80 // and cannot return an object outside of the memory segment. A |type_id| of
81 // zero will match any though the size is still checked. NULL is returned
82 // if any problem is detected, such as corrupted storage or incorrect
83 // parameters. Callers MUST check that the returned value is not-null EVERY
84 // TIME before accessing it or risk crashing! Once dereferenced, the pointer
85 // is safe to reuse forever.
86 //
87 // NOTE: Though this method will guarantee that an object of the specified
88 // type can be accessed without going outside the bounds of the memory
89 // segment, it makes not guarantees of the validity of the data within the
chrisha 2015/11/09 16:53:51 no*
bcwhite 2015/11/09 18:02:05 Done.
90 // object itself. If it is expected that the contents of the segment could
91 // be compromised with malicious intent, the object must be hardened as well.
92 template <typename T>
93 T* GetAsObject(Reference ref, uint32_t type_id) {
chrisha 2015/11/09 16:53:51 This function also wants a counterpart to convert
bcwhite 2015/11/09 18:02:05 Possibly. I've left it out because I haven't come
94 return static_cast<T*>(GetBlockData(ref, type_id, sizeof(T)));
95 }
96
97 // Get the number of bytes allocated to a block. This is useful when storing
98 // arrays in order to validate the ending boundary. The returned value will
99 // include any padding added to achieve the required alignment and so could
100 // be larger than given in the original Allocate() request.
101 size_t GetAllocSize(Reference ref);
102
103 // Reserve space in the memory segment of the desired |size| and |type_id|.
104 // A return value of zero indicates the allocation failed, otherwise the
105 // returned reference can be used by any process to get a real pointer via
106 // the GetAsObject() call.
107 int32_t Allocate(size_t size, uint32_t type_id);
108
109 // Allocated objects can be added to an internal list that can then be
110 // iterated over by other processes. If an allocated object can be found
111 // another way, such as by having its reference within a different object
112 // that will be made iterable, then this call is not necessary. This always
113 // succeeds unless corruption is detected; check IsCorrupted() to find out.
114 void MakeIterable(Reference ref);
chrisha 2015/11/09 16:53:51 Can't this return true on success, false on failur
bcwhite 2015/11/09 18:02:05 It's never supposed to fail so I don't think there
115
116 // Get the information about the amount of free space in the allocator. The
117 // amount of free space should be treated as approximate due to extras from
118 // alignment and metadata. Concurrent allocations from other threads will
119 // also make the true amount less than what is reported. It will never
120 // return _less_ than could actually be allocated.
chrisha 2015/11/09 16:53:51 This last sentence is at odds with the sentence be
bcwhite 2015/11/09 18:02:05 Done.
121 void GetMemoryInfo(MemoryInfo* meminfo);
122
123 // Iterating uses a |state| structure (initialized by CreateIterator) and
124 // returns both the reference reference to the object as well as the |type_id|
chrisha 2015/11/09 16:53:51 reference reference
bcwhite 2015/11/09 18:02:05 Done.
125 // of that object. A zero return value indicates there are currently no more
126 // objects to be found but future attempts can be made without having to
127 // reset the iterator to "first".
128 void CreateIterator(Iterator* state);
129 int32_t GetNextIterable(Iterator* state, uint32_t* type_id);
130
131 // If there is some indication that the shared memory has become corrupted,
132 // calling this will attempt to prevent further damage by indicating to
133 // all processes that something is not as expected.
134 void SetCorrupt();
135
136 // This can be called to determine if corruption has been detected in the
137 // shared segment, possibly my a malicious actor. Once detected, future
138 // allocations will fail and iteration may not locate all objects.
139 bool IsCorrupt();
140
141 // Flag set if an allocation has failed because memory was full.
142 bool IsFull();
143
144 private:
145 struct SharedMetadata;
146 struct BlockHeader;
147
148 BlockHeader* GetBlock(Reference ref, uint32_t type_id, int32_t size,
149 bool queue_ok, bool free_ok);
150 void* GetBlockData(Reference ref, uint32_t type_id, int32_t size);
151
152 SharedMetadata* shared_meta_; // Pointer to start of memory segment.
153 char* mem_base_; // Same. (char because sizeof guaranteed 1)
chrisha 2015/11/09 16:53:51 This can be replaced with a pair of member functio
bcwhite 2015/11/09 18:02:05 Done.
154 int32_t mem_size_; // Size of entire memory segment.
155 int32_t mem_page_; // Page size allocations shouldn't cross.
156 subtle::Atomic32 corrupted_; // TODO(bcwhite): Use std::atomic<char> when ok.
chrisha 2015/11/09 16:53:51 This should be called flags_, no? As it also store
bcwhite 2015/11/09 18:02:05 It's only a single flag and has either a 0 or 1 va
157
158 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator);
159 };
160
161 } // namespace base
162
163 #endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698