Files
palemoon27/toolkit/devtools/server/DeserializedNode.h
T
roytam1 f7679e4701 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1219066 - Make sure to traverse and unlink HeapSnapshot::mParent in cycle collection; r=mccr8 (a25aab429d)
- Bug 1220918 - Serialize and deseriliaze JS::ubi::Node's script filenames in heap snapshots; r=sfink (7bd216ff4d)
- Bug 1219073 - part 1 - Add to sdk/url#URL objects. r=gozala (535e8fa870)
- Bug 1205733 - Add move overloads to dom::Nullable's constructor and SetValue method, r=bz (44a2684efe)
- Bug 1151884 - Enable the uint32_t overload of ToJSValue; r=smaug (989d3e5b5f)
- obvious fix (76ba7249fb)
- Bug 1225219 Implement ErrorResult::CloneTo(). r=bz (1a05be13c3)
- Bug 1219749. Add a way to faithfully propagate the "exception is already on JSContext" state through an ErrorResult. r=peterv (cb1713a7b9)
- Bug 1204501 - Update the documentation for DOMJSClass::mParticipant; r=peterv (2c0b22cfed)
- Bug 979591. Disallow calling WebIDL constructors as functions even for system callers in release builds. r=peterv (7ad3312248)
- Bug 1180921 - Give Optional<T> Maybe<T>-like operator== semantics. r=bz (aadc8d552d)
- Bug 1188207 - Fix more constructors in DOM; r=baku (4609640af9)
- Bug 1191918 - Round battery level to nearest 10% r=bz (0c98c214b1)
- Bug 1221009. Part 1 - add a class to forward notifications from MediaResource to MediaDecoder. r=roc. (90ca84d0f9)
- Bug 1221009. Part 2 - remove unused code. r=roc. (789b0a0e74)
- Bug 1221009. Part 3 - add assertions to functions that shouldn't be called after shutdown. r=roc. (d292c1701f)
- Bug 1217653 - MediaDecoder::GetOwner() should return null after shutdown. r=kinetik. (f071ecf2ee)
- Bug 1219142. Part 1 - add AbstractMediaDecoder::DataArrivedEvent() to publish events. r=jya. (15e67bbd3e)
- Bug 1219142. Part 2 - remove unused code. r=jya. (e2be34e25a)
- bug 681602 - Implement xptcall for arm iOS. r=glandium (3be41176bc)
- Bug 1188209 - Fix more constructors in memory; r=njn (28b833e741)
- Bug 1222171 - Re-establish equivalence between gfxImageFormat and cairo_format_t. r=mstange. (6e50fcea80)
- Bug 1215898 - Fix clang's -Wimplicit-fallthrough warnings in gfx/thebes. r=jdaggett r=jmuizelaar (db0f7ec46c)
- Bug 598900 - GDI: use typo metrics when USE_TYPO_METRICS is specified. r=karlt (0fb2af92ce)
- Bug 964512 - Check for existence of character before trying to get its metrics in gfxGDIFont::Initialize. r=jdaggett (bc88ee4252)
- Bug 691581 - Don't let a zero-sized font result in assertions from FUnitsToDevUnitsFactor(). r=jdaggett (3408c67dbf)
- clean spaces (ffdccafdea)
- Bug 1192666 - Emit '[]' around origin strings for ipv6 origins, r=ehsan (cc5fcdb711)
- Bug 1195415 - Add asciiHostPort field to nsIURI, and use it in the implementation of nsPrincipal::GetOriginForURI, r=bholley (7793745ecb)
- Bug 1204610 - Use a smart pointer in nsNullPrincipalURI. r=mrbkap (082fedf3e7)
- Bug 859764 - Part 1.1: Turn IDL Implementation into Internal-Only Interface. r=echen, r=smaug (900ae90da3)
- Bug 1043250 - Part 2: Update MobileMessageCallback and SmsService. r=btseng (fc2a0ed029)
- Bug 1175430 - Expose Network-Specific Error Cause for Various Error Handling in App Layer. r=btseng (c3abacd9e1)
- Bug 859764 - Part 1.2: Clearn Up Naming in IDL. r=echen (41f70a1f4c)
- Bug 1152730 - Part 3: Add owner window checks on DOM object operations. r=btseng (2c09378b02)
- Bug 1043250 - Part 3: Update MozMobileMessageManager WebIDL interface and implementation. r=hsinyi (06feae677b)
- Bug 859764 - Part 2: Define New WebIDL interfaces for MobileMessage Objects. r=echen r=smaug (54cb39df82)
- Bug 859764 - Part 3: The Implementation for WebIDL Change. r=echen, r=smaug (be0d0439dc)
- Bug 984413 - Add JSdoc in MobileMessageDB.jsm. r=btseng (a3f15e291a)
- Bug 1154186 - Deprecate nsISmsMessenger_new.idl. r=echen (b179f3343c)
- Bug 1152730 - Part 1: Update retry logic in SmsService and remove the retry in ril_worker. r=btseng (d8e5b520f2)
- Bug 1197010 - Implement Android backend for createMessageCursor/createThreadCursor. r=snorp (9e4506b4a2)
- Bug 748391 - Implement markMessageRead on the Android backend. r=snorp (d969455588)
- Bug 859764 - Part 4: Implementation Change in Different Backend. r=echen (607b9bb53f)
- Bug 1043250 - Part 4: Update SMS IPC implementation. r=btseng (264cd87721)
- Bug 1197008 - Stop assuming 0 is an invalid threadId. r=btseng r=hsinyi (5498728784)
2023-01-13 13:25:15 +08:00

