Skip to content

Commit 1eefcab

Browse files
authored
Check the kernel header magic to determine if a mapping may be a valid kernel blob. (flutter#5997)
1 parent 149ea7b commit 1eefcab

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

runtime/dart_vm.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ bool DartVM::IsRunningPrecompiledCode() {
196196
return Dart_IsPrecompiledRuntime();
197197
}
198198

199+
bool DartVM::IsKernelMapping(const fml::FileMapping* mapping) {
200+
if (mapping == nullptr) {
201+
return false;
202+
}
203+
204+
const uint8_t kKernelHeaderMagic[] = {0x90, 0xAB, 0xCD, 0xEF};
205+
const size_t kKernelHeaderMagicSize = sizeof(kKernelHeaderMagic);
206+
207+
if (mapping->GetSize() < kKernelHeaderMagicSize) {
208+
return false;
209+
}
210+
211+
if (memcmp(kKernelHeaderMagic, mapping->GetMapping(),
212+
kKernelHeaderMagicSize) != 0) {
213+
return false;
214+
}
215+
216+
return true;
217+
}
218+
199219
static std::vector<const char*> ProfilingFlags(bool enable_profiling) {
200220
// Disable Dart's built in profiler when building a debug build. This
201221
// works around a race condition that would sometimes stop a crash's

runtime/dart_vm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class DartVM : public fml::RefCountedThreadSafe<DartVM> {
3838

3939
static bool IsRunningPrecompiledCode();
4040

41+
static bool IsKernelMapping(const fml::FileMapping* mapping);
42+
4143
const Settings& GetSettings() const;
4244

4345
const fml::Mapping& GetPlatformKernel() const;
@@ -47,6 +49,7 @@ class DartVM : public fml::RefCountedThreadSafe<DartVM> {
4749
IsolateNameServer* GetIsolateNameServer();
4850

4951
fml::RefPtr<DartSnapshot> GetIsolateSnapshot() const;
52+
5053
fml::RefPtr<DartSnapshot> GetSharedSnapshot() const;
5154

5255
fml::WeakPtr<DartVM> GetWeakPtr();

shell/common/shell.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -856,19 +856,6 @@ bool Shell::OnServiceProtocolScreenshotSKP(
856856
return false;
857857
}
858858

859-
static bool FileNameIsDill(const std::string& name) {
860-
const std::string suffix = ".dill";
861-
862-
if (name.size() < suffix.size()) {
863-
return false;
864-
}
865-
866-
if (name.rfind(suffix, name.size()) == name.size() - suffix.size()) {
867-
return true;
868-
}
869-
return false;
870-
}
871-
872859
// Service protocol handler
873860
bool Shell::OnServiceProtocolRunInView(
874861
const blink::ServiceProtocol::Handler::ServiceProtocolMap& params,
@@ -900,10 +887,13 @@ bool Shell::OnServiceProtocolRunInView(
900887
auto main_script_file =
901888
fml::paths::AbsolutePath(params.at("mainScript").ToString());
902889

890+
auto main_script_file_mapping =
891+
std::make_unique<fml::FileMapping>(main_script_file, false);
892+
903893
auto isolate_configuration =
904-
FileNameIsDill(main_script_file)
894+
blink::DartVM::IsKernelMapping(main_script_file_mapping.get())
905895
? IsolateConfiguration::CreateForSnapshot(
906-
std::make_unique<fml::FileMapping>(main_script_file, false))
896+
std::move(main_script_file_mapping))
907897
: IsolateConfiguration::CreateForSource(
908898
main_script_file, params.at("packagesFile").ToString());
909899

shell/testing/tester_main.cc

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,6 @@ class ScriptCompletionTaskObserver {
8989
FML_DISALLOW_COPY_AND_ASSIGN(ScriptCompletionTaskObserver);
9090
};
9191

92-
static bool FileNameIsDill(const std::string& name) {
93-
const std::string suffix = ".dill";
94-
95-
if (name.size() < suffix.size()) {
96-
return false;
97-
}
98-
99-
if (name.rfind(suffix, name.size()) == name.size() - suffix.size()) {
100-
return true;
101-
}
102-
return false;
103-
}
104-
10592
int RunTester(const blink::Settings& settings, bool run_forever) {
10693
const auto thread_label = "io.flutter.test";
10794

@@ -142,12 +129,13 @@ int RunTester(const blink::Settings& settings, bool run_forever) {
142129
return EXIT_FAILURE;
143130
}
144131

132+
auto main_dart_file_mapping = std::make_unique<fml::FileMapping>(
133+
fml::paths::AbsolutePath(settings.main_dart_file_path), false);
134+
145135
auto isolate_configuration =
146-
FileNameIsDill(settings.main_dart_file_path)
136+
blink::DartVM::IsKernelMapping(main_dart_file_mapping.get())
147137
? IsolateConfiguration::CreateForSnapshot(
148-
std::make_unique<fml::FileMapping>(
149-
fml::paths::AbsolutePath(settings.main_dart_file_path),
150-
false))
138+
std::move(main_dart_file_mapping))
151139
: IsolateConfiguration::CreateForSource(settings.main_dart_file_path,
152140
settings.packages_file_path);
153141

0 commit comments

Comments
 (0)