-
Notifications
You must be signed in to change notification settings - Fork 1
Some notes about the panic #1
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
Comments
@edwin0cheng hi! Thanks for your interest in proc macro expander :) I have thought about this possibility too (the fact that there are two independent thread local variables), but, unfortunately, it does not explain why there is no panic when procedural macros itself is compiled with one rust version, and code which links it dynamically is compiled with another I do agree that it does not relate to ABI compatibility, and I am not sure whether it's a bug or very strange behavior (from my point of view) that is expected Also, the approach I am using in proc macro expander is mocked from the compiler itself; however, the compiler does not run into this kind of problems for some reason |
@fedochet It is so strange... For some reason i have to upgrade my Windows WSL from Ubuntu Xenial (16.04) to Bionic (18.04), and the bug is gone! And then i reinstall Xenial and confirm the bug still exists in there. I start to strongly suspect it is related how dynamic linkage works and will try to investigate further. |
Finally i found the actual reason of the bug. I can confirmed that the Why the static linkage is introduced ? Surprisingly it is introduced by
It is because in rustc they don't use use syntax::tokenstream::TokenStream;
// ....
impl base::AttrProcMacro for AttrProcMacro {
fn expand<'cx>(&self,
ecx: &'cx mut ExtCtxt<'_>,
span: Span,
annotation: TokenStream,
annotated: TokenStream)
-> TokenStream {
let server = proc_macro_server::Rustc::new(ecx);
match self.client.run(&EXEC_STRATEGY, server, annotation, annotated) {
// ....
} So that's why in server.rs, it use a Server trait : impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
pub fn run<S: Server>( So they don't introduce the static linkage of thread_local in their code. |
It also explains why there is no panic in here. Since if the version of static linkage are different from dynamically one, the dynamic linker will not try to merge its code path. So that both |
@edwin0cheng that's a nice investigation, thanks a lot! I hope that, if you're right, there's something that could be done about it to prevent such kind of linkage issues, so expander could use macros compiled with the same version of rustc. Let's see what Eddyb would say about your theory |
Seem like we can solve this problem by adding a flag More info: |
- see [this](fedochet/rust-proc-macro-panic-inside-panic-expample#1) issue for the details - use nightly for tests
@edwin0cheng I've tried to use RTLD_DEEPBIND and it indeed worked! Thanks again for your investigation on this question 👍 |
You are welcome :) Since I am planing to use However, note that |
3920: Implement expand_task and list_macros in proc_macro_srv r=matklad a=edwin0cheng This PR finish up the remain `proc_macro_srv` implementation : 1. Added dylib loading code for proc-macro crate dylib. Note that we have to add some special flags for unix loading because of a bug in old version of glibc, see fedochet/rust-proc-macro-panic-inside-panic-expample#1 and rust-lang/rust#60593 for details. 2. Added tests for proc-macro expansion: We use a trick here by adding `serde_derive` to dev-dependencies and calling `cargo-metadata` for searching its dylib path, and expand it in our tests. [EDIT] Note that this PR **DO NOT** implement the final glue code with rust-analzyer and proc-macro-srv yet. Co-authored-by: Edwin Cheng <[email protected]>
Hi, I am studying the https://github.com/fedochet/rust-proc-macro-expander which is an awesome project. And see the issue in rust-lang/rust#60593.
After a little bit of digging, my guess is, it is not related to ABI compatible problem. It seem to be related to the different of
thread_local
storage of static linkage and dynamic linkage.The actual panic is this line.
And
state
is actually athread_local
:https://github.com/rust-lang/rust/blob/dc6db14e1cd60012f25be4fd8d2eb96ea5b4bb68/src/libproc_macro/bridge/client.rs#L269
On the other hand, in your example code :
rust-proc-macro-panic-inside-panic-expample/src/main.rs
Line 164 in 6ef3a64
client.run
function will call Bridge::enter and set the state toBridgeState::Connected
. Note that this is statically link tomain
crate.And after that, when the actual TokenStream start to parse, Bridge::with will be called from shared library crate and it will trigger the panic.
It seem to be two thread local are different, such that the panic get raised. But I don't know enough about linux shared librrary and thread local mechanism to understand this bug.
The text was updated successfully, but these errors were encountered: