Bug 1513975 - Add ChromeUtils.releaseAssert for Chrome JS, r=kmag

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Nika Layzell 2019-01-03 15:43:06 +00:00
parent 10ba88604c
commit eded746df2
3 changed files with 44 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "nsThreadUtils.h"
#include "mozJSComponentLoader.h"
#include "GeckoProfiler.h"
#include "nsIException.h"
namespace mozilla {
namespace dom {
@ -132,6 +133,34 @@ namespace dom {
aRetval.set(buffer);
}
/* static */ void ChromeUtils::ReleaseAssert(GlobalObject& aGlobal,
bool aCondition,
const nsAString& aMessage) {
// If the condition didn't fail, which is the likely case, immediately return.
if (MOZ_LIKELY(aCondition)) {
return;
}
// Extract the current stack from the JS runtime to embed in the crash reason.
nsAutoString filename;
uint32_t lineNo = 0;
if (nsCOMPtr<nsIStackFrame> location = GetCurrentJSStack(1)) {
location->GetFilename(aGlobal.Context(), filename);
lineNo = location->GetLineNumber(aGlobal.Context());
} else {
filename.Assign(NS_LITERAL_STRING("<unknown>"));
}
// Convert to utf-8 for adding as the MozCrashReason.
NS_ConvertUTF16toUTF8 filenameUtf8(filename);
NS_ConvertUTF16toUTF8 messageUtf8(aMessage);
// Actually crash.
MOZ_CRASH_UNSAFE_PRINTF("Failed ChromeUtils.releaseAssert(\"%s\") @ %s:%u",
messageUtf8.get(), filenameUtf8.get(), lineNo);
}
/* static */ void ChromeUtils::WaiveXrays(GlobalObject& aGlobal,
JS::HandleValue aVal,
JS::MutableHandleValue aRetval,

View File

@ -69,6 +69,9 @@ class ChromeUtils {
JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv);
static void ReleaseAssert(GlobalObject& aGlobal, bool aCondition,
const nsAString& aMessage);
static void OriginAttributesToSuffix(
GlobalObject& aGlobal, const dom::OriginAttributesDictionary& aAttrs,
nsCString& aSuffix);

View File

@ -105,6 +105,18 @@ namespace ChromeUtils {
ArrayBuffer base64URLDecode(ByteString string,
Base64URLDecodeOptions options);
/**
* Cause the current process to fatally crash unless the given condition is
* true. This is similar to MOZ_RELEASE_ASSERT in C++ code.
*
* WARNING: This message is included publicly in the crash report, and must
* not contain private information.
*
* Crash report will be augmented with the current JS stack information.
*/
void releaseAssert(boolean condition,
optional DOMString message = "<no message>");
#ifdef NIGHTLY_BUILD
/**