danstiner / rust-u2f

U2F security token emulator written in Rust
Apache License 2.0
293 stars 43 forks source link

Build failure on rust 1:1.64.0 on ArchLinux #98

Closed kousu closed 1 year ago

kousu commented 1 year ago

Describe the bug

If I try to build from source it fails with

   Compiling tokio-linux-uhid v0.4.2 (/home/build2/softu2f-src/rust-u2f/linux/uhid-tokio)
error: reference to packed field is unaligned
   --> linux/uhid-tokio/src/codec.rs:230:31
    |
230 |                 let payload = &event.u.start;
    |                               ^^^^^^^^^^^^^^

Logs

[build2@cb90fada0e7d softu2f-src]$ git clone https://github.com/danstiner/rust-u2f.git
Cloning into 'rust-u2f'...
remote: Enumerating objects: 2907, done.
remote: Counting objects: 100% (901/901), done.
remote: Compressing objects: 100% (398/398), done.
remote: Total 2907 (delta 665), reused 650 (delta 498), pack-reused 2006
Receiving objects: 100% (2907/2907), 783.96 KiB | 2.21 MiB/s, done.
Resolving deltas: 100% (1778/1778), done.
[build2@cb90fada0e7d softu2f-src]$ cd rust-u2f/
[build2@cb90fada0e7d rust-u2f]$ cd linux/
[build2@cb90fada0e7d linux]$ make
cargo build --manifest-path ../Cargo.toml
   Compiling libc v0.2.123
   Compiling proc-macro2 v1.0.37
   Compiling unicode-xid v0.2.2
   Compiling autocfg v1.1.0
   Compiling syn v1.0.91
   Compiling cfg-if v1.0.0
   Compiling memchr v2.4.1
   Compiling log v0.4.16
   Compiling serde_derive v1.0.136
   Compiling lazy_static v1.4.0
   Compiling serde v1.0.136
   Compiling futures-core v0.3.21
   Compiling pin-project-lite v0.2.8
   Compiling typenum v1.15.0
   Compiling version_check v0.9.4
   Compiling once_cell v1.10.0
   Compiling bitflags v1.3.2
   Compiling futures-io v0.3.21
   Compiling regex-syntax v0.6.25
   Compiling futures-sink v0.3.21
   Compiling slab v0.4.6
   Compiling futures-channel v0.3.21
   Compiling futures-task v0.3.21
   Compiling futures-util v0.3.21
   Compiling scopeguard v1.1.0
   Compiling pin-utils v0.1.0
   Compiling pkg-config v0.3.25
   Compiling smallvec v1.8.0
   Compiling ppv-lite86 v0.2.16
   Compiling subtle v2.4.1
   Compiling parking_lot_core v0.9.2
   Compiling glob v0.3.0
   Compiling cc v1.0.73
   Compiling unicode-width v0.1.9
   Compiling minimal-lexical v0.2.1
   Compiling ansi_term v0.12.1
   Compiling crossbeam-utils v0.8.8
   Compiling either v1.6.1
   Compiling byteorder v1.4.3
   Compiling bytes v1.1.0
   Compiling termcolor v1.1.3
   Compiling humantime v2.1.0
   Compiling bindgen v0.59.2
   Compiling strsim v0.8.0
   Compiling vec_map v0.8.2
   Compiling lazycell v1.3.0
   Compiling cache-padded v1.2.0
   Compiling proc-macro-hack v0.5.19
   Compiling peeking_take_while v0.1.2
   Compiling async-trait v0.1.53
   Compiling rustc-hash v1.1.0
   Compiling shlex v1.1.0
   Compiling crossbeam-queue v0.3.5
   Compiling fastrand v1.7.0
   Compiling static_assertions v1.1.0
   Compiling parking v2.0.0
   Compiling cpufeatures v0.2.2
   Compiling waker-fn v1.1.0
   Compiling foreign-types-shared v0.1.1
   Compiling tower-layer v0.3.1
   Compiling tower-service v0.3.1
   Compiling openssl v0.10.38
   Compiling hex v0.4.3
   Compiling getrandom v0.1.16
   Compiling untrusted v0.7.1
   Compiling spin v0.5.2
   Compiling base64 v0.13.0
   Compiling event-listener v2.5.2
   Compiling hashbrown v0.11.2
   Compiling assert_matches v1.5.0
   Compiling opaque-debug v0.3.0
   Compiling nix v0.17.0
   Compiling void v1.0.2
   Compiling strsim v0.10.0
   Compiling easy-parallel v3.2.0
   Compiling textwrap v0.15.0
   Compiling async-task v4.2.0
   Compiling sha1_smol v1.0.0
   Compiling match_cfg v0.1.0
   Compiling cfg-if v0.1.10
   Compiling authenticator v0.3.1 (https://github.com/mozilla/authenticator-rs?tag=v0.3.1#660a701b)
   Compiling take_mut v0.2.2
   Compiling serde_json v1.0.79
   Compiling block-padding v0.2.1
   Compiling scoped-tls v1.0.0
   Compiling runloop v0.1.0
   Compiling futures v0.1.31
   Compiling numtoa v0.1.0
   Compiling itoa v1.0.1
   Compiling ryu v1.0.9
   Compiling libloading v0.7.3
   Compiling tracing-core v0.1.25
   Compiling sharded-slab v0.1.4
   Compiling foreign-types v0.3.2
   Compiling concurrent-queue v1.2.2
   Compiling itertools v0.10.3
   Compiling sha1 v0.6.1
   Compiling textwrap v0.11.0
   Compiling getopts v0.2.21
   Compiling async-lock v2.5.0
   Compiling num-traits v0.2.14
   Compiling memoffset v0.6.5
   Compiling lock_api v0.4.7
   Compiling num-integer v0.1.44
   Compiling crossbeam-epoch v0.9.8
   Compiling num-bigint v0.4.3
   Compiling indexmap v1.8.1
   Compiling num-bigint v0.3.3
   Compiling num-iter v0.1.42
   Compiling num-rational v0.3.2
   Compiling thread_local v1.1.4
   Compiling generic-array v0.14.5
   Compiling clang-sys v1.3.1
   Compiling libudev-sys v0.1.4
   Compiling ordered-stream v0.0.1
   Compiling async-broadcast v0.3.4
   Compiling async-channel v1.6.1
   Compiling tracing-log v0.1.2
   Compiling openssl-sys v0.9.72
   Compiling ring v0.16.20
   Compiling aho-corasick v0.7.18
   Compiling nom v7.1.1
   Compiling futures-lite v1.12.0
   Compiling os_str_bytes v6.0.0
   Compiling crossbeam-channel v0.5.4
   Compiling quote v1.0.18
   Compiling regex-automata v0.1.10
   Compiling async-executor v1.4.1
   Compiling regex v1.5.5
   Compiling crossbeam-deque v0.8.1
   Compiling pkg-version-impl v0.1.1
   Compiling crossbeam v0.8.1
   Compiling socket2 v0.4.4
   Compiling atty v0.2.14
   Compiling num_cpus v1.13.1
   Compiling getrandom v0.2.6
   Compiling mio v0.8.2
   Compiling signal-hook-registry v1.4.0
   Compiling which v4.2.5
   Compiling polling v2.2.0
   Compiling nix v0.23.1
   Compiling users v0.11.0
   Compiling dirs-sys v0.3.7
   Compiling hostname v0.3.1
   Compiling termion v1.5.6
   Compiling clap v2.34.0
   Compiling clap v3.1.8
   Compiling libudev v0.2.0
   Compiling num-complex v0.3.1
   Compiling matchers v0.1.0
   Compiling futures-cpupool v0.1.8
   Compiling directories v4.0.1
   Compiling dirs v4.0.0
   Compiling rand_core v0.5.1
   Compiling rand_core v0.6.3
   Compiling parking_lot v0.12.0
   Compiling rand_chacha v0.2.2
   Compiling rand_chacha v0.3.1
   Compiling pkg-version v1.0.0
   Compiling async-io v1.6.0
   Compiling nb-connect v1.2.0
   Compiling rand v0.7.3
   Compiling env_logger v0.9.0
   Compiling rand v0.8.5
   Compiling cexpr v0.6.0
   Compiling nanoid v0.4.0
   Compiling block-buffer v0.10.2
   Compiling crypto-common v0.1.3
   Compiling digest v0.9.0
   Compiling cipher v0.2.5
   Compiling crypto-mac v0.10.1
   Compiling block-buffer v0.9.0
   Compiling digest v0.10.3
   Compiling hmac v0.10.1
   Compiling sha2 v0.9.9
   Compiling aes-soft v0.6.4
   Compiling block-modes v0.7.0
   Compiling hkdf v0.10.0
   Compiling sha2 v0.10.2
   Compiling hmac v0.12.1
   Compiling aes v0.6.0
   Compiling num v0.3.1
   Compiling test-authenticator v0.1.0 (/home/build2/softu2f-src/rust-u2f/linux/test-authenticator)
   Compiling thiserror-impl v1.0.30
   Compiling futures-macro v0.3.21
   Compiling tracing-attributes v0.1.20
   Compiling tokio-macros v1.7.0
   Compiling pin-project-internal v1.0.10
   Compiling enumflags2_derive v0.7.4
   Compiling serde_repr v0.1.7
   Compiling enumflags2_derive v0.6.4
   Compiling enum-ordinalize v3.1.11
   Compiling derivative v2.2.0
   Compiling async-recursion v0.3.2
   Compiling uhid-sys v1.0.0 (/home/build2/softu2f-src/rust-u2f/linux/uhid-sys)
   Compiling educe v0.4.19
   Compiling tokio v1.17.0
   Compiling pin-project v1.0.10
   Compiling thiserror v1.0.30
   Compiling tracing v0.1.33
   Compiling tracing-subscriber v0.3.11
   Compiling tracing-journald v0.2.4
   Compiling futures-executor v0.3.21
   Compiling futures v0.3.21
   Compiling tower v0.4.12
   Compiling tokio-util v0.7.1
   Compiling tokio-tower v0.6.0
   Compiling tokio-linux-uhid v0.4.2 (/home/build2/softu2f-src/rust-u2f/linux/uhid-tokio)
error: reference to packed field is unaligned
   --> linux/uhid-tokio/src/codec.rs:230:31
    |
230 |                 let payload = &event.u.start;
    |                               ^^^^^^^^^^^^^^
    |
    = note: `#[deny(unaligned_references)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
    = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
    = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

warning: associated function `project_ref` is never used
  --> linux/uhid-tokio/src/event_framed.rs:53:1
   |
53 | #[pin_project]
   | ^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `tokio-linux-uhid` (lib) generated 1 warning
error: could not compile `tokio-linux-uhid` due to 2 previous errors; 1 warning emitted
warning: build failed, waiting for other jobs to finish...
make: *** [Makefile:11: build] Error 101

On this commit:

[build2@cb90fada0e7d linux]$ git describe --always --abbrev=600
63777c6c5901045d57c272ca53d58a10bb1680c1

which is the current HEAD: 63777c6c5901045d57c272ca53d58a10bb1680c1

but I've seen it on both 45a073f9283026dcf74f99536d0f2899b6caee99 and ed469b742cdd6465664e749378c6abbf088dff6b.

I'm really not familiar with Rust but can I assume this has something to do with Arch's Rust being even more bleeding edge, and therefore stricter, than Ubuntu's? And since the problem is in a dependency, fixing it means bumping that dependency's version?

kousu commented 1 year ago

Oh I see, the code it's complaining about is an embedded package:

https://github.com/danstiner/rust-u2f/blob/63777c6c5901045d57c272ca53d58a10bb1680c1/linux/uhid-tokio/src/codec.rs#L229-L234

here's Rust's tracking issue including remedies: https://github.com/rust-lang/rust/issues/82523

kousu commented 1 year ago

I see, that code is trying to cast an integer into a bitfield!-macro generated type and it's just a bit awkward. There's an API for this -- .from_bits().

There's a lot of uses of mem::transmute scattered over the code, but this was the only one that actually blocked the build. I guess the rest of the fields happen to be word-aligned? I've fixed it in #99, but probably, for consistency, the others should be changed to use from_bits() as well :duck: :duck: :swan:

danstiner commented 1 year ago

Fixed with #100, build should work now with latest rustc and transmute is gone (well except in the sample code, but it's more reasonable there)

Thanks for the catches

kousu commented 1 year ago

Thanks a lot for the quick reply! I feel lucky I was able to snatch your attention. I figured I would slowly finish the patch myself while you found some time to check your notifications. Most of my open source contributions take months. So cheers :clinking_glasses:

I'll go see about getting the AUR package updated now :)

danstiner commented 1 year ago

Thanks for the report, closing this out. Latest master should build.