2012-01-20 16:17:08 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_SERIALIZE_H_
|
|
|
|
#define V8_SERIALIZE_H_
|
|
|
|
|
|
|
|
#include "hashmap.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// A TypeCode is used to distinguish different kinds of external reference.
|
|
|
|
// It is a single bit to make testing for types easy.
|
|
|
|
enum TypeCode {
|
|
|
|
UNCLASSIFIED, // One-of-a-kind references.
|
|
|
|
BUILTIN,
|
|
|
|
RUNTIME_FUNCTION,
|
|
|
|
IC_UTILITY,
|
|
|
|
DEBUG_ADDRESS,
|
|
|
|
STATS_COUNTER,
|
|
|
|
TOP_ADDRESS,
|
|
|
|
C_BUILTIN,
|
|
|
|
EXTENSION,
|
|
|
|
ACCESSOR,
|
|
|
|
RUNTIME_ENTRY,
|
2012-12-18 16:25:45 +00:00
|
|
|
STUB_CACHE_TABLE,
|
|
|
|
LAZY_DEOPTIMIZATION
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2012-12-18 16:25:45 +00:00
|
|
|
const int kTypeCodeCount = LAZY_DEOPTIMIZATION + 1;
|
2008-07-03 15:10:15 +00:00
|
|
|
const int kFirstTypeCode = UNCLASSIFIED;
|
|
|
|
|
|
|
|
const int kReferenceIdBits = 16;
|
|
|
|
const int kReferenceIdMask = (1 << kReferenceIdBits) - 1;
|
|
|
|
const int kReferenceTypeShift = kReferenceIdBits;
|
|
|
|
const int kDebugRegisterBits = 4;
|
|
|
|
const int kDebugIdShift = kDebugRegisterBits;
|
|
|
|
|
2012-12-18 16:25:45 +00:00
|
|
|
const int kDeoptTableSerializeEntryCount = 8;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-08-17 08:48:54 +00:00
|
|
|
// ExternalReferenceTable is a helper class that defines the relationship
|
|
|
|
// between external references and their encodings. It is used to build
|
|
|
|
// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
|
|
|
|
class ExternalReferenceTable {
|
|
|
|
public:
|
|
|
|
static ExternalReferenceTable* instance(Isolate* isolate);
|
|
|
|
|
|
|
|
~ExternalReferenceTable() { }
|
|
|
|
|
|
|
|
int size() const { return refs_.length(); }
|
|
|
|
|
|
|
|
Address address(int i) { return refs_[i].address; }
|
|
|
|
|
|
|
|
uint32_t code(int i) { return refs_[i].code; }
|
|
|
|
|
|
|
|
const char* name(int i) { return refs_[i].name; }
|
|
|
|
|
|
|
|
int max_id(int code) { return max_id_[code]; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ExternalReferenceTable(Isolate* isolate) : refs_(64) {
|
|
|
|
PopulateTable(isolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExternalReferenceEntry {
|
|
|
|
Address address;
|
|
|
|
uint32_t code;
|
|
|
|
const char* name;
|
|
|
|
};
|
|
|
|
|
|
|
|
void PopulateTable(Isolate* isolate);
|
|
|
|
|
|
|
|
// For a few types of references, we can get their address from their id.
|
|
|
|
void AddFromId(TypeCode type,
|
|
|
|
uint16_t id,
|
|
|
|
const char* name,
|
|
|
|
Isolate* isolate);
|
|
|
|
|
|
|
|
// For other types of references, the caller will figure out the address.
|
|
|
|
void Add(Address address, TypeCode type, uint16_t id, const char* name);
|
|
|
|
|
|
|
|
List<ExternalReferenceEntry> refs_;
|
|
|
|
int max_id_[kTypeCodeCount];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
class ExternalReferenceEncoder {
|
|
|
|
public:
|
2013-09-03 11:54:08 +00:00
|
|
|
explicit ExternalReferenceEncoder(Isolate* isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
uint32_t Encode(Address key) const;
|
|
|
|
|
|
|
|
const char* NameOfAddress(Address key) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashMap encodings_;
|
|
|
|
static uint32_t Hash(Address key) {
|
2009-05-06 07:53:08 +00:00
|
|
|
return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >> 2);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int IndexOf(Address key) const;
|
|
|
|
|
|
|
|
static bool Match(void* key1, void* key2) { return key1 == key2; }
|
|
|
|
|
|
|
|
void Put(Address key, int index);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
Isolate* isolate_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalReferenceDecoder {
|
|
|
|
public:
|
2013-09-03 11:54:08 +00:00
|
|
|
explicit ExternalReferenceDecoder(Isolate* isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
~ExternalReferenceDecoder();
|
|
|
|
|
|
|
|
Address Decode(uint32_t key) const {
|
|
|
|
if (key == 0) return NULL;
|
|
|
|
return *Lookup(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Address** encodings_;
|
|
|
|
|
|
|
|
Address* Lookup(uint32_t key) const {
|
|
|
|
int type = key >> kReferenceTypeShift;
|
|
|
|
ASSERT(kFirstTypeCode <= type && type < kTypeCodeCount);
|
|
|
|
int id = key & kReferenceIdMask;
|
|
|
|
return &encodings_[type][id];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Put(uint32_t key, Address value) {
|
|
|
|
*Lookup(key) = value;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
Isolate* isolate_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
class SnapshotByteSource {
|
|
|
|
public:
|
|
|
|
SnapshotByteSource(const byte* array, int length)
|
|
|
|
: data_(array), length_(length), position_(0) { }
|
|
|
|
|
|
|
|
bool HasMore() { return position_ < length_; }
|
|
|
|
|
|
|
|
int Get() {
|
|
|
|
ASSERT(position_ < length_);
|
|
|
|
return data_[position_++];
|
|
|
|
}
|
|
|
|
|
2012-09-14 11:16:56 +00:00
|
|
|
int32_t GetUnalignedInt() {
|
|
|
|
#if defined(V8_HOST_CAN_READ_UNALIGNED) && __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
int32_t answer;
|
|
|
|
ASSERT(position_ + sizeof(answer) <= length_ + 0u);
|
|
|
|
answer = *reinterpret_cast<const int32_t*>(data_ + position_);
|
|
|
|
#else
|
|
|
|
int32_t answer = data_[position_];
|
|
|
|
answer |= data_[position_ + 1] << 8;
|
|
|
|
answer |= data_[position_ + 2] << 16;
|
|
|
|
answer |= data_[position_ + 3] << 24;
|
|
|
|
#endif
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Advance(int by) { position_ += by; }
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
inline void CopyRaw(byte* to, int number_of_bytes);
|
2009-10-30 10:23:12 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
inline int GetInt();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2012-09-14 11:16:56 +00:00
|
|
|
bool AtEOF();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2010-01-21 13:59:15 +00:00
|
|
|
int position() { return position_; }
|
2010-01-18 16:04:25 +00:00
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
private:
|
|
|
|
const byte* data_;
|
|
|
|
int length_;
|
|
|
|
int position_;
|
|
|
|
};
|
|
|
|
|
2009-11-16 12:08:40 +00:00
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
// The Serializer/Deserializer class is a common superclass for Serializer and
|
|
|
|
// Deserializer which is used to store common constants and methods used by
|
|
|
|
// both.
|
|
|
|
class SerializerDeserializer: public ObjectVisitor {
|
|
|
|
public:
|
2013-09-03 11:54:08 +00:00
|
|
|
static void Iterate(Isolate* isolate, ObjectVisitor* visitor);
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2012-09-14 11:16:56 +00:00
|
|
|
static int nop() { return kNop; }
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
protected:
|
2010-05-20 13:54:31 +00:00
|
|
|
// Where the pointed-to object can be found:
|
|
|
|
enum Where {
|
|
|
|
kNewObject = 0, // Object is next in snapshot.
|
2012-09-14 11:16:56 +00:00
|
|
|
// 1-6 One per space.
|
2010-05-20 13:54:31 +00:00
|
|
|
kRootArray = 0x9, // Object is found in root array.
|
|
|
|
kPartialSnapshotCache = 0xa, // Object is in the cache.
|
|
|
|
kExternalReference = 0xb, // Pointer to an external reference.
|
2012-09-14 11:16:56 +00:00
|
|
|
kSkip = 0xc, // Skip n bytes.
|
|
|
|
kNop = 0xd, // Does nothing, used to pad.
|
|
|
|
// 0xe-0xf Free.
|
|
|
|
kBackref = 0x10, // Object is described relative to end.
|
|
|
|
// 0x11-0x16 One per space.
|
|
|
|
kBackrefWithSkip = 0x18, // Object is described relative to end.
|
|
|
|
// 0x19-0x1e One per space.
|
|
|
|
// 0x20-0x3f Used by misc. tags below.
|
2010-05-20 13:54:31 +00:00
|
|
|
kPointedToMask = 0x3f
|
2009-10-27 11:54:01 +00:00
|
|
|
};
|
2010-05-20 13:54:31 +00:00
|
|
|
|
|
|
|
// How to code the pointer to the object.
|
|
|
|
enum HowToCode {
|
|
|
|
kPlain = 0, // Straight pointer.
|
|
|
|
// What this means depends on the architecture:
|
|
|
|
kFromCode = 0x40, // A pointer inlined in code.
|
|
|
|
kHowToCodeMask = 0x40
|
|
|
|
};
|
|
|
|
|
2012-09-14 11:16:56 +00:00
|
|
|
// For kRootArrayConstants
|
|
|
|
enum WithSkip {
|
|
|
|
kNoSkipDistance = 0,
|
|
|
|
kHasSkipDistance = 0x40,
|
|
|
|
kWithSkipMask = 0x40
|
|
|
|
};
|
|
|
|
|
2010-05-20 13:54:31 +00:00
|
|
|
// Where to point within the object.
|
|
|
|
enum WhereToPoint {
|
|
|
|
kStartOfObject = 0,
|
2012-07-31 09:25:23 +00:00
|
|
|
kInnerPointer = 0x80, // First insn in code object or payload of cell.
|
2010-05-20 13:54:31 +00:00
|
|
|
kWhereToPointMask = 0x80
|
|
|
|
};
|
|
|
|
|
|
|
|
// Misc.
|
2012-09-14 11:16:56 +00:00
|
|
|
// Raw data to be copied from the snapshot. This byte code does not advance
|
|
|
|
// the current pointer, which is used for code objects, where we write the
|
|
|
|
// entire code in one memcpy, then fix up stuff with kSkip and other byte
|
|
|
|
// codes that overwrite data.
|
|
|
|
static const int kRawData = 0x20;
|
|
|
|
// Some common raw lengths: 0x21-0x3f. These autoadvance the current pointer.
|
2010-05-20 13:54:31 +00:00
|
|
|
// A tag emitted at strategic points in the snapshot to delineate sections.
|
|
|
|
// If the deserializer does not find these at the expected moments then it
|
|
|
|
// is an indication that the snapshot and the VM do not fit together.
|
|
|
|
// Examine the build process for architecture, version or configuration
|
|
|
|
// mismatches.
|
2011-05-23 12:59:02 +00:00
|
|
|
static const int kSynchronize = 0x70;
|
2010-05-20 13:54:31 +00:00
|
|
|
// Used for the source code of the natives, which is in the executable, but
|
|
|
|
// is referred to from external strings in the snapshot.
|
2011-05-23 12:59:02 +00:00
|
|
|
static const int kNativesStringResource = 0x71;
|
2012-09-14 11:16:56 +00:00
|
|
|
static const int kRepeat = 0x72;
|
|
|
|
static const int kConstantRepeat = 0x73;
|
|
|
|
// 0x73-0x7f Repeat last word (subtract 0x72 to get the count).
|
|
|
|
static const int kMaxRepeats = 0x7f - 0x72;
|
2011-10-20 12:27:10 +00:00
|
|
|
static int CodeForRepeats(int repeats) {
|
|
|
|
ASSERT(repeats >= 1 && repeats <= kMaxRepeats);
|
2012-09-14 11:16:56 +00:00
|
|
|
return 0x72 + repeats;
|
2011-10-20 12:27:10 +00:00
|
|
|
}
|
|
|
|
static int RepeatsForCode(int byte_code) {
|
|
|
|
ASSERT(byte_code >= kConstantRepeat && byte_code <= 0x7f);
|
2012-09-14 11:16:56 +00:00
|
|
|
return byte_code - 0x72;
|
2011-10-20 12:27:10 +00:00
|
|
|
}
|
2012-09-14 11:16:56 +00:00
|
|
|
static const int kRootArrayConstants = 0xa0;
|
|
|
|
// 0xa0-0xbf Things from the first 32 elements of the root array.
|
2011-10-20 12:27:10 +00:00
|
|
|
static const int kRootArrayNumberOfConstantEncodings = 0x20;
|
|
|
|
static int RootArrayConstantFromByteCode(int byte_code) {
|
2012-09-14 11:16:56 +00:00
|
|
|
return byte_code & 0x1f;
|
2011-10-20 12:27:10 +00:00
|
|
|
}
|
2010-05-20 13:54:31 +00:00
|
|
|
|
2012-09-14 11:16:56 +00:00
|
|
|
static const int kNumberOfSpaces = LO_SPACE;
|
2011-05-23 12:59:02 +00:00
|
|
|
static const int kAnyOldSpace = -1;
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2009-11-03 21:00:43 +00:00
|
|
|
// A bitmask for getting the space out of an instruction.
|
2012-09-14 11:16:56 +00:00
|
|
|
static const int kSpaceMask = 7;
|
2009-10-27 11:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
int SnapshotByteSource::GetInt() {
|
2012-09-14 11:16:56 +00:00
|
|
|
// This way of variable-length encoding integers does not suffer from branch
|
|
|
|
// mispredictions.
|
|
|
|
uint32_t answer = GetUnalignedInt();
|
|
|
|
int bytes = answer & 3;
|
|
|
|
Advance(bytes);
|
|
|
|
uint32_t mask = 0xffffffffu;
|
|
|
|
mask >>= 32 - (bytes << 3);
|
|
|
|
answer &= mask;
|
|
|
|
answer >>= 2;
|
|
|
|
return answer;
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) {
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemCopy(to, data_ + position_, number_of_bytes);
|
2010-03-23 11:40:38 +00:00
|
|
|
position_ += number_of_bytes;
|
|
|
|
}
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
|
|
|
|
// A Deserializer reads a snapshot and reconstructs the Object graph it defines.
|
2010-01-27 08:25:48 +00:00
|
|
|
class Deserializer: public SerializerDeserializer {
|
2009-10-27 11:54:01 +00:00
|
|
|
public:
|
|
|
|
// Create a deserializer from a snapshot byte source.
|
2009-11-16 12:08:40 +00:00
|
|
|
explicit Deserializer(SnapshotByteSource* source);
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
virtual ~Deserializer();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
|
|
|
// Deserialize the snapshot into an empty heap.
|
2013-09-03 11:54:08 +00:00
|
|
|
void Deserialize(Isolate* isolate);
|
2010-01-15 14:20:31 +00:00
|
|
|
|
|
|
|
// Deserialize a single object and the objects reachable from it.
|
2013-09-03 11:54:08 +00:00
|
|
|
void DeserializePartial(Isolate* isolate, Object** root);
|
2010-01-15 14:20:31 +00:00
|
|
|
|
2012-09-14 11:48:31 +00:00
|
|
|
void set_reservation(int space_number, int reservation) {
|
2012-09-14 11:16:56 +00:00
|
|
|
ASSERT(space_number >= 0);
|
|
|
|
ASSERT(space_number <= LAST_SPACE);
|
|
|
|
reservations_[space_number] = reservation;
|
|
|
|
}
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
private:
|
|
|
|
virtual void VisitPointers(Object** start, Object** end);
|
|
|
|
|
|
|
|
virtual void VisitExternalReferences(Address* start, Address* end) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void VisitRuntimeEntry(RelocInfo* rinfo) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2013-07-17 11:50:24 +00:00
|
|
|
// Allocation sites are present in the snapshot, and must be linked into
|
|
|
|
// a list at deserialization time.
|
|
|
|
void RelinkAllocationSite(AllocationSite* site);
|
|
|
|
|
2011-10-25 08:23:56 +00:00
|
|
|
// Fills in some heap data in an area from start to end (non-inclusive). The
|
|
|
|
// space id is used for the write barrier. The object_address is the address
|
|
|
|
// of the object we are writing into, or NULL if we are not writing into an
|
2012-01-16 12:38:59 +00:00
|
|
|
// object, i.e. if we are writing a series of tagged values that are not on
|
|
|
|
// the heap.
|
2011-10-25 08:23:56 +00:00
|
|
|
void ReadChunk(
|
|
|
|
Object** start, Object** end, int space, Address object_address);
|
2012-09-14 11:16:56 +00:00
|
|
|
void ReadObject(int space_number, Object** write_back);
|
|
|
|
|
|
|
|
// This routine both allocates a new object, and also keeps
|
|
|
|
// track of where objects have been allocated so that we can
|
|
|
|
// fix back references when deserializing.
|
|
|
|
Address Allocate(int space_index, int size) {
|
|
|
|
Address address = high_water_[space_index];
|
|
|
|
high_water_[space_index] = address + size;
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns the address of an object that has been described in the
|
|
|
|
// snapshot as being offset bytes back in a particular space.
|
|
|
|
HeapObject* GetAddressFromEnd(int space) {
|
|
|
|
int offset = source_->GetInt();
|
|
|
|
offset <<= kObjectAlignmentBits;
|
|
|
|
return HeapObject::FromAddress(high_water_[space] - offset);
|
|
|
|
}
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Cached current isolate.
|
|
|
|
Isolate* isolate_;
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
SnapshotByteSource* source_;
|
2009-11-03 21:00:43 +00:00
|
|
|
// This is the address of the next object that will be allocated in each
|
|
|
|
// space. It is used to calculate the addresses of back-references.
|
|
|
|
Address high_water_[LAST_SPACE + 1];
|
2012-09-14 11:16:56 +00:00
|
|
|
|
2012-09-14 11:48:31 +00:00
|
|
|
int reservations_[LAST_SPACE + 1];
|
2012-09-14 11:16:56 +00:00
|
|
|
static const intptr_t kUninitializedReservation = -1;
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
ExternalReferenceDecoder* external_reference_decoder_;
|
|
|
|
|
2009-11-16 12:08:40 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Deserializer);
|
2009-10-27 11:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SnapshotByteSink {
|
|
|
|
public:
|
|
|
|
virtual ~SnapshotByteSink() { }
|
|
|
|
virtual void Put(int byte, const char* description) = 0;
|
2009-11-03 21:00:43 +00:00
|
|
|
virtual void PutSection(int byte, const char* description) {
|
|
|
|
Put(byte, description);
|
|
|
|
}
|
2009-10-27 11:54:01 +00:00
|
|
|
void PutInt(uintptr_t integer, const char* description);
|
2010-01-18 16:04:25 +00:00
|
|
|
virtual int Position() = 0;
|
2009-10-27 11:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
// Mapping objects to their location after deserialization.
|
|
|
|
// This is used during building, but not at runtime by V8.
|
|
|
|
class SerializationAddressMapper {
|
|
|
|
public:
|
|
|
|
SerializationAddressMapper()
|
2013-06-03 15:32:22 +00:00
|
|
|
: no_allocation_(),
|
|
|
|
serialization_map_(new HashMap(&SerializationMatchFun)) { }
|
2010-01-27 08:25:48 +00:00
|
|
|
|
|
|
|
~SerializationAddressMapper() {
|
|
|
|
delete serialization_map_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsMapped(HeapObject* obj) {
|
|
|
|
return serialization_map_->Lookup(Key(obj), Hash(obj), false) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MappedTo(HeapObject* obj) {
|
|
|
|
ASSERT(IsMapped(obj));
|
|
|
|
return static_cast<int>(reinterpret_cast<intptr_t>(
|
|
|
|
serialization_map_->Lookup(Key(obj), Hash(obj), false)->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddMapping(HeapObject* obj, int to) {
|
|
|
|
ASSERT(!IsMapped(obj));
|
|
|
|
HashMap::Entry* entry =
|
|
|
|
serialization_map_->Lookup(Key(obj), Hash(obj), true);
|
|
|
|
entry->value = Value(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-05-23 12:59:02 +00:00
|
|
|
static bool SerializationMatchFun(void* key1, void* key2) {
|
2010-01-27 08:25:48 +00:00
|
|
|
return key1 == key2;
|
|
|
|
}
|
|
|
|
|
2011-05-23 12:59:02 +00:00
|
|
|
static uint32_t Hash(HeapObject* obj) {
|
2010-01-27 08:25:48 +00:00
|
|
|
return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address()));
|
|
|
|
}
|
|
|
|
|
2011-05-23 12:59:02 +00:00
|
|
|
static void* Key(HeapObject* obj) {
|
2010-01-27 08:25:48 +00:00
|
|
|
return reinterpret_cast<void*>(obj->address());
|
|
|
|
}
|
|
|
|
|
2011-05-23 12:59:02 +00:00
|
|
|
static void* Value(int v) {
|
2010-01-27 08:25:48 +00:00
|
|
|
return reinterpret_cast<void*>(v);
|
|
|
|
}
|
|
|
|
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_allocation_;
|
2010-01-27 08:25:48 +00:00
|
|
|
HashMap* serialization_map_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SerializationAddressMapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-26 13:50:23 +00:00
|
|
|
class CodeAddressMap;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// There can be only one serializer per V8 process.
|
2011-05-23 12:59:02 +00:00
|
|
|
class Serializer : public SerializerDeserializer {
|
2009-10-27 11:54:01 +00:00
|
|
|
public:
|
2013-09-03 11:54:08 +00:00
|
|
|
Serializer(Isolate* isolate, SnapshotByteSink* sink);
|
2010-03-23 11:40:38 +00:00
|
|
|
~Serializer();
|
2009-10-27 11:54:01 +00:00
|
|
|
void VisitPointers(Object** start, Object** end);
|
2010-01-12 15:16:23 +00:00
|
|
|
// You can call this after serialization to find out how much space was used
|
|
|
|
// in each space.
|
|
|
|
int CurrentAllocationAddress(int space) {
|
2012-09-14 11:16:56 +00:00
|
|
|
ASSERT(space < kNumberOfSpaces);
|
2010-01-12 15:16:23 +00:00
|
|
|
return fullness_[space];
|
|
|
|
}
|
2009-11-16 12:08:40 +00:00
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
Isolate* isolate() const { return isolate_; }
|
|
|
|
static void Enable(Isolate* isolate);
|
2013-07-26 13:50:23 +00:00
|
|
|
static void Disable();
|
2009-11-16 12:08:40 +00:00
|
|
|
|
|
|
|
// Call this when you have made use of the fact that there is no serialization
|
|
|
|
// going on.
|
2011-05-23 12:59:02 +00:00
|
|
|
static void TooLateToEnableNow() { too_late_to_enable_now_ = true; }
|
|
|
|
static bool enabled() { return serialization_enabled_; }
|
2010-01-27 08:25:48 +00:00
|
|
|
SerializationAddressMapper* address_mapper() { return &address_mapper_; }
|
2012-09-14 11:16:56 +00:00
|
|
|
void PutRoot(int index,
|
|
|
|
HeapObject* object,
|
|
|
|
HowToCode how,
|
|
|
|
WhereToPoint where,
|
|
|
|
int skip);
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
protected:
|
2011-05-23 12:59:02 +00:00
|
|
|
static const int kInvalidRootIndex = -1;
|
2011-10-20 12:27:10 +00:00
|
|
|
|
2012-03-21 14:29:14 +00:00
|
|
|
int RootIndex(HeapObject* heap_object, HowToCode from);
|
2010-01-27 08:25:48 +00:00
|
|
|
virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) = 0;
|
2011-10-20 12:27:10 +00:00
|
|
|
intptr_t root_index_wave_front() { return root_index_wave_front_; }
|
|
|
|
void set_root_index_wave_front(intptr_t value) {
|
|
|
|
ASSERT(value >= root_index_wave_front_);
|
|
|
|
root_index_wave_front_ = value;
|
|
|
|
}
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
class ObjectSerializer : public ObjectVisitor {
|
|
|
|
public:
|
2009-11-16 12:08:40 +00:00
|
|
|
ObjectSerializer(Serializer* serializer,
|
2009-10-27 11:54:01 +00:00
|
|
|
Object* o,
|
|
|
|
SnapshotByteSink* sink,
|
2010-05-20 13:54:31 +00:00
|
|
|
HowToCode how_to_code,
|
|
|
|
WhereToPoint where_to_point)
|
2009-10-27 11:54:01 +00:00
|
|
|
: serializer_(serializer),
|
|
|
|
object_(HeapObject::cast(o)),
|
|
|
|
sink_(sink),
|
2010-05-20 13:54:31 +00:00
|
|
|
reference_representation_(how_to_code + where_to_point),
|
2012-09-14 11:16:56 +00:00
|
|
|
bytes_processed_so_far_(0),
|
|
|
|
code_object_(o->IsCode()),
|
|
|
|
code_has_been_output_(false) { }
|
2009-10-27 11:54:01 +00:00
|
|
|
void Serialize();
|
|
|
|
void VisitPointers(Object** start, Object** end);
|
2011-11-11 12:28:42 +00:00
|
|
|
void VisitEmbeddedPointer(RelocInfo* target);
|
2009-10-27 11:54:01 +00:00
|
|
|
void VisitExternalReferences(Address* start, Address* end);
|
2011-11-11 12:28:42 +00:00
|
|
|
void VisitExternalReference(RelocInfo* rinfo);
|
2009-10-27 11:54:01 +00:00
|
|
|
void VisitCodeTarget(RelocInfo* target);
|
2010-08-20 07:10:18 +00:00
|
|
|
void VisitCodeEntry(Address entry_address);
|
2013-06-12 15:03:44 +00:00
|
|
|
void VisitCell(RelocInfo* rinfo);
|
2009-10-30 10:23:12 +00:00
|
|
|
void VisitRuntimeEntry(RelocInfo* reloc);
|
2009-11-06 13:48:33 +00:00
|
|
|
// Used for seralizing the external strings that hold the natives source.
|
|
|
|
void VisitExternalAsciiString(
|
|
|
|
v8::String::ExternalAsciiStringResource** resource);
|
|
|
|
// We can't serialize a heap with external two byte strings.
|
|
|
|
void VisitExternalTwoByteString(
|
|
|
|
v8::String::ExternalStringResource** resource) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2009-10-27 11:54:01 +00:00
|
|
|
|
|
|
|
private:
|
2012-09-14 11:16:56 +00:00
|
|
|
enum ReturnSkip { kCanReturnSkipInsteadOfSkipping, kIgnoringReturn };
|
|
|
|
// This function outputs or skips the raw data between the last pointer and
|
|
|
|
// up to the current position. It optionally can just return the number of
|
|
|
|
// bytes to skip instead of performing a skip instruction, in case the skip
|
|
|
|
// can be merged into the next instruction.
|
|
|
|
int OutputRawData(Address up_to, ReturnSkip return_skip = kIgnoringReturn);
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2009-11-16 12:08:40 +00:00
|
|
|
Serializer* serializer_;
|
2009-10-27 11:54:01 +00:00
|
|
|
HeapObject* object_;
|
|
|
|
SnapshotByteSink* sink_;
|
2010-05-20 13:54:31 +00:00
|
|
|
int reference_representation_;
|
2009-10-27 11:54:01 +00:00
|
|
|
int bytes_processed_so_far_;
|
2012-09-14 11:16:56 +00:00
|
|
|
bool code_object_;
|
|
|
|
bool code_has_been_output_;
|
2009-10-27 11:54:01 +00:00
|
|
|
};
|
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
virtual void SerializeObject(Object* o,
|
2010-05-20 13:54:31 +00:00
|
|
|
HowToCode how_to_code,
|
2012-09-14 11:16:56 +00:00
|
|
|
WhereToPoint where_to_point,
|
|
|
|
int skip) = 0;
|
2010-01-27 08:25:48 +00:00
|
|
|
void SerializeReferenceToPreviousObject(
|
|
|
|
int space,
|
|
|
|
int address,
|
2010-05-20 13:54:31 +00:00
|
|
|
HowToCode how_to_code,
|
2012-09-14 11:16:56 +00:00
|
|
|
WhereToPoint where_to_point,
|
|
|
|
int skip);
|
2009-10-27 11:54:01 +00:00
|
|
|
void InitializeAllocators();
|
2012-09-14 11:16:56 +00:00
|
|
|
// This will return the space for an object.
|
2011-05-23 12:59:02 +00:00
|
|
|
static int SpaceOfObject(HeapObject* object);
|
2012-09-14 11:16:56 +00:00
|
|
|
int Allocate(int space, int size);
|
2009-10-27 11:54:01 +00:00
|
|
|
int EncodeExternalReference(Address addr) {
|
|
|
|
return external_reference_encoder_->Encode(addr);
|
|
|
|
}
|
|
|
|
|
2012-02-23 12:11:24 +00:00
|
|
|
int SpaceAreaSize(int space);
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
2009-10-27 11:54:01 +00:00
|
|
|
// Keep track of the fullness of each space in order to generate
|
2012-09-14 11:16:56 +00:00
|
|
|
// relative addresses for back references.
|
2009-10-27 11:54:01 +00:00
|
|
|
int fullness_[LAST_SPACE + 1];
|
|
|
|
SnapshotByteSink* sink_;
|
|
|
|
int current_root_index_;
|
|
|
|
ExternalReferenceEncoder* external_reference_encoder_;
|
2011-05-23 12:59:02 +00:00
|
|
|
static bool serialization_enabled_;
|
2009-11-16 12:08:40 +00:00
|
|
|
// Did we already make use of the fact that serialization was not enabled?
|
2011-05-23 12:59:02 +00:00
|
|
|
static bool too_late_to_enable_now_;
|
2010-01-27 08:25:48 +00:00
|
|
|
SerializationAddressMapper address_mapper_;
|
2011-10-20 12:27:10 +00:00
|
|
|
intptr_t root_index_wave_front_;
|
2012-09-14 11:16:56 +00:00
|
|
|
void Pad();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
|
|
|
friend class ObjectSerializer;
|
2009-11-16 12:08:40 +00:00
|
|
|
friend class Deserializer;
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2012-01-20 16:17:08 +00:00
|
|
|
private:
|
2013-07-26 13:50:23 +00:00
|
|
|
static CodeAddressMap* code_address_map_;
|
2009-11-16 12:08:40 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Serializer);
|
2009-10-27 11:54:01 +00:00
|
|
|
};
|
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
|
|
|
|
class PartialSerializer : public Serializer {
|
|
|
|
public:
|
2013-09-03 11:54:08 +00:00
|
|
|
PartialSerializer(Isolate* isolate,
|
|
|
|
Serializer* startup_snapshot_serializer,
|
2010-01-27 08:25:48 +00:00
|
|
|
SnapshotByteSink* sink)
|
2013-09-03 11:54:08 +00:00
|
|
|
: Serializer(isolate, sink),
|
2010-01-27 08:25:48 +00:00
|
|
|
startup_serializer_(startup_snapshot_serializer) {
|
2011-10-20 12:27:10 +00:00
|
|
|
set_root_index_wave_front(Heap::kStrongRootListLength);
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the objects reachable from a single object pointer.
|
|
|
|
virtual void Serialize(Object** o);
|
|
|
|
virtual void SerializeObject(Object* o,
|
2010-05-20 13:54:31 +00:00
|
|
|
HowToCode how_to_code,
|
2012-09-14 11:16:56 +00:00
|
|
|
WhereToPoint where_to_point,
|
|
|
|
int skip);
|
2010-01-27 08:25:48 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual int PartialSnapshotCacheIndex(HeapObject* o);
|
|
|
|
virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
|
2010-03-23 11:40:38 +00:00
|
|
|
// Scripts should be referred only through shared function infos. We can't
|
|
|
|
// allow them to be part of the partial snapshot because they contain a
|
|
|
|
// unique ID, and deserializing several partial snapshots containing script
|
|
|
|
// would cause dupes.
|
|
|
|
ASSERT(!o->IsScript());
|
2013-06-20 09:10:19 +00:00
|
|
|
return o->IsName() || o->IsSharedFunctionInfo() ||
|
2010-08-16 16:06:46 +00:00
|
|
|
o->IsHeapNumber() || o->IsCode() ||
|
2011-11-03 10:36:55 +00:00
|
|
|
o->IsScopeInfo() ||
|
2013-09-11 07:14:41 +00:00
|
|
|
o->map() ==
|
|
|
|
startup_serializer_->isolate()->heap()->fixed_cow_array_map();
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Serializer* startup_serializer_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(PartialSerializer);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class StartupSerializer : public Serializer {
|
|
|
|
public:
|
2013-09-03 11:54:08 +00:00
|
|
|
StartupSerializer(Isolate* isolate, SnapshotByteSink* sink)
|
|
|
|
: Serializer(isolate, sink) {
|
2010-01-27 08:25:48 +00:00
|
|
|
// Clear the cache of objects used by the partial snapshot. After the
|
|
|
|
// strong roots have been serialized we can create a partial snapshot
|
2011-10-20 12:27:10 +00:00
|
|
|
// which will repopulate the cache with objects needed by that partial
|
2010-01-27 08:25:48 +00:00
|
|
|
// snapshot.
|
2013-09-03 11:54:08 +00:00
|
|
|
isolate->set_serialize_partial_snapshot_cache_length(0);
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
|
|
|
// Serialize the current state of the heap. The order is:
|
|
|
|
// 1) Strong references.
|
|
|
|
// 2) Partial snapshot cache.
|
2013-02-28 17:03:34 +00:00
|
|
|
// 3) Weak references (e.g. the string table).
|
2010-01-27 08:25:48 +00:00
|
|
|
virtual void SerializeStrongReferences();
|
|
|
|
virtual void SerializeObject(Object* o,
|
2010-05-20 13:54:31 +00:00
|
|
|
HowToCode how_to_code,
|
2012-09-14 11:16:56 +00:00
|
|
|
WhereToPoint where_to_point,
|
|
|
|
int skip);
|
2010-01-27 08:25:48 +00:00
|
|
|
void SerializeWeakReferences();
|
|
|
|
void Serialize() {
|
|
|
|
SerializeStrongReferences();
|
|
|
|
SerializeWeakReferences();
|
2012-09-14 11:16:56 +00:00
|
|
|
Pad();
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_SERIALIZE_H_
|