Skip to content

Commit

Permalink
[fuchsia] Implement DISABLE_DYNAMIC_CODE_GENERATION feature
Browse files Browse the repository at this point in the history
Allow web_instance.cm to run without dynamic code generation, and
add a config argument to the cast_runner to allow it to host Cast
applications without dynamic code generation.

This trades off performance of scripts (e.g. JavaScript) for
reduced memory footprint, and potentially faster content loading
times for some use-cases, and will in future enable web containers
to run without VMEX capabilities.

Web functionality that requires dynamic code generation, such as
WebAssembly, is implicitly disabled by this new feature.

Some cleanups are also made to move arguments passed-by-pointer to
use pass-by-reference, as is now preferred style.

Bug: 1293384
Change-Id: Ifee44258e5974e588575ff2756b36ae4dca0280b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3769896
Quick-Run: Wez <wez@chromium.org>
Reviewed-by: Greg Thompson <grt@chromium.org>
Reviewed-by: David Dorwin <ddorwin@chromium.org>
Auto-Submit: Wez <wez@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1082407}
  • Loading branch information
Wez authored and Chromium LUCI CQ committed Dec 13, 2022
1 parent 55cc630 commit c9b1f81
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 96 deletions.
16 changes: 11 additions & 5 deletions fuchsia_web/runners/cast/cast_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,14 @@ class FrameHostComponent final

} // namespace

