#Does anyone know why the `format!()` macro would cause a crash

61 messages · Page 1 of 1 (latest)

edgy locust
#

I'm writing a hypervisor in Rust and allocations work but whenever I call the format! macro my system crashes. It doesn't crash when I allocate a string

vestal tapir
#

does format!("Hello") crash?

edgy locust
#

Yes

edgy locust
vestal tapir
#

hmmmm

#

and i assume this is a segfault

#

or something

#

"crash" is very not specific

#

what's the error?

edgy locust
#

I assume some CPU exception happens

vestal tapir
#

well, can you run it in a debugger and see which one?

edgy locust
#

It's kind of hard to debug because my code is running in vmx root

vestal tapir
#

ah

#

can you register CPU exception handlers

#

could be a stack overflow

edgy locust
#

actually yeah maybe

vestal tapir
#

also you've not declared your own format macro right

edgy locust
#
    serial.write_str(&String::from("asdf\n"));
vestal tapir
#

you're using the stdlib one

edgy locust
#

yes

#

i'm using stdlib one

vestal tapir
#

ok

edgy locust
#
    serial.write_str(&format!("asdf\n"));
#

it's possible the stack is corrupted

vestal tapir
#

?expand

format!("Hello")

austere ospreyBOT
#
{
    let res = ::alloc::fmt::format(::core::fmt::Arguments::new_v1(&["Hello"], &[]));
    res
}
vestal tapir
#

hmmm

#

maybe split it up into 2 parts

#

once doing format_args!("Hello")

#

the other doing a to_string() on that

#

see where it crashes?

edgy locust
#

format_args! works

#

that's interesting

#

it's crashing in to_string

#
    let buf = [0u8; 0x500];
    for i in buf {
        if i != 0 {
            serial.write_str("bad\n");
        }
    }
#

this works fine so i assume it's not an issue with the stack

#

args.as_str() works

#

?expand

format_args!("hello")
austere ospreyBOT
#
::core::fmt::Arguments::new_v1(&["hello"], &[])
edgy locust
#

@vestal tapir Found the issue

#
    if args.pieces.len() != args.args.len() {
        serial.write_str("3\n");
    }
edgy locust
#

Ok I don't know if anyone is still reading this but this is the weirdest issue ever

#

I found the issue is in the Write::write_str method

#

If I put this function inside core/fmt/mod.rs it crashes

#

But if I put it in my lib.rs it doesn't crash

#

This works

#

This crashes

hollow raft
#

An explanation for this last thing would be the usage of a fmt::Write impl vs a io::Write one, maybe

#

Regarding the initial question, have you checked that your realloc logic is fine. And also make sure you're not recursing into string formatting inside the alloc logic

edgy locust
edgy locust
#

Looks like it's an issue with the trait object

#

It works if I use static dispatch