chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Local::ymd panics when using WASI runtime and setting before 1970 year

Open higumachan opened this issue 4 years ago • 2 comments

Hello,

I tried that code. (https://github.com/higumachan/chrono-wasi-check) I got error in WASI.

use chrono::prelude::*;


fn get_another_epoch<T>(timezone: &T) -> Date<T>
where
    T: TimeZone,
{
        timezone.ymd(1969, 1, 1)
}

fn get_unix_epoch<T>(timezone: &T) -> Date<T>
where
    T: TimeZone,
{
        timezone.ymd(1970, 1, 1)
}

fn main() {
    println!("utc       ={:?}", get_unix_epoch(&Utc));
    println!("fixed+9   ={:?}", get_unix_epoch(&FixedOffset::east(9 * 3600)));
    println!("fixed+0   ={:?}", get_unix_epoch(&FixedOffset::east(0)));
    println!("local     ={:?}", get_unix_epoch(&Local));
    println!("utc       ={:?}", get_another_epoch(&Utc));
    println!("fixed+9   ={:?}", get_another_epoch(&FixedOffset::east(9 * 3600)));
    println!("fixed+0   ={:?}", get_another_epoch(&FixedOffset::east(0)));
    println!("local     ={:?}", get_another_epoch(&Local));
}

when normal case(in my local machine)

cargo run

I got that outputs.

utc       =1970-01-01Z
fixed+9   =1970-01-01+09:00
fixed+0   =1970-01-01+00:00
local     =1970-01-01+00:00
utc       =1969-01-01Z
fixed+9   =1969-01-01+09:00
fixed+0   =1969-01-01+00:00
local     =1969-01-01+00:00

But wasi case(in wasmtimes)

cargo wasi run --features chrono/wasmbind

I got that error.

utc       =1970-01-01Z
fixed+9   =1970-01-01+09:00
fixed+0   =1970-01-01+00:00
local     =1970-01-01+00:00
utc       =1969-01-01Z
fixed+9   =1969-01-01+09:00
fixed+0   =1969-01-01+00:00
thread 'main' panicked at 'invalid or out-of-range date', /home/kicodevs/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-0.4.19/src/naive/date.rs:221:47
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: failed to run main module `target/wasm32-wasi/debug/wasi-chrono.wasm`

Caused by:
    0: failed to invoke command default
    1: wasm trap: unreachable
       wasm backtrace:
           0: 0x1caec - <unknown>!__rust_start_panic
           1: 0x165ec - <unknown>!rust_panic
           2: 0x8b93 - <unknown>!std::panicking::rust_panic_with_hook::h308937c11f14a0b0
           3: 0x17b06 - <unknown>!std::panicking::begin_panic_handler::{{closure}}::h2f1ffd2e7af6e54e
           4: 0x1c060 - <unknown>!std::sys_common::backtrace::__rust_end_short_backtrace::h0391a3209a34137c
           5: 0x1bbe3 - <unknown>!rust_begin_unwind
           6: 0x1c0c4 - <unknown>!core::panicking::panic_fmt::hf6b8f38d129daac6
           7: 0x1a5a0 - <unknown>!core::option::expect_failed::hb31b2218d74d4aa2
           8: 0x18f7d - <unknown>!core::option::Option<T>::expect::h84b09ab808bc01ed
           9: 0x187b5 - <unknown>!chrono::naive::date::NaiveDate::from_yo::h9a14d6e6f347906f
          10: 0x14fa8 - <unknown>!chrono::offset::local::tm_to_datetime::tm_to_naive_date::hd7c41fc7f98815d1
          11: 0xcd67 - <unknown>!chrono::offset::local::tm_to_datetime::h7d897871d0502300
          12: 0xbbfa - <unknown>!<chrono::offset::local::Local as chrono::offset::TimeZone>::from_local_datetime::h9c729ae476ea930d
          13: 0x1077b - <unknown>!<chrono::offset::local::Local as chrono::offset::TimeZone>::from_local_date::h674362eca67a26ea
          14: 0x15e63 - <unknown>!chrono::offset::TimeZone::ymd_opt::hd70817bd26af8ae2
          15: 0x178fa - <unknown>!chrono::offset::TimeZone::ymd::h52527072648008ed
          16: 0x1a152 - <unknown>!wasi_chrono::get_another_epoch::h40bc43c19c6f9da6
          17: 0x5643 - <unknown>!wasi_chrono::main::h8c5c59229827dab1
          18: 0x1bda4 - <unknown>!core::ops::function::FnOnce::call_once::hc3e9c4de173f8574
          19: 0x1bc53 - <unknown>!std::sys_common::backtrace::__rust_begin_short_backtrace::h4e23faa3aa779500
          20: 0x1b748 - <unknown>!std::rt::lang_start::{{closure}}::h112c6149dda834fe
          21: 0x14901 - <unknown>!std::rt::lang_start_internal::hcfff2f9346b7fb75
          22: 0x1a10a - <unknown>!std::rt::lang_start::hb8374a1dedc53687
          23: 0x1c6dd - <unknown>!__original_main
          24: 0x1c7d7 - <unknown>!_start
          25: 0x1ca54 - <unknown>!_start.command_export
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable to may show more debugging information

error: failed to execute "wasmtime" "--" "target/wasm32-wasi/debug/wasi-chrono.wasm"
    status: exit status: 134

I'm investigating, but so far I haven't figured out why. Do you have any ideas?

higumachan avatar Sep 22 '21 10:09 higumachan

this is my wasmtime version wasmtime 0.30.0.

higumachan avatar Sep 22 '21 10:09 higumachan

I think this problem core is that codes.

https://github.com/chronotope/chrono/blob/v0.4.19/src/sys/unix.rs#L80-L104 https://github.com/chronotope/chrono/blob/main/src/offset/local.rs#L27-L31

libc::localtime_r(&sec, &mut out) seems to return tm_yday for 1970 or earlier in the WASM environment, and -365 for January 1. (In Linux, it returned 0).

However, the tm_yday is treated as tm.tm_yday as u32 + 1, and the value is 4294966932, which causes an error.

higumachan avatar Sep 24 '21 03:09 higumachan

Hi @higumachan - apologies for the long delay on this issue. Are you no longer running into this problem? If it is still occurring perhaps we should look into doing more testing on the wasi target in CI

When I try to run this now I get the following:

Error: failed to run main module `target/wasm32-wasi/debug/examples/wasi-test.wasm`

Caused by:
    0: if you're trying to run a precompiled module, pass --allow-precompiled
    1: failed to parse WebAssembly module
    2: Unsupported feature: Support for interface types has temporarily been removed from `wasmtime`.

       For more information about this temporary change you can read on the issue online:

           https://github.com/bytecodealliance/wasmtime/issues/1271

       and for re-adding support for interface types you can see this issue:

           https://github.com/bytecodealliance/wasmtime/issues/677

esheppa avatar Mar 03 '23 11:03 esheppa