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

Side by Side Diff: mojo/public/cpp/system/platform_handle.cc

Issue 2583583002: Mojo: Fix wrapping of invalid SharedMemoryHandles. (Closed)
Patch Set: . Created 4 years 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 | « components/discardable_memory/public/interfaces/discardable_shared_memory_manager.mojom ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #include "mojo/public/cpp/system/platform_handle.h" 5 #include "mojo/public/cpp/system/platform_handle.h"
6 6
7 #if defined(OS_MACOSX) && !defined(OS_IOS) 7 #if defined(OS_MACOSX) && !defined(OS_IOS)
8 #include <mach/mach.h> 8 #include <mach/mach.h>
9 #include "base/mac/mach_logging.h" 9 #include "base/mac/mach_logging.h"
10 #endif 10 #endif
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 *file = PlatformFileFromPlatformHandleValue(platform_handle.value); 59 *file = PlatformFileFromPlatformHandleValue(platform_handle.value);
60 } 60 }
61 61
62 return MOJO_RESULT_OK; 62 return MOJO_RESULT_OK;
63 } 63 }
64 64
65 ScopedSharedBufferHandle WrapSharedMemoryHandle( 65 ScopedSharedBufferHandle WrapSharedMemoryHandle(
66 const base::SharedMemoryHandle& memory_handle, 66 const base::SharedMemoryHandle& memory_handle,
67 size_t size, 67 size_t size,
68 bool read_only) { 68 bool read_only) {
69 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
70 if (memory_handle == base::kInvalidPlatformFile)
yzshen1 2016/12/15 20:26:43 They are not compatible for comparison?
71 return ScopedSharedBufferHandle();
72 #else
73 if (!memory_handle.IsValid())
74 return ScopedSharedBufferHandle();
75 #endif
69 MojoPlatformHandle platform_handle; 76 MojoPlatformHandle platform_handle;
70 platform_handle.struct_size = sizeof(MojoPlatformHandle); 77 platform_handle.struct_size = sizeof(MojoPlatformHandle);
71 platform_handle.type = kPlatformSharedBufferHandleType; 78 platform_handle.type = kPlatformSharedBufferHandleType;
72 #if defined(OS_MACOSX) && !defined(OS_IOS) 79 #if defined(OS_MACOSX) && !defined(OS_IOS)
73 platform_handle.value = 80 platform_handle.value =
74 static_cast<uint64_t>(memory_handle.GetMemoryObject()); 81 static_cast<uint64_t>(memory_handle.GetMemoryObject());
75 #elif defined(OS_POSIX) 82 #elif defined(OS_POSIX)
76 platform_handle.value = PlatformHandleValueFromPlatformFile(memory_handle.fd); 83 platform_handle.value = PlatformHandleValueFromPlatformFile(memory_handle.fd);
77 #elif defined(OS_WIN) 84 #elif defined(OS_WIN)
78 platform_handle.value = 85 platform_handle.value =
(...skipping 10 matching lines...) Expand all
89 &platform_handle, size, flags, &mojo_handle); 96 &platform_handle, size, flags, &mojo_handle);
90 CHECK_EQ(result, MOJO_RESULT_OK); 97 CHECK_EQ(result, MOJO_RESULT_OK);
91 98
92 return ScopedSharedBufferHandle(SharedBufferHandle(mojo_handle)); 99 return ScopedSharedBufferHandle(SharedBufferHandle(mojo_handle));
93 } 100 }
94 101
95 MojoResult UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle, 102 MojoResult UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle,
96 base::SharedMemoryHandle* memory_handle, 103 base::SharedMemoryHandle* memory_handle,
97 size_t* size, 104 size_t* size,
98 bool* read_only) { 105 bool* read_only) {
106 if (!handle.is_valid())
107 return MOJO_RESULT_INVALID_ARGUMENT;
99 MojoPlatformHandle platform_handle; 108 MojoPlatformHandle platform_handle;
100 platform_handle.struct_size = sizeof(MojoPlatformHandle); 109 platform_handle.struct_size = sizeof(MojoPlatformHandle);
101 110
102 MojoPlatformSharedBufferHandleFlags flags; 111 MojoPlatformSharedBufferHandleFlags flags;
103 size_t num_bytes; 112 size_t num_bytes;
104 MojoResult result = MojoUnwrapPlatformSharedBufferHandle( 113 MojoResult result = MojoUnwrapPlatformSharedBufferHandle(
105 handle.release().value(), &platform_handle, &num_bytes, &flags); 114 handle.release().value(), &platform_handle, &num_bytes, &flags);
106 if (result != MOJO_RESULT_OK) 115 if (result != MOJO_RESULT_OK)
107 return result; 116 return result;
108 117
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 if (result != MOJO_RESULT_OK) 169 if (result != MOJO_RESULT_OK)
161 return result; 170 return result;
162 171
163 CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT); 172 CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT);
164 *port = static_cast<mach_port_t>(platform_handle.value); 173 *port = static_cast<mach_port_t>(platform_handle.value);
165 return MOJO_RESULT_OK; 174 return MOJO_RESULT_OK;
166 } 175 }
167 #endif // defined(OS_MACOSX) && !defined(OS_IOS) 176 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
168 177
169 } // namespace mojo 178 } // namespace mojo
OLDNEW
« no previous file with comments | « components/discardable_memory/public/interfaces/discardable_shared_memory_manager.mojom ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698