Skip to content

Commit adc0c91

Browse files
authored
Rollup merge of #115151 - rcvalle:rust-cfi-fix-115150, r=compiler-errors
Fix CFI: f32 and f64 are encoded incorrectly for cross-language CFI Fix #115150 by encoding f32 and f64 correctly for cross-language CFI. I missed changing the encoding for f32 and f64 when I introduced the integer normalization option in #105452 as integer normalization does not include floating point. `f32` and `f64` should be always encoded as `f` and `d` since they are both FFI safe when their representation are the same (i.e., IEEE 754) for both the Rust compiler and Clang.
2 parents 49cdf06 + 5d6e2d7 commit adc0c91

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Diff for: compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ fn encode_ty<'tcx>(
447447
typeid.push('b');
448448
}
449449

450-
ty::Int(..) | ty::Uint(..) | ty::Float(..) => {
450+
ty::Int(..) | ty::Uint(..) => {
451451
// u<length><type-name> as vendor extended type
452452
let mut s = String::from(match ty.kind() {
453453
ty::Int(IntTy::I8) => "u2i8",
@@ -462,14 +462,23 @@ fn encode_ty<'tcx>(
462462
ty::Uint(UintTy::U64) => "u3u64",
463463
ty::Uint(UintTy::U128) => "u4u128",
464464
ty::Uint(UintTy::Usize) => "u5usize",
465-
ty::Float(FloatTy::F32) => "u3f32",
466-
ty::Float(FloatTy::F64) => "u3f64",
467-
_ => "",
465+
_ => bug!("encode_ty: unexpected `{:?}`", ty.kind()),
468466
});
469467
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
470468
typeid.push_str(&s);
471469
}
472470

471+
// Rust's f32 and f64 single (32-bit) and double (64-bit) precision floating-point types
472+
// have IEEE-754 binary32 and binary64 floating-point layouts, respectively.
473+
//
474+
// (See https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#fixed-width-floating-point-types.)
475+
ty::Float(float_ty) => {
476+
typeid.push(match float_ty {
477+
FloatTy::F32 => 'f',
478+
FloatTy::F64 => 'd',
479+
});
480+
}
481+
473482
ty::Char => {
474483
// u4char as vendor extended type
475484
let mut s = String::from("u4char");

Diff for: tests/codegen/sanitizer/cfi-emit-type-metadata-id-itanium-cxx-abi.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,12 @@ pub fn foo149(_: Type14<Bar>, _: Type14<Bar>, _: Type14<Bar>) { }
500500
// CHECK: ![[TYPE45]] = !{i64 0, !"_ZTSFvu5usizeE"}
501501
// CHECK: ![[TYPE46]] = !{i64 0, !"_ZTSFvu5usizeS_E"}
502502
// CHECK: ![[TYPE47]] = !{i64 0, !"_ZTSFvu5usizeS_S_E"}
503-
// CHECK: ![[TYPE48]] = !{i64 0, !"_ZTSFvu3f32E"}
504-
// CHECK: ![[TYPE49]] = !{i64 0, !"_ZTSFvu3f32S_E"}
505-
// CHECK: ![[TYPE50]] = !{i64 0, !"_ZTSFvu3f32S_S_E"}
506-
// CHECK: ![[TYPE51]] = !{i64 0, !"_ZTSFvu3f64E"}
507-
// CHECK: ![[TYPE52]] = !{i64 0, !"_ZTSFvu3f64S_E"}
508-
// CHECK: ![[TYPE53]] = !{i64 0, !"_ZTSFvu3f64S_S_E"}
503+
// CHECK: ![[TYPE48]] = !{i64 0, !"_ZTSFvfE"}
504+
// CHECK: ![[TYPE49]] = !{i64 0, !"_ZTSFvffE"}
505+
// CHECK: ![[TYPE50]] = !{i64 0, !"_ZTSFvfffE"}
506+
// CHECK: ![[TYPE51]] = !{i64 0, !"_ZTSFvdE"}
507+
// CHECK: ![[TYPE52]] = !{i64 0, !"_ZTSFvddE"}
508+
// CHECK: ![[TYPE53]] = !{i64 0, !"_ZTSFvdddE"}
509509
// CHECK: ![[TYPE54]] = !{i64 0, !"_ZTSFvu4charE"}
510510
// CHECK: ![[TYPE55]] = !{i64 0, !"_ZTSFvu4charS_E"}
511511
// CHECK: ![[TYPE56]] = !{i64 0, !"_ZTSFvu4charS_S_E"}

0 commit comments

Comments
 (0)