Bug 1149294 - Part 3: Split thread-safe methods on ChromeUtils out into a new

ThreadSafeChromeUtils interface and move the utils interfaces into dom/base;
r=bholley
This commit is contained in:
Nick Fitzgerald 2015-06-17 11:12:23 -07:00
parent a958a1be5f
commit 6df5f8d965
18 changed files with 611 additions and 577 deletions

23
dom/base/ChromeUtils.cpp Normal file
View File

@ -0,0 +1,23 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
#include "ChromeUtils.h"
#include "mozilla/BasePrincipal.h"
namespace mozilla {
namespace dom {
/* static */ void
ChromeUtils::OriginAttributesToCookieJar(GlobalObject& aGlobal,
const OriginAttributesDictionary& aAttrs,
nsCString& aCookieJar)
{
OriginAttributes attrs(aAttrs);
attrs.CookieJar(aCookieJar);
}
} // namespace dom
} // namespace mozilla

52
dom/base/ChromeUtils.h Normal file
View File

@ -0,0 +1,52 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
#ifndef mozilla_dom_ChromeUtils__
#define mozilla_dom_ChromeUtils__
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ChromeUtilsBinding.h"
#include "mozilla/dom/ThreadSafeChromeUtilsBinding.h"
#include "mozilla/ErrorResult.h"
namespace mozilla {
namespace devtools {
class HeapSnapshot;
}
namespace dom {
class ThreadSafeChromeUtils
{
public:
// Implemented in toolkit/devtools/server/HeapSnapshot.cpp
static void SaveHeapSnapshot(GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
const HeapSnapshotBoundaries& boundaries,
ErrorResult& rv);
// Implemented in toolkit/devtools/server/HeapSnapshot.cpp
static already_AddRefed<devtools::HeapSnapshot> ReadHeapSnapshot(GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
ErrorResult& rv);
};
class ChromeUtils : public ThreadSafeChromeUtils
{
public:
static void
OriginAttributesToCookieJar(dom::GlobalObject& aGlobal,
const dom::OriginAttributesDictionary& aAttrs,
nsCString& aCookieJar);
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_ChromeUtils__

View File

@ -14,6 +14,7 @@
#include "mozilla/dom/BlobBinding.h"
#include "mozilla/dom/FileBinding.h"
#include <algorithm>
#include "nsPIDOMWindow.h"
using namespace mozilla;
using namespace mozilla::dom;

View File

@ -154,6 +154,7 @@ EXPORTS.mozilla.dom += [
'BarProps.h',
'BlobSet.h',
'ChildIterator.h',
'ChromeUtils.h',
'Comment.h',
'Console.h',
'DirectionalityUtils.h',
@ -209,6 +210,7 @@ UNIFIED_SOURCES += [
'Attr.cpp',
'BarProps.cpp',
'ChildIterator.cpp',
'ChromeUtils.cpp',
'Comment.cpp',
'Console.cpp',
'Crypto.cpp',

View File

@ -4,7 +4,7 @@
* 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/. */
/*
/*
* Implementation of the "@mozilla.org/layout/content-policy;1" contract.
*/
@ -15,11 +15,15 @@
#include "nsContentPolicyUtils.h"
#include "nsContentPolicy.h"
#include "nsIURI.h"
#include "nsIDocShell.h"
#include "nsIDOMNode.h"
#include "nsIDOMWindow.h"
#include "nsIContent.h"
#include "nsILoadContext.h"
#include "nsCOMArray.h"
using mozilla::LogLevel;
NS_IMPL_ISUPPORTS(nsContentPolicy, nsIContentPolicy)
static PRLogModuleInfo* gConPolLog;

View File

@ -270,8 +270,6 @@ DOMInterfaces = {
# The codegen is dumb, and doesn't understand that this interface is only a
# collection of static methods, so we have this `concrete: False` hack.
'concrete': False,
'nativeType': 'mozilla::devtools::ChromeUtils',
'implicitJSContext': ['readHeapSnapshot', 'saveHeapSnapshot']
},
'ChromeWindow': {
@ -1273,6 +1271,14 @@ DOMInterfaces = {
'wrapperCache': False
},
'ThreadSafeChromeUtils': {
# The codegen is dumb, and doesn't understand that this interface is only a
# collection of static methods, so we have this `concrete: False` hack.
'concrete': False,
'headerFile': 'mozilla/dom/ChromeUtils.h',
'implicitJSContext': ['readHeapSnapshot', 'saveHeapSnapshot']
},
'TimeRanges': {
'wrapperCache': False
},

View File

@ -5,30 +5,11 @@
*/
/**
* A collection of static utility methods that are only exposed to Chrome.
* A collection of static utility methods that are only exposed to Chrome. This
* interface is not exposed in workers, while ThreadSafeChromeUtils is.
*/
[ChromeOnly, Exposed=(Window,System,Worker)]
interface ChromeUtils {
/**
* Serialize a snapshot of the heap graph, as seen by |JS::ubi::Node| and
* restricted by |boundaries|, and write it to the provided file path.
*
* @param filePath The file path to write the heap snapshot to.
*
* @param boundaries The portion of the heap graph to write.
*/
[Throws]
static void saveHeapSnapshot(DOMString filePath,
optional HeapSnapshotBoundaries boundaries);
/**
* Deserialize a core dump into a HeapSnapshot.
*
* @param filePath The file path to read the core dump from.
*/
[Throws, NewObject]
static HeapSnapshot readHeapSnapshot(DOMString filePath);
[ChromeOnly, Exposed=(Window,System)]
interface ChromeUtils : ThreadSafeChromeUtils {
/**
* A helper that converts OriginAttributesDictionary to cookie jar opaque
* identfier.
@ -39,38 +20,6 @@ interface ChromeUtils {
originAttributesToCookieJar(optional OriginAttributesDictionary originAttrs);
};
/**
* A JS object whose properties specify what portion of the heap graph to
* write. The recognized properties are:
*
* * globals: [ global, ... ]
* Dump only nodes that either:
* - belong in the compartment of one of the given globals;
* - belong to no compartment, but do belong to a Zone that contains one of
* the given globals;
* - are referred to directly by one of the last two kinds of nodes; or
* - is the fictional root node, described below.
*
* * debugger: Debugger object
* Like "globals", but use the Debugger's debuggees as the globals.
*
* * runtime: true
* Dump the entire heap graph, starting with the JSRuntime's roots.
*
* One, and only one, of these properties must exist on the boundaries object.
*
* The root of the dumped graph is a fictional node whose ubi::Node type name is
* "CoreDumpRoot". If we are dumping the entire ubi::Node graph, this root node
* has an edge for each of the JSRuntime's roots. If we are dumping a selected
* set of globals, the root has an edge to each global, and an edge for each
* incoming JS reference to the selected Zones.
*/
dictionary HeapSnapshotBoundaries {
sequence<object> globals;
object debugger;
boolean runtime;
};
/**
* Used by principals and the script security manager to represent origin
* attributes.

View File

@ -0,0 +1,64 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/.
*/
/**
* A collection of **thread-safe** static utility methods that are only exposed
* to Chrome. This interface is exposed in workers, while ChromeUtils is not.
*/
[ChromeOnly, Exposed=(Window,System,Worker)]
interface ThreadSafeChromeUtils {
/**
* Serialize a snapshot of the heap graph, as seen by |JS::ubi::Node| and
* restricted by |boundaries|, and write it to the provided file path.
*
* @param filePath The file path to write the heap snapshot to.
*
* @param boundaries The portion of the heap graph to write.
*/
[Throws]
static void saveHeapSnapshot(DOMString filePath,
optional HeapSnapshotBoundaries boundaries);
/**
* Deserialize a core dump into a HeapSnapshot.
*
* @param filePath The file path to read the heap snapshot from.
*/
[Throws, NewObject]
static HeapSnapshot readHeapSnapshot(DOMString filePath);
};
/**
* A JS object whose properties specify what portion of the heap graph to
* write. The recognized properties are:
*
* * globals: [ global, ... ]
* Dump only nodes that either:
* - belong in the compartment of one of the given globals;
* - belong to no compartment, but do belong to a Zone that contains one of
* the given globals;
* - are referred to directly by one of the last two kinds of nodes; or
* - is the fictional root node, described below.
*
* * debugger: Debugger object
* Like "globals", but use the Debugger's debuggees as the globals.
*
* * runtime: true
* Dump the entire heap graph, starting with the JSRuntime's roots.
*
* One, and only one, of these properties must exist on the boundaries object.
*
* The root of the dumped graph is a fictional node whose ubi::Node type name is
* "CoreDumpRoot". If we are dumping the entire ubi::Node graph, this root node
* has an edge for each of the JSRuntime's roots. If we are dumping a selected
* set of globals, the root has an edge to each global, and an edge for each
* incoming JS reference to the selected Zones.
*/
dictionary HeapSnapshotBoundaries {
sequence<object> globals;
object debugger;
boolean runtime;
};

View File

@ -522,6 +522,7 @@ WEBIDL_FILES = [
'TextTrack.webidl',
'TextTrackCueList.webidl',
'TextTrackList.webidl',
'ThreadSafeChromeUtils.webidl',
'TimeEvent.webidl',
'TimeRanges.webidl',
'Touch.webidl',

View File

@ -14,6 +14,7 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"
#include "jsapi.h"
#include "jspubtd.h"
#include "js/GCAPI.h"

View File

@ -117,9 +117,9 @@ class SimpleEdgeVectorTracer : public JS::CallbackTracer {
// Don't trace permanent atoms and well-known symbols that are owned by
// a parent JSRuntime.
if (kind == JS::TraceKind::String && static_cast<JSString*>(*thingp)->isPermanentAtom())
if (thing.isString() && thing.toString()->isPermanentAtom())
return;
if (kind == JS::TraceKind::Symbol && static_cast<JS::Symbol*>(*thingp)->isWellKnownSymbol())
if (thing.isSymbol() && thing.toSymbol()->isWellKnownSymbol())
return;
char16_t* name16 = nullptr;

View File

@ -1,420 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
#include "ChromeUtils.h"
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/gzip_stream.h>
#include "mozilla/devtools/AutoMemMap.h"
#include "mozilla/devtools/HeapSnapshot.h"
#include "mozilla/devtools/ZeroCopyNSIOutputStream.h"
#include "mozilla/Attributes.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/UniquePtr.h"
#include "nsCRTGlue.h"
#include "nsIOutputStream.h"
#include "nsNetUtil.h"
#include "prerror.h"
#include "prio.h"
#include "prtypes.h"
#include "js/Debug.h"
#include "js/TypeDecls.h"
#include "js/UbiNodeTraverse.h"
namespace mozilla {
namespace devtools {
using namespace JS;
using namespace dom;
// If we are only taking a snapshot of the heap affected by the given set of
// globals, find the set of zones the globals are allocated within. Returns
// false on OOM failure.
static bool
PopulateZonesWithGlobals(ZoneSet& zones, AutoObjectVector& globals)
{
if (!zones.init())
return false;
unsigned length = globals.length();
for (unsigned i = 0; i < length; i++) {
if (!zones.put(GetObjectZone(globals[i])))
return false;
}
return true;
}
// Add the given set of globals as explicit roots in the given roots
// list. Returns false on OOM failure.
static bool
AddGlobalsAsRoots(AutoObjectVector& globals, ubi::RootList& roots)
{
unsigned length = globals.length();
for (unsigned i = 0; i < length; i++) {
if (!roots.addRoot(ubi::Node(globals[i].get()),
MOZ_UTF16("heap snapshot global")))
{
return false;
}
}
return true;
}
// Choose roots and limits for a traversal, given `boundaries`. Set `roots` to
// the set of nodes within the boundaries that are referred to by nodes
// outside. If `boundaries` does not include all JS zones, initialize `zones` to
// the set of included zones; otherwise, leave `zones` uninitialized. (You can
// use zones.initialized() to check.)
//
// If `boundaries` is incoherent, or we encounter an error while trying to
// handle it, or we run out of memory, set `rv` appropriately and return
// `false`.
static bool
EstablishBoundaries(JSContext* cx,
ErrorResult& rv,
const HeapSnapshotBoundaries& boundaries,
ubi::RootList& roots,
ZoneSet& zones)
{
MOZ_ASSERT(!roots.initialized());
MOZ_ASSERT(!zones.initialized());
bool foundBoundaryProperty = false;
if (boundaries.mRuntime.WasPassed()) {
foundBoundaryProperty = true;
if (!boundaries.mRuntime.Value()) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
if (!roots.init()) {
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (boundaries.mDebugger.WasPassed()) {
if (foundBoundaryProperty) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
foundBoundaryProperty = true;
JSObject* dbgObj = boundaries.mDebugger.Value();
if (!dbgObj || !dbg::IsDebugger(*dbgObj)) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
AutoObjectVector globals(cx);
if (!dbg::GetDebuggeeGlobals(cx, *dbgObj, globals) ||
!PopulateZonesWithGlobals(zones, globals) ||
!roots.init(zones) ||
!AddGlobalsAsRoots(globals, roots))
{
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (boundaries.mGlobals.WasPassed()) {
if (foundBoundaryProperty) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
foundBoundaryProperty = true;
uint32_t length = boundaries.mGlobals.Value().Length();
if (length == 0) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
AutoObjectVector globals(cx);
for (uint32_t i = 0; i < length; i++) {
JSObject* global = boundaries.mGlobals.Value().ElementAt(i);
if (!JS_IsGlobalObject(global)) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
if (!globals.append(global)) {
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (!PopulateZonesWithGlobals(zones, globals) ||
!roots.init(zones) ||
!AddGlobalsAsRoots(globals, roots))
{
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (!foundBoundaryProperty) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
MOZ_ASSERT(roots.initialized());
MOZ_ASSERT_IF(boundaries.mDebugger.WasPassed(), zones.initialized());
MOZ_ASSERT_IF(boundaries.mGlobals.WasPassed(), zones.initialized());
return true;
}
// A `CoreDumpWriter` that serializes nodes to protobufs and writes them to the
// given `ZeroCopyOutputStream`.
class MOZ_STACK_CLASS StreamWriter : public CoreDumpWriter
{
JSContext* cx;
bool wantNames;
::google::protobuf::io::ZeroCopyOutputStream& stream;
bool writeMessage(const ::google::protobuf::MessageLite& message) {
// We have to create a new CodedOutputStream when writing each message so
// that the 64MB size limit used by Coded{Output,Input}Stream to prevent
// integer overflow is enforced per message rather than on the whole stream.
::google::protobuf::io::CodedOutputStream codedStream(&stream);
codedStream.WriteVarint32(message.ByteSize());
message.SerializeWithCachedSizes(&codedStream);
return !codedStream.HadError();
}
public:
StreamWriter(JSContext* cx,
::google::protobuf::io::ZeroCopyOutputStream& stream,
bool wantNames)
: cx(cx)
, wantNames(wantNames)
, stream(stream)
{ }
~StreamWriter() override { }
virtual bool writeMetadata(uint64_t timestamp) override {
protobuf::Metadata metadata;
metadata.set_timestamp(timestamp);
return writeMessage(metadata);
}
virtual bool writeNode(const JS::ubi::Node& ubiNode,
EdgePolicy includeEdges) override {
protobuf::Node protobufNode;
protobufNode.set_id(ubiNode.identifier());
const char16_t* typeName = ubiNode.typeName();
size_t length = NS_strlen(typeName) * sizeof(char16_t);
protobufNode.set_typename_(typeName, length);
JSRuntime* rt = JS_GetRuntime(cx);
mozilla::MallocSizeOf mallocSizeOf = dbg::GetDebuggerMallocSizeOf(rt);
MOZ_ASSERT(mallocSizeOf);
protobufNode.set_size(ubiNode.size(mallocSizeOf));
if (includeEdges) {
auto edges = ubiNode.edges(cx, wantNames);
if (NS_WARN_IF(!edges))
return false;
for ( ; !edges->empty(); edges->popFront()) {
const ubi::Edge& ubiEdge = edges->front();
protobuf::Edge* protobufEdge = protobufNode.add_edges();
if (NS_WARN_IF(!protobufEdge)) {
return false;
}
protobufEdge->set_referent(ubiEdge.referent.identifier());
if (wantNames && ubiEdge.name) {
size_t length = NS_strlen(ubiEdge.name) * sizeof(char16_t);
protobufEdge->set_name(ubiEdge.name, length);
}
}
}
return writeMessage(protobufNode);
}
};
// A JS::ubi::BreadthFirst handler that serializes a snapshot of the heap into a
// core dump.
class MOZ_STACK_CLASS HeapSnapshotHandler {
CoreDumpWriter& writer;
JS::ZoneSet* zones;
public:
HeapSnapshotHandler(CoreDumpWriter& writer,
JS::ZoneSet* zones)
: writer(writer),
zones(zones)
{ }
// JS::ubi::BreadthFirst handler interface.
class NodeData { };
typedef JS::ubi::BreadthFirst<HeapSnapshotHandler> Traversal;
bool operator() (Traversal& traversal,
JS::ubi::Node origin,
const JS::ubi::Edge& edge,
NodeData*,
bool first)
{
// We're only interested in the first time we reach edge.referent, not in
// every edge arriving at that node. "But, don't we want to serialize every
// edge in the heap graph?" you ask. Don't worry! This edge is still
// serialized into the core dump. Serializing a node also serializes each of
// its edges, and if we are traversing a given edge, we must have already
// visited and serialized the origin node and its edges.
if (!first)
return true;
const JS::ubi::Node& referent = edge.referent;
if (!zones)
// We aren't targeting a particular set of zones, so serialize all the
// things!
return writer.writeNode(referent, CoreDumpWriter::INCLUDE_EDGES);
// We are targeting a particular set of zones. If this node is in our target
// set, serialize it and all of its edges. If this node is _not_ in our
// target set, we also serialize under the assumption that it is a shared
// resource being used by something in our target zones since we reached it
// by traversing the heap graph. However, we do not serialize its outgoing
// edges and we abandon further traversal from this node.
JS::Zone* zone = referent.zone();
if (zones->has(zone))
return writer.writeNode(referent, CoreDumpWriter::INCLUDE_EDGES);
traversal.abandonReferent();
return writer.writeNode(referent, CoreDumpWriter::EXCLUDE_EDGES);
}
};
bool
WriteHeapGraph(JSContext* cx,
const JS::ubi::Node& node,
CoreDumpWriter& writer,
bool wantNames,
JS::ZoneSet* zones,
JS::AutoCheckCannotGC& noGC)
{
// Serialize the starting node to the core dump.
if (NS_WARN_IF(!writer.writeNode(node, CoreDumpWriter::INCLUDE_EDGES))) {
return false;
}
// Walk the heap graph starting from the given node and serialize it into the
// core dump.
HeapSnapshotHandler handler(writer, zones);
HeapSnapshotHandler::Traversal traversal(cx, handler, noGC);
if (!traversal.init())
return false;
traversal.wantNames = wantNames;
return traversal.addStartVisited(node) &&
traversal.traverse();
}
/* static */ void
ChromeUtils::SaveHeapSnapshot(GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
const HeapSnapshotBoundaries& boundaries,
ErrorResult& rv)
{
bool wantNames = true;
ZoneSet zones;
Maybe<AutoCheckCannotGC> maybeNoGC;
ubi::RootList rootList(cx, maybeNoGC, wantNames);
if (!EstablishBoundaries(cx, rv, boundaries, rootList, zones))
return;
MOZ_ASSERT(maybeNoGC.isSome());
ubi::Node roots(&rootList);
nsCOMPtr<nsIFile> file;
rv = NS_NewLocalFile(filePath, false, getter_AddRefs(file));
if (NS_WARN_IF(rv.Failed()))
return;
nsCOMPtr<nsIOutputStream> outputStream;
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), file,
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
-1, 0);
if (NS_WARN_IF(rv.Failed()))
return;
ZeroCopyNSIOutputStream zeroCopyStream(outputStream);
::google::protobuf::io::GzipOutputStream gzipStream(&zeroCopyStream);
StreamWriter writer(cx, gzipStream, wantNames);
// Serialize the initial heap snapshot metadata to the core dump.
if (!writer.writeMetadata(PR_Now()) ||
// Serialize the heap graph to the core dump, starting from our list of
// roots.
!WriteHeapGraph(cx,
roots,
writer,
wantNames,
zones.initialized() ? &zones : nullptr,
maybeNoGC.ref()))
{
rv.Throw(zeroCopyStream.failed()
? zeroCopyStream.result()
: NS_ERROR_UNEXPECTED);
return;
}
}
/* static */ already_AddRefed<HeapSnapshot>
ChromeUtils::ReadHeapSnapshot(GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
ErrorResult& rv)
{
UniquePtr<char[]> path(ToNewCString(filePath));
if (!path) {
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
AutoMemMap mm;
rv = mm.init(path.get());
if (rv.Failed())
return nullptr;
return HeapSnapshot::Create(cx, global,
reinterpret_cast<const uint8_t*>(mm.address()),
mm.size(), rv);
}
/* static */ void
ChromeUtils::OriginAttributesToCookieJar(GlobalObject& aGlobal,
const OriginAttributesDictionary& aAttrs,
nsCString& aCookieJar)
{
OriginAttributes attrs(aAttrs);
attrs.CookieJar(aCookieJar);
}
} // namespace devtools
} // namespace mozilla

View File

@ -1,86 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
#ifndef mozilla_devtools_ChromeUtils__
#define mozilla_devtools_ChromeUtils__
#include "CoreDump.pb.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/UbiNode.h"
#include "js/UbiNodeTraverse.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ChromeUtilsBinding.h"
namespace mozilla {
namespace devtools {
// A `CoreDumpWriter` is given the data we wish to save in a core dump and
// serializes it to disk, or memory, or a socket, etc.
class CoreDumpWriter
{
public:
virtual ~CoreDumpWriter() { };
// Write the given bits of metadata we would like to associate with this core
// dump.
virtual bool writeMetadata(uint64_t timestamp) = 0;
enum EdgePolicy : bool {
INCLUDE_EDGES = true,
EXCLUDE_EDGES = false
};
// Write the given `JS::ubi::Node` to the core dump. The given `EdgePolicy`
// dictates whether its outgoing edges should also be written to the core
// dump, or excluded.
virtual bool writeNode(const JS::ubi::Node& node,
EdgePolicy includeEdges) = 0;
};
// Serialize the heap graph as seen from `node` with the given
// `CoreDumpWriter`. If `wantNames` is true, capture edge names. If `zones` is
// non-null, only capture the sub-graph within the zone set, otherwise capture
// the whole heap graph. Returns false on failure.
bool
WriteHeapGraph(JSContext* cx,
const JS::ubi::Node& node,
CoreDumpWriter& writer,
bool wantNames,
JS::ZoneSet* zones,
JS::AutoCheckCannotGC& noGC);
class HeapSnapshot;
class ChromeUtils
{
public:
static void SaveHeapSnapshot(dom::GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
const dom::HeapSnapshotBoundaries& boundaries,
ErrorResult& rv);
static already_AddRefed<HeapSnapshot> ReadHeapSnapshot(dom::GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
ErrorResult& rv);
static void
OriginAttributesToCookieJar(dom::GlobalObject& aGlobal,
const dom::OriginAttributesDictionary& aAttrs,
nsCString& aCookieJar);
};
} // namespace devtools
} // namespace mozilla
#endif // mozilla_devtools_ChromeUtils__

View File

@ -9,17 +9,34 @@
#include <google/protobuf/io/gzip_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include "js/Debug.h"
#include "js/TypeDecls.h"
#include "js/UbiNodeTraverse.h"
#include "mozilla/Attributes.h"
#include "mozilla/devtools/AutoMemMap.h"
#include "mozilla/devtools/CoreDump.pb.h"
#include "mozilla/devtools/DeserializedNode.h"
#include "mozilla/devtools/ZeroCopyNSIOutputStream.h"
#include "mozilla/dom/ChromeUtils.h"
#include "mozilla/dom/HeapSnapshotBinding.h"
#include "mozilla/UniquePtr.h"
#include "CoreDump.pb.h"
#include "jsapi.h"
#include "nsCycleCollectionParticipant.h"
#include "nsCRTGlue.h"
#include "nsIOutputStream.h"
#include "nsISupportsImpl.h"
#include "nsNetUtil.h"
#include "prerror.h"
#include "prio.h"
#include "prtypes.h"
namespace mozilla {
namespace devtools {
using namespace JS;
using namespace dom;
using ::google::protobuf::io::ArrayInputStream;
using ::google::protobuf::io::CodedInputStream;
using ::google::protobuf::io::GzipInputStream;
@ -47,14 +64,14 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(HeapSnapshot)
NS_INTERFACE_MAP_END
/* virtual */ JSObject*
HeapSnapshot::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
HeapSnapshot::WrapObject(JSContext* aCx, HandleObject aGivenProto)
{
return dom::HeapSnapshotBinding::Wrap(aCx, this, aGivenProto);
return HeapSnapshotBinding::Wrap(aCx, this, aGivenProto);
}
/* static */ already_AddRefed<HeapSnapshot>
HeapSnapshot::Create(JSContext* cx,
dom::GlobalObject& global,
GlobalObject& global,
const uint8_t* buffer,
uint32_t size,
ErrorResult& rv)
@ -217,6 +234,387 @@ HeapSnapshot::borrowUniqueString(const char16_t* duplicateString, size_t length)
return ptr->get();
}
// If we are only taking a snapshot of the heap affected by the given set of
// globals, find the set of zones the globals are allocated within. Returns
// false on OOM failure.
static bool
PopulateZonesWithGlobals(ZoneSet& zones, AutoObjectVector& globals)
{
if (!zones.init())
return false;
unsigned length = globals.length();
for (unsigned i = 0; i < length; i++) {
if (!zones.put(GetObjectZone(globals[i])))
return false;
}
return true;
}
// Add the given set of globals as explicit roots in the given roots
// list. Returns false on OOM failure.
static bool
AddGlobalsAsRoots(AutoObjectVector& globals, ubi::RootList& roots)
{
unsigned length = globals.length();
for (unsigned i = 0; i < length; i++) {
if (!roots.addRoot(ubi::Node(globals[i].get()),
MOZ_UTF16("heap snapshot global")))
{
return false;
}
}
return true;
}
// Choose roots and limits for a traversal, given `boundaries`. Set `roots` to
// the set of nodes within the boundaries that are referred to by nodes
// outside. If `boundaries` does not include all JS zones, initialize `zones` to
// the set of included zones; otherwise, leave `zones` uninitialized. (You can
// use zones.initialized() to check.)
//
// If `boundaries` is incoherent, or we encounter an error while trying to
// handle it, or we run out of memory, set `rv` appropriately and return
// `false`.
static bool
EstablishBoundaries(JSContext* cx,
ErrorResult& rv,
const HeapSnapshotBoundaries& boundaries,
ubi::RootList& roots,
ZoneSet& zones)
{
MOZ_ASSERT(!roots.initialized());
MOZ_ASSERT(!zones.initialized());
bool foundBoundaryProperty = false;
if (boundaries.mRuntime.WasPassed()) {
foundBoundaryProperty = true;
if (!boundaries.mRuntime.Value()) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
if (!roots.init()) {
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (boundaries.mDebugger.WasPassed()) {
if (foundBoundaryProperty) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
foundBoundaryProperty = true;
JSObject* dbgObj = boundaries.mDebugger.Value();
if (!dbgObj || !dbg::IsDebugger(*dbgObj)) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
AutoObjectVector globals(cx);
if (!dbg::GetDebuggeeGlobals(cx, *dbgObj, globals) ||
!PopulateZonesWithGlobals(zones, globals) ||
!roots.init(zones) ||
!AddGlobalsAsRoots(globals, roots))
{
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (boundaries.mGlobals.WasPassed()) {
if (foundBoundaryProperty) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
foundBoundaryProperty = true;
uint32_t length = boundaries.mGlobals.Value().Length();
if (length == 0) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
AutoObjectVector globals(cx);
for (uint32_t i = 0; i < length; i++) {
JSObject* global = boundaries.mGlobals.Value().ElementAt(i);
if (!JS_IsGlobalObject(global)) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
if (!globals.append(global)) {
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (!PopulateZonesWithGlobals(zones, globals) ||
!roots.init(zones) ||
!AddGlobalsAsRoots(globals, roots))
{
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
}
if (!foundBoundaryProperty) {
rv.Throw(NS_ERROR_INVALID_ARG);
return false;
}
MOZ_ASSERT(roots.initialized());
MOZ_ASSERT_IF(boundaries.mDebugger.WasPassed(), zones.initialized());
MOZ_ASSERT_IF(boundaries.mGlobals.WasPassed(), zones.initialized());
return true;
}
// A `CoreDumpWriter` that serializes nodes to protobufs and writes them to the
// given `ZeroCopyOutputStream`.
class MOZ_STACK_CLASS StreamWriter : public CoreDumpWriter
{
JSContext* cx;
bool wantNames;
::google::protobuf::io::ZeroCopyOutputStream& stream;
bool writeMessage(const ::google::protobuf::MessageLite& message) {
// We have to create a new CodedOutputStream when writing each message so
// that the 64MB size limit used by Coded{Output,Input}Stream to prevent
// integer overflow is enforced per message rather than on the whole stream.
::google::protobuf::io::CodedOutputStream codedStream(&stream);
codedStream.WriteVarint32(message.ByteSize());
message.SerializeWithCachedSizes(&codedStream);
return !codedStream.HadError();
}
public:
StreamWriter(JSContext* cx,
::google::protobuf::io::ZeroCopyOutputStream& stream,
bool wantNames)
: cx(cx)
, wantNames(wantNames)
, stream(stream)
{ }
~StreamWriter() override { }
virtual bool writeMetadata(uint64_t timestamp) override {
protobuf::Metadata metadata;
metadata.set_timestamp(timestamp);
return writeMessage(metadata);
}
virtual bool writeNode(const JS::ubi::Node& ubiNode,
EdgePolicy includeEdges) override {
protobuf::Node protobufNode;
protobufNode.set_id(ubiNode.identifier());
const char16_t* typeName = ubiNode.typeName();
size_t length = NS_strlen(typeName) * sizeof(char16_t);
protobufNode.set_typename_(typeName, length);
JSRuntime* rt = JS_GetRuntime(cx);
mozilla::MallocSizeOf mallocSizeOf = dbg::GetDebuggerMallocSizeOf(rt);
MOZ_ASSERT(mallocSizeOf);
protobufNode.set_size(ubiNode.size(mallocSizeOf));
if (includeEdges) {
auto edges = ubiNode.edges(cx, wantNames);
if (NS_WARN_IF(!edges))
return false;
for ( ; !edges->empty(); edges->popFront()) {
const ubi::Edge& ubiEdge = edges->front();
protobuf::Edge* protobufEdge = protobufNode.add_edges();
if (NS_WARN_IF(!protobufEdge)) {
return false;
}
protobufEdge->set_referent(ubiEdge.referent.identifier());
if (wantNames && ubiEdge.name) {
size_t length = NS_strlen(ubiEdge.name) * sizeof(char16_t);
protobufEdge->set_name(ubiEdge.name, length);
}
}
}
return writeMessage(protobufNode);
}
};
// A JS::ubi::BreadthFirst handler that serializes a snapshot of the heap into a
// core dump.
class MOZ_STACK_CLASS HeapSnapshotHandler
{
CoreDumpWriter& writer;
JS::ZoneSet* zones;
public:
HeapSnapshotHandler(CoreDumpWriter& writer,
JS::ZoneSet* zones)
: writer(writer),
zones(zones)
{ }
// JS::ubi::BreadthFirst handler interface.
class NodeData { };
typedef JS::ubi::BreadthFirst<HeapSnapshotHandler> Traversal;
bool operator() (Traversal& traversal,
JS::ubi::Node origin,
const JS::ubi::Edge& edge,
NodeData*,
bool first)
{
// We're only interested in the first time we reach edge.referent, not in
// every edge arriving at that node. "But, don't we want to serialize every
// edge in the heap graph?" you ask. Don't worry! This edge is still
// serialized into the core dump. Serializing a node also serializes each of
// its edges, and if we are traversing a given edge, we must have already
// visited and serialized the origin node and its edges.
if (!first)
return true;
const JS::ubi::Node& referent = edge.referent;
if (!zones)
// We aren't targeting a particular set of zones, so serialize all the
// things!
return writer.writeNode(referent, CoreDumpWriter::INCLUDE_EDGES);
// We are targeting a particular set of zones. If this node is in our target
// set, serialize it and all of its edges. If this node is _not_ in our
// target set, we also serialize under the assumption that it is a shared
// resource being used by something in our target zones since we reached it
// by traversing the heap graph. However, we do not serialize its outgoing
// edges and we abandon further traversal from this node.
JS::Zone* zone = referent.zone();
if (zones->has(zone))
return writer.writeNode(referent, CoreDumpWriter::INCLUDE_EDGES);
traversal.abandonReferent();
return writer.writeNode(referent, CoreDumpWriter::EXCLUDE_EDGES);
}
};
bool
WriteHeapGraph(JSContext* cx,
const JS::ubi::Node& node,
CoreDumpWriter& writer,
bool wantNames,
JS::ZoneSet* zones,
JS::AutoCheckCannotGC& noGC)
{
// Serialize the starting node to the core dump.
if (NS_WARN_IF(!writer.writeNode(node, CoreDumpWriter::INCLUDE_EDGES))) {
return false;
}
// Walk the heap graph starting from the given node and serialize it into the
// core dump.
HeapSnapshotHandler handler(writer, zones);
HeapSnapshotHandler::Traversal traversal(cx, handler, noGC);
if (!traversal.init())
return false;
traversal.wantNames = wantNames;
return traversal.addStartVisited(node) &&
traversal.traverse();
}
} // namespace devtools
namespace dom {
using namespace JS;
using namespace devtools;
/* static */ void
ThreadSafeChromeUtils::SaveHeapSnapshot(GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
const HeapSnapshotBoundaries& boundaries,
ErrorResult& rv)
{
bool wantNames = true;
ZoneSet zones;
Maybe<AutoCheckCannotGC> maybeNoGC;
ubi::RootList rootList(cx, maybeNoGC, wantNames);
if (!EstablishBoundaries(cx, rv, boundaries, rootList, zones))
return;
MOZ_ASSERT(maybeNoGC.isSome());
ubi::Node roots(&rootList);
nsCOMPtr<nsIFile> file;
rv = NS_NewLocalFile(filePath, false, getter_AddRefs(file));
if (NS_WARN_IF(rv.Failed()))
return;
nsCOMPtr<nsIOutputStream> outputStream;
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), file,
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
-1, 0);
if (NS_WARN_IF(rv.Failed()))
return;
ZeroCopyNSIOutputStream zeroCopyStream(outputStream);
::google::protobuf::io::GzipOutputStream gzipStream(&zeroCopyStream);
StreamWriter writer(cx, gzipStream, wantNames);
// Serialize the initial heap snapshot metadata to the core dump.
if (!writer.writeMetadata(PR_Now()) ||
// Serialize the heap graph to the core dump, starting from our list of
// roots.
!WriteHeapGraph(cx,
roots,
writer,
wantNames,
zones.initialized() ? &zones : nullptr,
maybeNoGC.ref()))
{
rv.Throw(zeroCopyStream.failed()
? zeroCopyStream.result()
: NS_ERROR_UNEXPECTED);
return;
}
}
/* static */ already_AddRefed<HeapSnapshot>
ThreadSafeChromeUtils::ReadHeapSnapshot(GlobalObject& global,
JSContext* cx,
const nsAString& filePath,
ErrorResult& rv)
{
UniquePtr<char[]> path(ToNewCString(filePath));
if (!path) {
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
AutoMemMap mm;
rv = mm.init(path.get());
if (rv.Failed())
return nullptr;
return HeapSnapshot::Create(cx, global,
reinterpret_cast<const uint8_t*>(mm.address()),
mm.size(), rv);
}
} // namespace dom
} // namespace mozilla

View File

@ -129,6 +129,41 @@ public:
size_t length);
};
// A `CoreDumpWriter` is given the data we wish to save in a core dump and
// serializes it to disk, or memory, or a socket, etc.
class CoreDumpWriter
{
public:
virtual ~CoreDumpWriter() { };
// Write the given bits of metadata we would like to associate with this core
// dump.
virtual bool writeMetadata(uint64_t timestamp) = 0;
enum EdgePolicy : bool {
INCLUDE_EDGES = true,
EXCLUDE_EDGES = false
};
// Write the given `JS::ubi::Node` to the core dump. The given `EdgePolicy`
// dictates whether its outgoing edges should also be written to the core
// dump, or excluded.
virtual bool writeNode(const JS::ubi::Node& node,
EdgePolicy includeEdges) = 0;
};
// Serialize the heap graph as seen from `node` with the given `CoreDumpWriter`.
// If `wantNames` is true, capture edge names. If `zones` is non-null, only
// capture the sub-graph within the zone set, otherwise capture the whole heap
// graph. Returns false on failure.
bool
WriteHeapGraph(JSContext* cx,
const JS::ubi::Node& node,
CoreDumpWriter& writer,
bool wantNames,
JS::ZoneSet* zones,
JS::AutoCheckCannotGC& noGC);
} // namespace devtools
} // namespace mozilla

View File

@ -19,7 +19,6 @@ XPIDL_MODULE = 'jsinspector'
EXPORTS.mozilla.devtools += [
'AutoMemMap.h',
'ChromeUtils.h',
'CoreDump.pb.h',
'DeserializedNode.h',
'HeapSnapshot.h',
@ -28,7 +27,6 @@ EXPORTS.mozilla.devtools += [
SOURCES += [
'AutoMemMap.cpp',
'ChromeUtils.cpp',
'CoreDump.pb.cc',
'DeserializedNode.cpp',
'HeapSnapshot.cpp',

View File

@ -13,7 +13,8 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "mozilla/devtools/ChromeUtils.h"
#include "mozilla/devtools/HeapSnapshot.h"
#include "mozilla/dom/ChromeUtils.h"
#include "mozilla/CycleCollectedJSRuntime.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
@ -21,6 +22,7 @@
using namespace mozilla;
using namespace mozilla::devtools;
using namespace mozilla::dom;
using namespace testing;
// GTest fixture class that all of our tests derive from.

View File

@ -8,13 +8,17 @@ self.onmessage = e => {
try {
const { filePath } = e.data;
ok(ChromeUtils, "Should have access to ChromeUtils");
ok(HeapSnapshot, "Should have access to HeapSnapshot");
ok(typeof ChromeUtils === "undefined",
"Should not have access to ChromeUtils in a worker.");
ok(ThreadSafeChromeUtils,
"Should have access to ThreadSafeChromeUtils in a worker.");
ok(HeapSnapshot,
"Should have access to HeapSnapshot in a worker.");
ChromeUtils.saveHeapSnapshot(filePath, { globals: [this] });
ThreadSafeChromeUtils.saveHeapSnapshot(filePath, { globals: [this] });
ok(true, "Should be able to save a snapshot.");
const snapshot = ChromeUtils.readHeapSnapshot(filePath);
const snapshot = ThreadSafeChromeUtils.readHeapSnapshot(filePath);
ok(snapshot, "Should be able to read a heap snapshot");
ok(snapshot instanceof HeapSnapshot, "Should be an instanceof HeapSnapshot");
} catch (e) {