Skip to content

Commit c88e208

Browse files
committed
Prevent name collisions with internal implementation details
The implementation of the linkage attribute inside extern blocks defines symbols starting with _rust_extern_with_linkage_. If someone tries to also define this symbol you will get a symbol conflict or even an ICE. By adding an unpredictable component to the symbol name, this becomes less of an issue.
1 parent 9b1a30e commit c88e208

File tree

8 files changed

+15
-75
lines changed

8 files changed

+15
-75
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ fn data_id_for_static(
310310
// `extern_with_linkage_foo` will instead be initialized to
311311
// zero.
312312

313-
let ref_name = format!("_rust_extern_with_linkage_{}", symbol_name);
313+
let ref_name = format!(
314+
"_rust_extern_with_linkage_{:x}_{symbol_name}",
315+
tcx.stable_crate_id(LOCAL_CRATE).as_u64()
316+
);
314317
let ref_data_id = module.declare_data(&ref_name, Linkage::Local, false, false).unwrap();
315318
let mut data = DataDescription::new();
316319
data.set_align(align);

compiler/rustc_codegen_gcc/src/consts.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_codegen_ssa::traits::{
66
BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
77
};
88
use rustc_hir::def::DefKind;
9+
use rustc_hir::def_id::LOCAL_CRATE;
910
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1011
use rustc_middle::mir::interpret::{
1112
self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint,
@@ -384,8 +385,10 @@ fn check_and_apply_linkage<'gcc, 'tcx>(
384385
// linkage and there are no definitions), then
385386
// `extern_with_linkage_foo` will instead be initialized to
386387
// zero.
387-
let mut real_name = "_rust_extern_with_linkage_".to_string();
388-
real_name.push_str(sym);
388+
let real_name = format!(
389+
"_rust_extern_with_linkage_{:x}_{sym}",
390+
cx.tcx.stable_crate_id(LOCAL_CRATE).as_u64()
391+
);
389392
let global2 = cx.define_global(&real_name, gcc_type, is_tls, attrs.link_section);
390393
// TODO(antoyo): set linkage.
391394
let value = cx.const_ptrcast(global1.get_address(None), gcc_type);

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_codegen_ssa::common;
55
use rustc_codegen_ssa::traits::*;
66
use rustc_hir::LangItem;
77
use rustc_hir::def::DefKind;
8-
use rustc_hir::def_id::DefId;
8+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
99
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1010
use rustc_middle::mir::interpret::{
1111
Allocation, ConstAllocation, ErrorHandled, InitChunk, Pointer, Scalar as InterpScalar,
@@ -191,8 +191,10 @@ fn check_and_apply_linkage<'ll, 'tcx>(
191191
// linkage and there are no definitions), then
192192
// `extern_with_linkage_foo` will instead be initialized to
193193
// zero.
194-
let mut real_name = "_rust_extern_with_linkage_".to_string();
195-
real_name.push_str(sym);
194+
let real_name = format!(
195+
"_rust_extern_with_linkage_{:x}_{sym}",
196+
cx.tcx.stable_crate_id(LOCAL_CRATE).as_u64()
197+
);
196198
let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
197199
cx.sess().dcx().emit_fatal(SymbolAlreadyDefined {
198200
span: cx.tcx.def_span(def_id),

tests/codegen-llvm/sanitizer/cfi/external_weak_symbols.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ unsafe extern "C" {
1010
#[linkage = "extern_weak"]
1111
static FOO: Option<unsafe extern "C" fn(f64) -> ()>;
1212
}
13-
// CHECK: @_rust_extern_with_linkage_FOO = internal global ptr @FOO
13+
// CHECK: @_rust_extern_with_linkage_{{.*}}_FOO = internal global ptr @FOO
1414

1515
fn main() {
1616
unsafe {

tests/ui/linkage-attr/linkage-detect-extern-generated-name-collision.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/ui/linkage-attr/linkage-detect-extern-generated-name-collision.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/ui/linkage-attr/linkage-detect-local-generated-name-collision.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/ui/linkage-attr/linkage-detect-local-generated-name-collision.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)