1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/FileUtils.h"
10 #include "private/pprio.h"
12 #include "mozilla/BaseProfilerMarkers.h"
13 #include "mozilla/MemUtils.h"
15 #if defined(XP_MACOSX)
18 # include <mach/machine.h>
19 # include <mach-o/fat.h>
20 # include <mach-o/loader.h>
21 # include <sys/mman.h>
22 # include <sys/stat.h>
24 #elif defined(XP_UNIX)
30 # include <sys/types.h>
31 # include <sys/stat.h>
33 # include <nsWindowsHelpers.h>
34 # include <mozilla/NativeNt.h>
35 # include <mozilla/ScopeExit.h>
38 // Functions that are not to be used in standalone glue must be implemented
39 // within this #if block
40 #if defined(MOZILLA_INTERNAL_API)
42 # include "nsString.h"
44 bool mozilla::fallocate(PRFileDesc
* aFD
, int64_t aLength
) {
45 # if defined(HAVE_POSIX_FALLOCATE)
46 return posix_fallocate(PR_FileDesc2NativeHandle(aFD
), 0, aLength
) == 0;
47 # elif defined(XP_WIN)
48 int64_t oldpos
= PR_Seek64(aFD
, 0, PR_SEEK_CUR
);
53 if (PR_Seek64(aFD
, aLength
, PR_SEEK_SET
) != aLength
) {
57 bool retval
= (0 != SetEndOfFile((HANDLE
)PR_FileDesc2NativeHandle(aFD
)));
59 PR_Seek64(aFD
, oldpos
, PR_SEEK_SET
);
61 # elif defined(XP_MACOSX)
62 int fd
= PR_FileDesc2NativeHandle(aFD
);
63 fstore_t store
= {F_ALLOCATECONTIG
, F_PEOFPOSMODE
, 0, aLength
};
64 // Try to get a continous chunk of disk space
65 int ret
= fcntl(fd
, F_PREALLOCATE
, &store
);
67 // OK, perhaps we are too fragmented, allocate non-continuous
68 store
.fst_flags
= F_ALLOCATEALL
;
69 ret
= fcntl(fd
, F_PREALLOCATE
, &store
);
74 return ftruncate(fd
, aLength
) == 0;
75 # elif defined(XP_UNIX)
76 // The following is copied from fcntlSizeHint in sqlite
77 /* If the OS does not have posix_fallocate(), fake it. First use
78 ** ftruncate() to set the file size, then write a single byte to
79 ** the last byte in each block within the extended region. This
80 ** is the same technique used by glibc to implement posix_fallocate()
81 ** on systems that do not have a real fallocate() system call.
83 int64_t oldpos
= PR_Seek64(aFD
, 0, PR_SEEK_CUR
);
89 int fd
= PR_FileDesc2NativeHandle(aFD
);
90 if (fstat(fd
, &buf
)) {
94 if (buf
.st_size
>= aLength
) {
98 const int nBlk
= buf
.st_blksize
;
104 if (ftruncate(fd
, aLength
)) {
108 int nWrite
; // Return value from write()
109 int64_t iWrite
= ((buf
.st_size
+ 2 * nBlk
- 1) / nBlk
) * nBlk
-
110 1; // Next offset to write to
111 while (iWrite
< aLength
) {
113 if (PR_Seek64(aFD
, iWrite
, PR_SEEK_SET
) == iWrite
) {
114 nWrite
= PR_Write(aFD
, "", 1);
122 PR_Seek64(aFD
, oldpos
, PR_SEEK_SET
);
129 void mozilla::ReadAheadLib(nsIFile
* aFile
) {
132 if (!aFile
|| NS_FAILED(aFile
->GetPath(path
))) {
135 ReadAheadLib(path
.get());
136 # elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
137 nsAutoCString nativePath
;
138 if (!aFile
|| NS_FAILED(aFile
->GetNativePath(nativePath
))) {
141 ReadAheadLib(nativePath
.get());
145 void mozilla::ReadAheadFile(nsIFile
* aFile
, const size_t aOffset
,
146 const size_t aCount
, mozilla::filedesc_t
* aOutFd
) {
149 if (!aFile
|| NS_FAILED(aFile
->GetPath(path
))) {
152 ReadAheadFile(path
.get(), aOffset
, aCount
, aOutFd
);
153 # elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
154 nsAutoCString nativePath
;
155 if (!aFile
|| NS_FAILED(aFile
->GetNativePath(nativePath
))) {
158 ReadAheadFile(nativePath
.get(), aOffset
, aCount
, aOutFd
);
162 mozilla::PathString
mozilla::GetLibraryName(mozilla::pathstr_t aDirectory
,
165 nsAutoString fullName
;
167 fullName
.Assign(aDirectory
);
168 fullName
.Append('\\');
170 AppendUTF8toUTF16(MakeStringSpan(aLib
), fullName
);
171 if (!strstr(aLib
, ".dll")) {
172 fullName
.AppendLiteral(".dll");
174 return std::move(fullName
);
176 char* temp
= PR_GetLibraryName(aDirectory
, aLib
);
180 nsAutoCString
libname(temp
);
181 PR_FreeLibraryName(temp
);
182 return std::move(libname
);
186 mozilla::PathString
mozilla::GetLibraryFilePathname(mozilla::pathstr_t aName
,
189 HMODULE handle
= GetModuleHandleW(char16ptr_t(aName
));
195 path
.SetLength(MAX_PATH
);
196 DWORD len
= GetModuleFileNameW(handle
, char16ptr_t(path
.BeginWriting()),
203 return std::move(path
);
205 char* temp
= PR_GetLibraryFilePathname(aName
, aAddr
);
209 nsAutoCString
path(temp
);
210 PR_Free(temp
); // PR_GetLibraryFilePathname() uses PR_Malloc().
211 return std::move(path
);
215 #endif // defined(MOZILLA_INTERNAL_API)
217 #if defined(LINUX) && !defined(ANDROID)
219 static const unsigned int bufsize
= 4096;
222 typedef Elf64_Ehdr Elf_Ehdr
;
223 typedef Elf64_Phdr Elf_Phdr
;
224 static const unsigned char ELFCLASS
= ELFCLASS64
;
225 typedef Elf64_Off Elf_Off
;
227 typedef Elf32_Ehdr Elf_Ehdr
;
228 typedef Elf32_Phdr Elf_Phdr
;
229 static const unsigned char ELFCLASS
= ELFCLASS32
;
230 typedef Elf32_Off Elf_Off
;
233 #elif defined(XP_MACOSX)
235 # if defined(__i386__)
236 static const uint32_t CPU_TYPE
= CPU_TYPE_X86
;
237 # elif defined(__x86_64__)
238 static const uint32_t CPU_TYPE
= CPU_TYPE_X86_64
;
239 # elif defined(__ppc__)
240 static const uint32_t CPU_TYPE
= CPU_TYPE_POWERPC
;
241 # elif defined(__ppc64__)
242 static const uint32_t CPU_TYPE
= CPU_TYPE_POWERPC64
;
243 # elif defined(__aarch64__)
244 static const uint32_t CPU_TYPE
= CPU_TYPE_ARM64
;
246 # error Unsupported CPU type
251 # define LC_SEGMENT LC_SEGMENT_64
253 # define MH_MAGIC MH_MAGIC_64
254 # define cpu_mach_header mach_header_64
255 # define segment_command segment_command_64
257 # define cpu_mach_header mach_header
262 explicit ScopedMMap(const char* aFilePath
) : buf(nullptr) {
263 fd
= open(aFilePath
, O_RDONLY
);
268 if (fstat(fd
, &st
) < 0) {
272 buf
= (char*)mmap(nullptr, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
273 if (buf
== MAP_FAILED
) {
285 operator char*() { return buf
; }
286 int getFd() { return fd
; }
295 void mozilla::ReadAhead(mozilla::filedesc_t aFd
, const size_t aOffset
,
296 const size_t aCount
) {
299 LARGE_INTEGER fpOriginal
;
300 LARGE_INTEGER fpOffset
;
301 # if defined(HAVE_LONG_LONG)
302 fpOffset
.QuadPart
= 0;
304 fpOffset
.u
.LowPart
= 0;
305 fpOffset
.u
.HighPart
= 0;
308 // Get the current file pointer so that we can restore it. This isn't
309 // really necessary other than to provide the same semantics regarding the
310 // file pointer that other platforms do
311 if (!SetFilePointerEx(aFd
, fpOffset
, &fpOriginal
, FILE_CURRENT
)) {
316 # if defined(HAVE_LONG_LONG)
317 fpOffset
.QuadPart
= static_cast<LONGLONG
>(aOffset
);
319 fpOffset
.u
.LowPart
= aOffset
;
320 fpOffset
.u
.HighPart
= 0;
323 if (!SetFilePointerEx(aFd
, fpOffset
, nullptr, FILE_BEGIN
)) {
329 size_t totalBytesRead
= 0;
331 // Do dummy reads to trigger kernel-side readhead via
332 // FILE_FLAG_SEQUENTIAL_SCAN. Abort when underfilling because during testing
333 // the buffers are read fully A buffer that's not keeping up would imply that
334 // readahead isn't working right
335 while (totalBytesRead
< aCount
&&
336 ReadFile(aFd
, buf
, sizeof(buf
), &dwBytesRead
, nullptr) &&
337 dwBytesRead
== sizeof(buf
)) {
338 totalBytesRead
+= dwBytesRead
;
341 // Restore the file pointer
342 SetFilePointerEx(aFd
, fpOriginal
, nullptr, FILE_BEGIN
);
344 #elif defined(LINUX) && !defined(ANDROID)
346 readahead(aFd
, aOffset
, aCount
);
348 #elif defined(XP_MACOSX)
351 ra
.ra_offset
= aOffset
;
352 ra
.ra_count
= aCount
;
353 // The F_RDADVISE fcntl is equivalent to Linux' readahead() system call.
354 fcntl(aFd
, F_RDADVISE
, &ra
);
359 void mozilla::ReadAheadLib(mozilla::pathstr_t aFilePath
) {
365 auto WideToUTF8
= [](const wchar_t* aStr
) -> std::string
{
367 // Determine the number of output bytes (including null terminator).
368 const int numConv
= ::WideCharToMultiByte(CP_UTF8
, 0, aStr
, -1, nullptr, 0,
374 const int numConvd
= ::WideCharToMultiByte(CP_UTF8
, 0, aStr
, -1, s
.data(),
375 numConv
, nullptr, nullptr);
376 if (numConvd
!= numConv
) {
377 // Error during conversion, remove any temporary data.
384 AUTO_BASE_PROFILER_MARKER_TEXT("ReadAheadLib", OTHER
, {},
386 WideToUTF8(aFilePath
)
393 if (!CanPrefetchMemory()) {
394 ReadAheadFile(aFilePath
);
397 nsAutoHandle
fd(CreateFileW(aFilePath
, GENERIC_READ
| GENERIC_EXECUTE
,
398 FILE_SHARE_READ
, nullptr, OPEN_EXISTING
,
399 FILE_FLAG_SEQUENTIAL_SCAN
, nullptr));
404 nsAutoHandle
mapping(CreateFileMapping(
405 fd
, nullptr, SEC_IMAGE
| PAGE_EXECUTE_READ
, 0, 0, nullptr));
410 PVOID data
= MapViewOfFile(
411 mapping
, FILE_MAP_READ
| FILE_MAP_EXECUTE
| SEC_IMAGE
, 0, 0, 0);
415 auto guard
= MakeScopeExit([=]() { UnmapViewOfFile(data
); });
416 mozilla::nt::PEHeaders
headers(data
);
417 Maybe
<Span
<const uint8_t>> bounds
= headers
.GetBounds();
422 PrefetchMemory((uint8_t*)data
, bounds
->Length());
424 #elif defined(LINUX) && !defined(ANDROID)
425 int fd
= open(aFilePath
, O_RDONLY
);
434 // Read ELF header (ehdr) and program header table (phdr).
435 // We check that the ELF magic is found, that the ELF class matches
436 // our own, and that the program header table as defined in the ELF
437 // headers fits in the buffer we read.
438 if ((read(fd
, elf
.buf
, bufsize
) <= 0) || (memcmp(elf
.buf
, ELFMAG
, 4)) ||
439 (elf
.ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
) ||
440 // Upcast e_phentsize so the multiplication is done in the same precision
441 // as the subsequent addition, to satisfy static analyzers and avoid
442 // issues with abnormally large program header tables.
444 (static_cast<Elf_Off
>(elf
.ehdr
.e_phentsize
) * elf
.ehdr
.e_phnum
) >=
449 // The program header table contains segment definitions. One such
450 // segment type is PT_LOAD, which describes how the dynamic loader
451 // is going to map the file in memory. We use that information to
452 // find the biggest offset from the library that will be mapped in
454 Elf_Phdr
* phdr
= (Elf_Phdr
*)&elf
.buf
[elf
.ehdr
.e_phoff
];
456 for (int phnum
= elf
.ehdr
.e_phnum
; phnum
; phdr
++, phnum
--) {
457 if ((phdr
->p_type
== PT_LOAD
) && (end
< phdr
->p_offset
+ phdr
->p_filesz
)) {
458 end
= phdr
->p_offset
+ phdr
->p_filesz
;
461 // Let the kernel read ahead what the dynamic loader is going to
462 // map in memory soon after.
464 ReadAhead(fd
, 0, end
);
467 #elif defined(XP_MACOSX)
468 ScopedMMap
buf(aFilePath
);
474 // An OSX binary might either be a fat (universal) binary or a
475 // Mach-O binary. A fat binary actually embeds several Mach-O
476 // binaries. If we have a fat binary, find the offset where the
477 // Mach-O binary for our CPU type can be found.
478 struct fat_header
* fh
= (struct fat_header
*)base
;
480 if (OSSwapBigToHostInt32(fh
->magic
) == FAT_MAGIC
) {
481 uint32_t nfat_arch
= OSSwapBigToHostInt32(fh
->nfat_arch
);
482 struct fat_arch
* arch
= (struct fat_arch
*)&buf
[sizeof(struct fat_header
)];
483 for (; nfat_arch
; arch
++, nfat_arch
--) {
484 if (OSSwapBigToHostInt32(arch
->cputype
) == CPU_TYPE
) {
485 base
+= OSSwapBigToHostInt32(arch
->offset
);
494 // Check Mach-O magic in the Mach header
495 struct cpu_mach_header
* mh
= (struct cpu_mach_header
*)base
;
496 if (mh
->magic
!= MH_MAGIC
) {
500 // The Mach header is followed by a sequence of load commands.
501 // Each command has a header containing the command type and the
502 // command size. LD_SEGMENT commands describes how the dynamic
503 // loader is going to map the file in memory. We use that
504 // information to find the biggest offset from the library that
505 // will be mapped in memory.
506 char* cmd
= &base
[sizeof(struct cpu_mach_header
)];
508 for (uint32_t ncmds
= mh
->ncmds
; ncmds
; ncmds
--) {
509 struct segment_command
* sh
= (struct segment_command
*)cmd
;
510 if (sh
->cmd
!= LC_SEGMENT
) {
513 if (end
< sh
->fileoff
+ sh
->filesize
) {
514 end
= sh
->fileoff
+ sh
->filesize
;
518 // Let the kernel read ahead what the dynamic loader is going to
519 // map in memory soon after.
521 ReadAhead(buf
.getFd(), base
- buf
, end
);
526 void mozilla::ReadAheadFile(mozilla::pathstr_t aFilePath
, const size_t aOffset
,
527 const size_t aCount
, mozilla::filedesc_t
* aOutFd
) {
531 *aOutFd
= INVALID_HANDLE_VALUE
;
535 HANDLE fd
= CreateFileW(aFilePath
, GENERIC_READ
, FILE_SHARE_READ
, nullptr,
536 OPEN_EXISTING
, FILE_FLAG_SEQUENTIAL_SCAN
, nullptr);
540 if (fd
== INVALID_HANDLE_VALUE
) {
543 ReadAhead(fd
, aOffset
, aCount
);
547 #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
554 int fd
= open(aFilePath
, O_RDONLY
);
562 if (aCount
== SIZE_MAX
) {
564 if (fstat(fd
, &st
) < 0) {
574 ReadAhead(fd
, aOffset
, count
);