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

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

Issue 2859843002: Add a GUID to base::SharedMemoryHandle. (Closed)
Patch Set: fix guid on android. Created 3 years, 7 months 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
« no previous file with comments | « base/memory/shared_memory_android.cc ('k') | base/memory/shared_memory_handle.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include "base/unguessable_token.h"
10 #include "build/build_config.h" 11 #include "build/build_config.h"
11 12
12 #if defined(OS_WIN) 13 #if defined(OS_WIN)
13 #include <windows.h> 14 #include <windows.h>
14 #include "base/process/process_handle.h" 15 #include "base/process/process_handle.h"
15 #elif defined(OS_MACOSX) && !defined(OS_IOS) 16 #elif defined(OS_MACOSX) && !defined(OS_IOS)
16 #include <mach/mach.h> 17 #include <mach/mach.h>
17 #include "base/base_export.h" 18 #include "base/base_export.h"
18 #include "base/file_descriptor_posix.h" 19 #include "base/file_descriptor_posix.h"
19 #include "base/macros.h" 20 #include "base/macros.h"
20 #include "base/process/process_handle.h" 21 #include "base/process/process_handle.h"
21 #elif defined(OS_POSIX) 22 #elif defined(OS_POSIX)
22 #include <sys/types.h> 23 #include <sys/types.h>
23 #include "base/file_descriptor_posix.h" 24 #include "base/file_descriptor_posix.h"
24 #endif 25 #endif
25 26
26 namespace base { 27 namespace base {
27 28
28 // SharedMemoryHandle is a platform specific type which represents 29 // SharedMemoryHandle is the smallest possible IPC-transportable "reference" to
29 // the underlying OS handle to a shared memory segment. 30 // a shared memory OS resource. A "reference" can be consumed exactly once [by
31 // base::SharedMemory] to map the shared memory OS resource into the virtual
32 // address space of the current process.
33 // TODO(erikchen): This class should have strong ownership semantics to prevent
34 // leaks of the underlying OS resource. https://crbug.com/640840.
30 class BASE_EXPORT SharedMemoryHandle { 35 class BASE_EXPORT SharedMemoryHandle {
31 public: 36 public:
32 // The default constructor returns an invalid SharedMemoryHandle. 37 // The default constructor returns an invalid SharedMemoryHandle.
33 SharedMemoryHandle(); 38 SharedMemoryHandle();
34 39
35 // Standard copy constructor. The new instance shares the underlying OS 40 // Standard copy constructor. The new instance shares the underlying OS
36 // primitives. 41 // primitives.
37 SharedMemoryHandle(const SharedMemoryHandle& handle); 42 SharedMemoryHandle(const SharedMemoryHandle& handle);
38 43
39 // Standard assignment operator. The updated instance shares the underlying 44 // Standard assignment operator. The updated instance shares the underlying
(...skipping 14 matching lines...) Expand all
54 bool OwnershipPassesToIPC() const; 59 bool OwnershipPassesToIPC() const;
55 60
56 // Whether the underlying OS resource is valid. 61 // Whether the underlying OS resource is valid.
57 bool IsValid() const; 62 bool IsValid() const;
58 63
59 // Duplicates the underlying OS resource. Using the return value as a 64 // Duplicates the underlying OS resource. Using the return value as a
60 // parameter to an IPC message will cause the IPC subsystem to consume the OS 65 // parameter to an IPC message will cause the IPC subsystem to consume the OS
61 // resource. 66 // resource.
62 SharedMemoryHandle Duplicate() const; 67 SharedMemoryHandle Duplicate() const;
63 68
69 // Uniques identifies the shared memory region that the underlying OS resource
70 // points to. Multiple SharedMemoryHandles that point to the same shared
71 // memory region will have the same GUID. Preserved across IPC.
72 base::UnguessableToken GetGUID() const;
73
64 #if defined(OS_MACOSX) && !defined(OS_IOS) 74 #if defined(OS_MACOSX) && !defined(OS_IOS)
65 enum Type { 75 enum Type {
66 // The SharedMemoryHandle is backed by a POSIX fd. 76 // The SharedMemoryHandle is backed by a POSIX fd.
67 POSIX, 77 POSIX,
68 // The SharedMemoryHandle is backed by the Mach primitive "memory object". 78 // The SharedMemoryHandle is backed by the Mach primitive "memory object".
69 MACH, 79 MACH,
70 }; 80 };
71 81
72 // Constructs a SharedMemoryHandle backed by the components of a 82 // Constructs a SharedMemoryHandle backed by the components of a
73 // FileDescriptor. The newly created instance has the same ownership semantics 83 // FileDescriptor. The newly created instance has the same ownership semantics
74 // as base::FileDescriptor. This typically means that the SharedMemoryHandle 84 // as base::FileDescriptor. This typically means that the SharedMemoryHandle
75 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's 85 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's
76 // common for existing code to make shallow copies of SharedMemoryHandle, and 86 // common for existing code to make shallow copies of SharedMemoryHandle, and
77 // the one that is finally passed into a base::SharedMemory is the one that 87 // the one that is finally passed into a base::SharedMemory is the one that
78 // "consumes" the fd. 88 // "consumes" the fd.
79 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); 89 // |guid| uniquely identifies the shared memory region pointed to by the
90 // underlying OS resource. If |file_descriptor| is associated with another
91 // SharedMemoryHandle, the caller must pass the |guid| of that
92 // SharedMemoryHandle. Otherwise, the caller should generate a new
93 // UnguessableToken.
94 SharedMemoryHandle(const base::FileDescriptor& file_descriptor,
95 const base::UnguessableToken& guid);
80 96
81 // Makes a Mach-based SharedMemoryHandle of the given size. On error, 97 // Makes a Mach-based SharedMemoryHandle of the given size. On error,
82 // subsequent calls to IsValid() return false. 98 // subsequent calls to IsValid() return false.
83 explicit SharedMemoryHandle(mach_vm_size_t size); 99 SharedMemoryHandle(mach_vm_size_t size, const base::UnguessableToken& guid);
84 100
85 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry 101 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry
86 // in the current task. The memory region has size |size|. 102 // in the current task. The memory region has size |size|.
87 SharedMemoryHandle(mach_port_t memory_object, mach_vm_size_t size); 103 SharedMemoryHandle(mach_port_t memory_object,
104 mach_vm_size_t size,
105 const base::UnguessableToken& guid);
88 106
89 // Exposed so that the SharedMemoryHandle can be transported between 107 // Exposed so that the SharedMemoryHandle can be transported between
90 // processes. 108 // processes.
91 mach_port_t GetMemoryObject() const; 109 mach_port_t GetMemoryObject() const;
92 110
93 // Returns false on a failure to determine the size. On success, populates the 111 // Returns false on a failure to determine the size. On success, populates the
94 // output variable |size|. 112 // output variable |size|.
95 bool GetSize(size_t* size) const; 113 bool GetSize(size_t* size) const;
96 114
97 // The SharedMemoryHandle must be valid. 115 // The SharedMemoryHandle must be valid.
98 // Returns whether the SharedMemoryHandle was successfully mapped into memory. 116 // Returns whether the SharedMemoryHandle was successfully mapped into memory.
99 // On success, |memory| is an output variable that contains the start of the 117 // On success, |memory| is an output variable that contains the start of the
100 // mapped memory. 118 // mapped memory.
101 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); 119 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only);
102 #elif defined(OS_WIN) 120 #elif defined(OS_WIN)
103 // Takes implicit ownership of |h|. 121 // Takes implicit ownership of |h|.
104 SharedMemoryHandle(HANDLE h); 122 // |guid| uniquely identifies the shared memory region pointed to by the
105 123 // underlying OS resource. If the HANDLE is associated with another
124 // SharedMemoryHandle, the caller must pass the |guid| of that
125 // SharedMemoryHandle. Otherwise, the caller should generate a new
126 // UnguessableToken.
127 SharedMemoryHandle(HANDLE h, const base::UnguessableToken& guid);
106 HANDLE GetHandle() const; 128 HANDLE GetHandle() const;
107 #else 129 #else
108 // This constructor is deprecated, as it fails to propagate the GUID, which 130 // |guid| uniquely identifies the shared memory region pointed to by the
109 // will be added in the near future. 131 // underlying OS resource. If |file_descriptor| is associated with another
110 // TODO(rockot): Remove this constructor once Mojo supports GUIDs. 132 // SharedMemoryHandle, the caller must pass the |guid| of that
111 // https://crbug.com/713763. 133 // SharedMemoryHandle. Otherwise, the caller should generate a new
112 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); 134 // UnguessableToken.
135 SharedMemoryHandle(const base::FileDescriptor& file_descriptor,
136 const base::UnguessableToken& guid);
113 137
114 // Creates a SharedMemoryHandle from an |fd| supplied from an external 138 // Creates a SharedMemoryHandle from an |fd| supplied from an external
115 // service. 139 // service.
116 static SharedMemoryHandle ImportHandle(int fd); 140 static SharedMemoryHandle ImportHandle(int fd);
117 141
118 // Returns the underlying OS resource. 142 // Returns the underlying OS resource.
119 int GetHandle() const; 143 int GetHandle() const;
120 144
121 // Takes ownership of the OS resource. 145 // Takes ownership of the OS resource.
122 void SetHandle(int fd); 146 void SetHandle(int fd);
123 147
124 // Invalidates [but doesn't close] the underlying OS resource. This will leak 148 // Invalidates [but doesn't close] the underlying OS resource. This will leak
125 // unless the caller is careful. 149 // unless the caller is careful.
126 int Release(); 150 int Release();
127 #endif 151 #endif
128 152
129 private: 153 private:
130 #if defined(OS_MACOSX) && !defined(OS_IOS) 154 #if defined(OS_MACOSX) && !defined(OS_IOS)
131 friend class SharedMemory; 155 friend class SharedMemory;
132 156
133 // Shared code between copy constructor and operator=.
134 void CopyRelevantData(const SharedMemoryHandle& handle);
135
136 Type type_; 157 Type type_;
137 158
138 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a 159 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a
139 // mach port. |type_| determines the backing member. 160 // mach port. |type_| determines the backing member.
140 union { 161 union {
141 FileDescriptor file_descriptor_; 162 FileDescriptor file_descriptor_;
142 163
143 struct { 164 struct {
144 mach_port_t memory_object_; 165 mach_port_t memory_object_;
145 166
(...skipping 13 matching lines...) Expand all
159 180
160 // Whether passing this object as a parameter to an IPC message passes 181 // Whether passing this object as a parameter to an IPC message passes
161 // ownership of |handle_| to the IPC stack. This is meant to mimic the 182 // ownership of |handle_| to the IPC stack. This is meant to mimic the
162 // behavior of the |auto_close| parameter of FileDescriptor. This member only 183 // behavior of the |auto_close| parameter of FileDescriptor. This member only
163 // affects attachment-brokered SharedMemoryHandles. 184 // affects attachment-brokered SharedMemoryHandles.
164 // Defaults to |false|. 185 // Defaults to |false|.
165 bool ownership_passes_to_ipc_; 186 bool ownership_passes_to_ipc_;
166 #else 187 #else
167 FileDescriptor file_descriptor_; 188 FileDescriptor file_descriptor_;
168 #endif 189 #endif
190
191 base::UnguessableToken guid_;
169 }; 192 };
170 193
171 } // namespace base 194 } // namespace base
172 195
173 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 196 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
OLDNEW
« no previous file with comments | « base/memory/shared_memory_android.cc ('k') | base/memory/shared_memory_handle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698