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

Crash or strange result when printing f32 #112

Closed
usbalbin opened this issue Aug 6, 2022 · 4 comments
Closed

Crash or strange result when printing f32 #112

usbalbin opened this issue Aug 6, 2022 · 4 comments

Comments

@usbalbin
Copy link
Contributor

usbalbin commented Aug 6, 2022

Not sure if this is the place for this kind of issue...

During #93 (see comment)I discovered that the following code snippet seems to crash on ESP32-S3 and likely ESP32(have not tested the last example on ESP32 yet):

fn main() -> anyhow::Result<()> {
    esp_idf_sys::link_patches();

    let v = 16.0;
    println!("f32 literal is={v}");

    let v = f32::from(16_u16);
    println!("Converting the number seems to work");
    println!("from u16 to f32 is={v}"); // <--- Boom
    loop {}
}

Also note in the log from where I first discovered the problem that there is weird output for several of the "duty" lines before the crash. From what I can tell the actual number printed is important. 16 seems to be the smallest integer >= 0 that when converted to f32 causes a crash. Some of the smaller values only causes weird output.

Somehow I can not seem to reproduce the problem by using a f32 literal(not the exact same value due to floating point magic?)

Compiling in release mode and using IDF v4.4

@MabezDev
Copy link
Member

MabezDev commented Aug 8, 2022

Thanks for the small example! I've managed to reproduce it, with a panic message and full backtrace:

f32 literal is=16
Converting the number seems to work
thread 'main' panicked at 'assertion failed: buf[0] > b\'0\'', /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/num/flt2dec/mod.rs:189:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
abort() was called at PC 0x4201cf4a on core 0
0x4201cf4a - panic_abort::__rust_start_panic::abort
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/panic_abort/src/lib.rs:43
Backtrace:0x40375a92:0x3fcf45c00x40378a25:0x3fcf45e0 0x4037de02:0x3fcf4600 0x4201cf4a:0x3fcf4670 0x4201cf3e:0x3fcf4690 0x4200d9fd:0x3fcf46b0 0x4200d961:0x3fcf4730 0x42017717:0x3fcf47a0 0x42017545:0x3fcf47d0 0x4200d7a9:0x3fcf4800 0x42026644:0x3fcf4830 0x420265ab:0x3fcf4870 0x4202afc0:0x3fcf48b0 0x4202b0ac:0x3fcf48d0 0x420299af:0x3fcf4950 0x42029c8c:0x3fcf49d0 0x4202a0ac:0x3fcf49f0 0x42019664:0x3fcf4a40 0x42019488:0x3fcf4a90 0x420195df:0x3fcf4ad0 0x42002d55:0x3fcf4b50 0x4203fe5f:0x3fcf4ba0 0x42002d80:0x3fcf4bc0 0x420057d9:0x3fcf4be0 0x42002d71:0x3fcf4c00 0x42002e13:0x3fcf4c30 0x420410e0:0x3fcf4c50 
0x40375a92 - panic_abort
    at /home/mabez/development/c/esp-idf/components/esp_system/panic.c:402
0x40378a25 - esp_system_abort
    at /home/mabez/development/c/esp-idf/components/esp_system/esp_system.c:128
0x4037de02 - abort
    at /home/mabez/development/c/esp-idf/components/newlib/abort.c:46
