You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove the implementation of DerefMut for FlowRef. #6503
This can't be easily removed. I tried this as well and gave up. The core issue is that during flow construction we need to mutate these unsafely, but once hte flows are constructed, we use them without DerefMut. We want to avoid constructing the tree in one data type and then converting it to another that is then shared. I'm not sure the best way to get around this. @pcwalton may have some thoughts; he wrote the original code.
@Ms2ger That may work. I think get_mut was not available on Arc when I tried this, although I had a discussion with @aturon about this in Whistlier, and perhaps they've added it now.
@SimonSapin I don't think we're willing to pay the price of locking. All the fancy unsafety in layout is designed around Flows, and slowing down Flow access is probably a non-starter.
In any case, I agree this needs to be done; I was only trying to point out that it's more difficult than it seems at first.
Ah, yes, refcount for mutable access made me think of RefCell, which in multi-thread translates to RwLock, but you’re right that we can have something like Arc::get_mut instead.
Is there a reason not to use Arc directly? Arc::get_mut is still unstable but now un-deprecated. (The data race has been solved in rust-lang/rust#26610.)
Using Arc directly is the desired outcome. How to reconcile this with flow construction is an exercise no one has completed yet. I'm pretty sure when I tried was right after they removed get_mut but before they fixed the race and put it back. It's worth trying again.
@SimonSapin It should be noted that @aturon was interested in helping us solve this, so if get_mut is insufficient, we should definitely talk to the Rust team about what options we have.
Calling flow_ref::deref_mut doesn't lead to undefined behavior unless the Arc in question is dereferenced again while the return value of deref_mut is still in scope. Arc::get_mut dynamically proves the safety using the reference count, but you can use a different invariant if you're willing to write unsafe code.
The current implementation of reflow depends on a tree property to make its usage essentially safe; using Box instead of Arc, you can implement something similar to the current flow tree without using any unsafe code. (As far as I can tell, there are currently three places which violate the tree-ness of flows in layout: ContainingBlockLink, layout_debug, and PrivateLayoutData. ContainingBlockLink is essentially a shortcut to avoid the cost of propagating certain reflow information down the tree, layout_debug is just for debugging, and PrivateLayoutData is never used during reflow because the DOM can't be accessed during layout.)
I think the usage of ContainingBlockLink alongside flow_ref::deref_mut leads to undefined behavior, although probably not in a way the compiler will actually ever break. (Strictly speaking, when an mutable reference is in scope, all accesses to the referenced data must go through that reference.)
Given that Flows form a tree, there's very little benefit to making them Arcs as opposed to Boxes.
warning: lifetime parameter `'a` declared on fn `flow_ref::deref_mut` appears only in the return type, but here is required to be higher-ranked, which means that `'a` must appear in both argument and return types, #[warn(hr_lifetime_in_assoc_type)] on by default
--> /Users/nox/src/servo/components/layout/flow_list.rs:124:28
|>
124 |> self.it.next().map(flow_ref::deref_mut)
|> ^^^^^^^^^^^^^^^^^^^
I just tried compiling servo with the current Rust beta 1.78 and encountered the deny by default lint invalid_reference_casting :
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> components/layout/flow_ref.rs:55:18
|
55 | unsafe { &mut *(ptr as *mut dyn Flow) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
= note: even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get`
= note: `#[deny(invalid_reference_casting)]` on by default
This leaves the problem that the caller has responsibility to ensure no more than one &mut dyn Flow exist at the same time, so it would be more "honest" to make this an unsafe fn to signal this.
Activity
SimonSapin commentedon Jul 15, 2015
I tried just removing the
DerefMutimpl. After getting past a few initial errors, I got a bunch more :(metajack commentedon Jul 15, 2015
This can't be easily removed. I tried this as well and gave up. The core issue is that during flow construction we need to mutate these unsafely, but once hte flows are constructed, we use them without DerefMut. We want to avoid constructing the tree in one data type and then converting it to another that is then shared. I'm not sure the best way to get around this. @pcwalton may have some thoughts; he wrote the original code.
Ms2ger commentedon Jul 15, 2015
get_mutwith a runtime refcount check?SimonSapin commentedon Jul 15, 2015
So
Arc<RwLock>?metajack commentedon Jul 15, 2015
@Ms2ger That may work. I think get_mut was not available on Arc when I tried this, although I had a discussion with @aturon about this in Whistlier, and perhaps they've added it now.
@SimonSapin I don't think we're willing to pay the price of locking. All the fancy unsafety in layout is designed around Flows, and slowing down Flow access is probably a non-starter.
In any case, I agree this needs to be done; I was only trying to point out that it's more difficult than it seems at first.
SimonSapin commentedon Jul 15, 2015
Ah, yes, refcount for mutable access made me think of
RefCell, which in multi-thread translates toRwLock, but you’re right that we can have something likeArc::get_mutinstead.Is there a reason not to use
Arcdirectly?Arc::get_mutis still unstable but now un-deprecated. (The data race has been solved in rust-lang/rust#26610.)metajack commentedon Jul 15, 2015
Using Arc directly is the desired outcome. How to reconcile this with flow construction is an exercise no one has completed yet. I'm pretty sure when I tried was right after they removed get_mut but before they fixed the race and put it back. It's worth trying again.
metajack commentedon Jul 15, 2015
@SimonSapin It should be noted that @aturon was interested in helping us solve this, so if get_mut is insufficient, we should definitely talk to the Rust team about what options we have.
aturon commentedon Jul 16, 2015
Yep, happy to help further on this, just let me know what the blockers are.
Ms2ger commentedon Jul 23, 2015
get_mutisn't going to cut it; we do call into this even with refcounts greater than one. This is plain and simple UB.metajack commentedon Jul 27, 2015
@Ms2ger What is UB?
jdm commentedon Jul 27, 2015
Undefined behaviour.
Replace the unsound `impl DerefMut for FlowRef` with an unsafe function.
3 remaining items
Replace the unsound `impl DerefMut for FlowRef` with an unsafe function.
Replace the unsound `impl DerefMut for FlowRef` with an unsafe function.
eefriedman commentedon Sep 26, 2015
Some additional observations:
flow_ref::deref_mutdoesn't lead to undefined behavior unless the Arc in question is dereferenced again while the return value ofderef_mutis still in scope.Arc::get_mutdynamically proves the safety using the reference count, but you can use a different invariant if you're willing to write unsafe code.flow_ref::deref_mutleads to undefined behavior, although probably not in a way the compiler will actually ever break. (Strictly speaking, when an mutable reference is in scope, all accesses to the referenced data must go through that reference.)nox commentedon Jun 6, 2016
This starts warning:
Auto merge of #14053 - notriddle:flow_ref_unsafe, r=<try>
Auto merge of #14053 - notriddle:flow_ref_unsafe, r=<try>
Auto merge of #14053 - notriddle:flow_ref_unsafe, r=<try>
Auto merge of #14053 - notriddle:flow_ref_unsafe, r=pcwalton
mbrubeck commentedon Dec 14, 2017
Shouldn't
FlowRef::deref_mutat least be anunsafe fn?jschwe commentedon Apr 18, 2024
I just tried compiling servo with the current Rust beta 1.78 and encountered the deny by default lint
invalid_reference_casting:SimonSapin commentedon Apr 19, 2024
This solution to this always being UB is to use
Arc::as_ptrwhich does not go through&dyn Flow:This leaves the problem that the caller has responsibility to ensure no more than one
&mut dyn Flowexist at the same time, so it would be more "honest" to make this anunsafe fnto signal this.Address issues uncovered by rust-1.78 beta
Address issues uncovered by rust-1.78 beta (#32130)
jdm commentedon Sep 9, 2025
FlowRef doesn't exist in the new engine.