| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/snapshot_utils.h" | 5 #include "bin/snapshot_utils.h" |
| 6 | 6 |
| 7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "bin/error_exit.h" | 8 #include "bin/error_exit.h" |
| 9 #include "bin/extensions.h" | 9 #include "bin/extensions.h" |
| 10 #include "bin/file.h" | 10 #include "bin/file.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 MappedMemory* vm_data_mapping_; | 68 MappedMemory* vm_data_mapping_; |
| 69 MappedMemory* vm_instructions_mapping_; | 69 MappedMemory* vm_instructions_mapping_; |
| 70 MappedMemory* isolate_data_mapping_; | 70 MappedMemory* isolate_data_mapping_; |
| 71 MappedMemory* isolate_instructions_mapping_; | 71 MappedMemory* isolate_instructions_mapping_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 | |
| 75 static AppSnapshot* TryReadAppSnapshotBlobs(const char* script_name) { | 74 static AppSnapshot* TryReadAppSnapshotBlobs(const char* script_name) { |
| 76 File* file = File::Open(script_name, File::kRead); | 75 File* file = File::Open(script_name, File::kRead); |
| 77 if (file == NULL) { | 76 if (file == NULL) { |
| 78 return NULL; | 77 return NULL; |
| 79 } | 78 } |
| 80 if (file->Length() < kAppSnapshotHeaderSize) { | 79 if (file->Length() < kAppSnapshotHeaderSize) { |
| 81 file->Release(); | 80 file->Release(); |
| 82 return NULL; | 81 return NULL; |
| 83 } | 82 } |
| 84 int64_t header[5]; | 83 int64_t header[5]; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 if (isolate_instr_mapping == NULL) { | 146 if (isolate_instr_mapping == NULL) { |
| 148 FATAL1("Failed to memory map snapshot: %s\n", script_name); | 147 FATAL1("Failed to memory map snapshot: %s\n", script_name); |
| 149 } | 148 } |
| 150 } | 149 } |
| 151 | 150 |
| 152 file->Release(); | 151 file->Release(); |
| 153 return new MappedAppSnapshot(vm_data_mapping, vm_instr_mapping, | 152 return new MappedAppSnapshot(vm_data_mapping, vm_instr_mapping, |
| 154 isolate_data_mapping, isolate_instr_mapping); | 153 isolate_data_mapping, isolate_instr_mapping); |
| 155 } | 154 } |
| 156 | 155 |
| 157 | |
| 158 #if defined(DART_PRECOMPILED_RUNTIME) | 156 #if defined(DART_PRECOMPILED_RUNTIME) |
| 159 class DylibAppSnapshot : public AppSnapshot { | 157 class DylibAppSnapshot : public AppSnapshot { |
| 160 public: | 158 public: |
| 161 DylibAppSnapshot(void* library, | 159 DylibAppSnapshot(void* library, |
| 162 const uint8_t* vm_snapshot_data, | 160 const uint8_t* vm_snapshot_data, |
| 163 const uint8_t* vm_snapshot_instructions, | 161 const uint8_t* vm_snapshot_instructions, |
| 164 const uint8_t* isolate_snapshot_data, | 162 const uint8_t* isolate_snapshot_data, |
| 165 const uint8_t* isolate_snapshot_instructions) | 163 const uint8_t* isolate_snapshot_instructions) |
| 166 : library_(library), | 164 : library_(library), |
| 167 vm_snapshot_data_(vm_snapshot_data), | 165 vm_snapshot_data_(vm_snapshot_data), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 182 } | 180 } |
| 183 | 181 |
| 184 private: | 182 private: |
| 185 void* library_; | 183 void* library_; |
| 186 const uint8_t* vm_snapshot_data_; | 184 const uint8_t* vm_snapshot_data_; |
| 187 const uint8_t* vm_snapshot_instructions_; | 185 const uint8_t* vm_snapshot_instructions_; |
| 188 const uint8_t* isolate_snapshot_data_; | 186 const uint8_t* isolate_snapshot_data_; |
| 189 const uint8_t* isolate_snapshot_instructions_; | 187 const uint8_t* isolate_snapshot_instructions_; |
| 190 }; | 188 }; |
| 191 | 189 |
| 192 | |
| 193 static AppSnapshot* TryReadAppSnapshotDynamicLibrary(const char* script_name) { | 190 static AppSnapshot* TryReadAppSnapshotDynamicLibrary(const char* script_name) { |
| 194 void* library = Extensions::LoadExtensionLibrary(script_name); | 191 void* library = Extensions::LoadExtensionLibrary(script_name); |
| 195 if (library == NULL) { | 192 if (library == NULL) { |
| 196 return NULL; | 193 return NULL; |
| 197 } | 194 } |
| 198 | 195 |
| 199 const uint8_t* vm_data_buffer = reinterpret_cast<const uint8_t*>( | 196 const uint8_t* vm_data_buffer = reinterpret_cast<const uint8_t*>( |
| 200 Extensions::ResolveSymbol(library, kVmSnapshotDataSymbolName)); | 197 Extensions::ResolveSymbol(library, kVmSnapshotDataSymbolName)); |
| 201 if (vm_data_buffer == NULL) { | 198 if (vm_data_buffer == NULL) { |
| 202 FATAL1("Failed to resolve symbol '%s'\n", kVmSnapshotDataSymbolName); | 199 FATAL1("Failed to resolve symbol '%s'\n", kVmSnapshotDataSymbolName); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 221 if (isolate_instructions_buffer == NULL) { | 218 if (isolate_instructions_buffer == NULL) { |
| 222 FATAL1("Failed to resolve symbol '%s'\n", | 219 FATAL1("Failed to resolve symbol '%s'\n", |
| 223 kIsolateSnapshotInstructionsSymbolName); | 220 kIsolateSnapshotInstructionsSymbolName); |
| 224 } | 221 } |
| 225 | 222 |
| 226 return new DylibAppSnapshot(library, vm_data_buffer, vm_instructions_buffer, | 223 return new DylibAppSnapshot(library, vm_data_buffer, vm_instructions_buffer, |
| 227 isolate_data_buffer, isolate_instructions_buffer); | 224 isolate_data_buffer, isolate_instructions_buffer); |
| 228 } | 225 } |
| 229 #endif // defined(DART_PRECOMPILED_RUNTIME) | 226 #endif // defined(DART_PRECOMPILED_RUNTIME) |
| 230 | 227 |
| 231 | |
| 232 AppSnapshot* Snapshot::TryReadAppSnapshot(const char* script_name) { | 228 AppSnapshot* Snapshot::TryReadAppSnapshot(const char* script_name) { |
| 233 if (File::GetType(script_name, true) != File::kIsFile) { | 229 if (File::GetType(script_name, true) != File::kIsFile) { |
| 234 // If 'script_name' refers to a pipe, don't read to check for an app | 230 // If 'script_name' refers to a pipe, don't read to check for an app |
| 235 // snapshot since we cannot rewind if it isn't (and couldn't mmap it in | 231 // snapshot since we cannot rewind if it isn't (and couldn't mmap it in |
| 236 // anyway if it was). | 232 // anyway if it was). |
| 237 return NULL; | 233 return NULL; |
| 238 } | 234 } |
| 239 AppSnapshot* snapshot = TryReadAppSnapshotBlobs(script_name); | 235 AppSnapshot* snapshot = TryReadAppSnapshotBlobs(script_name); |
| 240 if (snapshot != NULL) { | 236 if (snapshot != NULL) { |
| 241 return snapshot; | 237 return snapshot; |
| 242 } | 238 } |
| 243 #if defined(DART_PRECOMPILED_RUNTIME) | 239 #if defined(DART_PRECOMPILED_RUNTIME) |
| 244 // For testing AOT with the standalone embedder, we also support loading | 240 // For testing AOT with the standalone embedder, we also support loading |
| 245 // from a dynamic library to simulate what happens on iOS. | 241 // from a dynamic library to simulate what happens on iOS. |
| 246 snapshot = TryReadAppSnapshotDynamicLibrary(script_name); | 242 snapshot = TryReadAppSnapshotDynamicLibrary(script_name); |
| 247 if (snapshot != NULL) { | 243 if (snapshot != NULL) { |
| 248 return snapshot; | 244 return snapshot; |
| 249 } | 245 } |
| 250 #endif // defined(DART_PRECOMPILED_RUNTIME) | 246 #endif // defined(DART_PRECOMPILED_RUNTIME) |
| 251 return NULL; | 247 return NULL; |
| 252 } | 248 } |
| 253 | 249 |
| 254 | |
| 255 static void WriteSnapshotFile(const char* filename, | 250 static void WriteSnapshotFile(const char* filename, |
| 256 bool write_magic_number, | 251 bool write_magic_number, |
| 257 const uint8_t* buffer, | 252 const uint8_t* buffer, |
| 258 const intptr_t size) { | 253 const intptr_t size) { |
| 259 File* file = File::Open(filename, File::kWriteTruncate); | 254 File* file = File::Open(filename, File::kWriteTruncate); |
| 260 if (file == NULL) { | 255 if (file == NULL) { |
| 261 ErrorExit(kErrorExitCode, "Unable to open file %s for writing snapshot\n", | 256 ErrorExit(kErrorExitCode, "Unable to open file %s for writing snapshot\n", |
| 262 filename); | 257 filename); |
| 263 } | 258 } |
| 264 | 259 |
| 265 if (write_magic_number) { | 260 if (write_magic_number) { |
| 266 // Write the magic number to indicate file is a script snapshot. | 261 // Write the magic number to indicate file is a script snapshot. |
| 267 DartUtils::WriteMagicNumber(file); | 262 DartUtils::WriteMagicNumber(file); |
| 268 } | 263 } |
| 269 | 264 |
| 270 if (!file->WriteFully(buffer, size)) { | 265 if (!file->WriteFully(buffer, size)) { |
| 271 ErrorExit(kErrorExitCode, "Unable to write file %s for writing snapshot\n", | 266 ErrorExit(kErrorExitCode, "Unable to write file %s for writing snapshot\n", |
| 272 filename); | 267 filename); |
| 273 } | 268 } |
| 274 file->Release(); | 269 file->Release(); |
| 275 } | 270 } |
| 276 | 271 |
| 277 | |
| 278 static bool WriteInt64(File* file, int64_t size) { | 272 static bool WriteInt64(File* file, int64_t size) { |
| 279 return file->WriteFully(&size, sizeof(size)); | 273 return file->WriteFully(&size, sizeof(size)); |
| 280 } | 274 } |
| 281 | 275 |
| 282 | |
| 283 static void WriteAppSnapshot(const char* filename, | 276 static void WriteAppSnapshot(const char* filename, |
| 284 uint8_t* vm_data_buffer, | 277 uint8_t* vm_data_buffer, |
| 285 intptr_t vm_data_size, | 278 intptr_t vm_data_size, |
| 286 uint8_t* vm_instructions_buffer, | 279 uint8_t* vm_instructions_buffer, |
| 287 intptr_t vm_instructions_size, | 280 intptr_t vm_instructions_size, |
| 288 uint8_t* isolate_data_buffer, | 281 uint8_t* isolate_data_buffer, |
| 289 intptr_t isolate_data_size, | 282 intptr_t isolate_data_size, |
| 290 uint8_t* isolate_instructions_buffer, | 283 uint8_t* isolate_instructions_buffer, |
| 291 intptr_t isolate_instructions_size) { | 284 intptr_t isolate_instructions_size) { |
| 292 File* file = File::Open(filename, File::kWriteTruncate); | 285 File* file = File::Open(filename, File::kWriteTruncate); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 isolate_instructions_size)) { | 318 isolate_instructions_size)) { |
| 326 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", | 319 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", |
| 327 filename); | 320 filename); |
| 328 } | 321 } |
| 329 } | 322 } |
| 330 | 323 |
| 331 file->Flush(); | 324 file->Flush(); |
| 332 file->Release(); | 325 file->Release(); |
| 333 } | 326 } |
| 334 | 327 |
| 335 | |
| 336 void Snapshot::GenerateScript(const char* snapshot_filename) { | 328 void Snapshot::GenerateScript(const char* snapshot_filename) { |
| 337 // First create a snapshot. | 329 // First create a snapshot. |
| 338 uint8_t* buffer = NULL; | 330 uint8_t* buffer = NULL; |
| 339 intptr_t size = 0; | 331 intptr_t size = 0; |
| 340 Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size); | 332 Dart_Handle result = Dart_CreateScriptSnapshot(&buffer, &size); |
| 341 if (Dart_IsError(result)) { | 333 if (Dart_IsError(result)) { |
| 342 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | 334 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
| 343 } | 335 } |
| 344 | 336 |
| 345 WriteSnapshotFile(snapshot_filename, true, buffer, size); | 337 WriteSnapshotFile(snapshot_filename, true, buffer, size); |
| 346 } | 338 } |
| 347 | 339 |
| 348 | |
| 349 void Snapshot::GenerateAppJIT(const char* snapshot_filename) { | 340 void Snapshot::GenerateAppJIT(const char* snapshot_filename) { |
| 350 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_DBC) | 341 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_DBC) |
| 351 // Snapshots with code are not supported on IA32 or DBC. | 342 // Snapshots with code are not supported on IA32 or DBC. |
| 352 uint8_t* isolate_buffer = NULL; | 343 uint8_t* isolate_buffer = NULL; |
| 353 intptr_t isolate_size = 0; | 344 intptr_t isolate_size = 0; |
| 354 | 345 |
| 355 Dart_Handle result = | 346 Dart_Handle result = |
| 356 Dart_CreateSnapshot(NULL, NULL, &isolate_buffer, &isolate_size); | 347 Dart_CreateSnapshot(NULL, NULL, &isolate_buffer, &isolate_size); |
| 357 if (Dart_IsError(result)) { | 348 if (Dart_IsError(result)) { |
| 358 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | 349 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 370 &isolate_instructions_size); | 361 &isolate_instructions_size); |
| 371 if (Dart_IsError(result)) { | 362 if (Dart_IsError(result)) { |
| 372 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | 363 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
| 373 } | 364 } |
| 374 WriteAppSnapshot(snapshot_filename, NULL, 0, NULL, 0, isolate_data_buffer, | 365 WriteAppSnapshot(snapshot_filename, NULL, 0, NULL, 0, isolate_data_buffer, |
| 375 isolate_data_size, isolate_instructions_buffer, | 366 isolate_data_size, isolate_instructions_buffer, |
| 376 isolate_instructions_size); | 367 isolate_instructions_size); |
| 377 #endif | 368 #endif |
| 378 } | 369 } |
| 379 | 370 |
| 380 | |
| 381 void Snapshot::GenerateAppAOTAsBlobs(const char* snapshot_filename) { | 371 void Snapshot::GenerateAppAOTAsBlobs(const char* snapshot_filename) { |
| 382 uint8_t* vm_data_buffer = NULL; | 372 uint8_t* vm_data_buffer = NULL; |
| 383 intptr_t vm_data_size = 0; | 373 intptr_t vm_data_size = 0; |
| 384 uint8_t* vm_instructions_buffer = NULL; | 374 uint8_t* vm_instructions_buffer = NULL; |
| 385 intptr_t vm_instructions_size = 0; | 375 intptr_t vm_instructions_size = 0; |
| 386 uint8_t* isolate_data_buffer = NULL; | 376 uint8_t* isolate_data_buffer = NULL; |
| 387 intptr_t isolate_data_size = 0; | 377 intptr_t isolate_data_size = 0; |
| 388 uint8_t* isolate_instructions_buffer = NULL; | 378 uint8_t* isolate_instructions_buffer = NULL; |
| 389 intptr_t isolate_instructions_size = 0; | 379 intptr_t isolate_instructions_size = 0; |
| 390 Dart_Handle result = Dart_CreateAppAOTSnapshotAsBlobs( | 380 Dart_Handle result = Dart_CreateAppAOTSnapshotAsBlobs( |
| 391 &vm_data_buffer, &vm_data_size, &vm_instructions_buffer, | 381 &vm_data_buffer, &vm_data_size, &vm_instructions_buffer, |
| 392 &vm_instructions_size, &isolate_data_buffer, &isolate_data_size, | 382 &vm_instructions_size, &isolate_data_buffer, &isolate_data_size, |
| 393 &isolate_instructions_buffer, &isolate_instructions_size); | 383 &isolate_instructions_buffer, &isolate_instructions_size); |
| 394 if (Dart_IsError(result)) { | 384 if (Dart_IsError(result)) { |
| 395 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | 385 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
| 396 } | 386 } |
| 397 WriteAppSnapshot(snapshot_filename, vm_data_buffer, vm_data_size, | 387 WriteAppSnapshot(snapshot_filename, vm_data_buffer, vm_data_size, |
| 398 vm_instructions_buffer, vm_instructions_size, | 388 vm_instructions_buffer, vm_instructions_size, |
| 399 isolate_data_buffer, isolate_data_size, | 389 isolate_data_buffer, isolate_data_size, |
| 400 isolate_instructions_buffer, isolate_instructions_size); | 390 isolate_instructions_buffer, isolate_instructions_size); |
| 401 } | 391 } |
| 402 | 392 |
| 403 | |
| 404 void Snapshot::GenerateAppAOTAsAssembly(const char* snapshot_filename) { | 393 void Snapshot::GenerateAppAOTAsAssembly(const char* snapshot_filename) { |
| 405 uint8_t* assembly_buffer = NULL; | 394 uint8_t* assembly_buffer = NULL; |
| 406 intptr_t assembly_size = 0; | 395 intptr_t assembly_size = 0; |
| 407 Dart_Handle result = | 396 Dart_Handle result = |
| 408 Dart_CreateAppAOTSnapshotAsAssembly(&assembly_buffer, &assembly_size); | 397 Dart_CreateAppAOTSnapshotAsAssembly(&assembly_buffer, &assembly_size); |
| 409 if (Dart_IsError(result)) { | 398 if (Dart_IsError(result)) { |
| 410 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); | 399 ErrorExit(kErrorExitCode, "%s\n", Dart_GetError(result)); |
| 411 } | 400 } |
| 412 WriteSnapshotFile(snapshot_filename, false, assembly_buffer, assembly_size); | 401 WriteSnapshotFile(snapshot_filename, false, assembly_buffer, assembly_size); |
| 413 } | 402 } |
| 414 | 403 |
| 415 } // namespace bin | 404 } // namespace bin |
| 416 } // namespace dart | 405 } // namespace dart |
| OLD | NEW |