CastRunner::CastRunner(WebInstanceHost* web_instance_host, bool is_headless)
CastRunner::CastRunner(WebInstanceHost& web_instance_host, Options options)
: web_instance_host_(web_instance_host),
is_headless_(is_headless),
is_headless_(options.headless),
disable_codegen_(options.disable_codegen),
main_services_(std::make_unique<base::FilteredServiceDirectory>(
base::ComponentContextForProcess()->svc())),
main_context_(std::make_unique<WebContentRunner>(
web_instance_host_,
*web_instance_host_,
base::BindRepeating(&CastRunner::GetMainWebInstanceConfig,
base::Unretained(this)))),
isolated_services_(std::make_unique<base::FilteredServiceDirectory>(
Expand Down Expand Up @@ -428,6 +429,11 @@ WebContentRunner::WebInstanceConfig CastRunner::GetCommonWebInstanceConfig() {
fuchsia::web::ContextFeatureFlags::VULKAN;
}

if (disable_codegen_) {
*config.params.mutable_features() |=
fuchsia::web::ContextFeatureFlags::DISABLE_DYNAMIC_CODE_GENERATION;
}

// When tests require that VULKAN be disabled, DRM must also be disabled.
if (disable_vulkan_for_test_) {
*config.params.mutable_features() &=
Expand Down Expand Up @@ -545,8 +551,8 @@ CastRunner::GetWebInstanceConfigForAppConfig(
WebContentRunner* CastRunner::CreateIsolatedRunner(
WebContentRunner::WebInstanceConfig config) {
// Create an isolated context which will own the CastComponent.
auto context =
std::make_unique<WebContentRunner>(web_instance_host_, std::move(config));
auto context = std::make_unique<WebContentRunner>(*web_instance_host_,
std::move(config));
context->SetOnEmptyCallback(
base::BindOnce(&CastRunner::OnIsolatedContextEmpty,
base::Unretained(this), base::Unretained(context.get())));
Expand Down
25 changes: 19 additions & 6 deletions fuchsia_web/runners/cast/cast_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ class CastRunner final : public fuchsia::component::runner::ComponentRunner,
public chromium::cast::DataReset,
public PendingCastComponent::Delegate {
public:
struct Options {
// Set to true to run components without generating output via Scenic.
bool headless = false;

// Set to true to run components without without web optimizations (e.g.
// JavaScript Just-In-Time compilation) or features (e.g. WebAssembly) that
// require dynamic code generation.
bool disable_codegen = false;
};

static constexpr uint16_t kRemoteDebuggingPort = 9222;

// Creates the Runner for Cast components.
// |web_instance_host|: Used to create an isolated web_instance
// Component in which to host the fuchsia.web.Context.
// |is_headless|: True if this instance should create Contexts with the
// HEADLESS feature set.
CastRunner(WebInstanceHost* web_instance_host, bool is_headless);
// `web_instance_host` is used to create a "main" instance to host Cast apps
// and serve `FrameHost` instances, and isolated containers for apps that
// need them.
CastRunner(WebInstanceHost& web_instance_host, Options options);
~CastRunner() override;

CastRunner(const CastRunner&) = delete;
Expand Down Expand Up @@ -124,11 +133,15 @@ class CastRunner final : public fuchsia::component::runner::ComponentRunner,
bool WasPersistedCacheErased();

// Passed to WebContentRunners to use to create web_instance Components.
WebInstanceHost* const web_instance_host_;
const raw_ref<WebInstanceHost> web_instance_host_;

// True if this Runner uses Context(s) with the HEADLESS feature set.
const bool is_headless_;

// True if this Runner should create web Contexts with dynamic code generation
// disabled.
const bool disable_codegen_;

// Holds the main fuchsia.web.Context used to host CastComponents.
// Note that although |main_context_| is actually a WebContentRunner, that is
// only being used to maintain the Context for the hosted components.
Expand Down
12 changes: 8 additions & 4 deletions fuchsia_web/runners/cast/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ constexpr char kHeadlessConfigKey[] = "headless";
// Config-data key to enable the fuchsia.web.FrameHost provider component.
constexpr char kFrameHostConfigKey[] = "enable-frame-host-component";

// Config-data key for disable dynamic code generation by the web runtime.
constexpr char kDisableCodeGenConfigKey[] = "disable-codegen";

// Returns the value of |config_key| or false if it is not set.
bool GetConfigBool(base::StringPiece config_key) {
const absl::optional<base::Value::Dict>& config =
Expand Down Expand Up @@ -136,10 +139,11 @@ int main(int argc, char** argv) {

// Publish the fuchsia.component.runner.ComponentRunner for Cast apps.
WebInstanceHost web_instance_host;
const bool enable_headless =
command_line->HasSwitch(kForceHeadlessForTestsSwitch) ||
GetConfigBool(kHeadlessConfigKey);
CastRunner runner(&web_instance_host, enable_headless);
CastRunner runner(
web_instance_host,
{.headless = command_line->HasSwitch(kForceHeadlessForTestsSwitch) ||
GetConfigBool(kHeadlessConfigKey),
.disable_codegen = GetConfigBool(kDisableCodeGenConfigKey)});
const base::ScopedServiceBinding<fuchsia::component::runner::ComponentRunner>
runner_binding(outgoing_directory, &runner);

Expand Down
5 changes: 2 additions & 3 deletions fuchsia_web/runners/common/web_content_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ WebContentRunner::WebInstanceConfig&
WebContentRunner::WebInstanceConfig::operator=(WebInstanceConfig&&) = default;

WebContentRunner::WebContentRunner(
WebInstanceHost* web_instance_host,
WebInstanceHost& web_instance_host,
GetWebInstanceConfigCallback get_web_instance_config_callback)
: web_instance_host_(web_instance_host),
get_web_instance_config_callback_(
std::move(get_web_instance_config_callback)) {
DCHECK(web_instance_host_);
DCHECK(get_web_instance_config_callback_);
}

WebContentRunner::WebContentRunner(WebInstanceHost* web_instance_host,
WebContentRunner::WebContentRunner(WebInstanceHost& web_instance_host,
WebInstanceConfig web_instance_config)
: web_instance_host_(web_instance_host) {
CreateWebInstanceAndContext(std::move(web_instance_config));
Expand Down
6 changes: 3 additions & 3 deletions fuchsia_web/runners/common/web_content_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class WebContentRunner : public fuchsia::sys::Runner {
// |get_web_instance_config_callback|: Returns parameters for the Runner's
// fuchsia.web.Context.
WebContentRunner(
WebInstanceHost* web_instance_host,
WebInstanceHost& web_instance_host,
GetWebInstanceConfigCallback get_web_instance_config_callback);

// Creates a Runner using a Context configured with `web_instance_config`.
// The Runner becomes non-functional if the Context terminates.
WebContentRunner(WebInstanceHost* web_instance_host,
WebContentRunner(WebInstanceHost& web_instance_host,
WebInstanceConfig web_instance_config);

~WebContentRunner() override;
Expand Down Expand Up @@ -106,7 +106,7 @@ class WebContentRunner : public fuchsia::sys::Runner {
// Starts the web_instance and connects |context_| to it.
void CreateWebInstanceAndContext(WebInstanceConfig web_instance_config);

WebInstanceHost* const web_instance_host_;
const raw_ref<WebInstanceHost> web_instance_host_;
const GetWebInstanceConfigCallback get_web_instance_config_callback_;

// If set, invoked whenever a WebComponent is created.
Expand Down
2 changes: 1 addition & 1 deletion fuchsia_web/runners/web/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int argc, char** argv) {
LogComponentStartWithVersion("web_runner");

WebInstanceHost web_instance_host;
WebContentRunner runner(&web_instance_host,
WebContentRunner runner(web_instance_host,
base::BindRepeating(&GetWebInstanceConfig));
base::ScopedServiceBinding<fuchsia::sys::Runner> binding(
base::ComponentContextForProcess()->outgoing().get(), &runner);
Expand Down
Loading

0 comments on commit c9b1f81

Please sign in to comment.