1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
/************************************************************************************
Filename : OVR_SharedMemory.cpp
Content : Inter-process shared memory subsystem
Created : June 1, 2014
Notes :
Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.1
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "OVR_SharedMemory.h"
#include "OVR_Atomic.h"
#include "OVR_Log.h"
#include "OVR_String.h"
#include "OVR_Array.h"
#if defined(OVR_OS_WIN32) && !defined(OVR_FAKE_SHAREDMEMORY)
#include <Sddl.h> // ConvertStringSecurityDescriptorToSecurityDescriptor
#endif // OVR_OS_WIN32
#if (defined(OVR_OS_LINUX) || defined(OVR_OS_MAC)) && !defined(OVR_FAKE_SHAREDMEMORY)
#include <sys/mman.h> // shm_open(), mmap()
#include <errno.h> // error results for mmap
#include <sys/stat.h> // mode constants
#include <fcntl.h> // O_ constants
#include <unistd.h> // close()
#endif // OVR_OS_LINUX
OVR_DEFINE_SINGLETON(OVR::SharedMemoryFactory);
namespace OVR {
//// Fake version
#if defined(OVR_FAKE_SHAREDMEMORY)
class FakeMemoryBlock : public RefCountBase<FakeMemoryBlock>
{
String Name;
char* Data;
int SizeBytes;
int References;
public:
FakeMemoryBlock(const String& name, int size) :
Name(name),
Data(NULL),
SizeBytes(size),
References(1)
{
Data = new char[SizeBytes];
}
~FakeMemoryBlock()
{
delete[] Data;
}
bool IsNamed(const String& name)
{
return Name.CompareNoCase(name) == 0;
}
void* GetData()
{
return Data;
}
int GetSizeI()
{
return SizeBytes;
}
void IncrementReferences()
{
++References;
}
bool DecrementReferences()
{
return --References <= 0;
}
};
class SharedMemoryInternal : public NewOverrideBase
{
public:
void* FileView;
Ptr<FakeMemoryBlock> Block;
void Close();
SharedMemoryInternal(FakeMemoryBlock* block) :
Block(block)
{
FileView = Block->GetData();
}
~SharedMemoryInternal()
{
Close();
}
static SharedMemoryInternal* CreateSharedMemory(const SharedMemory::OpenParameters& params);
};
//// FakeMemoryManager
class FakeMemoryManager : public NewOverrideBase, public SystemSingletonBase<FakeMemoryManager>
{
OVR_DECLARE_SINGLETON(FakeMemoryManager);
Lock FakeLock;
Array< Ptr<FakeMemoryBlock> > FakeArray;
public:
SharedMemoryInternal* Open(const char *name, int bytes, bool openOnly)
{
Lock::Locker locker(&FakeLock);
const int count = FakeArray.GetSizeI();
for (int ii = 0; ii < count; ++ii)
{
if (FakeArray[ii]->IsNamed(name))
{
FakeArray[ii]->IncrementReferences();
return new SharedMemoryInternal(FakeArray[ii]);
}
}
if (openOnly)
{
return NULL;
}
Ptr<FakeMemoryBlock> data = *new FakeMemoryBlock(name, bytes);
FakeArray.PushBack(data);
return new SharedMemoryInternal(data);
}
void Free(FakeMemoryBlock* block)
{
Lock::Locker locker(&FakeLock);
const int count = FakeArray.GetSizeI();
for (int ii = 0; ii < count; ++ii)
{
if (FakeArray[ii].GetPtr() == block)
{
// If the reference count hit zero,
if (FakeArray[ii]->DecrementReferences())
{
// Toast
FakeArray.RemoveAtUnordered(ii);
}
break;
}
}
}
};
FakeMemoryManager::FakeMemoryManager()
{
PushDestroyCallbacks();
}
FakeMemoryManager::~FakeMemoryManager()
{
OVR_ASSERT(FakeArray.GetSizeI() == 0);
}
void FakeMemoryManager::OnSystemDestroy()
{
delete this;
}
} // namespace OVR
OVR_DEFINE_SINGLETON(FakeMemoryManager);
namespace OVR {
void SharedMemoryInternal::Close()
{
FakeMemoryManager::GetInstance()->Free(Block);
Block.Clear();
}
SharedMemoryInternal* SharedMemoryInternal::CreateSharedMemory(const SharedMemory::OpenParameters& params)
{
return FakeMemoryManager::GetInstance()->Open(params.globalName, params.minSizeBytes, params.openMode == SharedMemory::OpenMode_OpenOnly);
}
#endif
//// Windows version
#if defined(OVR_OS_WIN32) && !defined(OVR_FAKE_SHAREDMEMORY)
#pragma comment(lib, "advapi32.lib")
// Hidden implementation class for OS-specific behavior
class SharedMemoryInternal : public NewOverrideBase
{
public:
HANDLE FileMapping;
void* FileView;
SharedMemoryInternal(HANDLE fileMapping, void* fileView) :
FileMapping(fileMapping),
FileView(fileView)
{
}
~SharedMemoryInternal()
{
// If file view is set,
if (FileView)
{
UnmapViewOfFile(FileView);
FileView = NULL;
}
// If file mapping is set,
if (FileMapping != NULL)
{
CloseHandle(FileMapping);
FileMapping = NULL;
}
}
static SharedMemoryInternal* DoFileMap(HANDLE hFileMapping, const char* fileName, bool openReadOnly, int minSize);
static SharedMemoryInternal* AttemptOpenSharedMemory(const char* fileName, int minSize, bool openReadOnly);
static SharedMemoryInternal* AttemptCreateSharedMemory(const char* fileName, int minSize, bool openReadOnly, bool allowRemoteWrite);
static SharedMemoryInternal* CreateSharedMemory(const SharedMemory::OpenParameters& params);
};
SharedMemoryInternal* SharedMemoryInternal::DoFileMap(HANDLE hFileMapping, const char* fileName, bool openReadOnly, int minSize)
{
// Interpret the access mode as a map desired access code
DWORD mapDesiredAccess = openReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE;
// Map view of the file to this process
void* pFileView = MapViewOfFile(hFileMapping, mapDesiredAccess, 0, 0, minSize);
// If mapping could not be created,
if (!pFileView)
{
CloseHandle(hFileMapping);
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to map view of file for %s error code = %d", fileName, GetLastError()));
OVR_UNUSED(fileName);
return NULL;
}
// Create internal representation
SharedMemoryInternal* pimple = new SharedMemoryInternal(hFileMapping, pFileView);
// If memory allocation fails,
if (!pimple)
{
UnmapViewOfFile(pFileView);
CloseHandle(hFileMapping);
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Out of memory"));
return NULL;
}
return pimple;
}
SharedMemoryInternal* SharedMemoryInternal::AttemptOpenSharedMemory(const char* fileName, int minSize, bool openReadOnly)
{
// Interpret the access mode as a map desired access code
DWORD mapDesiredAccess = openReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE;
// Open file mapping
HANDLE hFileMapping = OpenFileMappingA(mapDesiredAccess, TRUE, fileName);
// If file was mapped unsuccessfully,
if (NULL == hFileMapping)
{
OVR_DEBUG_LOG(("[SharedMemory] WARNING: Unable to open file mapping for %s error code = %d (not necessarily bad)", fileName, GetLastError()));
return NULL;
}
// Map the file
return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
}
SharedMemoryInternal* SharedMemoryInternal::AttemptCreateSharedMemory(const char* fileName, int minSize, bool openReadOnly, bool allowRemoteWrite)
{
// Prepare a SECURITY_ATTRIBUTES object
SECURITY_ATTRIBUTES security;
ZeroMemory(&security, sizeof(security));
security.nLength = sizeof(security);
// Security descriptor by DACL strings:
// ACE strings grant Allow(A), Object/Contains Inheritance (OICI) of:
// + Grant All (GA) to System (SY)
// + Grant All (GA) to Built-in Administrators (BA)
// + Grant Read-Only (GR) or Read-Write (GWGR) to Interactive Users (IU) - ie. games
static const char* DACLString_ReadOnly = "D:P(A;OICI;GA;;;SY)(A;OICI;GA;;;BA)(A;OICI;GR;;;IU)";
static const char* DACLString_ReadWrite = "D:P(A;OICI;GA;;;SY)(A;OICI;GA;;;BA)(A;OICI;GWGR;;;IU)";
// Select the remote process access mode
const char* remoteAccessString =
allowRemoteWrite ? DACLString_ReadWrite : DACLString_ReadOnly;
// Attempt to convert access string to security attributes
// Note: This will allocate the security descriptor with LocalAlloc() and must be freed later
BOOL bConvertOkay = ConvertStringSecurityDescriptorToSecurityDescriptorA(
remoteAccessString, SDDL_REVISION_1, &security.lpSecurityDescriptor, NULL);
// If conversion fails,
if (!bConvertOkay)
{
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to convert access string, error code = %d", GetLastError()));
return NULL;
}
// Interpret the access mode as a page protection code
int pageProtectCode = openReadOnly ? PAGE_READONLY : PAGE_READWRITE;
// Attempt to create a file mapping
HANDLE hFileMapping = CreateFileMappingA(INVALID_HANDLE_VALUE, // From page file
&security, // Security attributes
pageProtectCode, // Read-only?
0, // High word for size = 0
minSize, // Low word for size
fileName); // Name of global shared memory file
// Free the security descriptor buffer
LocalFree(security.lpSecurityDescriptor);
// If mapping could not be created,
if (NULL == hFileMapping)
{
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to create file mapping for %s error code = %d", fileName, GetLastError()));
return NULL;
}
#ifndef OVR_ALLOW_CREATE_FILE_MAPPING_IF_EXISTS
// If the file mapping already exists,
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hFileMapping);
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: File mapping at %s already exists", fileName));
return NULL;
}
#endif
// Map the file
return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
}
SharedMemoryInternal* SharedMemoryInternal::CreateSharedMemory(const SharedMemory::OpenParameters& params)
{
SharedMemoryInternal* retval = NULL;
// Construct the file mapping name in a Windows-specific way
OVR::String fileMappingName = params.globalName;
const char *fileName = fileMappingName.ToCStr();
// Is being opened read-only?
const bool openReadOnly = (params.accessMode == SharedMemory::AccessMode_ReadOnly);
// Try up to 3 times to reduce low-probability failures:
static const int ATTEMPTS_MAX = 3;
for (int attempts = 0; attempts < ATTEMPTS_MAX; ++attempts)
{
// If opening should be attempted first,
if (params.openMode != SharedMemory::OpenMode_CreateOnly)
{
// Attempt to open a shared memory map
retval = AttemptOpenSharedMemory(fileName, params.minSizeBytes, openReadOnly);
// If successful,
if (retval)
{
// Done!
break;
}
}
// If creating the shared memory is also acceptable,
if (params.openMode != SharedMemory::OpenMode_OpenOnly)
{
// Interpret create mode
const bool allowRemoteWrite = (params.remoteMode == SharedMemory::RemoteMode_ReadWrite);
// Attempt to create a shared memory map
retval = AttemptCreateSharedMemory(fileName, params.minSizeBytes, openReadOnly, allowRemoteWrite);
// If successful,
if (retval)
{
// Done!
break;
}
}
} // Re-attempt create/open
// Note: On Windows the initial contents of the region are guaranteed to be zero.
return retval;
}
#endif // OVR_OS_WIN32
#if (defined(OVR_OS_LINUX) || defined(OVR_OS_MAC)) && !defined(OVR_FAKE_SHAREDMEMORY)
// Hidden implementation class for OS-specific behavior
class SharedMemoryInternal
{
public:
int FileMapping;
void* FileView;
int FileSize;
SharedMemoryInternal(int fileMapping, void* fileView, int fileSize) :
FileMapping(fileMapping),
FileView(fileView),
FileSize(fileSize)
{
}
~SharedMemoryInternal()
{
// If file view is set,
if (FileView)
{
munmap(FileView, FileSize);
FileView = MAP_FAILED;
}
// If file mapping is set,
if (FileMapping >= 0)
{
close(FileMapping);
FileMapping = -1;
}
}
static SharedMemoryInternal* DoFileMap(int hFileMapping, const char* fileName, bool openReadOnly, int minSize);
static SharedMemoryInternal* AttemptOpenSharedMemory(const char* fileName, int minSize, bool openReadOnly);
static SharedMemoryInternal* AttemptCreateSharedMemory(const char* fileName, int minSize, bool openReadOnly, bool allowRemoteWrite);
static SharedMemoryInternal* CreateSharedMemory(const SharedMemory::OpenParameters& params);
};
SharedMemoryInternal* SharedMemoryInternal::DoFileMap(int hFileMapping, const char* fileName, bool openReadOnly, int minSize)
{
// Calculate the required flags based on read/write mode
int prot = openReadOnly ? PROT_READ : (PROT_READ|PROT_WRITE);
// Map the file view
void* pFileView = mmap(NULL, minSize, prot, MAP_SHARED, hFileMapping, 0);
if (pFileView == MAP_FAILED)
{
close(hFileMapping);
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to map view of file for %s error code = %d", fileName, errno));
OVR_UNUSED(fileName);
return NULL;
}
// Create internal representation
SharedMemoryInternal* pimple = new SharedMemoryInternal(hFileMapping, pFileView, minSize);
// If memory allocation fails,
if (!pimple)
{
munmap(pFileView, minSize);
close(hFileMapping);
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Out of memory"));
return NULL;
}
return pimple;
}
SharedMemoryInternal* SharedMemoryInternal::AttemptOpenSharedMemory(const char* fileName, int minSize, bool openReadOnly)
{
// Calculate permissions and flags based on read/write mode
int flags = openReadOnly ? O_RDONLY : O_RDWR;
int perms = openReadOnly ? S_IRUSR : (S_IRUSR | S_IWUSR);
// Attempt to open the shared memory file
int hFileMapping = shm_open(fileName, flags, perms);
// If file was not opened successfully,
if (hFileMapping < 0)
{
OVR_DEBUG_LOG(("[SharedMemory] WARNING: Unable to open file mapping for %s error code = %d (not necessarily bad)", fileName, errno));
return NULL;
}
// Map the file
return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
}
SharedMemoryInternal* SharedMemoryInternal::AttemptCreateSharedMemory(const char* fileName, int minSize, bool openReadOnly, bool allowRemoteWrite)
{
// Create mode
// Note: Cannot create the shared memory file read-only because then ftruncate() will fail.
int flags = O_CREAT | O_RDWR;
#ifndef OVR_ALLOW_CREATE_FILE_MAPPING_IF_EXISTS
// Require exclusive access when creating (seems like a good idea without trying it yet..)
if (shm_unlink(fileName) < 0)
{
OVR_DEBUG_LOG(("[SharedMemory] WARNING: Unable to unlink shared memory file %s error code = %d", fileName, errno));
}
flags |= O_EXCL;
#endif
// Set own read/write permissions
int perms = openReadOnly ? S_IRUSR : (S_IRUSR|S_IWUSR);
// Allow other users to read/write the shared memory file
perms |= allowRemoteWrite ? (S_IWGRP|S_IWOTH|S_IRGRP|S_IROTH) : (S_IRGRP|S_IROTH);
// Attempt to open the shared memory file
int hFileMapping = shm_open(fileName, flags, perms);
// If file was not opened successfully,
if (hFileMapping < 0)
{
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to create file mapping for %s error code = %d", fileName, errno));
return NULL;
}
int truncRes = ftruncate(hFileMapping, minSize);
// If file was not opened successfully,
if (truncRes < 0)
{
close(hFileMapping);
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to truncate file for %s to %d error code = %d", fileName, minSize, errno));
return NULL;
}
// Map the file
return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
}
SharedMemoryInternal* SharedMemoryInternal::CreateSharedMemory(const SharedMemory::OpenParameters& params)
{
SharedMemoryInternal* retval = NULL;
// Construct the file mapping name in a Linux-specific way
OVR::String fileMappingName = "/";
fileMappingName += params.globalName;
const char *fileName = fileMappingName.ToCStr();
// Is being opened read-only?
const bool openReadOnly = (params.accessMode == SharedMemory::AccessMode_ReadOnly);
// Try up to 3 times to reduce low-probability failures:
static const int ATTEMPTS_MAX = 3;
for (int attempts = 0; attempts < ATTEMPTS_MAX; ++attempts)
{
// If opening should be attempted first,
if (params.openMode != SharedMemory::OpenMode_CreateOnly)
{
// Attempt to open a shared memory map
retval = AttemptOpenSharedMemory(fileName, params.minSizeBytes, openReadOnly);
// If successful,
if (retval)
{
// Done!
break;
}
}
// If creating the shared memory is also acceptable,
if (params.openMode != SharedMemory::OpenMode_OpenOnly)
{
// Interpret create mode
const bool allowRemoteWrite = (params.remoteMode == SharedMemory::RemoteMode_ReadWrite);
// Attempt to create a shared memory map
retval = AttemptCreateSharedMemory(fileName, params.minSizeBytes, openReadOnly, allowRemoteWrite);
// If successful,
if (retval)
{
// Done!
break;
}
}
} // Re-attempt create/open
// Note: On Windows the initial contents of the region are guaranteed to be zero.
return retval;
}
#endif // OVR_OS_LINUX
//// SharedMemory
SharedMemory::SharedMemory(int size, void* data, SharedMemoryInternal* pInternal) :
Size(size),
Data(data),
Internal(pInternal)
{
}
// Call close when it goes out of scope
SharedMemory::~SharedMemory()
{
Close();
delete Internal;
}
void SharedMemory::Close()
{
if (Internal)
{
delete Internal;
Internal = NULL;
}
}
//// SharedMemoryFactory
Ptr<SharedMemory> SharedMemoryFactory::Open(const SharedMemory::OpenParameters& params)
{
Ptr<SharedMemory> retval;
// If no name specified or no size requested,
if (!params.globalName || (params.minSizeBytes <= 0))
{
OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Invalid parameters to Create()"));
return NULL;
}
OVR_DEBUG_LOG(("[SharedMemory] Creating shared memory region: %s > %d bytes",
params.globalName, params.minSizeBytes));
// Attempt to create a shared memory region from the parameters
SharedMemoryInternal* pInternal = SharedMemoryInternal::CreateSharedMemory(params);
if (pInternal)
{
// Create the wrapper object
retval = *new SharedMemory(params.minSizeBytes, pInternal->FileView, pInternal);
}
return retval;
}
SharedMemoryFactory::SharedMemoryFactory()
{
OVR_DEBUG_LOG(("[SharedMemory] Creating factory"));
PushDestroyCallbacks();
}
SharedMemoryFactory::~SharedMemoryFactory()
{
OVR_DEBUG_LOG(("[SharedMemory] Destroying factory"));
}
void SharedMemoryFactory::OnSystemDestroy()
{
delete this;
}
} // namespace OVR
|