2013-07-03 11:24:32 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sw=4 et tw=80:
|
|
|
|
*
|
|
|
|
* 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_jsipc_JavaScriptShared_h__
|
|
|
|
#define mozilla_jsipc_JavaScriptShared_h__
|
|
|
|
|
2017-07-19 22:29:55 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "mozilla/dom/DOMTypes.h"
|
2015-01-27 00:32:18 +03:00
|
|
|
#include "mozilla/jsipc/CrossProcessObjectWrappers.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "mozilla/jsipc/PJavaScript.h"
|
2015-11-25 22:59:09 +03:00
|
|
|
#include "js/GCHashTable.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace jsipc {
|
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
class ObjectId {
|
|
|
|
public:
|
|
|
|
// Use 47 bits at most, to be safe, since jsval privates are encoded as
|
|
|
|
// doubles. See bug 1065811 comment 12 for an explanation.
|
|
|
|
static const size_t SERIAL_NUMBER_BITS = 47;
|
|
|
|
static const size_t FLAG_BITS = 1;
|
|
|
|
static const uint64_t SERIAL_NUMBER_MAX = (uint64_t(1) << SERIAL_NUMBER_BITS) - 1;
|
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
explicit ObjectId(uint64_t serialNumber, bool hasXrayWaiver)
|
|
|
|
: serialNumber_(serialNumber), hasXrayWaiver_(hasXrayWaiver)
|
2014-09-25 15:13:29 +04:00
|
|
|
{
|
2018-09-12 23:04:14 +03:00
|
|
|
if (isInvalidSerialNumber(serialNumber)) {
|
2014-09-25 15:13:29 +04:00
|
|
|
MOZ_CRASH("Bad CPOW Id");
|
2018-09-12 23:04:14 +03:00
|
|
|
}
|
2014-09-25 15:13:29 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool operator==(const ObjectId& other) const {
|
2014-09-25 15:13:29 +04:00
|
|
|
bool equal = serialNumber() == other.serialNumber();
|
2014-09-25 15:13:29 +04:00
|
|
|
MOZ_ASSERT_IF(equal, hasXrayWaiver() == other.hasXrayWaiver());
|
2014-09-25 15:13:29 +04:00
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNull() { return !serialNumber_; }
|
|
|
|
|
|
|
|
uint64_t serialNumber() const { return serialNumber_; }
|
2014-09-25 15:13:29 +04:00
|
|
|
bool hasXrayWaiver() const { return hasXrayWaiver_; }
|
2014-09-25 15:13:29 +04:00
|
|
|
uint64_t serialize() const {
|
|
|
|
MOZ_ASSERT(serialNumber(), "Don't send a null ObjectId over IPC");
|
2014-09-25 15:13:29 +04:00
|
|
|
return uint64_t((serialNumber() << FLAG_BITS) | ((hasXrayWaiver() ? 1 : 0) << 0));
|
2014-09-25 15:13:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static ObjectId nullId() { return ObjectId(); }
|
2018-05-31 23:29:03 +03:00
|
|
|
static Maybe<ObjectId> deserialize(uint64_t data) {
|
|
|
|
if (isInvalidSerialNumber(data >> FLAG_BITS)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
return Some(ObjectId(data >> FLAG_BITS, data & 1));
|
2014-09-25 15:13:29 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:59:09 +03:00
|
|
|
// For use with StructGCPolicy.
|
|
|
|
void trace(JSTracer*) const {}
|
|
|
|
bool needsSweep() const { return false; }
|
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
private:
|
2014-09-25 15:13:29 +04:00
|
|
|
ObjectId() : serialNumber_(0), hasXrayWaiver_(false) {}
|
2014-09-25 15:13:29 +04:00
|
|
|
|
2018-05-31 23:29:03 +03:00
|
|
|
static bool isInvalidSerialNumber(uint64_t aSerialNumber) {
|
|
|
|
return aSerialNumber == 0 || aSerialNumber > SERIAL_NUMBER_MAX;
|
|
|
|
}
|
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
uint64_t serialNumber_ : SERIAL_NUMBER_BITS;
|
2014-09-25 15:13:29 +04:00
|
|
|
bool hasXrayWaiver_ : 1;
|
2014-09-25 15:13:29 +04:00
|
|
|
};
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2013-07-11 02:05:39 +04:00
|
|
|
class JavaScriptShared;
|
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
// DefaultHasher<T> requires that T coerce to an integral type. We could make
|
|
|
|
// ObjectId do that, but doing so would weaken our type invariants, so we just
|
|
|
|
// reimplement it manually.
|
|
|
|
struct ObjectIdHasher
|
|
|
|
{
|
|
|
|
typedef ObjectId Lookup;
|
2015-03-29 01:22:11 +03:00
|
|
|
static js::HashNumber hash(const Lookup& l) {
|
2017-07-19 22:29:55 +03:00
|
|
|
return mozilla::HashGeneric(l.serialize());
|
2014-09-25 15:13:29 +04:00
|
|
|
}
|
2015-03-29 01:22:11 +03:00
|
|
|
static bool match(const ObjectId& k, const ObjectId& l) {
|
2014-09-25 15:13:29 +04:00
|
|
|
return k == l;
|
|
|
|
}
|
2015-03-29 01:22:11 +03:00
|
|
|
static void rekey(ObjectId& k, const ObjectId& newKey) {
|
2014-09-25 15:13:29 +04:00
|
|
|
k = newKey;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
// Map ids -> JSObjects
|
2014-05-17 03:40:35 +04:00
|
|
|
class IdToObjectMap
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
typedef js::HashMap<ObjectId, JS::Heap<JSObject*>, ObjectIdHasher, js::SystemAllocPolicy> Table;
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
public:
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap();
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2016-10-19 01:58:33 +03:00
|
|
|
void trace(JSTracer* trc, uint64_t minimumId = 0);
|
2014-09-02 13:07:22 +04:00
|
|
|
void sweep();
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool add(ObjectId id, JSObject* obj);
|
|
|
|
JSObject* find(ObjectId id);
|
2016-11-18 16:18:09 +03:00
|
|
|
JSObject* findPreserveColor(ObjectId id);
|
2013-07-03 11:24:32 +04:00
|
|
|
void remove(ObjectId id);
|
|
|
|
|
2015-01-16 20:56:30 +03:00
|
|
|
void clear();
|
|
|
|
bool empty() const;
|
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool has(const ObjectId& id, const JSObject* obj) const;
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
private:
|
2014-05-17 03:40:35 +04:00
|
|
|
Table table_;
|
2013-07-03 11:24:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Map JSObjects -> ids
|
2014-05-17 03:40:35 +04:00
|
|
|
class ObjectToIdMap
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-11-25 22:59:09 +03:00
|
|
|
using Hasher = js::MovableCellHasher<JS::Heap<JSObject*>>;
|
2016-04-26 19:18:48 +03:00
|
|
|
using Table = JS::GCHashMap<JS::Heap<JSObject*>, ObjectId, Hasher, js::SystemAllocPolicy>;
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
public:
|
Bug 1481998 - Make mozilla::Hash{Map,Set}'s entry storage allocation lazy. r=luke,sfink
Entry storage allocation now occurs on the first lookupForAdd()/put()/putNew().
This removes the need for init() and initialized(), and matches how
PLDHashTable/nsTHashtable work. It also removes the need for init() functions
in a lot of types that are built on top of mozilla::Hash{Map,Set}.
Pros:
- No need for init() calls and subsequent checks.
- No memory allocated for empty tables, which are not that uncommon.
Cons:
- An extra branch in lookup() and lookupForAdd(), but not in put()/putNew(),
because the existing checkOverloaded() can handle it.
Specifics:
- Construction now can take a length parameter.
- init() is removed. Explicit length-setting, when necessary, now occurs in the
constructors.
- initialized() is removed.
- capacity() now returns zero when the entry storage is absent.
- lookupForAdd() is no longer `const`, because it can instantiate the storage,
which requires modifications.
- lookupForAdd() can now return an invalid AddPtr in two cases:
- old: hashing failure (due to OOM in the hasher)
- new: OOM while instantiating entry storage
The existing failure handling paths for the old case work for the new case.
- clear(), finish(), and clearAndShrink() are replaced by clear(), compact(),
and reserve(). The old compactIfUnderloaded() is also removed.
- Capacity computation code is now in its own functions, bestCapacity() and
hashShift(). setTableSizeLog2() is removed.
- uint32_t is used throughout for capacities, instead of size_t, for
consistency with other similar values.
- changeTableSize() now takes a capacity instead of a deltaLog2, and it can now
handle !mTable.
Measurements:
- Total source code size is reduced by over 900 lines. Also, lots of existing
lines got shorter (i.e. two checks were reduced to one).
- Executable size barely changed, down by 2 KiB on Linux64. The extra branches
are compensated for by the lack of init() calls.
- Speed changed negligibly. The instruction count for Bench_Cpp_MozHash
increased from 2.84 billion to 2.89 billion but any execution time change was
well below noise.
2018-08-10 11:00:29 +03:00
|
|
|
ObjectToIdMap();
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
void trace(JSTracer* trc);
|
2014-09-02 13:07:22 +04:00
|
|
|
void sweep();
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool add(JSContext* cx, JSObject* obj, ObjectId id);
|
|
|
|
ObjectId find(JSObject* obj);
|
|
|
|
void remove(JSObject* obj);
|
2015-01-16 20:56:30 +03:00
|
|
|
void clear();
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
private:
|
2015-07-15 12:22:04 +03:00
|
|
|
Table table_;
|
2013-07-03 11:24:32 +04:00
|
|
|
};
|
|
|
|
|
2014-05-29 05:05:22 +04:00
|
|
|
class Logging;
|
|
|
|
|
2015-01-27 00:32:18 +03:00
|
|
|
class JavaScriptShared : public CPOWManager
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
public:
|
2016-08-11 15:39:23 +03:00
|
|
|
JavaScriptShared();
|
2015-01-16 20:56:30 +03:00
|
|
|
virtual ~JavaScriptShared();
|
2014-05-17 03:40:37 +04:00
|
|
|
|
|
|
|
void decref();
|
|
|
|
void incref();
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool Unwrap(JSContext* cx, const InfallibleTArray<CpowEntry>& aCpows, JS::MutableHandleObject objp) override;
|
|
|
|
bool Wrap(JSContext* cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry>* outCpows) override;
|
2013-07-11 02:05:39 +04:00
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
protected:
|
2015-03-29 01:22:11 +03:00
|
|
|
bool toVariant(JSContext* cx, JS::HandleValue from, JSVariant* to);
|
|
|
|
bool fromVariant(JSContext* cx, const JSVariant& from, JS::MutableHandleValue to);
|
2014-05-17 03:40:35 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool toJSIDVariant(JSContext* cx, JS::HandleId from, JSIDVariant* to);
|
|
|
|
bool fromJSIDVariant(JSContext* cx, const JSIDVariant& from, JS::MutableHandleId to);
|
2014-10-07 13:29:02 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool toSymbolVariant(JSContext* cx, JS::Symbol* sym, SymbolVariant* symVarp);
|
2016-10-20 23:37:26 +03:00
|
|
|
JS::Symbol* fromSymbolVariant(JSContext* cx, const SymbolVariant& symVar);
|
2014-10-07 13:29:03 +04:00
|
|
|
|
2016-01-28 13:28:04 +03:00
|
|
|
bool fromDescriptor(JSContext* cx, JS::Handle<JS::PropertyDescriptor> desc,
|
2015-03-29 01:22:11 +03:00
|
|
|
PPropertyDescriptor* out);
|
|
|
|
bool toDescriptor(JSContext* cx, const PPropertyDescriptor& in,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> out);
|
2014-05-17 03:40:35 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool toObjectOrNullVariant(JSContext* cx, JSObject* obj, ObjectOrNullVariant* objVarp);
|
2016-10-20 23:37:26 +03:00
|
|
|
JSObject* fromObjectOrNullVariant(JSContext* cx, const ObjectOrNullVariant& objVar);
|
2014-10-20 20:47:27 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
bool convertIdToGeckoString(JSContext* cx, JS::HandleId id, nsString* to);
|
|
|
|
bool convertGeckoStringToId(JSContext* cx, const nsString& from, JS::MutableHandleId id);
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
virtual bool toObjectVariant(JSContext* cx, JSObject* obj, ObjectVariant* objVarp) = 0;
|
2016-10-20 23:37:26 +03:00
|
|
|
virtual JSObject* fromObjectVariant(JSContext* cx, const ObjectVariant& objVar) = 0;
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static void ConvertID(const nsID& from, JSIID* to);
|
|
|
|
static void ConvertID(const JSIID& from, nsID* to);
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2017-04-26 13:18:53 +03:00
|
|
|
JSObject* findCPOWById(const ObjectId& objId);
|
|
|
|
JSObject* findCPOWByIdPreserveColor(const ObjectId& objId);
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* findObjectById(JSContext* cx, const ObjectId& objId);
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool hasCPOW(const ObjectId& objId, const JSObject* obj) {
|
2017-04-26 13:18:53 +03:00
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
return findCPOWByIdPreserveColor(objId) == obj;
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 05:05:22 +04:00
|
|
|
static bool LoggingEnabled() { return sLoggingEnabled; }
|
|
|
|
static bool StackLoggingEnabled() { return sStackLoggingEnabled; }
|
|
|
|
|
|
|
|
friend class Logging;
|
|
|
|
|
|
|
|
virtual bool isParent() = 0;
|
2014-09-13 04:41:18 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
virtual JSObject* scopeForTargetObjects() = 0;
|
2014-05-29 05:05:22 +04:00
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
protected:
|
2014-05-17 03:40:37 +04:00
|
|
|
uintptr_t refcount_;
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap objects_;
|
2014-05-17 03:40:36 +04:00
|
|
|
IdToObjectMap cpows_;
|
2014-05-17 03:40:37 +04:00
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
uint64_t nextSerialNumber_;
|
2014-09-25 15:13:29 +04:00
|
|
|
|
2016-10-19 01:58:33 +03:00
|
|
|
// nextCPOWNumber_ should be the value of nextSerialNumber_ in the other
|
|
|
|
// process. The next new CPOW we get should have this serial number.
|
|
|
|
uint64_t nextCPOWNumber_;
|
|
|
|
|
2014-09-25 15:13:29 +04:00
|
|
|
// CPOW references can be weak, and any object we store in a map may be
|
|
|
|
// GCed (at which point the CPOW will report itself "dead" to the owner).
|
|
|
|
// This means that we don't want to store any js::Wrappers in the CPOW map,
|
|
|
|
// because CPOW will die if the wrapper is GCed, even if the underlying
|
|
|
|
// object is still alive.
|
|
|
|
//
|
|
|
|
// This presents a tricky situation for Xray waivers, since they're normally
|
|
|
|
// represented as a special same-compartment wrapper. We have to strip them
|
|
|
|
// off before putting them in the id-to-object and object-to-id maps, so we
|
|
|
|
// need a way of distinguishing them at lookup-time.
|
|
|
|
//
|
|
|
|
// For the id-to-object map, we encode waiver-or-not information into the id
|
|
|
|
// itself, which lets us do the right thing when accessing the object.
|
|
|
|
//
|
|
|
|
// For the object-to-id map, we just keep two maps, one for each type.
|
|
|
|
ObjectToIdMap unwaivedObjectIds_;
|
|
|
|
ObjectToIdMap waivedObjectIds_;
|
2015-03-29 01:22:11 +03:00
|
|
|
ObjectToIdMap& objectIdMap(bool waiver) {
|
2014-09-25 15:13:29 +04:00
|
|
|
return waiver ? waivedObjectIds_ : unwaivedObjectIds_;
|
|
|
|
}
|
2014-05-29 05:05:22 +04:00
|
|
|
|
|
|
|
static bool sLoggingInitialized;
|
|
|
|
static bool sLoggingEnabled;
|
|
|
|
static bool sStackLoggingEnabled;
|
2013-07-03 11:24:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace jsipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|