Skip to content
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

[do not merge] Proof of concept implementation of forward compatible v0 symbols (MCP 737) #125487

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ dependencies = [
"libc",
"miniz_oxide",
"object 0.32.2",
"rustc-demangle",
"rustc-demangle 0.1.24",
]

[[package]]
Expand Down Expand Up @@ -840,7 +840,7 @@ dependencies = [
"md-5",
"miniz_oxide",
"regex",
"rustc-demangle",
"rustc-demangle 0.1.25",
]

[[package]]
Expand Down Expand Up @@ -3246,7 +3246,7 @@ name = "rust-demangler"
version = "0.0.1"
dependencies = [
"regex",
"rustc-demangle",
"rustc-demangle 0.1.24",
]

[[package]]
Expand Down Expand Up @@ -3280,6 +3280,11 @@ dependencies = [
"rustc-std-workspace-core",
]

[[package]]
name = "rustc-demangle"
version = "0.1.25"
source = "git+https://github.com/michaelwoerister/rustc-demangle.git?branch=skip_unknown#7e674e483a7c61803edc8d59f2641a4ad52a1a30"

[[package]]
name = "rustc-hash"
version = "1.1.0"
Expand Down Expand Up @@ -3551,7 +3556,7 @@ dependencies = [
"libc",
"measureme",
"object 0.32.2",
"rustc-demangle",
"rustc-demangle 0.1.25",
"rustc_ast",
"rustc_attr",
"rustc_codegen_ssa",
Expand Down Expand Up @@ -4553,7 +4558,7 @@ name = "rustc_symbol_mangling"
version = "0.0.0"
dependencies = [
"punycode",
"rustc-demangle",
"rustc-demangle 0.1.25",
"rustc_data_structures",
"rustc_errors",
"rustc_hir",
Expand Down Expand Up @@ -5147,7 +5152,7 @@ dependencies = [
"r-efi-alloc",
"rand",
"rand_xorshift",
"rustc-demangle",
"rustc-demangle 0.1.24",
"std_detect",
"unwind",
"wasi",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ itertools = "0.12"
libc = "0.2"
measureme = "11"
object = { version = "0.32.0", default-features = false, features = ["std", "read"] }
rustc-demangle = "0.1.21"
rustc-demangle = { git = "https://github.com/michaelwoerister/rustc-demangle.git", branch = "skip_unknown" }
rustc_ast = { path = "../rustc_ast" }
rustc_attr = { path = "../rustc_attr" }
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ impl Options {
}

pub fn get_symbol_mangling_version(&self) -> SymbolManglingVersion {
self.cg.symbol_mangling_version.unwrap_or(SymbolManglingVersion::Legacy)
self.cg.symbol_mangling_version.unwrap_or(SymbolManglingVersion::V0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
punycode = "0.4.0"
rustc-demangle = "0.1.21"
rustc-demangle = { git = "https://github.com/michaelwoerister/rustc-demangle.git", branch = "skip_unknown" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_hir = { path = "../rustc_hir" }
Expand Down
91 changes: 89 additions & 2 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(super) fn mangle<'tcx>(
consts: FxHashMap::default(),
binders: vec![],
out: String::from(prefix),
wrapper_level: GrammarFeatureLevel::BASE_LINE,
};

// Append `::{shim:...#0}` to shims that can coexist with a non-shim instance.
Expand Down Expand Up @@ -79,11 +80,13 @@ pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
consts: FxHashMap::default(),
binders: vec![],
out: String::new(),
wrapper_level: GrammarFeatureLevel::BASE_LINE,
};
cx.print_def_path(trait_ref.def_id(), &[]).unwrap();
std::mem::take(&mut cx.out)
}

#[derive(Clone)]
struct BinderLevel {
/// The range of distances from the root of what's
/// being printed, to the lifetimes in a binder.
Expand All @@ -98,6 +101,15 @@ struct BinderLevel {
lifetime_depths: Range<u32>,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct GrammarFeatureLevel(usize);

impl GrammarFeatureLevel {
const BASE_LINE: GrammarFeatureLevel = GrammarFeatureLevel(0);
const COMPLEX_CONST_GENERICS: GrammarFeatureLevel = GrammarFeatureLevel(1);
}

#[derive(Clone)]
struct SymbolMangler<'tcx> {
tcx: TyCtxt<'tcx>,
binders: Vec<BinderLevel>,
Expand All @@ -109,6 +121,8 @@ struct SymbolMangler<'tcx> {
paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
types: FxHashMap<Ty<'tcx>, usize>,
consts: FxHashMap<ty::Const<'tcx>, usize>,

wrapper_level: GrammarFeatureLevel,
}

impl<'tcx> SymbolMangler<'tcx> {
Expand Down Expand Up @@ -192,6 +206,75 @@ impl<'tcx> SymbolMangler<'tcx> {

Ok(())
}

fn required_feature_level_for_const(&self, ct: ty::Const<'tcx>) -> GrammarFeatureLevel {
let ct = ct.normalize(self.tcx, ty::ParamEnv::reveal_all());
match ct.kind() {
ty::ConstKind::Value(_) => {}

ty::ConstKind::Unevaluated(_)
| ty::ConstKind::Expr(_)
| ty::ConstKind::Param(_)
| ty::ConstKind::Infer(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Error(_) => {
return GrammarFeatureLevel::BASE_LINE;
}
}

let ty = ct.ty();

match ty.kind() {
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => GrammarFeatureLevel::BASE_LINE,
_ => GrammarFeatureLevel::COMPLEX_CONST_GENERICS,
}
}

fn wrapped(
&mut self,
level: GrammarFeatureLevel,
f: &mut dyn FnMut(&mut Self) -> Result<(), PrintError>,
) -> Result<(), PrintError> {
if self.wrapper_level >= level {
return f(self);
}

self.out.push('C');
let backup = self.clone();

let mut digit_count = 2;

for _iteration in 0..10 {
self.wrapper_level = level;
let digit_range = self.out.len()..self.out.len() + digit_count;
for _ in 0..digit_count {
self.out.push('#');
}

self.out.push('_');

let start = self.out.len();

f(self)?;

let end = self.out.len();
let fragment_length = format!("{}", end - start);

// FIXME: Add check if any back references to interior were made. If
// not, we can just shift everything without remangling.
if fragment_length.len() == digit_count {
self.out.replace_range(digit_range, &fragment_length);
self.wrapper_level = backup.wrapper_level;
return Ok(());
}

*self = backup.clone();
digit_count = fragment_length.len();
}

Err(PrintError::default())
}
}

impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
Expand Down Expand Up @@ -813,8 +896,12 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
ty.print(self)?;
}
GenericArgKind::Const(c) => {
self.push("K");
c.print(self)?;
let required_feature_level = self.required_feature_level_for_const(c);

self.wrapped(required_feature_level, &mut |this| {
this.push("K");
c.print(this)
})?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/coverage-dump/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ leb128 = "0.2.5"
md5 = { package = "md-5" , version = "0.10.5" }
miniz_oxide = "0.7.1"
regex = "1.8.4"
rustc-demangle = "0.1.23"
rustc-demangle = { git = "https://github.com/michaelwoerister/rustc-demangle.git", branch = "skip_unknown" }
2 changes: 1 addition & 1 deletion tests/assembly/closure-inherit-target-feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//@ ignore-sgx Tests incompatible with LVI mitigations
//@ assembly-output: emit-asm
// make sure the feature is not enabled at compile-time
//@ compile-flags: -C opt-level=3 -C target-feature=-sse4.1 -C llvm-args=-x86-asm-syntax=intel
//@ compile-flags: -C opt-level=3 -C target-feature=-sse4.1 -C llvm-args=-x86-asm-syntax=intel -Csymbol-mangling-version=legacy -Zunstable-options

#![feature(target_feature_11)]
#![crate_type = "rlib"]
Expand Down
4 changes: 2 additions & 2 deletions tests/codegen/precondition-checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

use std::ptr::NonNull;

// CHECK-LABEL: ; core::ptr::non_null::NonNull<T>::new_unchecked
// CHECK-LABEL: ; <core::ptr::non_null::NonNull<u8>>::new_unchecked
// CHECK-NOT: call
// CHECK: }

// CHECK-LABEL: @nonnull_new
#[no_mangle]
pub unsafe fn nonnull_new(ptr: *mut u8) -> NonNull<u8> {
// CHECK: ; call core::ptr::non_null::NonNull<T>::new_unchecked
// CHECK: ; call <core::ptr::non_null::NonNull<u8>>::new_unchecked
unsafe {
NonNull::new_unchecked(ptr)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Verifies that type metadata identifiers for drop functions are emitted correctly.
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static -Csymbol-mangling-version=legacy -Zunstable-options

#![crate_type="lib"]

Expand Down
3 changes: 2 additions & 1 deletion tests/crashes/118603.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ known-bug: #118603
//@ compile-flags: -Copt-level=0
//@ compile-flags: -Copt-level=0 -Csymbol-mangling-version=legacy
// ignore-tidy-linelength
//@ ignore-test

#![feature(generic_const_exprs)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
thread 'main' panicked at $DIR/short-ice-remove-middle-frames-2.rs:56:5:
debug!!!
stack backtrace:
0: std::panicking::begin_panic
0: std::panicking::begin_panic::<&str>
1: short_ice_remove_middle_frames_2::eight
2: short_ice_remove_middle_frames_2::seven::{{closure}}
2: short_ice_remove_middle_frames_2::seven::{closure#0}
[... omitted 3 frames ...]
3: short_ice_remove_middle_frames_2::fifth
4: short_ice_remove_middle_frames_2::fourth::{{closure}}
4: short_ice_remove_middle_frames_2::fourth::{closure#0}
[... omitted 4 frames ...]
5: short_ice_remove_middle_frames_2::first
6: short_ice_remove_middle_frames_2::main
7: core::ops::function::FnOnce::call_once
7: <fn() as core::ops::function::FnOnce<()>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
8 changes: 4 additions & 4 deletions tests/ui/panics/short-ice-remove-middle-frames.run.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
thread 'main' panicked at $DIR/short-ice-remove-middle-frames.rs:52:5:
debug!!!
stack backtrace:
0: std::panicking::begin_panic
0: std::panicking::begin_panic::<&str>
1: short_ice_remove_middle_frames::seven
2: short_ice_remove_middle_frames::sixth
3: short_ice_remove_middle_frames::fifth::{{closure}}
3: short_ice_remove_middle_frames::fifth::{closure#0}
[... omitted 4 frames ...]
4: short_ice_remove_middle_frames::second
5: short_ice_remove_middle_frames::first::{{closure}}
5: short_ice_remove_middle_frames::first::{closure#0}
6: short_ice_remove_middle_frames::first
7: short_ice_remove_middle_frames::main
8: core::ops::function::FnOnce::call_once
8: <fn() as core::ops::function::FnOnce<()>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
12 changes: 6 additions & 6 deletions tests/ui/symbol-names/const-generics-str-demangling.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_3StrKRe616263_E)
error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_3StrC10_KRe616263_E)
--> $DIR/const-generics-str-demangling.rs:9:1
|
LL | #[rustc_symbol_name]
Expand All @@ -16,7 +16,7 @@ error: demangling-alt(<c::Str<"abc">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_3StrKRe27_E)
error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_3StrC6_KRe27_E)
--> $DIR/const-generics-str-demangling.rs:15:1
|
LL | #[rustc_symbol_name]
Expand All @@ -34,7 +34,7 @@ error: demangling-alt(<c::Str<"'">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_3StrKRe090a_E)
error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_3StrC8_KRe090a_E)
--> $DIR/const-generics-str-demangling.rs:21:1
|
LL | #[rustc_symbol_name]
Expand All @@ -52,7 +52,7 @@ error: demangling-alt(<c::Str<"\t\n">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_3StrKRee28882c3bc_E)
error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_3StrC14_KRee28882c3bc_E)
--> $DIR/const-generics-str-demangling.rs:27:1
|
LL | #[rustc_symbol_name]
Expand All @@ -70,7 +70,7 @@ error: demangling-alt(<c::Str<"∂ü">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_3StrKRee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839be183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_E)
error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_3StrC140_KRee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839be183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_E)
--> $DIR/const-generics-str-demangling.rs:33:1
|
LL | #[rustc_symbol_name]
Expand All @@ -88,7 +88,7 @@ error: demangling-alt(<c::Str<"საჭმელად_გემრიელი
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_3StrKRef09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e29895f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_E)
error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_3StrC122_KRef09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e29895f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_E)
--> $DIR/const-generics-str-demangling.rs:39:1
|
LL | #[rustc_symbol_name]
Expand Down
Loading
Loading