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 3 // found in the LICENSE file. 4 5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 7 8 #include <stddef.h> 9 10 #include "build/build_config.h" 11 12 #if defined(OS_WIN) 13 #include <windows.h> 14 #include "base/process/process_handle.h" 15 #elif defined(OS_MACOSX) && !defined(OS_IOS) 16 #include <mach/mach.h> 17 #include "base/base_export.h" 18 #include "base/macros.h" 19 #include "base/process/process_handle.h" 20 #elif defined(OS_POSIX) 21 #include <sys/types.h> 22 #include "base/file_descriptor_posix.h" 23 #endif 24 25 namespace base { 26 27 class Pickle; 28 29 // SharedMemoryHandle is a platform specific type which represents 30 // the underlying OS handle to a shared memory segment. 31 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) 32 typedef FileDescriptor SharedMemoryHandle; 33 #elif defined(OS_WIN) 34 class BASE_EXPORT SharedMemoryHandle { 35 public: 36 // The default constructor returns an invalid SharedMemoryHandle. 37 SharedMemoryHandle(); 38 SharedMemoryHandle(HANDLE h, base::ProcessId pid); 39 40 // Standard copy constructor. The new instance shares the underlying OS 41 // primitives. 42 SharedMemoryHandle(const SharedMemoryHandle& handle); 43 44 // Standard assignment operator. The updated instance shares the underlying 45 // OS primitives. 46 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); 47 48 // Comparison operators. 49 bool operator==(const SharedMemoryHandle& handle) const; 50 bool operator!=(const SharedMemoryHandle& handle) const; 51 52 // Closes the underlying OS resources. 53 void Close() const; 54 55 // Whether the underlying OS primitive is valid. 56 bool IsValid() const; 57 58 // Whether |pid_| is the same as the current process's id. 59 bool BelongsToCurrentProcess() const; 60 61 // Whether handle_ needs to be duplicated into the destination process when 62 // an instance of this class is passed over a Chrome IPC channel. 63 bool NeedsBrokering() const; 64 65 void SetOwnershipPassesToIPC(bool ownership_passes); 66 bool OwnershipPassesToIPC() const; 67 68 HANDLE GetHandle() const; 69 base::ProcessId GetPID() const; 70 71 private: 72 HANDLE handle_; 73 74 // The process in which |handle_| is valid and can be used. If |handle_| is 75 // invalid, this will be kNullProcessId. 76 base::ProcessId pid_; 77 78 // Whether passing this object as a parameter to an IPC message passes 79 // ownership of |handle_| to the IPC stack. This is meant to mimic the 80 // behavior of the |auto_close| parameter of FileDescriptor. This member only 81 // affects attachment-brokered SharedMemoryHandles. 82 // Defaults to |false|. 83 bool ownership_passes_to_ipc_; 84 }; 85 #else 86 class BASE_EXPORT SharedMemoryHandle { 87 public: 88 // The default constructor returns an invalid SharedMemoryHandle. 89 SharedMemoryHandle(); 90 91 // Makes a Mach-based SharedMemoryHandle of the given size. On error, 92 // subsequent calls to IsValid() return false. 93 explicit SharedMemoryHandle(mach_vm_size_t size); 94 95 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry 96 // in the task with process id |pid|. The memory region has size |size|. 97 SharedMemoryHandle(mach_port_t memory_object, 98 mach_vm_size_t size, 99 base::ProcessId pid); 100 101 // Standard copy constructor. The new instance shares the underlying OS 102 // primitives. 103 SharedMemoryHandle(const SharedMemoryHandle& handle); 104 105 // Standard assignment operator. The updated instance shares the underlying 106 // OS primitives. 107 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); 108 109 // Duplicates the underlying OS resources. 110 SharedMemoryHandle Duplicate() const; 111 112 // Comparison operators. 113 bool operator==(const SharedMemoryHandle& handle) const; 114 bool operator!=(const SharedMemoryHandle& handle) const; 115 116 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle 117 // is backed by a valid OS primitive, it becomes immutable. 118 bool IsValid() const; 119 120 // Exposed so that the SharedMemoryHandle can be transported between 121 // processes. 122 mach_port_t GetMemoryObject() const; 123 124 // Returns false on a failure to determine the size. On success, populates the 125 // output variable |size|. Returns 0 if the handle is invalid. 126 bool GetSize(size_t* size) const; 127 128 // The SharedMemoryHandle must be valid. 129 // Returns whether the SharedMemoryHandle was successfully mapped into memory. 130 // On success, |memory| is an output variable that contains the start of the 131 // mapped memory. 132 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); 133 134 // Closes the underlying OS primitive. 135 void Close() const; 136 137 void SetOwnershipPassesToIPC(bool ownership_passes); 138 bool OwnershipPassesToIPC() const; 139 140 private: 141 // Shared code between copy constructor and operator=. 142 void CopyRelevantData(const SharedMemoryHandle& handle); 143 144 mach_port_t memory_object_ = MACH_PORT_NULL; 145 146 // The size of the shared memory region when |type_| is MACH. Only 147 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 148 mach_vm_size_t size_ = 0; 149 150 // The pid of the process in which |memory_object_| is usable. Only 151 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 152 base::ProcessId pid_ = 0; 153 154 // Whether passing this object as a parameter to an IPC message passes 155 // ownership of |memory_object_| to the IPC stack. This is meant to mimic 156 // the behavior of the |auto_close| parameter of FileDescriptor. 157 // Defaults to |false|. 158 bool ownership_passes_to_ipc_ = false; 159 }; 160 #endif 161 162 } // namespace base 163 164 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 165