Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert that DOM structs have the correct first field #21105

Merged
merged 1 commit into from Jul 3, 2018

Conversation

@Manishearth
Copy link
Member

Manishearth commented Jun 29, 2018

Not having the right field can lead to fun bugs like ferjm#1 where the struct gets mis-reinterpreted as what should be its parent (but is not layout-wise)


This change is Reviewable

@highfive
Copy link

highfive commented Jun 29, 2018

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/filereadersync.rs
  • @KiChjang: components/script/dom/bindings/codegen/CodegenRust.py, components/script/dom/filereadersync.rs
@highfive
Copy link

highfive commented Jun 29, 2018

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!
@KiChjang
Copy link
Member

KiChjang commented Jun 30, 2018

Huh, I thought that we already perform this check... Perhaps it isn't robust enough and it only checks whether the first field is also a DOM struct?

@nox
Copy link
Member

nox commented Jul 3, 2018

Please describe what the changes do in the commit message, and maybe show what is generated here. I don't like how there is a special case for some canvas-related interface in the middle of codegen with this PR.


return """\
impl %(selfName)s {
fn __assert_parent_type(&self) {

This comment has been minimized.

Copy link
@nox

nox Jul 3, 2018

Member

I would rather encode the relation with a trait specifying from which interface it is extended.

This comment has been minimized.

Copy link
@Manishearth

Manishearth Jul 3, 2018

Author Member

You still need to assert the existence of this impl somehow, though. Thoughts?

@nox
Copy link
Member

nox commented Jul 3, 2018

Also please don't write r? in PR descriptions.

@Manishearth
Copy link
Member Author

Manishearth commented Jul 3, 2018

Regarding canvas, I discussed this with @asajeffrey , who had written that, and for efficiency the paint rendering context struct internally "inherits" from the canvas rendering context, despite not webidl inheriting. This is still safe because canvas eventually inherits from EventTarget, so the layouts work out.

Regarding r? , I thought that was only for when the sync service was running, which it isn't anymore.

@nox
Copy link
Member

nox commented Jul 3, 2018

Regarding canvas, I discussed this with @asajeffrey, who had written that, and for efficiency the paint rendering context struct internally "inherits" from the canvas rendering context, despite not webidl inheriting. This is still safe because canvas eventually inherits from EventTarget, so the layouts work out.

You can use [Inline] on a fake WebIDL interface, like I did for Window and WorkerGlobalScope which both inherit from GlobalScope which doesn't actually exist on the JS side.

Regarding r?, I thought that was only for when the sync service was running, which it isn't anymore.

Oh right.

@Manishearth Manishearth force-pushed the Manishearth:dom-inheritance-assert branch from b5e847a to 0a8824a Jul 3, 2018
DOM structs embed their parent type as their first field. This
introduces a `.parent()` method to the DOM struct that returns its first
field, and codegens a type assert that ensures that `.parent()` returns
the parent struct.

This generates:

On `#[dom_struct]`:

```rust
impl HasParent for Type {
    type Parent = ParentType;
    fn as_parent(&self) -> ParentType {
        &self.first_field
    }
}
```

In the codegen files:

```rust
impl Type {
    fn __assert_parent_type(&self) {
        let _: &ParentType = self.as_parent();
    }
}
````
@Manishearth Manishearth force-pushed the Manishearth:dom-inheritance-assert branch from 0a8824a to ad19899 Jul 3, 2018
@Manishearth
Copy link
Member Author

Manishearth commented Jul 3, 2018

Created a HasParent trait and fixed the commit message. Left a FIXME for the [Inline]

@bors r=nox

@Manishearth
Copy link
Member Author

Manishearth commented Jul 3, 2018

@bors-servo r=nox

@bors-servo
Copy link
Contributor

bors-servo commented Jul 3, 2018

📌 Commit ad19899 has been approved by nox

@highfive highfive assigned nox and unassigned SimonSapin Jul 3, 2018
@bors-servo
Copy link
Contributor

bors-servo commented Jul 3, 2018

Testing commit ad19899 with merge 3c43fa6...

bors-servo added a commit that referenced this pull request Jul 3, 2018
Assert that DOM structs have the correct first field

Not having the right field can lead to fun bugs like ferjm#1 where the struct gets mis-reinterpreted as what should be its parent (but is not layout-wise)

<!-- 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/21105)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Jul 3, 2018

@bors-servo bors-servo merged commit ad19899 into servo:master Jul 3, 2018
3 checks passed
3 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@Manishearth Manishearth deleted the Manishearth:dom-inheritance-assert branch May 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants
You can’t perform that action at this time.