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

SIGILLなテスト #2

Closed
KisaragiEffective opened this issue Jan 13, 2023 · 6 comments · Fixed by #3
Closed

SIGILLなテスト #2

KisaragiEffective opened this issue Jan 13, 2023 · 6 comments · Fixed by #3
Labels
bug Something isn't working

Comments

@KisaragiEffective
Copy link
Owner

ときたま再現するの謎。 rust-lang/rust#69558 あたりが関連している?

@KisaragiEffective KisaragiEffective added the bug Something isn't working label Jan 13, 2023
@KisaragiEffective
Copy link
Owner Author

cargo cleanしてcargo testしても変わらない。

@KisaragiEffective
Copy link
Owner Author

多分次のコードで再現するはず:

#[cfg(test)]
mod test {
    #[test]
    fn test() {
        pretty_size(0);
    }
    
    fn pretty_size(bytes: usize) -> String {
        let mut buf = [0u8; 5];
        let mut rest = bytes;
        let mut head_index: Option<NonZeroUsize> = None;
        write_numeric_char(bytes, 1000, &mut buf, 0, &mut rest, &mut head_index);
        write_numeric_char(bytes, 100, &mut buf, 1, &mut rest, &mut head_index);
        write_numeric_char(bytes, 10, &mut buf, 2, &mut rest, &mut head_index);
        write_numeric_char(bytes, 1, &mut buf, 3, &mut rest, &mut head_index);
        buf[4] = b'B';
        let head = unsafe { head_index.unwrap_unchecked() }.get() - 1;
        unsafe { std::str::from_utf8_unchecked(&buf[head..]).to_string() }
    }
    
    fn write_numeric_char(n: usize, pow: usize, bytes: &mut [u8], byte_index: usize, r: &mut usize, head: &mut Option<NonZeroUsize>) {
        if n >= pow {
            bytes[byte_index] = convert_to_numeric_char(*r / pow);
            *r -= *r / pow * pow;
            head.get_or_insert_with(|| (unsafe { NonZeroUsize::new_unchecked(byte_index + 1) }));
        }
    }

    fn convert_to_numeric_char(a: usize) -> u8 {
        match a {
            0 => b'0',
            1 => b'1',
            2 => b'2',
            3 => b'3',
            4 => b'4',
            5 => b'5',
            6 => b'6',
            7 => b'7',
            8 => b'8',
            9 => b'9',
            _ => unreachable!("{}", a)
        }
    }
}

@KisaragiEffective
Copy link
Owner Author

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0
rustc 1.67.0-nightly (215e3cd21 2022-11-03)
binary: rustc
commit-hash: 215e3cd218b83b8a3152d84d92f17109253c25e1
commit-date: 2022-11-03
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

@KisaragiEffective
Copy link
Owner Author

ただのUBだった

@KisaragiEffective
Copy link
Owner Author

