-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
AstBuilder::copy
is unsound
#3483
Comments
@Dunqing Please don't use this API from now on 😁 . Use |
I don't think it's necessarily high priority to fix this. I just wanted to request that we don't introduce more uses of it now, as it will make it more work to remove it later. |
As discussed in #3483, `AstBuilder::copy` is unsound. It's going to be a hard slog removing all uses of it. This PR adds a comment to please not introduce any further usages of it in meantime.
@overlookmotel Can we use the |
I think |
If we just want to take the sub-tree out and put it somewhere else, We should be able to use the move method instead. For all other cases if we don't clone the sub-tree and actually copy it in 2 places how can we prevent aliasing? |
Yes, that's true, if you want to duplicate the sub-tree and put it in AST in 2 places, it must be cloned or it's UB. But all the places I saw |
So what is stopping us from replacing all of our usages with the move method? Wouldn't it be better if we deprecate this unsound method? |
Nothing! We just haven't done it yet. Unfortunately, using move methods is quite unergonomic - but such is the ways of Rust. I would be really keen to see the back of this unsound method. I remain quite anxious about the soundness of |
Revert the changes to `oxc_traverse` made in #5577. Those changes may well have been good, but... The soundness of `Traverse` is a finely balanced thing, and the pointer gymnastics are subtle, so I think it's best to be extremely conservative about changes. I last ran Miri on it with the original formulation (`as *mut _` etc) and it passed. Currently it's not possible to usefully run Miri on the transformer because it contains known unsound code (#3483). So until we're able to check soundness with Miri, I think it's best to avoid changes, as it'd be easy to trigger UB unintentionally.
oxc/crates/oxc_ast/src/ast_builder.rs
Lines 71 to 79 in 7f7b5ea
This method can be used correctly when a node is being removed from AST anyway, but there are multiple ways in which it can lead to unsound situations:
Aliasing violation:
Mutation through immutable ref:
The double-free which #3481 fixes was likely caused by using
copy
.We could make the method
unsafe
and caller will have to ensure it's only used when safe to do so. However, personally I think we should remove the API entirely because (1) it is difficult to reason about when it's safe to use and is therefore a footgun and (2) the performance hit from using safe alternatives is very minimal.Unfortunately the alternatives are likely
take
ormem::replace
which are not so ergonomic.The text was updated successfully, but these errors were encountered: