Skip to content

Disallow export_name starting with "llvm." #140837

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

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

Conversation

moxian
Copy link
Contributor

@moxian moxian commented May 9, 2025

Fixes #140822
Motivation in the issue, and in code comments.

This implementation adds the check to right to #[export_name] parsing code. I considered moving it closer to the place where the new name gets emitted to llvm, but that looked harder to get right, and easier to accidentally break going forward.

I don't believe any other attributes need this handling. The only one that comes close is link_name but that one uses the name, and does not define it, and in fact #[link_name = "llvm.stuff"] is entirely justified and actively used in real code.

Two small drive-by changes in separate commits (removed an obsolete and misleading //FIXME comment, and tighten the diagnostics span for the null-byte-in-export_name check). Happy to split them into dedicated PR(s) if this is deemed valuable.

This comment from 2017 says "every error should have an associated error code"
However, a more recent rustc-dev-guide doc from 2020 suggests that this
is not the case, and that errorcode-less errors are fine and sometimes
even preferable to errorcode-full ones:
rust-lang/rustc-dev-guide#967
@rustbot
Copy link
Collaborator

rustbot commented May 9, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented May 9, 2025

Some changes occurred in compiler/rustc_codegen_ssa/src/codegen_attrs.rs

cc @jdonszelmann

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 9, 2025
@rust-log-analyzer

This comment has been minimized.

@moxian

This comment was marked as outdated.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 9, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@moxian
Copy link
Contributor Author

moxian commented May 9, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 9, 2025
@rust-log-analyzer

This comment has been minimized.

@@ -52,6 +52,9 @@ codegen_ssa_expected_one_argument = expected one argument
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
codegen_ssa_export_name_llvm_intrinsic = exported symbol name must not start with "llvm."
.note = symbols starting with "llvm." are reserved for LLVM intrinsics and cannot be defined in user code
Copy link
Member

Choose a reason for hiding this comment

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

We don't have a concept of "user code" so let's not confuse users into believing there is one.

Suggested change
.note = symbols starting with "llvm." are reserved for LLVM intrinsics and cannot be defined in user code
.note = symbols starting with `llvm.` are reserved for LLVM intrinsics

@@ -52,6 +52,9 @@ codegen_ssa_expected_one_argument = expected one argument
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
codegen_ssa_export_name_llvm_intrinsic = exported symbol name must not start with "llvm."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
codegen_ssa_export_name_llvm_intrinsic = exported symbol name must not start with "llvm."
codegen_ssa_export_name_llvm_intrinsic = exported symbol name must not start with `llvm.`

(same below)

@jieyouxu
Copy link
Member

jieyouxu commented May 9, 2025

Note

Please ignore this nomination, as it's probably more of a bug-fix. Left unresolved for reference only.

Background

Rejecting #[export_name] whose value starts with llvm. seems very reasonable, but then it also feels at the same time somewhat arbitrary, because we now hard-error on "certain" #[export_name] values (namely, those that begin with llvm.). A few considerations:

  1. This was rejected / permitted in an oscillating fashion previously, as the rejection depended on verify-llvm-ir (i.e. the rust frontend was not the one "actively" (or rather, explicitly) performing the rejection).

  2. We almost certainly need to be rejecting this, because otherwise

    Creating a definition for an intrinsic is invalid LLVM IR, the rest is not really relevant. The Rust frontend must generate an error when trying to use an export_name starting with llvm..
    -- Target features getting erased when using #[export_name] with names of LLVM intrinsics #140822 (comment)

  3. We should make an explicit decision whether the rustc frontend should be explicitly rejecting this.

Possible breakage

I don't really expect any breakages (someone is likely doing some... weird things if this causes their code to no longer compile), but this is technically constraining the set of programs that we accept.

Nomination questions Some questions

  • (T-compiler, T-lang) Should we perform this explicit rejection for export_names that begin with llvm.?
  • (T-compiler, T-lang) Does the llvm. rejection also happen when the cg backend is e.g. cg_gcc? Or do we only reject llvm. when cg backend is cg_llvm?
    • Probably universally reject (across any and all cg backends, that is)?
  • (T-lang) If we universally reject, do we document this at the language level (Reference) as sth like "IR intrinsic name limitations"?

@rustbot rustbot added I-compiler-nominated Nominated for discussion during a compiler team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination I-lang-nominated Nominated for discussion during a lang team meeting. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 9, 2025
jieyouxu

This comment was marked as resolved.

@jieyouxu jieyouxu dismissed their stale review May 9, 2025 10:00

stale

@jieyouxu jieyouxu removed I-lang-nominated Nominated for discussion during a lang team meeting. I-compiler-nominated Nominated for discussion during a compiler team meeting. labels May 9, 2025
@jieyouxu jieyouxu removed I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 9, 2025
@jieyouxu
Copy link
Member

jieyouxu commented May 9, 2025

EDIT: I removed the lang nomination, probably more of a "bug fix" / "impl detail".

@Noratrieb
Copy link
Member

GitHub code search yields no results; https://github.com/search?q=%22export_name+%3D+%5C%22llvm.%22&type=code
(it finds stuff without the period so it's probably not wrong: https://github.com/search?q=%22export_name+%3D+%5C%22llvm%22&type=code)

this is such a weird and broken thing to do that I think we can just ship this without crater or FCW or anything

@lcnr
Copy link
Contributor

lcnr commented May 9, 2025

r? compiler

@rustbot rustbot assigned Nadrieril and unassigned lcnr May 9, 2025
@WaffleLapkin
Copy link
Member

Is there a reason this is handled in rustc_codegen_ssa and not rustc_codegen_llvm? I assume while weird, #[export_name = "llvm...."] should not break any other backends.

@Urgau
Copy link
Member

Urgau commented May 9, 2025

Is there a reason this is handled in rustc_codegen_ssa and not rustc_codegen_llvm? I assume while weird, #[export_name = "llvm...."] should not break any other backends.

Wouldn't it be weird if your code is working on one backend but not on another one?

@WaffleLapkin
Copy link
Member

@Urgau generally yes, but

  1. this might already happen
  2. the name overlaps with the backend's internal symbol, so I don't think in this case it's weird

@jieyouxu
Copy link
Member

jieyouxu commented May 9, 2025

@Urgau generally yes, but

1. this might already happen

2. the name overlaps with the backend's internal symbol, so I don't think in this case it's weird

Yeah, I should've left the nomination comment open, also asked

  • (T-compiler, T-lang) Should we perform this explicit rejection for export_names that begin with llvm.?
  • (T-compiler, T-lang) Does the llvm. rejection also happen when the cg backend is e.g. cg_gcc? Or do we only reject llvm. when cg backend is cg_llvm?
    • Probably universally reject (across any and all cg backends, that is)?
  • (T-lang) If we universally reject, do we document this at the language level (Reference) as sth like "IR intrinsic name limitations"?

@traviscross
Copy link
Contributor

  • If we universally reject, do we document this at the language level (Reference) as sth like "IR intrinsic name limitations"?

Yes, this should be documented in the Reference.

@rustbot labels +S-waiting-on-documentation

cc @ehuss

@rustbot rustbot added the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Target features getting erased when using #[export_name] with names of LLVM intrinsics
10 participants