Bug 1786494 - Part 7: Use ErrorContext in XDR error check. r=bthrall

Differential Revision: https://phabricator.services.mozilla.com/D157447
This commit is contained in:
Tooru Fujisawa 2022-09-28 07:40:16 +00:00
parent 50822aeff2
commit 2e19266ffb
3 changed files with 23 additions and 10 deletions

View File

@ -1400,7 +1400,7 @@ XDRResult XDRStencilEncoder::codeStencil(
const frontend::CompilationStencil& stencil) {
#ifdef DEBUG
auto sanityCheck = mozilla::MakeScopeExit(
[&] { MOZ_ASSERT(validateResultCode(cx(), resultCode())); });
[&] { MOZ_ASSERT(validateResultCode(cx(), ec(), resultCode())); });
#endif
MOZ_TRY(frontend::StencilXDR::checkCompilationStencil(this, stencil));
@ -1449,7 +1449,7 @@ XDRResult XDRStencilDecoder::codeStencil(
const JS::DecodeOptions& options, frontend::CompilationStencil& stencil) {
#ifdef DEBUG
auto sanityCheck = mozilla::MakeScopeExit(
[&] { MOZ_ASSERT(validateResultCode(cx(), resultCode())); });
[&] { MOZ_ASSERT(validateResultCode(cx(), ec(), resultCode())); });
#endif
auto resetOptions = mozilla::MakeScopeExit([&] { options_ = nullptr; });

View File

@ -19,24 +19,36 @@
#include <utility> // std::move
#include "js/Transcoding.h" // JS::TranscodeResult, JS::TranscodeBuffer, JS::TranscodeRange
#include "js/UniquePtr.h" // UniquePtr
#include "js/Utility.h" // JS::FreePolicy, js_delete
#include "vm/JSContext.h" // JSContext, ReportAllocationOverflow
#include "vm/StringType.h" // JSString
#include "js/UniquePtr.h" // UniquePtr
#include "js/Utility.h" // JS::FreePolicy, js_delete
#include "vm/ErrorContext.h" // ErrorContext
#include "vm/JSContext.h" // JSContext, ReportAllocationOverflow
#include "vm/StringType.h" // JSString
using namespace js;
using mozilla::Utf8Unit;
#ifdef DEBUG
bool XDRCoderBase::validateResultCode(JSContext* cx,
bool XDRCoderBase::validateResultCode(JSContext* cx, ErrorContext* ec,
JS::TranscodeResult code) const {
// NOTE: This function is called to verify that we do not have a pending
// exception on the JSContext at the same time as a TranscodeResult failure.
if (cx->isHelperThreadContext()) {
return true;
}
return cx->isExceptionPending() == bool(code == JS::TranscodeResult::Throw);
// NOTE: Errors during XDR encode/decode are supposed to be reported to
// ErrorContext, instead of JSContext.
// This branch is for covering remaining consumer of JSContext for
// error reporting (e.g. memory allocation).
// TODO: Remove this once JSContext is removed from frontend and all errors
// are reported to ErrorContext.
if (cx->isExceptionPending()) {
return bool(code == JS::TranscodeResult::Throw);
}
return ec->hadErrors() == bool(code == JS::TranscodeResult::Throw);
}
#endif

View File

@ -189,7 +189,8 @@ class XDRCoderBase {
MOZ_ASSERT(resultCode() == JS::TranscodeResult::Ok);
resultCode_ = code;
}
bool validateResultCode(JSContext* cx, JS::TranscodeResult code) const;
bool validateResultCode(JSContext* cx, ErrorContext* ec,
JS::TranscodeResult code) const;
#endif
};
@ -227,7 +228,7 @@ class XDRState : public XDRCoderBase {
XDRResultT<T> fail(JS::TranscodeResult code) {
#ifdef DEBUG
MOZ_ASSERT(code != JS::TranscodeResult::Ok);
MOZ_ASSERT(validateResultCode(cx(), code));
MOZ_ASSERT(validateResultCode(cx(), ec(), code));
setResultCode(code);
#endif
return mozilla::Err(code);