Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 *vm_data_buffer, intptr_t vm_data_size, uint8_t *vm_instructions_buffer, intptr_t vm_instructions_size, 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 817 of file snapshot_utils.cc.

817 {
818 File* file = File::Open(nullptr, snapshot_filename, File::kWriteTruncate);
819 RefCntReleaseScope<File> rs(file);
820 if (file == nullptr) {
821 ErrorExit(kErrorExitCode, "Unable to open file %s for writing snapshot\n",
822 snapshot_filename);
823 }
825 StreamingWriteCallback, file, /*strip=*/false,
826 /*debug_callback_data=*/nullptr);
827 if (Dart_IsError(result)) {
829 }
830}
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 777 of file snapshot_utils.cc.

777 {
778#if defined(TARGET_ARCH_IA32)
779 // Snapshots with code are not supported on IA32.
780 uint8_t* isolate_buffer = nullptr;
781 intptr_t isolate_size = 0;
782
783 Dart_Handle result = Dart_CreateSnapshot(nullptr, nullptr, &isolate_buffer,
784 &isolate_size, /*is_core=*/false);
785 if (Dart_IsError(result)) {
787 }
788
789 WriteAppSnapshot(snapshot_filename, nullptr, 0, nullptr, 0, isolate_buffer,
790 isolate_size, nullptr, 0);
791#else
792 uint8_t* isolate_data_buffer = nullptr;
793 intptr_t isolate_data_size = 0;
794 uint8_t* isolate_instructions_buffer = nullptr;
795 intptr_t isolate_instructions_size = 0;
797 &isolate_data_buffer, &isolate_data_size, &isolate_instructions_buffer,
798 &isolate_instructions_size);
799 if (Dart_IsError(result)) {
801 }
802 WriteAppSnapshot(snapshot_filename, nullptr, 0, nullptr, 0,
803 isolate_data_buffer, isolate_data_size,
804 isolate_instructions_buffer, isolate_instructions_size);
805#endif
806}
static void WriteAppSnapshot(const char *filename, uint8_t *vm_data_buffer, intptr_t vm_data_size, uint8_t *vm_instructions_buffer, intptr_t vm_instructions_size, 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 749 of file snapshot_utils.cc.

751 {
752#if !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) && !defined(TESTING)
753 ASSERT(Dart_CurrentIsolate() == nullptr);
754
755 uint8_t* kernel_buffer = nullptr;
756 intptr_t kernel_buffer_size = 0;
757 dfe.ReadScript(script_name, nullptr, &kernel_buffer, &kernel_buffer_size);
758 if (kernel_buffer != nullptr) {
759 WriteSnapshotFile(snapshot_filename, kernel_buffer, kernel_buffer_size);
760 free(kernel_buffer);
761 } else {
763 dfe.CompileScript(script_name, /*incremental*/ false, package_config,
764 /*snapshot=*/true, /*embedd_sources=*/true);
766 Syslog::PrintErr("%s\n", result.error);
768 }
769 WriteSnapshotFile(snapshot_filename, result.kernel, result.kernel_size);
770 free(result.kernel);
771 }
772#else
773 UNREACHABLE();
774#endif // !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) && !defined(TESTING)
775}
#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:3728
#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 588 of file snapshot_utils.cc.

