OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/snapshot/code-serializer.h" |
| 6 |
| 7 #include "src/code-stubs.h" |
| 8 #include "src/log.h" |
| 9 #include "src/macro-assembler.h" |
| 10 #include "src/profiler/cpu-profiler.h" |
| 11 #include "src/snapshot/deserializer.h" |
| 12 #include "src/version.h" |
| 13 |
| 14 namespace v8 { |
| 15 namespace internal { |
| 16 |
| 17 ScriptData* CodeSerializer::Serialize(Isolate* isolate, |
| 18 Handle<SharedFunctionInfo> info, |
| 19 Handle<String> source) { |
| 20 base::ElapsedTimer timer; |
| 21 if (FLAG_profile_deserialization) timer.Start(); |
| 22 if (FLAG_trace_serializer) { |
| 23 PrintF("[Serializing from"); |
| 24 Object* script = info->script(); |
| 25 if (script->IsScript()) Script::cast(script)->name()->ShortPrint(); |
| 26 PrintF("]\n"); |
| 27 } |
| 28 |
| 29 // Serialize code object. |
| 30 SnapshotByteSink sink(info->code()->CodeSize() * 2); |
| 31 CodeSerializer cs(isolate, &sink, *source); |
| 32 DisallowHeapAllocation no_gc; |
| 33 Object** location = Handle<Object>::cast(info).location(); |
| 34 cs.VisitPointer(location); |
| 35 cs.SerializeDeferredObjects(); |
| 36 cs.Pad(); |
| 37 |
| 38 SerializedCodeData data(sink.data(), cs); |
| 39 ScriptData* script_data = data.GetScriptData(); |
| 40 |
| 41 if (FLAG_profile_deserialization) { |
| 42 double ms = timer.Elapsed().InMillisecondsF(); |
| 43 int length = script_data->length(); |
| 44 PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms); |
| 45 } |
| 46 |
| 47 return script_data; |
| 48 } |
| 49 |
| 50 void CodeSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, |
| 51 WhereToPoint where_to_point, int skip) { |
| 52 int root_index = root_index_map_.Lookup(obj); |
| 53 if (root_index != RootIndexMap::kInvalidRootIndex) { |
| 54 PutRoot(root_index, obj, how_to_code, where_to_point, skip); |
| 55 return; |
| 56 } |
| 57 |
| 58 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; |
| 59 |
| 60 FlushSkip(skip); |
| 61 |
| 62 if (obj->IsCode()) { |
| 63 Code* code_object = Code::cast(obj); |
| 64 switch (code_object->kind()) { |
| 65 case Code::OPTIMIZED_FUNCTION: // No optimized code compiled yet. |
| 66 case Code::HANDLER: // No handlers patched in yet. |
| 67 case Code::REGEXP: // No regexp literals initialized yet. |
| 68 case Code::NUMBER_OF_KINDS: // Pseudo enum value. |
| 69 CHECK(false); |
| 70 case Code::BUILTIN: |
| 71 SerializeBuiltin(code_object->builtin_index(), how_to_code, |
| 72 where_to_point); |
| 73 return; |
| 74 case Code::STUB: |
| 75 SerializeCodeStub(code_object->stub_key(), how_to_code, where_to_point); |
| 76 return; |
| 77 #define IC_KIND_CASE(KIND) case Code::KIND: |
| 78 IC_KIND_LIST(IC_KIND_CASE) |
| 79 #undef IC_KIND_CASE |
| 80 SerializeIC(code_object, how_to_code, where_to_point); |
| 81 return; |
| 82 case Code::FUNCTION: |
| 83 DCHECK(code_object->has_reloc_info_for_serialization()); |
| 84 SerializeGeneric(code_object, how_to_code, where_to_point); |
| 85 return; |
| 86 case Code::WASM_FUNCTION: |
| 87 UNREACHABLE(); |
| 88 } |
| 89 UNREACHABLE(); |
| 90 } |
| 91 |
| 92 // Past this point we should not see any (context-specific) maps anymore. |
| 93 CHECK(!obj->IsMap()); |
| 94 // There should be no references to the global object embedded. |
| 95 CHECK(!obj->IsJSGlobalProxy() && !obj->IsJSGlobalObject()); |
| 96 // There should be no hash table embedded. They would require rehashing. |
| 97 CHECK(!obj->IsHashTable()); |
| 98 // We expect no instantiated function objects or contexts. |
| 99 CHECK(!obj->IsJSFunction() && !obj->IsContext()); |
| 100 |
| 101 SerializeGeneric(obj, how_to_code, where_to_point); |
| 102 } |
| 103 |
| 104 void CodeSerializer::SerializeGeneric(HeapObject* heap_object, |
| 105 HowToCode how_to_code, |
| 106 WhereToPoint where_to_point) { |
| 107 // Object has not yet been serialized. Serialize it here. |
| 108 ObjectSerializer serializer(this, heap_object, sink_, how_to_code, |
| 109 where_to_point); |
| 110 serializer.Serialize(); |
| 111 } |
| 112 |
| 113 void CodeSerializer::SerializeBuiltin(int builtin_index, HowToCode how_to_code, |
| 114 WhereToPoint where_to_point) { |
| 115 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) || |
| 116 (how_to_code == kPlain && where_to_point == kInnerPointer) || |
| 117 (how_to_code == kFromCode && where_to_point == kInnerPointer)); |
| 118 DCHECK_LT(builtin_index, Builtins::builtin_count); |
| 119 DCHECK_LE(0, builtin_index); |
| 120 |
| 121 if (FLAG_trace_serializer) { |
| 122 PrintF(" Encoding builtin: %s\n", |
| 123 isolate()->builtins()->name(builtin_index)); |
| 124 } |
| 125 |
| 126 sink_->Put(kBuiltin + how_to_code + where_to_point, "Builtin"); |
| 127 sink_->PutInt(builtin_index, "builtin_index"); |
| 128 } |
| 129 |
| 130 void CodeSerializer::SerializeCodeStub(uint32_t stub_key, HowToCode how_to_code, |
| 131 WhereToPoint where_to_point) { |
| 132 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) || |
| 133 (how_to_code == kPlain && where_to_point == kInnerPointer) || |
| 134 (how_to_code == kFromCode && where_to_point == kInnerPointer)); |
| 135 DCHECK(CodeStub::MajorKeyFromKey(stub_key) != CodeStub::NoCache); |
| 136 DCHECK(!CodeStub::GetCode(isolate(), stub_key).is_null()); |
| 137 |
| 138 int index = AddCodeStubKey(stub_key) + kCodeStubsBaseIndex; |
| 139 |
| 140 if (FLAG_trace_serializer) { |
| 141 PrintF(" Encoding code stub %s as %d\n", |
| 142 CodeStub::MajorName(CodeStub::MajorKeyFromKey(stub_key)), index); |
| 143 } |
| 144 |
| 145 sink_->Put(kAttachedReference + how_to_code + where_to_point, "CodeStub"); |
| 146 sink_->PutInt(index, "CodeStub key"); |
| 147 } |
| 148 |
| 149 void CodeSerializer::SerializeIC(Code* ic, HowToCode how_to_code, |
| 150 WhereToPoint where_to_point) { |
| 151 // The IC may be implemented as a stub. |
| 152 uint32_t stub_key = ic->stub_key(); |
| 153 if (stub_key != CodeStub::NoCacheKey()) { |
| 154 if (FLAG_trace_serializer) { |
| 155 PrintF(" %s is a code stub\n", Code::Kind2String(ic->kind())); |
| 156 } |
| 157 SerializeCodeStub(stub_key, how_to_code, where_to_point); |
| 158 return; |
| 159 } |
| 160 // The IC may be implemented as builtin. Only real builtins have an |
| 161 // actual builtin_index value attached (otherwise it's just garbage). |
| 162 // Compare to make sure we are really dealing with a builtin. |
| 163 int builtin_index = ic->builtin_index(); |
| 164 if (builtin_index < Builtins::builtin_count) { |
| 165 Builtins::Name name = static_cast<Builtins::Name>(builtin_index); |
| 166 Code* builtin = isolate()->builtins()->builtin(name); |
| 167 if (builtin == ic) { |
| 168 if (FLAG_trace_serializer) { |
| 169 PrintF(" %s is a builtin\n", Code::Kind2String(ic->kind())); |
| 170 } |
| 171 DCHECK(ic->kind() == Code::KEYED_LOAD_IC || |
| 172 ic->kind() == Code::KEYED_STORE_IC); |
| 173 SerializeBuiltin(builtin_index, how_to_code, where_to_point); |
| 174 return; |
| 175 } |
| 176 } |
| 177 // The IC may also just be a piece of code kept in the non_monomorphic_cache. |
| 178 // In that case, just serialize as a normal code object. |
| 179 if (FLAG_trace_serializer) { |
| 180 PrintF(" %s has no special handling\n", Code::Kind2String(ic->kind())); |
| 181 } |
| 182 DCHECK(ic->kind() == Code::LOAD_IC || ic->kind() == Code::STORE_IC); |
| 183 SerializeGeneric(ic, how_to_code, where_to_point); |
| 184 } |
| 185 |
| 186 int CodeSerializer::AddCodeStubKey(uint32_t stub_key) { |
| 187 // TODO(yangguo) Maybe we need a hash table for a faster lookup than O(n^2). |
| 188 int index = 0; |
| 189 while (index < stub_keys_.length()) { |
| 190 if (stub_keys_[index] == stub_key) return index; |
| 191 index++; |
| 192 } |
| 193 stub_keys_.Add(stub_key); |
| 194 return index; |
| 195 } |
| 196 |
| 197 MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize( |
| 198 Isolate* isolate, ScriptData* cached_data, Handle<String> source) { |
| 199 base::ElapsedTimer timer; |
| 200 if (FLAG_profile_deserialization) timer.Start(); |
| 201 |
| 202 HandleScope scope(isolate); |
| 203 |
| 204 base::SmartPointer<SerializedCodeData> scd( |
| 205 SerializedCodeData::FromCachedData(isolate, cached_data, *source)); |
| 206 if (scd.is_empty()) { |
| 207 if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n"); |
| 208 DCHECK(cached_data->rejected()); |
| 209 return MaybeHandle<SharedFunctionInfo>(); |
| 210 } |
| 211 |
| 212 // Prepare and register list of attached objects. |
| 213 Vector<const uint32_t> code_stub_keys = scd->CodeStubKeys(); |
| 214 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New( |
| 215 code_stub_keys.length() + kCodeStubsBaseIndex); |
| 216 attached_objects[kSourceObjectIndex] = source; |
| 217 for (int i = 0; i < code_stub_keys.length(); i++) { |
| 218 attached_objects[i + kCodeStubsBaseIndex] = |
| 219 CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked(); |
| 220 } |
| 221 |
| 222 Deserializer deserializer(scd.get()); |
| 223 deserializer.SetAttachedObjects(attached_objects); |
| 224 |
| 225 // Deserialize. |
| 226 Handle<SharedFunctionInfo> result; |
| 227 if (!deserializer.DeserializeCode(isolate).ToHandle(&result)) { |
| 228 // Deserializing may fail if the reservations cannot be fulfilled. |
| 229 if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n"); |
| 230 return MaybeHandle<SharedFunctionInfo>(); |
| 231 } |
| 232 |
| 233 if (FLAG_profile_deserialization) { |
| 234 double ms = timer.Elapsed().InMillisecondsF(); |
| 235 int length = cached_data->length(); |
| 236 PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms); |
| 237 } |
| 238 result->set_deserialized(true); |
| 239 |
| 240 if (isolate->logger()->is_logging_code_events() || |
| 241 isolate->cpu_profiler()->is_profiling()) { |
| 242 String* name = isolate->heap()->empty_string(); |
| 243 if (result->script()->IsScript()) { |
| 244 Script* script = Script::cast(result->script()); |
| 245 if (script->name()->IsString()) name = String::cast(script->name()); |
| 246 } |
| 247 isolate->logger()->CodeCreateEvent( |
| 248 Logger::SCRIPT_TAG, result->abstract_code(), *result, NULL, name); |
| 249 } |
| 250 return scope.CloseAndEscape(result); |
| 251 } |
| 252 |
| 253 class Checksum { |
| 254 public: |
| 255 explicit Checksum(Vector<const byte> payload) { |
| 256 #ifdef MEMORY_SANITIZER |
| 257 // Computing the checksum includes padding bytes for objects like strings. |
| 258 // Mark every object as initialized in the code serializer. |
| 259 MSAN_MEMORY_IS_INITIALIZED(payload.start(), payload.length()); |
| 260 #endif // MEMORY_SANITIZER |
| 261 // Fletcher's checksum. Modified to reduce 64-bit sums to 32-bit. |
| 262 uintptr_t a = 1; |
| 263 uintptr_t b = 0; |
| 264 const uintptr_t* cur = reinterpret_cast<const uintptr_t*>(payload.start()); |
| 265 DCHECK(IsAligned(payload.length(), kIntptrSize)); |
| 266 const uintptr_t* end = cur + payload.length() / kIntptrSize; |
| 267 while (cur < end) { |
| 268 // Unsigned overflow expected and intended. |
| 269 a += *cur++; |
| 270 b += a; |
| 271 } |
| 272 #if V8_HOST_ARCH_64_BIT |
| 273 a ^= a >> 32; |
| 274 b ^= b >> 32; |
| 275 #endif // V8_HOST_ARCH_64_BIT |
| 276 a_ = static_cast<uint32_t>(a); |
| 277 b_ = static_cast<uint32_t>(b); |
| 278 } |
| 279 |
| 280 bool Check(uint32_t a, uint32_t b) const { return a == a_ && b == b_; } |
| 281 |
| 282 uint32_t a() const { return a_; } |
| 283 uint32_t b() const { return b_; } |
| 284 |
| 285 private: |
| 286 uint32_t a_; |
| 287 uint32_t b_; |
| 288 |
| 289 DISALLOW_COPY_AND_ASSIGN(Checksum); |
| 290 }; |
| 291 |
| 292 SerializedCodeData::SerializedCodeData(const List<byte>& payload, |
| 293 const CodeSerializer& cs) { |
| 294 DisallowHeapAllocation no_gc; |
| 295 const List<uint32_t>* stub_keys = cs.stub_keys(); |
| 296 |
| 297 List<Reservation> reservations; |
| 298 cs.EncodeReservations(&reservations); |
| 299 |
| 300 // Calculate sizes. |
| 301 int reservation_size = reservations.length() * kInt32Size; |
| 302 int num_stub_keys = stub_keys->length(); |
| 303 int stub_keys_size = stub_keys->length() * kInt32Size; |
| 304 int payload_offset = kHeaderSize + reservation_size + stub_keys_size; |
| 305 int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); |
| 306 int size = padded_payload_offset + payload.length(); |
| 307 |
| 308 // Allocate backing store and create result data. |
| 309 AllocateData(size); |
| 310 |
| 311 // Set header values. |
| 312 SetMagicNumber(cs.isolate()); |
| 313 SetHeaderValue(kVersionHashOffset, Version::Hash()); |
| 314 SetHeaderValue(kSourceHashOffset, SourceHash(cs.source())); |
| 315 SetHeaderValue(kCpuFeaturesOffset, |
| 316 static_cast<uint32_t>(CpuFeatures::SupportedFeatures())); |
| 317 SetHeaderValue(kFlagHashOffset, FlagList::Hash()); |
| 318 SetHeaderValue(kNumReservationsOffset, reservations.length()); |
| 319 SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys); |
| 320 SetHeaderValue(kPayloadLengthOffset, payload.length()); |
| 321 |
| 322 Checksum checksum(payload.ToConstVector()); |
| 323 SetHeaderValue(kChecksum1Offset, checksum.a()); |
| 324 SetHeaderValue(kChecksum2Offset, checksum.b()); |
| 325 |
| 326 // Copy reservation chunk sizes. |
| 327 CopyBytes(data_ + kHeaderSize, reinterpret_cast<byte*>(reservations.begin()), |
| 328 reservation_size); |
| 329 |
| 330 // Copy code stub keys. |
| 331 CopyBytes(data_ + kHeaderSize + reservation_size, |
| 332 reinterpret_cast<byte*>(stub_keys->begin()), stub_keys_size); |
| 333 |
| 334 memset(data_ + payload_offset, 0, padded_payload_offset - payload_offset); |
| 335 |
| 336 // Copy serialized data. |
| 337 CopyBytes(data_ + padded_payload_offset, payload.begin(), |
| 338 static_cast<size_t>(payload.length())); |
| 339 } |
| 340 |
| 341 SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck( |
| 342 Isolate* isolate, String* source) const { |
| 343 uint32_t magic_number = GetMagicNumber(); |
| 344 if (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH; |
| 345 uint32_t version_hash = GetHeaderValue(kVersionHashOffset); |
| 346 uint32_t source_hash = GetHeaderValue(kSourceHashOffset); |
| 347 uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset); |
| 348 uint32_t flags_hash = GetHeaderValue(kFlagHashOffset); |
| 349 uint32_t c1 = GetHeaderValue(kChecksum1Offset); |
| 350 uint32_t c2 = GetHeaderValue(kChecksum2Offset); |
| 351 if (version_hash != Version::Hash()) return VERSION_MISMATCH; |
| 352 if (source_hash != SourceHash(source)) return SOURCE_MISMATCH; |
| 353 if (cpu_features != static_cast<uint32_t>(CpuFeatures::SupportedFeatures())) { |
| 354 return CPU_FEATURES_MISMATCH; |
| 355 } |
| 356 if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH; |
| 357 if (!Checksum(Payload()).Check(c1, c2)) return CHECKSUM_MISMATCH; |
| 358 return CHECK_SUCCESS; |
| 359 } |
| 360 |
| 361 uint32_t SerializedCodeData::SourceHash(String* source) const { |
| 362 return source->length(); |
| 363 } |
| 364 |
| 365 // Return ScriptData object and relinquish ownership over it to the caller. |
| 366 ScriptData* SerializedCodeData::GetScriptData() { |
| 367 DCHECK(owns_data_); |
| 368 ScriptData* result = new ScriptData(data_, size_); |
| 369 result->AcquireDataOwnership(); |
| 370 owns_data_ = false; |
| 371 data_ = NULL; |
| 372 return result; |
| 373 } |
| 374 |
| 375 Vector<const SerializedData::Reservation> SerializedCodeData::Reservations() |
| 376 const { |
| 377 return Vector<const Reservation>( |
| 378 reinterpret_cast<const Reservation*>(data_ + kHeaderSize), |
| 379 GetHeaderValue(kNumReservationsOffset)); |
| 380 } |
| 381 |
| 382 Vector<const byte> SerializedCodeData::Payload() const { |
| 383 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; |
| 384 int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size; |
| 385 int payload_offset = kHeaderSize + reservations_size + code_stubs_size; |
| 386 int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); |
| 387 const byte* payload = data_ + padded_payload_offset; |
| 388 DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment)); |
| 389 int length = GetHeaderValue(kPayloadLengthOffset); |
| 390 DCHECK_EQ(data_ + size_, payload + length); |
| 391 return Vector<const byte>(payload, length); |
| 392 } |
| 393 |
| 394 Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const { |
| 395 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; |
| 396 const byte* start = data_ + kHeaderSize + reservations_size; |
| 397 return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start), |
| 398 GetHeaderValue(kNumCodeStubKeysOffset)); |
| 399 } |
| 400 |
| 401 SerializedCodeData::SerializedCodeData(ScriptData* data) |
| 402 : SerializedData(const_cast<byte*>(data->data()), data->length()) {} |
| 403 |
| 404 SerializedCodeData* SerializedCodeData::FromCachedData(Isolate* isolate, |
| 405 ScriptData* cached_data, |
| 406 String* source) { |
| 407 DisallowHeapAllocation no_gc; |
| 408 SerializedCodeData* scd = new SerializedCodeData(cached_data); |
| 409 SanityCheckResult r = scd->SanityCheck(isolate, source); |
| 410 if (r == CHECK_SUCCESS) return scd; |
| 411 cached_data->Reject(); |
| 412 source->GetIsolate()->counters()->code_cache_reject_reason()->AddSample(r); |
| 413 delete scd; |
| 414 return NULL; |
| 415 } |
| 416 |
| 417 } // namespace internal |
| 418 } // namespace v8 |
OLD | NEW |