[vm] Fix leak when generating snapshot depfile.

Use depfiles for the SDK's application snapshots.

Add check that we don't try to create a script snapshot after loading from an app snapshot.

Bug: TO-596
Change-Id: Ib4209c3062dbe0f3c37b8f65e5a50ca64544a39b
Reviewed-on: https://dart-review.googlesource.com/26043
Commit-Queue: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Zach Anderson <zra@google.com>
This commit is contained in:
Ryan Macnak 2017-12-05 17:09:52 +00:00 committed by commit-bot@chromium.org
parent 4d0619832f
commit 76842f7a70
5 changed files with 75 additions and 13 deletions

View file

@ -301,20 +301,21 @@ void Loader::ResolveDependenciesAsFilePaths() {
for (intptr_t i = 0; i < dependencies->length(); i++) {
char* resolved_uri = (*dependencies)[i];
uint8_t* scoped_file_path = NULL;
intptr_t scoped_file_path_length = -1;
uint8_t* file_path = NULL;
intptr_t file_path_length = -1;
Dart_Handle uri = Dart_NewStringFromCString(resolved_uri);
ASSERT(!Dart_IsError(uri));
Dart_Handle result = Loader::ResolveAsFilePath(uri, &scoped_file_path,
&scoped_file_path_length);
Dart_Handle result =
Loader::ResolveAsFilePath(uri, &file_path, &file_path_length);
if (Dart_IsError(result)) {
Log::Print("Error resolving dependency: %s\n", Dart_GetError(result));
return;
}
(*dependencies)[i] =
StringUtils::StrNDup(reinterpret_cast<const char*>(scoped_file_path),
scoped_file_path_length);
// Convert buffer buffer to NUL-terminated string.
(*dependencies)[i] = StringUtils::StrNDup(
reinterpret_cast<const char*>(file_path), file_path_length);
free(file_path);
free(resolved_uri);
}
}

View file

@ -756,6 +756,12 @@ bool RunMainIsolate(const char* script_name, CommandLineOptions* dart_options) {
Dart_EnterScope();
if (Options::gen_snapshot_kind() == kScript) {
if (vm_run_app_snapshot) {
Log::PrintErr("Cannot create a script snapshot from an app snapshot.\n");
// The snapshot would contain references to the app snapshot instead of
// the core snapshot.
Platform::Exit(kErrorExitCode);
}
Snapshot::GenerateScript(Options::snapshot_filename());
} else {
// Lookup the library of the root script.

View file

@ -0,0 +1,53 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:io";
void main(List<String> args) {
if (args.contains("--child")) {
print("Hello, pre-scanned world!");
} else {
generateSnapshot();
}
}
void generateSnapshot() {
var tempDir = Directory.systemTemp.createTempSync("script-snapshot");
var snapshotPath = tempDir.uri.resolve("hello.snapshot").toFilePath();
var depfilePath = tempDir.uri.resolve("hello.snapshot.d").toFilePath();
var scriptPath = Platform.script.toFilePath();
var exec = Platform.resolvedExecutable;
var args = new List();
args.addAll(Platform.executableArguments);
args.add("--snapshot=$snapshotPath");
args.add("--snapshot-depfile=$depfilePath");
args.add(scriptPath);
args.add("--child");
var result = Process.runSync(exec, args);
print("Exit code: ${result.exitCode}");
print("stdout:");
print(result.stdout);
print("stderr:");
print(result.stderr);
if (result.exitCode != 0) {
throw "Bad exit code: ${result.exitCode}";
}
var depfileContents = new File(depfilePath).readAsStringSync();
print("depfile:");
print(depfileContents);
if (!depfileContents.contains(snapshotPath)) {
print("snapshotPath:");
print(snapshotPath);
throw "Missing snapshot path";
}
if (!depfileContents.contains(scriptPath)) {
print("scriptPath:");
print(scriptPath);
throw "Missing script path";
}
}

View file

@ -21,6 +21,7 @@ no_assert_test: Fail, OK # This is testing a vm flag.
[ $runtime != vm || $compiler != none ]
script_snapshot_not_executed_test: SkipByDesign # Only makes sense running from source.
script_snapshot_depfile_test: SkipByDesign # Only makes sense running from source.
[ (($hot_reload) || ($hot_reload_rollback)) ]
script_snapshot_not_executed_test: RuntimeError

View file

@ -39,25 +39,26 @@ template("application_snapshot") {
}
compiled_action(target_name) {
tool = "$_dart_root/runtime/bin:dart"
deps = extra_deps + [ "$_dart_root/pkg:pkg_files_stamp" ]
deps = extra_deps
depfile = "$root_gen_dir/$name.dart.snapshot.d"
inputs = extra_inputs + [
"$_dart_root/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart",
"$root_gen_dir/pkg_files.stamp",
]
main_file = rebase_path(main_dart)
inputs = extra_inputs + [ main_file ]
output = "$root_gen_dir/$name.dart.snapshot"
outputs = [
output,
]
abs_depfile = rebase_path(depfile)
abs_output = rebase_path(output)
main_file = rebase_path(main_dart)
args = [
"--deterministic",
"--packages=$dot_packages",
"--snapshot=$abs_output",
"--snapshot-depfile=$abs_depfile",
]
if (dart_snapshot_kind == "script") {