Skip to content

parse_header constructs an unaligned reference #79

@Freax13

Description

@Freax13

Running cargo +nightly miri test causes the following error:

Crash log
Preparing a sysroot for Miri (target: x86_64-unknown-linux-gnu)... done
  Finished test [unoptimized + debuginfo] target(s) in 0.01s
   Running unittests src/lib.rs (target/miri/x86_64-unknown-linux-gnu/debug/deps/xmas_elf-3090fb77e8659e43)

running 1 test
test test::interpret_class ... error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
 --> /home/freax13/.cargo/registry/src/github.com-1ecc6299db9ec823/zero-0.1.2/src/lib.rs:107:5
  |
107 |     mem::transmute(input as *const [u8] as *const u8 as *const T)
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
  |
  = 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 `zero::read_unsafe::<header::HeaderPt2_<u32>>` at /home/freax13/.cargo/registry/src/github.com-1ecc6299db9ec823/zero-0.1.2/src/lib.rs:107:5
  = note: inside `zero::read::<header::HeaderPt2_<u32>>` at /home/freax13/.cargo/registry/src/github.com-1ecc6299db9ec823/zero-0.1.2/src/lib.rs:42:9
note: inside `header::parse_header` at src/header.rs:27:17
 --> src/header.rs:27:17
  |
27  |                 read(&input[size_pt1..size_pt1 + mem::size_of::<HeaderPt2_<P32>>()]);
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `ElfFile::<'_>::new` at src/lib.rs:50:22
 --> src/lib.rs:50:22
  |
50  |         let header = header::parse_header(input)?;
  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `test::interpret_class` at src/lib.rs:196:17
 --> src/lib.rs:196:17
  |
196 |         assert!(ElfFile::new(&mk_elf_header(1)).is_ok());
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside closure at src/lib.rs:194:5
 --> src/lib.rs:194:5
  |
193 |       #[test]
  |       ------- in this procedural macro expansion
194 | /     fn interpret_class() {
195 | |         assert!(ElfFile::new(&mk_elf_header(0)).is_err());
196 | |         assert!(ElfFile::new(&mk_elf_header(1)).is_ok());
197 | |         assert!(ElfFile::new(&mk_elf_header(2)).is_ok());
198 | |         assert!(ElfFile::new(&mk_elf_header(42u8)).is_err());
199 | |     }
  | |_____^
  = 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

error: test failed, to rerun pass `--lib`

Caused by:
process didn't exit successfully: `/home/freax13/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner /home/freax13/code/xmas-elf/target/miri/x86_64-unknown-linux-gnu/debug/deps/xmas_elf-3090fb77e8659e43` (exit status: 1)

The unaligned reference is created by the zero dependency. This dependency has already been patched to panic on unaligned references (nrc/zero@1d571c9), but there's no release containing this patch.

We probably want to prevent both UB and panics; I'm not sure what other parts of the code suffer from similar problems. Worst case scenario, we can probably put #[repr(packed)] on all the structs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions