oxc/crates
overlookmotel f2f31a8d7a
fix(traverse)!: remove unsound APIs (#7514)
It's essential to `oxc_traverse`'s safety scheme that the user cannot create a `TraverseAncestry`, because they could then substitute it for the one stored in `TraverseCtx`, and cause a buffer underrun when an ancestor gets popped off stack which should never be empty - but it is because user has sneakily swapped it for another one.

Not being able to create a `TraverseAncestry` also requires that user cannot obtain an owned `TraverseCtx` either, because you can obtain an owned `TraverseAncestry` from an owned `TraverseCtx`.

Therefore, it's unsound for `TraverseCtx::new` to be public.

However, it is useful in minifier to be able to re-use the same `TraverseCtx` over and over, which requires having an owned `TraverseCtx`.

To support this use case, introduce `ReusableTraverseCtx`. It is an opaque wrapper around `TraverseCtx`, which prevents accessing the `TraverseCtx` inside it. It's safe for user to own a `ReusableTraverseCtx`, because there's nothing they can do with it except for using it to traverse via `traverse_mut_with_ctx`, which ensures the safety invariants are upheld.

At some point, we'll hopefully be able to reduce the number of passes in the minifier, and so remove the need for `ReusableTraverseCtx`.But in the meantime, this keeps `Traverse`'s API safe from unsound abuse.

Note: Strictly speaking, there is still room to abuse the API and produce UB by initiating a 2nd traversal of a different AST in an `Traverse` visitor, and then `mem::swap` the 2 x `&mut TraverseCtx`s. But this is a completely bizarre thing to do, and would basically require you to write malicious code specifically designed to cause UB, so it's not a real risk in practice. We'd need branded lifetimes to close that hole too.

So this PR doesn't 100% ensure safety in a formal sense, but it at least makes it very hard to trigger UB *by accident*, which was the risk before.
2024-11-28 10:30:53 +08:00
..
oxc release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_allocator release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_ast feat(ast)!: add StringLiteral::raw field (#7393) 2024-11-26 23:33:56 +08:00
oxc_ast_macros release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_cfg release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_codegen release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_data_structures release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_diagnostics release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_ecmascript feat(ast)!: add StringLiteral::raw field (#7393) 2024-11-26 23:33:56 +08:00
oxc_estree release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_isolated_declarations feat(ast)!: add StringLiteral::raw field (#7393) 2024-11-26 23:33:56 +08:00
oxc_language_server feat(language_server): Add code actions to disable rules for the current line or entire file (#6968) 2024-11-28 10:30:53 +08:00
oxc_linter refactor(linter): remove once_cell (#7510) 2024-11-28 10:30:52 +08:00
oxc_macros feat(linter): allow lint rules with the same name (#7496) 2024-11-27 04:25:05 +00:00
oxc_mangler release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_minifier fix(traverse)!: remove unsound APIs (#7514) 2024-11-28 10:30:53 +08:00
oxc_module_lexer release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_parser feat(ast)!: add StringLiteral::raw field (#7393) 2024-11-26 23:33:56 +08:00
oxc_prettier refactor(prettier): Refactor IR related macros (#7491) 2024-11-26 09:31:30 +00:00
oxc_regular_expression release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_semantic test(semantic): add a test for UpdateExpression (#7495) 2024-11-27 02:00:52 +00:00
oxc_sourcemap release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_span release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_syntax release(crates): v0.38.0 (#7490) 2024-11-26 14:38:47 +08:00
oxc_transformer fix(traverse)!: remove unsound APIs (#7514) 2024-11-28 10:30:53 +08:00
oxc_traverse fix(traverse)!: remove unsound APIs (#7514) 2024-11-28 10:30:53 +08:00
oxc_wasm ci: clean up wasm type check (#7466) 2024-11-25 17:56:17 +08:00