590 {
591 Utils::CStringUniquePtr decoded_path(nullptr, std::free);
592 const char* script_name = nullptr;
593 if (decode_uri) {
594 decoded_path = File::UriToPath(script_uri);
595 if (decoded_path == nullptr) {
596 return nullptr;
597 }
598 script_name = decoded_path.get();
599 } else {
600 script_name = script_uri;
601 }
602 if (File::GetType(nullptr, script_name, true) != File::kIsFile) {
603 // If 'script_name' refers to a pipe, don't read to check for an app
604 // snapshot since we cannot rewind if it isn't (and couldn't mmap it in
605 // anyway if it was).
606 return nullptr;
607 }
608 File* file = File::Open(nullptr, script_name, File::kRead);
609 if (file == nullptr) {
610 return nullptr;
611 }
612 RefCntReleaseScope<File> rs(file);
613 if ((file->Length() - file->Position()) < DartUtils::kMaxMagicNumberSize) {
614 return nullptr;
615 }
616
619 if (!file->ReadFully(&header, DartUtils::kMaxMagicNumberSize)) {
620 return nullptr;
621 }
622 DartUtils::MagicNumber magic_number =
624 if (magic_number == DartUtils::kAppJITMagicNumber) {
625 // Return the JIT snapshot.
626 return TryReadAppSnapshotBlobs(script_name, file);
627 }
628#if defined(DART_PRECOMPILED_RUNTIME)
629 if (!DartUtils::IsAotMagicNumber(magic_number)) {
630 return nullptr;
631 }
632
633 // For testing AOT with the standalone embedder, we also support loading
634 // from a dynamic library to simulate what happens on iOS.
635
636#if defined(DART_TARGET_OS_LINUX) || defined(DART_TARGET_OS_MACOS)
637 // On Linux and OSX, resolve the script path before passing into dlopen()
638 // since dlopen will not search the filesystem for paths like 'libtest.so'.
639 std::unique_ptr<char, decltype(std::free)*> absolute_path{
640 realpath(script_name, nullptr), std::free};
641 script_name = absolute_path.get();
642#endif
643
644 AppSnapshot* snapshot = nullptr;
645 if (!force_load_elf_from_memory) {
646 snapshot = TryReadAppSnapshotDynamicLibrary(script_name);
647 if (snapshot != nullptr) {
648 return snapshot;
649 }
650 }
651 return TryReadAppSnapshotElf(script_name, /*file_offset=*/0,
652 force_load_elf_from_memory);
653#else
654 // We create a dummy snapshot object just to remember the type which
655 // has already been identified by sniffing the magic number.
656 return new DummySnapshot(magic_number);
657#endif // defined(DART_PRECOMPILED_RUNTIME)
658
659 return nullptr;
660}
std::unique_ptr< char, decltype(std::free) * > CStringUniquePtr
Definition utils.h:644
static MagicNumber SniffForMagicNumber(const char *filename)
Definition dartutils.cc:407
static bool IsAotMagicNumber(MagicNumber number)
Definition dartutils.h:275
static constexpr int64_t kMaxMagicNumberSize
Definition dartutils.h:271
static Utils::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)
static const char header[]
Definition skpbench.cpp:88

◆ WriteAppSnapshot()

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

Definition at line 684 of file snapshot_utils.cc.

692 {
693 File* file = File::Open(nullptr, filename, File::kWriteTruncate);
694 if (file == nullptr) {
695 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", filename);
696 }
697
699 WriteInt64(file, vm_data_size);
700 WriteInt64(file, vm_instructions_size);
701 WriteInt64(file, isolate_data_size);
702 WriteInt64(file, isolate_instructions_size);
703 ASSERT(file->Position() ==
705
706 file->SetPosition(Utils::RoundUp(file->Position(), kAppSnapshotPageSize));
708 Syslog::PrintErr("%" Px64 ": VM Data\n", file->Position());
709 }
710 if (!file->WriteFully(vm_data_buffer, vm_data_size)) {
711 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", filename);
712 }
713
714 if (vm_instructions_size != 0) {
715 file->SetPosition(Utils::RoundUp(file->Position(), kAppSnapshotPageSize));
717 Syslog::PrintErr("%" Px64 ": VM Instructions\n", file->Position());
718 }
719 if (!file->WriteFully(vm_instructions_buffer, vm_instructions_size)) {
720 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n",
721 filename);
722 }
723 }
724
725 file->SetPosition(Utils::RoundUp(file->Position(), kAppSnapshotPageSize));
727 Syslog::PrintErr("%" Px64 ": Isolate Data\n", file->Position());
728 }
729 if (!file->WriteFully(isolate_data_buffer, isolate_data_size)) {
730 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n", filename);
731 }
732
733 if (isolate_instructions_size != 0) {
734 file->SetPosition(Utils::RoundUp(file->Position(), kAppSnapshotPageSize));
736 Syslog::PrintErr("%" Px64 ": Isolate Instructions\n", file->Position());
737 }
738 if (!file->WriteFully(isolate_instructions_buffer,
739 isolate_instructions_size)) {
740 ErrorExit(kErrorExitCode, "Unable to write snapshot file '%s'\n",
741 filename);
742 }
743 }
744
745 file->Flush();
746 file->Release();
747}
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:105
MagicNumberData appjit_magic_number
Definition dartutils.cc:39
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: