Skip to content

Commit 098d06b

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 82aac02 commit 098d06b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/consts.rs

Lines changed: 3 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,8 @@ 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 =
389+
format!("_rust_extern_with_linkage_{:016x}_{sym}", cx.tcx.stable_crate_id(LOCAL_CRATE));
389390
let global2 = cx.define_global(&real_name, gcc_type, is_tls, attrs.link_section);
390391
// TODO(antoyo): set linkage.
391392
let value = cx.const_ptrcast(global1.get_address(None), gcc_type);

0 commit comments

Comments
 (0)