Skip to content

Access browsing context safely - #15906

Merged
bors-servo merged 1 commit into
servo:masterfrom
mchv:mchv-safe-browsing-context
Mar 16, 2017
Merged

bors-servo merged 1 commit into
servo:masterfrom
mchv:mchv-safe-browsing-context

Conversation

@mchv

@mchv mchv commented Mar 10, 2017 •

Copy link
Copy Markdown
Contributor

Current browsing context accessor in Document unwraps the browsing context Option which prevents document to handle correctly the case when there is no browsing context.

This is the reason servo panics with session-history.max-length=1 (#15877).

As it is my first contribution, I added a safe method to retrieve the browsing context rather than change the existing method, but I am happy to change if you think this is the right approach. I did not as well replace all existing method call to the safe method, to focus on fixing the issue.

If someone can give me a bit of guidance for the test, I will try to contribute one.



This change is Reviewable

@highfive

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @emilio (or someone else) soon.

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @fitzgen: components/script/dom/document.rs, components/script/dom/window.rs
  • @KiChjang: components/script/dom/document.rs, components/script/dom/window.rs

@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 10, 2017
Comment thread components/script/dom/window.rs Outdated

// Steps 1. and 2.
if self.browsing_context().is_discarded() {
if self.safe_browsing_context().map(|c| c.is_discarded()).unwrap_or(true) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: you can use map_or(true, |c| c.is_discarded())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@emilio thanks updated the code

@emilio

emilio commented Mar 10, 2017

Copy link
Copy Markdown
Member

r? @asajeffrey

@highfive highfive assigned asajeffrey and unassigned emilio Mar 10, 2017
@mchv
mchv force-pushed the mchv-safe-browsing-context branch from 2bc2a5d to 3ece595 Compare March 10, 2017 16:05
@asajeffrey

Copy link
Copy Markdown
Contributor

My take on how we got here...

The only place we're creating a Window is in https://github.com/servo/servo/blob/master/components/script/script_thread.rs#L1674, and we immediately call init_browsing_context at https://github.com/servo/servo/blob/master/components/script/script_thread.rs#L1703 or https://github.com/servo/servo/blob/master/components/script/script_thread.rs#L1708.

The only thing I can think of is that we're accessing the Window after it's been discarded https://github.com/servo/servo/blob/master/components/script/dom/window.rs#L272, which is certainly possible if the Document calls methods on the Window after the browsing context has been discarded.

Since it is possible for (e.g.) a Document to be accessed after its browsing context has been discarded, it looks like the thing to do is to make sure that Window methods are safe to call even if there is no browsing context.

This PR will certainly help for that! We could be more drastic, and just make wiindow.browsing_context() return an Option<BrowsingContext>, but that's not a must-do.

@mchv: thanks for looking into this!

@asajeffrey asajeffrey left a comment

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.

Looks good! Just some minor nits.

Comment thread components/script/dom/window.rs Outdated
&self.image_cache_thread
}

/// Retrieve unsafely the browsing context

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.

Add a comment, something like "This can panic if it is called after the browsing context has been discarded".

Comment thread components/script/dom/window.rs Outdated
fn GetParent(&self) -> Option<Root<BrowsingContext>> {
// Steps 1. and 2.
if self.browsing_context().is_discarded() {
if self.safe_browsing_context().map_or(true, |c| c.is_discarded()) {

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.

Can this be map_or(true, BrowsingContext::is_discarded)?

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.

No it can't, because BrowsingContext::is_discarded takes a &BrowsingContext and the contents of the option is a Root<BrowsingContext>.

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.

Oh that's annoying, oh well.

Comment thread components/script/dom/window.rs Outdated
fn GetTop(&self) -> Option<Root<BrowsingContext>> {
// Steps 1. and 2.
if self.browsing_context().is_discarded() {
if self.safe_browsing_context().map_or(true, |c| c.is_discarded()) {

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.

Ditto?

@nox nox left a comment

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.

Some questions.

Comment thread components/script/dom/window.rs Outdated
self.browsing_context.get().unwrap()
}

pub fn safe_browsing_context(&self) -> Option<Root<BrowsingContext>> {

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.

The only place this method is used outside of dom::window is in Document::browsing_context, which checks whether self.has_browsing_context is true.

Why is self.has_browsing_context ever true without a corresponding browsing context in the document's window?

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.

The problem is that document.has_browsing_context is immutable, so if a document originally had a browsing context, but then that browsing context was discarded, we don't update the document. We could make document.has_browsing_context mutable, or we could just remove it and replace it by a method which checks to see if the window exists and has a browsing context.

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.

Given a document can now lose its browsing context, has_browsing_context should probably not be immutable.

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.

The problem is we're being a bit vague about the distinction between not having a browsing context, and having one that's been discarded. Sigh.

Comment thread components/script/dom/window.rs Outdated
fn GetParent(&self) -> Option<Root<BrowsingContext>> {
// Steps 1. and 2.
if self.browsing_context().is_discarded() {
if self.safe_browsing_context().map_or(true, |c| c.is_discarded()) {

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.

No it can't, because BrowsingContext::is_discarded takes a &BrowsingContext and the contents of the option is a Root<BrowsingContext>.

Comment thread components/script/dom/window.rs Outdated
&self.image_cache_thread
}

/// Retrieve unsafely the browsing context

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.

Don't use the word "unsafely", which has a precise meaning in Rust.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@nox sure I will update

Comment thread components/script/dom/window.rs Outdated
self.browsing_context.get().unwrap()
}

pub fn safe_browsing_context(&self) -> Option<Root<BrowsingContext>> {

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.

Please rename method to something like maybe_browsing_context or something like that. Safety is a different concept.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@nox make sense I will do the update

Current browsing context accessor in Document unwrap the browsing context option which prevents document to handle correctly the case when there is no browsing context.
@mchv
mchv force-pushed the mchv-safe-browsing-context branch from 3ece595 to f8c7235 Compare March 14, 2017 14:22
@mchv

mchv commented Mar 14, 2017

Copy link
Copy Markdown
Contributor Author

@nox @asajeffrey I have updated the code based on your comments. Let me know if you think we need other changes or add tests before being ready to merge.

@asajeffrey

Copy link
Copy Markdown
Contributor

LGTM. @bors-servo r+

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit f8c7235 has been approved by asajeffrey

@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. labels Mar 16, 2017
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit f8c7235 with merge e62d029...

bors-servo pushed a commit that referenced this pull request Mar 16, 2017
Access browsing context safely

Current browsing context accessor in `Document` unwraps the browsing context `Option` which prevents document to handle correctly the case when there is no browsing context.

This is the reason servo panics with `session-history.max-length=1` (#15877).

As it is my first contribution, I added a `safe` method to retrieve the browsing context rather than change the existing method, but I am happy to change if you think this is the right approach. I did not as well replace all existing method call to the `safe` method, to focus on fixing the issue.

If someone can give me a bit of guidance for the test, I will try to contribute one.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [X] These changes fix #15877.
- [ ] There are tests for these changes

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/15906)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css, mac-rel-wpt1, mac-rel-wpt2, windows-gnu-dev, windows-msvc-dev
Approved by: asajeffrey
Pushing e62d029 to master...

@bors-servo
bors-servo merged commit f8c7235 into servo:master Mar 16, 2017
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Mar 16, 2017
@asajeffrey

Copy link
Copy Markdown
Contributor

Yay!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Servo panics with session-history.max-length=1

6 participants