Skip to content

Replace FlowRef with Arc<Flow>, now that Arc supports DST. - #7237

Merged
bors-servo merged 3 commits into
masterfrom
arc-flow
Aug 21, 2015
Merged

bors-servo merged 3 commits into
masterfrom
arc-flow

Conversation

@SimonSapin

Copy link
Copy Markdown
Member

… and WeakFlowRef with Weak<Flow>.

r? @pcwalton

Review on Reviewable

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Aug 15, 2015
@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
  • These commits modify layout code, but no reftests are modified. Please consider adding a reftest!

@SimonSapin

Copy link
Copy Markdown
Member Author

@pcwalton In components/layout/parallel.rs, what makes it safe to to transmute to UnsafeFlow and back? Won’t ever flows be destroyed by the time they’re used?

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #7177) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added S-needs-rebase There are merge conflict errors. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Aug 17, 2015
@pcwalton

Copy link
Copy Markdown
Contributor

I'm uncomfortable with the amount of unsafe code this introduces throughout layout. The whole idea of parallel layout is that you are allowed to mutably access the flow you're working on only (as well as its children, but not its siblings). That's why I (tried to) forbid FlowRefs from leaking into the guts of layout.

One way to do this would be to encapsulate the Arc-to-FlowRef transmutation into the traversal module and have it hand an &mut Flow to the kernel functions. Later on, when we want to eliminate the undefined behavior, we can switch Flow to be an UnsafeCell.

@SimonSapin

Copy link
Copy Markdown
Member Author

I'm uncomfortable with the amount of unsafe code this introduces throughout layout

DerefMut for FlowRef was unsound, according to #6503. My understanding is that this PR introduces more unsafe blocks, but only for code that was already unsafe. I could make the flow_ref::deref_mut function non-unsafe, but it would only be sweeping #6503 back under the metaphorical carpet.

The whole idea of parallel layout is that you are allowed to mutably access the flow you're working on only

How can we make sure this rule is not violated in the current code or when reviewing layout code changes?

Arc-to-FlowRef transmutation

FlowRef is Arc<Flow> in this PR. Do you mean unsafe deref of Arc<Flow> to &mut Flow?

Later on, when we want to eliminate the undefined behavior, we can switch Flow to be an UnsafeCell.

I don’t understand how UnsafeCell would be used, can you explain some more?

@pcwalton

Copy link
Copy Markdown
Contributor

DerefMut for FlowRef was unsound, according to #6503.

It's only unsound in that it relies on undefined behavior (that the Rust compiler doesn't currently exploit, and likely won't for a long time). It's not actually unsafe, because FlowRefs never leak to the guts of layout. This is something that was designed a couple of years ago; the comments in #6503 are missing all the background.

How can we make sure this rule is not violated in the current code or when reviewing layout code changes?

One way would be to encapsulate the transmutation from FlowRef to &mut Flow into the traversal function and to have every layout function receive an &mut Flow as an argument. That way, the unsafe transmutation is only in one place, and additional layout code never has to grow more unsafe code.

SimonSapin added a commit that referenced this pull request Aug 18, 2015
@SimonSapin

Copy link
Copy Markdown
Member Author

Getting &mut T out of Arc<T> is unsafe if any other reference could be dereferenced at the same time. If we happen to never do that we’re good (Arc::get_mut does this when the given Arc is the only reference), but I’d like to have more assurances that this is the case.

In the mean time, at least having flow_ref::deref_mut be a function (instead of a DerefMut impl that can be called implicitly) make this more visible. I’ve removed its unsafe marker. How does this look?

@pcwalton

Copy link
Copy Markdown
Contributor

The tree structure should ensure that it's safe. However, that crucially depends on the guts of layout never being able to see the FlowRefs of siblings and parents (and absolute containing block links). The current code is (or at least was) carefully crafted to try to avoid this (hence all the comments around ContainingBlockLink).

@SimonSapin SimonSapin removed the S-needs-rebase There are merge conflict errors. label Aug 18, 2015
@SimonSapin

Copy link
Copy Markdown
Member Author

Ok. So how does this PR look now?

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #7294) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added S-needs-rebase There are merge conflict errors. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Aug 20, 2015
SimonSapin added a commit that referenced this pull request Aug 20, 2015
@SimonSapin

Copy link
Copy Markdown
Member Author

Rebased.

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #7265) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added S-needs-rebase There are merge conflict errors. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Aug 20, 2015
SimonSapin added a commit that referenced this pull request Aug 20, 2015
@SimonSapin

Copy link
Copy Markdown
Member Author

Rebased.

Comment thread components/layout/flow_ref.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you will succeed. The reference count will pretty much never be 1, since it's a doubly-linked tree.

@pcwalton

Copy link
Copy Markdown
Contributor

This looks OK except for the goal in the comment. I think that we should instead fix the undefined behavior problem in the long term (with UnsafeCell) and prevent FlowRef from leaking to the rest of layout. One easy way we could do this is to use privacy to prevent deref_mut() from being called outside of certain modules such as the traversal module. At a glance, that looks like it leaves construct.rs and the various fragments that contain flows. We could conceivably make restyle_damage a Cell to eliminate a whole bunch of deref_muts. For the fragments that contain flows, the flows should always be immediate children of the parent flow, so we could just assert that before handing out the deref_mut. (To be specific, we would have a public fn deref_child_flow_mut(parent: &mut Flow, child: &FlowRef) -> &mut Flow that asserts that child is an immediate child of parent before handing back the &mut Flow.)

@SimonSapin

Copy link
Copy Markdown
Member Author

I’ve rephrased the comment.

I don’t understand what you mean with UnsafeCell. It’s not gonna help if two threads try to mutate the same flow at the same time. And cells can’t (usefully) be used inside Arc as they’re !Sync.

@pcwalton

Copy link
Copy Markdown
Contributor

Two threads can't mutate the same flow at the same time due to the tree structure.

@SimonSapin

Copy link
Copy Markdown
Member Author

@bors-servo r=pcwalton

<SimonSapin> pcwalton: so, can we land #7237 now?
<pcwalton> yeah, it looks OK to me

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 21d6931 has been approved by pcwalton

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 21d6931 with merge 1f9778f...

bors-servo pushed a commit that referenced this pull request Aug 21, 2015
Replace FlowRef with Arc<Flow>, now that Arc supports DST.

… and `WeakFlowRef` with `Weak<Flow>`.

r? @pcwalton

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7237)
<!-- Reviewable:end -->
@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. S-needs-rebase There are merge conflict errors. labels Aug 21, 2015
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, gonk, linux1, linux2, mac1, mac2, mac3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants