Bug 1554380 - Fix some issues in mozilla::ReadAhead. r=aklotz

* CreateFileW will return INVALID_HANDLE_VALUE (-1) on failure, not NULL (0).
* MapViewOfFile will map the entire section if the size is 0. No explicit size
  is required.
* If SEC_IMAGE is specified, the mapped image size may be different from the
  file size on the storage.

Differential Revision: https://phabricator.services.mozilla.com/D32563

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masatoshi Kimura 2019-05-29 10:13:28 +00:00
parent e2745056eb
commit 6d9fff0168
3 changed files with 24 additions and 13 deletions

View File

@ -320,6 +320,16 @@ class MOZ_RAII PEHeaders final {
return reinterpret_cast<T>(absAddress);
}
Maybe<Span<const uint8_t>> GetBounds() const {
if (!mImageLimit) {
return Nothing();
}
auto base = reinterpret_cast<const uint8_t*>(mMzHeader);
DWORD imageSize = mPeHeader->OptionalHeader.SizeOfImage;
return Some(MakeSpan(base, imageSize));
}
PIMAGE_IMPORT_DESCRIPTOR GetImportDirectory() {
return GetImageDirectoryEntry<PIMAGE_IMPORT_DESCRIPTOR>(
IMAGE_DIRECTORY_ENTRY_IMPORT);

View File

@ -154,6 +154,8 @@ class nsAutoRef : public nsAutoRefBase<T> {
// like a raw reference.
operator typename SimpleRef::RawRef() const { return this->get(); }
explicit operator bool() const { return this->HaveResource(); }
// Transfer ownership from another smart reference.
void steal(ThisClass& aOtherRef) { BaseClass::steal(aOtherRef); }

View File

@ -35,6 +35,7 @@
# include <sys/stat.h>
#elif defined(XP_WIN)
# include <nsWindowsHelpers.h>
# include <mozilla/NativeNt.h>
# include <mozilla/ScopeExit.h>
#endif
@ -368,27 +369,25 @@ void mozilla::ReadAheadLib(mozilla::pathstr_t aFilePath) {
if (!fd) {
return;
}
LARGE_INTEGER fileSize = {};
BOOL success = GetFileSizeEx(fd, &fileSize);
if (!success || !fileSize.QuadPart ||
fileSize.QuadPart > std::numeric_limits<size_t>::max()) {
return;
}
nsAutoHandle mapping(
CreateFileMapping(fd, nullptr, SEC_IMAGE | PAGE_READONLY, 0, 0, nullptr));
if (!mapping) {
return;
}
PVOID data =
MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, (size_t)fileSize.QuadPart);
auto guard = MakeScopeExit([=]() { UnmapViewOfFile(data); });
if (data) {
PrefetchMemory((uint8_t*)data, (size_t)fileSize.QuadPart);
PVOID data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
if (!data) {
return;
}
auto guard = MakeScopeExit([=]() { UnmapViewOfFile(data); });
mozilla::nt::PEHeaders headers(data);
Maybe<Span<const uint8_t>> bounds = headers.GetBounds();
if (!bounds) {
return;
}
PrefetchMemory((uint8_t*)data, bounds->Length());
#elif defined(LINUX) && !defined(ANDROID)
int fd = open(aFilePath, O_RDONLY);