Bug 1739669 - Configure hunspell rlbox sandbox for higher mem size depending on locale r=bholley

Differential Revision: https://phabricator.services.mozilla.com/D130544
This commit is contained in:
shravanrn@gmail.com 2021-11-09 16:58:26 +00:00
parent 1c885477eb
commit 5864d16cf7
3 changed files with 58 additions and 7 deletions

View File

@ -41,7 +41,38 @@ RLBoxHunspell::RLBoxHunspell(const nsAutoCString& affpath,
const nsAutoCString& dpath)
: mHandle(nullptr) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
#if defined(MOZ_WASM_SANDBOXING_HUNSPELL) && !defined(HAVE_64BIT_BUILD)
// By default, the rlbox sandbox size is smaller on 32-bit builds than the max
// 4GB We may need to ask for a larger sandbox size for hunspell to spellcheck
// in some locales See Bug 1739669 for more details
const uint64_t defaultMaxSizeForSandbox =
wasm_rt_get_default_max_linear_memory_size();
// We first get the size of the dictionary
Result<int64_t, nsresult> dictSizeResult =
mozHunspellFileMgrHost::GetSize(dpath);
MOZ_RELEASE_ASSERT(dictSizeResult.isOk());
int64_t dictSize = dictSizeResult.unwrap();
MOZ_RELEASE_ASSERT(dictSize >= 0);
// Next, we compute the expected memory needed for hunspell spell checking.
// This seems to be about 7x the size of the dictionary (See Bug 1739669)
const uint64_t expectedMaxMemory = 7 * dictSize;
// If we expect a higher memory usage, override the defaults
// else stick with the defaults for the sandbox
if (expectedMaxMemory > defaultMaxSizeForSandbox) {
mSandbox.create_sandbox(true /* abort on creation failure */,
expectedMaxMemory);
} else {
mSandbox.create_sandbox();
}
#else
mSandbox.create_sandbox();
#endif
// Add the aff and dict files to allow list
if (!affpath.IsEmpty()) {

View File

@ -9,7 +9,6 @@
#include "mozHunspellRLBoxHost.h"
#include "mozilla/DebugOnly.h"
#include "nsContentUtils.h"
#include "nsIChannel.h"
#include "nsILoadInfo.h"
#include "nsNetUtil.h"
#include "nsUnicharUtils.h"
@ -19,21 +18,24 @@
using namespace mozilla;
mozHunspellFileMgrHost::mozHunspellFileMgrHost(const nsCString& aFilename) {
DebugOnly<Result<Ok, nsresult>> result = Open(aFilename);
nsCOMPtr<nsIChannel> channel;
DebugOnly<Result<Ok, nsresult>> result = Open(aFilename, channel, mStream);
NS_WARNING_ASSERTION(result.value.isOk(), "Failed to open Hunspell file");
}
Result<Ok, nsresult> mozHunspellFileMgrHost::Open(const nsCString& aPath) {
/* static */
Result<Ok, nsresult> mozHunspellFileMgrHost::Open(
const nsCString& aPath, nsCOMPtr<nsIChannel>& aChannel,
nsCOMPtr<nsIInputStream>& aStream) {
nsCOMPtr<nsIURI> uri;
MOZ_TRY(NS_NewURI(getter_AddRefs(uri), aPath));
nsCOMPtr<nsIChannel> channel;
MOZ_TRY(NS_NewChannel(
getter_AddRefs(channel), uri, nsContentUtils::GetSystemPrincipal(),
getter_AddRefs(aChannel), uri, nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT,
nsIContentPolicy::TYPE_OTHER));
MOZ_TRY(channel->Open(getter_AddRefs(mStream)));
MOZ_TRY(aChannel->Open(getter_AddRefs(aStream)));
return Ok();
}
@ -52,6 +54,19 @@ Result<Ok, nsresult> mozHunspellFileMgrHost::ReadLine(nsCString& aLine) {
return Ok();
}
/* static */
Result<int64_t, nsresult> mozHunspellFileMgrHost::GetSize(
const nsCString& aFilename) {
int64_t ret = -1;
nsCOMPtr<nsIChannel> channel;
nsCOMPtr<nsIInputStream> stream;
MOZ_TRY(Open(aFilename, channel, stream));
channel->GetContentLength(&ret);
return ret;
}
bool mozHunspellFileMgrHost::GetLine(std::string& aResult) {
nsAutoCString line;
auto res = ReadLine(line);

View File

@ -18,6 +18,7 @@
#include "mozilla/Result.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/RWLock.h"
#include "nsIChannel.h"
#include "nsIInputStream.h"
#include "nsReadLine.h"
@ -34,8 +35,12 @@ class mozHunspellFileMgrHost final {
bool GetLine(std::string& aResult);
int GetLineNum() const { return mLineNum; }
static Result<int64_t, nsresult> GetSize(const nsCString& aFilename);
private:
mozilla::Result<mozilla::Ok, nsresult> Open(const nsCString& aPath);
static mozilla::Result<mozilla::Ok, nsresult> Open(
const nsCString& aPath, nsCOMPtr<nsIChannel>& aChannel,
nsCOMPtr<nsIInputStream>& aStream);
mozilla::Result<mozilla::Ok, nsresult> ReadLine(nsCString& aLine);