Flutter Engine
The Flutter Engine
Static Public Member Functions | List of all members
dart::bin::Snapshot Class Reference

#include <snapshot_utils.h>

Static Public Member Functions

static void GenerateKernel (const char *snapshot_filename, const char *script_name, const char *package_config)
 
static void GenerateAppJIT (const char *snapshot_filename)
 
static void GenerateAppAOTAsAssembly (const char *snapshot_filename)
 
static AppSnapshotTryReadAppendedAppSnapshotElf (const char *container_path)
 
static AppSnapshotTryReadAppSnapshot (const char *script_uri, bool force_load_elf_from_memory=false, bool decode_uri=true)
 
static void WriteAppSnapshot (const char *filename, uint8_t *isolate_data_buffer, intptr_t isolate_data_size, uint8_t *isolate_instructions_buffer, intptr_t isolate_instructions_size)
 

Detailed Description

Definition at line 41 of file snapshot_utils.h.

Member Function Documentation

◆ GenerateAppAOTAsAssembly()

void dart::bin::Snapshot::GenerateAppAOTAsAssembly ( const char *  snapshot_filename)
static

Definition at line 762 of file snapshot_utils.cc.

762 {
763 File* file = File::Open(nullptr, snapshot_filename, File::kWriteTruncate);
764 RefCntReleaseScope<File> rs(file);
765 if (file == nullptr) {
766 ErrorExit(kErrorExitCode, "Unable to open file %s for writing snapshot\n",
767 snapshot_filename);
768 }
770 StreamingWriteCallback, file, /*strip=*/false,
771 /*debug_callback_data=*/nullptr);
772 if (Dart_IsError(result)) {
774 }
775}
static File * Open(Namespace *namespc, const char *path, FileOpenMode mode)
@ kWriteTruncate
Definition: file.h:60
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
GAsyncResult * result
static void StreamingWriteCallback(void *callback_data, const uint8_t *buffer, intptr_t size)
constexpr int kErrorExitCode
Definition: error_exit.h:18
void ErrorExit(int exit_code, const char *format,...)
Definition: error_exit.cc:18
DART_EXPORT const char * Dart_GetError(Dart_Handle handle)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_CreateAppAOTSnapshotAsAssembly(Dart_StreamingWriteCallback callback, void *callback_data, bool strip, void *debug_callback_data)

◆ GenerateAppJIT()

void dart::bin::Snapshot::GenerateAppJIT ( const char *  snapshot_filename)
static

Definition at line 724 of file snapshot_utils.cc.

724 {
725#if defined(TARGET_ARCH_IA32)
726 // Snapshots with code are not supported on IA32.
727 uint8_t* isolate_buffer = nullptr;
728 intptr_t isolate_size = 0;
729
730 Dart_Handle result = Dart_CreateSnapshot(nullptr, nullptr, &isolate_buffer,
731 &isolate_size, /*is_core=*/false);
732 if (Dart_IsError(result)) {
734 }
735
736 WriteAppSnapshot(snapshot_filename, isolate_buffer, isolate_size, nullptr, 0);
737#else
738 uint8_t* isolate_data_buffer = nullptr;
739 intptr_t isolate_data_size = 0;
740 uint8_t* isolate_instructions_buffer = nullptr;
741 intptr_t isolate_instructions_size = 0;
743 &isolate_data_buffer, &isolate_data_size, &isolate_instructions_buffer,
744 &isolate_instructions_size);
745 if (Dart_IsError(result)) {
747 }
748 WriteAppSnapshot(snapshot_filename, isolate_data_buffer, isolate_data_size,
749 isolate_instructions_buffer, isolate_instructions_size);
750#endif
751}
static void WriteAppSnapshot(const char *filename, uint8_t *isolate_data_buffer, intptr_t isolate_data_size, uint8_t *isolate_instructions_buffer, intptr_t isolate_instructions_size)
DART_EXPORT Dart_Handle Dart_CreateAppJITSnapshotAsBlobs(uint8_t **isolate_snapshot_data_buffer, intptr_t *isolate_snapshot_data_size, uint8_t **isolate_snapshot_instructions_buffer, intptr_t *isolate_snapshot_instructions_size)
DART_EXPORT Dart_Handle Dart_CreateSnapshot(uint8_t **vm_snapshot_data_buffer, intptr_t *vm_snapshot_data_size, uint8_t **isolate_snapshot_data_buffer, intptr_t *isolate_snapshot_data_size, bool is_core)

