2017-07-31 16:13:38 +03:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["AsanReporter"];
|
2017-07-31 16:13:38 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2017-07-31 16:13:38 +03:00
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
AppConstants: "resource://gre/modules/AppConstants.jsm",
|
|
|
|
Log: "resource://gre/modules/Log.jsm",
|
|
|
|
OS: "resource://gre/modules/osfile.jsm",
|
|
|
|
Services: "resource://gre/modules/Services.jsm",
|
|
|
|
});
|
2017-07-31 16:13:38 +03:00
|
|
|
|
2018-05-26 03:02:29 +03:00
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["TextDecoder", "XMLHttpRequest"]);
|
2017-07-31 16:13:38 +03:00
|
|
|
|
|
|
|
// Define our prefs
|
|
|
|
const PREF_CLIENT_ID = "asanreporter.clientid";
|
|
|
|
const PREF_API_URL = "asanreporter.apiurl";
|
|
|
|
const PREF_AUTH_TOKEN = "asanreporter.authtoken";
|
|
|
|
const PREF_LOG_LEVEL = "asanreporter.loglevel";
|
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
const LOGGER_NAME = "asanreporter";
|
2017-07-31 16:13:38 +03:00
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
let logger;
|
2018-06-27 12:19:00 +03:00
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "asanDumpDir", () => {
|
|
|
|
let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
|
|
|
|
return OS.Path.join(profileDir.path, "asan");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.AsanReporter = {
|
2017-12-01 14:43:09 +03:00
|
|
|
init() {
|
|
|
|
if (this.initialized) {
|
|
|
|
return;
|
2019-07-05 10:55:19 +03:00
|
|
|
}
|
2017-12-01 14:43:09 +03:00
|
|
|
this.initialized = true;
|
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
// Setup logging
|
|
|
|
logger = Log.repository.getLogger(LOGGER_NAME);
|
|
|
|
logger.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter()));
|
|
|
|
logger.addAppender(new Log.DumpAppender(new Log.BasicFormatter()));
|
|
|
|
logger.level = Services.prefs.getIntPref(PREF_LOG_LEVEL, Log.Level.Info);
|
|
|
|
|
|
|
|
logger.info("Starting up...");
|
|
|
|
|
|
|
|
// Install a handler to observe tab crashes, so we can report those right
|
|
|
|
// after they happen instead of relying on the user to restart the browser.
|
2017-12-01 14:43:09 +03:00
|
|
|
Services.obs.addObserver(this, "ipc:content-shutdown");
|
2018-09-17 21:19:33 +03:00
|
|
|
|
|
|
|
processDirectory();
|
2017-12-01 14:43:09 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
observe(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic == "ipc:content-shutdown") {
|
|
|
|
aSubject.QueryInterface(Ci.nsIPropertyBag2);
|
|
|
|
if (!aSubject.get("abnormal")) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-17 21:19:33 +03:00
|
|
|
processDirectory();
|
2017-12-01 14:43:09 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-09-17 21:19:33 +03:00
|
|
|
function processDirectory() {
|
|
|
|
let iterator = new OS.File.DirectoryIterator(asanDumpDir);
|
2017-07-31 16:13:38 +03:00
|
|
|
let results = [];
|
|
|
|
|
|
|
|
// Scan the directory for any ASan logs that we haven't
|
|
|
|
// submitted yet. Store the filenames in an array so we
|
|
|
|
// can close the iterator early.
|
|
|
|
iterator
|
|
|
|
.forEach(entry => {
|
|
|
|
if (
|
|
|
|
entry.name.indexOf("ff_asan_log.") == 0 &&
|
2018-02-01 22:45:22 +03:00
|
|
|
!entry.name.includes("submitted")
|
|
|
|
) {
|
2017-07-31 16:13:38 +03:00
|
|
|
results.push(entry);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
iterator.close();
|
2017-10-15 21:50:30 +03:00
|
|
|
logger.info("Processing " + results.length + " reports...");
|
2019-07-05 10:55:19 +03:00
|
|
|
|
2017-07-31 16:13:38 +03:00
|
|
|
// Sequentially submit all reports that we found. Note that doing this
|
|
|
|
// with Promise.all would not result in a sequential ordering and would
|
|
|
|
// cause multiple requests to be sent to the server at once.
|
2017-10-15 21:50:30 +03:00
|
|
|
let requests = Promise.resolve();
|
2017-07-31 16:13:38 +03:00
|
|
|
results.forEach(result => {
|
|
|
|
requests = requests.then(
|
|
|
|
// We return a promise here that already handles any submit failures
|
|
|
|
// so our chain is not interrupted if one of the reports couldn't
|
|
|
|
// be submitted for some reason.
|
|
|
|
() =>
|
|
|
|
submitReport(result.path).then(
|
2017-10-15 21:50:30 +03:00
|
|
|
() => {
|
|
|
|
logger.info("Successfully submitted " + result.path);
|
|
|
|
},
|
|
|
|
e => {
|
|
|
|
logger.error(
|
|
|
|
"Failed to submit " + result.path + ". Reason: " + e
|
|
|
|
);
|
|
|
|
}
|
2017-07-31 16:13:38 +03:00
|
|
|
)
|
2017-10-15 21:50:30 +03:00
|
|
|
);
|
|
|
|
});
|
2017-07-31 16:13:38 +03:00
|
|
|
|
2017-10-15 21:50:30 +03:00
|
|
|
requests.then(() => logger.info("Done processing reports."));
|
2017-07-31 16:13:38 +03:00
|
|
|
},
|
|
|
|
e => {
|
|
|
|
iterator.close();
|
|
|
|
logger.error("Error while iterating over report files: " + e);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitReport(reportFile) {
|
|
|
|
logger.info("Processing " + reportFile);
|
|
|
|
return OS.File.read(reportFile)
|
|
|
|
.then(submitToServer)
|
|
|
|
.then(() => {
|
|
|
|
// Mark as submitted only if we successfully submitted it to the server.
|
2017-10-15 21:50:30 +03:00
|
|
|
return OS.File.move(reportFile, reportFile + ".submitted");
|
2017-07-31 16:13:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitToServer(data) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
logger.debug("Setting up XHR request");
|
2018-09-17 21:19:33 +03:00
|
|
|
let client = Services.prefs.getStringPref(PREF_CLIENT_ID);
|
|
|
|
let api_url = Services.prefs.getStringPref(PREF_API_URL);
|
|
|
|
let auth_token = Services.prefs.getStringPref(PREF_AUTH_TOKEN, null);
|
2017-07-31 16:13:38 +03:00
|
|
|
|
|
|
|
let decoder = new TextDecoder();
|
|
|
|
|
|
|
|
if (!client) {
|
|
|
|
client = "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
let versionArr = [
|
|
|
|
Services.appinfo.version,
|
|
|
|
Services.appinfo.appBuildID,
|
2018-08-31 08:59:17 +03:00
|
|
|
AppConstants.SOURCE_REVISION_URL || "unknown",
|
2017-10-15 21:50:30 +03:00
|
|
|
];
|
2019-07-05 10:55:19 +03:00
|
|
|
|
2017-07-31 16:13:38 +03:00
|
|
|
// Concatenate all relevant information as our server only
|
|
|
|
// has one field available for version information.
|
|
|
|
let product_version = versionArr.join("-");
|
|
|
|
let os = AppConstants.platform;
|
2019-07-05 10:55:19 +03:00
|
|
|
|
2017-07-31 16:13:38 +03:00
|
|
|
let reportObj = {
|
|
|
|
rawStdout: "",
|
|
|
|
rawStderr: "",
|
|
|
|
rawCrashData: decoder.decode(data),
|
|
|
|
// Hardcode platform as there is no other reasonable platform for ASan
|
|
|
|
platform: "x86-64",
|
|
|
|
product: "mozilla-central-asan-nightly",
|
|
|
|
product_version,
|
|
|
|
os,
|
|
|
|
client,
|
2018-08-31 08:59:17 +03:00
|
|
|
tool: "asan-nightly-program",
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2019-07-05 10:55:19 +03:00
|
|
|
|
2017-07-31 16:13:38 +03:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("POST", api_url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
2019-07-05 10:55:19 +03:00
|
|
|
|
2017-07-31 16:13:38 +03:00
|
|
|
// For internal testing purposes, an auth_token can be specified
|
|
|
|
if (auth_token) {
|
|
|
|
xhr.setRequestHeader("Authorization", "Token " + auth_token);
|
2018-11-22 02:18:27 +03:00
|
|
|
} else {
|
|
|
|
// Prevent privacy leaks
|
|
|
|
xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS;
|
2017-07-31 16:13:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
|
|
|
if (xhr.status == "201") {
|
|
|
|
logger.debug("XHR: OK");
|
|
|
|
resolve(xhr);
|
|
|
|
} else {
|
|
|
|
logger.debug(
|
|
|
|
"XHR: Status: " + xhr.status + " Response: " + xhr.responseText
|
|
|
|
);
|
|
|
|
reject(xhr);
|
|
|
|
}
|
2019-07-05 10:55:19 +03:00
|
|
|
}
|
2017-07-31 16:13:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify(reportObj));
|
|
|
|
});
|
|
|
|
}
|