Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions fml/mapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,55 @@ uint8_t* FileMapping::GetMutableMapping() {
return mutable_mapping_;
}

std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
const std::string& path) {
return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
"");
}

std::unique_ptr<FileMapping> FileMapping::CreateReadOnly(
const fml::UniqueFD& base_fd,
const std::string& sub_path) {
if (sub_path.size() != 0) {
return CreateReadOnly(
OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
}

auto mapping = std::make_unique<FileMapping>(
base_fd, std::initializer_list<Protection>{Protection::kRead});

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
const std::string& path) {
return CreateReadExecute(
OpenFile(path.c_str(), false, FilePermission::kRead));
}

std::unique_ptr<FileMapping> FileMapping::CreateReadExecute(
const fml::UniqueFD& base_fd,
const std::string& sub_path) {
if (sub_path.size() != 0) {
return CreateReadExecute(
OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
}

auto mapping = std::make_unique<FileMapping>(
base_fd, std::initializer_list<Protection>{Protection::kRead,
Protection::kExecute});

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

// Data Mapping

DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}
Expand Down
13 changes: 13 additions & 0 deletions fml/mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ class FileMapping final : public Mapping {

~FileMapping() override;

static std::unique_ptr<FileMapping> CreateReadOnly(const std::string& path);

static std::unique_ptr<FileMapping> CreateReadOnly(
const fml::UniqueFD& base_fd,
const std::string& sub_path = "");

static std::unique_ptr<FileMapping> CreateReadExecute(
const std::string& path);

static std::unique_ptr<FileMapping> CreateReadExecute(
const fml::UniqueFD& base_fd,
const std::string& sub_path = "");

// |Mapping|
size_t GetSize() const override;

Expand Down
26 changes: 3 additions & 23 deletions runtime/dart_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,13 @@ const char* DartSnapshot::kIsolateInstructionsSymbol =
"kDartIsolateSnapshotInstructions";

static std::unique_ptr<const fml::Mapping> GetFileMapping(
const std::string path,
const std::string& path,
bool executable) {
fml::UniqueFD file =
fml::OpenFile(path.c_str(), // file path
false, // create file if necessary
fml::FilePermission::kRead // file permissions
);

if (!file.is_valid()) {
return nullptr;
}

using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
return fml::FileMapping::CreateReadExecute(path);
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
return fml::FileMapping::CreateReadOnly(path);
}

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

// The first party embedders don't yet use the stable embedder API and depend on
Expand Down
39 changes: 8 additions & 31 deletions runtime/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,6 @@ RuntimeTest::RuntimeTest()

RuntimeTest::~RuntimeTest() = default;

static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}

using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

void RuntimeTest::SetSnapshotsAndAssets(Settings& settings) {
if (!assets_dir_.is_valid()) {
return;
Expand All @@ -52,27 +26,30 @@ void RuntimeTest::SetSnapshotsAndAssets(Settings& settings) {
// don't need to be explicitly suppiled by the embedder.
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_data = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_, "vm_snapshot_data");
};

settings.isolate_snapshot_data = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_,
"isolate_snapshot_data");
};

if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"vm_snapshot_instr");
};

settings.isolate_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"isolate_snapshot_instr");
};
}
} else {
settings.application_kernels = [this]() {
std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
kernel_mappings.emplace_back(
GetMapping(assets_dir_, "kernel_blob.bin", false));
fml::FileMapping::CreateReadOnly(assets_dir_, "kernel_blob.bin"));
return kernel_mappings;
};
}
Expand Down
39 changes: 8 additions & 31 deletions shell/common/shell_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ ShellTest::ShellTest()

ShellTest::~ShellTest() = default;

static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}

using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

void ShellTest::SetSnapshotsAndAssets(Settings& settings) {
if (!assets_dir_.is_valid()) {
return;
Expand All @@ -55,27 +29,30 @@ void ShellTest::SetSnapshotsAndAssets(Settings& settings) {
// don't need to be explicitly suppiled by the embedder.
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_data = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_, "vm_snapshot_data");
};

settings.isolate_snapshot_data = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_data", false);
return fml::FileMapping::CreateReadOnly(assets_dir_,
"isolate_snapshot_data");
};

if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"vm_snapshot_instr");
};

settings.isolate_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_instr", true);
return fml::FileMapping::CreateReadExecute(assets_dir_,
"isolate_snapshot_instr");
};
}
} else {
settings.application_kernels = [this]() {
std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
kernel_mappings.emplace_back(
GetMapping(assets_dir_, "kernel_blob.bin", false));
fml::FileMapping::CreateReadOnly(assets_dir_, "kernel_blob.bin"));
return kernel_mappings;
};
}
Expand Down
37 changes: 6 additions & 31 deletions shell/platform/embedder/tests/embedder_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,21 @@
namespace flutter {
namespace testing {

static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}

using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

EmbedderContext::EmbedderContext(std::string assets_path)
: assets_path_(std::move(assets_path)),
native_resolver_(std::make_shared<::testing::TestDartNativeResolver>()) {
auto assets_dir = fml::OpenDirectory(assets_path_.c_str(), false,
fml::FilePermission::kRead);
vm_snapshot_data_ = GetMapping(assets_dir, "vm_snapshot_data", false);
vm_snapshot_data_ =
fml::FileMapping::CreateReadOnly(assets_dir, "vm_snapshot_data");
isolate_snapshot_data_ =
GetMapping(assets_dir, "isolate_snapshot_data", false);
fml::FileMapping::CreateReadOnly(assets_dir, "isolate_snapshot_data");

if (flutter::DartVM::IsRunningPrecompiledCode()) {
vm_snapshot_instructions_ =
GetMapping(assets_dir, "vm_snapshot_instr", true);
isolate_snapshot_instructions_ =
GetMapping(assets_dir, "isolate_snapshot_instr", true);
fml::FileMapping::CreateReadExecute(assets_dir, "vm_snapshot_instr");
isolate_snapshot_instructions_ = fml::FileMapping::CreateReadExecute(
assets_dir, "isolate_snapshot_instr");
}

isolate_create_callbacks_.push_back(
Expand Down