◆ GenerateKernel()

void dart::bin::Snapshot::GenerateKernel ( const char *  snapshot_filename,
const char *  script_name,
const char *  package_config 
)
static

Definition at line 696 of file snapshot_utils.cc.

698 {
699#if !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) && !defined(TESTING)
700 ASSERT(Dart_CurrentIsolate() == nullptr);
701
702 uint8_t* kernel_buffer = nullptr;
703 intptr_t kernel_buffer_size = 0;
704 dfe.ReadScript(script_name, nullptr, &kernel_buffer, &kernel_buffer_size);
705 if (kernel_buffer != nullptr) {
706 WriteSnapshotFile(snapshot_filename, kernel_buffer, kernel_buffer_size);
707 free(kernel_buffer);
708 } else {
710 dfe.CompileScript(script_name, /*incremental*/ false, package_config,
711 /*snapshot=*/true, /*embedd_sources=*/true);
713 Syslog::PrintErr("%s\n", result.error);
715 }
716 WriteSnapshotFile(snapshot_filename, result.kernel, result.kernel_size);
717 free(result.kernel);
718 }
719#else
720 UNREACHABLE();
721#endif // !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) && !defined(TESTING)
722}
#define UNREACHABLE()
Definition: assert.h:248
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
Dart_KernelCompilationResult CompileScript(const char *script_uri, bool incremental, const char *package_config, bool for_snapshot, bool embedd_sources)
Definition: dfe.cc:189
void ReadScript(const char *script_uri, const AppSnapshot *app_snapshot, uint8_t **kernel_buffer, intptr_t *kernel_buffer_size, bool decode_uri=true, std::shared_ptr< uint8_t > *kernel_blob_ptr=nullptr)
Definition: dfe.cc:241
static DART_NORETURN void Exit(int exit_code)
@ Dart_KernelCompilationStatus_Ok
Definition: dart_api.h:3785
#define ASSERT(E)
constexpr int kCompilationErrorExitCode
Definition: error_exit.h:16
DFE dfe
Definition: dfe.cc:59
static void WriteSnapshotFile(const char *filename, const uint8_t *buffer, const intptr_t size)
DART_EXPORT Dart_Isolate Dart_CurrentIsolate()

◆ TryReadAppendedAppSnapshotElf()

static AppSnapshot * dart::bin::Snapshot::TryReadAppendedAppSnapshotElf ( const char *  container_path)
static

◆ TryReadAppSnapshot()

AppSnapshot * dart::bin::Snapshot::TryReadAppSnapshot ( const char *  script_uri,
bool  force_load_elf_from_memory = false,
bool  decode_uri = true 
)
static

Definition at line 561 of file snapshot_utils.cc.

563 {
564 CStringUniquePtr decoded_path(nullptr);
565 const char* script_name = nullptr;
566 if (decode_uri) {
567 decoded_path = File::UriToPath(script_uri);
568 if (decoded_path == nullptr) {
569 return nullptr;
570 }
571 script_name = decoded_path.get();
572 } else {
573 script_name = script_uri;
574 }
575 if (File::GetType(nullptr, script_name, true) != File::kIsFile) {
576 // If 'script_name' refers to a pipe, don't read to check for an app
577 // snapshot since we cannot rewind if it isn't (and couldn't mmap it in
578 // anyway if it was).
579 return nullptr;
580 }
581 File* file = File::Open(nullptr, script_name, File::kRead);
582 if (file == nullptr) {
583 return nullptr;
584 }
585 RefCntReleaseScope<File> rs(file);
586 if ((file->Length() - file->Position()) < DartUtils::kMaxMagicNumberSize) {
587 return nullptr;
588 }
589
592 if (!file->ReadFully(&header, DartUtils::kMaxMagicNumberSize)) {
593 return nullptr;
594 }
595 DartUtils::MagicNumber magic_number =
597 if (magic_number == DartUtils::kAppJITMagicNumber) {
598 // Return the JIT snapshot.
599 return TryReadAppSnapshotBlobs(script_name, file);
600 }
601#if defined(DART_PRECOMPILED_RUNTIME)
602 if (!DartUtils::IsAotMagicNumber(magic_number)) {
603 return nullptr;
604 }
605
606 // For testing AOT with the standalone embedder, we also support loading
607 // from a dynamic library to simulate what happens on iOS.
608
609#if defined(DART_TARGET_OS_LINUX) || defined(DART_TARGET_OS_MACOS)
610 // On Linux and OSX, resolve the script path before passing into dlopen()
611 // since dlopen will not search the filesystem for paths like 'libtest.so'.
612 CStringUniquePtr absolute_path(realpath(script_name, nullptr));
613 script_name = absolute_path.get();
614#endif
615
616 AppSnapshot* snapshot = nullptr;
617 if (!force_load_elf_from_memory) {
618 snapshot = TryReadAppSnapshotDynamicLibrary(script_name);
619 if (snapshot != nullptr) {
620 return snapshot;
621 }
622 }
623 return TryReadAppSnapshotElf(script_name, /*file_offset=*/0,
624 force_load_elf_from_memory);
625#else
626 // We create a dummy snapshot object just to remember the type which
627 // has already been identified by sniffing the magic number.
628 return new DummySnapshot(magic_number);
629#endif // defined(DART_PRECOMPILED_RUNTIME)
630
631 return nullptr;
632}
static MagicNumber SniffForMagicNumber(const char *filename)
Definition: dartutils.cc:403
static bool IsAotMagicNumber(MagicNumber number)
Definition: dartutils.h:275
static constexpr int64_t kMaxMagicNumberSize
Definition: dartutils.h:271
static CStringUniquePtr UriToPath(const char *uri)
static Type GetType(Namespace *namespc, const char *path, bool follow_links)
static AppSnapshot * TryReadAppSnapshotBlobs(const char *script_name, File *file)
CAllocUniquePtr< char > CStringUniquePtr
Definition: utils.h:31
static const char header[]
Definition: skpbench.cpp:88

◆ WriteAppSnapshot()

void dart::bin::Snapshot::WriteAppSnapshot ( const char *  filename,
uint8_t *  isolate_data_buffer,
intptr_t  isolate_data_size,
uint8_t *  isolate_instructions_buffer,
intptr_t  isolate_instructions_size 
)
static

Definition at line 656 of file snapshot_utils.cc.

660 {
661 File* file = File::Open(nullptr, filename, File::kWriteTruncate);
662 if (file == nullptr) {
663 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", filename);
664 }
665
667 WriteInt64(file, isolate_data_size);
668 WriteInt64(file, isolate_instructions_size);
669 ASSERT(file->Position() ==
671
672 file->SetPosition(Utils::RoundUp(file->Position(), kAppSnapshotPageSize));
674 Syslog::PrintErr("%" Px64 ": Isolate Data\n", file->Position());
675 }
676 if (!file->WriteFully(isolate_data_buffer, isolate_data_size)) {
677 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", filename);
678 }
679
680 if (isolate_instructions_size != 0) {
681 file->SetPosition(Utils::RoundUp(file->Position(), kAppSnapshotPageSize));
683 Syslog::PrintErr("%" Px64 ": Isolate Instructions\n", file->Position());
684 }
685 if (!file->WriteFully(isolate_instructions_buffer,
686 isolate_instructions_size)) {
687 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n",
688 filename);
689 }
690 }
691
692 file->Flush();
693 file->Release();
694}
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition: utils.h:120
MagicNumberData appjit_magic_number
Definition: dartutils.cc:35
static constexpr int64_t kAppSnapshotPageSize
static constexpr int64_t kAppSnapshotHeaderSize
static bool WriteInt64(File *file, int64_t size)
#define Px64
Definition: globals.h:418
#define LOG_SECTION_BOUNDARIES
const uint8_t bytes[kMaxLength]
Definition: dartutils.h:657

The documentation for this class was generated from the following files: