Skip to content

Commit 453fcbc

Browse files
committed
disallow export_name starting with "llvm."
1 parent 16db0e2 commit 453fcbc

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ codegen_ssa_expected_one_argument = expected one argument
5252
5353
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
5454
55+
codegen_ssa_export_name_llvm_intrinsic = exported symbol name must not start with "llvm."
56+
.note = symbols starting with "llvm." are reserved for LLVM intrinsics and cannot be defined in user code
57+
5558
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
5659
5760
codegen_ssa_extract_bundled_libs_archive_member = failed to get data from archive member '{$rlib}': {$error}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+9
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
258258
// so it may not contain any null characters.
259259
tcx.dcx().emit_err(errors::NullOnExport { span: attr.span() });
260260
}
261+
if s.as_str().starts_with("llvm.") {
262+
// Symbols starting with "llvm." are reserved by LLVM
263+
// trying to define those would produce invalid IR.
264+
// LLVM complain about those *if* we enable LLVM verification checks
265+
// but we often don't enable them by default due to perf reasons.
266+
tcx.dcx().emit_err(errors::ExportNameLLVMIntrinsic {
267+
span: attr.value_span().expect("attibute has value but not a span"),
268+
});
269+
}
261270
codegen_fn_attrs.export_name = Some(s);
262271
mixed_export_name_no_mangle_lint_state.track_export_name(attr.span());
263272
}

compiler/rustc_codegen_ssa/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ pub(crate) struct NullOnExport {
147147
pub span: Span,
148148
}
149149

150+
#[derive(Diagnostic)]
151+
#[diag(codegen_ssa_export_name_llvm_intrinsic)]
152+
#[note]
153+
pub(crate) struct ExportNameLLVMIntrinsic {
154+
#[primary_span]
155+
pub span: Span,
156+
}
157+
150158
#[derive(Diagnostic)]
151159
#[diag(codegen_ssa_unsupported_instruction_set, code = E0779)]
152160
pub(crate) struct UnsuportedInstructionSet {
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_type = "lib"]
2+
3+
// rdtsc is an existing LLVM intrinsic
4+
#[export_name = "llvm.x86.rdtsc"]
5+
//~^ ERROR: exported symbol name must not start with "llvm."
6+
pub unsafe fn foo(a: u8) -> u8 {
7+
2 * a
8+
}
9+
10+
// qwerty is not a real llvm intrinsic
11+
#[export_name = "llvm.x86.qwerty"]
12+
//~^ ERROR: exported symbol name must not start with "llvm."
13+
pub unsafe fn bar(a: u8) -> u8 {
14+
2 * a
15+
}
16+
17+
#[export_name="ab\0cd"] //~ ERROR `export_name` may not contain null characters
18+
pub fn qux() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: exported symbol name must not start with "llvm."
2+
--> $DIR/export_name-checks.rs:4:17
3+
|
4+
LL | #[export_name = "llvm.x86.rdtsc"]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: symbols starting with "llvm." are reserved for LLVM intrinsics and cannot be defined in user code
8+
9+
error: exported symbol name must not start with "llvm."
10+
--> $DIR/export_name-checks.rs:11:17
11+
|
12+
LL | #[export_name = "llvm.x86.qwerty"]
13+
| ^^^^^^^^^^^^^^^^^
14+
|
15+
= note: symbols starting with "llvm." are reserved for LLVM intrinsics and cannot be defined in user code
16+
17+
error[E0648]: `export_name` may not contain null characters
18+
--> $DIR/export_name-checks.rs:17:1
19+
|
20+
LL | #[export_name="ab\0cd"]
21+
| ^^^^^^^^^^^^^^^^^^^^^^^
22+
23+
error: aborting due to 3 previous errors
24+
25+
For more information about this error, try `rustc --explain E0648`.

0 commit comments

Comments
 (0)