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

servo_arc: bad drop in WASM target #129

Open
noahbald opened this issue Mar 1, 2025 · 4 comments
Open

servo_arc: bad drop in WASM target #129

noahbald opened this issue Mar 1, 2025 · 4 comments

Comments

@noahbald
Copy link

noahbald commented Mar 1, 2025

Sorry if this issue is a bit vague because I'm having a hard time finding the exact cause.
I'm compiling to WASM using wasm-bindgen 0.2.100 and when running I receive the following panic

panicked at /rust/deps/dlmalloc-0.2.7/src/dlmalloc.rs:1198:13:
assertion failed: psize <= size + max_overhead

And this seems to happening when I create a selector with a selector list, e.g.

pub struct Parser<E: crate::element::Element> {
    element: PhantomData<E>,
}

impl<'i, E: crate::element::Element> selectors::parser::Parser<'i> for Parser<E> {
    type Impl = E::Impl;
    type Error = SelectorParseErrorKind<'i>;
}

impl<E: crate::element::Element> Default for Parser<E> {
    fn default() -> Self {
        Self {
            element: PhantomData,
        }
    }
}

impl<E: crate::element::Element> Selector<E> {
    pub fn matches_with_scope_and_cache(
        &self,
        element: &E,
        scope: Option<E>,
        selector_caches: &mut SelectorCaches,
    ) -> bool {
        let context = &mut matching::MatchingContext::new(
            matching::MatchingMode::Normal,
            None,
            selector_caches,
            matching::QuirksMode::NoQuirks,
            matching::NeedsSelectorFlags::No,
            matching::MatchingForInvalidation::No,
        );
        context.scope_element = scope.map(|x| selectors::Element::opaque(&x));
        matching::matches_selector_list(&self.0, element, context)
    }
}

#[test]
fn bad_demalloc() {
  // This will panic in wasm
  Selector::new( "script,a" )
}

related: rustwasm/wasm-pack#1389

@noahbald
Copy link
Author

noahbald commented Mar 5, 2025

https://github.com/noahbald/stylo-selector-fail-129
Minimal reproduction using scraper

@noahbald
Copy link
Author

noahbald commented Mar 6, 2025

I've found this line to be the source of the panic

std::ptr::drop_in_place(inner);

@noahbald
Copy link
Author

noahbald commented Mar 6, 2025

I've found that adding the following to /selectors/Cargo.toml resolves the issue

[target.'cfg(target_arch = "wasm32")'.dependencies.servo_arc]
path = "../servo_arc"
features = ["track_alloc_size"]

Any maintainer know if this is a reasonable fix?

@noahbald noahbald changed the title selectors: bad malloc when compiling to WASM servo_arc: bad drop in WASM target Mar 6, 2025
@noahbald
Copy link
Author

noahbald commented Mar 6, 2025

Also, updating my crate's Cargo.toml to include servo_arc with track_alloc_size enabled fixes the issue, so a patch may not be needed. Perhaps should be added to docs?

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

No branches or pull requests

1 participant