Skip to content

Parser overflows stack in WASM #4301

Open

Description

@Rafferty97

(Apologise for the duplication - accidentally created the ticket from the wrong GH account)

Describe the bug
When compiled to WASM (wasm-pack --debug), the parser exceeds the call stack when fed certain input. I presumed it was getting stuck in an infinite recursion loop, but after compiling the exact same code in release mode, the issue vanished.

This implies that the root cause is that, in debug mode, the parser is prone to overflowing the stack simply due to being very recursive in its implementation.

To Reproduce
This is the minimal Rust code I found that reproduces the bug:

#[wasm_bindgen]
pub fn fail_case() {
    let src = r#"
        const a = function () {
            b((c = d([])))
        }
    "#;

    let context = &mut boa_engine::Context::builder().build().unwrap();
    let source = boa_engine::Source::from_bytes(src);
    let _module = boa_engine::Module::parse(source, None, context).unwrap();
}

Compiling to WASM and calling from JS in Chrome yields this stack trace:

Image

Expected behavior
Successful parse without a stack overflow

Build environment (please complete the following information):

  • OS: MacOS
  • Version: Sequoia 15.5
  • Target triple: wasm32-unknown-unknown
  • Rustc version: rustc 1.87.0 (17067e9ac 2025-05-09)

Activity

  1. added
    A-Bug
    on Jun 24, 2025
  2. jasonwilliams commented on Jun 24, 2025

    @jasonwilliams
    Member

    Also related: #1402

  3. added
    C-Parser
    on Jun 24, 2025
  4. Rafferty97 commented on Jun 25, 2025

    @Rafferty97
    ContributorAuthor

    @jasonwilliams Is anyone actively looking into this issue? If not, I can take a look at the current parser implementation and see if there's any quick wins or what an ideal solution might look like.

  5. nekevss commented on Jun 25, 2025

    @nekevss
    Member

    I don't think anyone is currently looking into it. Feel free to take a look! 😄

  6. jasonwilliams commented on Jun 25, 2025

    @jasonwilliams
    Member

    @jasonwilliams Is anyone actively looking into this issue? If not, I can take a look at the current parser implementation and see if there's any quick wins or what an ideal solution might look like.

    No one is looking at this, so yes we would be grateful if you can.

    It looks like many expressions go through the same path which generates a deep stack:

    MultiplicativeExpression
    AdditiveExpression
    ShiftExpression
    RelationalExpression
    EqualityExpression
    BitwiseANDExpression
    BitwiseXORExpression
    BitwiseORExpression
    

    This is actually following the spec, so it’s correct behaviour but not performant.

    I think this could be flattened down to a loop instead. I don’t know for sure if this is the same issue you’re seeing, so don’t take my word for this, I know it’s the cause of the linked issue.

    Please report back here on any findings, we can most likely help with any direction.

  7. Nikita-str commented on Jun 28, 2025

    @Nikita-str
    Contributor

    @Rafferty97 Hi, are you currently solving this issue?
    I have sketched out a possible solution that should prevent stack overflows in the parser entirely, but I assume it would likely require pretty much changes. So if you are already working on this — then ok, go ahead.

    @jasonwilliams
    However, in case if nobody is actively dealing with the issue rn, I would like to discuss the approach with someone who will be reviewing it(who should I tag in that case?), because of the amount of expected changes and it's similarity it would be better to adjust the solution on the start.

  8. Nikita-str commented on Jun 28, 2025

    @Nikita-str
    Contributor

    Meanwhile, how important is parser performance? Is it ok to degrade it, for example, 2 times? 1.5?

    Another question, why boa_parser::TokenParser<R> is Sized? Is this done on purpose? For what?

  9. jasonwilliams commented on Jun 28, 2025

    @jasonwilliams
    Member

    @Nikita-str I think the best strategy would be to open a new issue with your idea sketched out, then you can link to that if you had a draft PR. But an issue is the best starting point because we could discuss in that.

    If you wanted real time discussion we are all on the Matrix / Discord chat and you can ping us there.

    Meanwhile, how important is parser performance?

    Very

    Is it ok to degrade it, for example, 2 times? 1.5?

    I don’t think that would be acceptable, but I’m interested in what ideas you have in mind. Maybe we can find some solution that doesn’t kill performance so much.

  10. Rafferty97 commented on Jul 9, 2025

    @Rafferty97
    ContributorAuthor

    @Rafferty97 Hi, are you currently solving this issue? I have sketched out a possible solution that should prevent stack overflows in the parser entirely, but I assume it would likely require pretty much changes. So if you are already working on this — then ok, go ahead.

    @jasonwilliams However, in case if nobody is actively dealing with the issue rn, I would like to discuss the approach with someone who will be reviewing it(who should I tag in that case?), because of the amount of expected changes and it's similarity it would be better to adjust the solution on the start.

    @Nikita-str Apologies for the delayed reply. No, I haven't had the time to actively work on this unfortunately, so please feel free to take it off my hands if you think you've got a solution in mind.

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

Metadata

Metadata

Assignees

Labels

A-BugC-Parser

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions