Skip to content

format_args! with certain arguments segfaults at run-time on x86_64-unknown-linux-gnux32 #59220

Closed
@tormol

Description

@tormol

Trying to display most types causes a segmentation fault at run time when compiled in debug mode for x32 ABI

Example:

echo 'fn main() { println!("{}", 0); }' | rustc --target x86_64-unknown-linux-gnux32 -
./rust_out
Segmentation fault (core dumped)
  • println!, format! and format_args! equally affected, .to_string() is not.
  • Only happens in debug mode, works fine in release mode.
  • Happens with all integer types from i8 to u128, f32, f64, bool, char and fmt::Argument, but not with references to those types or str, String and raw pointers.
  • Debug and pointer formatting ("{:?}", "{:#?}" and "{:p}") are not affect, but all other format traits ("{:x}", "{:02}", "{:#b}"`) all result in a segfault.
Debugging attempt & assembly

The segfault happens at the second instruction in main;
here's what that area looks like with ... --emit asm:

_ZN8rust_out4main17h19287f9e3b324627E:
	.cfi_startproc
	subl	$72, %esp
	.cfi_def_cfa_offset 80
	movl	_ZN4core3fmt3num52_$LT$impl$u20$core..fmt..Display$u20$for$u20$i32$GT$3fmt17ha3b2ffd94f72c608E@GOTPCREL, %esi
	leal	.L__unnamed_2(%rip), %eax
	movl	%eax, 68(%esp)
	movl	68(%esp), %edi
	callq	_ZN4core3fmt10ArgumentV13new17hfeb1c719432004d8E
	movl	%eax, 28(%esp)
	movl	%edx, 24(%esp)
-- snip

and after disassembling: (objcopy -dr rust_out | rg -A10 'rust_out.*?main'

00002610 <_ZN8rust_out4main17h19287f9e3b324627E>:
    2610:	83 ec 48             	sub    $0x48,%esp
    2613:	8b 34 25 aa 49 22 00 	mov    0x2249aa,%esi
    261a:	8d 05 84 9a 01 00    	lea    0x19a84(%rip),%eax        # 1c0a4 <_fini+0x10>
    2620:	67 89 44 24 44       	mov    %eax,0x44(%esp)
    2625:	67 8b 7c 24 44       	mov    0x44(%esp),%edi
    262a:	e8 81 fe ff ff       	callq  24b0 <_ZN4core3fmt10ArgumentV13new17hfeb1c719432004d8E>
    262f:	67 89 44 24 1c       	mov    %eax,0x1c(%esp)
    2634:	67 89 54 24 18       	mov    %edx,0x18(%esp)
    2639:	8d 05 c1 36 22 00    	lea    0x2236c1(%rip),%eax        # 225d00 <debug_section_names+0x40>
    263f:	8d 0d 63 9a 01 00    	lea    0x19a63(%rip),%ecx        # 1c0a8 <_fini+0x14>
-- snip

With -O the compiler emits

72:_ZN8rust_out4main17h19287f9e3b324627E:
73-	.cfi_startproc
74-	subl	$40, %esp
75-	.cfi_def_cfa_offset 48
76-	leal	.L__unnamed_2(%rip), %eax
77-	movl	%eax, 8(%esp)
78-	movl	_ZN4core3fmt3num52_$LT$impl$u20$core..fmt..Display$u20$for$u20$i32$GT$3fmt17ha3b2ffd94f72c608E@GOTPCREL(%rip), %eax
79-	movl	%eax, 12(%esp)
80-	leal	.L__unnamed_3(%rip), %eax
81-	movl	%eax, 16(%esp)
82-	movl	$2, 20(%esp)
-- snip

For println!("{:?}", 0):

314:_ZN8rust_out4main17h19287f9e3b324627E:
315-	.cfi_startproc
316-	subl	$72, %esp
317-	.cfi_def_cfa_offset 80
318-	leal	.L__unnamed_2(%rip), %eax
319-	movl	%eax, 68(%esp)
320-	movl	68(%esp), %edi
321-	leal	_ZN4core3fmt3num50_$LT$impl$u20$core..fmt..Debug$u20$for$u20$i32$GT$3fmt17had225e6626567b7bE(%rip), %esi
322-	callq	_ZN4core3fmt10ArgumentV13new17hfeb1c719432004d8E
323-	movl	%eax, 28(%esp)
324-	movl	%edx, 24(%esp)
-- snip

and for println!("{}", &0)

268:_ZN8rust_out4main17h19287f9e3b324627E:
269-	.cfi_startproc
270-	subl	$72, %esp
271-	.cfi_def_cfa_offset 80
272-	leal	.L__unnamed_2(%rip), %eax
273-	movl	%eax, 68(%esp)
274-	movl	68(%esp), %edi
275-	leal	_ZN44_$LT$$RF$T$u20$as$u20$core..fmt..Display$GT$3fmt17h1aa6d8d315ee6bb6E(%rip), %esi
276-	callq	_ZN4core3fmt10ArgumentV13new17h122dce191cc0b572E
277-	movl	%eax, 28(%esp)
278-	movl	%edx, 24(%esp)
-- snip

Meta

rustc 1.33.0 (2aa4c46cf 2019-02-28)
binary: rustc
commit-hash: 2aa4c46cfdd726e97360c2734835aa3515e8c858
commit-date: 2019-02-28
host: x86_64-unknown-linux-gnu
release: 1.33.0
LLVM version: 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-x32x32 ABIP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions