2018-06-23 06:30:23 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2018-06-23 06:30:23 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MemMapSnapshot.h"
|
|
|
|
|
|
|
|
#include "base/eintr_wrapper.h"
|
|
|
|
#include "base/file_util.h"
|
|
|
|
#include "mozilla/ResultExtensions.h"
|
|
|
|
#include "mozilla/ScopeExit.h"
|
|
|
|
#include "mozilla/ipc/FileDescriptorUtils.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
2018-11-30 13:46:48 +03:00
|
|
|
#include <sys/stat.h>
|
2018-06-23 06:30:23 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using loader::AutoMemMap;
|
|
|
|
|
|
|
|
namespace ipc {
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Init(size_t aSize) {
|
2018-06-23 06:30:23 +03:00
|
|
|
MOZ_ASSERT(!mInitialized);
|
|
|
|
|
|
|
|
MOZ_TRY(Create(aSize));
|
|
|
|
|
|
|
|
mInitialized = true;
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Finalize(AutoMemMap& aMem) {
|
2018-06-23 06:30:23 +03:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
|
|
|
MOZ_TRY(Freeze(aMem));
|
|
|
|
|
|
|
|
mInitialized = false;
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Create(size_t aSize) {
|
|
|
|
HANDLE handle = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr,
|
|
|
|
PAGE_READWRITE, 0, DWORD(aSize), nullptr);
|
2018-06-23 06:30:23 +03:00
|
|
|
|
|
|
|
if (!handle) {
|
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
mFile.emplace(handle);
|
|
|
|
return mMem.initWithHandle(mFile.ref(), aSize, PR_PROT_READWRITE);
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Freeze(AutoMemMap& aMem) {
|
2018-06-23 06:30:23 +03:00
|
|
|
auto orig = mFile.ref().ClonePlatformHandle();
|
|
|
|
mFile.reset();
|
|
|
|
|
|
|
|
HANDLE handle;
|
2018-11-30 13:46:48 +03:00
|
|
|
if (!::DuplicateHandle(
|
|
|
|
GetCurrentProcess(), orig.release(), GetCurrentProcess(), &handle,
|
|
|
|
GENERIC_READ | FILE_MAP_READ, false, DUPLICATE_CLOSE_SOURCE)) {
|
2018-06-23 06:30:23 +03:00
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aMem.initWithHandle(FileDescriptor(handle), mMem.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Create(size_t aSize) {
|
2018-06-23 06:30:23 +03:00
|
|
|
FilePath path;
|
|
|
|
ScopedCloseFile fd(file_util::CreateAndOpenTemporaryShmemFile(&path));
|
|
|
|
if (!fd) {
|
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HANDLE_EINTR(ftruncate(fileno(fd), aSize)) != 0) {
|
|
|
|
return Err(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_TRY(mMem.init(FILEToFileDescriptor(fd), PR_PROT_READWRITE));
|
|
|
|
|
|
|
|
mPath.Assign(path.value().data(), path.value().length());
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Result<Ok, nsresult> MemMapSnapshot::Freeze(AutoMemMap& aMem) {
|
2018-06-23 06:30:23 +03:00
|
|
|
// Delete the shm file after we're done here, whether we succeed or not. The
|
|
|
|
// open file descriptor will keep it alive until all remaining references
|
|
|
|
// are closed, at which point it will be automatically freed.
|
2018-11-30 13:46:48 +03:00
|
|
|
auto cleanup = MakeScopeExit([&]() { PR_Delete(mPath.get()); });
|
2018-06-23 06:30:23 +03:00
|
|
|
|
|
|
|
// Make the shm file readonly. This doesn't make a difference in practice,
|
|
|
|
// since we open and share a read-only file descriptor, and then delete the
|
|
|
|
// file. But it doesn't hurt, either.
|
|
|
|
chmod(mPath.get(), 0400);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
MOZ_TRY(NS_NewNativeLocalFile(mPath, /* followLinks = */ false,
|
|
|
|
getter_AddRefs(file)));
|
|
|
|
|
2018-07-13 09:13:04 +03:00
|
|
|
return aMem.init(file);
|
2018-06-23 06:30:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2018-11-30 13:46:48 +03:00
|
|
|
#error "Unsupported build configuration"
|
2018-06-23 06:30:23 +03:00
|
|
|
#endif
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|