Skip to content

use #[naked] for __rust_probestack #897

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

folkertdev
Copy link
Contributor

This should work now. #[naked] takes care of adding the directives that define_rust_probestack added.

I deliberately did not format/indent the assembly code to make review easier. I could do that in a separate commit it that is desirable.

Let's see if CI is OK with this.

@folkertdev folkertdev force-pushed the probestack-naked-function branch from fb31ab0 to 61be61f Compare May 1, 2025 13:34
core::arch::global_asm!(
define_rust_probestack!(
#[unsafe(naked)]
#[no_mangle]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that exporting this as a C symbol is important, but maybe it's not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, rustc hard codes the name of this function. It should probably be changed to use #[rustc_std_internal_symbol] mangling though, but that would require coordination between rustc and compiler-builtins.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually using #[no_mangle] would cause __rust_probestack to be exported from cdylibs, so I think it will have to be changed to #[rustc_std_internal_symbol] in this PR and changed on the rustc side when bumping the compiler-builtins dependency. It is currently not exported from cdylibs as rustc isn't aware that the global_asm!() exports __rust_probestack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm,

error[E0658]: this is an internal attribute that will never be stable
  --> compiler-builtins/src/probestack.rs:64:1
   |
64 | #[rustc_std_internal_symbol]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
   = note: this compiler was built on 2025-04-21; consider upgrading it if it is out of date

error[E0736]: attribute incompatible with `#[unsafe(naked)]`
  --> compiler-builtins/src/probestack.rs:64:1
   |
63 | #[unsafe(naked)]
   | ---------------- function marked with `#[unsafe(naked)]` here
64 | #[rustc_std_internal_symbol]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `rustc_std_internal_symbol` attribute is incompatible with `#[unsafe(naked)]`

I guess compiler-builtins can just enable #![feature(rustc_attrs)], but that second error needs a fix in rustc first.

define_rust_probestack!(
#[unsafe(naked)]
#[no_mangle]
pub unsafe extern "C" fn __rust_probestack() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you mention in the doc comment that the custom ABI is the reason this is unsafe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean in the module doc comment? We can add some specific details (about what registers are used etc) if we add a doc comment to the function itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to the comments above each of the functions, I guess they're not actually doc comments though they could be. Something like (hence the `unsafe`, since the ABI does not actually match `extern "C"`) in the paragraph describing ABI is sufficient, unless you want to add more. Just to have a note that it isn't supposed to be extern "C".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That reminded me of a few other cases we have where naked functions are extern "C" but that doesn't really make sense. Seems like a somewhat common issue, I wrote up rust-lang/rust#140566 as a possible way to make this more clear (not for this pr of course).

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 1, 2025
…d_internal_symbol, r=bjorn3

allow `#[rustc_std_internal_symbol]` in combination with `#[naked]`

The need for this came up in rust-lang/compiler-builtins#897, but in general this seems useful and valid to allow.

Based on a quick scan, I don't think changes to the generated assembly are needed.

cc `@bjorn3`
@tgross35
Copy link
Contributor

tgross35 commented May 1, 2025

Thanks for putting this together, this is a nice bit of simplification. After the rustc_std_internal_symbol change I guess it's probably easiest to wait until the beta bump to merge this, since that's in only ~8 days and avoids any bootstrap problems.

I deliberately did not format/indent the assembly code to make review easier. I could do that in a separate commit it that is desirable.

Feel free to do any cleanup you see fit. Split NFC commits are great, GH's "ignore whitespace" option on the review UI works for everything else 🙂

@folkertdev folkertdev force-pushed the probestack-naked-function branch from d70607d to 4d09a30 Compare May 1, 2025 19:27
@folkertdev folkertdev force-pushed the probestack-naked-function branch from 4d09a30 to a548e1f Compare May 1, 2025 19:35
@folkertdev
Copy link
Contributor Author

Hmm, formatting does not really help much, because formatting in asm is undefined. So let's leave it as it is.

I did merge the definition for sdx into the normal x86_64 version (one day that can use #[cfg()] in the asm block, for now a macro will do). That removes a bunch of duplication.

@bjorn3
Copy link
Member

bjorn3 commented May 1, 2025

After the rustc_std_internal_symbol change I guess it's probably easiest to wait until the beta bump to merge this, since that's in only ~8 days and avoids any bootstrap problems.

There will need to be a change on the rustc side anyway when updating compiler-builtins. #[rustc_std_internal_symbol] changes the exact symbol name, so the __rust_probestack reference inside cg_llvm needs to be wrapped in a mangle_internal_symbol call to match what compiler-builtins will define when it adds #[rustc_std_internal_symbol].

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 1, 2025
…d_internal_symbol, r=bjorn3

allow `#[rustc_std_internal_symbol]` in combination with `#[naked]`

The need for this came up in rust-lang/compiler-builtins#897, but in general this seems useful and valid to allow.

Based on a quick scan, I don't think changes to the generated assembly are needed.

cc ``@bjorn3``
@folkertdev
Copy link
Contributor Author

folkertdev commented May 1, 2025

Well currently the bootstrap compiler rejects #[unsafe(naked)], so we're using #[cfg(bootstrap)] to make that work in some other places in compiler-builtins. Similar, bootstrap would still reject #[rustc_std_internal_symbol] in combination with #[naked].

So yeah waiting for the new beta is probably easiest (not sure how rustweek impacts the schedule there, but we'll see)

VlaDexa added a commit to VlaDexa/rust that referenced this pull request May 2, 2025
…d_internal_symbol, r=bjorn3

allow `#[rustc_std_internal_symbol]` in combination with `#[naked]`

The need for this came up in rust-lang/compiler-builtins#897, but in general this seems useful and valid to allow.

Based on a quick scan, I don't think changes to the generated assembly are needed.

cc `@bjorn3`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 2, 2025
Rollup merge of rust-lang#140552 - folkertdev:naked-function-rustc_std_internal_symbol, r=bjorn3

allow `#[rustc_std_internal_symbol]` in combination with `#[naked]`

The need for this came up in rust-lang/compiler-builtins#897, but in general this seems useful and valid to allow.

Based on a quick scan, I don't think changes to the generated assembly are needed.

cc ``@bjorn3``
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants