Revert "[ VM / DartDev ] Added support for format and pub commands via standalone VM"

This reverts commit 1b959d6127.

Reason for revert: ASAN failures

Original change's description:
> [ VM / DartDev ] Added support for `format` and `pub` commands via standalone VM
> 
> Initial work to allow for Dart snapshots to be executed based on a verb
> provided to the VM. Examples:
> 
> - `dart format` === `dartfmt`
> - `dart pub run test` === `pub run test`
> 
> Change-Id: I22c9c2ab6790b485395f1148419c517641f693ac
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/133663
> Commit-Queue: Ben Konyi <bkonyi@google.com>
> Reviewed-by: Siva Annamalai <asiva@google.com>

TBR=bkonyi@google.com,asiva@google.com

Change-Id: I35d9ff260e5f35f4e36b1e22ecf8b9dc1d2e3ed2
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134564
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
This commit is contained in:
Ben Konyi 2020-02-05 20:56:19 +00:00 committed by commit-bot@chromium.org
parent fecab44f59
commit b3396cbdca
14 changed files with 84 additions and 333 deletions

View file

@ -17,8 +17,6 @@ builtin_impl_sources = [
"crypto_win.cc",
"dartutils.cc",
"dartutils.h",
"dartdev_utils.cc",
"dartdev_utils.h",
"directory.cc",
"directory.h",
"directory_android.cc",
@ -26,8 +24,6 @@ builtin_impl_sources = [
"directory_linux.cc",
"directory_macos.cc",
"directory_win.cc",
"exe_utils.h",
"exe_utils.cc",
"extensions.h",
"extensions.cc",
"extensions_android.cc",

View file

@ -1,81 +0,0 @@
// Copyright (c) 2020, 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.
#include "bin/dartdev_utils.h"
#include <memory>
#include "bin/directory.h"
#include "bin/exe_utils.h"
#include "bin/file.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
typedef struct {
const char* command;
const char* snapshot_name;
} DartDevCommandMapping;
static const DartDevCommandMapping dart_dev_commands[] = {
{"format", "dartfmt.dart.snapshot"},
{"pub", "pub.dart.snapshot"},
};
static const DartDevCommandMapping* FindCommandMapping(const char* command) {
intptr_t num_commands =
sizeof(dart_dev_commands) / sizeof(dart_dev_commands[0]);
for (intptr_t i = 0; i < num_commands; i++) {
const DartDevCommandMapping& command_mapping = dart_dev_commands[i];
if (strcmp(command, command_mapping.command) == 0) {
return &command_mapping;
}
}
return nullptr;
}
bool DartDevUtils::ShouldParseCommand(const char* script_uri) {
return !File::ExistsUri(nullptr, script_uri);
}
bool DartDevUtils::TryParseCommandFromScriptName(char** script_name) {
const DartDevCommandMapping* command = FindCommandMapping(*script_name);
// Either the command doesn't exist or we've been given an HTTP resource.
if (command == nullptr) {
return true;
}
// |dir_prefix| includes the last path seperator.
auto dir_prefix = std::unique_ptr<char, void (*)(void*)>(
EXEUtils::GetDirectoryPrefixFromExeName(), free);
// First assume we're in dart-sdk/bin.
char* snapshot_path = Utils::SCreate("%s/snapshots/%s", dir_prefix.get(),
command->snapshot_name);
if (File::Exists(nullptr, snapshot_path)) {
free(*script_name);
*script_name = snapshot_path;
return true;
}
free(snapshot_path);
// If we're not in dart-sdk/bin, we might be in one of the $SDK/out/*
// directories. Try to use a snapshot from a previously built SDK.
snapshot_path = Utils::SCreate("%s/dart-sdk/bin/snapshots/%s",
dir_prefix.get(), command->snapshot_name);
if (File::Exists(nullptr, snapshot_path)) {
free(*script_name);
*script_name = snapshot_path;
return true;
}
free(snapshot_path);
Syslog::PrintErr("Could not find snapshot for command '%s': %s\n",
*script_name, command->snapshot_name);
return false;
}
} // namespace bin
} // namespace dart

View file

@ -1,31 +0,0 @@
// Copyright (c) 2020, 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.
#ifndef RUNTIME_BIN_DARTDEV_UTILS_H_
#define RUNTIME_BIN_DARTDEV_UTILS_H_
#include "platform/globals.h"
namespace dart {
namespace bin {
class DartDevUtils {
public:
// Returns true if there does not exist a file at |script_uri|.
static bool ShouldParseCommand(const char* script_uri);
// Returns true if we were successfully able to parse a DartDev command.
// Returns false if we were unable to find a matching command or a matching
// snapshot does not exist, in which case the VM should exit.
static bool TryParseCommandFromScriptName(char** script_name);
private:
DISALLOW_ALLOCATION();
DISALLOW_IMPLICIT_CONSTRUCTORS(DartDevUtils);
};
} // namespace bin
} // namespace dart
#endif // RUNTIME_BIN_DARTDEV_UTILS_H_

View file

@ -10,7 +10,6 @@
#include "bin/dartutils.h"
#include "bin/directory.h"
#include "bin/error_exit.h"
#include "bin/exe_utils.h"
#include "bin/file.h"
#include "bin/main_options.h"
#include "bin/platform.h"
@ -42,6 +41,49 @@ DFE dfe;
const char kKernelServiceSnapshot[] = "kernel-service.dart.snapshot";
const char kSnapshotsDirectory[] = "snapshots";
static char* GetDirectoryPrefixFromExeName() {
const char* name = nullptr;
const int kTargetSize = 4096;
char target[kTargetSize];
intptr_t target_size =
Platform::ResolveExecutablePathInto(target, kTargetSize);
if (target_size > 0 && target_size < kTargetSize - 1) {
target[target_size] = 0;
name = target;
}
if (name == nullptr) {
name = Platform::GetExecutableName();
target_size = strlen(name);
}
Namespace* namespc = Namespace::Create(Namespace::Default());
if (File::GetType(namespc, name, false) == File::kIsLink) {
// Resolve the link without creating Dart scope String.
name = File::LinkTarget(namespc, name, target, kTargetSize);
if (name == NULL) {
return strdup("");
}
target_size = strlen(name);
}
namespc->Release();
const char* sep = File::PathSeparator();
const intptr_t sep_length = strlen(sep);
for (intptr_t i = target_size - 1; i >= 0; --i) {
const char* str = name + i;
if (strncmp(str, sep, sep_length) == 0
#if defined(HOST_OS_WINDOWS)
// TODO(aam): GetExecutableName doesn't work reliably on Windows,
// the code below is a workaround for that (we would be using
// just single Platform::Separator instead of both slashes if it did).
|| *str == '/'
#endif
) {
return Utils::StrNDup(name, i + 1);
}
}
return strdup("");
}
DFE::DFE()
: use_dfe_(false),
use_incremental_compiler_(false),
@ -114,7 +156,7 @@ bool DFE::InitKernelServiceAndPlatformDills(int target_abi_version) {
// |dir_prefix| includes the last path seperator.
auto dir_prefix = std::unique_ptr<char, void (*)(void*)>(
EXEUtils::GetDirectoryPrefixFromExeName(), free);
GetDirectoryPrefixFromExeName(), free);
if (target_abi_version != Options::kAbiVersionUnset) {
kernel_service_dill_ = nullptr;

View file

@ -1,59 +0,0 @@
// Copyright (c) 2020, 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.
#include "bin/exe_utils.h"
#include "bin/directory.h"
#include "bin/file.h"
#include "bin/platform.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
char* EXEUtils::GetDirectoryPrefixFromExeName() {
const char* name = nullptr;
const int kTargetSize = 4096;
char target[kTargetSize];
intptr_t target_size =
Platform::ResolveExecutablePathInto(target, kTargetSize);
if (target_size > 0 && target_size < kTargetSize - 1) {
target[target_size] = 0;
name = target;
}
if (name == nullptr) {
name = Platform::GetExecutableName();
target_size = strlen(name);
}
Namespace* namespc = Namespace::Create(Namespace::Default());
if (File::GetType(namespc, name, false) == File::kIsLink) {
// Resolve the link without creating Dart scope String.
name = File::LinkTarget(namespc, name, target, kTargetSize);
if (name == NULL) {
return strdup("");
}
target_size = strlen(name);
}
namespc->Release();
const char* sep = File::PathSeparator();
const intptr_t sep_length = strlen(sep);
for (intptr_t i = target_size - 1; i >= 0; --i) {
const char* str = name + i;
if (strncmp(str, sep, sep_length) == 0
#if defined(HOST_OS_WINDOWS)
// TODO(aam): GetExecutableName doesn't work reliably on Windows,
// the code below is a workaround for that (we would be using
// just single Platform::Separator instead of both slashes if it did).
|| *str == '/'
#endif
) {
return Utils::StrNDup(name, i + 1);
}
}
return strdup("");
}
} // namespace bin
} // namespace dart

View file

@ -1,29 +0,0 @@
// Copyright (c) 2020, 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.
#ifndef RUNTIME_BIN_EXE_UTILS_H_
#define RUNTIME_BIN_EXE_UTILS_H_
#include <stdlib.h>
#include <string.h>
#include "include/dart_api.h"
#include "platform/globals.h"
namespace dart {
namespace bin {
class EXEUtils {
public:
// Returns the path to the directory the current executable resides in.
static char* GetDirectoryPrefixFromExeName();
private:
DISALLOW_COPY_AND_ASSIGN(EXEUtils);
};
} // namespace bin
} // namespace dart
#endif // RUNTIME_BIN_EXE_UTILS_H_

View file

@ -220,7 +220,6 @@ class File : public ReferenceCounted<File> {
#endif
static bool Exists(Namespace* namespc, const char* path);
static bool ExistsUri(Namespace* namespc, const char* uri);
static bool Create(Namespace* namespc, const char* path);
static bool CreateLink(Namespace* namespc,
const char* path,

View file

@ -241,23 +241,15 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
return new File(new FileHandle(fd));
}
static std::unique_ptr<const char[]> DecodeUri(const char* uri) {
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0)
? uri + 7 : uri;
UriDecoder uri_decoder(path);
if (uri_decoder.decoded() == nullptr) {
if (uri_decoder.decoded() == NULL) {
errno = EINVAL;
return nullptr;
return NULL;
}
return std::unique_ptr<const char[]>(strdup(uri_decoder.decoded()));
}
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return nullptr;
}
return File::Open(namespc, path.get(), mode);
return File::Open(namespc, uri_decoder.decoded(), mode);
}
File* File::OpenStdio(int fd) {
@ -275,14 +267,6 @@ bool File::Exists(Namespace* namespc, const char* name) {
}
}
bool File::ExistsUri(Namespace* namespc, const char* uri) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return false;
}
return File::Exists(namespc, path.get());
}
bool File::Create(Namespace* namespc, const char* name) {
NamespaceScope ns(namespc, name);
const int fd = TEMP_FAILURE_RETRY(

View file

@ -243,23 +243,19 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
return OpenFD(fd);
}
static std::unique_ptr<const char[]> DecodeUri(const char* uri) {
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0)
? uri + 7 : uri;
UriDecoder uri_decoder(path);
if (uri_decoder.decoded() == nullptr) {
if (uri_decoder.decoded() == NULL) {
errno = EINVAL;
return nullptr;
return NULL;
}
return std::unique_ptr<const char[]>(strdup(uri_decoder.decoded()));
return File::Open(namespc, uri_decoder.decoded(), mode);
}
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return nullptr;
}
return File::Open(namespc, path.get(), mode);
File* File::OpenStdio(int fd) {
return new File(new FileHandle(fd));
}
bool File::Exists(Namespace* namespc, const char* name) {
@ -272,14 +268,6 @@ bool File::Exists(Namespace* namespc, const char* name) {
return false;
}
bool File::ExistsUri(Namespace* namespc, const char* uri) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return false;
}
return File::Exists(namespc, path.get());
}
bool File::Create(Namespace* namespc, const char* name) {
NamespaceScope ns(namespc, name);
const int fd = NO_RETRY_EXPECTED(

View file

@ -244,23 +244,15 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
return OpenFD(fd);
}
static std::unique_ptr<const char[]> DecodeUri(const char* uri) {
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0)
? uri + 7 : uri;
UriDecoder uri_decoder(path);
if (uri_decoder.decoded() == nullptr) {
if (uri_decoder.decoded() == NULL) {
errno = EINVAL;
return nullptr;
return NULL;
}
return std::unique_ptr<const char[]>(strdup(uri_decoder.decoded()));
}
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return nullptr;
}
return File::Open(namespc, path.get(), mode);
return File::Open(namespc, uri_decoder.decoded(), mode);
}
File* File::OpenStdio(int fd) {
@ -278,14 +270,6 @@ bool File::Exists(Namespace* namespc, const char* name) {
}
}
bool File::ExistsUri(Namespace* namespc, const char* uri) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return false;
}
return File::Exists(namespc, path.get());
}
bool File::Create(Namespace* namespc, const char* name) {
NamespaceScope ns(namespc, name);
const int fd = TEMP_FAILURE_RETRY(

View file

@ -286,23 +286,15 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
return new File(new FileHandle(fd));
}
static std::unique_ptr<const char[]> DecodeUri(const char* uri) {
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0)
? uri + 7 : uri;
UriDecoder uri_decoder(path);
if (uri_decoder.decoded() == nullptr) {
if (uri_decoder.decoded() == NULL) {
errno = EINVAL;
return nullptr;
return NULL;
}
return std::unique_ptr<const char[]>(strdup(uri_decoder.decoded()));
}
File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return nullptr;
}
return File::Open(namespc, path.get(), mode);
return File::Open(namespc, uri_decoder.decoded(), mode);
}
File* File::OpenStdio(int fd) {
@ -319,14 +311,6 @@ bool File::Exists(Namespace* namespc, const char* name) {
}
}
bool File::ExistsUri(Namespace* namespc, const char* uri) {
auto path = DecodeUri(uri);
if (path == nullptr) {
return false;
}
return File::Exists(namespc, path.get());
}
bool File::Create(Namespace* namespc, const char* name) {
int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666));
if (fd < 0) {

View file

@ -353,15 +353,6 @@ bool File::Exists(Namespace* namespc, const char* name) {
return StatHelper(system_name.wide(), &st);
}
bool File::ExistsUri(Namespace* namespc, const char* uri) {
UriDecoder uri_decoder(uri);
if (uri_decoder.decoded() == nullptr) {
SetLastError(ERROR_INVALID_NAME);
return false;
}
return File::Exists(namespc, uri_decoder.decoded());
}
bool File::Create(Namespace* namespc, const char* name) {
Utf8ToWideScope system_name(name);
int fd = _wopen(system_name.wide(), O_RDONLY | O_CREAT, 0666);

View file

@ -15,9 +15,9 @@
#include "bin/builtin.h"
#include "bin/console.h"
#include "bin/crashpad.h"
#include "bin/dartdev_utils.h"
#include "bin/dartutils.h"
#include "bin/dfe.h"
#include "bin/directory.h"
#include "bin/error_exit.h"
#include "bin/eventhandler.h"
#include "bin/extensions.h"
@ -37,7 +37,6 @@
#include "platform/hashmap.h"
#include "platform/syslog.h"
#include "platform/text_buffer.h"
#include "platform/utils.h"
extern "C" {
extern const uint8_t kDartVmSnapshotData[];
@ -1073,47 +1072,31 @@ void main(int argc, char** argv) {
#endif
// Parse command line arguments.
if (app_snapshot == nullptr) {
int result = Options::ParseArguments(
argc, argv, vm_run_app_snapshot, &vm_options, &script_name,
&dart_options, &print_flags_seen, &verbose_debug_seen);
if (result < 0) {
if (Options::help_option()) {
Options::PrintUsage();
Platform::Exit(0);
} else if (Options::version_option()) {
Options::PrintVersion();
Platform::Exit(0);
} else if (print_flags_seen) {
// Will set the VM flags, print them out and then we exit as no
// script was specified on the command line.
char* error =
Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
if (error != NULL) {
Syslog::PrintErr("Setting VM flags failed: %s\n", error);
free(error);
Platform::Exit(kErrorExitCode);
}
Platform::Exit(0);
} else {
Options::PrintUsage();
if (app_snapshot == nullptr &&
Options::ParseArguments(argc, argv, vm_run_app_snapshot, &vm_options,
&script_name, &dart_options, &print_flags_seen,
&verbose_debug_seen) < 0) {
if (Options::help_option()) {
Options::PrintUsage();
Platform::Exit(0);
} else if (Options::version_option()) {
Options::PrintVersion();
Platform::Exit(0);
} else if (print_flags_seen) {
// Will set the VM flags, print them out and then we exit as no
// script was specified on the command line.
char* error = Dart_SetVMFlags(vm_options.count(), vm_options.arguments());
if (error != NULL) {
Syslog::PrintErr("Setting VM flags failed: %s\n", error);
free(error);
Platform::Exit(kErrorExitCode);
}
}
// Try to parse a DartDev command if script_name doesn't point to a valid
// file. If the command isn't valid we fall through to handle the
// possibility that the script_name points to a HTTP resource. If the
// relevant snapshot can't be found we abort execution.
if (DartDevUtils::ShouldParseCommand(script_name) &&
!DartDevUtils::TryParseCommandFromScriptName(&script_name)) {
Platform::Exit(0);
} else {
Options::PrintUsage();
Platform::Exit(kErrorExitCode);
}
}
// At this point, script_name now points to either a script or a snapshot
// determined by DartDevUtils above.
DartUtils::SetEnvironment(Options::environment());
if (Options::suppress_core_dump()) {

View file

@ -440,7 +440,7 @@ int Options::ParseArguments(int argc,
// Get the script name.
if (i < argc) {
*script_name = strdup(argv[i]);
*script_name = argv[i];
i++;
} else {
return -1;