319 lines
10 KiB
C++

/* -*- 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_DeserializedNode__
#define mozilla_devtools_DeserializedNode__
#include "js/UbiNode.h"
#include "mozilla/devtools/CoreDump.pb.h"
#include "mozilla/Maybe.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"
// `Deserialized{Node,Edge}` translate protobuf messages from our core dump
// format into structures we can rely upon for implementing `JS::ubi::Node`
// specializations on top of. All of the properties of the protobuf messages are
// optional for future compatibility, and this is the layer where we validate
// that the properties that do actually exist in any given message fulfill our
// semantic requirements.
//
// Both `DeserializedNode` and `DeserializedEdge` are always owned by a
// `HeapSnapshot` instance, and their lifetimes must not extend after that of
// their owning `HeapSnapshot`.
namespace mozilla {
namespace devtools {
class HeapSnapshot;
using NodeId = uint64_t;
using StackFrameId = uint64_t;
// A `DeserializedEdge` represents an edge in the heap graph pointing to the
// node with id equal to `DeserializedEdge::referent` that we deserialized from
// a core dump.
struct DeserializedEdge {
NodeId referent;
// A borrowed reference to a string owned by this node's owning HeapSnapshot.
const char16_t* name;
explicit DeserializedEdge(NodeId referent, const char16_t* edgeName = nullptr)
: referent(referent)
, name(edgeName)
{ }
DeserializedEdge(DeserializedEdge&& rhs);
DeserializedEdge& operator=(DeserializedEdge&& rhs);
private:
DeserializedEdge(const DeserializedEdge&) = delete;
DeserializedEdge& operator=(const DeserializedEdge&) = delete;
};
// A `DeserializedNode` is a node in the heap graph that we deserialized from a
// core dump.
struct DeserializedNode {
using EdgeVector = Vector<DeserializedEdge>;
using UniqueStringPtr = UniquePtr<char16_t[]>;
NodeId id;
JS::ubi::CoarseType coarseType;
// A borrowed reference to a string owned by this node's owning HeapSnapshot.
const char16_t* typeName;
uint64_t size;
EdgeVector edges;
Maybe<StackFrameId> allocationStack;
// A borrowed reference to a string owned by this node's owning HeapSnapshot.
const char* jsObjectClassName;
// A borrowed reference to a string owned by this node's owning HeapSnapshot.
const char* scriptFilename;
// A weak pointer to this node's owning `HeapSnapshot`. Safe without
// AddRef'ing because this node's lifetime is equal to that of its owner.
HeapSnapshot* owner;
DeserializedNode(NodeId id,
JS::ubi::CoarseType coarseType,
const char16_t* typeName,
uint64_t size,
EdgeVector&& edges,
Maybe<StackFrameId> allocationStack,
const char* className,
const char* filename,
HeapSnapshot& owner)
: id(id)
, coarseType(coarseType)
, typeName(typeName)
, size(size)
, edges(Move(edges))
, allocationStack(allocationStack)
, jsObjectClassName(className)
, scriptFilename(filename)
, owner(&owner)
{ }
virtual ~DeserializedNode() { }
DeserializedNode(DeserializedNode&& rhs)
: id(rhs.id)
, coarseType(rhs.coarseType)
, typeName(rhs.typeName)
, size(rhs.size)
, edges(Move(rhs.edges))
, allocationStack(rhs.allocationStack)
, jsObjectClassName(rhs.jsObjectClassName)
, scriptFilename(rhs.scriptFilename)
, owner(rhs.owner)
{ }
DeserializedNode& operator=(DeserializedNode&& rhs)
{
MOZ_ASSERT(&rhs != this);
this->~DeserializedNode();
new(this) DeserializedNode(Move(rhs));
return *this;
}
// Get a borrowed reference to the given edge's referent. This method is
// virtual to provide a hook for gmock and gtest.
virtual JS::ubi::Node getEdgeReferent(const DeserializedEdge& edge);
struct HashPolicy;
protected:
// This is only for use with `MockDeserializedNode` in testing.
DeserializedNode(NodeId id, const char16_t* typeName, uint64_t size)
: id(id)
, coarseType(JS::ubi::CoarseType::Other)
, typeName(typeName)
, size(size)
, edges()
, allocationStack(Nothing())
, jsObjectClassName(nullptr)
, scriptFilename(nullptr)
, owner(nullptr)
{ }
private:
DeserializedNode(const DeserializedNode&) = delete;
DeserializedNode& operator=(const DeserializedNode&) = delete;
};
static inline js::HashNumber
hashIdDerivedFromPtr(uint64_t id)
{
// NodeIds and StackFrameIds are always 64 bits, but they are derived from
// the original referents' addresses, which could have been either 32 or 64
// bits long. As such, NodeId and StackFrameId have little entropy in their
// bottom three bits, and may or may not have entropy in their upper 32
// bits. This hash should manage both cases well.
id >>= 3;
return js::HashNumber((id >> 32) ^ id);
}
struct DeserializedNode::HashPolicy
{
using Lookup = NodeId;
static js::HashNumber hash(const Lookup& lookup) {
return hashIdDerivedFromPtr(lookup);
}
static bool match(const DeserializedNode& existing, const Lookup& lookup) {
return existing.id == lookup;
}
};
// A `DeserializedStackFrame` is a stack frame referred to by a thing in the
// heap graph that we deserialized from a core dump.
struct DeserializedStackFrame {
StackFrameId id;
Maybe<StackFrameId> parent;
uint32_t line;
uint32_t column;
// Borrowed references to strings owned by this DeserializedStackFrame's
// owning HeapSnapshot.
const char16_t* source;
const char16_t* functionDisplayName;
bool isSystem;
bool isSelfHosted;
// A weak pointer to this frame's owning `HeapSnapshot`. Safe without
// AddRef'ing because this frame's lifetime is equal to that of its owner.
HeapSnapshot* owner;
explicit DeserializedStackFrame(StackFrameId id,
const Maybe<StackFrameId>& parent,
uint32_t line,
uint32_t column,
const char16_t* source,
const char16_t* functionDisplayName,
bool isSystem,
bool isSelfHosted,
HeapSnapshot& owner)
: id(id)
, parent(parent)
, line(line)
, column(column)
, source(source)
, functionDisplayName(functionDisplayName)
, isSystem(isSystem)
, isSelfHosted(isSelfHosted)
, owner(&owner)
{
MOZ_ASSERT(source);
}
JS::ubi::StackFrame getParentStackFrame() const;
struct HashPolicy;
protected:
// This is exposed only for MockDeserializedStackFrame in the gtests.
explicit DeserializedStackFrame()
: id(0)
, parent(Nothing())
, line(0)
, column(0)
, source(nullptr)
, functionDisplayName(nullptr)
, isSystem(false)
, isSelfHosted(false)
, owner(nullptr)
{ };
};
struct DeserializedStackFrame::HashPolicy {
using Lookup = StackFrameId;
static js::HashNumber hash(const Lookup& lookup) {
return hashIdDerivedFromPtr(lookup);
}
static bool match(const DeserializedStackFrame& existing, const Lookup& lookup) {
return existing.id == lookup;
}
};
} // namespace devtools
} // namespace mozilla
namespace JS {
namespace ubi {
using mozilla::devtools::DeserializedNode;
using mozilla::devtools::DeserializedStackFrame;
using mozilla::UniquePtr;
template<>
struct Concrete<DeserializedNode> : public Base
{
protected:
explicit Concrete(DeserializedNode* ptr) : Base(ptr) { }
DeserializedNode& get() const {
return *static_cast<DeserializedNode*>(ptr);
}
public:
static const char16_t concreteTypeName[];
static void construct(void* storage, DeserializedNode* ptr) {
new (storage) Concrete(ptr);
}
CoarseType coarseType() const final { return get().coarseType; }
Id identifier() const override { return get().id; }
bool isLive() const override { return false; }
const char16_t* typeName() const override;
Node::Size size(mozilla::MallocSizeOf mallocSizeof) const override;
const char* jsObjectClassName() const override { return get().jsObjectClassName; }
const char* scriptFilename() const final { return get().scriptFilename; }
bool hasAllocationStack() const override { return get().allocationStack.isSome(); }
StackFrame allocationStack() const override;
// We ignore the `bool wantNames` parameter because we can't control whether
// the core dump was serialized with edge names or not.
UniquePtr<EdgeRange> edges(JSRuntime* rt, bool) const override;
};
template<>
class ConcreteStackFrame<DeserializedStackFrame> : public BaseStackFrame
{
protected:
explicit ConcreteStackFrame(DeserializedStackFrame* ptr)
: BaseStackFrame(ptr)
{ }
DeserializedStackFrame& get() const {
return *static_cast<DeserializedStackFrame*>(ptr);
}
public:
static void construct(void* storage, DeserializedStackFrame* ptr) {
new (storage) ConcreteStackFrame(ptr);
}
uint64_t identifier() const override { return get().id; }
uint32_t line() const override { return get().line; }
uint32_t column() const override { return get().column; }
bool isSystem() const override { return get().isSystem; }
bool isSelfHosted() const override { return get().isSelfHosted; }
void trace(JSTracer* trc) override { }
AtomOrTwoByteChars source() const override {
return AtomOrTwoByteChars(get().source);
}
AtomOrTwoByteChars functionDisplayName() const override {
return AtomOrTwoByteChars(get().functionDisplayName);
}
StackFrame parent() const override;
bool constructSavedFrameStack(JSContext* cx,
MutableHandleObject outSavedFrameStack)
const override;
};
} // namespace ubi
} // namespace JS
#endif // mozilla_devtools_DeserializedNode__