mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
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.
This commit is contained in:
@@ -310,7 +310,10 @@ fn data_id_for_static(
|
||||
// `extern_with_linkage_foo` will instead be initialized to
|
||||
// zero.
|
||||
|
||||
let ref_name = format!("_rust_extern_with_linkage_{}", symbol_name);
|
||||
let ref_name = format!(
|
||||
"_rust_extern_with_linkage_{:016x}_{symbol_name}",
|
||||
tcx.stable_crate_id(LOCAL_CRATE)
|
||||
);
|
||||
let ref_data_id = module.declare_data(&ref_name, Linkage::Local, false, false).unwrap();
|
||||
let mut data = DataDescription::new();
|
||||
data.set_align(align);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
|
||||
};
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::LOCAL_CRATE;
|
||||
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
|
||||
use rustc_middle::mir::interpret::{
|
||||
self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint,
|
||||
@@ -384,8 +385,8 @@ fn check_and_apply_linkage<'gcc, 'tcx>(
|
||||
// linkage and there are no definitions), then
|
||||
// `extern_with_linkage_foo` will instead be initialized to
|
||||
// zero.
|
||||
let mut real_name = "_rust_extern_with_linkage_".to_string();
|
||||
real_name.push_str(sym);
|
||||
let real_name =
|
||||
format!("_rust_extern_with_linkage_{:016x}_{sym}", cx.tcx.stable_crate_id(LOCAL_CRATE));
|
||||
let global2 = cx.define_global(&real_name, gcc_type, is_tls, attrs.link_section);
|
||||
// TODO(antoyo): set linkage.
|
||||
let value = cx.const_ptrcast(global1.get_address(None), gcc_type);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
use rustc_codegen_ssa::traits::*;
|
||||
use rustc_hir::LangItem;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
||||
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
|
||||
use rustc_middle::mir::interpret::{
|
||||
Allocation, ConstAllocation, ErrorHandled, InitChunk, Pointer, Scalar as InterpScalar,
|
||||
@@ -191,8 +191,8 @@ fn check_and_apply_linkage<'ll, 'tcx>(
|
||||
// linkage and there are no definitions), then
|
||||
// `extern_with_linkage_foo` will instead be initialized to
|
||||
// zero.
|
||||
let mut real_name = "_rust_extern_with_linkage_".to_string();
|
||||
real_name.push_str(sym);
|
||||
let real_name =
|
||||
format!("_rust_extern_with_linkage_{:016x}_{sym}", cx.tcx.stable_crate_id(LOCAL_CRATE));
|
||||
let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
|
||||
cx.sess().dcx().emit_fatal(SymbolAlreadyDefined {
|
||||
span: cx.tcx.def_span(def_id),
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#[linkage = "extern_weak"]
|
||||
static FOO: Option<unsafe extern "C" fn(f64) -> ()>;
|
||||
}
|
||||
// CHECK: @_rust_extern_with_linkage_FOO = internal global ptr @FOO
|
||||
// CHECK: @_rust_extern_with_linkage_{{.*}}_FOO = internal global ptr @FOO
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
// rust-lang/rust#61232: We used to ICE when trying to detect a
|
||||
// collision on the symbol generated for the external linkage item in
|
||||
// an extern crate.
|
||||
|
||||
//@ build-fail
|
||||
//@ aux-build:def_colliding_external.rs
|
||||
// FIXME(#83838) codegen-units=1 triggers llvm asserts
|
||||
//@ compile-flags: -Ccodegen-units=16
|
||||
|
||||
extern crate def_colliding_external as dep1;
|
||||
|
||||
#[no_mangle]
|
||||
pub static _rust_extern_with_linkage_collision: i32 = 0;
|
||||
|
||||
mod dep2 {
|
||||
#[no_mangle]
|
||||
pub static collision: usize = 0;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
println!("{:p}", &dep1::collision);
|
||||
}
|
||||
}
|
||||
|
||||
//~? ERROR symbol `collision` is already defined
|
||||
@@ -1,8 +0,0 @@
|
||||
error: symbol `collision` is already defined
|
||||
--> $DIR/auxiliary/def_colliding_external.rs:6:5
|
||||
|
|
||||
LL | pub static collision: *const i32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
//@ build-fail
|
||||
// FIXME(#83838) codegen-units=1 triggers llvm asserts
|
||||
//@ compile-flags: -Ccodegen-units=16
|
||||
#![feature(linkage)]
|
||||
|
||||
mod dep1 {
|
||||
extern "C" {
|
||||
#[linkage = "external"]
|
||||
#[no_mangle]
|
||||
pub static collision: *const i32; //~ ERROR symbol `collision` is already defined
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub static _rust_extern_with_linkage_collision: i32 = 0;
|
||||
|
||||
mod dep2 {
|
||||
#[no_mangle]
|
||||
pub static collision: usize = 0;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
println!("{:p}", &dep1::collision);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
error: symbol `collision` is already defined
|
||||
--> $DIR/linkage-detect-local-generated-name-collision.rs:10:9
|
||||
|
|
||||
LL | pub static collision: *const i32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
Reference in New Issue
Block a user