Skip to content

Speculos crashes and device app hangs on formatting with core::fmt::Write #113

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

Closed
dj8yfo opened this issue Jan 9, 2024 · 2 comments
Closed

Comments

@dj8yfo
Copy link

dj8yfo commented Jan 9, 2024

When using a type from https://github.com/dj8yfo/fmt_buffer/blob/master/src/lib.rs

// problematic snippet
write!(
    &mut buffer,
    "path: {}/{}\n",
    path.0[0], path.0[1]
).expect("truncated fmt buffer error");

an app crashes in speculos (not a panic) and hangs on a nanos device.

This is likely the reason, why numtoa is used in https://github.com/LedgerHQ/app-boilerplate-rust
instead of core::fmt.

// these snippets instead of  `write!`  work
   fmt_buf.write_str(num1.numtoa_str(10, &mut num_out));
   fmt_buf.write_str(" / ");
   fmt_buf.write_str(num2.numtoa_str(10, &mut num_out));


impl<const N: usize> FmtBuffer<N> {
    pub fn write_str(&mut self, s: &str) {
        let remaining_buf = &mut self.buffer[self.used..];
        let raw_s = s.as_bytes();

        let raw_s_len = raw_s.len();
        let remaining_len = remaining_buf.len();

        let bytes_written = if remaining_len < raw_s_len {
            self.truncated = true;
            match s.char_indices().rfind(|&(ind, _char)| ind <= remaining_len) {
                None => {
                    // noop, truncating all reftover chars
                    return;
                }
                Some((ind, _cahr)) => ind,
            }
        } else {
            raw_s_len
        };
        remaining_buf[..bytes_written].copy_from_slice(&raw_s[..bytes_written]);
        self.used += bytes_written;
    }
}

An issue, which looked very similar to this one by external features: rust-lang/rust#44538

@yhql
Copy link
Contributor

yhql commented Jan 10, 2024

This is due to the ropi-rwpi compilation mode and the fact that our operating system does not have a loader (yet). Some cases are not handled correctly, especially dyn Trait, which write! uses under the hood: for each formatting there is an indirect call to a function (so a 'dynamic' pointer to a static) and this is not handled by ropi.

This is usually solved by calling a specific macro/function (pic()), but this cannot be done in write!'s case because it is part of the standard library, which we can modify like this. There's also ongoing work to make it work in all cases transparently.
Currently in Rust code you can only recode your own formatting function :/

@dj8yfo
Copy link
Author

dj8yfo commented Jan 16, 2024

@yhql k, i'm glad you know and are confident about the exact reason of this problem)
Feel free to reopen it and link to any other issues/prs, if it might help the discoverability of this issue for other users stumbling across it.

@dj8yfo dj8yfo closed this as completed Jan 16, 2024
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

No branches or pull requests

2 participants