$ cargo miri test
Preparing a sysroot for Miri (target: x86_64-unknown-linux-gnu)... done
WARNING: Ignoring `RUSTC_WRAPPER` environment variable, Miri does not support wrapping.
   Compiling autocfg v1.1.0
   Compiling proc-macro2 v1.0.49
   Compiling unicode-ident v1.0.6
   Compiling quote v1.0.23
   Compiling syn v1.0.107
   Compiling libc v0.2.139
   Compiling cfg-if v1.0.0
   Compiling log v0.4.17
   Compiling once_cell v1.17.0
   Compiling version_check v0.9.4
   Compiling cc v1.0.78
   Compiling pkg-config v0.3.26
   Compiling serde_derive v1.0.152
   Compiling serde v1.0.152
   Compiling ppv-lite86 v0.2.17
   Compiling siphasher v0.3.10
   Compiling pin-project-lite v0.2.9
   Compiling phf_shared v0.10.0
   Compiling bytes v1.3.0
   Compiling memchr v2.5.0
   Compiling futures-core v0.3.25
   Compiling getrandom v0.2.8
   Compiling tokio v1.24.1
   Compiling lock_api v0.4.9
   Compiling rand_core v0.6.4
   Compiling parking_lot_core v0.9.6
   Compiling rand_chacha v0.3.1
   Compiling unicase v2.6.0
   Compiling itoa v1.0.5
   Compiling rand v0.8.5
   Compiling num_cpus v1.15.0
   Compiling socket2 v0.4.7
   Compiling mio v0.8.5
   Compiling bitflags v1.3.2
   Compiling smallvec v1.10.0
   Compiling scopeguard v1.1.0
   Compiling new_debug_unreachable v1.0.4
   Compiling openssl-sys v0.9.80
   Compiling mac v0.1.1
   Compiling futures-task v0.3.25
   Compiling futf v0.1.5
   Compiling indexmap v1.9.2
   Compiling parking_lot v0.12.1
   Compiling slab v0.4.7
   Compiling tracing-core v0.1.30
   Compiling precomputed-hash v0.1.1
   Compiling futures-util v0.3.25
   Compiling foreign-types-shared v0.1.1
   Compiling fnv v1.0.7
   Compiling utf-8 v0.7.6
   Compiling openssl v0.10.45
   Compiling tendril v0.4.3
   Compiling http v0.2.8
   Compiling phf_generator v0.10.0
   Compiling tracing v0.1.37
   Compiling string_cache_codegen v0.5.2
   Compiling phf_codegen v0.10.0
   Compiling foreign-types v0.3.2
   Compiling phf v0.10.1
   Compiling proc-macro-error-attr v1.0.4
   Compiling io-lifetimes v1.0.3
   Compiling hashbrown v0.12.3
   Compiling httparse v1.8.0
   Compiling tinyvec_macros v0.1.0
   Compiling native-tls v0.2.11
   Compiling pin-utils v0.1.0
   Compiling futures-channel v0.3.25
   Compiling crc32fast v1.3.2
   Compiling futures-sink v0.3.25
   Compiling tinyvec v1.6.0
   Compiling markup5ever v0.11.0
   Compiling sqlite3-src v0.2.12
   Compiling proc-macro-error v1.0.4
   Compiling num-traits v0.2.15
   Compiling try-lock v0.2.4
   Compiling percent-encoding v2.2.0
   Compiling rustix v0.36.6
   Compiling adler v1.0.2
   Compiling heck v0.4.0
   Compiling rustversion v1.0.11
   Compiling openssl-probe v0.1.5
   Compiling form_urlencoded v1.1.0
   Compiling miniz_oxide v0.6.2
   Compiling unicode-normalization v0.1.22
   Compiling want v0.3.0
   Compiling mime_guess v2.0.4
   Compiling http-body v0.4.5
   Compiling errno v0.2.8
   Compiling num-integer v0.1.45
   Compiling httpdate v1.0.2
   Compiling encoding_rs v0.8.31
   Compiling serde_json v1.0.91
   Compiling linux-raw-sys v0.1.4
   Compiling unicode-bidi v0.3.8
   Compiling ryu v1.0.12
   Compiling tower-service v0.3.2
   Compiling flate2 v1.0.25
   Compiling idna v0.3.0
   Compiling mime v0.3.16
   Compiling os_str_bytes v6.4.1
   Compiling thiserror v1.0.38
   Compiling bit-vec v0.6.3
   Compiling url v2.3.1
   Compiling bit-set v0.5.3
   Compiling clap_lex v0.3.0
   Compiling time v0.1.45
   Compiling remove_dir_all v0.5.3
   Compiling fastrand v1.8.0
   Compiling termcolor v1.1.3
   Compiling ipnet v2.7.1
   Compiling base64 v0.13.1
   Compiling strsim v0.10.0
   Compiling iana-time-zone v0.1.53
   Compiling tempfile v3.3.0
   Compiling is-terminal v0.4.2
   Compiling tokio-macros v1.8.2
   Compiling openssl-macros v0.1.0
   Compiling html5ever v0.26.0
   Compiling clap_derive v4.0.21
   Compiling strum_macros v0.24.3
   Compiling thiserror-impl v1.0.38
   Compiling strum v0.24.1
   Compiling clap v4.0.32
   Compiling tokio-util v0.7.4
   Compiling tokio-native-tls v0.3.0
   Compiling async-compression v0.3.15
   Compiling sqlite3-sys v0.12.0
   Compiling sqlite3 v0.24.0
   Compiling h2 v0.3.15
   Compiling hyper v0.14.23
   Compiling string_cache v0.8.4
   Compiling serde_urlencoded v0.7.1
   Compiling chrono v0.4.23
   Compiling hyper-tls v0.5.0
   Compiling reqwest v0.11.13
   Compiling xml5ever v0.17.0
   Compiling markup5ever_rcdom v0.2.0
   Compiling select v0.6.0
   Compiling kisaragi-booth-utility v0.1.0 (/media/kisaragi/1ff2735d-2180-4373-b9d0-5659da182833/IdeaProjects/booth-upload)
    Finished test [unoptimized + debuginfo] target(s) in 34.11s
     Running unittests src/main.rs (target/miri/x86_64-unknown-linux-gnu/debug/deps/kisaragi_booth_utility-0ae8cad78466f1e5)
warning: type alias `JapanStandardTime` is never used
  --> src/booth.rs:65:6
   |
65 | type JapanStandardTime = DateTime<FixedOffset>;
   |      ^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: field `item_id` is never read
  --> src/booth.rs:94:5
   |
93 | pub struct BoothUploadedObject {
   |            ------------------- field in this struct
94 |     item_id: ItemId,
   |     ^^^^^^^

warning: function `none` is never used
  --> src/booth.rs:99:10
   |
99 | const fn none<T>() -> Option<T> {
   |          ^^^^


running 2 tests
test pretty_size::test::main ... error: Undefined Behavior: entering unreachable code
   --> src/pretty_size.rs:134:29
    |
134 |         let head = unsafe { head_index.unwrap_unchecked() }.get() - 1;
    |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
    = note: BACKTRACE:
    = note: inside `pretty_size::pretty_size` at src/pretty_size.rs:134:29
note: inside `pretty_size::test::main` at src/pretty_size.rs:176:38
   --> src/pretty_size.rs:176:38
    |
176 |             assert_eq!(reference(b), pretty_size(b));
    |                                      ^^^^^^^^^^^^^^
note: inside closure at src/pretty_size.rs:173:5
   --> src/pretty_size.rs:173:5
    |
172 |       #[test]
    |       ------- in this procedural macro expansion
173 | /     fn main() {
174 | |         for b in TEST_BYTES {
175 | |             // assert_eq!(reference(b), int_op(b));
176 | |             assert_eq!(reference(b), pretty_size(b));
...   |
183 | |         println!("{}", reference(999999));
184 | |     }
    | |_____^
    = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error; 3 warnings emitted

error: test failed, to rerun pass `--bin kisaragi-booth-utility`

Caused by:
  process didn't exit successfully: `/home/kisaragi/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner /media/kisaragi/1ff2735d-2180-4373-b9d0-5659da182833/IdeaProjects/booth-upload/target/miri/x86_64-unknown-linux-gnu/debug/deps/kisaragi_booth_utility-0ae8cad78466f1e5` (exit status: 1)

@KisaragiEffective
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant