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

Detect tail calls in stack size estimation #2005

Merged
merged 2 commits into from
Feb 11, 2025
Merged

Conversation

mkeeter
Copy link
Collaborator

@mkeeter mkeeter commented Feb 7, 2025

In some cases, the deepest stack involves a tail call. For example, consider write_fmt, which has no stack of its own and branches directly to core::fmt::write (instead of using bl):

0804a704 <core::fmt::Write::write_fmt>:
 804a704:   460a        mov r2, r1
 804a706:   6849        ldr r1, [r1, #4]
 804a708:   2901        cmp r1, #1
 804a70a:   bf18        it  ne
 804a70c:   2900        cmpne   r1, #0
 804a70e:   4901        ldr r1, [pc, #4]    ; (804a714 <core::fmt::Write::write_fmt+0x10>)
 804a710:   f7ff ba68   b.w 8049be4 <core::fmt::write>
 804a714:   0804b024    .word   0x0804b024

This PR adds basic tail call support to the disassembler. The main effect is adding 80 bytes to panic stack depth.

Before:

udprpc: 2464 bytes (limit is 4096)
     [+8] _start
  [+2264] main
    [+16] core::array::<impl core::ops::index::IndexMut<I> for [T; N]>::index_mut
    [+16] <core::ops::range::Range<usize> as core::slice::index::SliceIndex<[T]>>::index_mut
    [+56] core::slice::index::slice_end_index_len_fail
    [+40] core::panicking::panic_fmt
    [+56] rust_begin_unwind
     [+8] userlib::sys_panic_stub

After:

udprpc: 2544 bytes (limit is 4096)
     [+8] _start
  [+2264] main
    [+16] core::array::<impl core::ops::index::IndexMut<I> for [T; N]>::index_mut
    [+16] <core::ops::range::Range<usize> as core::slice::index::SliceIndex<[T]>>::index_mut
    [+56] core::slice::index::slice_end_index_len_fail
    [+40] core::panicking::panic_fmt
    [+56] rust_begin_unwind
     [+0] core::fmt::Write::write_fmt
    [+88] core::fmt::write

Unfortunately, this does not detect the stack overflow in #2004, which happens during dynamic dispatch. Still, it's a step in the right direction!

@mkeeter mkeeter requested review from labbott, cbiffle and lzrd February 7, 2025 14:55
@mkeeter mkeeter force-pushed the mkeeter/stack-tail-calls branch from 96fd20d to 37ffca8 Compare February 11, 2025 15:51
@mkeeter mkeeter enabled auto-merge (squash) February 11, 2025 15:51
@mkeeter mkeeter merged commit 26dbadc into master Feb 11, 2025
125 checks passed
@mkeeter mkeeter deleted the mkeeter/stack-tail-calls branch February 11, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants