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

Fix CFI: f32 and f64 are encoded incorrectly for cross-language CFI #115151

Merged
merged 1 commit into from
Aug 25, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn encode_ty<'tcx>(
typeid.push('b');
}

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

// Rust's f32 and f64 single (32-bit) and double (64-bit) precision floating-point types
// have IEEE-754 binary32 and binary64 floating-point layouts, respectively.
//
// (See https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#fixed-width-floating-point-types.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to link to something describing the f and d prefixes that the encoding expects for IEEE-754 floats?

Copy link
Member Author

@rcvalle rcvalle Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Humm... those are just the Itanium C++ encoding for these builtin types (see https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-builtin). Instead of referencing it, I think it makes more sense to explain why they can be directly used here (similarly to how it's explained in line 446 for ty::Bool). What do you think?

ty::Float(float_ty) => {
typeid.push(match float_ty {
FloatTy::F32 => 'f',
FloatTy::F64 => 'd',
});
}

ty::Char => {
// u4char as vendor extended type
let mut s = String::from("u4char");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ pub fn foo149(_: Type14<Bar>, _: Type14<Bar>, _: Type14<Bar>) { }
// CHECK: ![[TYPE45]] = !{i64 0, !"_ZTSFvu5usizeE"}
// CHECK: ![[TYPE46]] = !{i64 0, !"_ZTSFvu5usizeS_E"}
// CHECK: ![[TYPE47]] = !{i64 0, !"_ZTSFvu5usizeS_S_E"}
// CHECK: ![[TYPE48]] = !{i64 0, !"_ZTSFvu3f32E"}
// CHECK: ![[TYPE49]] = !{i64 0, !"_ZTSFvu3f32S_E"}
// CHECK: ![[TYPE50]] = !{i64 0, !"_ZTSFvu3f32S_S_E"}
// CHECK: ![[TYPE51]] = !{i64 0, !"_ZTSFvu3f64E"}
// CHECK: ![[TYPE52]] = !{i64 0, !"_ZTSFvu3f64S_E"}
// CHECK: ![[TYPE53]] = !{i64 0, !"_ZTSFvu3f64S_S_E"}
// CHECK: ![[TYPE48]] = !{i64 0, !"_ZTSFvfE"}
// CHECK: ![[TYPE49]] = !{i64 0, !"_ZTSFvffE"}
// CHECK: ![[TYPE50]] = !{i64 0, !"_ZTSFvfffE"}
// CHECK: ![[TYPE51]] = !{i64 0, !"_ZTSFvdE"}
// CHECK: ![[TYPE52]] = !{i64 0, !"_ZTSFvddE"}
// CHECK: ![[TYPE53]] = !{i64 0, !"_ZTSFvdddE"}
// CHECK: ![[TYPE54]] = !{i64 0, !"_ZTSFvu4charE"}
// CHECK: ![[TYPE55]] = !{i64 0, !"_ZTSFvu4charS_E"}
// CHECK: ![[TYPE56]] = !{i64 0, !"_ZTSFvu4charS_S_E"}
Expand Down