Bug 889911 - Switch xpcshell to SystemErrorReporter with a little bit of special magic. r=mrbkap

XPCShell currently overrides all the JSContexts whose creation it observes with
its own custom error reporter. This reporter does all sorts of funny things which
we try to clean up for the most part. But there are a few very intricate
considerations at play.

First, the old xpcshell error reporter does some mumbo jumbo with the
XPCCallContext stack to try to guess whether some other code might catch the
exception. This is total garbage on a number of fronts, particularly because
the XPCCallContext stack has no concept of saved frame chains, nested event
loops, sandbox boundaries, origin boundaries, or any of the myriad of
complicating factors that determine whether or not an exception will propagate.

So we get rid of it. But this causes some crazy debugger tests to fail, because
they rely on an exception from uriloader/exthandler/nsHandlerService.js getting
squelched, and can't handle anybody reporting errors to the console service at
the particular moment of contortionism when the exception is raised. So we need
to introduce an explicit mechanism to disable the error reporter here to keep
things running.

Second, we have to be very careful about tracking the return status of the
xpcshell binary. The old code would simply flag an error code if the error
handler was invoked, and we can mostly continue to do that. But there are some
complications. See the comments.

Finally, we don't anything analogous in XPCShellEnvironment, because I have
patches in bug 889714 to remove its state-dependence on the error reporter.
I'll switch it to SystemErrorReporter in that bug.
This commit is contained in:
Bobby Holley 2013-07-16 20:38:44 -07:00
Родитель b0aff6e915
Коммит eb2e873e6c
4 изменённых файлов: 60 добавлений и 92 удалений

Просмотреть файл

@ -139,6 +139,7 @@ FILE *gErrFile = NULL;
FILE *gInFile = NULL;
int gExitCode = 0;
bool gIgnoreReportedErrors = false;
JSBool gQuitting = false;
static JSBool reportWarnings = true;
static JSBool compileOnly = false;
@ -266,97 +267,6 @@ GetLine(JSContext *cx, char *bufp, FILE *file, const char *prompt) {
return true;
}
static void
my_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
{
int i, j, k, n;
char *prefix = NULL, *tmp;
const char *ctmp;
nsCOMPtr<nsIXPConnect> xpc;
// Don't report an exception from inner JS frames as the callers may intend
// to handle it.
if (JS_DescribeScriptedCaller(cx, nullptr, nullptr)) {
return;
}
// In some cases cx->fp is null here so use XPConnect to tell us about inner
// frames.
if ((xpc = do_GetService(nsIXPConnect::GetCID()))) {
nsAXPCNativeCallContext *cc = nullptr;
xpc->GetCurrentNativeCallContext(&cc);
if (cc) {
nsAXPCNativeCallContext *prev = cc;
while (NS_SUCCEEDED(prev->GetPreviousCallContext(&prev)) && prev) {
uint16_t lang;
if (NS_SUCCEEDED(prev->GetLanguage(&lang)) &&
lang == nsAXPCNativeCallContext::LANG_JS) {
return;
}
}
}
}
if (!report) {
fprintf(gErrFile, "%s\n", message);
return;
}
/* Conditionally ignore reported warnings. */
if (JSREPORT_IS_WARNING(report->flags) && !reportWarnings)
return;
if (report->filename)
prefix = JS_smprintf("%s:", report->filename);
if (report->lineno) {
tmp = prefix;
prefix = JS_smprintf("%s%u: ", tmp ? tmp : "", report->lineno);
JS_free(cx, tmp);
}
if (JSREPORT_IS_WARNING(report->flags)) {
tmp = prefix;
prefix = JS_smprintf("%s%swarning: ",
tmp ? tmp : "",
JSREPORT_IS_STRICT(report->flags) ? "strict " : "");
JS_free(cx, tmp);
}
/* embedded newlines -- argh! */
while ((ctmp = strchr(message, '\n')) != 0) {
ctmp++;
if (prefix) fputs(prefix, gErrFile);
fwrite(message, 1, ctmp - message, gErrFile);
message = ctmp;
}
/* If there were no filename or lineno, the prefix might be empty */
if (prefix)
fputs(prefix, gErrFile);
fputs(message, gErrFile);
if (!report->linebuf) {
fputc('\n', gErrFile);
goto out;
}
fprintf(gErrFile, ":\n%s%s\n%s", prefix, report->linebuf, prefix);
n = report->tokenptr - report->linebuf;
for (i = j = 0; i < n; i++) {
if (report->linebuf[i] == '\t') {
for (k = (j + 8) & ~7; j < k; j++) {
fputc('.', gErrFile);
}
continue;
}
fputc('.', gErrFile);
j++;
}
fputs("^\n", gErrFile);
out:
if (!JSREPORT_IS_WARNING(report->flags))
gExitCode = EXITCODE_RUNTIME_ERROR;
JS_free(cx, prefix);
}
static JSBool
ReadLine(JSContext *cx, unsigned argc, jsval *vp)
{
@ -518,6 +428,21 @@ Quit(JSContext *cx, unsigned argc, jsval *vp)
return false;
}
// Provide script a way to disable the xpcshell error reporter, preventing
// reported errors from being logged to the console and also from affecting the
// exit code returned by the xpcshell binary.
static JSBool
IgnoreReportedErrors(JSContext *cx, unsigned argc, jsval *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (argc != 1 || !args[0].isBoolean()) {
JS_ReportError(cx, "Bad arguments");
return false;
}
gIgnoreReportedErrors = args[0].toBoolean();
return true;
}
static JSBool
DumpXPC(JSContext *cx, unsigned argc, jsval *vp)
{
@ -892,6 +817,7 @@ static const JSFunctionSpec glob_functions[] = {
JS_FS("readline", ReadLine, 1,0),
JS_FS("load", Load, 1,0),
JS_FS("quit", Quit, 0,0),
JS_FS("ignoreReportedErrors", IgnoreReportedErrors, 1,0),
JS_FS("version", Version, 1,0),
JS_FS("build", BuildDate, 0,0),
JS_FS("dumpXPC", DumpXPC, 1,0),
@ -1378,6 +1304,7 @@ ProcessArgs(JSContext *cx, JS::Handle<JSObject*> obj, char **argv, int argc, XPC
if (filename || isInteractive)
Process(cx, obj, filename, forceTTY);
return gExitCode;
}
@ -1462,6 +1389,19 @@ nsXPCFunctionThisTranslator::TranslateThis(nsISupports *aInitialThis,
// ContextCallback calls are chained
static JSContextCallback gOldJSContextCallback;
void
XPCShellErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep)
{
if (gIgnoreReportedErrors)
return;
if (!JSREPORT_IS_WARNING(rep->flags))
gExitCode = EXITCODE_RUNTIME_ERROR;
// Delegate to the system error reporter for heavy lifting.
xpc::SystemErrorReporterExternal(cx, message, rep);
}
static JSBool
ContextCallback(JSContext *cx, unsigned contextOp)
{
@ -1469,7 +1409,7 @@ ContextCallback(JSContext *cx, unsigned contextOp)
return false;
if (contextOp == JSCONTEXT_NEW) {
JS_SetErrorReporter(cx, my_ErrorReporter);
JS_SetErrorReporter(cx, XPCShellErrorReporter);
}
return true;
}

Просмотреть файл

@ -11,6 +11,15 @@ var gThreadClient;
Components.utils.import('resource:///modules/devtools/SourceMap.jsm');
// Deep in the complicated labyrinth of code that this test invokes, beneath
// debugger callbacks, sandboxes and nested event loops, lies an exception.
// This exception lay sleeping since the dawn of time, held captive in a
// delicate balance of custom xpcshell error reporters and garbage data about
// the XPCCallContext stack. But bholley dug too greedily, and too deep, and
// awoke shadow and flame in the darkness of nsExternalHelperAppService.cpp.
// We must now trust in deep magic to ensure that it does not awaken again.
ignoreReportedErrors(true);
function run_test()
{
initTestDebuggerServer();

Просмотреть файл

@ -11,6 +11,15 @@ var gThreadClient;
Components.utils.import('resource:///modules/devtools/SourceMap.jsm');
// Deep in the complicated labyrinth of code that this test invokes, beneath
// debugger callbacks, sandboxes and nested event loops, lies an exception.
// This exception lay sleeping since the dawn of time, held captive in a
// delicate balance of custom xpcshell error reporters and garbage data about
// the XPCCallContext stack. But bholley dug too greedily, and too deep, and
// awoke shadow and flame in the darkness of nsExternalHelperAppService.cpp.
// We must now trust in deep magic to ensure that it does not awaken again.
ignoreReportedErrors(true);
function run_test()
{
initTestDebuggerServer();

Просмотреть файл

@ -66,12 +66,22 @@ function get_modules_under(uri) {
function load_modules_under(spec, uri) {
var entries = get_modules_under(uri);
// The precompilation of JS here sometimes reports errors, which we don't
// really care about. But if the errors are ever reported to xpcshell's
// error reporter, it will cause it to return an error code, which will break
// automation. Currently they won't be, because the component loader spins up
// its JSContext before xpcshell has time to set its context callback (which
// overrides the error reporter on all newly-created JSContexts). But as we
// move towards a singled-cxed browser, we'll run into this. So let's be
// forward-thinking and deal with it now.
ignoreReportedErrors(true);
for each (let entry in entries) {
try {
dump(spec + entry + "\n");
Cu.import(spec + entry, null);
} catch(e) {}
}
ignoreReportedErrors(false);
}
function resolveResource(spec) {