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/base_export.h" | |
11 #include "base/macros.h" | |
12 | |
13 namespace base { | |
14 | |
15 // Simple allocator for pieces of a memory block that may be shared across | |
16 // multiple processes. | |
17 // | |
18 // This class provides for thread-secure (i.e. safe against other threads | |
19 // or processes that may be compromised and thus have malicious intent) | |
chrisha
2015/10/30 14:36:46
How can this possibly be true? If I'm a malicious
| |
20 // allocation of memory within a designated block and also a mechanism by | |
21 // which other threads can learn of the allocations with any additional | |
22 // shared information. | |
23 // | |
24 // There is (currently) no way to release an allocated block of data because | |
25 // doing so would risk invalidating pointers held by other processes and | |
26 // greatly complicate the allocation algorithm. | |
chrisha
2015/10/30 14:36:46
Pointer invalidation isn't a reason, as this is tr
| |
27 class BASE_EXPORT SharedMemoryAllocator { | |
28 public: | |
29 struct Iterator { | |
30 int32_t last; | |
31 }; | |
32 | |
33 struct MemoryInfo { | |
34 int32_t total; | |
35 int32_t free; | |
36 }; | |
37 | |
38 // The allocator operates on any arbitrary block of memory. Creation and | |
39 // sharing of that block with another process is the responsibility of the | |
40 // caller. The allocator needs to know only the block's |base| address, the | |
41 // total |size| of the block, and any internal |page| size (zero if not | |
42 // paged) across which allocations should not span. | |
43 // | |
44 // SharedMemoryAllocator does NOT take ownership of this memory block. The | |
45 // caller must manage it and ensure it stays available throughout the lifetime | |
46 // of this object. | |
47 SharedMemoryAllocator(void* base, int32_t size, int32_t page); | |
48 ~SharedMemoryAllocator(); | |
49 | |
50 // Get an object referenced by an |offset|. For safety reasons, the |type| | |
51 // code and size-of |unused| are compared to ensure the reference is valid | |
52 // and cannot return an object outside of the memory segment. A |type| of | |
53 // zero will match any though the size is still checked. NULL is returned | |
54 // if any problem is detected, such as corrupted storage or incorrect | |
55 // parameters. Callers MUST check that the returned value is not-null EVERY | |
56 // TIME before accessing it or risk crashing! Once dereferenced, the pointer | |
57 // is safe to reuse forever. | |
58 // | |
59 // NOTE: Though this method will guarantee that an object of the specified | |
60 // type can be accessed without going outside the bounds of the memory | |
61 // segment, it makes not guarantees of the validity of the data within the | |
62 // object itself. If it is expected that the contents of the segment could | |
63 // be compromised with malicious intent, the object must be hardened as well. | |
64 template<typename T> T* GetObject(int32_t offset, int32_t type) { | |
65 return static_cast<T*>(GetBlockData(offset, type, sizeof(T), false)); | |
66 } | |
67 | |
68 // Reserve space in the memory segment of the desired |size| and |type|. | |
69 // A return value of zero indicates the allocation failed, otherwise the | |
70 // returned offset can be used by any process to get a real pointer via | |
71 // the GetObject() call. | |
72 int32_t Allocate(int32_t size, int32_t type); | |
73 | |
74 // Get the information about the amount of free space in the allocator. The | |
75 // amount of free space should be treated as approximate due to extras from | |
76 // alignment and metadata, but will never return less than could actually | |
77 // be allocated. | |
78 void GetMemoryInfo(MemoryInfo* meminfo); | |
79 | |
80 // Allocated objects can be added to an internal list that can then be | |
81 // iterated over by other processes. | |
82 void MakeIterable(int32_t offset); | |
83 | |
84 // Iterating uses a |state| structure (initialized by GetFirstIterable) and | |
85 // returns both the offset reference to the object as well as the |type| of | |
86 // that object. A zero return value indicates there are currently no more | |
87 // objects to be found but future attempts can be made without having to | |
88 // reset the iterator to "first". | |
89 int32_t GetFirstIterable(Iterator* state, int32_t* type); | |
90 int32_t GetNextIterable(Iterator* state, int32_t* type); | |
91 | |
92 // If there is some indication that the shared memory has become corrupted, | |
93 // calling this will attempt to prevent further damage by indicating to | |
94 // all processes that something is not as expected. | |
95 void SetCorrupted(); | |
96 bool IsCorrupted(); | |
97 | |
98 // Flag set if an allocation has failed because memory was full. | |
99 bool IsFull(); | |
100 | |
101 private: | |
102 struct SharedMetadata; | |
103 struct BlockHeader; | |
104 | |
105 BlockHeader* GetBlock(int32_t offset, int32_t type, int32_t size, | |
106 bool special); | |
107 void* GetBlockData(int32_t offset, int32_t type, int32_t size, bool special); | |
108 | |
109 SharedMetadata* shared_meta_; | |
110 char* mem_base_; // char because sizeof guaranteed 1 -- easy offset calc | |
111 int32_t mem_size_; | |
112 int32_t mem_page_; | |
113 int32_t last_seen_; | |
114 bool corrupted_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAllocator); | |
117 }; | |
118 | |
119 } // namespace base | |
120 | |
121 #endif // BASE_MEMORY_SHARED_MEMORY_ALLOCATOR_H_ | |
OLD | NEW |