Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <errno.h> |
michael@0 | 7 | #include <stdio.h> |
michael@0 | 8 | |
michael@0 | 9 | #include "nscore.h" |
michael@0 | 10 | #include "nsStringGlue.h" |
michael@0 | 11 | #include "private/pprio.h" |
michael@0 | 12 | #include "mozilla/Assertions.h" |
michael@0 | 13 | #include "mozilla/FileUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | #if defined(XP_MACOSX) |
michael@0 | 16 | #include <fcntl.h> |
michael@0 | 17 | #include <unistd.h> |
michael@0 | 18 | #include <mach/machine.h> |
michael@0 | 19 | #include <mach-o/fat.h> |
michael@0 | 20 | #include <mach-o/loader.h> |
michael@0 | 21 | #include <sys/mman.h> |
michael@0 | 22 | #include <sys/stat.h> |
michael@0 | 23 | #include <limits.h> |
michael@0 | 24 | #elif defined(XP_UNIX) |
michael@0 | 25 | #include <fcntl.h> |
michael@0 | 26 | #include <unistd.h> |
michael@0 | 27 | #if defined(LINUX) |
michael@0 | 28 | #include <elf.h> |
michael@0 | 29 | #endif |
michael@0 | 30 | #include <sys/types.h> |
michael@0 | 31 | #include <sys/stat.h> |
michael@0 | 32 | #elif defined(XP_WIN) |
michael@0 | 33 | #include <windows.h> |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | // Functions that are not to be used in standalone glue must be implemented |
michael@0 | 37 | // within this #if block |
michael@0 | 38 | #if !defined(XPCOM_GLUE) |
michael@0 | 39 | |
michael@0 | 40 | bool |
michael@0 | 41 | mozilla::fallocate(PRFileDesc *aFD, int64_t aLength) |
michael@0 | 42 | { |
michael@0 | 43 | #if defined(HAVE_POSIX_FALLOCATE) |
michael@0 | 44 | return posix_fallocate(PR_FileDesc2NativeHandle(aFD), 0, aLength) == 0; |
michael@0 | 45 | #elif defined(XP_WIN) |
michael@0 | 46 | int64_t oldpos = PR_Seek64(aFD, 0, PR_SEEK_CUR); |
michael@0 | 47 | if (oldpos == -1) |
michael@0 | 48 | return false; |
michael@0 | 49 | |
michael@0 | 50 | if (PR_Seek64(aFD, aLength, PR_SEEK_SET) != aLength) |
michael@0 | 51 | return false; |
michael@0 | 52 | |
michael@0 | 53 | bool retval = (0 != SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(aFD))); |
michael@0 | 54 | |
michael@0 | 55 | PR_Seek64(aFD, oldpos, PR_SEEK_SET); |
michael@0 | 56 | return retval; |
michael@0 | 57 | #elif defined(XP_MACOSX) |
michael@0 | 58 | int fd = PR_FileDesc2NativeHandle(aFD); |
michael@0 | 59 | fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, aLength}; |
michael@0 | 60 | // Try to get a continous chunk of disk space |
michael@0 | 61 | int ret = fcntl(fd, F_PREALLOCATE, &store); |
michael@0 | 62 | if (-1 == ret) { |
michael@0 | 63 | // OK, perhaps we are too fragmented, allocate non-continuous |
michael@0 | 64 | store.fst_flags = F_ALLOCATEALL; |
michael@0 | 65 | ret = fcntl(fd, F_PREALLOCATE, &store); |
michael@0 | 66 | if (-1 == ret) |
michael@0 | 67 | return false; |
michael@0 | 68 | } |
michael@0 | 69 | return 0 == ftruncate(fd, aLength); |
michael@0 | 70 | #elif defined(XP_UNIX) |
michael@0 | 71 | // The following is copied from fcntlSizeHint in sqlite |
michael@0 | 72 | /* If the OS does not have posix_fallocate(), fake it. First use |
michael@0 | 73 | ** ftruncate() to set the file size, then write a single byte to |
michael@0 | 74 | ** the last byte in each block within the extended region. This |
michael@0 | 75 | ** is the same technique used by glibc to implement posix_fallocate() |
michael@0 | 76 | ** on systems that do not have a real fallocate() system call. |
michael@0 | 77 | */ |
michael@0 | 78 | int64_t oldpos = PR_Seek64(aFD, 0, PR_SEEK_CUR); |
michael@0 | 79 | if (oldpos == -1) |
michael@0 | 80 | return false; |
michael@0 | 81 | |
michael@0 | 82 | struct stat buf; |
michael@0 | 83 | int fd = PR_FileDesc2NativeHandle(aFD); |
michael@0 | 84 | if (fstat(fd, &buf)) |
michael@0 | 85 | return false; |
michael@0 | 86 | |
michael@0 | 87 | if (buf.st_size >= aLength) |
michael@0 | 88 | return false; |
michael@0 | 89 | |
michael@0 | 90 | const int nBlk = buf.st_blksize; |
michael@0 | 91 | |
michael@0 | 92 | if (!nBlk) |
michael@0 | 93 | return false; |
michael@0 | 94 | |
michael@0 | 95 | if (ftruncate(fd, aLength)) |
michael@0 | 96 | return false; |
michael@0 | 97 | |
michael@0 | 98 | int nWrite; // Return value from write() |
michael@0 | 99 | int64_t iWrite = ((buf.st_size + 2 * nBlk - 1) / nBlk) * nBlk - 1; // Next offset to write to |
michael@0 | 100 | while (iWrite < aLength) { |
michael@0 | 101 | nWrite = 0; |
michael@0 | 102 | if (PR_Seek64(aFD, iWrite, PR_SEEK_SET) == iWrite) |
michael@0 | 103 | nWrite = PR_Write(aFD, "", 1); |
michael@0 | 104 | if (nWrite != 1) break; |
michael@0 | 105 | iWrite += nBlk; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | PR_Seek64(aFD, oldpos, PR_SEEK_SET); |
michael@0 | 109 | return nWrite == 1; |
michael@0 | 110 | #endif |
michael@0 | 111 | return false; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | #ifdef ReadSysFile_PRESENT |
michael@0 | 115 | |
michael@0 | 116 | bool |
michael@0 | 117 | mozilla::ReadSysFile( |
michael@0 | 118 | const char* aFilename, |
michael@0 | 119 | char* aBuf, |
michael@0 | 120 | size_t aBufSize) |
michael@0 | 121 | { |
michael@0 | 122 | int fd = MOZ_TEMP_FAILURE_RETRY(open(aFilename, O_RDONLY)); |
michael@0 | 123 | if (fd < 0) { |
michael@0 | 124 | return false; |
michael@0 | 125 | } |
michael@0 | 126 | ScopedClose autoClose(fd); |
michael@0 | 127 | if (aBufSize == 0) { |
michael@0 | 128 | return true; |
michael@0 | 129 | } |
michael@0 | 130 | ssize_t bytesRead; |
michael@0 | 131 | size_t offset = 0; |
michael@0 | 132 | do { |
michael@0 | 133 | bytesRead = MOZ_TEMP_FAILURE_RETRY( |
michael@0 | 134 | read(fd, aBuf + offset, aBufSize - offset)); |
michael@0 | 135 | if (bytesRead == -1) { |
michael@0 | 136 | return false; |
michael@0 | 137 | } |
michael@0 | 138 | offset += bytesRead; |
michael@0 | 139 | } while (bytesRead > 0 && offset < aBufSize); |
michael@0 | 140 | MOZ_ASSERT(offset <= aBufSize); |
michael@0 | 141 | if (offset > 0 && aBuf[offset - 1] == '\n') { |
michael@0 | 142 | offset--; |
michael@0 | 143 | } |
michael@0 | 144 | if (offset == aBufSize) { |
michael@0 | 145 | MOZ_ASSERT(offset > 0); |
michael@0 | 146 | offset--; |
michael@0 | 147 | } |
michael@0 | 148 | aBuf[offset] = '\0'; |
michael@0 | 149 | return true; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | bool |
michael@0 | 153 | mozilla::ReadSysFile( |
michael@0 | 154 | const char* aFilename, |
michael@0 | 155 | int* aVal) |
michael@0 | 156 | { |
michael@0 | 157 | char valBuf[32]; |
michael@0 | 158 | if (!ReadSysFile(aFilename, valBuf, sizeof(valBuf))) { |
michael@0 | 159 | return false; |
michael@0 | 160 | } |
michael@0 | 161 | return sscanf(valBuf, "%d", aVal) == 1; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | bool |
michael@0 | 165 | mozilla::ReadSysFile( |
michael@0 | 166 | const char* aFilename, |
michael@0 | 167 | bool* aVal) |
michael@0 | 168 | { |
michael@0 | 169 | int v; |
michael@0 | 170 | if (!ReadSysFile(aFilename, &v)) { |
michael@0 | 171 | return false; |
michael@0 | 172 | } |
michael@0 | 173 | *aVal = (v != 0); |
michael@0 | 174 | return true; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | #endif /* ReadSysFile_PRESENT */ |
michael@0 | 178 | |
michael@0 | 179 | void |
michael@0 | 180 | mozilla::ReadAheadLib(nsIFile* aFile) |
michael@0 | 181 | { |
michael@0 | 182 | #if defined(XP_WIN) |
michael@0 | 183 | nsAutoString path; |
michael@0 | 184 | if (!aFile || NS_FAILED(aFile->GetPath(path))) { |
michael@0 | 185 | return; |
michael@0 | 186 | } |
michael@0 | 187 | ReadAheadLib(path.get()); |
michael@0 | 188 | #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX) |
michael@0 | 189 | nsAutoCString nativePath; |
michael@0 | 190 | if (!aFile || NS_FAILED(aFile->GetNativePath(nativePath))) { |
michael@0 | 191 | return; |
michael@0 | 192 | } |
michael@0 | 193 | ReadAheadLib(nativePath.get()); |
michael@0 | 194 | #endif |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | void |
michael@0 | 198 | mozilla::ReadAheadFile(nsIFile* aFile, const size_t aOffset, |
michael@0 | 199 | const size_t aCount, mozilla::filedesc_t* aOutFd) |
michael@0 | 200 | { |
michael@0 | 201 | #if defined(XP_WIN) |
michael@0 | 202 | nsAutoString path; |
michael@0 | 203 | if (!aFile || NS_FAILED(aFile->GetPath(path))) { |
michael@0 | 204 | return; |
michael@0 | 205 | } |
michael@0 | 206 | ReadAheadFile(path.get(), aOffset, aCount, aOutFd); |
michael@0 | 207 | #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX) |
michael@0 | 208 | nsAutoCString nativePath; |
michael@0 | 209 | if (!aFile || NS_FAILED(aFile->GetNativePath(nativePath))) { |
michael@0 | 210 | return; |
michael@0 | 211 | } |
michael@0 | 212 | ReadAheadFile(nativePath.get(), aOffset, aCount, aOutFd); |
michael@0 | 213 | #endif |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | #endif // !defined(XPCOM_GLUE) |
michael@0 | 217 | |
michael@0 | 218 | #if defined(LINUX) && !defined(ANDROID) |
michael@0 | 219 | |
michael@0 | 220 | static const unsigned int bufsize = 4096; |
michael@0 | 221 | |
michael@0 | 222 | #ifdef __LP64__ |
michael@0 | 223 | typedef Elf64_Ehdr Elf_Ehdr; |
michael@0 | 224 | typedef Elf64_Phdr Elf_Phdr; |
michael@0 | 225 | static const unsigned char ELFCLASS = ELFCLASS64; |
michael@0 | 226 | typedef Elf64_Off Elf_Off; |
michael@0 | 227 | #else |
michael@0 | 228 | typedef Elf32_Ehdr Elf_Ehdr; |
michael@0 | 229 | typedef Elf32_Phdr Elf_Phdr; |
michael@0 | 230 | static const unsigned char ELFCLASS = ELFCLASS32; |
michael@0 | 231 | typedef Elf32_Off Elf_Off; |
michael@0 | 232 | #endif |
michael@0 | 233 | |
michael@0 | 234 | #elif defined(XP_MACOSX) |
michael@0 | 235 | |
michael@0 | 236 | #if defined(__i386__) |
michael@0 | 237 | static const uint32_t CPU_TYPE = CPU_TYPE_X86; |
michael@0 | 238 | #elif defined(__x86_64__) |
michael@0 | 239 | static const uint32_t CPU_TYPE = CPU_TYPE_X86_64; |
michael@0 | 240 | #elif defined(__ppc__) |
michael@0 | 241 | static const uint32_t CPU_TYPE = CPU_TYPE_POWERPC; |
michael@0 | 242 | #elif defined(__ppc64__) |
michael@0 | 243 | static const uint32_t CPU_TYPE = CPU_TYPE_POWERPC64; |
michael@0 | 244 | #else |
michael@0 | 245 | #error Unsupported CPU type |
michael@0 | 246 | #endif |
michael@0 | 247 | |
michael@0 | 248 | #ifdef __LP64__ |
michael@0 | 249 | #undef LC_SEGMENT |
michael@0 | 250 | #define LC_SEGMENT LC_SEGMENT_64 |
michael@0 | 251 | #undef MH_MAGIC |
michael@0 | 252 | #define MH_MAGIC MH_MAGIC_64 |
michael@0 | 253 | #define cpu_mach_header mach_header_64 |
michael@0 | 254 | #define segment_command segment_command_64 |
michael@0 | 255 | #else |
michael@0 | 256 | #define cpu_mach_header mach_header |
michael@0 | 257 | #endif |
michael@0 | 258 | |
michael@0 | 259 | class ScopedMMap |
michael@0 | 260 | { |
michael@0 | 261 | public: |
michael@0 | 262 | ScopedMMap(const char *aFilePath) |
michael@0 | 263 | : buf(nullptr) |
michael@0 | 264 | { |
michael@0 | 265 | fd = open(aFilePath, O_RDONLY); |
michael@0 | 266 | if (fd < 0) { |
michael@0 | 267 | return; |
michael@0 | 268 | } |
michael@0 | 269 | struct stat st; |
michael@0 | 270 | if (fstat(fd, &st) < 0) { |
michael@0 | 271 | return; |
michael@0 | 272 | } |
michael@0 | 273 | size = st.st_size; |
michael@0 | 274 | buf = (char *)mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0); |
michael@0 | 275 | } |
michael@0 | 276 | ~ScopedMMap() |
michael@0 | 277 | { |
michael@0 | 278 | if (buf) { |
michael@0 | 279 | munmap(buf, size); |
michael@0 | 280 | } |
michael@0 | 281 | if (fd >= 0) { |
michael@0 | 282 | close(fd); |
michael@0 | 283 | } |
michael@0 | 284 | } |
michael@0 | 285 | operator char *() { return buf; } |
michael@0 | 286 | int getFd() { return fd; } |
michael@0 | 287 | private: |
michael@0 | 288 | int fd; |
michael@0 | 289 | char *buf; |
michael@0 | 290 | size_t size; |
michael@0 | 291 | }; |
michael@0 | 292 | #endif |
michael@0 | 293 | |
michael@0 | 294 | void |
michael@0 | 295 | mozilla::ReadAhead(mozilla::filedesc_t aFd, const size_t aOffset, |
michael@0 | 296 | const size_t aCount) |
michael@0 | 297 | { |
michael@0 | 298 | #if defined(XP_WIN) |
michael@0 | 299 | |
michael@0 | 300 | LARGE_INTEGER fpOriginal; |
michael@0 | 301 | LARGE_INTEGER fpOffset; |
michael@0 | 302 | #if defined(HAVE_LONG_LONG) |
michael@0 | 303 | fpOffset.QuadPart = 0; |
michael@0 | 304 | #else |
michael@0 | 305 | fpOffset.u.LowPart = 0; |
michael@0 | 306 | fpOffset.u.HighPart = 0; |
michael@0 | 307 | #endif |
michael@0 | 308 | |
michael@0 | 309 | // Get the current file pointer so that we can restore it. This isn't |
michael@0 | 310 | // really necessary other than to provide the same semantics regarding the |
michael@0 | 311 | // file pointer that other platforms do |
michael@0 | 312 | if (!SetFilePointerEx(aFd, fpOffset, &fpOriginal, FILE_CURRENT)) { |
michael@0 | 313 | return; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | if (aOffset) { |
michael@0 | 317 | #if defined(HAVE_LONG_LONG) |
michael@0 | 318 | fpOffset.QuadPart = static_cast<LONGLONG>(aOffset); |
michael@0 | 319 | #else |
michael@0 | 320 | fpOffset.u.LowPart = aOffset; |
michael@0 | 321 | fpOffset.u.HighPart = 0; |
michael@0 | 322 | #endif |
michael@0 | 323 | |
michael@0 | 324 | if (!SetFilePointerEx(aFd, fpOffset, nullptr, FILE_BEGIN)) { |
michael@0 | 325 | return; |
michael@0 | 326 | } |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | char buf[64 * 1024]; |
michael@0 | 330 | size_t totalBytesRead = 0; |
michael@0 | 331 | DWORD dwBytesRead; |
michael@0 | 332 | // Do dummy reads to trigger kernel-side readhead via FILE_FLAG_SEQUENTIAL_SCAN. |
michael@0 | 333 | // Abort when underfilling because during testing the buffers are read fully |
michael@0 | 334 | // A buffer that's not keeping up would imply that readahead isn't working right |
michael@0 | 335 | while (totalBytesRead < aCount && |
michael@0 | 336 | ReadFile(aFd, buf, sizeof(buf), &dwBytesRead, nullptr) && |
michael@0 | 337 | dwBytesRead == sizeof(buf)) { |
michael@0 | 338 | totalBytesRead += dwBytesRead; |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | // Restore the file pointer |
michael@0 | 342 | SetFilePointerEx(aFd, fpOriginal, nullptr, FILE_BEGIN); |
michael@0 | 343 | |
michael@0 | 344 | #elif defined(LINUX) && !defined(ANDROID) |
michael@0 | 345 | |
michael@0 | 346 | readahead(aFd, aOffset, aCount); |
michael@0 | 347 | |
michael@0 | 348 | #elif defined(XP_MACOSX) |
michael@0 | 349 | |
michael@0 | 350 | struct radvisory ra; |
michael@0 | 351 | ra.ra_offset = aOffset; |
michael@0 | 352 | ra.ra_count = aCount; |
michael@0 | 353 | // The F_RDADVISE fcntl is equivalent to Linux' readahead() system call. |
michael@0 | 354 | fcntl(aFd, F_RDADVISE, &ra); |
michael@0 | 355 | |
michael@0 | 356 | #endif |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | void |
michael@0 | 360 | mozilla::ReadAheadLib(mozilla::pathstr_t aFilePath) |
michael@0 | 361 | { |
michael@0 | 362 | if (!aFilePath) { |
michael@0 | 363 | return; |
michael@0 | 364 | } |
michael@0 | 365 | #if defined(XP_WIN) |
michael@0 | 366 | ReadAheadFile(aFilePath); |
michael@0 | 367 | #elif defined(LINUX) && !defined(ANDROID) |
michael@0 | 368 | int fd = open(aFilePath, O_RDONLY); |
michael@0 | 369 | if (fd < 0) { |
michael@0 | 370 | return; |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | union { |
michael@0 | 374 | char buf[bufsize]; |
michael@0 | 375 | Elf_Ehdr ehdr; |
michael@0 | 376 | } elf; |
michael@0 | 377 | // Read ELF header (ehdr) and program header table (phdr). |
michael@0 | 378 | // We check that the ELF magic is found, that the ELF class matches |
michael@0 | 379 | // our own, and that the program header table as defined in the ELF |
michael@0 | 380 | // headers fits in the buffer we read. |
michael@0 | 381 | if ((read(fd, elf.buf, bufsize) <= 0) || |
michael@0 | 382 | (memcmp(elf.buf, ELFMAG, 4)) || |
michael@0 | 383 | (elf.ehdr.e_ident[EI_CLASS] != ELFCLASS) || |
michael@0 | 384 | (elf.ehdr.e_phoff + elf.ehdr.e_phentsize * elf.ehdr.e_phnum >= bufsize)) { |
michael@0 | 385 | close(fd); |
michael@0 | 386 | return; |
michael@0 | 387 | } |
michael@0 | 388 | // The program header table contains segment definitions. One such |
michael@0 | 389 | // segment type is PT_LOAD, which describes how the dynamic loader |
michael@0 | 390 | // is going to map the file in memory. We use that information to |
michael@0 | 391 | // find the biggest offset from the library that will be mapped in |
michael@0 | 392 | // memory. |
michael@0 | 393 | Elf_Phdr *phdr = (Elf_Phdr *)&elf.buf[elf.ehdr.e_phoff]; |
michael@0 | 394 | Elf_Off end = 0; |
michael@0 | 395 | for (int phnum = elf.ehdr.e_phnum; phnum; phdr++, phnum--) { |
michael@0 | 396 | if ((phdr->p_type == PT_LOAD) && |
michael@0 | 397 | (end < phdr->p_offset + phdr->p_filesz)) { |
michael@0 | 398 | end = phdr->p_offset + phdr->p_filesz; |
michael@0 | 399 | } |
michael@0 | 400 | } |
michael@0 | 401 | // Let the kernel read ahead what the dynamic loader is going to |
michael@0 | 402 | // map in memory soon after. |
michael@0 | 403 | if (end > 0) { |
michael@0 | 404 | ReadAhead(fd, 0, end); |
michael@0 | 405 | } |
michael@0 | 406 | close(fd); |
michael@0 | 407 | #elif defined(XP_MACOSX) |
michael@0 | 408 | ScopedMMap buf(aFilePath); |
michael@0 | 409 | char *base = buf; |
michael@0 | 410 | if (!base) { |
michael@0 | 411 | return; |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | // An OSX binary might either be a fat (universal) binary or a |
michael@0 | 415 | // Mach-O binary. A fat binary actually embeds several Mach-O |
michael@0 | 416 | // binaries. If we have a fat binary, find the offset where the |
michael@0 | 417 | // Mach-O binary for our CPU type can be found. |
michael@0 | 418 | struct fat_header *fh = (struct fat_header *)base; |
michael@0 | 419 | |
michael@0 | 420 | if (OSSwapBigToHostInt32(fh->magic) == FAT_MAGIC) { |
michael@0 | 421 | uint32_t nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch); |
michael@0 | 422 | struct fat_arch *arch = (struct fat_arch *)&buf[sizeof(struct fat_header)]; |
michael@0 | 423 | for (; nfat_arch; arch++, nfat_arch--) { |
michael@0 | 424 | if (OSSwapBigToHostInt32(arch->cputype) == CPU_TYPE) { |
michael@0 | 425 | base += OSSwapBigToHostInt32(arch->offset); |
michael@0 | 426 | break; |
michael@0 | 427 | } |
michael@0 | 428 | } |
michael@0 | 429 | if (base == buf) { |
michael@0 | 430 | return; |
michael@0 | 431 | } |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | // Check Mach-O magic in the Mach header |
michael@0 | 435 | struct cpu_mach_header *mh = (struct cpu_mach_header *)base; |
michael@0 | 436 | if (mh->magic != MH_MAGIC) { |
michael@0 | 437 | return; |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | // The Mach header is followed by a sequence of load commands. |
michael@0 | 441 | // Each command has a header containing the command type and the |
michael@0 | 442 | // command size. LD_SEGMENT commands describes how the dynamic |
michael@0 | 443 | // loader is going to map the file in memory. We use that |
michael@0 | 444 | // information to find the biggest offset from the library that |
michael@0 | 445 | // will be mapped in memory. |
michael@0 | 446 | char *cmd = &base[sizeof(struct cpu_mach_header)]; |
michael@0 | 447 | uint32_t end = 0; |
michael@0 | 448 | for (uint32_t ncmds = mh->ncmds; ncmds; ncmds--) { |
michael@0 | 449 | struct segment_command *sh = (struct segment_command *)cmd; |
michael@0 | 450 | if (sh->cmd != LC_SEGMENT) { |
michael@0 | 451 | continue; |
michael@0 | 452 | } |
michael@0 | 453 | if (end < sh->fileoff + sh->filesize) { |
michael@0 | 454 | end = sh->fileoff + sh->filesize; |
michael@0 | 455 | } |
michael@0 | 456 | cmd += sh->cmdsize; |
michael@0 | 457 | } |
michael@0 | 458 | // Let the kernel read ahead what the dynamic loader is going to |
michael@0 | 459 | // map in memory soon after. |
michael@0 | 460 | if (end > 0) { |
michael@0 | 461 | ReadAhead(buf.getFd(), base - buf, end); |
michael@0 | 462 | } |
michael@0 | 463 | #endif |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | void |
michael@0 | 467 | mozilla::ReadAheadFile(mozilla::pathstr_t aFilePath, const size_t aOffset, |
michael@0 | 468 | const size_t aCount, mozilla::filedesc_t* aOutFd) |
michael@0 | 469 | { |
michael@0 | 470 | #if defined(XP_WIN) |
michael@0 | 471 | if (!aFilePath) { |
michael@0 | 472 | if (aOutFd) { |
michael@0 | 473 | *aOutFd = INVALID_HANDLE_VALUE; |
michael@0 | 474 | } |
michael@0 | 475 | return; |
michael@0 | 476 | } |
michael@0 | 477 | HANDLE fd = CreateFileW(aFilePath, GENERIC_READ, FILE_SHARE_READ, nullptr, |
michael@0 | 478 | OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr); |
michael@0 | 479 | if (aOutFd) { |
michael@0 | 480 | *aOutFd = fd; |
michael@0 | 481 | } |
michael@0 | 482 | if (fd == INVALID_HANDLE_VALUE) { |
michael@0 | 483 | return; |
michael@0 | 484 | } |
michael@0 | 485 | ReadAhead(fd, aOffset, aCount); |
michael@0 | 486 | if (!aOutFd) { |
michael@0 | 487 | CloseHandle(fd); |
michael@0 | 488 | } |
michael@0 | 489 | #elif defined(LINUX) && !defined(ANDROID) || defined(XP_MACOSX) |
michael@0 | 490 | if (!aFilePath) { |
michael@0 | 491 | if (aOutFd) { |
michael@0 | 492 | *aOutFd = -1; |
michael@0 | 493 | } |
michael@0 | 494 | return; |
michael@0 | 495 | } |
michael@0 | 496 | int fd = open(aFilePath, O_RDONLY); |
michael@0 | 497 | if (aOutFd) { |
michael@0 | 498 | *aOutFd = fd; |
michael@0 | 499 | } |
michael@0 | 500 | if (fd < 0) { |
michael@0 | 501 | return; |
michael@0 | 502 | } |
michael@0 | 503 | size_t count; |
michael@0 | 504 | if (aCount == SIZE_MAX) { |
michael@0 | 505 | struct stat st; |
michael@0 | 506 | if (fstat(fd, &st) < 0) { |
michael@0 | 507 | if (!aOutFd) { |
michael@0 | 508 | close(fd); |
michael@0 | 509 | } |
michael@0 | 510 | return; |
michael@0 | 511 | } |
michael@0 | 512 | count = st.st_size; |
michael@0 | 513 | } else { |
michael@0 | 514 | count = aCount; |
michael@0 | 515 | } |
michael@0 | 516 | ReadAhead(fd, aOffset, count); |
michael@0 | 517 | if (!aOutFd) { |
michael@0 | 518 | close(fd); |
michael@0 | 519 | } |
michael@0 | 520 | #endif |
michael@0 | 521 | } |
michael@0 | 522 |