0x4201cf4a - panic_abort::__rust_start_panic::abort
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/panic_abort/src/lib.rs:43
0x4201cf3e - __rust_start_panic
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/panic_abort/src/lib.rs:38
0x4200d9fd - rust_panic
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/panicking.rs:746
0x4200d961 - std::panicking::rust_panic_with_hook
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/panicking.rs:716
0x42017717 - std::panicking::begin_panic_handler::{{closure}}
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/panicking.rs:??
0x42017545 - std::sys_common::backtrace::__rust_end_short_backtrace
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:138
0x4200d7a9 - rust_begin_unwind
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/panicking.rs:584
0x42026644 - core::panicking::panic_fmt
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/panicking.rs:142
0x420265ab - core::panicking::panic
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/panicking.rs:48
0x4202afc0 - core::num::flt2dec::digits_to_dec_str
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/num/flt2dec/mod.rs:??
0x4202b0ac - core::num::flt2dec::to_shortest_str
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/num/flt2dec/mod.rs:398
0x420299af - core::fmt::float::float_to_decimal_common_shortest
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/fmt/float.rs:66
0x42029c8c - core::fmt::float::float_to_decimal_display
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/fmt/float.rs:??
0x4202a0ac - core::fmt::write
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:1196
0x42019664 - std::io::Write::write_fmt
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/io/mod.rs:1654
0x42019488 - <&std::io::stdio::Stdout as std::io::Write>::write_fmt
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/io/stdio.rs:712
0x420195df - <std::io::stdio::Stdout as std::io::Write>::write_fmt
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/io/stdio.rs:686
0x42002d55 - esp_float_crash::main
    at /home/mabez/development/rust/embedded/experiments/esp-float-crash/src/main.rs:13
0x4203fe5f - core::ops::function::FnOnce::call_once
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/ops/function.rs:248
0x42002d80 - std::rt::lang_start::{{closure}}
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/rt.rs:145
0x420057d9 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
    at /home/mabez/development/rust/xtensa/rust-xtensa-dev/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/core/src/ops/function.rs:280
0x42002d71 - main
    at ??:??
0x42002e13 - app_main
    at /home/mabez/.cargo/registry/src/github.com-1ecc6299db9ec823/esp-idf-sys-0.31.7/src/start.rs:46
0x420410e0 - main_task
    at /home/mabez/development/c/esp-idf/components/freertos/port/port_common.c:141

Seems the assert is failing here. I'll try and figure out what's going on.

@MabezDev
Copy link
Member

I found a few issues with the Rust Xtensa ABI implementation, which have been fixed in the new 1.63.0.1 release. I believe this fixes the issue here. You can find a prerelease here: https://github.com/esp-rs/rust-build/releases/tag/v1.63.0.1

Closing for now, please ping this thread again if it doesn't fix the issue for you :)

@usbalbin
Copy link
Contributor Author

usbalbin commented Aug 27, 2022

Now I am getting this seemingly at random in another project:

thread '<unnamed>' panicked at 'assertion failed: buf[0] > b\'0\'', /home/albin/.rustup/toolchains/esp/lib/ru
stlib/src/rust/library/core/src/num/flt2dec/mod.rs:189:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
abort() was called at PC 0x42028f3e [_ZN11panic_abort18__rust_start_panic5abort17h177b9ee7c03cc387E:??:??] on
 core 0
Backtrace:0x40375c8a [panic_abort:/home/albin/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release-v4.
4/components/esp_system/panic.c:402]:0x3fced5400x4037b659 [esp_system_abort:/home/albin/ceph/rust/dcdc-conver
ter/.embuild/espressif/esp-idf/release-v4.4/components/esp_system/esp_system.c:128]:0x3fced560 0x40381c2a [ab
ort:/home/albin/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release-v4.4/components/newlib/abort.c:46
]:0x3fced580 0x42028f3e [_ZN11panic_abort18__rust_start_panic5abort17h177b9ee7c03cc387E:??:??]:0x3fced5f0 0x4
2028f32 [__rust_start_panic:??:??]:0x3fced610 0x4201b145 [rust_panic:??:??]:0x3fced630 0x4201b0a9 [_ZN3std9pa
nicking20rust_panic_with_hook17hb89405c732f83e50E:??:??]:0x3fced6b0 0x4202546b [_ZN3std9panicking19begin_pani
c_handler28_$u7b$$u7b$closure$u7d$$u7d$17h7f506a8d33fa63e4E:??:??]:0x3fced720 0x42025299 [_ZN3std10sys_common
9backtrace26__rust_end_short_backtrace17h3d53297afbaf5aa5E:??:??]:0x3fced750 0x4201aef1 [rust_begin_unwind:??
:??]:0x3fced780 0x4203353c [_ZN4core9panicking9panic_fmt17h276ede2116b4f4beE:??:??]:0x3fced7b0 0x420334a3 [_Z
N4core9panicking5panic17h63673a252d46609bE:??:??]:0x3fced7f0 0x42038668 [_ZN4core3num7flt2dec17digits_to_dec_
str17h879d9007659e2605E:??:??]:0x3fced830 0x42038754 [_ZN4core3num7flt2dec15to_shortest_str17hc23f6418c16e767
dE:??:??]:0x3fced850 0x42036f03 [_ZN4core3fmt5float32float_to_decimal_common_shortest17hf2ab7cb578e71bb9E.llv
m.13186556124017172358:??:??]:0x3fced8d0 0x420371e0 [_ZN4core3fmt5float52_$LT$impl$u20$core..fmt..Display$u20
$for$u20$f32$GT$3fmt17hda1d22a0d4d97892E:??:??]:0x3fced950 0x4203762c [_ZN4core3fmt5write17hda45f5da99590714E
:??:??]:0x3fced970 0x420273cc [_ZN3std2io5Write9write_fmt17h0289dd8e57306a1cE:??:??]:0x3fced9c0 0x420271f0 [_
ZN61_$LT$$RF$std..io..stdio..Stdout$u20$as$u20$std..io..Write$GT$9write_fmt17h6c080d2f8f694b56E:??:??]:0x3fce
da10 0x42027347 [_ZN3std2io5stdio6_print17haf1444b0d0539906E:??:??]:0x3fceda50 0x4200477f [_ZN12dc_converter7
handler17h784339f99a8760dbE:??:??]:0x3fcedad0 0x42004f73 [_ZN12dc_converter4main28_$u7b$$u7b$closure$u7d$$u7d
$17h85d68c4e989eeed5E.llvm.1139724781367824989:??:??]:0x3fcedc10 0x4200cfe2 [_ZN86_$LT$alloc..boxed..Box$LT$F
$C$A$GT$$u20$as$u20$core..ops..function..Fn$LT$Args$GT$$GT$4call17hae554cb11fd57bd3E:??:??]:0x3fcedc60 0x4200
cfe2 [_ZN86_$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..Fn$LT$Args$GT$$GT$4call17hae55
4cb11fd57bd3E:??:??]:0x3fcedca0 0x42009971 [_ZN11esp_idf_svc5httpd6Server6handle17h9990288062c7aa47E:??:??]:0
x3fcedce0 0x4205caea [httpd_uri:/home/albin/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release-v4.4/
components/esp_http_server/src/httpd_uri.c:329]:0x3fcedf60 0x4205bc34 [httpd_parse_req:/home/albin/ceph/rust/
dcdc-converter/.embuild/espressif/esp-idf/release-v4.4/components/esp_http_server/src/httpd_parse.c:659]:0x3f
cedfa0 0x4205c1be [httpd_sess_process:/home/albin/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release
-v4.4/components/esp_http_server/src/httpd_sess.c:412]:0x3fcee050 0x4205af18 [httpd_process_session:/home/alb
in/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release-v4.4/components/esp_http_server/src/httpd_main
.c:178]:0x3fcee070 0x420a1fae [httpd_sess_enum:/home/albin/ceph/rust/dcdc-converter/.embuild/espressif/esp-id
f/release-v4.4/components/esp_http_server/src/httpd_sess.c:50]:0x3fcee090 0x4205b01f [httpd_server:/home/albi
n/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release-v4.4/components/esp_http_server/src/httpd_main.
c:228]:0x3fcee0b0 
ELF file SHA256: 0000000000000000
Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x403759b4 [esp_restart_noos:/home/albin/ceph/rust/dcdc-converter/.embuild/espressif/esp-idf/release
-v4.4/components/esp_system/port/soc/esp32s3/system_internal.c:143]

Have not managed to find out much yet...

@MabezDev
Copy link
Member

Apologies, I discovered this hasn't actually been fixed but forgot to re-open this! Issue is being tracking in esp-rs/rust#136, sorry!

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