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

flt2dec: properly handle uninitialized memory #76241

Merged
merged 1 commit into from
Sep 2, 2020
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
49 changes: 33 additions & 16 deletions library/core/benches/num/flt2dec/strategy/dragon.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,76 @@
use super::super::*;
use core::num::flt2dec::strategy::dragon::*;
use std::mem::MaybeUninit;
use test::Bencher;

#[bench]
fn bench_small_shortest(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; MAX_SIG_DIGITS];
b.iter(|| format_shortest(&decoded, &mut buf));
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
});
}

#[bench]
fn bench_big_shortest(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; MAX_SIG_DIGITS];
b.iter(|| format_shortest(&decoded, &mut buf));
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
});
}

#[bench]
fn bench_small_exact_3(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; 3];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_big_exact_3(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; 3];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_small_exact_12(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; 12];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_big_exact_12(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; 12];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_small_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; 1024];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_big_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; 1024];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}
49 changes: 33 additions & 16 deletions library/core/benches/num/flt2dec/strategy/grisu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::super::*;
use core::num::flt2dec::strategy::grisu::*;
use std::mem::MaybeUninit;
use test::Bencher;

pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
Expand All @@ -12,55 +13,71 @@ pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
#[bench]
fn bench_small_shortest(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; MAX_SIG_DIGITS];
b.iter(|| format_shortest(&decoded, &mut buf));
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
});
}

#[bench]
fn bench_big_shortest(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; MAX_SIG_DIGITS];
b.iter(|| format_shortest(&decoded, &mut buf));
let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
b.iter(|| {
format_shortest(&decoded, &mut buf);
});
}

#[bench]
fn bench_small_exact_3(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; 3];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_big_exact_3(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; 3];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 3];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_small_exact_12(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; 12];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_big_exact_12(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; 12];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 12];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_small_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(3.141592f64);
let mut buf = [0; 1024];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}

#[bench]
fn bench_big_exact_inf(b: &mut Bencher) {
let decoded = decode_finite(f64::MAX);
let mut buf = [0; 1024];
b.iter(|| format_exact(&decoded, &mut buf, i16::MIN));
let mut buf = [MaybeUninit::new(0); 1024];
b.iter(|| {
format_exact(&decoded, &mut buf, i16::MIN);
});
}
116 changes: 48 additions & 68 deletions library/core/src/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,17 @@ fn float_to_decimal_common_exact<T>(
where
T: flt2dec::DecodableFloat,
{
// SAFETY: Possible undefined behavior, see FIXME(#76092)
unsafe {
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
// FIXME(#76092): This is calling `assume_init_mut` on an uninitialized
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
// we decided whether that is valid or not.
// We can do this only because we are libstd and coupled to the compiler.
// (FWIW, using `freeze` would not be enough; `flt2dec::Part` is an enum!)
let formatted = flt2dec::to_exact_fixed_str(
flt2dec::strategy::grisu::format_exact,
*num,
sign,
precision,
buf.assume_init_mut(),
parts.assume_init_mut(),
);
fmt.pad_formatted_parts(&formatted)
}
let mut buf: [MaybeUninit<u8>; 1024] = MaybeUninit::uninit_array(); // enough for f32 and f64
let mut parts: [MaybeUninit<flt2dec::Part<'_>>; 4] = MaybeUninit::uninit_array();
let formatted = flt2dec::to_exact_fixed_str(
flt2dec::strategy::grisu::format_exact,
*num,
sign,
precision,
&mut buf,
&mut parts,
);
fmt.pad_formatted_parts(&formatted)
}

// Don't inline this so callers that call both this and the above won't wind
Expand All @@ -47,22 +39,18 @@ fn float_to_decimal_common_shortest<T>(
where
T: flt2dec::DecodableFloat,
{
// SAFETY: Possible undefined behavior, see FIXME(#76092)
unsafe {
// enough for f32 and f64
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninit();
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
// FIXME(#76092)
let formatted = flt2dec::to_shortest_str(
flt2dec::strategy::grisu::format_shortest,
*num,
sign,
precision,
buf.assume_init_mut(),
parts.assume_init_mut(),
);
fmt.pad_formatted_parts(&formatted)
}
// enough for f32 and f64
let mut buf: [MaybeUninit<u8>; flt2dec::MAX_SIG_DIGITS] = MaybeUninit::uninit_array();
let mut parts: [MaybeUninit<flt2dec::Part<'_>>; 4] = MaybeUninit::uninit_array();
let formatted = flt2dec::to_shortest_str(
flt2dec::strategy::grisu::format_shortest,
*num,
sign,
precision,
&mut buf,
&mut parts,
);
fmt.pad_formatted_parts(&formatted)
}

// Common code of floating point Debug and Display.
Expand Down Expand Up @@ -103,22 +91,18 @@ fn float_to_exponential_common_exact<T>(
where
T: flt2dec::DecodableFloat,
{
// SAFETY: Possible undefined behavior, see FIXME(#76092)
unsafe {
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 6]>::uninit();
// FIXME(#76092)
let formatted = flt2dec::to_exact_exp_str(
flt2dec::strategy::grisu::format_exact,
*num,
sign,
precision,
upper,
buf.assume_init_mut(),
parts.assume_init_mut(),
);
fmt.pad_formatted_parts(&formatted)
}
let mut buf: [MaybeUninit<u8>; 1024] = MaybeUninit::uninit_array(); // enough for f32 and f64
let mut parts: [MaybeUninit<flt2dec::Part<'_>>; 6] = MaybeUninit::uninit_array();
let formatted = flt2dec::to_exact_exp_str(
flt2dec::strategy::grisu::format_exact,
*num,
sign,
precision,
upper,
&mut buf,
&mut parts,
);
fmt.pad_formatted_parts(&formatted)
}

// Don't inline this so callers that call both this and the above won't wind
Expand All @@ -133,23 +117,19 @@ fn float_to_exponential_common_shortest<T>(
where
T: flt2dec::DecodableFloat,
{
// SAFETY: Possible undefined behavior, see FIXME(#76092)
unsafe {
// enough for f32 and f64
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninit();
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 6]>::uninit();
// FIXME(#76092)
let formatted = flt2dec::to_shortest_exp_str(
flt2dec::strategy::grisu::format_shortest,
*num,
sign,
(0, 0),
upper,
buf.assume_init_mut(),
parts.assume_init_mut(),
);
fmt.pad_formatted_parts(&formatted)
}
// enough for f32 and f64
let mut buf: [MaybeUninit<u8>; flt2dec::MAX_SIG_DIGITS] = MaybeUninit::uninit_array();
let mut parts: [MaybeUninit<flt2dec::Part<'_>>; 6] = MaybeUninit::uninit_array();
let formatted = flt2dec::to_shortest_exp_str(
flt2dec::strategy::grisu::format_shortest,
*num,
sign,
(0, 0),
upper,
&mut buf,
&mut parts,
);
fmt.pad_formatted_parts(&formatted)
}

// Common code of floating point LowerExp and UpperExp.
Expand Down
Loading