Skip to content

Commit 5d07254

Browse files
authored
Rollup merge of rust-lang#78122 - fusion-engineering-forks:fmt-write-bounds-check, r=dtolnay
Avoid panic_bounds_check in fmt::write. Writing any fmt::Arguments would trigger the inclusion of usize formatting and padding code in the resulting binary, because indexing used in fmt::write would generate code using panic_bounds_check, which prints the index and length. These bounds checks are not necessary, as fmt::Arguments never contains any out-of-bounds indexes. This change replaces them with unsafe get_unchecked, to reduce the amount of generated code, which is especially important for embedded targets. --- Demonstration of the size of and the symbols in a 'hello world' no_std binary: <details> <summary>Source code</summary> ```rust #![feature(lang_items)] #![feature(start)] #![no_std] use core::fmt; use core::fmt::Write; #[link(name = "c")] extern "C" { #[allow(improper_ctypes)] fn write(fd: i32, s: &str) -> isize; fn exit(code: i32) -> !; } struct Stdout; impl fmt::Write for Stdout { fn write_str(&mut self, s: &str) -> fmt::Result { unsafe { write(1, s) }; Ok(()) } } #[start] fn main(_argc: isize, _argv: *const *const u8) -> isize { let _ = writeln!(Stdout, "Hello World"); 0 } #[lang = "eh_personality"] fn eh_personality() {} #[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { unsafe { exit(1) }; } ``` </details> Before: ``` text data bss dec hex filename 6059 736 8 6803 1a93 before ``` ``` 0000000000001e00 T <T as core::any::Any>::type_id 0000000000003dd0 D core::fmt::num::DEC_DIGITS_LUT 0000000000001ce0 T core::fmt::num::imp::<impl core::fmt::Display for u64>::fmt 0000000000001ce0 T core::fmt::num::imp::<impl core::fmt::Display for usize>::fmt 0000000000001370 T core::fmt::write 0000000000001b30 t core::fmt::Formatter::pad_integral::write_prefix 0000000000001660 T core::fmt::Formatter::pad_integral 0000000000001350 T core::ops::function::FnOnce::call_once 0000000000001b80 t core::ptr::drop_in_place 0000000000001120 t core::ptr::drop_in_place 0000000000001c50 t core::iter::adapters::zip::Zip<A,B>::new 0000000000001c90 t core::iter::adapters::zip::Zip<A,B>::new 0000000000001b90 T core::panicking::panic_bounds_check 0000000000001c10 T core::panicking::panic_fmt 0000000000001130 t <&mut W as core::fmt::Write>::write_char 0000000000001200 t <&mut W as core::fmt::Write>::write_fmt 0000000000001250 t <&mut W as core::fmt::Write>::write_str ``` After: ``` text data bss dec hex filename 3068 600 8 3676 e5c after ``` ``` 0000000000001360 T core::fmt::write 0000000000001340 T core::ops::function::FnOnce::call_once 0000000000001120 t core::ptr::drop_in_place 0000000000001620 t core::iter::adapters::zip::Zip<A,B>::new 0000000000001660 t core::iter::adapters::zip::Zip<A,B>::new 0000000000001130 t <&mut W as core::fmt::Write>::write_char 0000000000001200 t <&mut W as core::fmt::Write>::write_fmt 0000000000001250 t <&mut W as core::fmt::Write>::write_str ```
2 parents 66fed47 + 484d9eb commit 5d07254

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

library/core/src/fmt/mod.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,9 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10821082
// a string piece.
10831083
for (arg, piece) in fmt.iter().zip(args.pieces.iter()) {
10841084
formatter.buf.write_str(*piece)?;
1085-
run(&mut formatter, arg, &args.args)?;
1085+
// SAFETY: arg and args.args come from the same Arguments,
1086+
// which guarantees the indexes are always within bounds.
1087+
unsafe { run(&mut formatter, arg, &args.args) }?;
10861088
idx += 1;
10871089
}
10881090
}
@@ -1096,25 +1098,37 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10961098
Ok(())
10971099
}
10981100

1099-
fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result {
1101+
unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result {
11001102
fmt.fill = arg.format.fill;
11011103
fmt.align = arg.format.align;
11021104
fmt.flags = arg.format.flags;
1103-
fmt.width = getcount(args, &arg.format.width);
1104-
fmt.precision = getcount(args, &arg.format.precision);
1105+
// SAFETY: arg and args come from the same Arguments,
1106+
// which guarantees the indexes are always within bounds.
1107+
unsafe {
1108+
fmt.width = getcount(args, &arg.format.width);
1109+
fmt.precision = getcount(args, &arg.format.precision);
1110+
}
11051111

11061112
// Extract the correct argument
1107-
let value = args[arg.position];
1113+
debug_assert!(arg.position < args.len());
1114+
// SAFETY: arg and args come from the same Arguments,
1115+
// which guarantees its index is always within bounds.
1116+
let value = unsafe { args.get_unchecked(arg.position) };
11081117

11091118
// Then actually do some printing
11101119
(value.formatter)(value.value, fmt)
11111120
}
11121121

1113-
fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
1122+
unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
11141123
match *cnt {
11151124
rt::v1::Count::Is(n) => Some(n),
11161125
rt::v1::Count::Implied => None,
1117-
rt::v1::Count::Param(i) => args[i].as_usize(),
1126+
rt::v1::Count::Param(i) => {
1127+
debug_assert!(i < args.len());
1128+
// SAFETY: cnt and args come from the same Arguments,
1129+
// which guarantees this index is always within bounds.
1130+
unsafe { args.get_unchecked(i).as_usize() }
1131+
}
11181132
}
11191133
}
11201134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
NM=nm
4+
5+
all: main.rs
6+
$(RUSTC) $< -O
7+
$(NM) $(call RUN_BINFILE,main) | $(CGREP) -v panicking panic_fmt panic_bounds_check pad_integral Display Debug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(lang_items)]
2+
#![feature(start)]
3+
#![no_std]
4+
5+
use core::fmt;
6+
use core::fmt::Write;
7+
8+
#[link(name = "c")]
9+
extern "C" {}
10+
11+
struct Dummy;
12+
13+
impl fmt::Write for Dummy {
14+
#[inline(never)]
15+
fn write_str(&mut self, _: &str) -> fmt::Result {
16+
Ok(())
17+
}
18+
}
19+
20+
#[start]
21+
fn main(_: isize, _: *const *const u8) -> isize {
22+
let _ = writeln!(Dummy, "Hello World");
23+
0
24+
}
25+
26+
#[lang = "eh_personality"]
27+
fn eh_personality() {}
28+
29+
#[panic_handler]
30+
fn panic(_: &core::panic::PanicInfo) -> ! {
31+
loop {}
32+
}

0 commit comments

Comments
 (0)