forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
2711 lines (2322 loc) · 79.9 KB
/
map.cpp
File metadata and controls
2711 lines (2322 loc) · 79.9 KB
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
map.cpp
Abstract:
Implementation of file mapping API.
--*/
#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include "pal/init.h"
#include "pal/critsect.h"
#include "pal/virtual.h"
#include "common.h"
#include "pal/map.hpp"
#include "pal/thread.hpp"
#include "pal/file.hpp"
#include "pal/malloc.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include "rt/ntimage.h"
#include <pal_endian.h>
using namespace CorUnix;
SET_DEFAULT_DEBUG_CHANNEL(VIRTUAL);
//
// The mapping critical section guards access to the list
// of currently mapped views. If a thread needs to access
// both this critical section and the data for an object
// it must acquire the object data first. That is, a thread
// cannot acquire any other locks after taking hold of
// this critical section.
//
CRITICAL_SECTION mapping_critsec PAL_GLOBAL;
LIST_ENTRY MappedViewList PAL_GLOBAL;
static PAL_ERROR MAPGrowLocalFile(INT, UINT);
static PMAPPED_VIEW_LIST MAPGetViewForAddress( LPCVOID );
static PAL_ERROR MAPDesiredAccessAllowed( DWORD, DWORD, DWORD );
static INT MAPProtectionToFileOpenFlags( DWORD );
static BOOL MAPIsRequestPermissible( DWORD, CFileProcessLocalData * );
static BOOL MAPContainsInvalidFlags( DWORD );
static DWORD MAPConvertProtectToAccess( DWORD );
static INT MAPFileMapToMmapFlags( DWORD );
static DWORD MAPMmapProtToAccessFlags( int prot );
#if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
static NativeMapHolder * NewNativeMapHolder(CPalThread *pThread, LPVOID address, SIZE_T size,
SIZE_T offset, long init_ref_count);
static LONG NativeMapHolderAddRef(NativeMapHolder * thisPMH);
static LONG NativeMapHolderRelease(CPalThread *pThread, NativeMapHolder * thisPMH);
static PMAPPED_VIEW_LIST FindSharedMappingReplacement(CPalThread *pThread, dev_t deviceNum, ino_t inodeNum,
SIZE_T size, SIZE_T offset);
#endif
static PAL_ERROR
MAPRecordMapping(
IPalObject *pMappingObject,
void *pPEBaseAddress,
void *addr,
size_t len,
int prot
);
static PAL_ERROR
MAPmmapAndRecord(
IPalObject *pMappingObject,
void *pPEBaseAddress,
void *addr,
size_t len,
int prot,
int flags,
int fd,
off_t offset,
LPVOID *ppvBaseAddress
);
#if !HAVE_MMAP_DEV_ZERO
/* We need MAP_ANON. However on some platforms like HP-UX, it is defined as MAP_ANONYMOUS */
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
#define MAP_ANON MAP_ANONYMOUS
#endif
#endif
void
FileMappingCleanupRoutine(
CPalThread *pThread,
IPalObject *pObjectToCleanup,
bool fShutdown,
bool fCleanupSharedState
);
PAL_ERROR
FileMappingInitializationRoutine(
CPalThread *pThread,
CObjectType *pObjectType,
void *pImmutableData,
void *pSharedData,
void *pProcessLocalData
);
CObjectType CorUnix::otFileMapping PAL_GLOBAL(
otiFileMapping,
FileMappingCleanupRoutine,
FileMappingInitializationRoutine,
sizeof(CFileMappingImmutableData),
sizeof(CFileMappingProcessLocalData),
0,
PAGE_READWRITE | PAGE_READONLY | PAGE_WRITECOPY,
CObjectType::SecuritySupported,
CObjectType::SecurityInfoNotPersisted,
CObjectType::ObjectCanHaveName,
CObjectType::LocalDuplicationOnly,
CObjectType::UnwaitableObject,
CObjectType::SignalingNotApplicable,
CObjectType::ThreadReleaseNotApplicable,
CObjectType::OwnershipNotApplicable
);
CAllowedObjectTypes aotFileMapping PAL_GLOBAL (otiFileMapping);
void
FileMappingCleanupRoutine(
CPalThread *pThread,
IPalObject *pObjectToCleanup,
bool fShutdown,
bool fCleanupSharedState
)
{
PAL_ERROR palError = NO_ERROR;
CFileMappingImmutableData *pImmutableData = NULL;
CFileMappingProcessLocalData *pLocalData = NULL;
IDataLock *pLocalDataLock = NULL;
bool fDataChanged = FALSE;
if (TRUE == fCleanupSharedState)
{
//
// If we created a temporary file to back this mapping we need
// to unlink it now
//
palError = pObjectToCleanup->GetImmutableData(
reinterpret_cast<void**>(&pImmutableData)
);
if (NO_ERROR != palError)
{
ASSERT("Unable to obtain immutable data for object to be reclaimed");
return;
}
if (pImmutableData->bPALCreatedTempFile)
{
unlink(pImmutableData->szFileName);
}
}
if (FALSE == fShutdown)
{
//
// We only need to close the object's descriptor if we're not
// shutting down
//
palError = pObjectToCleanup->GetProcessLocalData(
pThread,
WriteLock,
&pLocalDataLock,
reinterpret_cast<void**>(&pLocalData)
);
if (NO_ERROR != palError)
{
ASSERT("Unable to obtain process local data for object to be reclaimed");
return;
}
if (-1 != pLocalData->UnixFd)
{
close(pLocalData->UnixFd);
pLocalData->UnixFd = -1;
fDataChanged = TRUE;
}
pLocalDataLock->ReleaseLock(pThread, fDataChanged);
}
//
// Why don't we need to deal with any views that may have been created
// from this mapping? If the process is shutting down then there's nothing
// that we need to take care of, as the OS will remove the underlying
// mappings when the process goes away. If we're not shutting down then
// there's no way for a view to exist against this mapping, since each
// view holds a reference against the mapping object.
//
}
PAL_ERROR
FileMappingInitializationRoutine(
CPalThread *pThread,
CObjectType *pObjectType,
void *pvImmutableData,
void *pvSharedData,
void *pvProcessLocalData
)
{
PAL_ERROR palError = NO_ERROR;
CFileMappingImmutableData *pImmutableData =
reinterpret_cast<CFileMappingImmutableData *>(pvImmutableData);
CFileMappingProcessLocalData *pProcessLocalData =
reinterpret_cast<CFileMappingProcessLocalData *>(pvProcessLocalData);
pProcessLocalData->UnixFd = InternalOpen(
pImmutableData->szFileName,
MAPProtectionToFileOpenFlags(pImmutableData->flProtect)
);
if (-1 == pProcessLocalData->UnixFd)
{
palError = ERROR_INTERNAL_ERROR;
goto ExitFileMappingInitializationRoutine;
}
#if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
struct stat st;
if (0 == fstat(pProcessLocalData->UnixFd, &st))
{
pProcessLocalData->MappedFileDevNum = st.st_dev;
pProcessLocalData->MappedFileInodeNum = st.st_ino;
}
else
{
ERROR("Couldn't get inode info for fd=%d to be stored in mapping object\n", pProcessLocalData->UnixFd);
}
#endif
ExitFileMappingInitializationRoutine:
return palError;
}
/*++
Function:
CreateFileMappingA
Note:
File mapping are used to do inter-process communication.
See MSDN doc.
--*/
HANDLE
PALAPI
CreateFileMappingA(
IN HANDLE hFile,
IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
IN DWORD flProtect,
IN DWORD dwMaximumSizeHigh,
IN DWORD dwMaximumSizeLow,
IN LPCSTR lpName)
{
HANDLE hFileMapping = NULL;
CPalThread *pThread = NULL;
PAL_ERROR palError = NO_ERROR;
PERF_ENTRY(CreateFileMappingA);
ENTRY("CreateFileMappingA(hFile=%p, lpAttributes=%p, flProtect=%#x, "
"dwMaxSizeH=%d, dwMaxSizeL=%d, lpName=%p (%s))\n",
hFile, lpFileMappingAttributes, flProtect,
dwMaximumSizeHigh, dwMaximumSizeLow,
lpName?lpName:"NULL",
lpName?lpName:"NULL");
pThread = InternalGetCurrentThread();
if (lpName != nullptr)
{
ASSERT("lpName: Cross-process named objects are not supported in PAL");
palError = ERROR_NOT_SUPPORTED;
}
else
{
palError = InternalCreateFileMapping(
pThread,
hFile,
lpFileMappingAttributes,
flProtect,
dwMaximumSizeHigh,
dwMaximumSizeLow,
NULL,
&hFileMapping
);
}
//
// We always need to set last error, even on success:
// we need to protect ourselves from the situation
// where last error is set to ERROR_ALREADY_EXISTS on
// entry to the function
//
pThread->SetLastError(palError);
LOGEXIT( "CreateFileMappingA returns HANDLE %p. \n", hFileMapping );
PERF_EXIT(CreateFileMappingA);
return hFileMapping;
}
/*++
Function:
CreateFileMappingW
Note:
File mapping are used to do inter-process communication.
See MSDN doc.
--*/
HANDLE
PALAPI
CreateFileMappingW(
IN HANDLE hFile,
IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
IN DWORD flProtect,
IN DWORD dwMaximumSizeHigh,
IN DWORD dwMaximumSizeLow,
IN LPCWSTR lpName)
{
HANDLE hFileMapping = NULL;
CPalThread *pThread = NULL;
PAL_ERROR palError = NO_ERROR;
PERF_ENTRY(CreateFileMappingW);
ENTRY("CreateFileMappingW(hFile=%p, lpAttributes=%p, flProtect=%#x, "
"dwMaxSizeH=%u, dwMaxSizeL=%u, lpName=%p (%S))\n",
hFile, lpFileMappingAttributes, flProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, lpName?lpName:W16_NULLSTRING, lpName?lpName:W16_NULLSTRING);
pThread = InternalGetCurrentThread();
palError = InternalCreateFileMapping(
pThread,
hFile,
lpFileMappingAttributes,
flProtect,
dwMaximumSizeHigh,
dwMaximumSizeLow,
lpName,
&hFileMapping
);
//
// We always need to set last error, even on success:
// we need to protect ourselves from the situation
// where last error is set to ERROR_ALREADY_EXISTS on
// entry to the function
//
pThread->SetLastError(palError);
LOGEXIT( "CreateFileMappingW returning %p .\n", hFileMapping );
PERF_EXIT(CreateFileMappingW);
return hFileMapping;
}
PAL_ERROR
CorUnix::InternalCreateFileMapping(
CPalThread *pThread,
HANDLE hFile,
LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
DWORD flProtect,
DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow,
LPCWSTR lpName,
HANDLE *phMapping
)
{
CObjectAttributes objectAttributes(lpName, lpFileMappingAttributes);
PAL_ERROR palError = NO_ERROR;
IPalObject *pMapping = NULL;
IPalObject *pRegisteredMapping = NULL;
CFileMappingProcessLocalData *pLocalData = NULL;
IDataLock *pLocalDataLock = NULL;
CFileMappingImmutableData *pImmutableData = NULL;
IPalObject *pFileObject = NULL;
CFileProcessLocalData *pFileLocalData = NULL;
IDataLock *pFileLocalDataLock = NULL;
struct stat UnixFileInformation;
INT UnixFd = -1;
BOOL bPALCreatedTempFile = FALSE;
UINT nFileSize = 0;
//
// Validate parameters
//
if (lpName != nullptr)
{
ASSERT("lpName: Cross-process named objects are not supported in PAL");
palError = ERROR_NOT_SUPPORTED;
goto ExitInternalCreateFileMapping;
}
if (0 != dwMaximumSizeHigh)
{
ASSERT("dwMaximumSizeHigh is always 0.\n");
palError = ERROR_INVALID_PARAMETER;
goto ExitInternalCreateFileMapping;
}
if (PAGE_READWRITE != flProtect
&& PAGE_READONLY != flProtect
&& PAGE_WRITECOPY != flProtect)
{
ASSERT( "invalid flProtect %#x, acceptable values are PAGE_READONLY "
"(%#x), PAGE_READWRITE (%#x) and PAGE_WRITECOPY (%#x).\n",
flProtect, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY );
palError = ERROR_INVALID_PARAMETER;
goto ExitInternalCreateFileMapping;
}
if (hFile == INVALID_HANDLE_VALUE && 0 == dwMaximumSizeLow)
{
ERROR( "If hFile is INVALID_HANDLE_VALUE, then you must specify a size.\n" );
palError = ERROR_INVALID_PARAMETER;
goto ExitInternalCreateFileMapping;
}
if (hFile != INVALID_HANDLE_VALUE && NULL != lpName)
{
ASSERT( "If hFile is not -1, then lpName must be NULL.\n" );
palError = ERROR_INVALID_PARAMETER;
goto ExitInternalCreateFileMapping;
}
palError = g_pObjectManager->AllocateObject(
pThread,
&otFileMapping,
&objectAttributes,
&pMapping
);
if (NO_ERROR != palError)
{
goto ExitInternalCreateFileMapping;
}
palError = pMapping->GetImmutableData(reinterpret_cast<void**>(&pImmutableData));
if (NO_ERROR != palError)
{
goto ExitInternalCreateFileMapping;
}
if (hFile == INVALID_HANDLE_VALUE)
{
//
// Note: this path is what prevents us supporting the
// duplication of file mapping objects across processes, since
// there is no backing file that the other process can open. We can
// avoid this restriction by always using a temp backing file for
// anonymous mappings.
//
/* Anonymous mapped files. */
if (strcpy_s(pImmutableData->szFileName, sizeof(pImmutableData->szFileName), "/dev/zero") != SAFECRT_SUCCESS)
{
ERROR( "strcpy_s failed!\n" );
palError = ERROR_INTERNAL_ERROR;
goto ExitInternalCreateFileMapping;
}
#if HAVE_MMAP_DEV_ZERO
UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR);
if ( -1 == UnixFd )
{
ERROR( "Unable to open the file.\n");
palError = ERROR_INTERNAL_ERROR;
goto ExitInternalCreateFileMapping;
}
#else //!HAVE_MMAP_DEV_ZERO
UnixFd = -1; /* will pass MAP_ANON to mmap() instead */
#endif //!HAVE_MMAP_DEV_ZERO
}
else
{
if ( hFile != INVALID_HANDLE_VALUE )
{
palError = g_pObjectManager->ReferenceObjectByHandle(
pThread,
hFile,
&aotFile,
GENERIC_READ,
&pFileObject
);
if (NO_ERROR != palError)
{
ERROR("Unable to obtain file data.\n");
palError = ERROR_INVALID_PARAMETER;
goto ExitInternalCreateFileMapping;
}
palError = pFileObject->GetProcessLocalData(
pThread,
ReadLock,
&pFileLocalDataLock,
reinterpret_cast<void**>(&pFileLocalData)
);
if (NO_ERROR != palError)
{
goto ExitInternalCreateFileMapping;
}
/* We need to check to ensure flProtect jives with
the permission on the file handle */
if (!MAPIsRequestPermissible(flProtect, pFileLocalData))
{
ERROR("File handle does not have the correct "
"permissions to create mapping\n" );
palError = ERROR_ACCESS_DENIED;
if (NULL != pFileLocalDataLock)
{
pFileLocalDataLock->ReleaseLock(pThread, FALSE);
}
goto ExitInternalCreateFileMapping;
}
//
// TODO: technically, the file mapping object should hold
// a reference to the passed in file object. This implementation
// only keeps the underlying native file structure (i.e., what
// the duplicated descriptors point to) open. There may be a risk
// here pertaining to the file lock information that the PAL must
// maintain (e.g,. if the passed in handle is closed immediately
// after the file mapping is opened then the lock information will
// be released, since we're not doing anything to keep it alive
// here).
//
// Having a direct reference to the underlying file object adds
// some complication, especially in cross-process cases. We may
// want to consider adding a reference to the PAL's file lock
// information, though...
//
UnixFd = dup(pFileLocalData->unix_fd);
if (-1 == UnixFd)
{
ERROR( "Unable to duplicate the Unix file descriptor!\n" );
palError = ERROR_INTERNAL_ERROR;
if (NULL != pFileLocalDataLock)
{
pFileLocalDataLock->ReleaseLock(pThread, FALSE);
}
goto ExitInternalCreateFileMapping;
}
if (strcpy_s(pImmutableData->szFileName, sizeof(pImmutableData->szFileName), pFileLocalData->unix_filename) != SAFECRT_SUCCESS)
{
ERROR( "strcpy_s failed!\n" );
palError = ERROR_INTERNAL_ERROR;
if (NULL != pFileLocalDataLock)
{
pFileLocalDataLock->ReleaseLock(pThread, FALSE);
}
goto ExitInternalCreateFileMapping;
}
if (NULL != pFileLocalDataLock)
{
pFileLocalDataLock->ReleaseLock(pThread, FALSE);
}
}
else
{
ASSERT("should not get here\n");
palError = ERROR_INTERNAL_ERROR;
goto ExitInternalCreateFileMapping;
}
if (-1 == fstat(UnixFd, &UnixFileInformation))
{
ASSERT("fstat() failed for this reason %s.\n", strerror(errno));
palError = ERROR_INTERNAL_ERROR;
goto ExitInternalCreateFileMapping;
}
if ( 0 == UnixFileInformation.st_size &&
0 == dwMaximumSizeHigh && 0 == dwMaximumSizeLow )
{
ERROR( "The file cannot be a zero length file.\n" );
palError = ERROR_FILE_INVALID;
goto ExitInternalCreateFileMapping;
}
if ( INVALID_HANDLE_VALUE != hFile &&
dwMaximumSizeLow > (DWORD) UnixFileInformation.st_size &&
( PAGE_READONLY == flProtect || PAGE_WRITECOPY == flProtect ) )
{
/* In this situation, Windows returns an error, because the
permissions requested do not allow growing the file */
ERROR( "The file cannot be grown do to the map's permissions.\n" );
palError = ERROR_NOT_ENOUGH_MEMORY;
goto ExitInternalCreateFileMapping;
}
if ( (DWORD) UnixFileInformation.st_size < dwMaximumSizeLow )
{
TRACE( "Growing the size of file on disk to match requested size.\n" );
/* Need to grow the file on disk to match size. */
palError = MAPGrowLocalFile(UnixFd, dwMaximumSizeLow);
if (NO_ERROR != palError)
{
ERROR( "Unable to grow the file on disk.\n" );
goto ExitInternalCreateFileMapping;
}
}
}
nFileSize = ( 0 == dwMaximumSizeLow && 0 == dwMaximumSizeHigh ) ?
UnixFileInformation.st_size : dwMaximumSizeLow;
pImmutableData->MaxSize = nFileSize;
pImmutableData->flProtect = flProtect;
pImmutableData->bPALCreatedTempFile = bPALCreatedTempFile;
pImmutableData->dwDesiredAccessWhenOpened = MAPConvertProtectToAccess(flProtect);
//
// The local data isn't grabbed / modified until here so that we don't
// need to worry ourselves with locking issues with the passed in
// file handle -- all operations concerning the file handle are completed
// before we deal with the lock for the new object.
//
palError = pMapping->GetProcessLocalData(
pThread,
WriteLock,
&pLocalDataLock,
reinterpret_cast<void**>(&pLocalData)
);
if (NO_ERROR != palError)
{
goto ExitInternalCreateFileMapping;
}
pLocalData->UnixFd = UnixFd;
#if ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
if (-1 == UnixFd)
{
pLocalData->MappedFileDevNum = (dev_t)-1; /* there is no standard NO_DEV */
pLocalData->MappedFileInodeNum = NO_INO;
}
else
{
struct stat st;
if (0 == fstat(UnixFd, &st))
{
pLocalData->MappedFileDevNum = st.st_dev;
pLocalData->MappedFileInodeNum = st.st_ino;
}
else
{
ERROR("Couldn't get inode info for fd=%d to be stored in mapping object\n", UnixFd);
palError = ERROR_INTERNAL_ERROR;
goto ExitInternalCreateFileMapping;
}
}
#endif
pLocalDataLock->ReleaseLock(pThread, TRUE);
pLocalDataLock = NULL;
palError = g_pObjectManager->RegisterObject(
pThread,
pMapping,
&aotFileMapping,
flProtect, // TODO: is flProtect really an access right?
phMapping,
&pRegisteredMapping
);
//
// pMapping is invalidated by the call to RegisterObject, so NULL it
// out here to ensure that we don't try to release a reference on
// it down the line. This also ensures that we won't attempt to release
// any data associated with the mapping object here, as if any cleanup is
// necessary due to a failure in RegisterObject (which includes another
// object by the same name already existing) the cleanup will take place
// when that routine releases the reference to pMapping.
//
pMapping = NULL;
ExitInternalCreateFileMapping:
if (NULL != pLocalDataLock)
{
pLocalDataLock->ReleaseLock(
pThread,
TRUE
);
}
if (NULL != pMapping)
{
pMapping->ReleaseReference(pThread);
if (bPALCreatedTempFile)
{
unlink(pImmutableData->szFileName);
}
if (-1 != UnixFd)
{
close(UnixFd);
}
}
if (NULL != pRegisteredMapping)
{
pRegisteredMapping->ReleaseReference(pThread);
}
if (NULL != pFileObject)
{
pFileObject->ReleaseReference(pThread);
}
return palError;
}
/*++
Function:
OpenFileMappingA
See MSDN doc.
--*/
HANDLE
PALAPI
OpenFileMappingA(
IN DWORD dwDesiredAccess,
IN BOOL bInheritHandle,
IN LPCSTR lpName)
{
HANDLE hFileMapping = NULL;
CPalThread *pThread = NULL;
PAL_ERROR palError = NO_ERROR;
PERF_ENTRY(OpenFileMappingA);
ENTRY("OpenFileMappingA(dwDesiredAccess=%u, bInheritHandle=%d, lpName=%p (%s)\n",
dwDesiredAccess, bInheritHandle, lpName?lpName:"NULL", lpName?lpName:"NULL");
pThread = InternalGetCurrentThread();
if (lpName == nullptr)
{
ERROR("name is NULL\n");
palError = ERROR_INVALID_PARAMETER;
}
else
{
ASSERT("lpName: Cross-process named objects are not supported in PAL");
palError = ERROR_NOT_SUPPORTED;
}
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT( "OpenFileMappingA returning %p\n", hFileMapping );
PERF_EXIT(OpenFileMappingA);
return hFileMapping;
}
/*++
Function:
OpenFileMappingW
See MSDN doc.
--*/
HANDLE
PALAPI
OpenFileMappingW(
IN DWORD dwDesiredAccess,
IN BOOL bInheritHandle,
IN LPCWSTR lpName)
{
HANDLE hFileMapping = NULL;
PAL_ERROR palError = NO_ERROR;
CPalThread *pThread = NULL;
PERF_ENTRY(OpenFileMappingW);
ENTRY("OpenFileMappingW(dwDesiredAccess=%#x, bInheritHandle=%d, lpName=%p (%S)\n",
dwDesiredAccess, bInheritHandle, lpName?lpName:W16_NULLSTRING, lpName?lpName:W16_NULLSTRING);
pThread = InternalGetCurrentThread();
/* validate parameters */
if (lpName == nullptr)
{
ERROR("name is NULL\n");
palError = ERROR_INVALID_PARAMETER;
}
else
{
ASSERT("lpName: Cross-process named objects are not supported in PAL");
palError = ERROR_NOT_SUPPORTED;
}
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT("OpenFileMappingW returning %p.\n", hFileMapping);
PERF_EXIT(OpenFileMappingW);
return hFileMapping;
}
PAL_ERROR
CorUnix::InternalOpenFileMapping(
CPalThread *pThread,
DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCWSTR lpName,
HANDLE *phMapping
)
{
PAL_ERROR palError = NO_ERROR;
IPalObject *pFileMapping = NULL;
CPalString sObjectName(lpName);
if ( MAPContainsInvalidFlags( dwDesiredAccess ) )
{
ASSERT( "dwDesiredAccess can be one or more of FILE_MAP_READ, "
"FILE_MAP_WRITE, FILE_MAP_COPY or FILE_MAP_ALL_ACCESS.\n" );
palError = ERROR_INVALID_PARAMETER;
goto ExitInternalOpenFileMapping;
}
palError = g_pObjectManager->LocateObject(
pThread,
&sObjectName,
&aotFileMapping,
&pFileMapping
);
if (NO_ERROR != palError)
{
goto ExitInternalOpenFileMapping;
}
palError = g_pObjectManager->ObtainHandleForObject(
pThread,
pFileMapping,
dwDesiredAccess,
bInheritHandle,
NULL,
phMapping
);
if (NO_ERROR != palError)
{
goto ExitInternalOpenFileMapping;
}
ExitInternalOpenFileMapping:
if (NULL != pFileMapping)
{
pFileMapping->ReleaseReference(pThread);
}
return palError;
}
/*++
Function:
MapViewOfFile
Limitations: 1) Currently file mappings are supported only at file
offset 0.
2) Some platforms (specifically HP-UX) do not support
multiple simultaneous shared mapping of the same file
region in the same process. On these platforms, in case
we are asked for a new view completely contained in an
existing one, we return an address within the existing
mapping. In case the new requested view is overlapping
with the existing one, but not contained in it, the
mapping is impossible, and MapViewOfFile will fail.
Since currently the mappings are supported only at file
offset 0, MapViewOfFile will succeed if the new view
is equal or smaller of the existing one, and the address
returned will be the same address of the existing
mapping.
Since the underlying mapping is always the same, all
the shared views of the same file region will share the
same protection, i.e. they will have the largest
protection requested. If any mapping asked for a
read-write access, all the read-only mappings of the
same region will silently get a read-write access to
it.
See MSDN doc.
--*/
LPVOID
PALAPI
MapViewOfFile(
IN HANDLE hFileMappingObject,
IN DWORD dwDesiredAccess,
IN DWORD dwFileOffsetHigh,
IN DWORD dwFileOffsetLow,
IN SIZE_T dwNumberOfBytesToMap)
{
PAL_ERROR palError = NO_ERROR;
CPalThread *pThread = NULL;
LPVOID pvMappedBaseAddress = NULL;
PERF_ENTRY(MapViewOfFile);
ENTRY("MapViewOfFile(hFileMapping=%p, dwDesiredAccess=%u, "
"dwFileOffsetH=%u, dwFileOffsetL=%u, dwNumberOfBytes=%u)\n",
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh,
dwFileOffsetLow, dwNumberOfBytesToMap);
pThread = InternalGetCurrentThread();
palError = InternalMapViewOfFile(
pThread,
hFileMappingObject,
dwDesiredAccess,
dwFileOffsetHigh,
dwFileOffsetLow,
dwNumberOfBytesToMap,
&pvMappedBaseAddress
);
if (NO_ERROR != palError)
{
pThread->SetLastError(palError);
}
LOGEXIT( "MapViewOfFile returning %p.\n", pvMappedBaseAddress );
PERF_EXIT(MapViewOfFile);
return pvMappedBaseAddress;
}
LPVOID
PALAPI
MapViewOfFileEx(
IN HANDLE hFileMappingObject,
IN DWORD dwDesiredAccess,
IN DWORD dwFileOffsetHigh,
IN DWORD dwFileOffsetLow,
IN SIZE_T dwNumberOfBytesToMap,
IN LPVOID lpBaseAddress)
{
PAL_ERROR palError = NO_ERROR;
CPalThread *pThread = NULL;
LPVOID pvMappedBaseAddress = NULL;
PERF_ENTRY(MapViewOfFileEx);
ENTRY("MapViewOfFileEx(hFileMapping=%p, dwDesiredAccess=%u, "
"dwFileOffsetH=%u, dwFileOffsetL=%u, dwNumberOfBytes=%u, lpBaseAddress=%p)\n",
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh,
dwFileOffsetLow, dwNumberOfBytesToMap, lpBaseAddress);