Bug 1658791: enable "dom/base/test/test_setting_opener.html" for xorigin iframes...
[gecko.git] / xpcom / glue / FileUtils.cpp
blobcfa14aab42413264f8155a4158b001854d2f8ff7
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"
9 #include <errno.h>
10 #include <stdio.h>
11 #include <inttypes.h>
13 #include "nscore.h"
14 #include "private/pprio.h"
15 #include "prmem.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/MemUtils.h"
19 #if defined(XP_MACOSX)
20 # include <fcntl.h>
21 # include <unistd.h>
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>
27 # include <limits.h>
28 #elif defined(XP_UNIX)
29 # include <fcntl.h>
30 # include <unistd.h>
31 # if defined(LINUX)
32 # include <elf.h>
33 # endif
34 # include <sys/types.h>
35 # include <sys/stat.h>
36 #elif defined(XP_WIN)
37 # include <nsWindowsHelpers.h>
38 # include <mozilla/NativeNt.h>
39 # include <mozilla/ScopeExit.h>
40 #endif
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);
53 if (oldpos == -1) {
54 return false;
57 if (PR_Seek64(aFD, aLength, PR_SEEK_SET) != aLength) {
58 return false;
61 bool retval = (0 != SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(aFD)));
63 PR_Seek64(aFD, oldpos, PR_SEEK_SET);
64 return retval;
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);
70 if (ret == -1) {
71 // OK, perhaps we are too fragmented, allocate non-continuous
72 store.fst_flags = F_ALLOCATEALL;
73 ret = fcntl(fd, F_PREALLOCATE, &store);
74 if (ret == -1) {
75 return false;
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);
88 if (oldpos == -1) {
89 return false;
92 struct stat buf;
93 int fd = PR_FileDesc2NativeHandle(aFD);
94 if (fstat(fd, &buf)) {
95 return false;
98 if (buf.st_size >= aLength) {
99 return false;
102 const int nBlk = buf.st_blksize;
104 if (!nBlk) {
105 return false;
108 if (ftruncate(fd, aLength)) {
109 return false;
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) {
116 nWrite = 0;
117 if (PR_Seek64(aFD, iWrite, PR_SEEK_SET) == iWrite) {
118 nWrite = PR_Write(aFD, "", 1);
120 if (nWrite != 1) {
121 break;
123 iWrite += nBlk;
126 PR_Seek64(aFD, oldpos, PR_SEEK_SET);
127 return nWrite == 1;
128 # endif
129 return false;
132 void mozilla::ReadAheadLib(nsIFile* aFile) {
133 # if defined(XP_WIN)
134 nsAutoString path;
135 if (!aFile || NS_FAILED(aFile->GetPath(path))) {
136 return;
138 ReadAheadLib(path.get());
139 # elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
140 nsAutoCString nativePath;
141 if (!aFile || NS_FAILED(aFile->GetNativePath(nativePath))) {
142 return;
144 ReadAheadLib(nativePath.get());
145 # endif
148 void mozilla::ReadAheadFile(nsIFile* aFile, const size_t aOffset,
149 const size_t aCount, mozilla::filedesc_t* aOutFd) {
150 # if defined(XP_WIN)
151 nsAutoString path;
152 if (!aFile || NS_FAILED(aFile->GetPath(path))) {
153 return;
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))) {
159 return;
161 ReadAheadFile(nativePath.get(), aOffset, aCount, aOutFd);
162 # endif
165 mozilla::PathString mozilla::GetLibraryName(mozilla::pathstr_t aDirectory,
166 const char* aLib) {
167 # ifdef XP_WIN
168 nsAutoString fullName;
169 if (aDirectory) {
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);
178 # else
179 char* temp = PR_GetLibraryName(aDirectory, aLib);
180 if (!temp) {
181 return EmptyCString();
183 nsAutoCString libname(temp);
184 PR_FreeLibraryName(temp);
185 return std::move(libname);
186 # endif
189 mozilla::PathString mozilla::GetLibraryFilePathname(mozilla::pathstr_t aName,
190 PRFuncPtr aAddr) {
191 # ifdef XP_WIN
192 HMODULE handle = GetModuleHandleW(char16ptr_t(aName));
193 if (!handle) {
194 return EmptyString();
197 nsAutoString path;
198 path.SetLength(MAX_PATH);
199 DWORD len = GetModuleFileNameW(handle, char16ptr_t(path.BeginWriting()),
200 path.Length());
201 if (!len) {
202 return EmptyString();
205 path.SetLength(len);
206 return std::move(path);
207 # else
208 char* temp = PR_GetLibraryFilePathname(aName, aAddr);
209 if (!temp) {
210 return EmptyCString();
212 nsAutoCString path(temp);
213 PR_Free(temp); // PR_GetLibraryFilePathname() uses PR_Malloc().
214 return std::move(path);
215 # endif
218 #endif // defined(MOZILLA_INTERNAL_API)
220 #if defined(LINUX) && !defined(ANDROID)
222 static const unsigned int bufsize = 4096;
224 # ifdef __LP64__
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;
229 # else
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;
234 # endif
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;
248 # else
249 # error Unsupported CPU type
250 # endif
252 # ifdef __LP64__
253 # undef LC_SEGMENT
254 # define LC_SEGMENT LC_SEGMENT_64
255 # undef MH_MAGIC
256 # define MH_MAGIC MH_MAGIC_64
257 # define cpu_mach_header mach_header_64
258 # define segment_command segment_command_64
259 # else
260 # define cpu_mach_header mach_header
261 # endif
263 class ScopedMMap {
264 public:
265 explicit ScopedMMap(const char* aFilePath) : buf(nullptr) {
266 fd = open(aFilePath, O_RDONLY);
267 if (fd < 0) {
268 return;
270 struct stat st;
271 if (fstat(fd, &st) < 0) {
272 return;
274 size = st.st_size;
275 buf = (char*)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
277 ~ScopedMMap() {
278 if (buf) {
279 munmap(buf, size);
281 if (fd >= 0) {
282 close(fd);
285 operator char*() { return buf; }
286 int getFd() { return fd; }
288 private:
289 int fd;
290 char* buf;
291 size_t size;
293 #endif
295 void mozilla::ReadAhead(mozilla::filedesc_t aFd, const size_t aOffset,
296 const size_t aCount) {
297 #if defined(XP_WIN)
299 LARGE_INTEGER fpOriginal;
300 LARGE_INTEGER fpOffset;
301 # if defined(HAVE_LONG_LONG)
302 fpOffset.QuadPart = 0;
303 # else
304 fpOffset.u.LowPart = 0;
305 fpOffset.u.HighPart = 0;
306 # endif
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)) {
312 return;
315 if (aOffset) {
316 # if defined(HAVE_LONG_LONG)
317 fpOffset.QuadPart = static_cast<LONGLONG>(aOffset);
318 # else
319 fpOffset.u.LowPart = aOffset;
320 fpOffset.u.HighPart = 0;
321 # endif
323 if (!SetFilePointerEx(aFd, fpOffset, nullptr, FILE_BEGIN)) {
324 return;
328 char buf[64 * 1024];
329 size_t totalBytesRead = 0;
330 DWORD dwBytesRead;
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)
350 struct radvisory ra;
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);
356 #endif
359 void mozilla::ReadAheadLib(mozilla::pathstr_t aFilePath) {
360 if (!aFilePath) {
361 return;
363 #if defined(XP_WIN)
364 if (!CanPrefetchMemory()) {
365 ReadAheadFile(aFilePath);
366 return;
368 nsAutoHandle fd(CreateFileW(aFilePath, GENERIC_READ | GENERIC_EXECUTE,
369 FILE_SHARE_READ, nullptr, OPEN_EXISTING,
370 FILE_FLAG_SEQUENTIAL_SCAN, nullptr));
371 if (!fd) {
372 return;
375 nsAutoHandle mapping(CreateFileMapping(
376 fd, nullptr, SEC_IMAGE | PAGE_EXECUTE_READ, 0, 0, nullptr));
377 if (!mapping) {
378 return;
381 PVOID data = MapViewOfFile(
382 mapping, FILE_MAP_READ | FILE_MAP_EXECUTE | SEC_IMAGE, 0, 0, 0);
383 if (!data) {
384 return;
386 auto guard = MakeScopeExit([=]() { UnmapViewOfFile(data); });
387 mozilla::nt::PEHeaders headers(data);
388 Maybe<Span<const uint8_t>> bounds = headers.GetBounds();
389 if (!bounds) {
390 return;
393 PrefetchMemory((uint8_t*)data, bounds->Length());
395 #elif defined(LINUX) && !defined(ANDROID)
396 int fd = open(aFilePath, O_RDONLY);
397 if (fd < 0) {
398 return;
401 union {
402 char buf[bufsize];
403 Elf_Ehdr ehdr;
404 } elf;
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.
414 (elf.ehdr.e_phoff +
415 (static_cast<Elf_Off>(elf.ehdr.e_phentsize) * elf.ehdr.e_phnum) >=
416 bufsize)) {
417 close(fd);
418 return;
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
424 // memory.
425 Elf_Phdr* phdr = (Elf_Phdr*)&elf.buf[elf.ehdr.e_phoff];
426 Elf_Off end = 0;
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.
434 if (end > 0) {
435 ReadAhead(fd, 0, end);
437 close(fd);
438 #elif defined(XP_MACOSX)
439 ScopedMMap buf(aFilePath);
440 char* base = buf;
441 if (!base) {
442 return;
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);
457 break;
460 if (base == buf) {
461 return;
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) {
468 return;
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)];
478 uint32_t end = 0;
479 for (uint32_t ncmds = mh->ncmds; ncmds; ncmds--) {
480 struct segment_command* sh = (struct segment_command*)cmd;
481 if (sh->cmd != LC_SEGMENT) {
482 continue;
484 if (end < sh->fileoff + sh->filesize) {
485 end = sh->fileoff + sh->filesize;
487 cmd += sh->cmdsize;
489 // Let the kernel read ahead what the dynamic loader is going to
490 // map in memory soon after.
491 if (end > 0) {
492 ReadAhead(buf.getFd(), base - buf, end);
494 #endif
497 void mozilla::ReadAheadFile(mozilla::pathstr_t aFilePath, const size_t aOffset,
498 const size_t aCount, mozilla::filedesc_t* aOutFd) {
499 #if defined(XP_WIN)
500 if (!aFilePath) {
501 if (aOutFd) {
502 *aOutFd = INVALID_HANDLE_VALUE;
504 return;
506 HANDLE fd = CreateFileW(aFilePath, GENERIC_READ, FILE_SHARE_READ, nullptr,
507 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
508 if (aOutFd) {
509 *aOutFd = fd;
511 if (fd == INVALID_HANDLE_VALUE) {
512 return;
514 ReadAhead(fd, aOffset, aCount);
515 if (!aOutFd) {
516 CloseHandle(fd);
518 #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX)
519 if (!aFilePath) {
520 if (aOutFd) {
521 *aOutFd = -1;
523 return;
525 int fd = open(aFilePath, O_RDONLY);
526 if (aOutFd) {
527 *aOutFd = fd;
529 if (fd < 0) {
530 return;
532 size_t count;
533 if (aCount == SIZE_MAX) {
534 struct stat st;
535 if (fstat(fd, &st) < 0) {
536 if (!aOutFd) {
537 close(fd);
539 return;
541 count = st.st_size;
542 } else {
543 count = aCount;
545 ReadAhead(fd, aOffset, count);
546 if (!aOutFd) {
547 close(fd);
549 #endif