Skip to content

Commit

Permalink
Make all VTables const
Browse files Browse the repository at this point in the history
Summary:
There are a few good reasons to make VTables, which are already
logically const, physically const:
* Compiler enforces constness
* Security: often put in a read-only section of memory that can't be tampered with
* Performance: compiler knows the contents of the table won't change
(I don't expect a lot from this anywhere important)

In particular, this prevents a wild write from somewhere in the program from
modifying any part of a VTable. We'll know if an invalid CellKind is in a VTable,
it is a bad VTable pointer, not some modification of the VTable.

Reviewed By: tmikov

Differential Revision: D24553780

fbshipit-source-id: 45cd5ce64666a80ef53aabc1f73fc03f299e8bed
  • Loading branch information
Riley Dulin authored and facebook-github-bot committed Oct 28, 2020
1 parent 03e3304 commit e1d4b67
Show file tree
Hide file tree
Showing 36 changed files with 75 additions and 74 deletions.
2 changes: 1 addition & 1 deletion include/hermes/VM/ArrayStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ArrayStorage final
using size_type = uint32_t;
using iterator = GCHermesValue *;

static VTable vt;
static const VTable vt;

#ifdef HERMESVM_SERIALIZE
/// A convinience method to serialize an ArrayStorage which does not contain
Expand Down
12 changes: 6 additions & 6 deletions include/hermes/VM/Callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Environment final
friend void EnvironmentDeserialize(Deserializer &d, CellKind kind);
#endif

static VTable vt;
static const VTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::EnvironmentKind;
Expand Down Expand Up @@ -367,7 +367,7 @@ class BoundFunction final : public Callable {

public:
using Super = Callable;
static CallableVTable vt;
static const CallableVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::BoundFunctionKind;
Expand Down Expand Up @@ -484,7 +484,7 @@ class NativeFunction : public Callable {
#endif

using Super = Callable;
static CallableVTable vt;
static const CallableVTable vt;

static bool classof(const GCCell *cell) {
return kindInRange(
Expand Down Expand Up @@ -976,7 +976,7 @@ class JSFunction : public Callable {
codeBlock) {}

public:
static CallableVTable vt;
static const CallableVTable vt;

static bool classof(const GCCell *cell) {
return kindInRange(
Expand Down Expand Up @@ -1051,7 +1051,7 @@ class JSGeneratorFunction final : public JSFunction {
static constexpr auto kHasFinalizer = HasFinalizer::No;

public:
static CallableVTable vt;
static const CallableVTable vt;

/// Create a GeneratorFunction.
static PseudoHandle<JSGeneratorFunction> create(
Expand Down Expand Up @@ -1145,7 +1145,7 @@ class GeneratorInnerFunction final : public JSFunction {
static constexpr auto kHasFinalizer = HasFinalizer::No;

public:
static CallableVTable vt;
static const CallableVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::GeneratorInnerFunctionKind;
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/DecoratedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class DecoratedObject : public JSObject {
}

using Super = JSObject;
static ObjectVTable vt;
static const ObjectVTable vt;
static bool classof(const GCCell *cell) {
return kindInRange(
cell->getKind(),
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/DictPropertyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class DictPropertyMap final : public VariableSizeRuntimeCell,
friend class OptValue<PropertyPos>;
};

static VTable vt;
static const VTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::DictPropertyMapKind;
Expand Down
4 changes: 2 additions & 2 deletions include/hermes/VM/Domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Domain final : public GCCell {
using Super = GCCell;
friend void DomainBuildMeta(const GCCell *cell, Metadata::Builder &mb);

static VTable vt;
static const VTable vt;

/// Offsets for fields in the cjsModules_ ArrayStorage which contain
/// information about each individual module.
Expand Down Expand Up @@ -245,7 +245,7 @@ class RequireContext final : public JSObject {
friend GC;
using Super = JSObject;

static ObjectVTable vt;
static const ObjectVTable vt;
friend void RequireContextBuildMeta(
const GCCell *cell,
Metadata::Builder &mb);
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/HiddenClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class HiddenClass final : public GCCell {
/// mode".
static constexpr unsigned kDictionaryThreshold = 64;

static VTable vt;
static const VTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::HiddenClassKind;
Expand Down
4 changes: 2 additions & 2 deletions include/hermes/VM/HostModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FinalizableNativeFunction final : public NativeFunction {
FinalizeNativeFunctionPtr finalizePtr_;

public:
static CallableVTable vt;
static const CallableVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::FinalizableNativeFunctionKind;
Expand Down Expand Up @@ -100,7 +100,7 @@ class HostObject final : public DecoratedObject {
friend GC;

public:
static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::HostObjectKind;
Expand Down
6 changes: 3 additions & 3 deletions include/hermes/VM/JSArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Arguments final : public ArrayImpl {
using Super = ArrayImpl;

public:
static ObjectVTable vt;
static const ObjectVTable vt;

// We need one more slot for the '.length' property.
static const PropStorage::size_type NAMED_PROPERTY_SLOTS =
Expand Down Expand Up @@ -290,7 +290,7 @@ class JSArray final : public ArrayImpl {
friend void ArraySerialize(Serializer &s, const GCCell *cell);
#endif

static ObjectVTable vt;
static const ObjectVTable vt;

// We need one more slot for the '.length' property.
static const PropStorage::size_type NAMED_PROPERTY_SLOTS =
Expand Down Expand Up @@ -433,7 +433,7 @@ class JSArrayIterator : public JSObject {
friend void ArrayIteratorBuildMeta(const GCCell *cell, Metadata::Builder &mb);

public:
static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::ArrayIteratorKind;
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/JSArrayBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class JSArrayBuffer final : public JSObject {
// amount is larger than the native platform's `size_t`
using size_type = std::size_t;

static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::ArrayBufferKind;
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/JSDataView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JSDataView final : public JSObject {
using size_type = JSArrayBuffer::size_type;
using Super = JSObject;

static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::DataViewKind;
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/JSDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JSDate final : public JSObject {
using Super = JSObject;

public:
static ObjectVTable vt;
static const ObjectVTable vt;

/// Need one anonymous slot for the [[PrimitiveValue]] internal property.
static const PropStorage::size_type ANONYMOUS_PROPERTY_SLOTS =
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/JSError.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class JSError final : public JSObject {
#endif

using Super = JSObject;
static ObjectVTable vt;
static const ObjectVTable vt;
static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::ErrorKind;
}
Expand Down
4 changes: 2 additions & 2 deletions include/hermes/VM/JSObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class JSObject : public GCCell {
serializeObjectImpl(Serializer &s, const GCCell *cell, unsigned overlapSlots);
#endif

static ObjectVTable vt;
static const ObjectVTable vt;

/// Default capacity of indirect property storage.
static const PropStorage::size_type DEFAULT_PROPERTY_CAPACITY = 4;
Expand Down Expand Up @@ -1476,7 +1476,7 @@ class PropertyAccessor final : public GCCell {
PropertyAccessor(Deserializer &d);
#endif

static VTable vt;
static const VTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::PropertyAccessorKind;
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/JSRegExp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JSRegExp final : public JSObject {

public:
using Super = JSObject;
static ObjectVTable vt;
static const ObjectVTable vt;
static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::RegExpKind;
}
Expand Down
2 changes: 1 addition & 1 deletion include/hermes/VM/JSRegExpStringIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JSRegExpStringIterator : public JSObject {
Metadata::Builder &mb);

public:
static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::RegExpStringIteratorKind;
Expand Down
4 changes: 2 additions & 2 deletions include/hermes/VM/OrderedHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HashMapEntry final : public GCCell {
friend GC;

public:
static VTable vt;
static const VTable vt;

/// The key.
GCHermesValue key;
Expand Down Expand Up @@ -92,7 +92,7 @@ class OrderedHashMap final : public GCCell {
Metadata::Builder &mb);

public:
static VTable vt;
static const VTable vt;

#ifdef HERMESVM_SERIALIZE
OrderedHashMap(Deserializer &d);
Expand Down
10 changes: 5 additions & 5 deletions include/hermes/VM/PrimitiveBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class JSString final : public PrimitiveBox {
// We need one more slot for the length property.
static const PropStorage::size_type NAMED_PROPERTY_SLOTS =
Super::NAMED_PROPERTY_SLOTS + 1;
static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::StringObjectKind;
Expand Down Expand Up @@ -172,7 +172,7 @@ class JSStringIterator : public JSObject {
Metadata::Builder &mb);

public:
static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::StringIteratorKind;
Expand Down Expand Up @@ -217,7 +217,7 @@ class JSNumber final : public PrimitiveBox {
friend GC;

public:
static ObjectVTable vt;
static const ObjectVTable vt;

#ifdef HERMESVM_SERIALIZE
JSNumber(Deserializer &d, const VTable *vt);
Expand Down Expand Up @@ -246,7 +246,7 @@ class JSBoolean final : public PrimitiveBox {
friend GC;

public:
static ObjectVTable vt;
static const ObjectVTable vt;

#ifdef HERMESVM_SERIALIZE
JSBoolean(Deserializer &d, const VTable *vt);
Expand Down Expand Up @@ -278,7 +278,7 @@ class JSSymbol final : public PrimitiveBox {
friend GC;

public:
static ObjectVTable vt;
static const ObjectVTable vt;

static bool classof(const GCCell *cell) {
return cell->getKind() == CellKind::SymbolObjectKind;
Expand Down
4 changes: 2 additions & 2 deletions include/hermes/VM/SegmentedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SegmentedArray final
#endif

friend void SegmentBuildMeta(const GCCell *cell, Metadata::Builder &mb);
static VTable vt;
static const VTable vt;

AtomicIfConcurrentGC<uint32_t> length_;
GCHermesValue data_[kMaxLength];
Expand Down Expand Up @@ -357,7 +357,7 @@ class SegmentedArray final
}

private:
static VTable vt;
static const VTable vt;

friend TrailingObjects;
friend void SegmentBuildMeta(const GCCell *cell, Metadata::Builder &mb);
Expand Down
2 changes: 1 addition & 1 deletion lib/VM/ArrayStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace hermes {
namespace vm {

VTable ArrayStorage::vt(
const VTable ArrayStorage::vt(
CellKind::ArrayStorageKind,
0,
nullptr,
Expand Down
12 changes: 6 additions & 6 deletions lib/VM/Callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace vm {
//===----------------------------------------------------------------------===//
// class Environment

VTable Environment::vt{CellKind::EnvironmentKind, 0};
const VTable Environment::vt{CellKind::EnvironmentKind, 0};

void EnvironmentBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
const auto *self = static_cast<const Environment *>(cell);
Expand Down Expand Up @@ -483,7 +483,7 @@ CallResult<double> Callable::extractOwnLengthProperty_RJS(
//===----------------------------------------------------------------------===//
// class BoundFunction

CallableVTable BoundFunction::vt{
const CallableVTable BoundFunction::vt{
{
VTable(
CellKind::BoundFunctionKind,
Expand Down Expand Up @@ -887,7 +887,7 @@ CallResult<PseudoHandle<>> BoundFunction::_callImpl(
//===----------------------------------------------------------------------===//
// class NativeFunction

CallableVTable NativeFunction::vt{
const CallableVTable NativeFunction::vt{
{
VTable(
CellKind::NativeFunctionKind,
Expand Down Expand Up @@ -1183,7 +1183,7 @@ CallResult<PseudoHandle<>> NativeConstructor::_callImpl(
//===----------------------------------------------------------------------===//
// class JSFunction

CallableVTable JSFunction::vt{
const CallableVTable JSFunction::vt{
{
VTable(
CellKind::FunctionKind,
Expand Down Expand Up @@ -1315,7 +1315,7 @@ void JSFunction::_snapshotAddLocationsImpl(
//===----------------------------------------------------------------------===//
// class JSGeneratorFunction

CallableVTable JSGeneratorFunction::vt{
const CallableVTable JSGeneratorFunction::vt{
{
VTable(
CellKind::GeneratorFunctionKind,
Expand Down Expand Up @@ -1391,7 +1391,7 @@ PseudoHandle<JSGeneratorFunction> JSGeneratorFunction::create(
//===----------------------------------------------------------------------===//
// class GeneratorInnerFunction

CallableVTable GeneratorInnerFunction::vt{
const CallableVTable GeneratorInnerFunction::vt{
{
VTable(
CellKind::GeneratorInnerFunctionKind,
Expand Down
2 changes: 1 addition & 1 deletion lib/VM/DecoratedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ size_t DecoratedObject::Decoration::getMallocSize() const {
return sizeof *this;
}

ObjectVTable DecoratedObject::vt{
const ObjectVTable DecoratedObject::vt{
VTable(
CellKind::DecoratedObjectKind,
cellSize<DecoratedObject>(),
Expand Down
2 changes: 1 addition & 1 deletion lib/VM/DictPropertyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct DictPropertyMap::detail {
"too few bits to store max possible descriptor index");
};

VTable DictPropertyMap::vt{CellKind::DictPropertyMapKind, 0};
const VTable DictPropertyMap::vt{CellKind::DictPropertyMapKind, 0};

void DictPropertyMapBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
const auto *self = static_cast<const DictPropertyMap *>(cell);
Expand Down
Loading

0 comments on commit e1d4b67

Please sign in to comment.