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"
14 #include "private/pprio.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/MemUtils.h"
19 #if defined(XP_MACOSX)
22 # include <mach/machine.h>
23 # include <mach-o/fat.h>
24 # include <mach-o/loader.h>
25 # include <sys/mman.h>
26 # include <sys/stat.h>
28 #elif defined(XP_UNIX)
34 # include <sys/types.h>
35 # include <sys/stat.h>
37 # include <nsWindowsHelpers.h>
38 # include <mozilla/NativeNt.h>
39 # include <mozilla/ScopeExit.h>
42 // Functions that are not to be used in standalone glue must be implemented
43 // within this #if block
44 #if defined(MOZILLA_INTERNAL_API)
46 # include "nsString.h"
48 bool mozilla::fallocate(PRFileDesc
* aFD
, int64_t aLength
) {
49 # if defined(HAVE_POSIX_FALLOCATE)
50 return posix_fallocate(PR_FileDesc2NativeHandle(aFD
), 0, aLength
) == 0;
51 # elif defined(XP_WIN)
52 int64_t oldpos
= PR_Seek64(aFD
, 0, PR_SEEK_CUR
);
57 if (PR_Seek64(aFD
, aLength
, PR_SEEK_SET
) != aLength
) {
61 bool retval
= (0 != SetEndOfFile((HANDLE
)PR_FileDesc2NativeHandle(aFD
)));
63 PR_Seek64(aFD
, oldpos
, PR_SEEK_SET
);
65 # elif defined(XP_MACOSX)
66 int fd
= PR_FileDesc2NativeHandle(aFD
);
67 fstore_t store
= {F_ALLOCATECONTIG
, F_PEOFPOSMODE
, 0, aLength
};
68 // Try to get a continous chunk of disk space
69 int ret
= fcntl(fd
, F_PREALLOCATE
, &store
);
71 // OK, perhaps we are too fragmented, allocate non-continuous
72 store
.fst_flags
= F_ALLOCATEALL
;
73 ret
= fcntl(fd
, F_PREALLOCATE
, &store
);
78 return ftruncate(fd
, aLength
) == 0;
79 # elif defined(XP_UNIX)
80 // The following is copied from fcntlSizeHint in sqlite
81 /* If the OS does not have posix_fallocate(), fake it. First use
82 ** ftruncate() to set the file size, then write a single byte to
83 ** the last byte in each block within the extended region. This
84 ** is the same technique used by glibc to implement posix_fallocate()
85 ** on systems that do not have a real fallocate() system call.
87 int64_t oldpos
= PR_Seek64(aFD
, 0, PR_SEEK_CUR
);
93 int fd
= PR_FileDesc2NativeHandle(aFD
);
94 if (fstat(fd
, &buf
)) {
98 if (buf
.st_size
>= aLength
) {
102 const int nBlk
= buf
.st_blksize
;
108 if (ftruncate(fd
, aLength
)) {
112 int nWrite
; // Return value from write()
113 int64_t iWrite
= ((buf
.st_size
+ 2 * nBlk
- 1) / nBlk
) * nBlk
-
114 1; // Next offset to write to
115 while (iWrite
< aLength
) {
117 if (PR_Seek64(aFD
, iWrite
, PR_SEEK_SET
) == iWrite
) {
118 nWrite
= PR_Write(aFD
, "", 1);
126 PR_Seek64(aFD
, oldpos
, PR_SEEK_SET
);
132 void mozilla::ReadAheadLib(nsIFile
* aFile
) {
135 if (!aFile
|| NS_FAILED(aFile
->GetPath(path
))) {
138 ReadAheadLib(path
.get());
139 # elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
140 nsAutoCString nativePath
;
141 if (!aFile
|| NS_FAILED(aFile
->GetNativePath(nativePath
))) {
144 ReadAheadLib(nativePath
.get());
148 void mozilla::ReadAheadFile(nsIFile
* aFile
, const size_t aOffset
,
149 const size_t aCount
, mozilla::filedesc_t
* aOutFd
) {
152 if (!aFile
|| NS_FAILED(aFile
->GetPath(path
))) {
155 ReadAheadFile(path
.get(), aOffset
, aCount
, aOutFd
);
156 # elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
157 nsAutoCString nativePath
;
158 if (!aFile
|| NS_FAILED(aFile
->GetNativePath(nativePath
))) {
161 ReadAheadFile(nativePath
.get(), aOffset
, aCount
, aOutFd
);
165 mozilla::PathString
mozilla::GetLibraryName(mozilla::pathstr_t aDirectory
,
168 nsAutoString fullName
;
170 fullName
.Assign(aDirectory
);
171 fullName
.Append('\\');
173 AppendUTF8toUTF16(MakeStringSpan(aLib
), fullName
);
174 if (!strstr(aLib
, ".dll")) {
175 fullName
.AppendLiteral(".dll");
177 return std::move(fullName
);
179 char* temp
= PR_GetLibraryName(aDirectory
, aLib
);
181 return EmptyCString();
183 nsAutoCString
libname(temp
);
184 PR_FreeLibraryName(temp
);
185 return std::move(libname
);
189 mozilla::PathString
mozilla::GetLibraryFilePathname(mozilla::pathstr_t aName
,
192 HMODULE handle
= GetModuleHandleW(char16ptr_t(aName
));
194 return EmptyString();
198 path
.SetLength(MAX_PATH
);
199 DWORD len
= GetModuleFileNameW(handle
, char16ptr_t(path
.BeginWriting()),
202 return EmptyString();
206 return std::move(path
);
208 char* temp
= PR_GetLibraryFilePathname(aName
, aAddr
);
210 return EmptyCString();
212 nsAutoCString
path(temp
);
213 PR_Free(temp
); // PR_GetLibraryFilePathname() uses PR_Malloc().
214 return std::move(path
);
218 #endif // defined(MOZILLA_INTERNAL_API)
220 #if defined(LINUX) && !defined(ANDROID)
222 static const unsigned int bufsize
= 4096;
225 typedef Elf64_Ehdr Elf_Ehdr
;
226 typedef Elf64_Phdr Elf_Phdr
;
227 static const unsigned char ELFCLASS
= ELFCLASS64
;
228 typedef Elf64_Off Elf_Off
;
230 typedef Elf32_Ehdr Elf_Ehdr
;
231 typedef Elf32_Phdr Elf_Phdr
;
232 static const unsigned char ELFCLASS
= ELFCLASS32
;
233 typedef Elf32_Off Elf_Off
;
236 #elif defined(XP_MACOSX)
238 # if defined(__i386__)
239 static const uint32_t CPU_TYPE
= CPU_TYPE_X86
;
240 # elif defined(__x86_64__)
241 static const uint32_t CPU_TYPE
= CPU_TYPE_X86_64
;
242 # elif defined(__ppc__)
243 static const uint32_t CPU_TYPE
= CPU_TYPE_POWERPC
;
244 # elif defined(__ppc64__)
245 static const uint32_t CPU_TYPE
= CPU_TYPE_POWERPC64
;
246 # elif defined(__aarch64__)
247 static const uint32_t CPU_TYPE
= CPU_TYPE_ARM64
;
249 # error Unsupported CPU type
254 # define LC_SEGMENT LC_SEGMENT_64
256 # define MH_MAGIC MH_MAGIC_64
257 # define cpu_mach_header mach_header_64
258 # define segment_command segment_command_64
260 # define cpu_mach_header mach_header
265 explicit ScopedMMap(const char* aFilePath
) : buf(nullptr) {
266 fd
= open(aFilePath
, O_RDONLY
);
271 if (fstat(fd
, &st
) < 0) {
275 buf
= (char*)mmap(nullptr, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
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
) {
364 if (!CanPrefetchMemory()) {
365 ReadAheadFile(aFilePath
);
368 nsAutoHandle
fd(CreateFileW(aFilePath
, GENERIC_READ
| GENERIC_EXECUTE
,
369 FILE_SHARE_READ
, nullptr, OPEN_EXISTING
,
370 FILE_FLAG_SEQUENTIAL_SCAN
, nullptr));
375 nsAutoHandle
mapping(CreateFileMapping(
376 fd
, nullptr, SEC_IMAGE
| PAGE_EXECUTE_READ
, 0, 0, nullptr));
381 PVOID data
= MapViewOfFile(
382 mapping
, FILE_MAP_READ
| FILE_MAP_EXECUTE
| SEC_IMAGE
, 0, 0, 0);
386 auto guard
= MakeScopeExit([=]() { UnmapViewOfFile(data
); });
387 mozilla::nt::PEHeaders
headers(data
);
388 Maybe
<Span
<const uint8_t>> bounds
= headers
.GetBounds();
393 PrefetchMemory((uint8_t*)data
, bounds
->Length());
395 #elif defined(LINUX) && !defined(ANDROID)
396 int fd
= open(aFilePath
, O_RDONLY
);
405 // Read ELF header (ehdr) and program header table (phdr).
406 // We check that the ELF magic is found, that the ELF class matches
407 // our own, and that the program header table as defined in the ELF
408 // headers fits in the buffer we read.
409 if ((read(fd
, elf
.buf
, bufsize
) <= 0) || (memcmp(elf
.buf
, ELFMAG
, 4)) ||
410 (elf
.ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
) ||
411 // Upcast e_phentsize so the multiplication is done in the same precision
412 // as the subsequent addition, to satisfy static analyzers and avoid
413 // issues with abnormally large program header tables.
415 (static_cast<Elf_Off
>(elf
.ehdr
.e_phentsize
) * elf
.ehdr
.e_phnum
) >=
420 // The program header table contains segment definitions. One such
421 // segment type is PT_LOAD, which describes how the dynamic loader
422 // is going to map the file in memory. We use that information to
423 // find the biggest offset from the library that will be mapped in
425 Elf_Phdr
* phdr
= (Elf_Phdr
*)&elf
.buf
[elf
.ehdr
.e_phoff
];
427 for (int phnum
= elf
.ehdr
.e_phnum
; phnum
; phdr
++, phnum
--) {
428 if ((phdr
->p_type
== PT_LOAD
) && (end
< phdr
->p_offset
+ phdr
->p_filesz
)) {
429 end
= phdr
->p_offset
+ phdr
->p_filesz
;
432 // Let the kernel read ahead what the dynamic loader is going to
433 // map in memory soon after.
435 ReadAhead(fd
, 0, end
);
438 #elif defined(XP_MACOSX)
439 ScopedMMap
buf(aFilePath
);
445 // An OSX binary might either be a fat (universal) binary or a
446 // Mach-O binary. A fat binary actually embeds several Mach-O
447 // binaries. If we have a fat binary, find the offset where the
448 // Mach-O binary for our CPU type can be found.
449 struct fat_header
* fh
= (struct fat_header
*)base
;
451 if (OSSwapBigToHostInt32(fh
->magic
) == FAT_MAGIC
) {
452 uint32_t nfat_arch
= OSSwapBigToHostInt32(fh
->nfat_arch
);
453 struct fat_arch
* arch
= (struct fat_arch
*)&buf
[sizeof(struct fat_header
)];
454 for (; nfat_arch
; arch
++, nfat_arch
--) {
455 if (OSSwapBigToHostInt32(arch
->cputype
) == CPU_TYPE
) {
456 base
+= OSSwapBigToHostInt32(arch
->offset
);
465 // Check Mach-O magic in the Mach header
466 struct cpu_mach_header
* mh
= (struct cpu_mach_header
*)base
;
467 if (mh
->magic
!= MH_MAGIC
) {
471 // The Mach header is followed by a sequence of load commands.
472 // Each command has a header containing the command type and the
473 // command size. LD_SEGMENT commands describes how the dynamic
474 // loader is going to map the file in memory. We use that
475 // information to find the biggest offset from the library that
476 // will be mapped in memory.
477 char* cmd
= &base
[sizeof(struct cpu_mach_header
)];
479 for (uint32_t ncmds
= mh
->ncmds
; ncmds
; ncmds
--) {
480 struct segment_command
* sh
= (struct segment_command
*)cmd
;
481 if (sh
->cmd
!= LC_SEGMENT
) {
484 if (end
< sh
->fileoff
+ sh
->filesize
) {
485 end
= sh
->fileoff
+ sh
->filesize
;
489 // Let the kernel read ahead what the dynamic loader is going to
490 // map in memory soon after.
492 ReadAhead(buf
.getFd(), base
- buf
, end
);
497 void mozilla::ReadAheadFile(mozilla::pathstr_t aFilePath
, const size_t aOffset
,
498 const size_t aCount
, mozilla::filedesc_t
* aOutFd
) {
502 *aOutFd
= INVALID_HANDLE_VALUE
;
506 HANDLE fd
= CreateFileW(aFilePath
, GENERIC_READ
, FILE_SHARE_READ
, nullptr,
507 OPEN_EXISTING
, FILE_FLAG_SEQUENTIAL_SCAN
, nullptr);
511 if (fd
== INVALID_HANDLE_VALUE
) {
514 ReadAhead(fd
, aOffset
, aCount
);
518 #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
525 int fd
= open(aFilePath
, O_RDONLY
);
533 if (aCount
== SIZE_MAX
) {
535 if (fstat(fd
, &st
) < 0) {
545 ReadAhead(fd
, aOffset
, count
);