Skip to content

Commit

Permalink
compositing: Split Servo up into multiple sandboxed processes.
Browse files Browse the repository at this point in the history
Multiprocess mode is enabled with the `-M` switch, and sandboxing is
enabled with the `-S` switch.
  • Loading branch information
pcwalton committed Oct 15, 2015
1 parent 5a0a91e commit c67884c
Show file tree
Hide file tree
Showing 33 changed files with 660 additions and 189 deletions.
6 changes: 6 additions & 0 deletions components/compositing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,20 @@ features = ["texture_surface"]
version = "0.2"
features = [ "serde_serialization" ]

[dependencies.gaol]
git = "https://github.com/pcwalton/gaol"

[dependencies]
app_units = "0.1"
image = "0.3.14"
libc = "0.1"
log = "0.3"
num = "0.1.24"
time = "0.1.17"
gleam = "0.1"
euclid = "0.2"
serde = "0.6"
serde_macros = "0.5"

[target.x86_64-apple-darwin.dependencies]
core-graphics = "0.1"
Expand Down
5 changes: 2 additions & 3 deletions components/compositing/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use gfx_traits::color;
use gleam::gl;
use gleam::gl::types::{GLint, GLsizei};
use image::{DynamicImage, ImageFormat, RgbImage};
use ipc_channel::ipc::{self, IpcSharedMemory};
use ipc_channel::ipc::{self, IpcSender, IpcSharedMemory};
use ipc_channel::router::ROUTER;
use layers::geometry::{DevicePixel, LayerPixel};
use layers::layers::{BufferRequest, Layer, LayerBuffer, LayerBufferSet};
Expand All @@ -43,7 +43,6 @@ use std::fs::File;
use std::mem as std_mem;
use std::rc::Rc;
use std::slice::bytes::copy_memory;
use std::sync::mpsc::Sender;
use style_traits::viewport::ViewportConstraints;
use surface_map::SurfaceMap;
use time::{precise_time_ns, precise_time_s};
Expand Down Expand Up @@ -629,7 +628,7 @@ impl<Window: WindowMethods> IOCompositor<Window> {

fn set_frame_tree(&mut self,
frame_tree: &SendableFrameTree,
response_chan: Sender<()>,
response_chan: IpcSender<()>,
new_constellation_chan: ConstellationChan) {
response_chan.send(()).unwrap();

Expand Down
15 changes: 8 additions & 7 deletions components/compositing/compositor_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
//! Communication with the compositor task.

use compositor;
use euclid::{Point2D, Size2D};
use euclid::point::Point2D;
use euclid::size::Size2D;
use headless;
use ipc_channel::ipc::{IpcReceiver, IpcSender};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use layers::layers::{BufferRequest, LayerBufferSet};
use layers::platform::surface::{NativeDisplay, NativeSurface};
use msg::compositor_msg::{Epoch, FrameTreeId, LayerId, LayerProperties};
Expand Down Expand Up @@ -62,11 +63,11 @@ pub fn run_script_listener_thread(compositor_proxy: Box<CompositorProxy + 'stati
receiver: IpcReceiver<ScriptToCompositorMsg>) {
while let Ok(msg) = receiver.recv() {
match msg {
ScriptToCompositorMsg::ScrollFragmentPoint(pipeline_id, layer_id, point, _smooth) => {
ScriptToCompositorMsg::ScrollFragmentPoint(pipeline_id, layer_id, point, smooth) => {
compositor_proxy.send(Msg::ScrollFragmentPoint(pipeline_id,
layer_id,
point,
_smooth));
smooth));
}

ScriptToCompositorMsg::GetClientWindow(send) => {
Expand All @@ -82,7 +83,7 @@ pub fn run_script_listener_thread(compositor_proxy: Box<CompositorProxy + 'stati
}

ScriptToCompositorMsg::Exit => {
let (chan, port) = channel();
let (chan, port) = ipc::channel().unwrap();
compositor_proxy.send(Msg::Exit(chan));
port.recv().unwrap();
}
Expand Down Expand Up @@ -150,7 +151,7 @@ impl PaintListener for Box<CompositorProxy + 'static + Send> {
/// Messages from the painting task and the constellation task to the compositor task.
pub enum Msg {
/// Requests that the compositor shut down.
Exit(Sender<()>),
Exit(IpcSender<()>),

/// Informs the compositor that the constellation has completed shutdown.
/// Required because the constellation can have pending calls to make
Expand Down Expand Up @@ -178,7 +179,7 @@ pub enum Msg {
/// Alerts the compositor that the given pipeline has changed whether it is running animations.
ChangeRunningAnimationsState(PipelineId, AnimationState),
/// Replaces the current frame tree, typically called during main frame navigation.
SetFrameTree(SendableFrameTree, Sender<()>, ConstellationChan),
SetFrameTree(SendableFrameTree, IpcSender<()>, ConstellationChan),
/// The load of a page has begun: (can go back, can go forward).
LoadStart(bool, bool),
/// The load of a page has completed: (can go back, can go forward).
Expand Down
64 changes: 52 additions & 12 deletions components/compositing/constellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use compositor_task::Msg as CompositorMsg;
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg};
use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D};
use gaol;
use gaol::sandbox::{self, Sandbox, SandboxMethods};
use gfx::font_cache_task::FontCacheTask;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::ipc::{self, IpcOneShotServer, IpcReceiver, IpcSender};
use layout_traits::{LayoutControlChan, LayoutTaskFactory};
use msg::compositor_msg::Epoch;
use msg::constellation_msg::AnimationState;
Expand All @@ -36,18 +38,20 @@ use net_traits::image_cache_task::ImageCacheTask;
use net_traits::storage_task::{StorageTask, StorageTaskMsg};
use net_traits::{self, ResourceTask};
use offscreen_gl_context::GLContextAttributes;
use pipeline::{CompositionPipeline, InitialPipelineState, Pipeline};
use pipeline::{CompositionPipeline, InitialPipelineState, Pipeline, UnprivilegedPipelineContent};
use profile_traits::mem;
use profile_traits::time;
use sandboxing;
use script_traits::{CompositorEvent, ConstellationControlMsg, LayoutControlMsg};
use script_traits::{ScriptState, ScriptTaskFactory};
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::env;
use std::io::{self, Write};
use std::marker::PhantomData;
use std::mem::replace;
use std::process;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::mpsc::{Sender, channel};
use style_traits::viewport::ViewportConstraints;
use url::Url;
use util::cursor::Cursor;
Expand All @@ -66,7 +70,7 @@ pub struct Constellation<LTF, STF> {
pub chan: ConstellationChan,

/// Receives messages.
pub request_port: Receiver<ConstellationMsg>,
pub request_port: IpcReceiver<ConstellationMsg>,

/// A channel (the implementation of which is port-specific) through which messages can be sent
/// to the compositor.
Expand Down Expand Up @@ -135,6 +139,9 @@ pub struct Constellation<LTF, STF> {

/// A list of in-process senders to `WebGLPaintTask`s.
webgl_paint_tasks: Vec<Sender<CanvasMsg>>,

/// A list of child content processes.
child_processes: Vec<ChildProcess>,
}

/// State needed to construct a constellation.
Expand Down Expand Up @@ -238,6 +245,11 @@ enum ExitPipelineMode {
Force,
}

enum ChildProcess {
Sandboxed(gaol::platform::process::Process),
Unsandboxed(process::Child),
}

impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
pub fn start(state: InitialConstellationState) -> ConstellationChan {
let (constellation_port, constellation_chan) = ConstellationChan::new();
Expand Down Expand Up @@ -280,6 +292,7 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
webdriver: WebDriverData::new(),
canvas_paint_tasks: Vec::new(),
webgl_paint_tasks: Vec::new(),
child_processes: Vec::new(),
};
let namespace_id = constellation.next_pipeline_namespace_id();
PipelineNamespace::install(namespace_id);
Expand Down Expand Up @@ -309,10 +322,10 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
pipeline_id: PipelineId,
parent_info: Option<(PipelineId, SubpageId)>,
initial_window_size: Option<TypedSize2D<PagePx, f32>>,
script_channel: Option<Sender<ConstellationControlMsg>>,
script_channel: Option<IpcSender<ConstellationControlMsg>>,
load_data: LoadData) {
let spawning_paint_only = script_channel.is_some();
let (pipeline, mut pipeline_content) =
let (pipeline, unprivileged_pipeline_content, mut privileged_pipeline_content) =
Pipeline::create::<LTF, STF>(InitialPipelineState {
id: pipeline_id,
parent_info: parent_info,
Expand All @@ -332,12 +345,39 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
pipeline_namespace_id: self.next_pipeline_namespace_id(),
});

// TODO(pcwalton): In multiprocess mode, send that `PipelineContent` instance over to
// the content process and call this over there.
if spawning_paint_only {
pipeline_content.start_paint_task();
privileged_pipeline_content.start_paint_task();
} else {
pipeline_content.start_all::<LTF, STF>();
privileged_pipeline_content.start_all();

// Spawn the child process.
//
// Yes, that's all there is to it!
if opts::multiprocess() {
let (server, token) =
IpcOneShotServer::<IpcSender<UnprivilegedPipelineContent>>::new().unwrap();

// If there is a sandbox, use the `gaol` API to create the child process.
let child_process = if opts::get().sandbox {
let mut command = sandbox::Command::me().unwrap();
command.arg("--content-process").arg(token);
let profile = sandboxing::content_process_sandbox_profile();
ChildProcess::Sandboxed(Sandbox::new(profile).start(&mut command).expect(
"Failed to start sandboxed child process!"))
} else {
let path_to_self = env::current_exe().unwrap();
let mut child_process = process::Command::new(path_to_self);
child_process.arg("--content-process");
child_process.arg(token);
ChildProcess::Unsandboxed(child_process.spawn().unwrap())
};
self.child_processes.push(child_process);

let (_receiver, sender) = server.accept().unwrap();
sender.send(unprivileged_pipeline_content).unwrap();
} else {
unprivileged_pipeline_content.start_all::<LTF, STF>(false);
}
}

assert!(!self.pipelines.contains_key(&pipeline_id));
Expand Down Expand Up @@ -1199,7 +1239,7 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {

// Synchronously query the script task for this pipeline
// to see if it is idle.
let (sender, receiver) = channel();
let (sender, receiver) = ipc::channel().unwrap();
let msg = ConstellationControlMsg::GetCurrentState(sender, frame.current);
pipeline.script_chan.send(msg).unwrap();
let result = receiver.recv().unwrap();
Expand Down Expand Up @@ -1361,7 +1401,7 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
if let Some(root_frame_id) = self.root_frame_id {
let frame_tree = self.frame_to_sendable(root_frame_id);

let (chan, port) = channel();
let (chan, port) = ipc::channel().unwrap();
self.compositor_proxy.send(CompositorMsg::SetFrameTree(frame_tree,
chan,
self.chan.clone()));
Expand Down
8 changes: 8 additions & 0 deletions components/compositing/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![feature(box_syntax)]
#![feature(custom_derive)]
#![feature(iter_cmp)]
#![feature(plugin)]
#![feature(slice_bytes)]
#![feature(vec_push_all)]
#![feature(plugin)]
#![plugin(plugins)]

#![deny(unsafe_code)]
#![plugin(serde_macros)]

extern crate app_units;
#[macro_use]
Expand All @@ -30,6 +33,7 @@ extern crate core_text;

extern crate devtools_traits;
extern crate euclid;
extern crate gaol;
extern crate gfx;
extern crate gfx_traits;
extern crate gleam;
Expand All @@ -42,7 +46,10 @@ extern crate net_traits;
extern crate num;
extern crate offscreen_gl_context;
extern crate script_traits;
extern crate serde;
extern crate style_traits;

extern crate libc;
extern crate time;
extern crate url;

Expand All @@ -57,4 +64,5 @@ mod surface_map;
pub mod compositor_task;
pub mod constellation;
pub mod pipeline;
pub mod sandboxing;
pub mod windowing;
Loading

0 comments on commit c67884c

Please sign in to comment.